*&---------------------------------------------------------------------*
*& Report ZF_CREATE_TXGRP_IN_BSEG
*& Tax Group TXGRP in table BSEG is corrected using table BSET
* Report is attached to note 647424,
* please keep attachment up to date.
*----------------------------------------------------------------------
* TXGRP in BSET must be ok !!
* otherwise this report cannot be used
*
*&---------------------------------------------------------------------*
************************************************************************
* Last change:
* 2016-03-18: LINEWISE not only in India
* 2014-03-21: Adopt FORM fi_tax_set_txgrp to FACI FORM subst_taxps
* 2014-03-20: Print also BSET-TAXPS and BSEG-TAXPS
* 2014-03-18: Parameter LINEWISE: force tax calculation line-by-line
* makes sense only in IN and
* for glvor = RMRP (MIRO, MRKO) ??
* see FM CALCULATE_TAX_DOCUMENT
* 2013-06-10: not line-by-line -> clear bseg-taxps in all items
* 2012-03-30: bypass function 'FI_TAX_SET_TXGRP'
* 2012-03-23: note 1692637
* 2012-01-23: technical correction
* 2012-01-20: check bseg-taxps
* 2011-07-08
* clear ybseg-txgrp, -taxps for complete redetermination
* set bset-txgrp = bset-taxps
* 2011-06-08 note 1042117
* Dec. 2, 2005 use standard function in case of linewise
* tax calculation
* October 21, 2005: compressed manual tax line items in
* BSEG and/or BSET
* October 12, 2005:
* Redesign: applicable now also for company code without linewise tax
* calculation (see tr. OBCO) and MIRO documents with manual tax line
* items (see note 887711)
* output structure
DATA: BEGIN OF xoutput OCCURS 0,
bukrs LIKE bkpf-bukrs,
belnr LIKE bkpf-belnr,
gjahr LIKE bkpf-gjahr,
text(20) TYPE c,
corrupt TYPE c,
updated TYPE c,
END OF xoutput.
DATA: e_external,
e_linewise.
* counter
DATA: exist_bset_count TYPE i,
calc_bset_count TYPE i,
docs_checked TYPE i,
docs_corrupt TYPE i,
xcorrupt TYPE c,
icommit TYPE i.
*--------------------------------------------------------------------------
START-OF-SELECTION.
* store initial value of paramter linewise.
save_linewise = linewise. "2014-03-18
* select documents
SELECT * FROM bkpf INTO xbkpf "August 9, 2005
WHERE bukrs = p_bukrs "2014-03-18
AND belnr IN p_belnr
AND gjahr = p_gjahr "2014-03-18
AND budat IN p_budat
AND blart IN p_blart
AND awtyp IN p_awtyp
*"/ only posted, no statistical doc (no down paym. req)
AND bstat = space.
STOP.
ENDSELECT.
*
* check in table TTYPVX is also necessary "2014-03-18
* but new table TTYPVX may not be known in customer system !! "2014-03-18
* see notes 1916751 1779136 "2014-03-18
*
** begin of 2016-03-18
* SELECT * FROM ttypvx WHERE awtyp EQ xbkpf-awtyp
* AND ( bukrs EQ xbkpf-bukrs
* OR bukrs EQ space )
* AND ( blart EQ xbkpf-blart
* OR blart EQ space ).
* WRITE: / 'Actual document number: ', xbkpf-bukrs,
* xbkpf-belnr,
* xbkpf-gjahr.
* WRITE: / 'Actual Reference Procedure of the document: ',
* xbkpf-awtyp.
* WRITE: /
* 'Select only Reference Procedures without compression. STOP'.
*
* WRITE: / 'See notes 1779136, 1916751, 643273, 36353 and related ones'.
*
* STOP.
*
* ENDSELECT.
* ENDIF.
** end of 2016-03-18
ENDIF.
ENDIF.
old_bukrs = xbkpf-bukrs.
old_awtyp = xbkpf-awtyp.
ENDSELECT.
LOOP AT xbkpf.
xcorrupt = space.
docs_checked = docs_checked + 1.
* begin of 2016-03-18
IF l_t001-land1 = 'IN'.
CALL FUNCTION 'FUNCTION_EXISTS'
EXPORTING
funcname = 'J_1I4_CALCULATE_TAX_DOCUMENT'
EXCEPTIONS
function_not_exist = 1
OTHERS = 2.
IF sy-subrc IS INITIAL.
CALL FUNCTION 'J_1I4_CALCULATE_TAX_DOCUMENT' " see note 1872988
EXPORTING
t_bkpf = xbkpf
TABLES
t_bseg = xbseg "see note 2056080
CHANGING
c_flg_xtxit = e_linewise.
ENDIF.
ENDIF.
* end of 2016-03-18
IF testrun IS INITIAL
AND xcorrupt = 'X'.
* insert new TXGRP in BSEG
LOOP AT ybseg.
UPDATE bseg SET txgrp = ybseg-txgrp
taxps = ybseg-taxps "change 2012-03-23
mwskz = ybseg-mwskz "change 2012-03-23
txjcd = ybseg-txjcd "change 2012-03-23
WHERE bukrs = ybseg-bukrs
AND belnr = ybseg-belnr
AND gjahr = ybseg-gjahr
AND buzei = ybseg-buzei.
ENDLOOP.
xoutput-updated = 'X'.
icommit = icommit + 1.
ENDIF.
APPEND xoutput.
IF xcorrupt = 'X'.
docs_corrupt = docs_corrupt + 1.
ENDIF.
IF icommit GT 500.
COMMIT WORK.
CLEAR icommit.
ENDIF.
ENDLOOP. "xbkpf
COMMIT WORK.
PERFORM write_output_documents.
END-OF-SELECTION.
**********************************************************************
* form write_output_documents
**********************************************************************
FORM write_output_documents.
LOOP AT xoutput.
WRITE: /(6) xoutput-bukrs,
(12) xoutput-belnr,
(6) xoutput-gjahr,
(8) xoutput-corrupt,
(8) xoutput-updated,
(20) xoutput-text.
ENDLOOP.
NEW-LINE. ULINE.
FORMAT COLOR COL_TOTAL.
WRITE: / 'Documents checked:', docs_checked.
WRITE: / 'Documents corrupt:', docs_corrupt.
FORMAT COLOR OFF.
NEW-PAGE.
FORMAT COLOR COL_GROUP.
WRITE: 'Checked documents: Please compare old and new BSEG with BSET'.
FORMAT COLOR COL_HEADING.
* WRITE: / 'STA CompC Doc.No Year POS TXG S TC JurCode'.
WRITE: / 'STA CompC Doc.No Year POS TXG TAXPS S TC JurCode'. "2014-03-
20
LOOP AT xoutput.
IF onlycorr = 'X'.
CHECK xoutput-corrupt ='X'.
ENDIF.
ULINE.
IF xoutput-corrupt ='X'.
FORMAT COLOR COL_NEGATIVE.
WRITE: / 'Tax Group TXGRP in table BSEG seems to be wrong'.
WRITE: '- Please check'.
FORMAT COLOR COL_NORMAL.
ENDIF.
* write bset
PERFORM write_output_bset USING doc_key-bukrs
doc_key-belnr
doc_key-gjahr.
* write bseg
LOOP AT bseg_output WHERE bukrs = doc_key-bukrs
AND belnr = doc_key-belnr
AND gjahr = doc_key-gjahr.
IF bseg_det IS INITIAL.
* no tax line items in bseg
CHECK bseg_output-mwart = space.
ENDIF.
* write bseg
IF bseg_output-status = 'BSEGNEW'.
FORMAT COLOR COL_POSITIVE.
ELSE.
FORMAT COLOR COL_NEGATIVE.
ENDIF.
WRITE: /(7) bseg_output-status.
FORMAT COLOR COL_NORMAL.
IF bseg_output-status = 'BSEGNEW'.
FORMAT COLOR COL_POSITIVE.
ELSE.
FORMAT COLOR COL_NEGATIVE.
ENDIF.
WRITE: (3) bseg_output-txgrp.
WRITE: (6) bseg_output-taxps. "2014-03-20
FORMAT COLOR COL_NORMAL.
ENDLOOP.
ENDLOOP.
ENDFORM. "write_output_documents
*&---------------------------------------------------------------------*
*& Form write_output_bset
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_DOC_KEY text
*----------------------------------------------------------------------*
FORM write_output_bset USING p_bukrs
p_belnr
p_gjahr.
LOOP AT bset_output
WHERE bukrs = p_bukrs
AND belnr = p_belnr
AND gjahr = p_gjahr.
IF bset_det IS INITIAL.
* only first occurence of each txgrp
CHECK bset_output-txgrp NE old_txgrp.
old_txgrp = bset_output-txgrp.
ENDIF.
* write bset
IF bset_det = 'X'.
WRITE: (15) bset_output-fwste,
(15) bset_output-fwbas,
(3) bset_output-ktosl,
(4) bset_output-kschl.
ENDIF.
ENDLOOP.
CLEAR xcorrupt.
IF e_linewise IS INITIAL
AND linewise IS INITIAL. "2014-03-18
*----------------------------------------------------------------------
* here no linewise tax calculation
*----------------------------------------------------------------------
PERFORM set_txgrp_not_linewise.
* do not use function 'FI_TAX_SET_TXGRP' !!!
* CALL FUNCTION 'FI_TAX_SET_TXGRP'
* EXPORTING
* i_bukrs = xbkpf-bukrs
* i_xtxit = space
* TABLES
* t_bkpf = xbkpf
* t_bseg = ybseg
* t_bset = xbset.
*
LOOP AT xbseg.
sbseg_old-buzei = xbseg-buzei.
sbseg_old-txgrp = xbseg-txgrp.
sbseg_old-taxps = xbseg-taxps. "change 2012-03-23
sbseg_old-mwskz = xbseg-mwskz. "change 2012-03-23
sbseg_old-txjcd = xbseg-txjcd. "change 2012-03-23
APPEND sbseg_old.
ENDLOOP.
LOOP AT ybseg.
sbseg_new-buzei = ybseg-buzei.
sbseg_new-txgrp = ybseg-txgrp.
sbseg_new-taxps = ybseg-taxps. "change 2012-03-23
sbseg_new-mwskz = ybseg-mwskz. "change 2012-03-23
sbseg_new-txjcd = ybseg-txjcd. "change 2012-03-23
APPEND sbseg_new.
ENDLOOP.
IF sbseg_old[] NE sbseg_new[].
xcorrupt = 'X'.
ENDIF.
ELSE.
*----------------------------------------------------------------------
* here linewise tax calculation
*----------------------------------------------------------------------
ELSE. "2012-03-30
PERFORM fi_tax_set_txgrp TABLES ybkpf "2012-03-30
ybseg. "2012-03-30
ENDIF. "2012-03-30
*--------------------------------------------------------
* clear txgrp
* in customer/vendor line items
* in tax line items
* do no use form set_txgrp_linewise. Dec. 2, 2005
* PERFORM set_txgrp_linewise.
LOOP AT xbseg.
sbseg_old-buzei = xbseg-buzei.
sbseg_old-txgrp = xbseg-txgrp.
sbseg_old-taxps = xbseg-taxps. "change 2012-03-23
sbseg_old-mwskz = xbseg-mwskz. "change 2012-03-23
sbseg_old-txjcd = xbseg-txjcd. "change 2012-03-23
APPEND sbseg_old.
ENDLOOP.
LOOP AT ybseg.
sbseg_new-buzei = ybseg-buzei.
sbseg_new-txgrp = ybseg-txgrp.
sbseg_new-taxps = ybseg-taxps. "change 2012-03-23
sbseg_new-mwskz = ybseg-mwskz. "change 2012-03-23
sbseg_new-txjcd = ybseg-txjcd. "change 2012-03-23
APPEND sbseg_new.
ENDLOOP.
IF sbseg_old[] NE sbseg_new[].
xcorrupt = 'X'.
ENDIF.
ENDIF.
REFRESH taxgrp.
CLEAR taxgrp.
IF l_manual_tax_lines_in_bseg GT 0
AND l_manual_tax_lines_in_bset GT 0.
IF l_manual_tax_lines_in_bseg GT l_manual_tax_lines_in_bset.
l_bset_compressed = 'X'.
ELSEIF l_manual_tax_lines_in_bseg LT l_manual_tax_lines_in_bset.
l_bseg_compressed = 'X'.
ENDIF.
ENDIF.
LOOP AT ybset.
CLEAR taxgrp.
taxgrp-bukrs = ybset-bukrs.
taxgrp-mwskz = ybset-mwskz.
taxgrp-txjcd = ybset-txjdp. "!txjdp!
taxgrp-txgrp = ybset-txgrp.
taxgrp-shkzg = ybset-shkzg.
taxgrp-ktosl = ybset-ktosl.
taxgrp-hkont = ybset-hkont.
COLLECT taxgrp.
ENDLOOP.
DATA: e_exit.
*-----------------------------------------------------------------------
READ TABLE xbkpf INDEX 1.
CLEAR old_txgrp.
REFRESH tb.
CLEAR: old_ebeln,
old_ebelp,
old_zekkn.
ENDLOOP. "/ybseg
* if not found:
* look for corresponding entry in bseg
* without txgrp
IF sy-subrc NE 0.
IF e_exit = space.
ELSE.
EXIT.
ENDIF.
ENDIF.
ENDLOOP. "/ybseg
ENDIF.
ENDLOOP. "/xbset
*-----------------------------------------------------------------------
* txgrp in ybseg exists in xbset ?
* this is a sane question although it looks strange
* their tax is then included in bset-txgrp = 001
* (or the lowest txgrp)
CLEAR p_exit.
* LIV: txgrp is set per PO item (only when there are lines with
* different KTOSL)
* only MIRO, MR8M etc from Fugr MR1M
IF xbkpf-glvor = 'RMRP'
AND xbkpf-awtyp = 'RMRP'.
* same tax group for all lines that belong to a PO item;
* different tax group for
* lines of a PO item when KTOSL is WRX
* or G/L line items without PO relation
* or freight line items (bseg-buzd = F)
EXIT.
ELSE. "August 9, 2005
* first call with ebeln, ebelp
IF old_ebeln IS INITIAL
AND old_ebelp IS INITIAL
AND NOT ybseg-ebeln IS INITIAL
AND NOT ybseg-ebelp IS INITIAL.
old_ebeln = ybseg-ebeln.
old_ebelp = ybseg-ebelp.
old_zekkn = ybseg-zekkn.
IF ybseg-txgrp NE xbset-txgrp.
ybseg-txgrp = xbset-txgrp.
MODIFY ybseg.
xcorrupt = 'X'.
ENDIF.
* save line item number
tb-txgrp = xbset-txgrp.
tb-buzei = ybseg-buzei.
APPEND tb.
EXIT.
ENDIF.
* second call
IF NOT old_ebeln IS INITIAL
AND NOT old_ebelp IS INITIAL.
* AND NOT ybseg-ebeln IS INITIAL "August 10, 2005
* AND NOT ybseg-ebelp IS INITIAL. "August 10, 2005
IF old_ebeln = ybseg-ebeln
AND old_ebelp = ybseg-ebelp
AND old_zekkn = ybseg-zekkn.
* with same ebeln, ebelp
* and zekkn initial
IF ybseg-txgrp NE xbset-txgrp.
ybseg-txgrp = xbset-txgrp.
MODIFY ybseg.
xcorrupt = 'X'.
ENDIF.
* save line item number
tb-txgrp = xbset-txgrp.
tb-buzei = ybseg-buzei.
APPEND tb.
EXIT.
ELSE.
* with different ebeln, ebelp or
* or same ebeln, ebelp, but different zekkn
* then no change and no further bseg item with this txgrp
p_exit = 'X'.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. " set_txgrp_bseg
*&---------------------------------------------------------------------*
*& Form DELETE_MWSKZ_BSEG_RMRP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM delete_mwskz_bseg_rmrp .
* change in context of note 1692637
* there are line items in bseg that total up to 0
* and have therefore no BSET and should have therefore also no MWSKZ
* note 1692637 deletes mwskz in MRMP form accit_aufbauen
ENDLOOP.
ENDLOOP.
* LIV: txgrp is set per PO item (only when there are lines with
* different KTOSL)
* only MIRO, MR8M etc from Fugr MR1M N587975
IF t_bkpf-glvor = 'RMRP'
AND t_bkpf-awtyp = 'RMRP'. "N587975
h_count = h_count - 1. "N1692637
IF ( old_ebeln IS INITIAL AND old_ebelp IS INITIAL ) OR
* same tax group for all lines that belong to a PO item
* Begin of note 887636
* By change of PO with multi-accounting new taxgroup needed.
* ( old_ebeln NE t_bseg-ebeln OR old_ebelp NE t_bseg-ebelp ) OR
( old_ebeln NE <bseg>-ebeln OR
old_ebelp NE <bseg>-ebelp OR
old_xref3 NE <bseg>-xref3 OR
old_zekkn NE <bseg>-zekkn ) OR
* end of note 887636
* different tax group for lines of a PO item when KTOSL is WRX
<bseg>-ktosl EQ 'WRX' OR
* multi-accounting "N587975 "N887636
* t_bseg-zekkn GT '01' OR "N587975 "N887636
* freight line items "N881794
<bseg>-buzid = 'F' OR "N881794
* begin of note 908364
old_ebeln = <bseg>-ebeln AND NOT old_ebeln IS INITIAL AND
old_ebelp = <bseg>-ebelp AND NOT old_ebelp IS INITIAL AND
old_zekkn = <bseg>-zekkn AND NOT old_zekkn IS INITIAL AND
old_zekkn = '99' AND "2014-03-21
old_xref3 = <bseg>-xref3 AND
old_buzid = <bseg>-buzid OR
* end of note 908364
* G/L line items without invoice relation
( <bseg>-ebeln IS INITIAL AND <bseg>-ebelp IS INITIAL ).
h_count = h_count + 1.
old_ebeln = <bseg>-ebeln.
old_ebelp = <bseg>-ebelp.
old_zekkn = <bseg>-zekkn. "N887636
old_xref3 = <bseg>-xref3. "N887636
old_buzid = <bseg>-buzid. "N908364
ENDIF.
* ENDIF. "N587975
<bseg>-txgrp = h_count.
ELSE. "N587975
* t_bseg-txgrp = sy-tabix. "N587975 N1692637
<bseg>-txgrp = h_count. "N1692637
ENDIF. "N587975
* begin of note 1464426
* bseg-taxps was initial and will be set from txgrp
ASSIGN COMPONENT 'TAXPS' OF STRUCTURE <bseg> TO <bseg_taxps>.
IF sy-subrc IS INITIAL.
<bseg_taxps> = <bseg>-txgrp.
ENDIF.
* end of note 1464426
ENDLOOP. "at t_bseg