Advertisement
Guest User

stbal

a guest
Feb 10th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.29 KB | None | 0 0
  1. {pm/vers-top.i stk/stbal.p 22 287344 9 014298}
  2. /******************************************************************************/
  3. /* PROGRAM : stbal.p */
  4. /******************************************************************************/
  5. /* This is an unpublished work created in 1987,88,89,90,91,92 */
  6. /* APPLIED LOGIC LTD. owns all rights to this work and intends to keep */
  7. /* the work confidential so as to maintain its value as a trade secret. */
  8. /* APPLIED LOGIC LTD. may also seek to protect this work as an unpublished */
  9. /* copyright work. In the event of either inadvertent or deliberate */
  10. /* publication, APPLIED LOGIC LTD. intends to enforce its rights for this */
  11. /* work under the copyright laws as a published work; and to that end, */
  12. /* APPLIED LOGIC LTD. hereby affixes the following statutory notice: */
  13. /* (C) Copyright, APPLIED LOGIC LTD., 1987,88,89,90,91,92 */
  14. /******************************************************************************
  15. AUTHOR : Stephen Hawkins
  16. DATE : 21 June 1995
  17.  
  18. PURPOSE : Compute theoretical qty and cost balances for a stock item with
  19. specified serial-no up to specified date.
  20. Note: SJH commented this as if it is for serial items only , but he uses it
  21. in stk/sttakupd.p for all stock items. there is a check for
  22. item.serial-type = "N" so it should work ok for non serial/batch items
  23.  
  24.  
  25. Code adapted from old stbal.i and stbalcmp.i.
  26. If p-serial-no = ? then balances are for all serial numbers.
  27. Parameter p-include-take decides whether or not we include any
  28. stock-take records on p-to-date. This will be no when posting a
  29. stock-take as we don't want to include previous posting.
  30.  
  31. USE OF STOCK-TAKE RECORDS:
  32. Balances start off from lastest stock-take for speed purposes (it
  33. is therefore important that stock-take integrity is strong).
  34. In the case where this procedure is used to calculate the balance
  35. for a serial item for all serial/batch codes (p-serial-no = ?),
  36. we have to be take into account that not necessarily all serial
  37. numbers were counted in the last stock-take. Therefore we may
  38. need to go to previous stock-takes for serial items left out
  39. of the last stock-take. The workfile w-take is defined for this
  40. purpose -> each serial-no has its own start trans-date.
  41.  
  42. FALSE STOCK-TAKES:
  43. Note that false stock-takes have been implemented at some sites to
  44. set the start date of stock costing (in the case where earlier
  45. stock-trans exist which should not be modified). These
  46. stock-takes have trans-no = 0.
  47. Because the stock-take index forces each ledger to have a unique
  48. trans-no, the serial-no field has been given extra responsibility.
  49. For 0 trans stock-takes, serial-no may include ledger information
  50. to keep record unique. This explains why there is an extra
  51. stock-take find to determine start situation.
  52. Assumption for a false stock-take is that it is scoped
  53. to an entire item.
  54.  
  55. CURRENT INDEX LIMITATIONS:
  56. (1) No index of stock-take has ledger-code
  57. (2) Item-date index of stock-trans (used in for each) does not
  58. include serial-no.
  59.  
  60. MODIFIED : FCN 19/9/96 Dual Unit of Measure - TASK 8897.
  61. ******************************************************************************/
  62.  
  63. def input param p-recid as recid no-undo.
  64. def input param p-serial-no like stock-trans.serial-no no-undo.
  65. def input param p-to-date as date no-undo.
  66. def input param p-include-take as log no-undo.
  67. def output param p-qty like stock-trans.qty no-undo.
  68. def output param p-cost like stock-trans.cost no-undo.
  69. def output param p-alt-qty like stock-trans.alt-qty no-undo.
  70.  
  71. {at/atshared.i shared}
  72.  
  73. def var l-start-date as date no-undo.
  74. def var l-to-date as date no-undo.
  75. def var l-take-date as date no-undo.
  76. def var l-factor as dec no-undo.
  77. def var l-qty like stock-trans.qty no-undo.
  78. def var l-alt-qty like stock-trans.alt-qty no-undo.
  79.  
  80. def var l-ht-qty as deci no-undo.
  81. def var l-ht-cost as deci no-undo.
  82. def var l-ht-alt-qty as deci no-undo.
  83.  
  84. def temp-table w-take no-undo
  85. field serial-no like stock-take.serial-no
  86. field trans-date like stock-take.trans-date
  87. index idx serial-no.
  88.  
  89. DEF TEMP-TABLE tt-stktrans NO-UNDO
  90. FIELD id LIKE stock-trans.id
  91. FIELD serial-no LIKE stock-trans.serial-no
  92. FIELD trans-date LIKE stock-trans.trans-date
  93. INDEX main IS PRIMARY UNIQUE
  94. id
  95. INDEX secaa
  96. serial-no
  97. trans-date
  98. .
  99.  
  100. l-to-date = p-to-date + if p-include-take then 1 else 0.
  101.  
  102. find stock where recid( stock ) = p-recid no-lock no-error.
  103. if not available stock then
  104. return.
  105.  
  106. find
  107. item where
  108. item.company-code = stock.company-code and
  109. item.item-no = stock.item-no and
  110. item.type-code = 'S'
  111. no-lock no-error.
  112. if not available item then
  113. return.
  114.  
  115. /* P-SERIAL-NO IS SPECIFICALLY BLANK IF ITEM NOT BATCH/SERIAL */
  116. if p-serial-no = ? and item.serial-type = "N" then
  117. p-serial-no = "".
  118.  
  119. /* INITIAL VALUES */
  120. assign
  121. l-start-date = s-first-date
  122. p-qty = 0
  123. p-cost = 0.
  124.  
  125.  
  126. /******************************************************************************
  127. DOES THERE EXIST A ZERO TRANS FALSE STOCK-TAKE?
  128. A zero trans stock-take, if it exists, sets the start date for legitimate
  129. stock tranactions for the item (all prior stock-trans and stock-takes are
  130. ignored).
  131. It also gives us the initial quantity and cost.
  132. Note that serial-no = ledger-code + serial-no to get around index limitation.
  133. It is assumed that a zero trans stock-take is performed for an entire item.
  134. When determining initial qty and cost balances we use all zero trans
  135. stock-takes for item and ledger if unspecific p-serial-no.
  136. ******************************************************************************/
  137.  
  138. /* IS THERE A ZERO TRANS STOCK-TAKE? */
  139. find last
  140. stock-take use-index prim-idx where
  141. stock-take.company-code = stock.company-code and
  142. stock-take.trans-no = 0 and
  143. stock-take.ledger-code = stock.ledger-code and
  144. stock-take.item-no = stock.item-no and
  145. stock-take.trans-date < l-to-date
  146. no-lock no-error.
  147. if available stock-take then
  148. do:
  149.  
  150. /* STOCK-TAKE DETERMINES INITIAL START DATE */
  151. l-start-date = stock-take.trans-date.
  152.  
  153. /* DETERMINE INITIAL QTY AND COST BALANCES */
  154. for last stock-take use-index prim-idx where
  155. stock-take.company-code = stock.company-code and
  156. stock-take.trans-no = 0 and
  157. stock-take.ledger-code = stock.ledger-code and
  158. stock-take.item-no = stock.item-no and
  159. stock-take.trans-date <= l-start-date and
  160. (stock-take.serial-no = p-serial-no or
  161. p-serial-no = ?)
  162. no-lock:
  163. assign
  164. p-qty = p-qty + stock-take.count-qty
  165. p-cost = p-cost + stock-take.count-cost
  166. p-alt-qty = p-alt-qty + stock-take.alt-qty.
  167. end.
  168.  
  169. end.
  170.  
  171.  
  172. /******************************************************************************
  173. IF NOT MULTI-SERIAL THEN FIND LAST GENUINE STOCK-TAKE
  174. In this case serial-no is specific and we look for the single last stock-take
  175. for ledger, item, serial within the accepted date range.
  176. If stock-take is found, initial values for start-date, quantity and cost will
  177. be overridden.
  178. A pure cost adjusting stock-take is implemented here. If a cost is counted
  179. but not cost, then the quantity balance is taken from the old-qty field
  180. which was the calculated balance at stock-take date.
  181. ******************************************************************************/
  182.  
  183. /* IS SERIAL-NO SPECIFIC? */ /* or blank for non serial */
  184. if p-serial-no <> ? then
  185. do:
  186. find last
  187. stock-take use-index date-idx where
  188. stock-take.company-code = stock.company-code and
  189. stock-take.ledger-code = stock.ledger-code and
  190. stock-take.item-no = stock.item-no and
  191. stock-take.serial-no = p-serial-no and
  192. stock-take.trans-date > l-start-date and
  193. stock-take.trans-date < l-to-date
  194. no-lock no-error.
  195. if available stock-take then
  196. do:
  197. assign
  198. l-start-date = stock-take.trans-date
  199. p-cost = stock-take.cost-adjust.
  200. if stock-take.count-qty = 0 and stock-take.count-cost > 0 then
  201. assign p-qty = stock-take.old-qty
  202. p-alt-qty = stock-take.alt-old-qty.
  203. else
  204. assign
  205. p-qty = stock-take.count-qty
  206. p-alt-qty = stock-take.alt-qty.
  207. end.
  208. end.
  209.  
  210.  
  211. /******************************************************************************
  212. DETERMINE LAST STOCK-TAKE FOR EACH SERIAL-NO FOR MULTI-SERIAL
  213. This is for the case where we want to find the balance for an entire stock
  214. item which has batch/serial tracking.
  215. The last stock-take for each stock-serial is looked for in case not each
  216. stock-serial is counted in a particular stock-count-h.
  217. If the assumption is introduced that any stock-serial not counted when a
  218. batch/serial item is counted implies a count of zero for that stock-serial,
  219. then this code will not be necessary.
  220. A w-take record is created for the last stock-take of each stock-serial and
  221. l-start-date is the minimum of all the w-take dates.
  222. ******************************************************************************/
  223.  
  224. /* P-SERIAL-NO = ? -> NON-SPECIFIC */
  225. else
  226. do:
  227. for each stock-serial where
  228. stock-serial.company-code = stock.company-code and
  229. stock-serial.ledger-code = stock.ledger-code and
  230. stock-serial.item-no = stock.item-no and
  231. stock-serial.status-code <> 'X'
  232. no-lock:
  233.  
  234. /* LAST STOCK-TAKE FOR STOCK-SERIAL */
  235. find last
  236. stock-take use-index date-idx where
  237. stock-take.company-code = stock-serial.company-code and
  238. stock-take.item-no = stock-serial.item-no and
  239. stock-take.serial-no = stock-serial.serial-no and
  240. stock-take.ledger-code = stock-serial.ledger-code and
  241. stock-take.trans-date > l-start-date and
  242. stock-take.trans-date < l-to-date
  243. no-lock no-error.
  244.  
  245. /* MAINTAIN W-TAKE, P-QTY AND P-COST */
  246. if available stock-take then
  247. do:
  248. if l-take-date = ? or l-take-date > stock-take.trans-date then
  249. l-take-date = stock-take.trans-date.
  250. create w-take.
  251. assign
  252. w-take.serial-no = stock-take.serial-no
  253. w-take.trans-date = stock-take.trans-date
  254. p-cost = p-cost + stock-take.cost-adjust.
  255. if stock-take.count-qty = 0 and stock-take.count-cost > 0 then
  256. assign p-qty = p-qty + stock-take.old-qty
  257. p-alt-qty = p-alt-qty + stock-take.alt-old-qty.
  258. else
  259. assign
  260. p-qty = p-qty + stock-take.count-qty
  261. p-alt-qty = p-alt-qty + stock-take.alt-qty.
  262. end.
  263.  
  264. end.
  265.  
  266. /* EARLIEST STOCK-TAKE */
  267. if l-take-date <> ? then /** HT 26/04/96 **/
  268. l-start-date = l-take-date.
  269. end.
  270.  
  271.  
  272. /******************************************************************************
  273. LOOP THROUGH CORRECT RANGE OF STOCK-TRANS
  274. Add costs and quantities for each stock-trans for stock (or stock-serial) in
  275. date range.
  276. Only interested in complete transactions (status "C").
  277. Date range starts at l-start-date but for unspecific p-serial-no, ignore any
  278. stock-trans after a specific stock-serial's w-take date.
  279. Quantity and cost is subtracted for outward type stock-trans (not in
  280. stk/stttp-r.i).
  281. ******************************************************************************/
  282. if l-take-date = ? then
  283. l-take-date = s-first-date.
  284.  
  285. for each stock-trans no-lock
  286. USE-INDEX item-date
  287. WHERE stock-trans.company-code = stock.company-code
  288. AND stock-trans.item-no = stock.item-no
  289. AND stock-trans.ledger-code = stock.ledger-code
  290. AND stock-trans.status-code = "C"
  291. AND stock-trans.trans-date > l-start-date
  292. :
  293.  
  294. IF stock-trans.trans-date > p-to-date THEN
  295. LEAVE.
  296.  
  297. IF p-serial-no <> ?
  298. AND stock-trans.serial-no <> p-serial-no THEN
  299. NEXT.
  300.  
  301. CREATE tt-stktrans.
  302. ASSIGN
  303. tt-stktrans.id = stock-trans.id
  304. tt-stktrans.serial-no = stock-trans.serial-no
  305. tt-stktrans.trans-date = stock-trans.trans-date
  306. .
  307. END.
  308.  
  309. FOR EACH tt-stktrans
  310. USE-INDEX secaa
  311. break by tt-stktrans.serial-no
  312. by tt-stktrans.trans-date:
  313.  
  314. FIND stock-trans NO-LOCK
  315. WHERE stock-trans.id = tt-stktrans.id
  316. NO-ERROR.
  317. IF NOT AVAIL stock-trans THEN
  318. NEXT.
  319.  
  320. /* DO NOT INCLUDE STOCK-TRANS BEFORE INDIVIDUAL SERIAL'S STOCK-TAKE DATE */
  321. if first-of( tt-stktrans.serial-no ) then
  322. do:
  323. find FIRST w-take
  324. WHERE w-take.serial-no = stock-trans.serial-no
  325. no-error.
  326. if available w-take then
  327. l-take-date = w-take.trans-date.
  328. else
  329. l-take-date = s-first-date.
  330. end.
  331.  
  332. /* DO NOT INCLUDE STOCK-TRANS IF TIED TO STOCK-TAKE ON P-DATE AND NOT
  333. P-INCLUDE-TAKE */
  334. if stock-trans.type-code begins "X" and
  335. stock-trans.trans-date = p-to-date and
  336. not p-include-take
  337. then next.
  338.  
  339. if stock-trans.trans-date <= l-take-date then
  340. next.
  341.  
  342. /* CONVERT UNITS */
  343. l-factor = 1.
  344. if stock-trans.unit <> item.unit then
  345. run stk/unitconv.p (
  346. input stock-trans.unit,
  347. input item.unit,
  348. input stock-trans.item-no,
  349. input-output l-factor
  350. ).
  351. assign l-qty = stock-trans.qty * l-factor.
  352.  
  353. /* SUM P-QTY AND P-COST */
  354. if {stk/sttyp-r.i stock-trans} then
  355. do:
  356. assign
  357. p-qty = p-qty + l-qty
  358. p-cost = p-cost + stock-trans.cost
  359. p-alt-qty = p-alt-qty + stock-trans.alt-qty.
  360. end.
  361. else
  362. do:
  363. assign
  364. p-qty = p-qty - l-qty
  365. p-cost = p-cost - stock-trans.cost
  366. p-alt-qty = p-alt-qty - stock-trans.alt-qty.
  367. end.
  368. end. /* for each tt-stktrans */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement