Browse Source

preparing to make items available as document positions

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 4 hours ago
parent
commit
438d8d4aad
  1. 16
      frontend/src/Components/Item.svelte
  2. 14
      frontend/src/routes/document/ItemList.svelte
  3. 16
      stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java
  4. 1
      stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java
  5. 13
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

16
frontend/src/Components/Item.svelte

@ -4,6 +4,18 @@ @@ -4,6 +4,18 @@
<fieldset {onclick}>
<legend>{item.code} | {item.name}</legend>
<div>{@html item.description.rendered}</div>
<span>{item.unit_price/100} {item.currency} / {item.unit}</span>
{#if item.properties}
<table>
<tbody>
{#each Object.entries(item.properties) as [idx,prop]}
<tr>
<th>{prop.name}</th>
<td>{prop.value} {prop.unit}</td>
</tr>
{/each}
</tbody>
</table>
{/if}
<!--<div>{@html item.description.rendered}</div> -->
<!-- <span>{item.unit_price/100} {item.currency} / {item.unit}</span> -->
</fieldset>

14
frontend/src/routes/document/ItemList.svelte

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
<script>
import { onMount } from 'svelte';
import { api } from '../../urls.svelte.js';
import { api, post } from '../../urls.svelte.js';
import { error, yikes } from '../../warn.svelte';
import { t } from '../../translations.svelte.js';
@ -15,13 +15,9 @@ @@ -15,13 +15,9 @@
let items = $state(null);
async function loadItems(){
const url = api('items/list');
let data = { company_id: company_id };
const resp = await fetch(url,{
credentials: 'include',
method : 'POST',
body : JSON.stringify(data)
});
const url = api('stock/list');
let data = { company_id };
const resp = await post(url,data);
if (resp.ok){
items = await resp.json();
yikes();
@ -37,7 +33,7 @@ @@ -37,7 +33,7 @@
<div>
<h1>{t('items')}</h1>
{#if items}
{#each items as item,id}
{#each items as item,idx}
<Item item={item} onclick={() => onSelect(item)} />
{/each}
{/if}

16
stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java

@ -218,6 +218,22 @@ public class SqliteDb extends BaseDb implements StockDb { @@ -218,6 +218,22 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public Collection<Item> listItemsOf(Company company) {
try {
var owner = company.dbCode();
var rs = select(ALL).from(TABLE_ITEMS).where(OWNER,equal(owner)).exec(db);
var list = new ArrayList<Item>();
while (rs.next()) list.add(Item.of(rs));
rs.close();
for (var item : list) loadProperties(item);
return list;
} catch (SQLException e){
throw databaseException("Failed to load items of {0}",company);
}
}
@Override
public Item loadProperties(Item item){
try {

1
stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java

@ -13,6 +13,7 @@ public interface StockDb { @@ -13,6 +13,7 @@ public interface StockDb {
Collection<DbLocation> listChildLocations(long parentId);
Collection<DbLocation> listCompanyLocations(Company company);
Collection<Item> listItemsAt(Location location);
Collection<Item> listItemsOf(Company company);
Collection<Property> listProperties();
Collection<DbLocation> listUserLocations(UmbrellaUser userId);
Item loadItem(long id);

13
stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

@ -8,6 +8,8 @@ import static de.srsoftware.umbrella.core.Constants.*; @@ -8,6 +8,8 @@ import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Field.ITEM;
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.Paths.LIST;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
import static de.srsoftware.umbrella.stock.Constants.*;
import static java.lang.System.Logger.Level.WARNING;
@ -33,6 +35,7 @@ import org.json.JSONObject; @@ -33,6 +35,7 @@ import org.json.JSONObject;
public class StockModule extends BaseHandler implements StockService {
private final StockDb stockDb;
private Comparator<Item> byName = (a,b) -> a.name().compareToIgnoreCase(b.name());
public StockModule(Configuration config) throws UmbrellaException {
super();
@ -160,6 +163,7 @@ public class StockModule extends BaseHandler implements StockService { @@ -160,6 +163,7 @@ public class StockModule extends BaseHandler implements StockService {
var head = path.pop();
return switch (head) {
case ITEM -> postItem(user.get(), ex);
case LIST -> postItemList(user.get(), path, ex);
case LOCATION -> postLocation(user.get(),ex);
case PROPERTY -> postProperty(user.get(),ex);
case null, default -> super.doPost(path,ex);
@ -316,6 +320,15 @@ public class StockModule extends BaseHandler implements StockService { @@ -316,6 +320,15 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,stockDb.save(newItem));
}
private boolean postItemList(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
var json = json(ex);
if (!json.has(COMPANY_ID) || !(json.get(COMPANY_ID) instanceof Number company_id)) throw missingFieldException(COMPANY_ID);
var company = companyService().get(company_id.longValue());
if (!companyService().membership(company_id.longValue(),user.id())) throw forbidden("You are not a member of {0}!", company.name());
var items = stockDb.listItemsOf(company);
return sendContent(ex,items.stream().sorted(byName).map(Item::toMap));
}
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);

Loading…
Cancel
Save