RETURNS integer
as
$$
declare
rec record;
begin
update stockcurrent set units=units+(select handqty from shipmentinline sp inner join stockcurrent st on st.product=sp.product where sp.documentno=$1);
for rec in (select sp.location,sp.handqty,sp.product from shipmentinline sp where sp.documentno=sp_documentno and not exists(select handqty from shipmentinline sp inner join stockcurrent st on st.product=sp.product where sp.documentno=$1)) loop
insert into stockcurrent(location,product,attributesetinstance_id,units)values(rec.location,rec.product,null,rec.handqty);
end loop;
end;
$$
language 'plpgsql' VOLATILE;
select f_m_stockcurrent_post('123');