|
|
|
@ -745,3 +745,64 @@ begin
|
|
|
|
|
|
|
|
|
|
return query select * from sos.v_session where session_uuid = _session_uuid;
|
|
|
|
|
end; $function$;
|
|
|
|
|
|
|
|
|
|
create or replace function sos.deduct_stock_for_purchase(_transaction_uuid uuid)
|
|
|
|
|
returns setof sos.v_item
|
|
|
|
|
language plpgsql
|
|
|
|
|
as $function$
|
|
|
|
|
declare
|
|
|
|
|
_item_uuids uuid[];
|
|
|
|
|
_cart_uuid uuid;
|
|
|
|
|
_item_uuid uuid;
|
|
|
|
|
_item_count integer;
|
|
|
|
|
_stockchange_uuid uuid;
|
|
|
|
|
begin
|
|
|
|
|
-- Get cart uuid (and check completed)
|
|
|
|
|
select transaction_cart_uuid into _cart_uuid
|
|
|
|
|
from sos."transaction"
|
|
|
|
|
where transaction_uuid = _transaction_uuid
|
|
|
|
|
and transaction_payment_state = 'completed';
|
|
|
|
|
|
|
|
|
|
-- Double check we got the cart
|
|
|
|
|
-- (Will be null if transaction is not complete)
|
|
|
|
|
if _cart_uuid is null then
|
|
|
|
|
raise 'Transaction not complete';
|
|
|
|
|
end if;
|
|
|
|
|
|
|
|
|
|
-- Get item array
|
|
|
|
|
_item_uuids := ARRAY(
|
|
|
|
|
select cart_item_item_uuid from sos."cart_item"
|
|
|
|
|
where cart_item_cart_uuid = _cart_uuid
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
-- Make stock-change record for each item
|
|
|
|
|
foreach _item_uuid in array _item_uuids loop
|
|
|
|
|
select cart_item_count into _item_count from sos."cart_item"
|
|
|
|
|
where cart_item_cart_uuid = _cart_uuid
|
|
|
|
|
and cart_item_item_uuid = _item_uuid;
|
|
|
|
|
|
|
|
|
|
insert into sos."item_stockchange" (
|
|
|
|
|
stockchange_type,
|
|
|
|
|
stockchange_item_uuid,
|
|
|
|
|
stockchange_change,
|
|
|
|
|
stockchange_direction
|
|
|
|
|
) values (
|
|
|
|
|
'purchase',
|
|
|
|
|
_item_uuid,
|
|
|
|
|
_item_count,
|
|
|
|
|
'subtracted'
|
|
|
|
|
) returning stockchange_uuid into _stockchange_uuid;
|
|
|
|
|
|
|
|
|
|
insert into sos."item_stockchange_purchase" (
|
|
|
|
|
stockchange_uuid,
|
|
|
|
|
stockchange_type,
|
|
|
|
|
stockchange_transaction_uuid
|
|
|
|
|
) values (
|
|
|
|
|
_stockchange_uuid,
|
|
|
|
|
'purchase',
|
|
|
|
|
_transaction_uuid
|
|
|
|
|
);
|
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
|
|
return query select * from sos.v_item where item_uuid = any(_item_uuids);
|
|
|
|
|
end; $function$;
|
|
|
|
|