Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

*----------------------------------------------------------------------*

* Program : ZFAPR005 - Miss Days Extract for ReTech and Non-ReTech

* Date : 9th November 2002

* Release : 4.6c

* Module : N/A

* Logical DB : N/A

*

* Description : This program uploads Missing Days file in a-csv format

* delimited by (|) of all BATA products which are

* on promotion sold from the BATA Re-Tech and non Re-tech outlets in the

* ZPROMO_TRANS table.

* The program will perform the following;

* 1. Read the Missing days file and populate the

* holing table ZPROMO_MISSDAYS with the data.

* 2. Process the Missing days and Insert appropriate records

* into the ZPROMO_MAST table.

*

*

*----------------------------------------------------------------------*

* SDVK970048 CR2431 LL Tan 21.8.2006 OneSAP Upgrade&Unicode 4.7

*

*----------------------------------------------------------------------*

REPORT zfprd005

NO STANDARD PAGE HEADING

LINE-SIZE 80

LINE-COUNT 65(0)

MESSAGE-ID zl.

* TABLES

TABLES: zpromo_mast,

zpromo_trans,

zpromo_missdays,

zpromo_group,

mbew,

mvke,

kna1,

mara.

* I N T E R N A L T A B L E S

DATA: t_mdays TYPE zpromo_missdays OCCURS 0 WITH HEADER LINE.

DATA: t_bscan TYPE zpromo_bscan OCCURS 0 WITH HEADER LINE.

DATA: t_trans TYPE zpromo_trans OCCURS 0 WITH HEADER LINE.

DATA: t_group TYPE zpromo_group OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF t_bscan_data OCCURS 0,

date_code TYPE zpromo_bscan-date_code,

uom_code TYPE zpromo_bscan-uom_code,

tot_quantity TYPE zpromo_bscan-tot_quantity,

cust_code TYPE zpromo_bscan-cust_code,

store_id TYPE zpromo_bscan-store_id,

ubc_code TYPE zpromo_bscan-ubc_code,

ean_code TYPE zpromo_bscan-ean_code,

subbanner_code TYPE zpromo_bscan-subbanner_code,

whs_quantity TYPE zpromo_bscan-whs_quantity,

norm_sell_price TYPE zpromo_bscan-norm_sell_price,

avg_retail_price TYPE zpromo_bscan-avg_retail_price,

avg_whsale_price TYPE zpromo_bscan-avg_whsale_price,

day_of_week TYPE zpromo_bscan-day_of_week,

date_cr TYPE zpromo_bscan-date_cr,

rec_status TYPE zpromo_bscan-rec_status,

END OF t_bscan_data.

DATA: BEGIN OF t_bscan_tot OCCURS 0,

date_code TYPE zpromo_bscan-date_code,

* uom_code TYPE zpromo_bscan-uom_code,

tot_quantity TYPE zpromo_bscan-tot_quantity,

END OF t_bscan_tot.

* Internal Table to hold Promotion Master Records

DATA: t_mast TYPE zpromo_mast OCCURS 0 WITH HEADER LINE.

* Internal table to hold file records

DATA: BEGIN OF transdata OCCURS 0,

data(1000) TYPE c,

END OF transdata.

DATA t_exec_protocol LIKE btcxpm OCCURS 10 WITH HEADER LINE.

* Internal Table to store material master data

* Internal Table

DATA: BEGIN OF it_mara OCCURS 0,

matnr TYPE mara-matnr,

uom TYPE mara-meins,

prdha TYPE mara-prdha,

prdha4(4) TYPE c,

kbetr TYPE konp-kbetr,

kpein TYPE konp-kpein,

kmein TYPE konp-kmein,

END OF it_mara.

DATA: BEGIN OF it_per OCCURS 0,

date TYPE sy-datum,

year TYPE t009b-bdatj,

period TYPE t009b-poper,

END OF it_per.* Internal Table to Hold Promotion Master DataDATA: BEGIN OF it_mast OCCURS 0,

kunnr TYPE zpromo_mast-kunnr,

sub_trademark TYPE zpromo_mast-sub_trademark,

gjahr TYPE zpromo_mast-gjahr,

monat TYPE zpromo_mast-monat,

zpromo_id TYPE zpromo_mast-zpromo_id,

start_dt TYPE zpromo_mast-start_dt,

end_dt TYPE zpromo_mast-end_dt,

retail_perc TYPE zpromo_mast-retail_perc,

pkt_markup TYPE zpromo_mast-pkt_markup,

ctn_markup TYPE zpromo_mast-ctn_markup,

pch_markup TYPE zpromo_mast-pch_markup,

END OF it_mast.

DATA: BEGIN OF w_t006a,

msehi LIKE t006a-msehi,

msehl LIKE t006a-msehl,

END OF w_t006a.

DATA: BEGIN OF w_konp,

knumh LIKE konp-knumh,

kopos LIKE konp-kopos,

kbetr LIKE konp-kbetr,

konwa LIKE konp-konwa,

kmein LIKE konp-kmein,

kpein LIKE konp-kpein,

END OF w_konp.

DATA: BEGIN OF w_astab_1,

mandt LIKE konp-mandt,

kappl LIKE konp-kappl,

kschl LIKE konp-kschl,

varkey(1000) TYPE c,

END OF w_astab_1.

DATA: BEGIN OF w_astab_2,

tabname LIKE dd03l-tabname,

kappl LIKE konp-kappl,

kschl LIKE konp-kschl,

varkey(1000) TYPE c,

END OF w_astab_2.

DATA: BEGIN OF w_astab_3,

kappl LIKE konp-kappl,

kschl LIKE konp-kschl,

datbi LIKE konh-datbi,

datab LIKE konh-datab,

knumh LIKE konp-knumh,

END OF w_astab_3.

DATA: t_edidc LIKE edidc OCCURS 0 WITH HEADER LINE,

t_edidd LIKE edidd OCCURS 0 WITH HEADER LINE,

t_lines LIKE tline OCCURS 0 WITH HEADER LINE,

t_z1mmecm LIKE z1mmecm OCCURS 0 WITH HEADER LINE,

* T_T179T LIKE W_T179T OCCURS 0 WITH HEADER LINE,

t_t9i22 LIKE t9i22 OCCURS 0 WITH HEADER LINE,

t_t9i23 LIKE t9i23 OCCURS 0 WITH HEADER LINE,

t_astab_2 LIKE w_astab_2 OCCURS 0 WITH HEADER LINE,

t_astab_3 LIKE w_astab_3 OCCURS 0 WITH HEADER LINE,

t_konp LIKE w_konp OCCURS 0 WITH HEADER LINE,

t_t006a LIKE w_t006a OCCURS 0 WITH HEADER LINE,

