1、该函数的作用:入库单提交/审核通过时,将相关数据插到/更新‘出入库历史记录表’:delivery_entering_record
由于页面端屏蔽了单价,指定的用户才能看到,故 与单价有关的数据插不进“出入库历史记录表”delivery_entering_record中,所以要用后台的用户替换当前用户,执行完相关语句后,在还原为当前用户(如代码:178~184和第208行所示);否则,插进去的数据全为0。
1 create or replace function delivery_ew_record_insert(p_ew_code VARCHAR2, 2 p_action VARCHAR2, 3 p_wf_node_code VARCHAR2 4 ) return VARCHAR2 AS 5 6 -- 作用:入库单提交/审核通过时,将相关数据插到/更新‘出入库历史记录表’:delivery_entering_record 7 v_ew_code entering_warehouse.ew_code%TYPE; 8 v_ew_type entering_warehouse.ew_type%TYPE; 9 v_ew_date entering_warehouse.ew_date%TYPE; 10 v_material_code ew_detail.material_code%TYPE; 11 v_region ew_detail.region%TYPE; 12 v_wh_code ew_detail.wh_code%TYPE; 13 v_fs_code ew_detail.fs_code%TYPE; 14 v_actual_ew_qty ew_detail.actual_ew_qty%TYPE; 15 v_unit_price ew_detail.unit_price%TYPE; 16 v_seq NUMBER; 17 v_ewdo_code VARCHAR2(30);
-- 与“出入库历史记录表”:BEFOR_QTY、AFTER_QTY、BEFOR_PRICE、AFTER_PRICE,有关的函数,在这三条函数更新数量或单价之前和之后,分别将库存表中的更新前后的数量及单价插入到“出入库历史记录表”的相应字段上 -- 18 -- -- ew_stocking_process2 与入库单数量相关的函数 19 v_ew_do_code delivery_entering_record.ew_do_code%TYPE; -- inventory_price_update 与出库单价格相关的函数 20 v_next_qty delivery_entering_record.next_qty%TYPE; -- do_stocking_process2 与出库单数量相关的函数 21 v_next_price delivery_entering_record.next_price%TYPE; 22 v_material_code2 delivery_entering_record.material_code%TYPE; 23 v_region2 delivery_entering_record.region%TYPE; 24 v_wh_code2 delivery_entering_record.wh_code%TYPE; 25 v_fs_code2 delivery_entering_record.fs_code%TYPE; 26 27 CURSOR get_parameters IS 28 SELECT ew.ew_code, 29 ew.ew_type, 30 ed.material_code, 31 ed.region, 32 ed.wh_code, 33 ed.fs_code, 34 ed.actual_ew_qty, 35 ed.unit_price, 36 ew.ew_date 37 FROM EW_DETAIL ed,ENTERING_WAREHOUSE ew 38 WHERE ed.ew_code = ew.ew_code AND ew.ew_code = p_ew_code; 39 40 -- 如果‘出入库历史记录表’中,该单已存在,则跟新,不存在,则插入 41 CURSOR get_ew_do_code IS 42 SELECT count(der.ew_do_code) 43 FROM delivery_entering_record der 44 WHERE der.ew_do_code = p_ew_code; 45 46 -- 确定上一单有多少相同的数据(单号),用来更新表:delivery_entering_record 的“下单生效前库存数量”及“下单生效前库存单价”两字段。 47 v_ew_do_code2 VARCHAR2(30); 48 CURSOR get_ewdo_code IS 49 SELECT der.ew_do_code,der.material_code,der.region,der.wh_code,fs_code 50 FROM delivery_entering_record der 51 WHERE der.ew_do_code = (SELECT min(ew_do_code) 52 FROM (select * FROM 53 (select * from delivery_entering_record der order by der.ew_do_code desc) 54 where ROWNUM < (SELECT COUNT(der.ew_do_code) 55 FROM delivery_entering_record der 56 WHERE der.ew_do_code = p_ew_code) + 2)); 57 58 -- 下一单生效前的库存数量及单价 59 CURSOR get_existent_qty_price IS 60 SELECT DISTINCT i.existent_qty,i.price 61 FROM inventory i,ew_detail ew 62 WHERE i.region = ew.region AND i.wh_code = v_wh_code2 63 AND i.material_code = v_material_code2 64 AND i.fs_code = v_fs_code2 65 AND ew.ew_code = v_ew_do_code2; 66 67 -- 如果该出库单/入库单被删除了,‘出入库历史记录表’的status字段显示失效 68 CURSOR get_ew_do_code2 IS 69 SELECT der.ew_do_code 70 FROM delivery_entering_record der 71 WHERE der.ew_do_code NOT IN (SELECT der.ew_do_code 72 FROM delivery_entering_record der 73 WHERE der.ew_do_code IN (SELECT ew.ew_code FROM entering_warehouse ew) 74 OR der.ew_do_code IN (SELECT do.do_code FROM delivery_order do)); 75 76 -- 审核通过时间 77 v_auditor_audit_date entering_warehouse.auditor_audit_date%TYPE; 78 CURSOR get_auditor_audit_date IS 79 SELECT ew.auditor_audit_date 80 FROM entering_warehouse ew 81 WHERE ew.ew_code = p_ew_code; 82 -- 83 v_appuser varchar2(30); 84 v_backgrounduser varchar2(30); 85 86 BEGIN 87 88 OPEN get_ew_do_code; 89 FETCH get_ew_do_code INTO v_ewdo_code; 90 CLOSE get_ew_do_code; 91 92 -- 入库单不存在(delivery_entering_record 表) 93 IF p_action IN (‘02‘,‘18‘,‘19‘) AND p_wf_node_code = ‘1‘ AND v_ewdo_code = 0 THEN 94 95 OPEN get_parameters; 96 LOOP 97 FETCH get_parameters INTO v_ew_code,v_ew_type,v_material_code,v_region,v_wh_code,v_fs_code,v_actual_ew_qty,v_unit_price,v_ew_date; 98 EXIT WHEN get_parameters%NOTFOUND; 99 100 select dewr_seq.Nextval INTO v_seq from dual; 101 INSERT INTO delivery_entering_record(seq, 102 ew_or_do, 103 ew_do_code, 104 ew_do_type, 105 material_code, 106 region, 107 wh_code, 108 fs_code, 109 ew_do_qty, 110 ew_do_price, 111 ew_do_date, 112 status 113 ) 114 VALUES(v_seq, 115 ‘01‘, 116 v_ew_code, 117 v_ew_type, 118 v_material_code, 119 v_region, 120 v_wh_code, 121 v_fs_code, 122 v_actual_ew_qty, 123 v_unit_price, 124 v_ew_date, 125 ‘01‘ 126 ); 127 END LOOP; 128 CLOSE get_parameters; 129 END IF; 130 -- 入库单存在 (delivery_entering_record 表) 131 IF p_action IN (‘02‘,‘18‘,‘19‘) AND p_wf_node_code = ‘1‘ AND v_ewdo_code <> 0 THEN 132 133 OPEN get_parameters; 134 LOOP 135 FETCH get_parameters INTO v_ew_code,v_ew_type,v_material_code,v_region,v_wh_code,v_fs_code,v_actual_ew_qty,v_unit_price,v_ew_date; 136 EXIT WHEN get_parameters%NOTFOUND; 137 UPDATE delivery_entering_record der SET der.ew_or_do = ‘01‘, 138 der.ew_do_code = v_ew_code, 139 der.ew_do_type = v_ew_type, 140 der.material_code = v_material_code, 141 der.region = v_region, 142 der.wh_code = v_wh_code, 143 der.fs_code = v_fs_code, 144 der.ew_do_qty = v_actual_ew_qty, 145 der.ew_do_price = v_unit_price, 146 der.ew_do_date = v_ew_date, 147 der.status = ‘01‘ 148 WHERE der.ew_do_code = p_ew_code 149 AND der.material_code = v_material_code 150 AND der.wh_code = v_wh_code 151 AND der.fs_code = v_fs_code; 152 END LOOP; 153 CLOSE get_parameters; 154 END IF; 155 156 -- 如果入库单中没有该单,而‘出入库历史记录表’中有,则该单不存在,出入库历史记录表status = ‘00‘ 失效,否则‘01‘ 生效 157 IF p_action IN (‘02‘,‘18‘,‘19‘) AND p_wf_node_code = ‘1‘ THEN 158 OPEN get_ew_do_code2; 159 LOOP 160 FETCH get_ew_do_code2 INTO v_ew_do_code; 161 EXIT WHEN get_ew_do_code2%NOTFOUND; 162 UPDATE delivery_entering_record der SET der.status = ‘00‘ WHERE der.ew_do_code = v_ew_do_code; 163 END LOOP; 164 CLOSE get_ew_do_code2; 165 END IF; 166 167 -- 审核通过时间 168 IF p_action = ‘03‘ AND p_wf_node_code IN (‘2‘,‘3‘,‘21‘) THEN 169 OPEN get_auditor_audit_date; 170 FETCH get_auditor_audit_date INTO v_auditor_audit_date; 171 CLOSE get_auditor_audit_date; 172 173 UPDATE delivery_entering_record der SET der.ew_do_udate = v_auditor_audit_date WHERE der.ew_do_code = p_ew_code; 174 175 END IF; 176 177 178 v_appuser := context_pkg.app_user_name; --取当前用户 179 select para_value 180 into v_backgrounduser 181 from system_parameter 182 where para_name = ‘background_username ‘; --取后台用户 183 184 context_pkg.set_app_user_name(v_backgrounduser); --把当前用户替换为后台用户 185 186 187 -- 下一单生效前的库存数量及单价(插到上一单中 ) 188 IF p_action <> ‘03‘ AND p_wf_node_code <> ‘3‘ THEN 189 OPEN get_ewdo_code; 190 LOOP 191 FETCH get_ewdo_code INTO v_ew_do_code2,v_material_code2,v_region2,v_wh_code2,v_fs_code2; 192 EXIT WHEN get_ewdo_code%NOTFOUND; 193 194 OPEN get_existent_qty_price; 195 FETCH get_existent_qty_price INTO v_next_qty,v_next_price; 196 CLOSE get_existent_qty_price; 197 198 UPDATE delivery_entering_record der 199 SET der.next_qty = nvl(v_next_qty,0),der.next_price = nvl(v_next_price,0) WHERE der.ew_do_code = v_ew_do_code2 200 AND der.material_code = v_material_code2 201 AND der.region = v_region2 202 AND der.wh_code = v_wh_code2 203 AND der.fs_code = v_fs_code2; 204 END LOOP; 205 CLOSE get_ewdo_code; 206 END IF; 207 208 context_pkg.set_app_user_name(v_appuser); --还原当前用户 209 210 RETURN ‘0‘; 211 end delivery_ew_record_insert; 212 213
2、以下是与库存表更新单价前后的函数:inventory_price_update
1 CREATE OR REPLACE FUNCTION inventory_price_update (p_ew_code VARCHAR2, 2 p_wf_node_code VARCHAR2, 3 p_auditor_status VARCHAR2) 4 5 RETURN VARCHAR2 AS 6 7 v_material_code ew_detail.material_code%TYPE; 8 v_region ew_detail.region%TYPE; 9 v_wh_code ew_detail.wh_code%TYPE; 10 v_fs_code ew_detail.fs_code%TYPE; 11 v_specification ew_detail.specification%TYPE; 12 v_manufaturer ew_detail.manufaturer%TYPE; 13 v_partner ew_detail.partner%TYPE; 14 v_unit_price ew_detail.unit_price%TYPE; 15 v_actual_ew_qty ew_detail.actual_ew_qty%TYPE; 16 v_price inventory.price%TYPE; 17 v_existent_qty inventory.existent_qty%TYPE; 18 v_new_price inventory.price%TYPE; 19 20 CURSOR get_ew IS 21 SELECT ed.region, 22 ed.wh_code, 23 ed.fs_code, 24 ed.specification, 25 ed.material_code, 26 ed.unit_price, 27 ed.actual_ew_qty, 28 ed.partner, 29 ed.manufaturer 30 FROM ew_detail ed 31 WHERE ed.ew_code = p_ew_code; 32 33 CURSOR get_inventory IS 34 SELECT DISTINCT i.price,i.existent_qty 35 FROM inventory i 36 WHERE i.material_code = v_material_code 37 AND i.region = v_region 38 AND i.wh_code = v_wh_code 39 AND i.fs_code = v_fs_code 40 AND i.specification = v_specification; 41 --AND i.partner = v_partner 42 --AND i.manufaturer = v_manufaturer; 43 44 -------------------------- 库存表中更新前的库存数量和单价 45 v_region2 ew_detail.region%TYPE; 46 v_wh_code2 ew_detail.wh_code%TYPE; 47 v_fs_code2 ew_detail.fs_code%TYPE; 48 v_material_code2 ew_detail.material_code%TYPE; 49 v_price2 inventory.price%TYPE; 50 51 CURSOR get_ew_detail2 IS 52 SELECT ed.region, 53 ed.wh_code, 54 ed.fs_code, 55 ed.material_code 56 FROM ew_detail ed 57 WHERE ed.ew_code = p_ew_code; 58 59 CURSOR get_existent_qty2 IS 60 SELECT DISTINCT i.price 61 FROM INVENTORY i,delivery_entering_record DER 62 WHERE DER.EW_DO_CODE = p_ew_code 63 AND i.MATERIAL_CODE = v_material_code2 64 AND i.REGION = v_region2 65 AND i.WH_CODE = v_wh_code2 66 AND i.FS_CODE = v_fs_code2; 67 -- 68 v_region3 ew_detail.region%TYPE; 69 v_wh_code3 ew_detail.wh_code%TYPE; 70 v_fs_code3 ew_detail.fs_code%TYPE; 71 v_material_code3 ew_detail.material_code%TYPE; 72 v_price3 inventory.price%TYPE; 73 74 CURSOR get_ew_detail3 IS 75 SELECT ed.region, 76 ed.wh_code, 77 ed.fs_code, 78 ed.material_code 79 FROM ew_detail ed 80 WHERE ed.ew_code = p_ew_code; 81 82 CURSOR get_existent_qty3 IS 83 SELECT DISTINCT i.price 84 FROM INVENTORY i,delivery_entering_record DER 85 WHERE DER.EW_DO_CODE = p_ew_code 86 AND i.MATERIAL_CODE = v_material_code3 87 AND i.REGION = v_region3 88 AND i.WH_CODE = v_wh_code3 89 AND i.FS_CODE = v_fs_code3; 90 91 -- 92 v_appuser varchar2(30); 93 v_backgrounduser varchar2(30); 94 95 BEGIN 96 97 v_appuser := context_pkg.app_user_name; --取当前用户 98 select para_value 99 into v_backgrounduser 100 from system_parameter 101 where para_name = ‘background_username ‘; --取后台用户 102 103 context_pkg.set_app_user_name(v_backgrounduser); --把当前用户替换为后台用户 104 105 106 -- 库存表中更新前的库存数量和单价 create by denghaibin 2018-01-11 107 IF p_wf_node_code = 3 AND p_auditor_status =‘03‘ THEN 108 OPEN get_ew_detail2; 109 LOOP 110 FETCH get_ew_detail2 INTO v_region2,v_wh_code2,v_fs_code2,v_material_code2; 111 EXIT WHEN get_ew_detail2%NOTFOUND; 112 113 OPEN get_existent_qty2; 114 FETCH get_existent_qty2 INTO v_price2; 115 CLOSE get_existent_qty2; 116 117 UPDATE delivery_entering_record der SET der.befor_price = nvl(v_price2,0) 118 WHERE der.ew_do_code = p_ew_code 119 AND der.material_code = v_material_code2 120 AND der.region = v_region2 121 AND der.wh_code = v_wh_code2 122 AND der.fs_code = v_fs_code2; 123 124 END LOOP; 125 CLOSE get_ew_detail2; 126 END IF; 127 -- 128 --------------------------------- 129 IF p_wf_node_code = 3 AND p_auditor_status =‘03‘ THEN 130 OPEN get_ew; 131 LOOP 132 FETCH get_ew INTO v_region,v_wh_code,v_fs_code,v_specification,v_material_code, 133 v_unit_price,v_actual_ew_qty,v_partner,v_manufaturer; 134 135 EXIT WHEN get_ew%NOTFOUND; 136 137 OPEN get_inventory; 138 FETCH get_inventory INTO v_price,v_existent_qty; 139 CLOSE get_inventory; 140 141 v_new_price := (nvl(v_unit_price,0) * nvl(v_actual_ew_qty,0) + nvl(v_price,0) * nvl(v_existent_qty,0))/(nvl(v_actual_ew_qty,0) + nvl(v_existent_qty,0)); 142 143 UPDATE inventory i SET i.price = v_new_price WHERE i.region = v_region 144 AND i.material_code = v_material_code 145 AND i.wh_code = v_wh_code 146 AND i.fs_code = v_fs_code 147 AND i.specification = v_specification; 148 --AND i.partner = v_partner 149 --AND i.manufaturer = v_manufaturer; 150 END LOOP; 151 CLOSE get_ew; 152 END IF; 153 --------------------------------- 154 -- 库存表中更新后的单价 create by denghaibin 2018-01-11 155 IF p_wf_node_code = 3 AND p_auditor_status =‘03‘ THEN 156 OPEN get_ew_detail3; 157 LOOP 158 FETCH get_ew_detail3 INTO v_region3,v_wh_code3,v_fs_code3,v_material_code3; 159 EXIT WHEN get_ew_detail3%NOTFOUND; 160 161 OPEN get_existent_qty3; 162 FETCH get_existent_qty3 INTO v_price3; 163 CLOSE get_existent_qty3; 164 165 UPDATE delivery_entering_record der SET der.after_price = nvl(v_price3,0) 166 WHERE der.ew_do_code = p_ew_code 167 AND der.material_code = v_material_code3 168 AND der.region = v_region3 169 AND der.wh_code = v_wh_code3 170 AND der.fs_code = v_fs_code3; 171 172 END LOOP; 173 CLOSE get_ew_detail3; 174 END IF; 175 -- 176 context_pkg.set_app_user_name(v_appuser); --还原当前用户 177 178 RETURN ‘0‘; 179 180 END inventory_price_update;
作者:DSHORE 出处:http://www.cnblogs.com/dshore123/ 欢迎转载,转载务必说明出处。(如果本文对你有用,可以点击一下右下角的 推荐,谢谢!) |