Loop iteration

jscarcella

New Member
I have a piece of code for a report and I want it to display every Site with it's own extended price. If I put the site in from and to it works fine, but if I run it wide open it accumulates all sites together I feel its a scope problem but just can't get it to work right the code is below if anyone can help I would appreciate it very much.

John

/* DISPLAY TITLE */
{mfdtitle.i "++ "} /*FL44*/
define variable addr like tr_addr.
define variable addr1 like tr_addr.
define variable so_job like tr_so_job.
define variable so_job1 like tr_so_job.
define variable part like tr_part.
define variable part1 like tr_part.
define variable trdate like tr_effdate.
define variable trdate1 like tr_effdate.
define variable region like cm_region.
define variable region1 like cm_region.
define variable site like tr_site.
define variable site1 like tr_site.
define variable name like ad_name.
define variable ext_price like tr_price label "Ext Price"
format "->,>>>,>>>,>>9.99".
define variable gr_margin like tr_price.
define variable ext_gr_margin like tr_price label "Ext Margin"
format "->,>>>,>>>,>>9.99<<<".
define variable summary like mfc_logical format "Summary/Detail" label "Summary/Detail".
define variable base_rpt like so_curr.
define variable base_price like tr_price.
define variable disp_curr as character format "x(1)" label "C".
define variable prod_line like tr_prod_line.
define variable prod_line1 like tr_prod_line.
define variable desc2 like pl_desc.
define variable pct_margin as decimal format "->>9.99" label "%Margin".
define variable unit_cost like tr_mtl_std.
define variable tmp_margin like pct_margin.
define variable region_chk like mfc_logical.
define variable l_first_prod_line like mfc_logical no-undo.
define variable l_first_part like mfc_logical no-undo.
define variable l_prod_line_ok like mfc_logical no-undo.
define variable l_part_ok like mfc_logical no-undo.
define variable l_report_ok like mfc_logical no-undo.
define variable sidesc like si_desc.
define variable ext-price like ext_price.

form
trdate colon 15
trdate1 label {t001.i} colon 49 skip
site colon 15
site1 label {t001.i} colon 49 skip(1)
with frame a side-labels width 80 attr-space.
repeat:
if trdate = low_date then trdate = ?.
if trdate1 = hi_date then trdate1 = ?.
if site1 = hi_char then site1 = "".
update trdate trdate1 site site1
with frame a.
bcdparm = "".
{mfquoter.i trdate }
{mfquoter.i trdate1 }
{mfquoter.i site }
{mfquoter.i site1 }
if part1 = "" then part1 = hi_char.
if prod_line1 = "" then prod_line1 = hi_char.
if addr1 = "" then addr1 = hi_char.
if trdate = ? then trdate = low_date.
if trdate1 = ? then trdate1 = hi_date.
if so_job1 = "" then so_job1 = hi_char.
if region1 = "" then region1 = hi_char.
if site1 = "" then site1 = hi_char.
/* SELECT PRINTER */
{mfselbpr.i "printer" 132}
{mfphead.i}
form header
skip(1)
with frame p1 page-top width 132 attr-space.
view frame p1.
/* SELECTION OF REGION OCCURS OUTSIDE THE FOR EACH LOOP(TO USE THE
REPORT AT REMOTE DB). THE "OK" FLAG INDICATES IF ANY VALID RECORD
HAS BEEN SELECTED FOR THE REPORT, PRODUCT LINE OR PART */
l_report_ok = no.
for each tr_hist where (tr_part >= part and tr_part <= part1)
and (tr_prod_line >= prod_line and tr_prod_line <= prod_line1)
and (tr_effdate >= trdate and tr_effdate <= trdate1)
and (tr_addr >= addr and tr_addr <= addr1)
and (tr_site >= site and tr_site <= site1)
and (tr_so_job >= so_job and tr_so_job <= so_job1)
and (tr_type = "ISS-SO" or tr_type = "RCT-SOR")
and ((base_rpt = "")
or (tr_curr = base_rpt))
use-index tr_eff_trnbr no-lock
break by tr_site by tr_prod_line by tr_part by tr_effdate with frame b width 132 no-box:
ext-price = 0.

if first-of(tr_site) then do:
end.

if first-of(tr_prod_line) then
assign
l_first_prod_line = yes
l_prod_line_ok = no.
/* SAVE THE first-of(tr_prod_line) OCCURRENCE FOR REFERRING TO VALID
tr_hist RECORDS.
INITIALIZE O.K. TO PRINT PRODUCT LINE TOTALS VARIABLE.*/
if first-of(tr_part) then
assign
l_first_part = yes
l_part_ok = no.
/* SAVE THE first-of(tr_part) OCCURRENCE FOR REFERRING TO VALID
tr_hist RECORDS.
AND INITIALIZE O.K. TO PRINT PART TOTALS VARIABLE.*/

/* Check customer region (if customers in this db) */
if (region > "" or region1 < hi_char)
and can-find(first cm_mstr where cm_addr >= "") then do:
find cm_mstr where cm_addr = tr_addr no-lock no-error.
region_chk = (available cm_mstr
and cm_region >= region and cm_region <= region1).
end.
else
region_chk = true.
if l_first_prod_line and region_chk then do:
/* CHECK FOR FIRST VALID RECORD FOR THIS PRDUCT LINE */
l_first_prod_line = no.
find pl_mstr where pl_prod_line = tr_prod_line no-lock no-error.
if available pl_mstr then do:
desc2 = pl_desc.
end. /* if available pl_mstr then do: */
end. /* if l_first_prod_line and region_chk then do: */
if region_chk then do:
l_part_ok = yes.
/* AT-LEAST ONE VALID RECORD FOUND FOR THIS PART BREAK */
end. /* If region for this line is valid */

/* ACCUMULATES VALUES */
base_price = tr_price.
disp_curr = "".
if base_rpt <> ""
and tr_curr <> base_curr then
base_price = base_price * tr_ex_rate.
if base_rpt = ""
and tr_curr <> base_curr then disp_curr = "Y".
ext_price = - tr_qty_loc * base_price.
unit_cost = - tr_gl_amt / tr_qty_loc.
if tr_ship_type <> " " then
assign unit_cost = tr_mtl_std + tr_lbr_std + tr_bdn_std +
tr_ovh_std + tr_sub_std.
gr_margin = tr_price - unit_cost.
if base_rpt <> ""
and tr_curr <> base_curr then
gr_margin = gr_margin * tr_ex_rate.
ext_gr_margin = - tr_qty_loc * gr_margin.
if unit_cost = 0 then pct_margin = 100.
else if (- tr_qty_loc) = 0 then pct_margin = 0.
else if ext_price = 0 then pct_margin = 0.
else pct_margin = (ext_gr_margin / ext_price) * 100.
accumulate ext_price(total).
/*jps find pt_mstr where pt_part = tr_part no-lock no-wait no-error.*/
/* DISPLAY PART TOTALS WHEN AT-LEAST ONE tr_hist RECORD QUALIFIES SELECTION CRITERIA */
if last-of(tr_site) then do:
ext-price = (accum total ext_price).
display tr_site ext-price.
end.
end. /* for each */
{mfrpexit.i}
/* REPORT TRAILER */
{mfrtrail.i}
end. /* repeat */

*** the output looks like this**** *** It Should look like this***
Site Ext Price Site Ext Price
--------- ------------ ---------- -------------
HARPCO 1,934.94 HARPCO 1,934.94
HARPMEX 134,356.63 HARPMEX 132,421.96
TNN 191,622.37 TNN 57,265.74
 

jongpau

Member
Try:

accumulate ext_price(sub-total by tr_site).

and

if last-of(tr_site) then do:
display tr_site (accum sub-total by tr_site ext_price).
end.
 

jscarcella

New Member
Thank You

Jongpau,

Thank you very much for your reply, it worked just the way I wanted it to. You have made me very happy and have taught me something, thanks again for passing on your wisdom.


John:biggrin:
 
Top