t_t682i LIKE t682i OCCURS 0 WITH HEADER LINE,

t_dd03l LIKE dd03l OCCURS 0 WITH HEADER LINE.

* Used for Logging Purposes

DATA: l_message LIKE zpromo_log-message,

l_status LIKE zpromo_log-status,

l_log_counter1 TYPE n.



* W O R K V A R I A B L E S

DATA: w_rec_count TYPE i, "record counter

w_flag TYPE c.

DATA: w_status LIKE btcxp3-exitstat.

DATA: w_error_fl TYPE c,

w_found_fl TYPE c.

DATA: w_file(150) TYPE c,

w_filep(150) TYPE c,

w_command LIKE sxpgcolist-name,

w_additional_parameter LIKE sxpgcolist-parameters,

w_gjahr LIKE t009b-bdatj,

w_buper LIKE t009b-poper,

w_prev_gjahr LIKE t009b-bdatj,

w_prev_buper LIKE t009b-poper,

w_start_dt LIKE sy-datum,

w_end_dt LIKE sy-datum,

w_error_cnt TYPE i,

w_total_cnt TYPE i,

l_count TYPE i,

l_rec_count TYPE i,

l_mdays_count TYPE i,

l_trans_count TYPE i,

w_astab(4) TYPE c,

w_wholesale_price LIKE konp-kbetr,

w_sales_uom_quantity LIKE mard-vmlab,

w_string_1(1100) TYPE c,

w_string_2(1100) TYPE c,

w_varkey(1000) TYPE c,

w_gst LIKE zpromo_mast-retail_perc VALUE '10',

w_end.

DATA: BEGIN OF t_head,

rec_indicator(1) TYPE c,

period_code(6) TYPE c,

rec_count(10) TYPE n,

END OF t_head.


* C O N S T A N T S

CONSTANTS:

c_missf(13) TYPE c VALUE 'SCANMISSDAYS_',

c_not_processed(01) TYPE c VALUE 'N',

c_bwkey TYPE mbew-bwkey VALUE '1210',

c_periv LIKE t009b-periv VALUE 'P1',

c_true(1) TYPE c VALUE 'X',

c_false(1) TYPE c VALUE ' ',

c_kopos_01 LIKE konp-kopos VALUE '01',

c_kvewe_pricing LIKE t682i-kvewe VALUE 'A',

c_kappl_sd LIKE t682i-kappl VALUE 'V',

c_retech TYPE c VALUE '2',

c_non_retech TYPE c VALUE '3',

c_kozgf_zpr0 LIKE t682i-kozgf VALUE 'ZPR0'.

* S E L E C T I O N S C R E E N

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK a0 WITH FRAME.

PARAMETERS: p_auto RADIOBUTTON GROUP rad1 DEFAULT 'X'.

SELECTION-SCREEN SKIP 1.

PARAMETERS: p_delim(01) TYPE c DEFAULT '|',

p_days(3) TYPE n DEFAULT '180',

p_pathi(60) TYPE c LOWER CASE,

p_pathp(60) TYPE c LOWER CASE.

SELECTION-SCREEN SKIP 3.

* Selction screen for manual running of program for

* refreshing data

PARAMETERS: p_man RADIOBUTTON GROUP rad1.

SELECTION-SCREEN SKIP 1.

*SELECT-OPTIONS:s_gjahr FOR zpromo_mast-gjahr no-extension NO INTERVALS,

* s_monat FOR zpromo_mast-monat no-extension NO INTERVALS.

SELECT-OPTIONS: s_kunnr FOR zpromo_trans-kunnr,

s_saledt FOR zpromo_trans-sale_dt.

SELECTION-SCREEN END OF BLOCK a0.

SELECTION-SCREEN SKIP 3.

PARAMETERS: p_kschlw LIKE konp-kschl DEFAULT 'ZPR0'.

* A T S E L E C T I O N - S C R E E N

AT SELECTION-SCREEN.

IF p_auto = 'X'.

IF p_pathi IS INITIAL OR

p_pathp IS INITIAL.

MESSAGE e001 WITH 'Please Enter In and Out Unix Path'.

ENDIF.

ENDIF.

* I N I T I A L I Z A T I O N

INITIALIZATION.

* S T A R T - O F - S E L E C T I O N

START-OF-SELECTION.

IF p_auto = 'X'.

* Begin of Logging Process

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'S'

status = 'S'

MESSAGE = 'Program started (Automatic Mode)'.

* End of Logging Process

PERFORM auto_upload_process.

* Begin of Logging Process

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'E'

status = 'S'

MESSAGE = 'Program ended (Automatic Mode)'.

* End of Logging Process

ELSE.

* Begin of Logging Process

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'S'

status = 'S'

MESSAGE = 'Program started (Manual Mode)'.

* End of Logging Process

PERFORM manual_refresh_data.

* Begin of Logging Process

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'E'

status = 'S'

MESSAGE = 'Program ended (Manual Mode)'.

* End of Logging Process

ENDIF.


* E N D - O F - S E L E C T I O N .

END-OF-SELECTION.




*&---------------------------------------------------------------------*

*& Form upload_to_missfile

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM upload_to_missfile.

DATA l_kunnr LIKE kna1-kunnr.

* Modify the table

LOOP AT transdata.

* The first record of the file contains '*'

IF transdata-data(1) = '*'.

CLEAR t_head.

condense transdata-data no-gaps.

SPLIT transdata-data AT p_delim INTO

t_head-rec_indicator

t_head-period_code

t_head-rec_count.

IF sy-subrc NE 0.

ENDIF.

ELSE.

CLEAR t_mdays.

SPLIT transdata-data AT p_delim INTO

t_mdays-cust_code

t_mdays-store_id

t_mdays-date_code.

IF sy-subrc = 0.

CHECK NOT t_mdays-cust_code IS INITIAL.

CONDENSE t_mdays-store_id NO-GAPS.

* Convert Customer Number to Internal Format

CLEAR l_kunnr.

MOVE t_mdays-cust_code TO l_kunnr.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = l_kunnr

IMPORTING

output = l_kunnr.

MOVE l_kunnr TO t_mdays-cust_code.


* move the date of file into

MOVE: t_head-period_code(4) TO t_mdays-gjahr,

t_head-period_code+4(2) TO t_mdays-monat.

APPEND t_mdays.

ENDIF.

ENDIF.

ENDLOOP.

* Check if the Number of Records Passed match the number in the file

DESCRIBE TABLE t_mdays LINES l_count.

MOVE t_head-rec_count TO l_rec_count.

IF l_count NE l_rec_count.

* The Record count does not match hence give error message

* Give an error message and exit

* WRITE: / 'Record Counts do not Match','Records in Table:',l_count,

* 'Record Count in File Header:',l_rec_count.

* EXIT.

ENDIF.

* Load the Data in the Table

INSERT zpromo_missdays FROM TABLE t_mdays

ACCEPTING DUPLICATE KEYS.

IF sy-subrc = 0.

MOVE sy-dbcnt TO l_mdays_count.

ENDIF.

COMMIT WORK.

ENDFORM.

*&---------------------------------------------------------------------*

*& Form lock_trans_table

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM lock_trans_table.

CALL FUNCTION 'ENQUEUE_EZPROMO_TRANS'

* EXPORTING

* MODE_ZPROMO_TRANS = 'E'

* MANDT = SY-MANDT

* ZPROMO_ID =

* KUNNR =

* MATNR =

* X_ZPROMO_ID = ' '

* X_KUNNR = ' '

* X_MATNR = ' '

* _SCOPE = '2'

* _WAIT = ' '

* _COLLECT = ' '

* EXCEPTIONS

* FOREIGN_LOCK = 1

* SYSTEM_FAILURE = 2

* OTHERS = 3

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDFORM. " lock_trans_table

*&---------------------------------------------------------------------*

*& Form unlock_trans_table

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM unlock_trans_table.

CALL FUNCTION 'DEQUEUE_EZPROMO_TRANS'

* EXPORTING

* MODE_ZPROMO_TRANS = 'E'

* MANDT = SY-MANDT

* ZPROMO_ID =

* KUNNR =

* MATNR =

* X_ZPROMO_ID = ' '

* X_KUNNR = ' '

* X_MATNR = ' '

* _SCOPE = '3'

* _SYNCHRON = ' '

* _COLLECT = ' '.

.

ENDFORM. " unlock_trans_table

*&---------------------------------------------------------------------*

*& Form get_all_scan_files

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM get_all_scan_files.

CLEAR: w_command,

w_additional_parameter.

MOVE: 'Z_LS' TO w_command,

p_pathi TO w_additional_parameter.

PERFORM system_command USING w_command w_additional_parameter.

ENDFORM. " get_all_scan_files

*----------------------------------------------------------------------*

* Form SYSTEM_COMMAND

*----------------------------------------------------------------------*

* Execute a system command. *

*----------------------------------------------------------------------*

* --> W_COMMAND System command

* --> W_ADDITIONAL_PARAMETER Additional parameter for command

* <-- T_EXEC_PROTOCOL Results from command

*----------------------------------------------------------------------*

FORM system_command

USING

w_command LIKE sxpgcolist-name

w_additional_parameter LIKE sxpgcolist-parameters.

REFRESH t_exec_protocol.

CALL FUNCTION 'SXPG_COMMAND_EXECUTE'

EXPORTING

commandname = w_command

additional_parameters = w_additional_parameter

IMPORTING

status = w_status

TABLES

exec_protocol = t_exec_protocol

EXCEPTIONS

no_permission = 1

command_not_found = 2

parameters_too_long = 3

security_risk = 4

wrong_check_call_interface = 5

program_start_error = 6

program_termination_error = 7

x_error = 8

parameter_expected = 9

too_many_parameters = 10

illegal_command = 11

wrong_asynchronous_parameters = 12

cant_enq_tbtco_entry = 13

jobcount_generation_error = 14

OTHERS = 15.

ENDFORM. " B0300_SYSTEM_COMMAND

*---------------------------------------------------------------------*

* FORM upload_from_unix *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

* --> P_FILE *

*---------------------------------------------------------------------*

FORM upload_from_unix USING p_file.

REFRESH transdata.

*>>>>> Begin of changes SDVK970048 - Unicode

* OPEN DATASET p_file FOR INPUT IN TEXT MODE.

OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

*<<<<< End of changes SDVK970048 - Unicode

IF syst-subrc <> 0.

WRITE: / 'ERROR:', p_file, ' not found'.

EXIT.

ENDIF.

DO.

READ DATASET p_file INTO transdata.

IF syst-subrc <> 0.

APPEND transdata.

EXIT.

ELSE.

APPEND transdata.

ENDIF.

ENDDO.

WRITE: / 'Upload from unix completed:'.

ENDFORM.

*&---------------------------------------------------------------------*

*& Form move_file

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM move_file USING p_file p_filep.

CLEAR: w_command,

w_additional_parameter.

MOVE: 'Z_MV' TO w_command.

* Determine the additional parameter for the move command.

CLEAR w_additional_parameter.

CONCATENATE: p_file

p_filep

INTO w_additional_parameter

SEPARATED BY space.

* Perform the Unix command

PERFORM system_command USING w_command w_additional_parameter.

ENDFORM. " move_file

*&---------------------------------------------------------------------*

*& Form process_missing_days

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM process_missing_days.

DATA: l_day TYPE p.

DATA: l_day_sale TYPE p.

DATA l_kunnr LIKE kna1-kunnr.

DATA l_mast LIKE zpromo_mast.

DATA l_matnr LIKE mara-matnr.

DATA l_prdh4(4) TYPE c.

LOOP AT t_mdays.

REFRESH: t_bscan_data,t_bscan_tot.

MOVE t_mdays-cust_code TO l_kunnr.

* Check if Valid Customer.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = l_kunnr

IMPORTING

output = l_kunnr.

SELECT SINGLE kunnr FROM kna1 INTO kna1-kunnr

WHERE kunnr = l_kunnr.

IF sy-subrc NE 0.

w_error_cnt = w_error_cnt + 1.

WRITE:/ 'Invalid Customer Number:',l_kunnr.

CONTINUE.

ENDIF.

* Check if there was a valid promotion running for the customer

* at this date

* Internal Table t_mast has all the promotions

CLEAR: w_found_fl,l_mast.

LOOP AT it_mast

WHERE kunnr = l_kunnr

AND start_dt <= t_mdays-date_code

AND end_dt >= t_mdays-date_code.

MOVE-CORRESPONDING it_mast TO l_mast.

MOVE 'Y' TO w_found_fl.

EXIT.

ENDLOOP.

IF w_found_fl IS INITIAL.

w_error_cnt = w_error_cnt + 1.

WRITE: /'No Promotion Records found for:',

l_kunnr,

t_mdays-date_code.

CONTINUE.

ENDIF.


* Check if a record exists for the missing day in transaction table

* There should not be any record for that day as that would signify

* that there was no technical failure

* May need Index support

SELECT SINGLE * FROM zpromo_trans

WHERE kunnr = l_kunnr

AND sale_dt = t_mdays-date_code.

IF sy-subrc = 0.

w_error_cnt = w_error_cnt + 1.

WRITE:/ 'Record already exists in Missing Days Table:',

l_kunnr,

t_mdays-date_code.

CONTINUE.

ENDIF.


* get the day of the week for the Missing Day

CLEAR l_day.

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

datum = t_mdays-date_code

IMPORTING

wotnr = l_day.

* Get the Promotion Period and Year

READ TABLE it_per WITH KEY date = t_mdays-date_code BINARY SEARCH.

CHECK sy-subrc = 0.

* Get all the records for the whole promotion Period in which

* the promotion was running from Batscan Holding Table for the same day

* of the week

REFRESH: t_bscan_data,t_bscan_tot.

SELECT * FROM zpromo_bscan

INTO CORRESPONDING FIELDS OF TABLE t_bscan_data

WHERE cust_code = l_kunnr

AND gjahr = it_per-year

AND monat = it_per-period

AND day_of_week = l_day.

IF sy-subrc NE 0.

w_error_cnt = w_error_cnt + 1.

WRITE:/ 'No Records found in ZPROMO_BSCAN table:',

l_kunnr,

it_per-year,

it_per-period,

l_day.

CONTINUE.

ENDIF.

* Convert all the quantities into Cartons to find out the

* Day of Maximum Sale. We only consider the Cigarette Sales

* so any other Unit of Measure apart frm carton and Packs should

* be ingnored.

* all the quantities are converted to carton and added for each date

LOOP AT t_bscan_data.

CLEAR l_matnr.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = t_bscan_data-ubc_code

IMPORTING

output = l_matnr.

* Build the table IT_MARA which has all material and UOM

MOVE: l_matnr TO it_mara-matnr,

t_bscan_data-uom_code TO it_mara-uom.

APPEND it_mara.

*

CLEAR mara.

SELECT SINGLE * FROM mara WHERE matnr = l_matnr.

CHECK mara-prdha(4) = '1CIG'.

* CHECK t_bscan_data-uom_code = 'CTN' OR

* t_bscan_data-uom_code = 'PKT'.

*

PERFORM convert_units

USING l_matnr t_bscan_data-tot_quantity t_bscan_data-uom_code.

MOVE-CORRESPONDING t_bscan_data TO t_bscan_tot.

COLLECT t_bscan_tot.

ENDLOOP.


* sort the table t_bscan_tot descending, this will bring the highest

* quantity to the top. Reading the first row will give the date with

* highest quantity

SORT t_bscan_tot BY tot_quantity DESCENDING.

READ TABLE t_bscan_tot INDEX 1.

* We Need to populate the values in the missing days table

CLEAR zpromo_missdays.

SELECT SINGLE * FROM zpromo_missdays

WHERE cust_code = l_kunnr

AND store_id = t_mdays-store_id

AND date_code = t_mdays-date_code.

IF sy-subrc = 0.

CLEAR: zpromo_missdays-date1,

zpromo_missdays-act_vol1,

zpromo_missdays-date2,

zpromo_missdays-act_vol2,

zpromo_missdays-date3,

zpromo_missdays-act_vol3,

zpromo_missdays-date4,

zpromo_missdays-act_vol4.

LOOP AT t_bscan_tot.

IF sy-tabix = '1'.

MOVE: t_bscan_tot-date_code TO zpromo_missdays-date1,

t_bscan_tot-tot_quantity TO zpromo_missdays-act_vol1.

ELSEIF sy-tabix = '2'.

MOVE: t_bscan_tot-date_code TO zpromo_missdays-date2,

t_bscan_tot-tot_quantity TO zpromo_missdays-act_vol2.

ELSEIF sy-tabix = '3'.

MOVE: t_bscan_tot-date_code TO zpromo_missdays-date3,

t_bscan_tot-tot_quantity TO zpromo_missdays-act_vol3.

ELSEIF sy-tabix = '4'.

MOVE: t_bscan_tot-date_code TO zpromo_missdays-date4,

t_bscan_tot-tot_quantity TO zpromo_missdays-act_vol4.

ENDIF.

ENDLOOP.

* update the table zpromo_missdays

* UPDATE zpromo_missdays SET: date1 = zpromo_missdays-date1,

* act_vol1 = zpromo_missdays-act_vol1,

* date2 = zpromo_missdays-date2,

* act_vol2 = zpromo_missdays-act_vol2,

* date3 = zpromo_missdays-date3,

* act_vol3 = zpromo_missdays-act_vol3,

* date4 = zpromo_missdays-date4,

* act_vol4 = zpromo_missdays-act_vol4

* WHERE cust_code = l_kunnr

* AND store_id = t_mdays-store_id

* AND date_code = t_mdays-date_code.

MODIFY zpromo_missdays.

IF sy-subrc NE 0.

WRITE: / 'Error in updating zpromo_missdays',t_mdays-cust_code,

t_mdays-store_id,t_mdays-date_code.

ENDIF.

COMMIT WORK.

ENDIF.

* Get all the material fields and store in internal table

* PERFORM get_material_details.



* sort the table t_bscan_tot descending, this will bring the highest

* quantity to the top. Reading the first row will give the date with

* highest quantity

SORT t_bscan_tot BY tot_quantity DESCENDING.

READ TABLE t_bscan_tot INDEX 1.

* get all the records for the date with highest sale

* we should only select the products which are on promotion

* REFRESH t_bscan.

LOOP AT t_bscan_data WHERE date_code = t_bscan_tot-date_code.

CLEAR l_matnr.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = t_bscan_data-ubc_code

IMPORTING

output = l_matnr.

* Convert the Material Number to Sub Trade mark

CLEAR mara.

SELECT SINGLE * FROM mara WHERE matnr = l_matnr.

MOVE mara-prdha+12(4) TO l_prdh4.

* Certain sub trademarks or product hierarchy can be grouped

* check if the product hierarchy belongs to a group.

* if yes then get the group value.

READ TABLE t_group WITH KEY sub_trademark = l_prdh4.

IF sy-subrc = 0.

MOVE t_group-sub_trademark_gr TO l_prdh4.

ENDIF.


* Check if there was a valid promotion running for the customer

* at the Missing day

* Internal Table t_mast has all the promotions

CLEAR: w_found_fl,l_mast.

LOOP AT it_mast

WHERE kunnr = l_kunnr

AND start_dt <= t_mdays-date_code

AND end_dt >= t_mdays-date_code

AND sub_trademark = l_prdh4 .

MOVE-CORRESPONDING it_mast TO l_mast.

MOVE 'Y' TO w_found_fl.

EXIT.

ENDLOOP.

CHECK w_found_fl = 'Y'.

MOVE-CORRESPONDING t_bscan_data TO t_bscan.

* substitute the Missing date to the data of the highest sale date

MOVE t_mdays-date_code TO t_bscan-date_code.

APPEND t_bscan.

ENDLOOP.

ENDLOOP.

ENDFORM. " process_missing_days

*&---------------------------------------------------------------------*

*& Form get_material_details

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM get_material_details.

DATA: w_astab1(4) TYPE c.

DATA: w_astab2(4) TYPE c.

DATA l_knumh LIKE konp-knumh.

* Read the Materials into an internal table. This table

* will be used to get the level 4 product hierarchy

* SELECT matnr prdha INTO CORRESPONDING FIELDS OF TABLE it_mara

* FROM mara.

* Read the Materials into an internal table. This table

* will be used to get the level 4 product hierarchy

SORT it_mara BY matnr uom.

DELETE ADJACENT DUPLICATES FROM it_mara.

* Read the condition types.

* These condition type access sequence tables are read dynamically

* because they vary from system to system.

* Read the access sequence tables for the pricing.

PERFORM c0200_read_access_seq_tables.

* Get all the Marm Records

LOOP AT it_mara.

SELECT SINGLE prdha INTO mara-prdha

FROM mara WHERE matnr = it_mara-matnr.

CHECK sy-subrc = 0.

* Get the Level 4 Product hierarchy

MOVE mara-prdha+12(4) TO it_mara-prdha4.

* Certain sub trademarks or product hierarchy can be grouped

* check if the product hierarchy belongs to a group.

* if yes then get the group value.

READ TABLE t_group WITH KEY sub_trademark = it_mara-prdha4.

IF sy-subrc = 0.

MOVE t_group-sub_trademark_gr TO it_mara-prdha4.

ENDIF.


** Clear KONP

* CLEAR konp.

*

** Check if Matarial Level Pricing is defined

* clear l_knumh.

* SELECT SINGLE knumh FROM (W_ASTAB1) into l_knumh

* WHERE kappl = 'V'

* AND kschl = 'ZPR0'

* AND vkorg = 'RPMA'

* AND matnr = it_mara-matnr

* AND datbi >= sy-datum

* AND datab <= sy-datum.

* IF sy-subrc = 0.

* SELECT SINGLE * FROM konp

* WHERE knumh = l_knumh.

*

* ELSE.

* clear l_knumh.

* SELECT SINGLE knumh FROM (W_ASTAB2) into l_knumh

* WHERE kappl = 'V'

* AND kschl = 'ZPR0'

* AND vkorg = 'RPMA'

* AND zzprodh4 = it_mara-prdha4

* AND datbi >= sy-datum

* AND datab <= sy-datum.

*

* CLEAR konp.

* SELECT SINGLE * FROM konp

* WHERE knumh = l_knumh.

*

*

* ENDIF.

*

** Check if there is some Value in KONP

* IF NOT konp IS INITIAL.

*

* MOVE: konp-kbetr TO it_mara-kbetr,

* konp-kpein TO it_mara-kpein,

* konp-kmein TO it_mara-kmein.

* ENDIF.

* Determine the wholesale and recommended retail prices.

SELECT SINGLE * FROM mvke WHERE matnr = it_mara-matnr.

CLEAR: w_wholesale_price.

PERFORM d0200_determine_price USING p_kschlw

CHANGING w_wholesale_price.

MODIFY it_mara.

ENDLOOP.

SORT it_mara BY matnr uom.



ENDFORM. " get_material_details

*&---------------------------------------------------------------------*

*& Form UPLOAD_TO_trans_table

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM upload_to_trans_table.

DATA l_date LIKE sy-datum.

DATA l_kunnr LIKE kna1-kunnr.

DATA l_mast LIKE it_mast.

DATA l_exclude(1) TYPE c.

REFRESH t_trans.

*

DATA l_qty LIKE plfh-mgvgw.

DATA l_value LIKE plfh-mgvgw.

DATA l_valid(1) TYPE c.

DATA l_wprice LIKE zpromo_trans-wholesale_price.

*

* Validate the Data

LOOP AT t_bscan.

CLEAR t_trans.

* Convert UBC Code to Internal format

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = t_bscan-ubc_code

IMPORTING

output = t_trans-matnr.

* Read the Internal Table IT_MARA to get level 4 product Hierarchy

* and wholesale price

READ TABLE it_mara WITH KEY matnr = t_trans-matnr

uom = t_bscan-uom_code BINARY SEARCH.

IF sy-subrc = 0.

MOVE: it_mara-prdha4 TO t_trans-sub_trademark.

MOVE it_mara-kpein TO l_value.

* Now Convert the Unit to the Unit of the Line

CALL FUNCTION 'CF_UT_UNIT_CONVERSION'

EXPORTING

matnr_imp = it_mara-matnr

* MEINS_IMP = ' '

unit_new_imp = it_mara-uom

unit_old_imp = it_mara-kmein

value_old_imp = l_value

IMPORTING

value_new_exp = l_qty

EXCEPTIONS

overflow = 1

OTHERS = 2

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

IF l_qty NE 0.

t_trans-wholesale_price = it_mara-kbetr / l_qty.

ENDIF.

ENDIF.

MOVE t_bscan-date_code TO t_trans-sale_dt.

READ TABLE it_per WITH KEY date = t_trans-sale_dt.

* Customer

MOVE t_bscan-cust_code TO l_kunnr.

* Convert UBC Code to Internal format

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = l_kunnr

IMPORTING

output = t_trans-kunnr.

* Check if the Record falls into Current Promotional Month/Period and

* between the start and end date

CLEAR l_valid.

LOOP AT it_mast WHERE kunnr = t_trans-kunnr

AND sub_trademark = t_trans-sub_trademark

AND gjahr = it_per-year

AND monat = it_per-period

AND start_dt <= t_trans-sale_dt

AND end_dt >= t_trans-sale_dt.

MOVE 'Y' TO l_valid.

ENDLOOP.

CHECK l_valid = 'Y'.

* READ TABLE it_mast WITH KEY kunnr = t_trans-kunnr

* sub_trademark = t_trans-sub_trademark

* gjahr = it_per-curr_year

* monat = it_per-curr_period

* BINARY SEARCH.

* CHECK sy-subrc = 0.

* Now Validate the Record

SELECT SINGLE * FROM kna1 WHERE kunnr = t_trans-kunnr.

IF sy-subrc NE 0.

w_error_cnt = w_error_cnt + 1.

WRITE: 'Invalid Customer Number:',t_trans-kunnr.

CONTINUE.

ENDIF.

CLEAR mara.

SELECT SINGLE * FROM mara WHERE matnr = t_trans-matnr.

IF sy-subrc NE 0.

w_error_cnt = w_error_cnt + 1.

WRITE: 'Invalid Material Number:',t_trans-matnr.

CONTINUE.

ENDIF.

* In the sub trade mark for Roll your Own(RYO) only pickup

* products which have UOM 'PCH'

CLEAR l_exclude.

IF mara-prdha(4) = '1RYO'.

IF t_bscan-uom_code NE 'PCH'.

l_exclude = 'Y'.

ENDIF.

ENDIF.

CHECK l_exclude IS INITIAL.




MOVE it_mast-zpromo_id TO t_trans-zpromo_id.

MOVE t_bscan-subbanner_code TO t_trans-cg3_val.

MOVE t_bscan-store_id TO t_trans-batscan_id.

MOVE t_bscan-uom_code TO t_trans-uom.

MOVE t_bscan-ean_code TO t_trans-ean11.

MOVE t_bscan-tot_quantity TO t_trans-act_vol.

* Calculate the Retail Quantity based on the Total and Wholesale Qty

* passed in the file

IF t_bscan-whs_quantity = 0.

t_trans-retl_vol = ( it_mast-retail_perc * t_trans-act_vol ) / 100.

ELSE.

t_trans-retl_vol = t_bscan-tot_quantity - t_bscan-whs_quantity.

ENDIF.

MOVE t_bscan-norm_sell_price TO t_trans-bookprice.

MOVE t_bscan-avg_retail_price TO t_trans-prom_price.

* Calculation of the Norm_price which is calculated by applying the

* mark up percentages to the wholesale prices

* we need to add GST to Wholesale price to calculate the normal price

MOVE t_trans-wholesale_price TO l_wprice.

l_wprice = l_wprice * ( 1 + ( w_gst / 100 ) ) .

IF t_bscan-uom_code = 'CTN '.

t_trans-norm_price =

l_wprice * ( 1 + ( it_mast-ctn_markup / 100 ) ).

ELSEIF t_bscan-uom_code = 'PKT '.

t_trans-norm_price =

l_wprice * ( 1 + ( it_mast-pkt_markup / 100 ) ).

ELSEIF t_bscan-uom_code = 'PCH '.

t_trans-norm_price =

l_wprice * ( 1 + ( it_mast-pch_markup / 100 ) ).

ENDIF.

* MOVE T_BSCAN-AVG_WHSALE_PRICE TO T_TRANS-WHOLESALE_PRICE.

*

MOVE 'T' TO t_trans-failure_reason.

MOVE sy-uname TO t_trans-created_by.

MOVE sy-datum TO t_trans-creation_dt.

* Insert the Record

APPEND t_trans.

ENDLOOP.




ENDFORM. " UPLOAD_TO_trans_table

*---------------------------------------------------------------------*

* FORM write_report *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

FORM write_report.

WRITE: /'Input files processed :',w_file,

/'Number of records read from input file', 75 l_count,

/'Number of records stored in holding table ZPROMO_MISSDAYS',

75 l_mdays_count,

/'Number of records updated in the ZPROM_TRANS table',

75 l_trans_count,

/'Number of records in error:', 75 w_error_cnt.

* Begin of logging process

CLEAR: l_message, l_log_counter1.

MOVE l_count TO l_log_counter1.

CONCATENATE 'Input file '

w_file

' processed'

INTO l_message.

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'FO'

status = 'I'

MESSAGE = l_message.

CLEAR: l_message, l_log_counter1.

MOVE l_count TO l_log_counter1.

CONCATENATE 'Number of records read from input file:'

l_log_counter1

INTO l_message.

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'DP'

status = 'I'

MESSAGE = l_message.

CLEAR: l_message, l_log_counter1.

MOVE l_mdays_count TO l_log_counter1.

CONCATENATE 'Number of records stored in'

'holding table ZPROMO_MISSDAYS'

l_log_counter1

INTO l_message.

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'DP'

status = 'I'

MESSAGE = l_message.

CLEAR: l_message, l_log_counter1.

MOVE l_trans_count TO l_log_counter1.

CONCATENATE 'Number of records updated in the ZPROM_TRANS table'

l_log_counter1

INTO l_message.

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'DP'

status = 'I'

MESSAGE = l_message.

IF w_error_cnt NE 0.

CLEAR: l_message, l_log_counter1.

MOVE w_error_cnt TO l_log_counter1.

CONCATENATE 'Number of records in error:'

l_log_counter1

INTO l_message.

CALL FUNCTION 'ZFPRD_CREATE_MESSAGE_LOG'

EXPORTING

source_program = 'ZFPRD005'

step = 'DP'

status = 'E'

MESSAGE = l_message.

ENDIF.

* End of Logging Process

ENDFORM. " write_report

*---------------------------------------------------------------------*

* FORM get_promotions *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

FORM get_promotions.

DATA: BEGIN OF it_period OCCURS 0,

year TYPE t009b-bdatj,

period TYPE t009b-poper,

END OF it_period.

LOOP AT it_per.

MOVE-CORRESPONDING it_per TO it_period.

APPEND it_period.

ENDLOOP.

SORT it_period.

DELETE ADJACENT DUPLICATES FROM it_period.

IF NOT it_period[] IS INITIAL.

SELECT * FROM

zpromo_mast INTO CORRESPONDING FIELDS OF TABLE it_mast

FOR ALL ENTRIES IN it_period

WHERE gjahr = it_period-year

AND monat = it_period-period

AND scan_type = '2'.

ENDIF.

ENDFORM. " get_promotions

*---------------------------------------------------------------------*

* FORM build_period_table *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

FORM build_period_table.

DATA: BEGIN OF it_date OCCURS 0,

date TYPE d,

END OF it_date.

DATA l_date LIKE sy-datum.

* Build the date Table

LOOP AT t_mdays.

MOVE t_mdays-date_code TO it_date-date.

APPEND it_date.

ENDLOOP.

SORT it_date.

DELETE ADJACENT DUPLICATES FROM it_date.

LOOP AT it_date.

CHECK NOT it_date-date IS INITIAL.

* Get the Current Year and Period for Promotion Calender

CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'

EXPORTING

i_date = it_date-date

i_periv = c_periv

IMPORTING

e_buper = w_buper

e_gjahr = w_gjahr

EXCEPTIONS

input_false = 1

t009_notfound = 2

t009b_notfound = 3

OTHERS = 4.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

MOVE: it_date-date TO it_per-date,

w_buper TO it_per-period,

w_gjahr TO it_per-year.

APPEND it_per.

ENDLOOP.

SORT it_per BY date.

ENDFORM. " build_period_table

*---------------------------------------------------------------------*

* FORM convert_units *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

* --> QTY *

* --> UOM *

*---------------------------------------------------------------------*

FORM convert_units USING matnr qty

uom.

DATA l_qty LIKE plfh-mgvgw.

DATA l_value LIKE plfh-mgvgw.

MOVE qty TO l_value.

* Now Convert the Unit to the Unit of the Line

