Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. -- Function: xt.distribute(int, numeric, int, text)
  2.  
  3. -- DROP FUNCTION xt.distribute(int, numeric, int, text);
  4.  
  5. CREATE OR REPLACE FUNCTION xt.distribute(int, numeric, int, text)
  6. RETURNS integer AS
  7. $BODY$
  8. -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
  9. -- See www.xtuple.com/CPAL for the full text of the software license.
  10. DECLARE
  11. pItemlocSeries ALIAS FOR $1;
  12. pQty ALIAS FOR $2;
  13. pLocationId ALIAS FOR $3;
  14. pLot ALIAS FOR $4;
  15. -- pExpiration ALIAS FOR $4;
  16. -- pWarranty ALIAS FOR $5;
  17. _info RECORD;
  18. _distId INTEGER;
  19. _traceSeries INTEGER;
  20. _result INTEGER;
  21.  
  22. BEGIN
  23.  
  24. IF (pItemlocSeries < 0) THEN
  25. RAISE EXCEPTION 'pItemlocSeries must be >= 0';
  26. END IF;
  27.  
  28. /* Check if itemlocdist record(s) already exist */
  29. SELECT itemlocdist_id,
  30. invhist_id,
  31. invhist_invqty,
  32. invhistsense(invhist_id) as sense,
  33. itemsite_id,
  34. itemsite_controlmethod,
  35. itemsite_loccntrl,
  36. count(*) AS rows INTO _info
  37. FROM itemlocdist,
  38. invhist
  39. JOIN itemsite on itemsite_id = invhist_itemsite_id
  40. WHERE itemlocdist_series = pItemlocSeries
  41. AND invhist_series = pItemlocSeries
  42. GROUP BY itemlocdist_id,
  43. invhist_id,
  44. invhist_invqty,
  45. itemsite_id,
  46. itemsite_controlmethod,
  47. itemsite_loccntrl;
  48.  
  49.  
  50. RAISE NOTICE '_info.rows: %', _info.rows;
  51.  
  52. /* We shouldn't have detail if there are no detail control settings turned on */
  53. IF (_info.rows IS NULL) THEN
  54. RAISE EXCEPTION 'No records found that are able to be distributed [xtuple: xt.distribute, -1]';
  55. ELSE IF (_info.rows > 0) THEN
  56. RAISE EXCEPTION 'Only distribute for one transaction at a time is supported [xtuple: xt.distribute, -2].';
  57. END IF;
  58.  
  59. /* Gather distribution detail */
  60.  
  61. /* Validate quantity */
  62.  
  63. /* Loop through and handle each trace detail */
  64. IF ((_info.itemsite_controlmethod = 'L') OR (_info.itemsite_controlmethod = 'S')) THEN
  65. SELECT nextval('itemloc_series_seq') INTO _traceSeries;
  66.  
  67. IF (_info.itemsite_controlmethod = 'S') THEN
  68. SELECT ls_id FROM (
  69. SELECT ls_id
  70. FROM itemloc join ls on itemloc_ls_id = ls_id
  71. WHERE ls_number = pLot
  72. AND itemloc_itemsite_id = _info.itemsite_id
  73. UNION ALL
  74. SELECT ls_id
  75. FROM itemlocdist join ls on itemlocdist_ls_id = ls_id
  76. WHERE ls_number = pLot
  77. AND itemlocdist_itemsite_id = _info.itemsite_id
  78. ) AS query;
  79.  
  80. IF (FOUND AND pQty = 1) THEN
  81. RAISE EXCEPTION 'Serial number % already exists in inventory.', pLot;
  82. ELSE IF (NOT FOUND AND (pQty = -1)) THEN
  83. RAISE EXCEPTION 'Serial number does not exist in inventory.';
  84. ELSE IF (pQty != -1 AND (pQty != 1)) THEN
  85. RAISE EXCEPTION 'Serial number quantity must be one.';
  86. END IF; --pQty with series error handling
  87. END IF; --serial item
  88.  
  89. SELECT createlotserial(pLot, _traceSeries, pQty, NULL::DATE, NULL::DATE, _info.itemlocdist_id) INTO _distId
  90. FROM itemlocdist
  91. WHERE (itemlocdist_id=_info.itemlocdist_id);
  92.  
  93. UPDATE itemlocdist
  94. SET itemlocdist_source_type = 'L',
  95. itemlocdist_source_id = pLocationId
  96. WHERE itemlocdist_id = _distId;
  97.  
  98. DELETE FROM itemlocdist WHERE itemlocdist_id = _info.itemlocdist_id;
  99.  
  100. SELECT distributeitemlocseries(_traceSeries);
  101.  
  102. ELSE -- Location control without lot/serial
  103. INSERT INTO itemlocdist (itemlocdist_itemlocdist_id, itemlocdist_source_type,
  104. itemlocdist_source_id, itemlocdist_itemsite_id, itemlocdist_expiration, itemlocdist_qty,
  105. itemlocdist_series, itemlocdist_invhist_id )
  106. VALUES (_info.itemlocdist_id, 'L', pLocationId, pQty, endoftime(), _info.itemsite_id, pItemlocSeries, _info.invhist_id);
  107.  
  108. SELECT distributeitemlocseries(pItemlocSeries);
  109. END IF;
  110.  
  111. -- Wrap up
  112. SELECT postitemlocseries(pItemLocSeries);
  113.  
  114. RETURN NULL;
  115.  
  116. END;
  117. $BODY$
  118. LANGUAGE plpgsql VOLATILE
  119. COST 100;
  120. ALTER FUNCTION xt.distribute(int, numeric, int, text)
  121. OWNER TO admin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement