oracl storage function, update the inventory

create procedure PRO_update_Goods_group_stock is
  v_min_stock NUMBER(10, 2);
  v_gg_stock  NUMBER(10, 2);
  v_goods_no  number;
  v_sum_cost  NUMBER(10, 2);
begin
  for g_goods_no in (select ggi.goods_no
                       from goods_group_item ggi
                      where ggi.is_delete = 'N'
                      The GROUP  by ggi.goods_no) Loop
     the SELECT  COUNT ( * )
       INTO v_goods_no
       from goods_no_stock GN
      the WHERE gn.goods_no = g_goods_no.goods_no;
     IF v_goods_no >  0  the then 
      the Continue ;
     End  IF ; 

    - Search Stock divided by the combination of sub-items bound taking the minimum number of sub-gg g product main item 
    SELECT  min (TRUNC (g.available_stock / 
                     decode (gg1.group_goods_amount, 
                            0 ,
                             . 1 ,
                            gg1.group_goods_amount),
                     0))
      into v_min_stock
      from goods_group_item gg1, goods g, goods gg
     where gg1.goods_no = g_goods_no.goods_no
       and gg1.is_delete = 'N'
       and gg.goods_id = gg1.goods_id
       and gg.is_delete = 'N'
       and g.goods_id = gg1.GROUP_GOODS_ID;

    --计算成本价格    gg主商品 g子商品
    select trunc(sum(g.cost_price * gg1.group_goods_amount), 2)
      into v_sum_cost
      from goods_group_item gg1, goods g, goods gg
     where gg1.goods_no = g_goods_no.goods_no
       and gg1.is_delete = 'N'
       and gg.goods_id = gg1.goods_id
       and gg.is_delete = 'N'
       and g.goods_id = gg1.GROUP_GOODS_ID;

    update goods
       set available_stock = v_min_stock, COST_PRICE = v_sum_cost
     where goods_no = g_goods_no.goods_no
       and is_delete = 'N';
    commit;
  end loop;

  --下架问题组合码
  insert into sys_user_message
    (MESSAGE_ID,
     PRODUCT_CODE,
     TARGET_MODUL_ID,
     MESSAGE_BODY,
     MESSAGE_STAGE,
     IS_DELETE)
    select sys_user_message_seq.nextval,
           p.product_code,
           254,
           '商品:'||p.product_code||' 没有维护组合码',
           '0',
           'N'
      from product p
     where not exists (select 1
              from goods_group_item ggi
             where ggi.goods_no = p.product_code)
       and p.is_delete = 'N'
       and p.is_onsale = 'Y'
       and p.product_code like 'A%';

     update product p set p.is_onsale = 'N'
     where not exists (select 1
              from goods_group_item ggi
             where ggi.goods_no = p.product_code)
      and p.is_delete = 'N'
       and p.is_onsale = 'Y'
       and p.product_code like 'A%';

 commit;
end;

select *

      from goods_no_stock gn
     where gn.goods_no in  (select ggi.goods_no
                       from goods_group_item ggi
                      where ggi.is_delete = 'N'
                      GROUP by ggi.goods_no);


select  * from goods g where  g.GOODS_NO='02-00056';


  select  * from goods g where g.GOODS_NO='70261507';


select  * from goods g where g.goods_no='70261506';


select gn.goods_no

      from goods_no_stock gn;
goods_no_stock: Inventory synchronization blacklist, the data in this table are not related update operation
 

 

Guess you like

Origin www.cnblogs.com/wangchuanfu/p/10981193.html