CALL FUNCTION 'CF_UT_UNIT_CONVERSION'

EXPORTING

matnr_imp = matnr

* MEINS_IMP = ' '

unit_new_imp = 'STK'

unit_old_imp = uom

value_old_imp = l_value

IMPORTING

value_new_exp = l_qty

EXCEPTIONS

overflow = 1

OTHERS = 2.

IF sy-subrc = 0.

MOVE l_qty TO qty.

ENDIF.

ENDFORM. " convert_units

*---------------------------------------------------------------------*

* FORM d0200_determine_price *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

* --> W_KSCHL *

* --> W_KBETR *

*---------------------------------------------------------------------*

FORM d0200_determine_price USING w_kschl LIKE konp-kschl

CHANGING w_kbetr LIKE konp-kbetr.

DATA: w_knumh LIKE konp-knumh.

* Clear the discount amount.

CLEAR: w_knumh,

w_kbetr.

* Determine the condition record number.

LOOP AT t_astab_2

WHERE kschl = w_kschl.

CLEAR w_varkey.

LOOP AT t_dd03l

WHERE tabname = t_astab_2-tabname.

CASE t_dd03l-fieldname.

WHEN 'VKORG'.

CONCATENATE w_varkey

'RPMA' "MVKE-VKORG

INTO w_varkey.

WHEN 'MATNR'.

CONCATENATE w_varkey

it_mara-matnr

INTO w_varkey.

WHEN 'ZZPRODH4'.

CONCATENATE w_varkey

it_mara-prdha4

INTO w_varkey.

ENDCASE.

ENDLOOP.

READ TABLE t_astab_3 INDEX sy-tabix.

CLEAR w_string_1.

CONCATENATE t_astab_2-tabname

t_astab_2-kappl

t_astab_2-kschl

t_astab_2-varkey

INTO w_string_1.

CLEAR w_string_2.

CONCATENATE t_astab_2-tabname

t_astab_2-kappl

t_astab_2-kschl

w_varkey

t_astab_3-datbi

t_astab_3-datab

t_astab_3-knumh

INTO w_string_2.

IF w_string_1 = w_string_2.

w_knumh = t_astab_3-knumh.

EXIT.

ENDIF.

ENDLOOP.

* Read the condition items to obtain the discount.

IF sy-subrc = 0.

READ TABLE t_konp

WITH KEY knumh = w_knumh

kopos = c_kopos_01

BINARY SEARCH.

IF sy-subrc = 0.

MOVE: t_konp-kbetr TO it_mara-kbetr,

t_konp-kpein TO it_mara-kpein,

t_konp-kmein TO it_mara-kmein.

* PERFORM E0200_DETERM_SALES_UOM_QTY.

*

* W_KBETR = T_KONP-KBETR / W_SALES_UOM_QUANTITY.

ENDIF.

ENDIF.

ENDFORM. " D0200_DETERMINE_PRICE

*---------------------------------------------------------------------*

* FORM c0200_read_access_seq_tables *

*---------------------------------------------------------------------*

* ........ *

*---------------------------------------------------------------------*

FORM c0200_read_access_seq_tables.

* Read the condition types.

* These condition type access sequence tables are read dynamically

* because they vary from system to system.

SELECT * FROM t682i INTO TABLE t_t682i

WHERE kvewe = c_kvewe_pricing

AND kappl = c_kappl_sd

AND kozgf = c_kozgf_zpr0

ORDER BY kolnr.

LOOP AT t_t682i.

CLEAR w_astab.

CONCATENATE t_t682i-kvewe

t_t682i-kotabnr

INTO w_astab.

CLEAR w_astab_1.

SELECT * FROM (w_astab)

INTO w_astab_1

WHERE kappl = c_kappl_sd

AND kschl = p_kschlw

AND datbi >= sy-datum

AND datab <= sy-datum.

CLEAR t_astab_2.

MOVE: w_astab TO t_astab_2-tabname,

w_astab_1-kappl TO t_astab_2-kappl,

w_astab_1-kschl TO t_astab_2-kschl,

w_astab_1-varkey TO t_astab_2-varkey.

APPEND t_astab_2.

CLEAR w_astab_1.

ENDSELECT.

SELECT kappl kschl datbi datab knumh

INTO (t_astab_3-kappl, t_astab_3-kschl, t_astab_3-datbi,

t_astab_3-datab, t_astab_3-knumh)

FROM (w_astab)

WHERE kappl = c_kappl_sd

AND kschl = p_kschlw

AND datbi >= sy-datum

AND datab <= sy-datum.

APPEND t_astab_3.

ENDSELECT.

SELECT * APPENDING TABLE t_dd03l

FROM dd03l

WHERE tabname = w_astab.

SORT t_dd03l BY tabname position.

ENDLOOP.

* Read the condition items.

SELECT knumh kopos kbetr konwa kmein kpein

FROM konp

INTO CORRESPONDING FIELDS OF TABLE t_konp

WHERE kappl = c_kappl_sd

AND kschl = p_kschlw

AND loevm_ko = c_false

ORDER BY knumh kopos.

ENDFORM. " C0200_READ_ACCESS_SEQ_TABLES

*&---------------------------------------------------------------------*

*& Form build_trans_table

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM build_trans_table.

DATA l_matnr LIKE mara-matnr.

DATA l_appr_id LIKE zpromo_trans-appr_id.

DATA l_appr_flag(1) TYPE c.

DATA: ti_trans TYPE zpromo_trans OCCURS 0 WITH HEADER LINE.

** Build the table IT_MARA which has all material and UOM

* LOOP AT t_bscan.

* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

* EXPORTING

* input = t_bscan-ubc_code

* IMPORTING

* output = l_matnr.

*

* MOVE: l_matnr TO it_mara-matnr,

* t_bscan-uom_code TO it_mara-uom.

* APPEND it_mara.

* ENDLOOP.

*

** Get all the material fields and store in internal table

PERFORM get_material_details.

* Load data in trans table

PERFORM upload_to_trans_table.

LOOP AT t_trans.

* Sanjiv Sharma 18/06/2003 Changes to allow multiple approvals

* Get the current approval ID. This is required to facilititate

* multiple approvals of the promotions

PERFORM get_approval_id USING l_appr_id l_appr_flag.

* Check to see if the record exists in Transaction Table

SELECT SINGLE * FROM zpromo_trans

WHERE zpromo_id = t_trans-zpromo_id

AND kunnr = t_trans-kunnr

AND sub_trademark = t_trans-sub_trademark

AND matnr = t_trans-matnr

AND uom = t_trans-uom

AND sale_dt = t_trans-sale_dt.

IF sy-subrc NE 0.

* We need to get the accrual flag from the current record

* so that the record is not accrued twice.

