preparing to make items available as document positions
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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 @@
|
||||
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 @@
|
||||
<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}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
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 {
|
||||
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 {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user