summaryrefslogtreecommitdiff
path: root/sql/modules/Inventory.sql
blob: fa4d6d0e98a82b5195c02bafa0d352ebafed0f83 (plain)
  1. CREATE OR REPLACE FUNCTION inventory_get_item_at_day
  2. (in_transdate date, in_partnumber text)
  3. RETURNS parts AS
  4. $$
  5. DECLARE out_row parts%ROWTYPE;
  6. BEGIN
  7. SELECT p.id, p.partnumber, p.description, p.unit, p.listprice,
  8. p.sellprice, p.lastcost, p.priceupdate, p.weight,
  9. p.onhand - sum(coalesce(i.qty, 0))
  10. AS onhand, p.notes, p.makemodel, p.assembly, p.alternate,
  11. p.rop, p.inventory_accno_id, p.income_accno_id, p.expense_accno_id,
  12. p.bin, p.obsolete, p.bom, p.image, p.microfiche, p.partsgroup_id,
  13. p.project_id, p.avgcost
  14. INTO out_row;
  15. FROM parts p
  16. LEFT JOIN invoice i ON (i.parts_id = p.id
  17. AND i.trans_id IN
  18. (select id FROM ar WHERE transdate > in_trans_date
  19. UNION
  20. SELECT id FROM ap WHERE transdate > in_trans_date))
  21. WHERE p.partnumber = in_partnumber
  22. AND assembly IS FALSE AND obsolete IS NOT TRUE
  23. GROUP BY p.id, p.partnumber, p.description, p.unit, p.listprice,
  24. p.sellprice, p.lastcost, p.priceupdate, p.weight,
  25. p.onhand, p.notes, p.makemodel, p.assembly, p.alternate,
  26. p.rop, p.inventory_accno_id, p.income_accno_id, p.expense_accno_id,
  27. p.bin, p.obsolete, p.bom, p.image, p.microfiche, p.partsgroup_id,
  28. p.project_id, p.avgcost;
  29. RETURN out_row;
  30. END;
  31. $$ LANGUAGE PLPGSQL;
  32. CREATE OR REPLACE FUNCTION inventory_create_report(in_transdate date) RETURNS int
  33. AS
  34. $$
  35. BEGIN
  36. INSERT INTO inventory_report(entry_date) values (in_transdate);
  37. RETURN currval('inventory_report_id_seq');
  38. END;
  39. $$ language plpgsql;
  40. CREATE OR REPLACE FUNCTION inventory_report__add_line
  41. (in_report_id int, in_parts_id int, in_onhand int, in_counted int)
  42. RETURNS int AS
  43. $$
  44. BEGIN
  45. INSERT INTO inventory_report_line(report_id, parts_id, onhand, counted)
  46. VALUES (in_report_id, in_parts_id, in_onhand, in_counted);
  47. RETURN currval('inventory_report_line_id_seq');
  48. $$ LANGUAGE plpgsql;