* MOVE zpromo_trans-accrual_flag TO t_trans-accrual_flag.

MOVE-CORRESPONDING t_trans TO zpromo_trans.

MOVE sy-mandt TO zpromo_trans-mandt.

CLEAR: zpromo_trans-accr_docno,zpromo_trans-rvrs_docno.

* Set the Change Date and Changed By fields in zpromo_trans

MOVE: sy-datum TO zpromo_trans-creation_dt,

sy-uname TO zpromo_trans-created_by.

* move the approval ID

IF l_appr_flag IS INITIAL.

zpromo_trans-appr_id = l_appr_id.

ELSE.

zpromo_trans-appr_id = l_appr_id + 1.

ENDIF.

MOVE-CORRESPONDING zpromo_trans TO ti_trans.

APPEND ti_trans.

* INSERT zpromo_trans.

ELSE.

* This record is approved and paid so write a message

WRITE:/ 'The data for:' COLOR COL_GROUP,

/ 'Promo Id :', t_trans-zpromo_id COLOR COL_GROUP,

/ 'Customer :', t_trans-kunnr COLOR COL_GROUP,

/ 'Sub Trade Mark:',t_trans-sub_trademark COLOR COL_GROUP,

/ 'Material :',t_trans-matnr COLOR COL_GROUP,

/ 'UOM :',t_trans-uom COLOR COL_GROUP,

/'Sales Date :',t_trans-sale_dt DD/MM/YYYY COLOR COL_GROUP,

/ 'Exists, Record Skipped' COLOR COL_GROUP.

SKIP 1.

ENDIF.

* ELSE.

*

*

** New Record so Insert

* MOVE-CORRESPONDING t_trans TO zpromo_trans.

* CLEAR: zpromo_trans-accr_docno,zpromo_trans-rvrs_docno.

*

** move the approval ID

* IF l_appr_flag IS INITIAL.

* zpromo_trans-appr_id = l_appr_id.

* ELSE.

* zpromo_trans-appr_id = l_appr_id + 1.

* ENDIF.

*

* INSERT zpromo_trans.

* IF sy-subrc EQ 0.

* l_trans_count = l_trans_count + 1.

** l_rec_inserted = l_rec_inserted + 1.

* ENDIF.

*

* ENDIF.

** move the approval ID

* IF l_appr_flag IS INITIAL.

* zpromo_trans-appr_id = l_appr_id.

* ELSE.

* zpromo_trans-appr_id = l_appr_id + 1.

* ENDIF.

*

*

*

* MODIFY zpromo_trans.

* IF sy-subrc EQ 0.

* l_trans_count = l_trans_count + 1.

* ENDIF.

ENDLOOP.

* Insert the data in the holding table

INSERT zpromo_trans FROM TABLE ti_trans.

* ACCEPTING DUPLICATE KEYS.

IF sy-subrc EQ 0.

l_trans_count = sy-dbcnt.

* l_rec_modified = l_rec_modified + 1.

ENDIF.

* Commit the Changes

COMMIT WORK.

* Modify the table

* Lock the table Promotion Transaction Table

* PERFORM LOCK_TRANS_TABLE.

* Insert the row in Transaction Table








* Unlock the Table

* PERFORM UNLOCK_TRANS_TABLE.

ENDFORM. " build_trans_table

*&---------------------------------------------------------------------*

*& Form auto_upload_process

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM auto_upload_process.

* Read the Input directory to get list of all batscan files

* This will allow to upload all the files which are not yet processed

PERFORM get_all_scan_files.

PERFORM read_group_table.

LOOP AT t_exec_protocol.

* check for relevent file

TRANSLATE t_exec_protocol-message TO UPPER CASE.

CHECK t_exec_protocol-message(13) = c_missf.

* build the input file name and Processed file names

CLEAR: w_file,w_filep.

CONCATENATE p_pathi t_exec_protocol-message(50) INTO w_file.

CONCATENATE p_pathp t_exec_protocol-message(50) INTO w_filep.

* Load the Unix file into Internal Table

PERFORM upload_from_unix USING w_file.

* Upload data to Table

PERFORM upload_to_missfile.

* Build the Periods Table for the dates in the Batscan file

PERFORM build_period_table.

* Get All the Promotions for the Current and Pre

PERFORM get_promotions.

* Process the Missing days records

PERFORM process_missing_days.

*

PERFORM build_trans_table.

* write report

PERFORM write_report.

* Move files to Processed directory

PERFORM move_file USING w_file w_filep.

ENDLOOP.

ENDFORM. " auto_upload_process

*&---------------------------------------------------------------------*

*& Form manual_refresh_data

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM manual_refresh_data.

PERFORM read_group_table.

* Read data from zpromo_bscan table

PERFORM read_missdays_data.

* Build the Periods Table for the dates in the Batscan file

PERFORM build_period_table.

* Get All the Promotions for the Current and Pre

PERFORM get_promotions.

* Process the Missing days records

PERFORM process_missing_days.

*

PERFORM build_trans_table.

* write report

PERFORM write_report.

ENDFORM. " manual_refresh_data

*&---------------------------------------------------------------------*

*& Form read_missdays_data

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM read_missdays_data.

DATA l_kunnr LIKE kna1-kunnr.

SELECT * FROM zpromo_missdays

WHERE cust_code IN s_kunnr

AND date_code IN s_saledt.

MOVE-CORRESPONDING zpromo_missdays TO t_mdays.

APPEND t_mdays.

ENDSELECT.


ENDFORM. " read_missdays_data

*&---------------------------------------------------------------------*

*& Form read_group_table

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM read_group_table.

* read all the sub trademark groupings

SELECT * FROM zpromo_group INTO TABLE t_group.

SORT t_group BY sub_trademark.

ENDFORM. " read_group_table

*&---------------------------------------------------------------------*

*& Form get_approval_id

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* -->P_L_APPR_ID text

*----------------------------------------------------------------------*

FORM get_approval_id USING l_appr_id l_appr_flag.

* Find out the Maximum Approval Id for the current record which

* has been approved

CLEAR: l_appr_id,l_appr_flag.

SELECT MAX( appr_id ) INTO l_appr_id

FROM zpromo_trans

WHERE zpromo_id = t_trans-zpromo_id

AND kunnr = t_trans-kunnr

AND sub_trademark = t_trans-sub_trademark

AND uom = t_trans-uom.

* Check if the record has been approved

SELECT SINGLE approval_fl INTO l_appr_flag

FROM zpromo_adjst

WHERE zpromo_id = t_trans-zpromo_id

AND kunnr = t_trans-kunnr

AND sub_trademark = t_trans-sub_trademark

AND uom = t_trans-uom

AND appr_id = l_appr_id.

ENDFORM. " get_approval_id

  • No labels