Preorder: Marks pre-orders as shippable when more stock is added

main
Ashelyn Dawn 3 years ago
parent ca38eb7086
commit 32470a6201

@ -84,7 +84,7 @@ function formatTime(time){
}
function ShippingStatus({order, totalShipping, delivery, isAdmin}) {
const preorderDates = order.transactions.filter(t => t.has_preorder).map(t => DateTime.fromISO(t.preorder_fulfill_date))
const preorderDates = order.transactions.filter(t => t.has_preorder && !t.preorder_ready_to_ship).map(t => DateTime.fromISO(t.preorder_fulfill_date))
preorderDates.sort((b, a) => a < b ? -1 : a > b ? 1 : 0)
const latestPreorder = preorderDates[0]?.toLocaleString(DateTime.DATE_FULL)

@ -1131,11 +1131,87 @@ begin
'shipment',
_shipment_uuid
);
perform sos.mark_preorders_shippable(_item_uuids[i], _counts[i]);
end loop;
return query select * from sos.v_shipment where shipment_uuid = _shipment_uuid;
end; $function$;
create or replace function sos.mark_preorders_shippable(_item_uuid uuid, _count integer)
returns void
language plpgsql
as $function$
declare
_preorder_uuids uuid[];
_num_marked integer := 0;
_current_num integer;
_current_direction sos.stock_change_dir_enum;
_remaining_preorders integer;
begin
-- Get unfulfilled pre-order stockchanges for this item, ordered by transaction completion time
select array_agg(item_stockchange.stockchange_uuid order by transaction_completion_time) into _preorder_uuids
from sos.item_stockchange
left join sos.item_stockchange_preorder on item_stockchange.stockchange_uuid = item_stockchange_preorder.stockchange_uuid
left join sos.transaction on stockchange_transaction_uuid = transaction_uuid
where stockchange_item_uuid = _item_uuid
and item_stockchange.stockchange_type = 'preorder'
and stockchange_preorder_ready_to_ship = false
group by stockchange_item_uuid;
-- For all stockchanges
if _preorder_uuids is not null then
for i in array_lower(_preorder_uuids, 1) .. array_upper(_preorder_uuids, 1) loop
select
stockchange_direction,
stockchange_change
into
_current_direction,
_current_num
from sos.item_stockchange
where stockchange_uuid = _preorder_uuids[i];
-- Sanity check: Only fulfill ones that deducted stock
if _current_direction != 'subtracted' then
continue;
end if;
-- Don't fulfill more than we have
if _num_marked + _current_num > _count then
exit;
end if;
update sos.item_stockchange_preorder set
stockchange_preorder_ready_to_ship = true
where stockchange_uuid = _preorder_uuids[i];
_num_marked := _num_marked + _current_num;
end loop;
end if;
-- Count remaining pre-orders
select count(item_stockchange.stockchange_uuid) into _remaining_preorders
from sos.item_stockchange
left join sos.item_stockchange_preorder on item_stockchange.stockchange_uuid = item_stockchange_preorder.stockchange_uuid
where stockchange_item_uuid = _item_uuid
and item_stockchange.stockchange_type = 'preorder'
and stockchange_preorder_ready_to_ship = false
group by stockchange_item_uuid;
-- If we no longer have pre-orders clear item pre-order records
if _remaining_preorders < 1 then
update sos.item
set (
item_preorder_availability_date,
item_preorder_maximum
) = (
null,
null
)
where item_uuid = _item_uuid;
end if;
end; $function$;
create or replace function sos.set_delivery_tracking(_order_uuid uuid, _tracking_number text, _date_shipped timestamptz, _price_cents integer)
returns setof sos.v_order
language plpgsql

Loading…
Cancel
Save