f_update_mtrlware_spt_out.srf 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. $PBExportHeader$f_update_mtrlware_spt_out.srf
  2. global type f_update_mtrlware_spt_out from function_object
  3. end type
  4. forward prototypes
  5. global function integer f_update_mtrlware_spt_out (integer arg_billtype, integer arg_thflag, long arg_scid, long arg_mtrlid, string arg_mtrlcode, string arg_plancode, string arg_status, decimal arg_qty, decimal arg_costamt, decimal arg_planprice, long arg_sptid, string arg_woodcode, string arg_pcode, ref string arg_msg, boolean arg_ifcommit, transaction arg_transaction)
  6. end prototypes
  7. global function integer f_update_mtrlware_spt_out (integer arg_billtype, integer arg_thflag, long arg_scid, long arg_mtrlid, string arg_mtrlcode, string arg_plancode, string arg_status, decimal arg_qty, decimal arg_costamt, decimal arg_planprice, long arg_sptid, string arg_woodcode, string arg_pcode, ref string arg_msg, boolean arg_ifcommit, transaction arg_transaction);Int rslt = 1
  8. Long ls_newid
  9. Decimal ld_cost
  10. String or_err_part
  11. Decimal ld_thqty,ld_thamt
  12. Decimal ld_inqty,ld_inamt
  13. Decimal ld_balcqty,ld_balcamt
  14. Decimal ld_pypkqty,ld_pypkamt
  15. IF sys_option_wfjgware_nocost = 0 THEN //核算金额
  16. UPDATE ow_wfjgware
  17. SET noallocqty = noallocqty + :arg_qty ,
  18. wareamt = case noallocqty + :arg_qty when 0 then 0 else (wareamt + :arg_costamt) END ,
  19. cost = case noallocqty + :arg_qty when 0 then cost else round( (wareamt + :arg_costamt)/(noallocqty + :arg_qty),:sys_option_cost_dec) END ,
  20. waredate = getdate()
  21. WHERE ( mtrlid = :arg_mtrlid ) AND
  22. (status = :arg_Status) AND
  23. woodcode = :arg_woodcode AND
  24. pcode = :arg_pcode AND
  25. plancode = :arg_plancode AND
  26. sptid = :arg_sptid AND
  27. scid = :arg_scid USING arg_transaction ;
  28. IF arg_transaction.SQLCode = 0 THEN
  29. IF arg_transaction.SQLNRows = 0 THEN
  30. ls_newid = f_sys_scidentity(0,"ow_wfjgware","mtrlwareid",arg_msg,TRUE,id_sqlca)
  31. IF ls_newid <= 0 THEN
  32. rslt = 0
  33. GOTO ext
  34. END IF
  35. ld_cost = Round(arg_costamt/arg_qty,sys_option_cost_dec)
  36. INSERT INTO ow_wfjgware
  37. (mtrlwareid,
  38. mtrlid,
  39. plancode,
  40. status,
  41. noallocqty,
  42. allocqty,
  43. wareamt,
  44. planprice,
  45. sptid,
  46. woodcode,
  47. pcode,
  48. cost,
  49. scid)
  50. VALUES (
  51. :ls_newid,
  52. :arg_mtrlid,
  53. :arg_plancode,
  54. :arg_status,
  55. :arg_qty,
  56. 0,
  57. :arg_costamt,
  58. :arg_planprice,
  59. :arg_sptid,
  60. :arg_woodcode,
  61. :arg_pcode,
  62. :ld_cost,
  63. :arg_scid) USING arg_transaction ;
  64. IF arg_transaction.SQLCode <> 0 THEN
  65. rslt = 0
  66. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]库存建立操作失败"+"~n"+arg_transaction.SQLErrText
  67. GOTO ext
  68. END IF
  69. END IF
  70. ELSE
  71. rslt = 0
  72. or_err_part = 'column insert or update conflicts with a rule imposed by a previous create rule statement'
  73. IF arg_transaction.SQLCode = 513 OR Pos(Lower(arg_transaction.SQLErrText),or_err_part) > 0 OR Pos(Lower(arg_transaction.SQLErrText),'noallocqty') > 0 THEN
  74. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]没有足够的库存支持冲减"
  75. ELSEIF Pos(Lower(arg_transaction.SQLErrText),'cost') > 0 or Pos(Lower(arg_transaction.SQLErrText),'wareamt') > 0 THEN
  76. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]没有足够的库存金额支持冲减"
  77. ELSE
  78. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]库存更新操作失败"+"~n"+arg_transaction.SQLErrText
  79. END IF
  80. GOTO ext
  81. END IF
  82. //--------------------更新结存
  83. ld_balcqty = arg_qty
  84. ld_balcamt = arg_costamt
  85. CHOOSE CASE arg_billtype
  86. CASE 4,5
  87. IF arg_thflag = 0 THEN
  88. ld_inqty = arg_qty
  89. ld_inamt = arg_costamt
  90. ld_thqty = 0
  91. ld_thamt = 0
  92. ld_pypkqty = 0
  93. ld_pypkamt = 0
  94. ELSE
  95. ld_inqty = 0
  96. ld_inamt = 0
  97. ld_thqty = 0 - arg_qty
  98. ld_thamt = 0 - arg_costamt
  99. ld_pypkqty = 0
  100. ld_pypkamt = 0
  101. END IF
  102. CASE 9
  103. ld_inqty = 0
  104. ld_inamt = 0
  105. ld_thqty = 0
  106. ld_thamt = 0
  107. ld_pypkqty = 0 + arg_qty
  108. ld_pypkamt = 0 + arg_costamt
  109. END CHOOSE
  110. UPDATE ow_wfjgbalc
  111. SET
  112. balcqty = balcqty + :ld_balcqty,
  113. balcamt = case balcqty + :ld_balcqty when 0 then 0 else balcamt + :ld_balcamt END ,
  114. inqty = inqty + :ld_inqty,
  115. inamt = inamt + :ld_inamt,
  116. thqty = thqty + :ld_thqty,
  117. thamt = thamt + :ld_thamt,
  118. pypkqty = pypkqty + :ld_pypkqty,
  119. pypkamt = pypkamt + :ld_pypkamt
  120. WHERE ( mtrlid = :arg_mtrlid ) AND
  121. ( balcdateint = 0 ) AND
  122. ( status = :arg_status ) AND
  123. ( woodcode = :arg_woodcode ) AND
  124. ( sptid = :arg_sptid ) AND
  125. ( pcode = :arg_pcode ) AND
  126. ( scid = :arg_scid ) USING arg_transaction ;
  127. IF arg_transaction.SQLCode = 0 THEN
  128. IF arg_transaction.SQLNRows = 0 THEN
  129. INSERT INTO ow_wfjgbalc
  130. (balcdateint,
  131. sptid,
  132. mtrlid,
  133. status,
  134. woodcode,
  135. pcode,
  136. thqty,
  137. thamt,
  138. inqty,
  139. inamt,
  140. balcqty,
  141. balcamt,
  142. pypkqty,
  143. pypkamt,
  144. scid)
  145. VALUES (0,
  146. :arg_sptid,
  147. :arg_mtrlid,
  148. :arg_status,
  149. :arg_woodcode,
  150. :arg_pcode,
  151. :ld_thqty,
  152. :ld_thamt,
  153. :ld_inqty,
  154. :ld_inamt,
  155. :ld_balcqty,
  156. :ld_balcamt,
  157. :ld_pypkqty,
  158. :ld_pypkamt,
  159. :arg_scid) USING arg_transaction;
  160. IF arg_transaction.SQLCode <> 0 THEN
  161. rslt = 0
  162. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]结存建立操作失败"+"~n"+arg_transaction.SQLErrText
  163. GOTO ext
  164. END IF
  165. END IF
  166. ELSE
  167. rslt = 0
  168. or_err_part = 'column insert or update conflicts with a rule imposed by a previous create rule statement'
  169. IF arg_transaction.SQLCode = 513 OR Pos(Lower(arg_transaction.SQLErrText),or_err_part) > 0 THEN
  170. arg_msg = "外协商库存物料["+arg_mtrlcode+"]没有足够的结存支持冲减"
  171. ELSE
  172. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]结存更新操作失败"+"~n"+arg_transaction.SQLErrText
  173. END IF
  174. GOTO ext
  175. END IF
  176. ELSE
  177. UPDATE ow_wfjgware
  178. SET noallocqty = noallocqty + :arg_qty ,
  179. wareamt = 0 ,
  180. cost = 0 ,
  181. waredate = getdate()
  182. WHERE ( mtrlid = :arg_mtrlid ) AND
  183. (status = :arg_Status) AND
  184. woodcode = :arg_woodcode AND
  185. pcode = :arg_pcode AND
  186. plancode = :arg_plancode AND
  187. sptid = :arg_sptid AND
  188. scid = :arg_scid USING arg_transaction ;
  189. IF arg_transaction.SQLCode = 0 THEN
  190. IF arg_transaction.SQLNRows = 0 THEN
  191. ls_newid = f_sys_scidentity(0,"ow_wfjgware","mtrlwareid",arg_msg,TRUE,id_sqlca)
  192. IF ls_newid <= 0 THEN
  193. rslt = 0
  194. GOTO ext
  195. END IF
  196. INSERT INTO ow_wfjgware
  197. (mtrlwareid,
  198. mtrlid,
  199. plancode,
  200. status,
  201. noallocqty,
  202. allocqty,
  203. wareamt,
  204. planprice,
  205. sptid,
  206. woodcode,
  207. pcode,
  208. cost,
  209. scid)
  210. VALUES (
  211. :ls_newid,
  212. :arg_mtrlid,
  213. :arg_plancode,
  214. :arg_status,
  215. :arg_qty,
  216. 0,
  217. 0,
  218. 0,
  219. :arg_sptid,
  220. :arg_woodcode,
  221. :arg_pcode,
  222. 0,
  223. :arg_scid) USING arg_transaction ;
  224. IF arg_transaction.SQLCode <> 0 THEN
  225. rslt = 0
  226. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]库存建立操作失败"+"~n"+arg_transaction.SQLErrText
  227. GOTO ext
  228. END IF
  229. END IF
  230. ELSE
  231. rslt = 0
  232. or_err_part = 'column insert or update conflicts with a rule imposed by a previous create rule statement'
  233. IF arg_transaction.SQLCode = 513 OR Pos(Lower(arg_transaction.SQLErrText),or_err_part) > 0 OR Pos(Lower(arg_transaction.SQLErrText),'noallocqty') > 0 THEN
  234. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]没有足够的库存支持冲减"
  235. ELSEIF Pos(Lower(arg_transaction.SQLErrText),'cost') > 0 or Pos(Lower(arg_transaction.SQLErrText),'wareamt') > 0 THEN
  236. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]没有足够的库存金额支持冲减"
  237. ELSE
  238. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]库存更新操作失败"+"~n"+arg_transaction.SQLErrText
  239. END IF
  240. GOTO ext
  241. END IF
  242. //--------------------更新结存
  243. ld_balcqty = arg_qty
  244. ld_balcamt = arg_costamt
  245. CHOOSE CASE arg_billtype
  246. CASE 4,5
  247. IF arg_thflag = 0 THEN
  248. ld_inqty = arg_qty
  249. ld_inamt = 0
  250. ld_thqty = 0
  251. ld_thamt = 0
  252. ld_pypkqty = 0
  253. ld_pypkamt = 0
  254. ELSE
  255. ld_inqty = 0
  256. ld_inamt = 0
  257. ld_thqty = 0 - arg_qty
  258. ld_thamt = 0
  259. ld_pypkqty = 0
  260. ld_pypkamt = 0
  261. END IF
  262. CASE 9
  263. ld_inqty = 0
  264. ld_inamt = 0
  265. ld_thqty = 0
  266. ld_thamt = 0
  267. ld_pypkqty = 0 + arg_qty
  268. ld_pypkamt = 0
  269. END CHOOSE
  270. UPDATE ow_wfjgbalc
  271. SET
  272. balcqty = balcqty + :ld_balcqty,
  273. balcamt = 0 ,
  274. inqty = inqty + :ld_inqty,
  275. inamt = 0,
  276. thqty = thqty + :ld_thqty,
  277. thamt = 0,
  278. pypkqty = pypkqty + :ld_pypkqty,
  279. pypkamt = 0
  280. WHERE ( mtrlid = :arg_mtrlid ) AND
  281. ( balcdateint = 0 ) AND
  282. ( status = :arg_status ) AND
  283. ( woodcode = :arg_woodcode ) AND
  284. ( sptid = :arg_sptid ) AND
  285. ( pcode = :arg_pcode ) AND
  286. ( scid = :arg_scid ) USING arg_transaction ;
  287. IF arg_transaction.SQLCode = 0 THEN
  288. IF arg_transaction.SQLNRows = 0 THEN
  289. INSERT INTO ow_wfjgbalc
  290. (balcdateint,
  291. sptid,
  292. mtrlid,
  293. status,
  294. woodcode,
  295. pcode,
  296. thqty,
  297. thamt,
  298. inqty,
  299. inamt,
  300. balcqty,
  301. balcamt,
  302. pypkqty,
  303. pypkamt,
  304. scid)
  305. VALUES (0,
  306. :arg_sptid,
  307. :arg_mtrlid,
  308. :arg_status,
  309. :arg_woodcode,
  310. :arg_pcode,
  311. :ld_thqty,
  312. 0,
  313. :ld_inqty,
  314. 0,
  315. :ld_balcqty,
  316. 0,
  317. :ld_pypkqty,
  318. 0,
  319. :arg_scid) USING arg_transaction;
  320. IF arg_transaction.SQLCode <> 0 THEN
  321. rslt = 0
  322. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]结存建立操作失败"+"~n"+arg_transaction.SQLErrText
  323. GOTO ext
  324. END IF
  325. END IF
  326. ELSE
  327. rslt = 0
  328. or_err_part = 'column insert or update conflicts with a rule imposed by a previous create rule statement'
  329. IF arg_transaction.SQLCode = 513 OR Pos(Lower(arg_transaction.SQLErrText),or_err_part) > 0 THEN
  330. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]没有足够的结存支持冲减"
  331. ELSE
  332. arg_msg = "外协商库存,物料["+arg_mtrlcode+"]结存更新操作失败"+"~n"+arg_transaction.SQLErrText
  333. END IF
  334. GOTO ext
  335. END IF
  336. END IF
  337. ext:
  338. IF rslt = 0 THEN
  339. ROLLBACK USING arg_transaction;
  340. ELSEIF rslt = 1 AND arg_ifcommit THEN
  341. COMMIT USING arg_transaction;
  342. END IF
  343. RETURN rslt
  344. end function