Vous êtes sur la page 1sur 76

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

*& Include
ZFIPE_021_CD_V2
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include
ZFIPE_LETADM_V1_CD
*& DESCRIPCION : Canje de Documentos
*
*&---------------------------------------------------------------------*
*---------------------------------------------------------------------*
*
Form canje_documentos
*---------------------------------------------------------------------*
FORM canje_documentos .
PERFORM rescatar_datos1.
"BEGIN OF I-JCM221015{
PERFORM generar_lotes_automatico TABLES t_detalle.
PERFORM generar_lotes_automatico_2 TABLES t_detalle. "CSM-16.12.2015 Automatic
o
CHECK w_cherror IS INITIAL.
IF r_opc9 IS INITIAL.
"}END OF I-JCM221015
PERFORM alv_columnas.
PERFORM alv_fill_layout.
PERFORM alv_lineas_de_titulo USING g_list_top_of_page[].
PERFORM alv_carga_evento
USING t_events[].
PERFORM alv_desplegar.
ENDIF.
ENDFORM.
" canje_documentos
*---------------------------------------------------------------------*
*
Form rescatar_datos1
*---------------------------------------------------------------------*
FORM rescatar_datos1 .
*BEGIN OF I-JCM221015{
TYPES: BEGIN OF lty_bseg,
bukrs TYPE bseg-bukrs,
belnr TYPE bseg-belnr,
gjahr TYPE bseg-gjahr,
koart TYPE bseg-koart,
END OF lty_bseg.
" encryption/decryption data fields
DATA: s_faede
LIKE faede.
DATA: lt_bseg
TYPE STANDARD TABLE OF lty_bseg,
lt_detalle_aux LIKE STANDARD TABLE OF t_detalle,
lt_detalle_xx LIKE STANDARD TABLE OF t_detalle.
DATA: lr_vbeln
TYPE RANGE OF vbrp-vbeln,
lr_vbeln_det TYPE RANGE OF bsid-vbeln,
lr_koart_det TYPE RANGE OF bseg-koart.
DATA: ls_vbeln
LIKE LINE OF lr_vbeln,
ls_vbeln_det LIKE LINE OF lr_vbeln_det,
ls_koart_det LIKE LINE OF lr_koart_det.
FIELD-SYMBOLS: <fs_bseg>
<fs_det>
<fs_vbrp_aut>
<fs_detxx>

LIKE
LIKE
LIKE
LIKE

LINE
LINE
LINE
LINE

OF
OF
OF
OF

lt_bseg,
lt_detalle_aux,
gt_vbrp_aut,
lt_detalle_xx.

*}END OF I-JCM221015
CLEAR : t_detalle.
REFRESH: t_detalle.
IF p_umskz <> space.
SELECT a~kunnr a~blart a~belnr a~gjahr a~xblnr a~bldat a~budat
a~zlsch a~umskz a~zterm a~zfbdt a~zbd1t a~wrbtr a~dmbtr
a~buzei a~shkzg a~xref1 a~xref2 a~zuonr a~sgtxt a~rebzg
b~bktxt b~waers b~hwaer b~kursf a~rebzj a~rebzz
c~name1 c~name2 c~name3 c~name4 c~regio a~prctr a~kkber
b~stblg a~zbd2t a~zbd3t a~rebzt a~vbeln "I-JCM221015
INTO CORRESPONDING FIELDS OF TABLE t_detalle
FROM bsid AS a
INNER JOIN bkpf AS b
ON b~bukrs = a~bukrs AND
b~gjahr = a~gjahr AND
b~belnr = a~belnr
INNER JOIN kna1 AS c
ON c~kunnr = a~kunnr
WHERE a~bukrs
= p_bukrs
AND a~kunnr
IN s_kunnrn "U-JCM231015(s_kunnr X s_kunnrn)
AND a~budat
IN s_bldat
AND a~blart
IN s_blart
AND a~bldat
IN s_bldat
AND a~belnr
IN s_belnr2
AND a~xblnr
IN s_xblnr
AND a~zterm
IN s_zterm
AND a~zlsch
IN s_zlsch
AND a~umskz
= p_umskz
AND b~xreversal = ' '.
ELSE.
SELECT a~kunnr a~blart a~belnr a~gjahr a~xblnr a~bldat a~budat
a~zlsch a~umskz a~zterm a~zfbdt a~zbd1t a~wrbtr a~dmbtr
a~buzei a~shkzg a~xref1 a~xref2 a~zuonr a~sgtxt a~rebzg
b~bktxt b~waers b~hwaer b~kursf a~rebzj a~rebzz
c~name1 c~name2 c~name3 c~name4 c~regio a~prctr a~kkber
b~stblg a~zbd2t a~zbd3t a~rebzt a~vbeln "I-JCM221015
INTO CORRESPONDING FIELDS OF TABLE t_detalle
FROM bsid AS a
INNER JOIN bkpf AS b
ON b~bukrs = a~bukrs AND
b~gjahr = a~gjahr AND
b~belnr = a~belnr
INNER JOIN kna1 AS c
ON c~kunnr = a~kunnr
WHERE a~bukrs
= p_bukrs
AND a~kunnr
IN s_kunnrn "U-JCM231015(s_kunnr X s_kunnrn)
AND a~budat
IN s_bldat
AND a~blart
IN s_blart
AND a~bldat
IN s_bldat
AND a~belnr
IN s_belnr2
AND a~xblnr
IN s_xblnr
AND a~zterm
IN s_zterm
AND a~zlsch
IN s_zlsch
AND b~xreversal = ' '.
ENDIF.

DELETE t_detalle WHERE zlsch EQ 'F'. "Documentos enviados a FEDD


"BEGIN OF I-JCM221015{
DELETE t_detalle WHERE blart NOT IN gr_blart.
DELETE t_detalle WHERE zterm NOT IN gr_zterm.
IF t_detalle[] IS NOT
CLEAR ls_koart_det.
ls_koart_det-sign =
ls_koart_det-low =
ls_koart_det-low =
SELECT
INTO
FROM
FOR
WHERE
AND
AND
AND

INITIAL.
'I'. ls_koart_det-option = 'EQ'.
'K'. APPEND ls_koart_det TO lr_koart_det.
'D'. APPEND ls_koart_det TO lr_koart_det.

bukrs belnr gjahr koart


TABLE lt_bseg
bseg
ALL ENTRIES IN t_detalle
bukrs EQ p_bukrs
belnr EQ t_detalle-belnr
gjahr EQ t_detalle-gjahr
koart IN lr_koart_det.
"09032016

TYPES: BEGIN OF GTY_VBFA,


VBELV
TYPE VBELN_VON,
POSNV TYPE POSNR_VON,
VBELN
TYPE VBELN_NACH,
POSNN
TYPE POSNR_NACH,
VBTYP_N
TYPE VBTYP_N,
END OF GTY_VBFA.
DATA GTD_VBFA TYPE STANDARD TABLE OF GTY_VBFA.
FIELD-SYMBOLS: <FS_VBFA> TYPE GTY_VBFA.
CLEAR GTD_VBFA.
REFRESH GTD_VBFA.
SELECT VBELV POSNV VBELN POSNN VBTYP_N
INTO TABLE GTD_VBFA
FROM VBFA
FOR ALL ENTRIES IN t_detalle
WHERE VBELN EQ t_detalle-VBELN
AND VBTYP_V EQ 'C'.
"09032016
SORT lt_bseg BY bukrs belnr gjahr.
DELETE ADJACENT DUPLICATES FROM lt_bseg COMPARING bukrs belnr gjahr.
ENDIF.
"Considerar solo los documentos vencidos
LOOP AT t_detalle.
CLEAR s_faede.
w_index = sy-tabix.
READ TABLE lt_bseg ASSIGNING <fs_bseg> WITH KEY bukrs = p_bukrs
belnr = t_detalle-belnr
gjahr = t_detalle-gjahr
BINARY SEARCH.
IF sy-subrc EQ 0.
s_faede-koart = <fs_bseg>-koart.

ENDIF.
s_faede-shkzg
s_faede-zfbdt
s_faede-zbd1t
s_faede-zbd2t
s_faede-zbd3t
s_faede-rebzg
s_faede-rebzt
s_faede-bldat

=
=
=
=
=
=
=
=

t_detalle-shkzg.
t_detalle-zfbdt.
t_detalle-zbd1t.
t_detalle-zbd2t.
t_detalle-zbd3t.
t_detalle-rebzg.
t_detalle-rebzt.
t_detalle-bldat.

CALL FUNCTION 'DETERMINE_DUE_DATE'


EXPORTING
i_faede = s_faede
IMPORTING
e_faede = s_faede
EXCEPTIONS
OTHERS = 1.
IF s_faede-netdt LT sy-datum.
t_detalle-venci = 'X'.
MODIFY t_detalle INDEX w_index.
ENDIF.
"09032016
READ TABLE GTD_VBFA ASSIGNING <FS_VBFA> WITH KEY VBELN = t_detalle-VBELN.
IF SY-SUBRC EQ 0.
t_detalle-VBELV = <FS_VBFA>-VBELV.
MODIFY t_detalle INDEX w_index.
ENDIF.
"09032016
ENDLOOP.
DELETE t_detalle WHERE venci EQ 'X'.
DELETE t_detalle WHERE stblg IS NOT INITIAL.
********************************************************************************
* Eliminar las notas de credito que no se encuentren vinculadas a las facturas
********************************************************************************
IF ( ( p_vkbur EQ gc_3004 ) AND ( p_relac-low IS NOT INITIAL ) ) OR
( ( p_vkbur EQ gc_3003 ) OR ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3001
) ).
REFRESH gt_vbrk.
CLEAR gs_vbrk.
lt_detalle_aux[] = t_detalle[].
lt_detalle_xx[] = t_detalle[].
DELETE lt_detalle_aux WHERE blart NOT IN gr_blart_nd."Queda D7,D8,DD
DELETE lt_detalle_xx WHERE blart IN gr_blart_nd.
"Queda los que sena <>
de D7,D8,DD
SELECT vbeln xblnr INTO TABLE gt_vbrk
FROM vbrk
FOR ALL ENTRIES IN lt_detalle_aux
WHERE vbeln = lt_detalle_aux-vbeln.
IF sy-subrc = 0.
LOOP AT t_detalle ASSIGNING <fs_detxx> .
CHECK <fs_detxx>-blart IN gr_blart_nd.
READ TABLE gt_vbrk INTO gs_vbrk WITH KEY xblnr = <fs_detxx>-xblnr.
CHECK sy-subrc <> 0.
DELETE t_detalle.
ENDLOOP.
ENDIF.
ELSE.

DELETE t_detalle WHERE blart IN gr_blart_nd.


ENDIF.
********************************************************************************
*********
* SORT lt_detalle_aux BY belnr gjahr.
* LOOP AT t_detalle WHERE blart IN gr_blart_nx.
*
w_index = sy-tabix.
*
READ TABLE lt_detalle_aux ASSIGNING <fs_det> WITH KEY bukrs = p_bukrs
*
belnr = t_detalle-reb
zg
*
gjahr = t_detalle-reb
zj
*
BINARY SEARCH.
*
IF sy-subrc NE 0.
*
t_detalle-venci = 'X'.
*
MODIFY t_detalle INDEX w_index.
*
ENDIF.
* ENDLOOP.
*
* DELETE t_detalle WHERE venci EQ 'X'.
"Eliminar los documentos que no pertenezcan a la Organizacin y oficina de Venta
s seleccionada
REFRESH: lr_vbeln, lr_vbeln_det, gt_vbrp_aut.
LOOP AT t_detalle.
CLEAR ls_vbeln.
ls_vbeln-sign = 'I'.
ls_vbeln-option = 'EQ'.
ls_vbeln-low
= t_detalle-vbeln.
APPEND ls_vbeln TO lr_vbeln.
ENDLOOP.
IF lr_vbeln[] IS NOT INITIAL.
SORT lr_vbeln BY low.
DELETE ADJACENT DUPLICATES FROM lr_vbeln COMPARING low.
DELETE lr_vbeln WHERE low IS INITIAL.
SELECT
INTO
FROM
WHERE
AND
AND

vbeln posnr vkorg_auft vkbur aubel aupos


TABLE gt_vbrp_aut
vbrp
vbeln
IN lr_vbeln
vkorg_auft EQ p_vkorg
vkbur
EQ p_vkbur.

SORT gt_vbrp_aut BY vbeln.


DELETE ADJACENT DUPLICATES FROM gt_vbrp_aut COMPARING vbeln.
LOOP AT gt_vbrp_aut ASSIGNING <fs_vbrp_aut>.
CLEAR ls_vbeln_det.
ls_vbeln_det-sign = 'I'.
ls_vbeln_det-option = 'EQ'.
ls_vbeln_det-low
= <fs_vbrp_aut>-vbeln.
APPEND ls_vbeln_det TO lr_vbeln_det.
ENDLOOP.
IF lr_vbeln_det[] IS INITIAL.
REFRESH t_detalle.
ELSE.

DELETE t_detalle WHERE vbeln NOT IN lr_vbeln_det.


ENDIF.
ENDIF.
IF ( p_vkbur EQ '3004' ) AND ( p_relac IS NOT INITIAL ).
DELETE t_detalle WHERE blart IN gr_blart_nx.
ENDIF.
"}END OF I-JCM221015
IF NOT t_detalle[] IS INITIAL.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE t_bsed
FROM bsed
FOR ALL ENTRIES IN t_detalle
WHERE bukrs = p_bukrs
AND belnr = t_detalle-belnr
AND gjahr = t_detalle-gjahr
AND buzei = t_detalle-buzei.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE t_faglflexa
FROM faglflexa
FOR ALL ENTRIES IN t_detalle
WHERE rbukrs = p_bukrs
AND belnr = t_detalle-belnr
AND gjahr = t_detalle-gjahr
AND buzei = t_detalle-buzei.
IF s_prctr IS NOT INITIAL.
DELETE t_faglflexa WHERE prctr NOT IN s_prctr.
ENDIF.
IF s_segme IS NOT INITIAL.
DELETE t_faglflexa WHERE segment NOT IN s_segme.
ENDIF.
ENDIF.
*Actualizo fecha vencimiento y borrar documento que ya estan en lotes
LOOP AT t_detalle.
w_index = sy-tabix.

*
*

READ TABLE t_faglflexa WITH KEY belnr = t_detalle-belnr


buzei = t_detalle-buzei.
ryear = t_detalle-gjahr "INSERT @010
rbukrs = t_detalle-bukrs."INSERT @010
IF sy-subrc NE 0.
t_detalle-belnr = 'BORRAR'.
MODIFY t_detalle INDEX w_index.
CONTINUE.
ELSE.
t_detalle-prctr = t_faglflexa-prctr.
t_detalle-segment = t_faglflexa-segment.
ENDIF.

IF t_detalle-rebzg NE space.
CONCATENATE t_detalle-rebzg t_detalle-rebzj t_detalle-rebzz INTO t_detalle
-refer SEPARATED BY space.
ELSE.

CONCATENATE t_detalle-belnr t_detalle-gjahr t_detalle-buzei INTO t_detalle


-refer SEPARATED BY space.
ENDIF.
READ TABLE t_bsed WITH KEY belnr = t_detalle-belnr gjahr = t_detalle-gjahr b
uzei = t_detalle-buzei.
IF sy-subrc = 0.
t_detalle-wbzog = t_bsed-wbzog.
w_kunnr = t_detalle-wbzog.
PERFORM nombres USING w_kunnr w_name1.
t_detalle-name2 = w_name1.
t_detalle-wort2 = t_bsed-wort2.
w_kunnr = t_detalle-wort2.
PERFORM nombres USING w_kunnr w_name1.
t_detalle-name3 = w_name1.
t_detalle-bankl
t_detalle-bankn
t_detalle-banks
t_detalle-bkont
t_detalle-wstat
t_detalle-wevwv
t_detalle-wgbkz
t_detalle-xaktz
t_detalle-xsiwe
t_detalle-wdate
ENDIF.

=
=
=
=
=
=
=
=
=
=

t_bsed-wlzbp+5(15).
t_bsed-wlzbp+21(18).
t_bsed-wlzbp+1(4).
t_bsed-wlzbp+40(2).
t_bsed-wstat.
t_bsed-wevwv.
t_bsed-wgbkz.
t_bsed-xaktz.
t_bsed-xsiwe.
t_bsed-wdate.

CLEAR w_nlote.
SELECT SINGLE a~nlote INTO w_nlote
FROM zlpet_fi_0027 AS a
INNER JOIN zlpet_fi_0022 AS b
ON a~bukrs = b~bukrs AND
a~nlote = b~nlote
WHERE a~bukrs = p_bukrs
AND a~belnr = t_detalle-belnr
AND a~gjahr = t_detalle-gjahr
AND a~buzei = t_detalle-buzei
AND b~ianul = ''.
IF sy-subrc = 0.
t_detalle-belnr = 'BORRAR'.
MODIFY t_detalle INDEX w_index.
CONTINUE.
ELSE.
CONCATENATE t_detalle-name1 t_detalle-name2 t_detalle-name3 t_detalle-name
4
INTO t_detalle-name1 SEPARATED BY ' '.
t_detalle-zfbdt = t_detalle-zfbdt + t_detalle-zbd1t.
IF t_detalle-zfbdt LT sy-datum.
t_detalle-staven = '@AG@'.
ELSEIF t_detalle-zfbdt EQ sy-datum.
t_detalle-staven = '@1V@'.
ELSE.
t_detalle-staven = '@1U@'.
ENDIF.
CONCATENATE t_detalle-zfbdt+6(2) t_detalle-zfbdt+4(2) t_detalle-zfbdt(4) I
NTO t_detalle-feven SEPARATED BY '.'.
t_detalle-vencim = t_detalle-zfbdt.
IF t_detalle-shkzg = 'H'.

t_detalle-dmbtr = t_detalle-dmbtr * -1.


t_detalle-wrbtr = t_detalle-wrbtr * -1.
ENDIF.
PERFORM descripcion_estados.
MODIFY t_detalle INDEX w_index.
ENDIF.
ENDLOOP.
DELETE t_detalle WHERE belnr EQ 'BORRAR'.
SORT t_detalle ASCENDING BY belnr.
ENDFORM.
" rescatar_datos1
*---------------------------------------------------------------------*
*
Form generar_lotes
*---------------------------------------------------------------------*
FORM generar_lotes
TABLES pt_detalle STRUCTURE t_detalle. "I-JCM221015
"BEGIN OF I-JCM121115{
DATA: lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_times TYPE i,
lw_flag
TYPE c,
lw_zw_div
TYPE c.
"}END OF I-JCM121115
FIELD-SYMBOLS : <fs_det> LIKE LINE OF t_detalle. "I-JCM121115
CLEAR: ws_waers, ws_wrbtr, ws_cant, w_sw, w_kunnr, ws_bankk, w_kunnr, ws_amort
.
READ TABLE pt_detalle WITH KEY opc = 'X'.
IF sy-subrc NE 0.
w_sw = 'X'.
MESSAGE 'Seleccione documento(s)' TYPE 'E'.
ENDIF.
CLEAR : w_prctr, ws_fini, ws_rebzg, ws_rebzj.

"cvc 140913

LOOP AT pt_detalle
ASSIGNING <fs_det> "I-JCM121115
WHERE opc = 'X'.
CLEAR: lw_flag,lw_zw_div . "I-JCM121115

IF w_kunnr IS INITIAL.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
ws_cutil = t_detalle-umskz.
ELSE.
IF w_kunnr <> <fs_det>-kunnr.
MESSAGE 'Documentos seleccionados son de clientes diferentes' TYPE 'E'.
w_sw = 'X'.
EXIT.
ELSE.
IF ws_waers <> <fs_det>-waers.

MESSAGE 'Documentos seleccionados con monedas diferentes' TYPE 'E'.


w_sw = 'X'.
EXIT.
ELSE.
IF ws_bankk <> <fs_det>-bankl.
MESSAGE 'Documentos seleccionados con bancos diferentes' TYPE 'E'.
w_sw = 'X'.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
PERFORM doc_bloqueado USING <fs_det>-belnr <fs_det>-gjahr w_sw.
IF w_sw = 'X'.
CONCATENATE 'Documento' <fs_det>-belnr 'bloqueado por usuario' INTO w_mess
SEPARATED BY space.
MESSAGE w_mess TYPE 'E'.
EXIT.
ENDIF.
"ws_wrbtr = ws_wrbtr + pt_detalle-wrbtr. "D-JCM121115
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr. "I-JCM121115
*
ws_cant = ws_cant + 1.
*

cvc 140913
IF w_prctr IS INITIAL AND <fs_det>-prctr IS NOT INITIAL.
w_prctr = <fs_det>-prctr.
ENDIF.

IF <fs_det>-bldat > ws_fini.


ws_fini = <fs_det>-bldat.
ws_rebzg = <fs_det>-belnr.
ws_rebzj = <fs_det>-gjahr.
ws_rebzz = <fs_det>-buzei.
ENDIF.
* End cvc
"BEGIN OF I-JCM121115{
IF p_vkbur EQ '3004'.
"Calcular el tipo de cambio para el calculo de los maximos y minimos impor
tes comprendidos
IF ws_waers EQ 'PEN'.
CLEAR: lw_fecval, lw_ukurs, lw_minimg, lw_maximg.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= ws_fini
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc
lw_minimg
lw_maximg
ENDIF.
ELSE.
lw_minimg =

EQ 0.
= p_minim * lw_ukurs.
= p_maxim * lw_ukurs.
p_minim.

lw_maximg = p_maxim.
ENDIF.
"Calcular la cantidad de letras por factura
IF <fs_det>-wrbtr BETWEEN lw_minimg AND lw_maximg.
<fs_det>-ws_cant = 1.
ELSE.
lw_times = 2.
PERFORM f_calculo_cant_letxfact
USING lw_minimg lw_maximg
lw_times lw_flag lw_zw_div
CHANGING <fs_det>.
ENDIF.
ELSE.
IF <fs_det>-wrbtr LT 100.
w_sw = 'X'.
MESSAGE text-x04 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ELSE.
<fs_det>-ws_cant = 1.
ENDIF.
ENDIF.
ws_cant = ws_cant + <fs_det>-ws_cant.
ws_dias = <fs_det>-zbd1t.
"}END OF I-JCM121115
ENDLOOP.
CALL FUNCTION 'DEQUEUE_ALL'.
IF NOT ws_wrbtr > 0.
w_sw = 'X'.
MESSAGE 'La suma de importes de documentos seleccionados es negativo' TYPE '
E'.
ENDIF.
IF w_sw IS INITIAL.
CLEAR: t_letras, ws_acepta, ws_name1, ws_aval1, ws_name2, ws_aval2, ws_nam
e3, ws_desreg.
REFRESH: t_letras.
w_swg
= ' '.
w_gjahr = sy-datum(4).
ws_xint = ' '.
ws_xrec = 'X'.
ws_xamo = ' '.
*
ws_fini = sy-datum.
ws_fint = ws_fini. "sy-datum.
*

ws_dias = 30. "D-JCM121115


ws_lu = 'X'.
ws_ma = 'X'.
ws_mi = 'X'.
ws_ju = 'X'.
ws_vi = 'X'.
ws_sa = 'X'.
ws_do = 'X'.
ws_fe = 'X'.
ws_inter = 0.
ws_recau = 0.
ws_total = 0.
ws_sumimp = 0.
ws_acepta = w_kunnr.

ws_cant = 1. "D-JCM121115
ws_total_cj = ws_wrbtr.
CONDENSE ws_cant.
SELECT SINGLE *
FROM zlpet_fi_0023
WHERE bukrs = p_bukrs.
IF sy-subrc = 0.
ws_blart = zlpet_fi_0023-blart.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
IF ws_cutil EQ space.
ws_cutil = zlpet_fi_0023-wevwv.
ENDIF.
ws_tasa = zlpet_fi_0023-tasaint.
ws_dtasa = zlpet_fi_0023-diasint."MTC141013 ZFIPET_021_10
ENDIF.
SELECT SINGLE *
FROM zlpet_fi_0030
WHERE kunnr = ws_acepta.
IF sy-subrc = 0.
ws_aval1 = zlpet_fi_0030-aval1.
ws_aval2 = zlpet_fi_0030-aval2.
ws_tasa = zlpet_fi_0030-tasaint.
ws_dtasa = zlpet_fi_0030-diasint.
ENDIF.
w_vez = 1.
w_ucomm = space.
LOOP AT pt_detalle WHERE opc = 'X'.
ws_regio = pt_detalle-regio.
ws_kkber = pt_detalle-kkber.
EXIT.
ENDLOOP.

"BEGIN OF U-JCM231015{
"CALL SCREEN '9000' STARTING AT 30 1.
IF r_opc9 NE 'X'.
CALL SCREEN '9000' STARTING AT 30 1.
ELSE.
PERFORM f_proponer_valores_letra.
PERFORM f_grabar_lotes_letra.
ENDIF.
"}END OF U-JCM231015
IF w_swg = 'X'.
PERFORM grabar_documento_origen.
PERFORM rescatar_datos1.
ENDIF.
ENDIF.
ENDFORM.
" generar_lotes
*---------------------------------------------------------------------*
*
Module STATUS_9000 OUTPUT
*---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'ST01'.
ENDMODULE.
" STATUS_9000 OUTPUT
*---------------------------------------------------------------------*

*
Module USER_COMMAND_9000 INPUT
*---------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
*BEGIN OF D-JCM231015{
* DATA: w_postm
TYPE t059p-wt_postm,
*
l_with_item LIKE with_item,
*
l_t059minmax LIKE t059minmax,
*
l_t059z
LIKE t059z,
*
w_multi(4) TYPE c.
*}END OF D-JCM231015

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

CASE sy-ucomm.
WHEN 'EXIT' OR 'CANCEL' OR 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'S_BTPRO'.
"BEGIN OF U-JCM231015{
PERFORM f_proponer_valores_letra.
IF ws_amort GE ws_wrbtr.
MESSAGE 'Importe de amortizacin debe ser menor a Importe Doc.' TYPE 'E'.
EXIT.
ENDIF.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
w_ucomm = space.
w_seg = space.
IF ws_xint EQ 'X' AND ( ws_tasa = 0 ).
MESSAGE 'Debe ingresar tasa' TYPE 'W'.
w_seg = 'X'.
ENDIF.
SELECT SINGLE bezei
INTO ws_desreg
FROM t005u
WHERE bland = ws_regio
AND spras = 'S'
AND land1 = 'PE'.
CHECK w_seg = space.
CLEAR: w_inicial, w_final, w_equita, ws_inter, ws_recau.
IF ws_xrec EQ 'X'.
PERFORM calcular_montos.
ENDIF.
ws_recau = w_inicial + w_final + w_equita.
PERFORM rescatar_nombres.
CLEAR : t_letras, ws_sumimp.
REFRESH: t_letras.
ws_total = ws_wrbtr.
IF ws_xamo = 'X'.
ws_total = ws_total - ws_amort.
ELSE.
ws_amort = '0.00'.
ENDIF.
IF ws_xrec = 'X'.
ws_total = ws_total + ws_recau.
ELSE.
ws_recau = 0.
ENDIF.
w_feccal = ws_fini.
w_canequi = w_equita / ws_cant.
w_cancal = ( ws_wrbtr - ws_amort ) / ws_cant.

*
w_cancal = w_cancal + w_canequi.
*
*
DO ws_cant TIMES.
*
w_cancal2 = w_cancal.
*
IF sy-index = 1.
*
w_cancal2 = w_cancal2 + w_inicial.
*
ENDIF.
*
IF sy-index = ws_cant.
*
IF ws_xrec = 'X'.
*
w_cancal2 = w_cancal2 + w_final.
*
ENDIF.
*
ENDIF.
*
w_multi = ws_dias * sy-index.
*
w_feccal = ws_fini + w_multi.
*
PERFORM verif_fecha_dia_habil.
*
t_letras-difdi = w_difdi.
*
t_letras-budat = w_feccal.
*
t_letras-wrbtr = w_cancal2.
*
w_inter = 0.
*
IF ws_xint = 'X'.
*
PERFORM calcular_interes.
*
ENDIF.
*
t_letras-inter = w_inter.
*
t_letras-total = t_letras-wrbtr + t_letras-inter.
*
ws_sumimp = ws_sumimp + t_letras-total .
*
ws_inter = ws_inter + w_inter.
*
*
IF sy-index = ws_cant.
*
w_dif = ( ws_total + ws_inter ) - ws_sumimp.
*
t_letras-total = t_letras-total + w_dif.
*
ws_sumimp = ws_sumimp + w_dif.
*
ENDIF.
*
*
APPEND t_letras.
*
CLEAR: t_letras.
*
ENDDO.
*
IF ws_xint = 'X'.
*
ws_total = ws_total + ws_inter.
*
ELSE.
*
ws_inter = 0.
*
ENDIF.
*
*
ws_total_cj = ws_total.
** Canje en otra moneda diferente.
*
IF ws_waers NE ws_waer2.
*
CALL FUNCTION 'READ_EXCHANGE_RATE'
*
EXPORTING
*
date
= ws_fini
*
foreign_currency = 'USD'
*
local_currency = 'PEN'
*
type_of_rate
= 'M'
*
IMPORTING
*
exchange_rate
= w_ukurs
*
valid_from_date = w_fecval
*
EXCEPTIONS
*
OTHERS
= 1.
*
*
w_kursf2 = w_ukurs.
*
*
IF ws_waers EQ 'PEN'.

*
ws_total_cj = ws_total * w_ukurs.
*
ELSE.
*
ws_total_cj = ws_total / w_ukurs.
*
ENDIF.
*
*
CLEAR ws_sumimp.
*
LOOP AT t_letras.
*
IF ws_waers EQ 'PEN'.
*
t_letras-wrbtr = t_letras-wrbtr * w_ukurs.
*
t_letras-inter = t_letras-inter * w_ukurs.
*
t_letras-total = t_letras-wrbtr + t_letras-inter.
*
ELSE.
*
t_letras-wrbtr = t_letras-wrbtr / w_ukurs.
*
t_letras-inter = t_letras-inter / w_ukurs.
*
t_letras-total = t_letras-wrbtr + t_letras-inter.
*
ENDIF.
*
MODIFY t_letras.
*
ADD t_letras-total TO ws_sumimp.
*
ENDLOOP.
*
ENDIF.
*
*
CLEAR : ws_sumimp, ws_inter.
*
LOOP AT t_letras.
*
PERFORM actualizar_fec.
*
w_inter = 0.
*
IF ws_xint = 'X'.
*
PERFORM calcular_interes.
*
ENDIF.
*
t_letras-inter = w_inter.
*
t_letras-total = t_letras-wrbtr + t_letras-inter.
*
ws_sumimp = ws_sumimp + t_letras-total.
*
ws_inter = ws_inter + w_inter.
*
*
MODIFY t_letras.
*
ENDLOOP.
*
*
LOOP AT t_letras.
*
IF sy-tabix = ws_cant.
*
w_dif = ws_total_cj - ws_sumimp.
*
t_letras-total = t_letras-total + w_dif.
*
ws_sumimp = ws_sumimp + w_dif.
*
MODIFY t_letras.
*
ENDIF.
*
ENDLOOP.
*}END OF U-JCM231015
WHEN 'S_BTREC'.
IF ws_sumimp IS NOT INITIAL.
w_ucomm = 'X'.
CLEAR: ws_sumimp, ws_inter.
PERFORM rescatar_nombres.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
LOOP AT t_letras.
PERFORM actualizar_fec
USING t_letras-vencim. "I-JCM131115
w_inter = 0.
IF ws_xint = 'X'.
PERFORM calcular_interes.
ENDIF.
t_letras-inter = w_inter.

t_letras-total = t_letras-wrbtr + t_letras-inter.


ws_sumimp = ws_sumimp + t_letras-total.
ws_inter = ws_inter + w_inter.
MODIFY t_letras.
ENDLOOP.
LOOP AT t_letras.
IF sy-tabix = ws_cant.
w_dif = ws_total_cj - ws_sumimp.
t_letras-total = t_letras-total + w_dif.
ws_sumimp = ws_sumimp + w_dif.
MODIFY t_letras.
ENDIF.
ENDLOOP.
ELSE.
MESSAGE 'No hay propuesta de letras' TYPE 'I'.
ENDIF.
WHEN 'S_BTVER'.
w_ucomm = space.
IF w_vez = 1.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
PERFORM rescatar_nombres.
CLEAR: ws_sumimp.
LOOP AT t_letras.
ws_sumimp = ws_sumimp + t_letras-total.
ENDLOOP.
ws_dif = abs( ws_sumimp - ws_total_cj ).
IF ( ws_dif NE 0 AND ws_waers EQ ws_waer2 ) OR ( ws_dif > 1 AND ws_waers
NE ws_waer2 ).
MESSAGE 'Importe Total no cuadra con Suma Importes' TYPE 'I'.
ENDIF.
ELSE.
MESSAGE 'Recalculo incompleto' TYPE 'I'.
ENDIF.

*
*
*
s
*
*
*
*
*
*
*
*
*
*
*
*
*
*

WHEN 'S_BTGRA'.
w_ucomm = space.
"BEGIN OF U-JCM231015{
PERFORM f_grabar_lotes_letra .
IF ws_sumimp IS NOT INITIAL.
ws_dif = abs( ws_sumimp - ws_total_cj ).
IF ( ws_dif NE 0 AND ws_waers EQ ws_waer2 ) OR ( ws_dif > 1 AND ws_waer
NE ws_waer2 ).
MESSAGE 'Importe Total no cuadra con Suma Importes' TYPE 'I'.
ELSE.
IF w_vez = 1.
PERFORM grabar_lote.
PERFORM solicitud_letra.
IF ws_xint = 'X' AND ws_inter > 0.
PERFORM nota_debito_funcion.
ENDIF.
PERFORM cambiar_letras.
PERFORM cambiar_documento.
PERFORM desplegar_log_canje.
LEAVE TO SCREEN 0.
ELSE.
MESSAGE 'Recalculo incompleto' TYPE 'I'.

*
*
*
*
*

ENDIF.
ENDIF.
ELSE.
MESSAGE 'No hay propuesta de letras' TYPE 'I'.
ENDIF.
"}END OF U-JCM231015
ENDCASE.
CLEAR sy-ucomm.

ENDMODULE.

" USER_COMMAND_9000 INPUT

*---------------------------------------------------------------------*
*
Form grabar_lote
*---------------------------------------------------------------------*
FORM grabar_lote .
DATA: r_nlote LIKE LINE OF p_nlote.
"09032016
DATA V_LINEAS TYPE I.
DESCRIBE TABLE t_letras LINES V_LINEAS.
CLEAR GTD_LOTE.
REFRESH GTD_LOTE.
DO V_LINEAS TIMES.
"09032016
*Rescatar ultimo numero de lote correlativo
CLEAR w_nlote.
SELECT SINGLE nlote
INTO w_nlote
FROM zlpet_fi_0024 " Antes el nombre de la tabla era ZFIPET_LET11
WHERE bukrs = w_bukrs
AND gjahr = w_gjahr.
w_nlote = w_nlote + 1.
CONDENSE w_nlote.
*Actualizar Numero de Lote correlativo
UPDATE zlpet_fi_0024 " Antes el nombre de la tabla era ZFIPET_LET11
SET nlote = w_nlote
WHERE bukrs = w_bukrs
AND gjahr = w_gjahr.
*Genera numero de Lote
CONCATENATE 'G' w_gjahr+2(2) w_nlote INTO w_nlotec.
"09032016
APPEND INITIAL LINE TO GTD_LOTE ASSIGNING <FS_LOTE>.
<FS_LOTE>-w_nlotec = w_nlotec.
"09032016
*Grabar cabecera Lote
zlpet_fi_0022-bukrs
zlpet_fi_0022-nlote
zlpet_fi_0022-fcrea
zlpet_fi_0022-usnam
zlpet_fi_0022-ianul
zlpet_fi_0022-wrbtrd
zlpet_fi_0022-wrbtri

=
=
=
=
=
=
=

w_bukrs."MTC141013 ZFIPET_021_1
w_nlotec.
sy-datum.
sy-uname.
''.
ws_wrbtr.
ws_inter.

zlpet_fi_0022-wrbtrr = ws_recau.
zlpet_fi_0022-wrbtrt = ws_total.
zlpet_fi_0022-wevwv = ws_cutil.
zlpet_fi_0022-irecau = ws_xrec.
zlpet_fi_0022-iint
= ws_xint.
zlpet_fi_0022-finicio = ws_fini.
zlpet_fi_0022-fdevol = ws_fdev.
zlpet_fi_0022-cefect = ws_cant.
zlpet_fi_0022-defect = ws_dias.
zlpet_fi_0022-tasaint = ws_tasa.
zlpet_fi_0022-diasint = ws_dtasa.
zlpet_fi_0022-kunnr = ws_acepta.
zlpet_fi_0022-aval1 = ws_aval1.
zlpet_fi_0022-aval2 = ws_aval2.
zlpet_fi_0022-waers = ws_waers.
zlpet_fi_0022-iamort = ws_xamo.
zlpet_fi_0022-wrbtra = ws_amort.
INSERT zlpet_fi_0022."MTC141013 ZFIPET_021_1
* Inserta o actualiza datos del cliente
SELECT SINGLE *
FROM zlpet_fi_0030
WHERE kunnr = ws_acepta.
IF sy-subrc = 0.
UPDATE zlpet_fi_0030
SET
aval1 = ws_aval1
aval2 = ws_aval2
tasaint = ws_tasa
diasint = ws_dtasa
WHERE kunnr = ws_acepta.
ELSE.
zlpet_fi_0030-kunnr = ws_acepta.
zlpet_fi_0030-aval1 = ws_aval1.
zlpet_fi_0030-aval2 = ws_aval2.
zlpet_fi_0030-tasaint = ws_tasa.
zlpet_fi_0030-diasint = ws_dtasa.
INSERT zlpet_fi_0030.
ENDIF.
CHECK r_opc9 IS NOT INITIAL. "Automatico
REFRESH p_nlote.
r_nlote-sign = 'I'.
r_nlote-option = 'EQ'.
r_nlote-low
= w_nlotec.
APPEND r_nlote TO p_nlote.
"09032016
ENDDO.
"09032016
ENDFORM.

" grabar_lote

*---------------------------------------------------------------------*
*
Form solicitud_letra
*---------------------------------------------------------------------*
FORM solicitud_letra .
DATA: lw_minimo LIKE bsis-wrbtr. "I-JCM231015
REFRESH: t_dlote.
CLEAR: t_dlote.

* { @002 delete
**Rescatar ultimo numero de letra
* CLEAR: w_nletra.
* SELECT SINGLE nletra
*
INTO w_nletra
*
FROM zlpet_fi_0024 " Antes el nombre de la tabla era ZFIPET_LET11
*
WHERE bukrs = w_bukrs
*
AND gjahr = w_gjahr.
* } @002 delete
CLEAR: w_swe.
LOOP AT t_letras.
"09032016
DATA L_TABIX TYPE SY-TABIX.
CLEAR L_TABIX.
L_TABIX = SY-TABIX.
"09032016
CLEAR: bdcdata, t_msgbi.
REFRESH: bdcdata, t_msgbi.
IF w_swe IS INITIAL.
* { @002 insert
*
*Rescatar ultimo numero de letra
CLEAR: w_nletra.
SELECT SINGLE nletra
INTO w_nletra
FROM zlpet_fi_0024 " Antes el nombre de la tabla era ZFIPET_LET11
WHERE bukrs = w_bukrs
AND gjahr = w_gjahr.
* } @002 insert
w_nletra = w_nletra + 1.
CONDENSE w_nletra.
*Actualizar Numero de Letra correlativo
UPDATE zlpet_fi_0024 " Antes el nombre de la tabla era ZFIPET_LET11
SET nletra = w_nletra
WHERE bukrs = w_bukrs
AND gjahr = w_gjahr.
*Genera numero de letra
CONCATENATE w_gjahr+2(2) w_nlote w_nletra INTO w_nletrac.
*
*

w_wrbtrc = t_letras-total.
REPLACE ALL OCCURRENCES OF '.' IN w_wrbtrc WITH ','.
WRITE t_letras-total TO w_wrbtrc CURRENCY ws_waers.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = w_wrbtrc
IMPORTING
output = w_wrbtrc.

*
CONCATENATE ws_fini+6(2) ws_fini+4(2) ws_fini(4) INTO w_fecha SEPARATED B
Y '.'. "D-JCM131115
CONCATENATE t_letras-vencim+6(2) t_letras-vencim+4(2) t_letras-vencim(4) I
NTO w_fecha SEPARATED BY '.'. "I-JCM131115
PERFORM bdc_dynpro

USING 'SAPMF05A'

'0119'.

PERFORM bdc_field
USING 'BDC_CURSOR'
'BSED-WBANK'.
PERFORM bdc_field
USING 'BDC_OKCODE'
'=BU'.
PERFORM bdc_field
USING 'BKPF-BLDAT'
w_fecha.
PERFORM bdc_field
USING 'BKPF-BLART'
ws_blart.
PERFORM bdc_field
USING 'BKPF-BUKRS'
w_bukrs.
PERFORM bdc_field
USING 'BKPF-BUDAT'
w_fecha.
PERFORM bdc_field
USING 'BKPF-WAERS'
ws_waers.
PERFORM bdc_field
USING 'RF05A-NEWKO' ws_acepta.
PERFORM bdc_field
USING 'BSEG-WRBTR'
w_wrbtrc.
PERFORM bdc_field
USING 'BSEG-HZUON'
ws_kkber.
PERFORM bdc_field
USING 'BSEG-PRCTR'
'9908030001'.
CONCATENATE ws_fdev+6(2) ws_fdev+4(2) ws_fdev(4) INTO w_fecha SEPARATED BY

*
'.'.

PERFORM bdc_field
PERFORM bdc_field

USING 'BSEG-ANFAE'
USING 'BSEG-ZUONR'

w_fecha.
w_nletrac.

"09032016
READ TABLE GTD_LOTE ASSIGNING <FS_LOTE> INDEX L_TABIX.
IF SY-SUBRC EQ 0.
PERFORM bdc_field
USING 'BSEG-SGTXT'
<FS_LOTE>-w_nlotec.
T_LETRAS-w_nlotec = <FS_LOTE>-w_nlotec.
MODIFY T_LETRAS INDEX L_TABIX.
ENDIF.
*
PERFORM bdc_field
USING 'BSEG-SGTXT'
w_nlotec.
"09032016
CLEAR: w_fecha.
CONCATENATE t_letras-budat+6(2) t_letras-budat+4(2) t_letras-budat(4) INTO
w_fecha SEPARATED BY '.'.
PERFORM bdc_field
USING 'BSEG-ZFBDT'
w_fecha.
PERFORM bdc_field
USING 'BSED-WBZOG'
ws_aval1.
PERFORM bdc_field
USING 'BSED-WORT2'
ws_aval2.
PERFORM bdc_field
USING 'BSED-REGIO'
ws_regio.
IF ws_bankk NE space.
PERFORM bdc_field
USING 'BSEC-BANKL'
ws_bankk.
PERFORM bdc_field
USING 'BSEC-BANKS'
'PE'.
PERFORM bdc_field
USING 'BSEC-BANKN'
'***'.
ENDIF.
opt-dismode = 'N'. "w_bi.
opt-updmode = 'A'.
opt-defsize = 'X'.
CALL TRANSACTION 'FBW1' USING
bdcdata
OPTIONS FROM opt
MESSAGES INTO t_msgbi.
*Determinar Errores.
CLEAR: ggw_elog.
READ TABLE t_msgbi WITH KEY msgid = 'F5' msgnr = 312 msgtyp = 'S'.
IF sy-subrc = 0.
w_swg = 'X'.
w_belnr = t_msgbi-msgv1.
w_gjahr = ws_fini(4). "sy-datum(4).
PERFORM revizar_documento USING w_belnr w_gjahr w_sw.
PERFORM revizar_documento_bseg USING w_belnr w_gjahr w_sw.
IF w_sw IS INITIAL.
"09032016
*
PERFORM graba_detalle_lote.

data: w_nlotec1(20) type c.


CLEAR w_nlotec1.
w_nlotec1 = <FS_LOTE>-w_nlotec.
PERFORM graba_detalle_lote USING w_nlotec1.
"09032016
ENDIF.
ELSE.
ROLLBACK WORK.
ggw_elog = 1.
PERFORM log_errores.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
" solicitud_letra
*---------------------------------------------------------------------*
*
Form graba_detalle_lote
*---------------------------------------------------------------------*
FORM graba_detalle_lote USING w_nlotec1.
zlpet_fi_0027-bukrs = w_bukrs.
"09032016
* zlpet_fi_0027-nlote = w_nlotec.
zlpet_fi_0027-nlote = w_nlotec1.
"09032016
zlpet_fi_0027-belnr
zlpet_fi_0027-gjahr
zlpet_fi_0027-buzei
zlpet_fi_0027-iapunte
INSERT zlpet_fi_0027.
t_dlote-bukrs
"09032016
t_dlote-nlote
t_dlote-nlote
"09032016
t_dlote-nletra
t_dlote-belnr
t_dlote-gjahr
t_dlote-buzei
APPEND t_dlote.
ENDFORM.

=
=
=
=

w_belnr.
w_gjahr.
1.
'X'.

= w_bukrs.
= w_nlotec.
= w_nlotec1.
=
=
=
=

w_nletrac.
w_belnr.
w_gjahr.
1.
" graba_detalle_lote

*---------------------------------------------------------------------*
*
Form grabar_documento_origen
*---------------------------------------------------------------------*
FORM grabar_documento_origen .
DATA: ls_det_aux LIKE LINE OF ggt_det_aux.
IF r_opc9 IS INITIAL.
LOOP AT t_detalle WHERE opc = 'X'.
zlpet_fi_0027-bukrs = w_bukrs.

"Manual

"09032016
READ TABLE T_LETRAS WITH KEY DIFDI = t_detalle-ZTERM+2(2).
IF SY-SUBRC EQ 0.

zlpet_fi_0027-nlote = T_LETRAS-w_nlotec.
*
zlpet_fi_0027-nlote = w_nlotec.
ENDIF.
"09032016
zlpet_fi_0027-belnr
zlpet_fi_0027-gjahr
zlpet_fi_0027-buzei
zlpet_fi_0027-iapunte
INSERT zlpet_fi_0027.
ENDLOOP.
ELSE.
*

=
=
=
=

t_detalle-belnr.
t_detalle-gjahr.
t_detalle-buzei.
' '.
"Automatico

LOOP AT t_detalle WHERE opc EQ gc_x


AND
LOOP AT ggt_det_aux INTO ls_det_aux WHERE opc
kunnr
waers
bankl
zlpet_fi_0027-bukrs = w_bukrs.

EQ
EQ
EQ
EQ

gc_x
AND
ggw_kunnr AND
ggw_waers AND
ggw_bankk.

"09032016
READ TABLE T_LETRAS WITH KEY DIFDI = t_detalle-ZTERM+2(2).
IF SY-SUBRC EQ 0.
zlpet_fi_0027-nlote = T_LETRAS-w_nlotec.
*
zlpet_fi_0027-nlote = w_nlotec.
ENDIF.
"09032016
zlpet_fi_0027-belnr
zlpet_fi_0027-gjahr
zlpet_fi_0027-buzei
zlpet_fi_0027-iapunte
INSERT zlpet_fi_0027.
ENDLOOP.
ENDIF.
COMMIT WORK.
ENDFORM.

=
=
=
=

ls_det_aux-belnr.
ls_det_aux-gjahr.
ls_det_aux-buzei.
' '.

" grabar_documento_origen

*---------------------------------------------------------------------*
*
Form rescatar_nombres
*---------------------------------------------------------------------*
FORM agregarceros USING xcodigo TYPE kunnr CHANGING xcodigoc .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = xcodigo
IMPORTING
output = xcodigoc.
ENDFORM .

"AgregarCeros

*&---------------------------------------------------------------------*
*&
Form rescatar_nombres
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM rescatar_nombres.

DATA: i_name1
i_name2
i_name3
i_name4

TYPE
TYPE
TYPE
TYPE

name1,
name1,
name1,
name1.

PERFORM agregarceros USING ws_acepta CHANGING ws_acepta .


SELECT SINGLE name1 name2 name3 name4 INTO (i_name1, i_name2, i_name3, i_name4
)
FROM kna1 WHERE kunnr = ws_acepta.
IF sy-subrc = 0.
CONCATENATE i_name1 i_name2 i_name3 i_name4 INTO ws_name1 SEPARATED BY ' '.
ENDIF.
PERFORM agregarceros USING ws_aval1 CHANGING ws_aval1 .
SELECT SINGLE name1 name2 name3 name4 INTO (i_name1, i_name2, i_name3, i_name
4)
FROM kna1 WHERE kunnr = ws_aval1.
IF sy-subrc = 0.
CONCATENATE i_name1 i_name2 i_name3 i_name4 INTO ws_name2 SEPARATED BY ' '.
.
ENDIF.
PERFORM agregarceros USING ws_aval2 CHANGING ws_aval2 .
SELECT SINGLE name1 name2 name3 name4 INTO (i_name1, i_name2, i_name3, i_name
4)
FROM kna1 WHERE kunnr = ws_aval2.
IF sy-subrc = 0.
CONCATENATE i_name1 i_name2 i_name3 i_name4 INTO ws_name3 SEPARATED BY ' '
..
ENDIF.
ENDFORM.

" rescatar_nombres

CONTROLS: tc_letras TYPE TABLEVIEW USING SCREEN 9000.


*controls: t_let01 type tableview using screen 9003.
*----------------------------------------------------------------------*
* MODULE tc_letras_change_tc_attr OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE tc_letras_change_tc_attr OUTPUT.
DESCRIBE TABLE t_letras LINES tc_letras-lines.
ENDMODULE.
"TC_LETRAS_CHANGE_TC_ATTR OUTPUT
*----------------------------------------------------------------------*
* MODULE tc_letras_modify INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE tc_letras_modify INPUT.
MODIFY t_letras
INDEX tc_letras-current_line.
ENDMODULE.
"TC_LETRAS_MODIFY INPUT
*&---------------------------------------------------------------------*
*&
Form VERIF_FECHA_DIA_HABIL
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text

*----------------------------------------------------------------------*
FORM verif_fecha_dia_habil
USING pi_fec TYPE sy-datum. "I-JCM131115
DATA: num_day TYPE p,
w_newfec TYPE sy-datum,
w_sal
TYPE c.
IF ws_fe NE 'X'.
CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
EXPORTING
date
= w_feccal
factory_calendar_id = 'PE'
IMPORTING
date
= w_newfec.
ELSE.
w_newfec = w_feccal.
ENDIF.
w_sal = ' '.
WHILE w_sal = ' '.
CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
datum = w_newfec
IMPORTING
wotnr = num_day.
IF ws_lu = 'X' AND num_day = 1.
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_ma = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_mi = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_ju = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_vi = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_sa = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSEIF ws_do = 'X' AND num_day =
w_feccal = w_newfec.
w_sal = 'X'.
ELSE.
w_newfec = w_newfec + 1.
ENDIF.
ENDWHILE.

2.
3.
4.
5.
6.
7.

* w_difdi = w_feccal - ws_fini. "D-JCM131115


w_difdi = w_feccal - pi_fec. "I-JCM131115
ENDFORM.

" VERIF_FECHA_DIA_HABIL

*&---------------------------------------------------------------------*
*&
Form CALCULAR_MONTOS
*&---------------------------------------------------------------------*

*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM calcular_montos.
CLEAR: t_recaud, w_realiza, w_valor, w_postm, w_retpago, w_inicial, w_final, w
_equita.
REFRESH: t_recaud.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE t_recaud
FROM zlpet_fi_0033.
LOOP AT t_recaud.
w_realiza = 'X'.
w_valor = 0.
SELECT SINGLE wt_postm
INTO w_postm
FROM t059p
WHERE land1 = 'PE'
AND witht
= t_recaud-witht.
IF w_postm = 2.
w_retpago = 'X'.
ELSE.
w_retpago = ' '.
ENDIF.
LOOP AT t_detalle WHERE opc = 'X'.
CLEAR: l_with_item.
SELECT SINGLE *
INTO l_with_item
FROM with_item
WHERE bukrs
= p_bukrs
AND belnr = t_detalle-belnr
AND gjahr
= t_detalle-gjahr
AND buzei
= t_detalle-buzei
AND witht
=
t_recaud-witht
AND wt_withcd = t_recaud-wt_withcd.
IF sy-subrc = 0.
IF w_retpago = 'X'.
w_valor = l_with_item-wt_qsshb + w_valor.
ELSE.
w_valor = l_with_item-wt_qbshb + w_valor.
ENDIF.
ENDIF.
ENDLOOP.
IF w_retpago = 'X'.
CLEAR: l_t059minmax.
SELECT SINGLE *
INTO l_t059minmax
FROM t059minmax
WHERE land1
= 'PE'
AND witht
= t_recaud-witht
AND wt_withcd =
t_recaud-wt_withcd
AND wt_date LE sy-datum.
IF w_valor LT l_t059minmax-wt_wtminb.
w_realiza = ' '.

w_valor = 0.
ELSE.
CLEAR: l_t059z.
SELECT SINGLE *
INTO l_t059z
FROM t059z
WHERE land1
= 'PE'
AND witht
= t_recaud-witht
AND wt_withcd =
t_recaud-wt_withcd.
w_valor = w_valor * ( l_t059z-qproz / 100 ) * ( l_t059z-qsatz / 100 ).
ENDIF.
ENDIF.
IF w_realiza = 'X'.
IF t_recaud-signoapli = 'NEG'.
w_valor = w_valor * -1.
ENDIF.
IF t_recaud-asigprolet = 'INI'.
w_inicial = w_valor + w_inicial.
ENDIF.
IF t_recaud-asigprolet = 'FIN'.
w_final = w_valor + w_final.
ENDIF.
IF t_recaud-asigprolet = 'EQU'.
w_equita = w_valor + w_equita.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.

" CALCULAR_MONTOS

*&---------------------------------------------------------------------*
*&
Form CAMBIAR_DOCUMENTO
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM cambiar_documento .
DATA: ls_det_aux LIKE LINE OF ggt_det_aux.
SELECT SINGLE *
FROM zlpet_fi_0023
WHERE bukrs = p_bukrs.
REFRESH: i_bseg, i_bkpf.
IF r_opc9 IS INITIAL.
LOOP AT t_detalle WHERE opc = 'X'.
CLEAR: wa_bkpf, war_bseg.
SELECT SINGLE * FROM bkpf INTO wa_bkpf
WHERE bukrs = p_bukrs AND
belnr = t_detalle-belnr AND

"Manual

gjahr = t_detalle-gjahr.
wa_bkpf-xref1_hd = w_nlotec.
APPEND wa_bkpf TO i_bkpf.
SELECT SINGLE * FROM bseg INTO war_bseg
WHERE bukrs = p_bukrs AND
belnr = t_detalle-belnr AND
gjahr = t_detalle-gjahr AND
buzei = t_detalle-buzei.
war_bseg-zlspr = zlpet_fi_0023-zlspr.
wa_bseg
= war_bseg.
APPEND wa_bseg TO i_bseg.
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
t_bkdf = i_bkdf
t_bkpf = i_bkpf
t_bsec = i_bsec
t_bsed = i_bsed
t_bseg = i_bseg
t_bset = i_bset.
COMMIT WORK.
CALL FUNCTION 'DEQUEUE_ALL'.
ENDLOOP.
CALL FUNCTION 'DEQUEUE_ALL'.
ELSE.
"Automatico
*
LOOP AT t_detalle WHERE opc = 'X'
AND
LOOP AT ggt_det_aux INTO ls_det_aux WHERE opc = 'X'
AND
kunnr = ggw_kunnr AND
waers = ggw_waers AND
bankl = ggw_bankk.
CLEAR: wa_bkpf, war_bseg.
SELECT SINGLE * FROM bkpf INTO wa_bkpf
WHERE bukrs = p_bukrs AND
belnr = ls_det_aux-belnr AND
gjahr = ls_det_aux-gjahr.
wa_bkpf-xref1_hd = w_nlotec.
APPEND wa_bkpf TO i_bkpf.
SELECT SINGLE * FROM bseg INTO war_bseg
WHERE bukrs = p_bukrs AND
belnr = ls_det_aux-belnr AND
gjahr = ls_det_aux-gjahr AND
buzei = ls_det_aux-buzei.
war_bseg-zlspr = zlpet_fi_0023-zlspr.
wa_bseg
= war_bseg.
APPEND wa_bseg TO i_bseg.
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
t_bkdf = i_bkdf
t_bkpf = i_bkpf
t_bsec = i_bsec
t_bsed = i_bsed

t_bseg = i_bseg
t_bset = i_bset.
COMMIT WORK.
CALL FUNCTION 'DEQUEUE_ALL'.
ENDLOOP.
CALL FUNCTION 'DEQUEUE_ALL'.
ENDIF.
ENDFORM.
" CAMBIAR_DOCUMENTO
*&---------------------------------------------------------------------*
*&
Form DESPLEGAR_LOG_CANJE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM desplegar_log_canje.
CLEAR: t_lista.
REFRESH: t_lista.
*titulos del LOG
t_lista-nletra = 'CANJE DOCUMENTOS'.
APPEND t_lista.
t_lista-bukrs =
t_lista-nlote =
t_lista-nletra =
t_lista-belnr =
t_lista-gjahr =
APPEND t_lista.

'Soc'.
'Nro. Lote'.
'Nro. Letra'.
'Documento'.
'Ao'.

t_lista-bukrs =
t_lista-nlote =
t_lista-nletra =
t_lista-belnr =
t_lista-gjahr =
APPEND t_lista.

'______'.
'____________________'.
'____________________'.
'________________'.
'__________'.

LOOP AT t_dlote.
t_lista-bukrs
t_lista-nlote
t_lista-nletra
t_lista-belnr
t_lista-gjahr
APPEND t_lista.
ENDLOOP.

=
=
=
=
=

w_bukrs.
t_dlote-nlote.
t_dlote-nletra.
t_dlote-belnr.
t_dlote-gjahr.

CALL FUNCTION 'POPUP_WITH_TABLE_DISPLAY_OK'


EXPORTING
endpos_col = 100
endpos_row = 20
startpos_col = 10
startpos_row = 5
titletext
= 'LOG DE PROCESO'
TABLES
valuetab
= t_lista.

ENDFORM.

" DESPLEGAR_LOG_CANJE

*&---------------------------------------------------------------------*
*&
Module INI_9000 OUTPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
MODULE ini_9000 OUTPUT.
REFRESH: list, t_utili.
CLEAR: list, t_utili.
SELECT wevwv denom
INTO CORRESPONDING FIELDS OF TABLE t_utili
FROM zlpet_fi_0031.
name = 'WS_CUTIL'.
LOOP AT t_utili.
value-key = t_utili-wevwv.
value-text = t_utili-denom.
APPEND value TO list.
ENDLOOP.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
id
= name
values = list.
IF w_ucomm = 'X'.
IF w_vez = 1.
w_vez = 2.
LOOP AT SCREEN.
IF screen-name =
screen-input =
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ELSE.
w_vez = 1.
LOOP AT SCREEN.
IF screen-name =
screen-input =
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
w_ucomm = space.
ENDIF.
ENDMODULE.

'T_LETRAS-DIFDI' OR screen-name = 'T_LETRAS-WRBTR'.


1.

'T_LETRAS-DIFDI' OR screen-name = 'T_LETRAS-WRBTR'.


0.

" INI_9000 OUTPUT

*&---------------------------------------------------------------------*
*&
Form ACTUALIZAR_FEC
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM actualizar_fec
USING pi_fec TYPE sy-datum. "I-JCM131115

* w_feccal = ws_fini + t_letras-difdi. "D-JCM131115


w_feccal = pi_fec + t_letras-difdi. "I-JCM131115
PERFORM verif_fecha_dia_habil
USING pi_fec. "I-JCM131115
t_letras-budat = w_feccal.
t_letras-difdi = w_difdi.
ENDFORM.

" ACTUALIZAR_FEC

*&---------------------------------------------------------------------*
*&
Form CALCULAR_INTERES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM calcular_interes.
* w_igv = c_igv.
SELECT SINGLE rate
INTO w_igv
FROM zlpet_fi_0034
WHERE bukrs = p_bukrs.
CLEAR: w_rest, w_fact, w_inter.
w_rest = t_letras-difdi - ws_dtasa.
w_igv = ( w_igv / 100 ) + 1.
IF w_rest LE 0.
w_inter = 0.
ELSE.
w_inter = ( ( ( 1 + ( ws_tasa / 100 ) ) ** ( w_rest / 360 ) ) - 1 ) * t_letr
as-wrbtr * w_igv.
ENDIF.
ENDFORM.

" CALCULAR_INTERES

*&---------------------------------------------------------------------*
*&
Form CAMBIAR_LETRAS
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM cambiar_letras .
CLEAR: a_x_with_item, i_x_with_item, added_with_item, wa_bkpf, war_bseg, wa_bs
eg.
REFRESH: a_x_with_item, i_x_with_item, added_with_item, i_bkpf, i_bseg.
CALL FUNCTION 'FI_WT_DETERM_RELEVANT_TYPES'
EXPORTING
i_bukrs
= p_bukrs
i_acct
= ws_acepta
i_koart
= 'D'
i_budat
= sy-datum
i_postingtime
= '2'
TABLES

t_with
= a_x_with_item
EXCEPTIONS
nothing_selected = 1
OTHERS
= 2.
IF sy-subrc = 0.
LOOP AT t_dlote.
CLEAR: added_with_item, wa_bkpf, war_bseg, wa_bseg.
REFRESH: added_with_item, i_bkpf, i_bseg.
LOOP AT a_x_with_item.
CLEAR i_x_with_item.
i_x_with_item-bukrs = t_dlote-bukrs.
i_x_with_item-belnr = t_dlote-belnr.
i_x_with_item-gjahr = t_dlote-gjahr.
i_x_with_item-buzei = t_dlote-buzei.
i_x_with_item-koart = 'K'.
i_x_with_item-wt_acco = ws_acepta.
i_x_with_item-wt_stat = 'V'.
i_x_with_item-witht = a_x_with_item-witht.
APPEND i_x_with_item.
MOVE-CORRESPONDING i_x_with_item TO added_with_item.
APPEND added_with_item.
ENDLOOP.
INSERT with_item FROM TABLE added_with_item.
SELECT SINGLE * FROM bkpf INTO wa_bkpf
WHERE bukrs = t_dlote-bukrs AND
belnr = t_dlote-belnr AND
gjahr = t_dlote-gjahr.
wa_bkpf-xref1_hd = w_nlotec.
APPEND wa_bkpf TO i_bkpf.
SELECT SINGLE * FROM bseg INTO war_bseg
WHERE bukrs = t_dlote-bukrs AND
belnr = t_dlote-belnr AND
gjahr = t_dlote-gjahr AND
buzei = t_dlote-buzei.
war_bseg-qsskz = 'XX'.
wa_bseg
= war_bseg.
APPEND wa_bseg TO i_bseg.
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
t_bkdf = i_bkdf
t_bkpf = i_bkpf
t_bsec = i_bsec
t_bsed = i_bsed
t_bseg = i_bseg
t_bset = i_bset.
COMMIT WORK.
CALL FUNCTION 'DEQUEUE_ALL'.
ENDLOOP.
ENDIF.
CALL FUNCTION 'DEQUEUE_ALL'.
ENDFORM.

" CAMBIAR_LETRAS

*&---------------------------------------------------------------------*
*&
Form NOTA_DEBITO
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM nota_debito .
CLEAR: w_interes, w_documentheader, t_accountgl, t_accountreceivable, t_accoun
ttax, t_currencyamount, t_return.
REFRESH: t_accountgl, t_accountreceivable, t_accounttax, t_currencyamount, t_r
eturn.
SELECT SINGLE *
INTO w_interes
FROM zlpet_fi_0034
WHERE bukrs = p_bukrs.
w_documentheader-bus_act
w_documentheader-username
w_documentheader-header_txt
w_documentheader-comp_code
w_documentheader-doc_date
w_documentheader-pstng_date
w_documentheader-doc_type
t_accountgl-itemno_acc
t_accountgl-gl_account
t_accountgl-item_text
t_accountgl-tax_code
APPEND t_accountgl.

=
=
=
=

=
=
=
=
=
=
=

'RFBU'.
sy-uname.
'INTERESES CANJE LETRAS'.
p_bukrs.
ws_fint.
ws_fint.
w_interes-blart.

'0000000002'.
w_interes-saknr.
'INTERESES CANJE LETRAS'.
w_interes-mwskz.

t_accountreceivable-itemno_acc
t_accountreceivable-customer
t_accountreceivable-pymt_meth
t_accountreceivable-pmnt_block
t_accountreceivable-pmnttrms
t_accountreceivable-item_text
APPEND t_accountreceivable.

=
=
=
=
=
=

'0000000001'.
ws_acepta.
w_interes-zlsch.
w_interes-zlspr.
w_interes-zterm.
'INTERESES CANJE LETRAS'.

t_accounttax-itemno_acc = '0000000003'.
t_accounttax-tax_code = w_interes-mwskz.
APPEND t_accounttax.
t_currencyamount-itemno_acc
t_currencyamount-currency
t_currencyamount-amt_doccur
APPEND t_currencyamount.
t_currencyamount-itemno_acc
t_currencyamount-currency
t_currencyamount-amt_doccur
) ) * -1.
APPEND t_currencyamount.
t_currencyamount-itemno_acc
t_currencyamount-currency
t_currencyamount-amt_doccur
teres-rate ) ) * -1.
t_currencyamount-amt_base

= '0000000001'.
= ws_waers.
= ws_inter.
= '0000000002'.
= ws_waers.
= ( ( ws_inter * 100 ) / ( 100 + w_interes-rate
= '0000000003'.
= ws_waers.
= ( ( ws_inter * w_interes-rate ) / ( 100 + w_in
= ( ( ws_inter * 100 ) / ( 100 + w_interes-rat

e ) ) * -1.
APPEND t_currencyamount.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader
= w_documentheader
TABLES
accountgl
= t_accountgl
accountreceivable = t_accountreceivable
accounttax
= t_accounttax
currencyamount
= t_currencyamount
return
= t_return.
READ TABLE t_return WITH KEY type = 'E'.
IF sy-subrc = 0.
LOOP AT t_return.
i_errores-type
= t_return-type.
i_errores-id
= t_return-id.
i_errores-number
= t_return-number.
i_errores-message_v1 = t_return-message_v1.
i_errores-message_v2 = t_return-message_v2.
i_errores-message_v3 = t_return-message_v3.
i_errores-message_v4 = t_return-message_v4.
APPEND i_errores.
ENDLOOP.
PERFORM desplegar_log_alv.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
READ TABLE t_return WITH KEY type = 'S' number = '605'.
IF sy-subrc = 0.
w_belnr = t_return-message_v2(10).
w_gjahr = sy-datum(4).
PERFORM revizar_documento USING w_belnr w_gjahr w_sw.
PERFORM revizar_documento_bseg USING w_belnr w_gjahr w_sw.
IF w_sw IS INITIAL.
t_dlote-bukrs = w_bukrs.
t_dlote-belnr = w_belnr.
t_dlote-gjahr = w_gjahr.
t_dlote-buzei = 1.
APPEND t_dlote.
zlpet_fi_0027-bukrs
zlpet_fi_0027-nlote
zlpet_fi_0027-belnr
zlpet_fi_0027-gjahr
zlpet_fi_0027-buzei
zlpet_fi_0027-iapunte
INSERT zlpet_fi_0027.
ENDIF.
ENDIF.
ENDIF.
ENDFORM.

=
=
=
=
=
=

p_bukrs.
w_nlotec.
w_belnr.
w_gjahr.
1.
' '.

" NOTA_DEBITO

*&---------------------------------------------------------------------*
*&
Form NOTA_DEBITO_FUNCION
*&---------------------------------------------------------------------*
FORM nota_debito_funcion .

DATA: w_belnr
w_kursf
w_hkont
w_ktopl
lv_cebe

TYPE
TYPE
TYPE
TYPE
TYPE

bkpf-belnr,
bkpf-kursf,
bseg-hkont,
ktopl,
prctr.

CLEAR: w_interes, t_return, w_belnr, w_kursf, w_hkont, w_ktopl.", lv_cebe.


REFRESH: t_return, ltd_bkpf, ltd_bseg, ltd_bset.
SELECT SINGLE *
INTO w_interes
FROM zlpet_fi_0034
WHERE bukrs = p_bukrs.
IF ws_waers NE 'PEN'.
PERFORM tipo_cambio USING ws_fini ws_waers CHANGING w_kursf.
ELSE.
w_kursf = 1.
ENDIF.
IF ws_waers NE ws_waer2.
IF ws_waers EQ 'PEN'.
ws_inter = ws_inter * w_kursf2.
ELSE.
ws_inter = ws_inter / w_kursf2.
ENDIF.
ENDIF.
CLEAR: ls_bkpf.
ls_bkpf-bukrs = p_bukrs.
ls_bkpf-gjahr = ws_fini(4).
ls_bkpf-blart = w_interes-blart.
nto
ls_bkpf-bldat = ws_fini.
ento
ls_bkpf-budat = ws_fini.
ilizacion
ls_bkpf-monat = ws_fini+4(2).
ls_bkpf-wwert = ws_fini.
sion
ls_bkpf-usnam = sy-uname.
ls_bkpf-tcode = 'FB01'.
ls_bkpf-bktxt = 'NOTA DEBITO INTERESES'.
cumento
ls_bkpf-waers = ws_waers.
ls_bkpf-glvor = 'RFBU'.
sarial
ls_bkpf-hwaer = 'PEN'.
ls_bkpf-xmwst = 'X'.
ls_bkpf-kursf = w_kursf.
* ls_bkpf-hwae2 = 'USD'.
* ls_bkpf-curt2 = '40'.
* ls_bkpf-kuty2 = 'M'.
n
APPEND ls_bkpf TO ltd_bkpf.
* CLEAR: gv_total_segm, ls_segmento.
* REFRESH: ltd_detalle_segm, ltd_segmento.

"Sociedad
"Ejercicio
"Clase de docume
"Fecha del docum
"Fecha de contab
"Periodo
"Fecha de conver
"Usuario
"Transaccion
"Cabecera del do
"Moneda
"Operacion empre
"Moneda local
"Moneda fuerte
"Tipo Moneda 2
"Tipo Cotizacio

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

APPEND LINES OF t_detalle TO ltd_detalle_segm.


DELETE ltd_detalle_segm WHERE opc IS INITIAL.
SORT ltd_detalle_segm BY segment.
LOOP AT ltd_detalle_segm ASSIGNING <fs_detalle_segm>.
CLEAR: ls_segmento.
ls_segmento-segment = <fs_detalle_segm>-segment.
ls_segmento-dmbtr = <fs_detalle_segm>-dmbtr.
COLLECT ls_segmento INTO ltd_segmento.
gv_total_segm = gv_total_segm + <fs_detalle_segm>-dmbtr.
ENDLOOP.
SORT ltd_segmento BY dmbtr DESCENDING.
READ TABLE ltd_segmento INTO ls_segmento INDEX 1. "Se asigna el mayor ingreso
a la posicion de deudor

CLEAR: gv_ind_segm, gv_dmbtr_last, ls_bseg.


gv_ind_segm = 001.
ls_bseg-bukrs = p_bukrs.
ls_bseg-gjahr = ws_fini(4).
ls_bseg-buzei = gv_ind_segm.
ls_bseg-bschl = '01'.
ilizacion
ls_bseg-koart = 'D'.
* ls_bseg-umskz = ''.
ls_bseg-shkzg = 'S'.
ls_bseg-wrbtr = ws_inter.
ls_bseg-kkber = ws_kkber.
* IF ws_waers NE 'PEN'.
*
ls_bseg-dmbtr = ws_inter * w_kursf.
* ELSE.
*
ls_bseg-dmbtr = ws_inter.
* ENDIF.
SELECT SINGLE akont
INTO w_hkont
FROM knb1
WHERE kunnr EQ ws_acepta
AND bukrs EQ p_bukrs.
IF sy-subrc EQ 0.
ls_bseg-hkont = w_hkont.
ENDIF.
ls_bseg-kunnr = ws_acepta.
ls_bseg-rebzg = ws_rebzg.
ura
ls_bseg-rebzj = ws_rebzj.
ls_bseg-rebzz = ws_rebzz.
ura
* ls_bseg-prctr = ''.
icio
ls_bseg-prctr = w_prctr.
icio
ls_bseg-zuonr = w_nlotec.
ls_bseg-sgtxt = 'INTERESES CANJE LETRAS'.
ls_bseg-zterm = w_interes-zterm.
iones de pago
ls_bseg-zlsch = w_interes-zlsch.
ls_bseg-zlspr = w_interes-zlspr.
ueo de pago
ls_bseg-zfbdt = ws_fini.

"Sociedad
"Ejercicio
"Posicion
"Clave de contab
"Clase Cuenta
"Indicador CME
"Debe/Haber
"Importe de Pago
"Importe en ML
"Importe en ML

"Cuenta de Mayor
"Cliente
"Referencia fact
"Ao de Refer fact
"Centro de benef
"cvc 140913 "Centro de benef
"Asignacion
"Texto posicion
"Clave de condic
"Va de pago
"Clave para bloq

* ls_bseg-kkber = ls_segmento-segment. "Se asigna el mayor ingreso a la posicio


n de deudor
APPEND ls_bseg TO ltd_bseg.
* gv_dmbtr_last = ws_inter.
* READ TABLE t_detalle WITH KEY opc = 'X'.
* IF sy-subrc EQ 0.
*
SELECT SINGLE prctr
*
INTO lv_cebe
*
FROM faglflexa
*
WHERE ryear EQ t_detalle-gjahr
*
AND docnr EQ t_detalle-belnr
*
AND rldnr EQ '0L'
*
AND rbukrs EQ p_bukrs.
* ENDIF.
CLEAR: ls_bseg.
gv_ind_segm = gv_ind_segm + 1.
ls_bseg-bukrs = p_bukrs.
"Sociedad
ls_bseg-gjahr = ws_fint(4).
"Ejercicio
ls_bseg-buzei = gv_ind_segm.
"Posicion
ls_bseg-bschl = '50'.
"Clave de contab
ilizacion
ls_bseg-koart = 'S'.
"Clase Cuenta
ls_bseg-umskz = ''.
"Indicador CME
ls_bseg-shkzg = 'H'.
"Debe/Haber
ls_bseg-mwskz = w_interes-mwskz.
"Indicador IVA
* ls_bseg-wrbtr = ( ( ws_inter * 100 ) / ( 100 + w_interes-rate ) ). "Importe d
e Pago
ls_bseg-wrbtr = ws_inter.
* IF ws_waers NE 'PEN'.
*
ls_bseg-dmbtr = ls_bseg-wrbtr * w_kursf.
"Importe e
n ML
* ELSE.
*
ls_bseg-dmbtr = ls_bseg-wrbtr.
"Importe e
n ML
* ENDIF.
* ls_bseg-kunnr = ws_acepta.
"Cliente
ls_bseg-hkont = w_interes-saknr.
"Cuenta de mayor
ls_bseg-rebzg = ''.
"Referencia fact
ura
ls_bseg-rebzj = ''.
"Ao de Refer fact
ura
SELECT SINGLE prctr
INTO lv_cebe
FROM tka3a
WHERE bukrs EQ p_bukrs
AND kstar EQ w_interes-saknr.
ls_bseg-prctr = w_prctr. "lv_cebe.
ro de beneficio
ls_bseg-zuonr = ''.
ls_bseg-sgtxt = 'INTERESES CANJE LETRAS'.
ls_bseg-zfbdt = ws_fini.
* ls_bseg-KKBER = ls_segmento-segment.
* ls_bseg-zterm = w_interes-zterm.
ciones de pago
* ls_bseg-zlsch = w_interes-zlsch.

"Cent
"Asignacion
"Texto posicion
"Clave de condi
"Va de pago

* ls_bseg-zlspr = w_interes-zlspr.
queo de pago

"Clave para blo

APPEND ls_bseg TO ltd_bseg.


* gv_dmbtr_last = gv_dmbtr_last - ls_bseg-wrbtr.
*
* PERFORM prorrateo_segmento USING w_kursf.
CLEAR: w_hkont.
SELECT SINGLE ktopl
INTO w_ktopl
FROM t001
WHERE bukrs EQ p_bukrs.
SELECT SINGLE konts
INTO w_hkont
FROM t030k
WHERE ktopl EQ w_ktopl
AND ktosl EQ 'MWS'.
REFRESH ltd_bset.
CLEAR ls_bset.
ls_bset-bukrs = p_bukrs.
ls_bset-gjahr = ws_fini(4).
ls_bset-buzei = '001'.
ls_bset-mwskz = w_interes-mwskz.
ls_bset-hkont = w_hkont.
ls_bset-txgrp = '001'.
ls_bset-shkzg = 'H'.
ls_bset-fwbas = ( ( ws_inter * 100 ) / ( 100 + w_interes-rate ) ).
ls_bset-hwbas = ls_bset-fwbas. "* w_kursf.
ls_bset-fwste = ( ( ws_inter * w_interes-rate ) / ( 100 + w_interes-rate ) ).
ls_bset-hwste = ls_bset-fwste. " * w_kursf.
ls_bset-ktosl = 'MWS'.
ls_bset-kschl = 'MWAS'.
APPEND ls_bset TO ltd_bset.
w_belnr = '0000000000'.
CALL FUNCTION 'ZFIPEF_LET01_V02'
IMPORTING
belnr = w_belnr
TABLES
t_bkpf = ltd_bkpf
t_bseg = ltd_bseg
t_bset = ltd_bset.
IF sy-subrc EQ 0.
t_return-type
= 'S'.
t_return-id
= ''.
t_return-number
= '605'.
CONCATENATE 'Se ha creado el documento' w_belnr INTO t_return-message SEPARA
TED BY space.
t_return-log_no
= ''.
t_return-log_msg_no = ''.
t_return-message_v1 = ''.
t_return-message_v2 = w_belnr.
t_return-message_v3 = ''.
t_return-message_v4 = ''.
APPEND t_return.

ELSE.
t_return-type
= 'E'.
t_return-id
= ''.
t_return-number
= ''.
MOVE sy-msgv1 TO t_return-message.
t_return-log_no
= ''.
t_return-log_msg_no = ''.
t_return-message_v1 = ''.
t_return-message_v2 = ''.
t_return-message_v3 = ''.
t_return-message_v4 = ''.
APPEND t_return.
ENDIF.
READ TABLE t_return WITH KEY type = 'E'.
IF sy-subrc = 0.
LOOP AT t_return.
i_errores-type
= t_return-type.
i_errores-id
= t_return-id.
i_errores-number
= t_return-number.
i_errores-message_v1 = t_return-message_v1.
i_errores-message_v2 = t_return-message_v2.
i_errores-message_v3 = t_return-message_v3.
i_errores-message_v4 = t_return-message_v4.
APPEND i_errores.
ENDLOOP.
PERFORM desplegar_log_alv.
ELSE.
READ TABLE t_return WITH KEY type = 'S' number = '605'.
IF sy-subrc = 0.
*

w_belnr = t_return-message_v2(10).
w_gjahr = sy-datum(4).
PERFORM revizar_documento USING w_belnr w_gjahr w_sw.
PERFORM revizar_documento_bseg USING w_belnr w_gjahr w_sw.
IF w_sw IS INITIAL.
t_dlote-bukrs = w_bukrs.
t_dlote-belnr = w_belnr.
t_dlote-gjahr = w_gjahr.
t_dlote-buzei = 1.
APPEND t_dlote.

zlpet_fi_0027-bukrs
zlpet_fi_0027-nlote
zlpet_fi_0027-belnr
zlpet_fi_0027-gjahr
zlpet_fi_0027-buzei
zlpet_fi_0027-iapunte
zlpet_fi_0027-ianotad

=
=
=
=
=
=
=

p_bukrs.
w_nlotec.
w_belnr.
w_gjahr.
1.
' '.
'X'. "Para identificar Nota de Debito Preliminar

!!
INSERT zlpet_fi_0027.
CLEAR zlpet_fi_0027-ianotad.
ENDIF.
ENDIF.
ENDIF.
ENDFORM.

" NOTA_DEBITO_FUNCION

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

*&
Form TIPO_CAMBIO
*&---------------------------------------------------------------------*
FORM tipo_cambio USING p_bldat TYPE bldat
p_waers TYPE waers
CHANGING p_kursf TYPE kursf.
DATA: w_fcobro TYPE d,
w_cobro TYPE d,
w_gdatu LIKE tcurr-gdatu.
CLEAR: w_fcobro, w_cobro, w_gdatu.
*TIPO DE CMABIO ACTUAL
CONCATENATE p_bldat+6(2) p_bldat+4(2) p_bldat(4) INTO w_fcobro.
WHILE p_kursf IS INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_INVDT_INPUT'
EXPORTING
input = w_fcobro
IMPORTING
output = w_gdatu.
SELECT SINGLE ukurs
INTO p_kursf
FROM tcurr
WHERE kurst = 'M'
AND fcurr = p_waers
AND tcurr = 'PEN'
AND gdatu = w_gdatu.
IF p_kursf IS INITIAL.
CONCATENATE w_fcobro+4(4) w_fcobro+2(2) w_fcobro(2) INTO w_cobro.
CALL FUNCTION 'TV_GO_BACK_N_DAYS'
EXPORTING
start_date = w_cobro
n_days
= 1
IMPORTING
hist_date = w_cobro.
CONCATENATE w_cobro+6(2) w_cobro+4(2) w_cobro(4) INTO w_fcobro.
ENDIF.
ENDWHILE.
ENDFORM.
" TIPO_CAMBIO*&--------------------------------------------------------------------*
*& Include
ZFIPE_LET01_CD
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTES_AUTOMATICO
*&---------------------------------------------------------------------*
FORM generar_lotes_automatico TABLES pt_detalle STRUCTURE t_detalle.
IF pt_detalle[] IS NOT INITIAL.
SORT pt_detalle BY kunnr waers.
CASE p_vkbur.
WHEN '3001'.
PERFORM f_generar_group_fact TABLES pt_detalle.
WHEN '3002' OR '3003'.
*
PERFORM f_generar_group_ped TABLES pt_detalle."CSM-09.12.2015
WHEN '3004'.
IF p_group-low EQ 'F'.
PERFORM f_generar_group_fact TABLES pt_detalle.
ELSEIF p_group-low EQ 'P'.
*
PERFORM f_generar_group_ped TABLES pt_detalle. "CSM-15.12.2015
ENDIF.

ENDCASE.
ELSE.
CASE p_vkbur.
WHEN '3001'.
w_cherror = 'X'.
MESSAGE text-x05 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
WHEN '3002' OR '3003'.
w_cherror = 'X'.
MESSAGE text-x06 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
WHEN '3004'.
IF p_group-low EQ 'F'.
w_cherror = 'X'.
MESSAGE text-x05 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ELSEIF p_group-low EQ 'P'.
w_cherror = 'X'.
MESSAGE text-x06 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDCASE.
ENDIF.
ENDFORM.
" GENERAR_LOTES_AUTOMATICO
*&---------------------------------------------------------------------*
*&
Form F_PROPONER_VALORES_LETRA
*&---------------------------------------------------------------------*
FORM f_proponer_valores_letra .
"BEGIN OF I-JCM231015{
DATA: lw_maximo LIKE bsis-wrbtr,
lw_minimo LIKE bsis-wrbtr,
lw_lines
TYPE i,
lw_tabix
TYPE sy-tabix,
lw_tabix2 TYPE sy-tabix,
lw_total
LIKE bsis-wrbtr,
lw_cont
TYPE i.
DATA: lt_letras_aux LIKE STANDARD TABLE OF t_letras,
lt_letras_fin LIKE STANDARD TABLE OF t_letras.
FIELD-SYMBOLS: <fs_let>
LIKE LINE OF lt_letras_aux,
<fs_let_aux> LIKE LINE OF lt_letras_aux,
<fs_det_aux> LIKE LINE OF t_detalle.
"}END OF I-JCM231015
"BEGIN OF I-JCM231015{
IF r_opc9 EQ 'X' AND p_vkbur EQ '3004'.
IF ws_waers EQ 'PEN'.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= ws_fini
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= w_ukurs
valid_from_date = w_fecval
EXCEPTIONS
OTHERS
= 1.
ENDIF.

ENDIF.
"}END OF I-JCM231015
IF r_opc9 EQ 'X'. "I-JCM231015
CLEAR: ws_amort.
ELSE.
IF ws_amort GE ws_wrbtr.
MESSAGE 'Importe de amortizacin debe ser menor a Importe Doc.' TYPE 'E'.
EXIT.
ENDIF.
ENDIF.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
w_ucomm = space.
w_seg = space.
IF r_opc9 EQ 'X'. "I-JCM231015
CLEAR ws_tasa.
ELSE.
IF ws_xint EQ 'X' AND ( ws_tasa = 0 ).
MESSAGE 'Debe ingresar tasa' TYPE 'W'.
w_seg = 'X'.
ENDIF.
ENDIF.
SELECT
INTO
FROM
WHERE
AND
AND

SINGLE bezei
ws_desreg
t005u
bland = ws_regio
spras = 'S'
land1 = 'PE'.

CHECK w_seg = space.


CLEAR: w_inicial, w_final, w_equita, ws_inter, ws_recau.
IF ws_xrec EQ 'X'.
*
PERFORM calcular_montos.
ENDIF.
ws_recau = w_inicial + w_final + w_equita.
PERFORM rescatar_nombres.
CLEAR : t_letras, ws_sumimp.
REFRESH: t_letras.
ws_total =
IF ws_xamo
ws_total
ELSE.
ws_amort
ENDIF.
IF ws_xrec
ws_total
ELSE.
ws_recau
ENDIF.
*
*
*
*
*

ws_wrbtr.
= 'X'.
= ws_total - ws_amort.
= '0.00'.
= 'X'.
= ws_total + ws_recau.
= 0.

BEGIN OF D-JCM121115{
w_feccal = ws_fini.
w_canequi = w_equita / ws_cant.
w_cancal = ( ws_wrbtr - ws_amort ) / ws_cant.
w_cancal = w_cancal + w_canequi.

*}END OF D-JCM121115
* LOOP AT t_detalle ASSIGNING <fs_det_aux>"CSM-07.12.2015
LOOP AT gt_detalle ASSIGNING <fs_det_aux> "CSM-07.12.2015
WHERE opc = 'X'.
"I-JCM121115
w_feccal
w_canequi
w_cancal
w_cancal

=
=
=
=

<fs_det_aux>-budat.
w_equita / <fs_det_aux>-ws_cant.
( <fs_det_aux>-wrbtr - ws_amort ) / <fs_det_aux>-ws_cant.
w_cancal + w_canequi.

"DO ws_cant TIMES. "D-JCM121115


DO <fs_det_aux>-ws_cant TIMES. "I-JCM121115
w_cancal2 = w_cancal.
IF sy-index = 1.
w_cancal2 = w_cancal2 + w_inicial.
ENDIF.
IF sy-index = ws_cant.
IF ws_xrec = 'X'.
w_cancal2 = w_cancal2 + w_final.
ENDIF.
ENDIF.
*
w_multi = ws_dias * sy-index.
"CSM-12.02.2016
w_multi = <fs_det_aux>-zbd1t * sy-index. "CSM-12.02.2016 Tiene que toma
r los dias que le corresponden
"w_feccal = ws_fini + w_multi. "D-JCM131115
w_feccal = <fs_det_aux>-budat + w_multi. "I-JCM131115
PERFORM verif_fecha_dia_habil
USING <fs_det_aux>-budat. "I-JCM131115
t_letras-difdi = <fs_det_aux>-zterm+1(3)."w_difdi."CSM-14.12.2015
t_letras-budat = w_feccal.
t_letras-wrbtr = w_cancal2.
t_letras-vencim = <fs_det_aux>-budat. "I-JCM131115
w_inter = 0.
IF ws_xint = 'X'.
PERFORM calcular_interes.
ENDIF.
t_letras-inter = w_inter.
t_letras-total = t_letras-wrbtr + t_letras-inter.
ws_sumimp = ws_sumimp + t_letras-total .
ws_inter = ws_inter + w_inter.
IF sy-index = ws_cant.
w_dif = ( ws_total + ws_inter ) - ws_sumimp.
t_letras-total = t_letras-total + w_dif.
ws_sumimp = ws_sumimp + w_dif.
ENDIF.
APPEND t_letras.
CLEAR: t_letras.
ENDDO.
ENDLOOP.
IF ws_xint = 'X'.
ws_total = ws_total + ws_inter.
ELSE.

ws_inter = 0.
ENDIF.
ws_total_cj = ws_total.
* Canje en otra moneda diferente.
IF ws_waers NE ws_waer2.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= ws_fini
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= w_ukurs
valid_from_date = w_fecval
EXCEPTIONS
OTHERS
= 1.
w_kursf2 = w_ukurs.
IF ws_waers EQ 'PEN'.
ws_total_cj = ws_total * w_ukurs.
ELSE.
ws_total_cj = ws_total / w_ukurs.
ENDIF.
CLEAR ws_sumimp.
LOOP AT t_letras.
IF ws_waers EQ 'PEN'.
t_letras-wrbtr = t_letras-wrbtr
t_letras-inter = t_letras-inter
t_letras-total = t_letras-wrbtr
ELSE.
t_letras-wrbtr = t_letras-wrbtr
t_letras-inter = t_letras-inter
t_letras-total = t_letras-wrbtr
ENDIF.
MODIFY t_letras.
ADD t_letras-total TO ws_sumimp.
ENDLOOP.
ENDIF.

* w_ukurs.
* w_ukurs.
+ t_letras-inter.
/ w_ukurs.
/ w_ukurs.
+ t_letras-inter.

CLEAR : ws_sumimp, ws_inter.


LOOP AT t_letras.
*
PERFORM actualizar_fec USING t_letras-vencim. "I-JCM131115
w_inter = 0.
IF ws_xint = 'X'.
PERFORM calcular_interes.
ENDIF.
t_letras-inter = w_inter.
t_letras-total = t_letras-wrbtr + t_letras-inter.
ws_sumimp = ws_sumimp + t_letras-total.
ws_inter = ws_inter + w_inter.
MODIFY t_letras.
ENDLOOP.
LOOP AT t_letras.
IF sy-tabix = ws_cant.
w_dif = ws_total_cj - ws_sumimp.

t_letras-total = t_letras-total + w_dif.


ws_sumimp = ws_sumimp + w_dif.
MODIFY t_letras.
ENDIF.
ENDLOOP.
ENDFORM.
" F_PROPONER_VALORES_LETRA
*&---------------------------------------------------------------------*
*&
Form F_GRABAR_LOTES
*&---------------------------------------------------------------------*
FORM f_grabar_lotes_letra .
IF ws_sumimp IS NOT INITIAL.
ws_dif = abs( ws_sumimp - ws_total_cj ).
IF ( ws_dif NE 0 AND ws_waers EQ ws_waer2 ) OR ( ws_dif > 1 AND ws_waers NE
ws_waer2 ).
MESSAGE 'Importe Total no cuadra con Suma Importes' TYPE 'I'.
ELSE.
IF w_vez = 1.
PERFORM grabar_lote.
PERFORM solicitud_letra.
CHECK ggw_elog IS INITIAL.
"Continua si solo si no hay problemas al g
enerar LETRAS (FBW1)
IF ws_xint = 'X' AND ws_inter > 0.
PERFORM nota_debito_funcion.
ENDIF.
PERFORM cambiar_letras.
PERFORM cambiar_documento.
"Manual y Automatico ??
IF r_opc9 IS NOT INITIAL.
"Automatico
CHECK w_swg = gc_x.
PERFORM grabar_documento_origen."Actualiza zlpet_fi_0027 con Doc.origi
nales
PERFORM rescatar_datos2.
"CSM-18.12.2015 Para Contabilizar ??
PERFORM aceptar_lote.
"CSM-18.12.2015 Para Contabilizar ??
ENDIF.
CHECK r_opc9 IS INITIAL.
"CSM-18.12.2015 Que muestre solo cuando sea
Manual
PERFORM desplegar_log_canje.
IF r_opc9 NE 'X'. "I-JCM231015
LEAVE TO SCREEN 0.
ENDIF.
ELSE.
MESSAGE 'Recalculo incompleto' TYPE 'I'.
ENDIF.
ENDIF.
ELSE.
MESSAGE 'No hay propuesta de letras' TYPE 'I'.
ENDIF.
ENDFORM.
" F_GRABAR_LOTES
*&---------------------------------------------------------------------*
*&
Form F_GENERAR_GROUP_FACT
*&---------------------------------------------------------------------*
FORM f_generar_group_fact TABLES ptt_detalle STRUCTURE t_detalle.
TYPES: BEGIN OF lty_vbrp,
vbeln TYPE vbrp-vbeln,
aubel TYPE vbrp-aubel,
END OF lty_vbrp,
BEGIN OF lty_vbak,
vbeln TYPE vbak-vbeln,
xblnr TYPE vbak-xblnr,
END OF lty_vbak.

DATA: lt_detalle_aux1
lt_detalle_aux2
lt_detalle_aux3
lt_vbrp
lt_vbak

LIKE
LIKE
LIKE
TYPE
TYPE

STANDARD
STANDARD
STANDARD
STANDARD
STANDARD

FIELD-SYMBOLS: <fs_detalle_aux1>
<fs_detalle_aux2>
<fs_detalle_aux3>
<fs_vbrp_f>
<fs_vbak_f>

TABLE
TABLE
TABLE
TABLE
TABLE

LIKE
LIKE
LIKE
LIKE
LIKE

LINE
LINE
LINE
LINE
LINE

OF
OF
OF
OF
OF
OF
OF
OF
OF
OF

t_detalle,
t_detalle,
t_detalle,
lty_vbrp,
lty_vbak.
lt_detalle_aux1,
lt_detalle_aux2,
lt_detalle_aux3,
lt_vbrp,
lt_vbak.

lt_detalle_aux1[] = ptt_detalle[].
lt_detalle_aux2[] = ptt_detalle[].
lt_detalle_aux3[] = ptt_detalle[].
DELETE lt_detalle_aux1 WHERE blart IN gr_blart_nx.
DELETE lt_detalle_aux2 WHERE blart NOT IN gr_blart_nx.
IF lt_detalle_aux2[] IS NOT INITIAL.
"Obtener los documentos de ventas
SELECT vbeln aubel
INTO TABLE lt_vbrp
FROM vbrp
FOR ALL ENTRIES IN lt_detalle_aux2
WHERE vbeln EQ lt_detalle_aux2-vbeln.
IF lt_vbrp[] IS NOT INITIAL.
SORT lt_vbrp BY aubel.
DELETE ADJACENT DUPLICATES FROM lt_vbrp COMPARING aubel.
SELECT
INTO
FROM
FOR
WHERE

vbeln xblnr
TABLE lt_vbak
vbak
ALL ENTRIES IN lt_vbrp
vbeln EQ lt_vbrp-aubel.

SORT lt_vbak BY xblnr.


DELETE ADJACENT DUPLICATES FROM lt_vbak COMPARING xblnr.
ENDIF.
ENDIF.
SORT lt_detalle_aux3 BY kunnr belnr gjahr.
SORT lt_vbrp BY vbeln.
SORT lt_vbak BY vbeln.
LOOP AT lt_detalle_aux2 ASSIGNING <fs_detalle_aux2>.
READ TABLE lt_vbrp ASSIGNING <fs_vbrp_f> WITH KEY vbeln = <fs_detalle_aux2>vbeln BINARY SEARCH.
IF sy-subrc EQ 0.
READ TABLE lt_vbak ASSIGNING <fs_vbak_f> WITH KEY vbeln = <fs_vbrp_f>-aube
l BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux2>-xblnr_fac = <fs_vbak_f>-xblnr.
ENDIF.
ENDIF.
ENDLOOP.
SORT lt_detalle_aux2 BY xblnr_fac.

LOOP AT lt_detalle_aux2 ASSIGNING <fs_detalle_aux2>.


READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr = <fs_
detalle_aux2>-kunnr
belnr = <fs_
detalle_aux2>-belnr
gjahr = <fs_
detalle_aux2>-gjahr BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux3>-opc = 'X'.
<fs_detalle_aux3>-venci = 'X'.
ENDIF.
AT END OF xblnr_fac.
LOOP AT lt_detalle_aux1 ASSIGNING <fs_detalle_aux1> WHERE xblnr EQ <fs_det
alle_aux2>-xblnr_fac.
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr =
<fs_detalle_aux1>-kunnr
belnr =
<fs_detalle_aux1>-belnr
gjahr =
<fs_detalle_aux1>-gjahr BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux3>-opc = 'X'.
<fs_detalle_aux3>-venci = 'X'.
ENDIF.
ENDLOOP.
IF lt_detalle_aux3[] IS NOT INITIAL.
IF r_opc9 EQ 'X'.
PERFORM generar_lotes TABLES lt_detalle_aux3.
DELETE lt_detalle_aux3 WHERE venci EQ 'X'.
ENDIF.
ELSE.
w_cherror = 'X'.
MESSAGE text-x05 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDAT.
ENDLOOP.
* LOOP AT lt_detalle_aux1 ASSIGNING <fs_detalle_aux1>.
*
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr = <fs
_detalle_aux1>-kunnr
*
belnr = <fs
_detalle_aux1>-belnr
*
gjahr = <fs
_detalle_aux1>-gjahr BINARY SEARCH.
*
IF sy-subrc EQ 0.
*
<fs_detalle_aux3>-opc = 'X'.
*
<fs_detalle_aux3>-venci = 'X'.
*
ENDIF.
*
LOOP AT lt_detalle_aux2 ASSIGNING <fs_detalle_aux2> WHERE kunnr EQ <fs_deta
lle_aux1>-kunnr
*
AND rebzg EQ <fs_deta
lle_aux1>-belnr
*
AND rebzj EQ <fs_deta
lle_aux1>-gjahr.
*
*
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr = <

fs_detalle_aux2>-kunnr
*
belnr = <
fs_detalle_aux2>-belnr
*
gjahr = <
fs_detalle_aux2>-gjahr BINARY SEARCH.
*
IF sy-subrc EQ 0.
*
<fs_detalle_aux3>-opc = 'X'.
*
<fs_detalle_aux3>-venci = 'X'.
*
ENDIF.
*
ENDLOOP.
*
*
IF lt_detalle_aux3[] IS NOT INITIAL.
*
PERFORM generar_lotes TABLES lt_detalle_aux3.
*
DELETE lt_detalle_aux3 WHERE venci EQ 'X'.
*
ENDIF.
* ENDLOOP.
ENDFORM.
" F_GENERAR_GROUP_FACT
*&---------------------------------------------------------------------*
*&
Form F_GENERAR_GROUP_PED
*&---------------------------------------------------------------------*
FORM f_generar_group_ped TABLES ptt_detalle STRUCTURE t_detalle.
DATA: lt_detalle_aux1 LIKE STANDARD TABLE OF t_detalle,
lt_detalle_aux2 LIKE STANDARD TABLE OF t_detalle,
lt_detalle_aux3 LIKE STANDARD TABLE OF t_detalle,
lt_detalle_aux4 LIKE STANDARD TABLE OF t_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln,
lw_subrc TYPE sy-subrc.
FIELD-SYMBOLS: <fs_detalle_aux1>
<fs_detalle_aux2>
<fs_detalle_aux3>
<fs_vbrp_automat>

LIKE
LIKE
LIKE
LIKE

LINE
LINE
LINE
LINE

OF
OF
OF
OF

lt_detalle_aux1,
lt_detalle_aux2,
lt_detalle_aux3,
gt_vbrp_aut.

lt_detalle_aux1[] = ptt_detalle[].
lt_detalle_aux2[] = ptt_detalle[].
lt_detalle_aux3[] = ptt_detalle[].
SORT gt_vbrp_aut BY vbeln.
SORT lt_detalle_aux3 BY kunnr belnr gjahr.
LOOP AT lt_detalle_aux1 ASSIGNING <fs_detalle_aux1>.
CLEAR lw_vbeln.
lw_vbeln = <fs_detalle_aux1>-vbeln.
READ TABLE gt_vbrp_aut ASSIGNING <fs_vbrp_automat> WITH KEY vbeln = lw_vbeln
BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux1>-aubel = <fs_vbrp_automat>-aubel.
<fs_detalle_aux1>-aupos = <fs_vbrp_automat>-aupos.
ENDIF.
ENDLOOP.
SORT lt_detalle_aux1 BY kunnr aubel aupos.
LOOP AT lt_detalle_aux1 ASSIGNING <fs_detalle_aux1>.
REFRESH lt_detalle_aux4.
CLEAR lw_subrc.
*
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr = <fs
_detalle_aux1>-kunnr
*
belnr = <fs

_detalle_aux1>-belnr
*
gjahr = <fs
_detalle_aux1>-gjahr BINARY SEARCH.
*
IF sy-subrc EQ 0.
*
<fs_detalle_aux3>-opc = 'X'.
*
<fs_detalle_aux3>-venci = 'X'.
*
ENDIF.
lw_subrc = 4.
LOOP AT lt_detalle_aux2 ASSIGNING <fs_detalle_aux2> WHERE kunnr EQ <fs_detal
le_aux1>-kunnr
AND aubel EQ <fs_detal
le_aux1>-aubel
AND aupos EQ <fs_detal
le_aux1>-aupos.
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr
s_detalle_aux2>-kunnr
belnr
s_detalle_aux2>-belnr
gjahr
s_detalle_aux2>-gjahr BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux3>-opc = 'X'.
<fs_detalle_aux3>-venci = 'X'.
lw_subrc = 0.
ENDIF.
ENDLOOP.
IF lw_subrc EQ 0.
READ TABLE lt_detalle_aux3 ASSIGNING <fs_detalle_aux3> WITH KEY kunnr
s_detalle_aux1>-kunnr
belnr
s_detalle_aux1>-belnr
gjahr
s_detalle_aux1>-gjahr BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_detalle_aux3>-opc = 'X'.
<fs_detalle_aux3>-venci = 'X'.
ENDIF.
lt_detalle_aux4[] = lt_detalle_aux3[].
DELETE lt_detalle_aux4 WHERE venci NE 'X'.
ENDIF.
IF lt_detalle_aux4[] IS NOT INITIAL.
IF r_opc9 EQ 'X'.
PERFORM generar_lotes TABLES lt_detalle_aux4.
DELETE lt_detalle_aux3 WHERE venci EQ 'X'.
ENDIF.
ELSE.
w_cherror = 'X'.
MESSAGE text-x06 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDLOOP.
ENDFORM.
" F_GENERAR_GROUP_PED
*&---------------------------------------------------------------------*
*&
Form F_CALCULO_CANT_LETXFACT
*&---------------------------------------------------------------------*
FORM f_calculo_cant_letxfact USING
pi_min TYPE bsis-wrbtr
pi_max TYPE bsis-wrbtr

= <f
= <f
= <f

= <f
= <f
= <f

pi_times TYPE i
pi_flag TYPE c
pi_zw_div TYPE c
CHANGING po_det LIKE LINE OF ggt_det_aux.
DATA: lwi_wrbtr TYPE bsis-wrbtr.
CHECK pi_flag IS INITIAL.
* lwi_wrbtr = po_det-wrbtr / pi_times.
lwi_wrbtr = ws_wrbtr / pi_times.
IF ( lwi_wrbtr BETWEEN pi_min AND pi_max )." OR ( lwi_wrbtr < pi_min ). "CSM-0
2.12.2015
po_det-ws_cant = pi_times.
pi_flag = 'X'.
pi_zw_div = 1.
ELSEIF lwi_wrbtr > pi_max.
pi_zw_div = 1.
ADD 1 TO pi_times.
PERFORM f_calculo_cant_letxfact USING
pi_min pi_max
pi_times pi_flag pi_zw_div
CHANGING po_det.
ENDIF.
ENDFORM.
" F_CALCULO_CANT_LETXFACT
*&---------------------------------------------------------------------*
*&
Form F_VALIDAR_MINIMO
*&---------------------------------------------------------------------*
FORM f_validar_minimo
USING p_minim TYPE dmbtr
CHANGING po_chare TYPE c.
CLEAR po_chare.
IF p_minim LT 100.
po_chare = 'X'.
MESSAGE text-x03 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDFORM.
" F_VALIDAR_MINIMO
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTES_2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM generar_lotes_2 TABLES pt_detalle STRUCTURE t_detalle.
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
ls_det
TYPE ty_detalle.
"BEGIN OF I-JCM121115{
DATA: lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_times TYPE i,
lw_flag
TYPE c,
lw_zsw
TYPE c,
lw_zswp
TYPE c,
lw_zsw_aubel TYPE c,
lw_zw_div
TYPE c,

lw_zsw_div TYPE c.
"}END OF I-JCM121115
FIELD-SYMBOLS: <fs_det>
LIKE LINE OF t_detalle. "I-JCM121115
FIELD-SYMBOLS: <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
DATA: ls_detalle LIKE LINE OF t_detalle. "CSM-07.12.2015
CLEAR: ws_waers, ws_wrbtr, ws_cant, w_sw, w_kunnr, ws_bankk, w_kunnr, ws_amort
.
READ TABLE pt_detalle WITH KEY opc = 'X'.
IF sy-subrc NE 0.
w_sw = 'X'.
MESSAGE 'Seleccione documento(s)' TYPE 'E'.
ENDIF.
CLEAR : w_prctr, ws_fini, ws_rebzg, ws_rebzj.
"cvc 140913
REFRESH: gt_detalle.
****************
*Actualiza AUBEL
****************
PERFORM actualiza_ubel TABLES pt_detalle.
IF ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_f ).
lt_detalle[] = pt_detalle[].
ENDIF.
********************
*L O G I C A
********************
LOOP AT pt_detalle
ASSIGNING <fs_det> "I-JCM121115
WHERE opc = 'X'.
CLEAR: lw_flag,lw_zw_div. "I-JCM121115
CLEAR: ls_detalle.

IF w_kunnr IS INITIAL.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
ws_cutil = t_detalle-umskz.
ELSE.
IF w_kunnr <> <fs_det>-kunnr.
MESSAGE 'Documentos seleccionados son de clientes diferentes' TYPE 'E'.
w_sw = 'X'.
EXIT.
ELSE.
IF ws_waers <> <fs_det>-waers.
MESSAGE 'Documentos seleccionados con monedas diferentes' TYPE 'E'.
w_sw = 'X'.
EXIT.
ELSE.
IF ws_bankk <> <fs_det>-bankl.
MESSAGE 'Documentos seleccionados con bancos diferentes' TYPE 'E'.
w_sw = 'X'.
EXIT.
ENDIF.
ENDIF.
ENDIF.

ENDIF.
PERFORM doc_bloqueado USING <fs_det>-belnr <fs_det>-gjahr w_sw.
IF w_sw = 'X'.
CONCATENATE 'Documento' <fs_det>-belnr 'bloqueado por usuario' INTO w_mess
SEPARATED BY space.
MESSAGE w_mess TYPE 'E'.
EXIT.
ENDIF.
"ws_wrbtr = ws_wrbtr + pt_detalle-wrbtr. "D-JCM121115
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr. "I-JCM121115
*
ws_cant = ws_cant + 1.
*

cvc 140913
IF w_prctr IS INITIAL AND <fs_det>-prctr IS NOT INITIAL.
w_prctr = <fs_det>-prctr.
ENDIF.

IF <fs_det>-bldat > ws_fini.


ws_fini = <fs_det>-bldat.
ws_rebzg = <fs_det>-belnr.
ws_rebzj = <fs_det>-gjahr.
ws_rebzz = <fs_det>-buzei.
ENDIF.
* End cvc
"BEGIN OF I-JCM121115{
IF ( p_vkbur EQ '3004' ) AND ( p_group-low EQ gc_group_f ).
*
CLEAR lw_zsw_aubel.
*
LOOP AT lt_detalle INTO ls_det WHERE aubel = <fs_det>-aubel.
*
ADD 1 TO lw_zsw_aubel.
*
ENDLOOP.
*
CHECK lw_zsw_aubel = 1.
"Calcular el tipo de cambio para el calculo de los maximos y minimos impor
tes comprendidos
IF ws_waers EQ 'PEN'.
CLEAR: lw_fecval, lw_ukurs, lw_minimg, lw_maximg.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc
lw_minimg
lw_maximg
ENDIF.
ELSE.
lw_minimg =
lw_maximg =
ENDIF.

EQ 0.
= p_minim * lw_ukurs.
= p_maxim * lw_ukurs.
p_minim.
p_maxim.

"Calcular la cantidad de letras por factura


IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
lw_zsw = 1.

<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.
lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact

USING lw_minimg lw_maximg


lw_times lw_flag lw_zw_div
CHANGING <fs_det>.

CHECK lw_zw_div IS NOT INITIAL.


ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
ENDIF.
ELSEIF p_vkbur EQ gc_3001.
PERFORM generar_lote_3001 USING gt_detalle CHANGING <fs_det> lw_zsw. "CS
M-09.12.2015
ENDIF.
ws_cant = ws_cant + <fs_det>-ws_cant.
ws_dias = <fs_det>-zbd1t.
ENDLOOP.
**************************************
*Genera lote 3002/3003/3004 GROUP = P
**************************************
IF ( ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3003 ) ).
"CSM-09.12.201
5
PERFORM genera_lote_3_0203 TABLES pt_detalle
USING gt_detalle
CHANGING lw_zsw lw_zswp.
ENDIF.
IF ( ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_p ) ) ."CSM-17.12.20
15
PERFORM genera_lote_3004p TABLES pt_detalle
USING gt_detalle
CHANGING lw_zsw lw_zswp .
ENDIF.
**************************
CALL FUNCTION 'DEQUEUE_ALL'.
CLEAR:ws_wrbtr.
LOOP AT gt_detalle ASSIGNING <fs_det>.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
ENDLOOP.
IF ( lw_zsw IS INITIAL ) AND ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_gr
oup_f ).
w_sw = 'X'.
MESSAGE text-e01 TYPE 'E'. "CSM-07.12.2015
ELSEIF ( lw_zswp IS INITIAL ) AND ( ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_
3003 ) OR ( ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_p ) ) ).
w_sw = 'X'.
MESSAGE text-e06 TYPE 'E'.
ELSEIF ( lw_zw_div IS INITIAL ) AND ( lw_zsw_div IS NOT INITIAL ).
w_sw = 'X'.
MESSAGE text-e07 TYPE 'E'.
ELSEIF ( lw_zswp IS INITIAL AND lw_zsw IS INITIAL ) AND

( ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3003 ) OR ( ( p_vkbur EQ g


c_3004 ) AND ( p_group-low EQ gc_group_p ) ) ).
w_sw = 'X'.
MESSAGE text-e08 TYPE 'E'.
ELSEIF ( lw_zswp IS NOT INITIAL AND lw_zsw IS INITIAL ) AND
( ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3003 ) OR ( ( p_vkbur EQ g
c_3004 ) AND ( p_group-low EQ gc_group_p ) ) ).
w_sw = 'X'.
MESSAGE text-e01 TYPE 'E'.
ENDIF.
IF w_sw IS INITIAL.
CLEAR: t_letras, ws_acepta, ws_name1, ws_aval1, ws_name2, ws_aval2, ws_nam
e3, ws_desreg.
REFRESH: t_letras.
w_swg
= ' '.
w_gjahr = sy-datum(4).
ws_xint = ' '.
ws_xrec = 'X'.
ws_xamo = ' '.
*
ws_fini = sy-datum.
ws_fint = ws_fini. "sy-datum.
*

ws_dias = 30. "D-JCM121115


ws_lu = 'X'.
ws_ma = 'X'.
ws_mi = 'X'.
ws_ju = 'X'.
ws_vi = 'X'.
ws_sa = 'X'.
ws_do = 'X'.
ws_fe = 'X'.
ws_inter = 0.
ws_recau = 0.
ws_total = 0.
ws_sumimp = 0.
ws_acepta = w_kunnr.
ws_cant = 1. "D-JCM121115
ws_total_cj = ws_wrbtr.
CONDENSE ws_cant.
SELECT SINGLE *
FROM zlpet_fi_0023
WHERE bukrs = p_bukrs.
IF sy-subrc = 0.
ws_blart = zlpet_fi_0023-blart.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
IF ws_cutil EQ space.
ws_cutil = zlpet_fi_0023-wevwv.
ENDIF.
ws_tasa = zlpet_fi_0023-tasaint.
ws_dtasa = zlpet_fi_0023-diasint."MTC141013 ZFIPET_021_10
ENDIF.
SELECT SINGLE *
FROM zlpet_fi_0030
WHERE kunnr = ws_acepta.
IF sy-subrc = 0.
ws_aval1 = zlpet_fi_0030-aval1.

ws_aval2 = zlpet_fi_0030-aval2.
ws_tasa = zlpet_fi_0030-tasaint.
ws_dtasa = zlpet_fi_0030-diasint.
ENDIF.
w_vez = 1.
w_ucomm = space.
LOOP AT pt_detalle WHERE opc = 'X'.
ws_regio = pt_detalle-regio.
ws_kkber = pt_detalle-kkber.
EXIT.
ENDLOOP.
"BEGIN OF U-JCM231015{
"CALL SCREEN '9000' STARTING AT 30 1.
IF r_opc9 NE 'X'.
CALL SCREEN '9000' STARTING AT 30 1.
ELSE.
PERFORM f_proponer_valores_letra.
PERFORM f_grabar_lotes_letra.
ENDIF.
"}END OF U-JCM231015
IF w_swg = 'X'.
PERFORM grabar_documento_origen.
PERFORM rescatar_datos1.
ENDIF.
ENDIF.
ENDFORM.
" GENERAR_LOTES_2
*&---------------------------------------------------------------------*
*&
Form VALIDA_CANTIDADES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_T_DETALLE text
*----------------------------------------------------------------------*
FORM valida_cantidades TABLES p_detalle STRUCTURE t_detalle
CHANGING lw_sw.
DATA: lw_wrbtr
lc_x
lw_ukurs
lw_fecval
lw_zsw_s
lw_zsw_d

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

bsid-wrbtr,
c LENGTH 1 VALUE 'X',
tcurr-ukurs,
bkpf-budat,
c,
c.

FIELD-SYMBOLS:<fs_detalle> LIKE LINE OF t_detalle.


DATA: ls_detalle LIKE LINE OF t_detalle.
CLEAR lw_sw.
*****************
*Verifica moneda
*****************
CHECK r_opc9 IS INITIAL.
LOOP AT p_detalle ASSIGNING <fs_detalle> WHERE opc = 'X'.
ls_detalle = <fs_detalle>.
lw_wrbtr = lw_wrbtr + <fs_detalle>-wrbtr. "Acumula valores
IF <fs_detalle>-waers = gc_pen.
lw_zsw_s = 1.

ELSEIF <fs_detalle>-waers = gc_usd.


lw_zsw_d = 1.
ENDIF.
ENDLOOP.
IF ( lw_zsw_s IS NOT INITIAL ) AND ( lw_zsw_d IS NOT INITIAL ).
MESSAGE text-e02 TYPE 'E'.
w_sw = gc_x.
EXIT.
ENDIF.
CHECK w_sw IS INITIAL.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF ( ls_detalle-waers = gc_pen ) AND ( lw_wrbtr < ( lw_ukurs * gw_minimo ) ).
lw_sw = 1.
ELSEIF ( ls_detalle-waers = gc_usd ) AND ( lw_wrbtr < gw_minimo ).
lw_sw = 1.
ENDIF.
ENDFORM.
" VALIDA_CANTIDADES
*&---------------------------------------------------------------------*
*&
Form ACUMULA_VAOLRES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_T_DETALLE text
*----------------------------------------------------------------------*
FORM acumula_vaolres TABLES pt_detalle STRUCTURE t_detalle
USING p_tabix p_minimg p_maximg
CHANGING p_wrbtr TYPE bsid-wrbtr.
DATA: lw_tabix TYPE sy-tabix.
gt_detalle[] = pt_detalle[].
lw_tabix = p_tabix.
DO.
READ TABLE gt_detalle INTO gs_detalle INDEX lw_tabix.
ADD 1 TO lw_tabix.
IF ( sy-subrc = 0 ) AND ( gs_detalle-opc = gc_x ).
p_wrbtr = p_wrbtr + gs_detalle-wrbtr.
IF p_wrbtr BETWEEN p_minimg AND p_maximg.
EXIT.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDFORM.
" ACUMULA_VAOLRES
*&---------------------------------------------------------------------*

*&
Form F_CALCULO_CANT_LETXFACT_2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_LW_MINIMG text
*
-->P_LW_MAXIMG text
*
-->P_LW_TIMES text
*
-->P_LW_FLAG text
*
-->P_WS_WRBTR text
*
<--P_<FS_DET> text
*----------------------------------------------------------------------*
FORM f_calculo_cant_letxfact_2 USING pi_min TYPE bsis-wrbtr
pi_max TYPE bsis-wrbtr
pi_times TYPE i
pi_flag TYPE c
pi_wrbtr TYPE bsis-wrbtr
CHANGING po_det STRUCTURE t_detalle.
DATA: lwi_wrbtr TYPE bsis-wrbtr.
CHECK pi_flag IS INITIAL.
lwi_wrbtr = pi_wrbtr / pi_times.
IF ( lwi_wrbtr BETWEEN pi_min AND pi_max ) OR ( lwi_wrbtr < pi_min ).
po_det-ws_cant = pi_times.
pi_flag = 'X'.
ELSE.
ADD 1 TO pi_times.
PERFORM f_calculo_cant_letxfact_2 USING pi_min
pi_max
pi_times
pi_flag
pi_wrbtr
CHANGING po_det.
ENDIF.
ENDFORM.
" F_CALCULO_CANT_LETXFACT_2
*&---------------------------------------------------------------------*
*&
Form CARGA_CONSTANTES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM carga_constantes .
DATA: lt_const TYPE STANDARD TABLE OF zostb_constantes,
ls_const TYPE zostb_constantes.
CONSTANTS: lc_mod TYPE zosed_modulo
VALUE 'FI',
lc_apli TYPE zosed_aplicacion VALUE 'OS_ACEPT_LETRAS',
lc_prg TYPE programm
VALUE 'ZLPEP_FI_0026_V2'.
SELECT * INTO TABLE lt_const
FROM zostb_constantes
WHERE modulo
EQ lc_mod
AND aplicacion EQ lc_apli
AND programa EQ lc_prg.
LOOP AT lt_const INTO ls_const.

CASE ls_const-campo.
WHEN 'MINIMO'.
gw_minimo = ls_const-valor1.
ENDCASE.
ENDLOOP.
ENDFORM.
" CARGA_CONSTANTES
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTE_3001
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM generar_lote_3001 USING pt_detalle TYPE tab_detalle
CHANGING p_det LIKE LINE OF t_detalle lw_zsw TYPE c.
DATA: lw_minimg
lw_maximg
lw_ukurs
lw_fecval
lw_times
lw_flag

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

bsis-wrbtr,
bsis-wrbtr,
tcurr-ukurs,
bkpf-budat,
i,
c.

FIELD-SYMBOLS:<fs_det> LIKE LINE OF t_detalle.


DATA: ls_detalle LIKE LINE OF t_detalle.
CLEAR: ws_waers, ws_wrbtr, w_sw, ws_bankk, w_kunnr, ws_amort.
CLEAR: w_prctr,
ws_fini,
ws_rebzg,
ws_rebzj,
lw_flag.
CLEAR: ls_detalle.
ASSIGN p_det TO <fs_det>.
IF w_kunnr IS INITIAL.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
ELSE.
IF w_kunnr <> <fs_det>-kunnr. "Cliente
MESSAGE text-e03 TYPE 'E'.
w_sw = gc_x.
EXIT.
ELSE.
IF ws_waers <> <fs_det>-waers."Moneda
MESSAGE text-e04 TYPE 'E'.
w_sw = gc_x.
EXIT.
ELSE.
IF ws_bankk <> <fs_det>-bankl. "Bancos
MESSAGE text-e05 TYPE 'E'.
w_sw = gc_x.
EXIT.

ENDIF.
ENDIF.
ENDIF.
ENDIF.
PERFORM doc_bloqueado USING <fs_det>-belnr <fs_det>-gjahr w_sw.
IF w_sw = gc_x.
CONCATENATE 'Documento' <fs_det>-belnr 'bloqueado por usuario' INTO w_mess S
EPARATED BY space.
MESSAGE w_mess TYPE 'E'.
EXIT.
ENDIF.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
IF w_prctr IS INITIAL AND <fs_det>-prctr IS NOT INITIAL.
w_prctr = <fs_det>-prctr.
ENDIF.
IF <fs_det>-bldat > ws_fini.
ws_fini = <fs_det>-bldat.
ws_rebzg = <fs_det>-belnr.
ws_rebzj = <fs_det>-gjahr.
ws_rebzz = <fs_det>-buzei.
ENDIF.
"Calcular el tipo de cambio para el calculo de los maximos y minimos importes
comprendidos
IF ws_waers EQ gc_pen.
CLEAR: lw_fecval, lw_ukurs, lw_minimg, lw_maximg.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc EQ 0.
lw_minimg = p_minim * lw_ukurs.
lw_maximg = p_maxim * lw_ukurs.
ENDIF.
ELSE.
lw_minimg = p_minim.
lw_maximg = p_maxim.
ENDIF.
********************************************
*Valida si es menor al minimo
********************************************
IF <fs_det>-waers EQ gc_pen.
CHECK ws_wrbtr > ( lw_ukurs * gw_minimo ).
lw_zsw
= 1.
<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO pt_detalle.
CLEAR: ws_wrbtr.

ELSE.
CHECK ws_wrbtr > gw_minimo.
lw_zsw
= 1.
<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO pt_detalle.
CLEAR: ws_wrbtr.
ENDIF.
ENDFORM.
" GENERAR_LOTE_3001
*&---------------------------------------------------------------------*
*&
Form GENERA_LOTE_30203
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM genera_lote_3_0203 TABLES pt_detalle STRUCTURE t_detalle
USING gt_detalle TYPE tab_detalle
CHANGING p_zsw p_zswp.
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
ls_detalle TYPE ty_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat.
FIELD-SYMBOLS : <fs_det>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_deta>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE opc = gc_x.
IF w_kunnr IS INITIAL.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
ELSE.
IF w_kunnr <> <fs_det>-kunnr.
MESSAGE 'Documentos seleccionados son de clientes diferentes' TYPE 'E'.
w_sw = gc_x.
EXIT.
ELSE.
IF ws_waers <> <fs_det>-waers.
MESSAGE 'Documentos seleccionados con monedas diferentes' TYPE 'E'.
w_sw = gc_x.

EXIT.
ELSE.
IF ws_bankk <> <fs_det>-bankl.
MESSAGE 'Documentos seleccionados con bancos diferentes' TYPE 'E'.
w_sw = gc_x.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CHECK w_sw IS INITIAL.
****************
*Actualiza AUBEL
****************
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE opc = gc_x.
CLEAR lw_vbeln.
lw_vbeln = <fs_det>-vbeln.
READ TABLE gt_vbrp_aut ASSIGNING <fs_vbrp_automat> WITH KEY vbeln = lw_vbeln
BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_det>-aubel = <fs_vbrp_automat>-aubel.
<fs_det>-aupos = <fs_vbrp_automat>-aupos.
ENDIF.
ENDLOOP.
******************
*Agrupamos x AUBEL
******************
lt_detalle[] = pt_detalle[].
SORT lt_detalle BY opc.
DELETE lt_detalle WHERE opc <> gc_x.
SORT lt_detalle BY aubel.
DELETE ADJACENT DUPLICATES FROM lt_detalle COMPARING aubel.
LOOP AT lt_detalle ASSIGNING <fs_deta>.
CLEAR:ws_wrbtr,ls_detalle.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
ENDLOOP.
IF <fs_det>-waers = gc_pen.
IF ws_wrbtr > ( lw_ukurs * gw_minimo ).
ls_detalle = <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = 1.
ws_cant = ws_cant + ls_detalle-ws_cant.
ws_dias = ls_detalle-zbd1t.
APPEND ls_detalle TO gt_detalle.
p_zsw = 1.
p_zswp = 1.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ELSE.
IF ( ws_wrbtr > gw_minimo ).
ls_detalle = <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = 1.
ws_cant = ws_cant + ls_detalle-ws_cant.

ws_dias = ls_detalle-zbd1t.
APPEND ls_detalle TO gt_detalle.
p_zsw = 1.
p_zswp = 1.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
" GENERA_LOTE_30203
*&---------------------------------------------------------------------*
*&
Form GENERA_LOTE_30203
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM genera_lote_3004p TABLES pt_detalle STRUCTURE t_detalle
USING gt_detalle TYPE tab_detalle
CHANGING p_zsw p_zswp .
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
ls_detalle TYPE ty_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_cont TYPE i,
lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_zsw
TYPE c,
lw_times TYPE i,
lw_zsw_div TYPE c,
lw_flag
TYPE c,
lw_zw_div TYPE c.
FIELD-SYMBOLS : <fs_det>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_deta>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
IF ws_waers EQ 'PEN'.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc EQ 0.
lw_minimg = p_minim * lw_ukurs.
lw_maximg = p_maxim * lw_ukurs.

ENDIF.
ELSE.
lw_minimg = p_minim.
lw_maximg = p_maxim.
ENDIF.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE opc = gc_x.
IF w_kunnr IS INITIAL.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
ELSE.
IF w_kunnr <> <fs_det>-kunnr.
MESSAGE 'Documentos seleccionados son de clientes diferentes' TYPE 'E'.
w_sw = gc_x.
EXIT.
ELSE.
IF ws_waers <> <fs_det>-waers.
MESSAGE 'Documentos seleccionados con monedas diferentes' TYPE 'E'.
w_sw = gc_x.
EXIT.
ELSE.
IF ws_bankk <> <fs_det>-bankl.
MESSAGE 'Documentos seleccionados con bancos diferentes' TYPE 'E'.
w_sw = gc_x.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CHECK w_sw IS INITIAL.
****************
*Actualiza AUBEL
****************
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE opc = gc_x.
CLEAR lw_vbeln.
lw_vbeln = <fs_det>-vbeln.
READ TABLE gt_vbrp_aut ASSIGNING <fs_vbrp_automat> WITH KEY vbeln = lw_vbeln
BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_det>-aubel = <fs_vbrp_automat>-aubel.
<fs_det>-aupos = <fs_vbrp_automat>-aupos.
ENDIF.
ENDLOOP.
******************
*Agrupamos x AUBEL
******************
lt_detalle[] = pt_detalle[].
SORT lt_detalle BY opc.
DELETE lt_detalle WHERE opc <> gc_x.
SORT lt_detalle BY aubel.
DELETE ADJACENT DUPLICATES FROM lt_detalle COMPARING aubel.
LOOP AT lt_detalle ASSIGNING <fs_deta>.
CLEAR:ws_wrbtr,
ls_detalle,
lw_cont,
lw_times.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.

ADD 1 TO lw_cont.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
ENDLOOP.
IF <fs_det>-waers = gc_pen.
IF ( ws_wrbtr > ( lw_ukurs * gw_minimo ) ) AND ( lw_cont >= 2 ).
"Calcular la cantidad de letras por factura
IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
ls_detalle = <fs_det>.
lw_zsw = 1.
ls_detalle-ws_cant = 1.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.
lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact
USING lw_minimg lw_maximg
lw_times lw_flag lw_zw_div
CHANGING <fs_det>.
IF lw_zw_div IS NOT INITIAL.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = lw_times.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSE.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSEIF ws_wrbtr < lw_maximg.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ELSE.
IF ( ws_wrbtr > gw_minimo ) AND ( lw_cont >= 2 ).
IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
ls_detalle = <fs_det>.
lw_zsw = 1.
ls_detalle-ws_cant = 1.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.

lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact

USING lw_minimg lw_maximg


lw_times lw_flag lw_zw_div
CHANGING <fs_det>.

IF lw_zw_div IS NOT INITIAL.


ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = lw_times.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSE.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSEIF ws_wrbtr < lw_maximg.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ENDIF.
ws_cant = ws_cant + ls_detalle-ws_cant.
ws_dias = ls_detalle-zbd1t.
ENDLOOP.
ENDFORM.
" GENERA_LOTE_3004P
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTES_AUTOMATICO_2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_T_DETALLE text
*----------------------------------------------------------------------*
FORM generar_lotes_automatico_2 TABLES pt_detalle STRUCTURE t_detalle.
FIELD-SYMBOLS:<fs_det> LIKE LINE OF t_detalle.
DATA: lw_sw TYPE c LENGTH 1.
CHECK ( pt_detalle[] IS NOT INITIAL ) AND ( r_opc9 = gc_x ).
LOOP AT pt_detalle ASSIGNING <fs_det>.
<fs_det>-opc = gc_x.
ENDLOOP.
PERFORM valida_cantidades TABLES pt_detalle CHANGING lw_sw.
IF lw_sw IS INITIAL.
FREE gt_log_al.
PERFORM generar_lotes_3 TABLES pt_detalle.
ELSE.
w_cherror = gc_x.
MESSAGE text-x04 TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
ENDFORM.
" GENERAR_LOTES_AUTOMATICO_2
*&---------------------------------------------------------------------*

*&
Form ACTUALIZA_UBEL
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_PT_DETALLE text
*----------------------------------------------------------------------*
FORM actualiza_ubel TABLES ppt_detalle STRUCTURE t_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln.
FIELD-SYMBOLS:<fs_det>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS:<fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
LOOP AT ppt_detalle ASSIGNING <fs_det> WHERE opc = gc_x.
CLEAR lw_vbeln.
lw_vbeln = <fs_det>-vbeln.
READ TABLE gt_vbrp_aut ASSIGNING <fs_vbrp_automat> WITH KEY vbeln = lw_vbeln
BINARY SEARCH.
IF sy-subrc EQ 0.
<fs_det>-aubel = <fs_vbrp_automat>-aubel.
<fs_det>-aupos = <fs_vbrp_automat>-aupos.
ENDIF.
ENDLOOP.
ENDFORM.
" ACTUALIZA_UBEL
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTES_3
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_T_DETALLE text
*----------------------------------------------------------------------*
FORM generar_lotes_3 TABLES pt_detalle STRUCTURE t_detalle.
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
lt_detppal TYPE STANDARD TABLE OF ty_detalle,
lt_det_auxi TYPE STANDARD TABLE OF ty_detalle,
ls_detppal TYPE ty_detalle,
ls_det
TYPE ty_detalle,
ls_det_auxi LIKE LINE OF lt_det_auxi.
DATA: lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_times TYPE i,
lw_flag
TYPE c,
lw_zsw
TYPE c,
lw_zswp
TYPE c,
lw_zsw_aubel TYPE c,
lw_zw_div
TYPE c,
lw_zsw_div TYPE c.
FIELD-SYMBOLS: <fs_det>
LIKE LINE OF lt_det_auxi.
FIELD-SYMBOLS: <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
FIELD-SYMBOLS: <fs_detalle>
LIKE LINE OF lt_det_auxi.
DATA: ls_detalle LIKE LINE OF t_detalle.

READ TABLE pt_detalle WITH KEY opc = 'X'.


IF sy-subrc NE 0.
w_sw = 'X'.
MESSAGE 'Seleccione documento(s)' TYPE 'E'.
ENDIF.
REFRESH gt_detalle.
****************
*Actualiza AUBEL
****************
PERFORM actualiza_ubel TABLES pt_detalle.
IF ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_f ).
SORT pt_detalle BY aubel.
lt_detalle[] = pt_detalle[].
ENDIF.
********************
*L O G I C A
********************
ggt_det_aux[] = lt_det_auxi[] =
SORT lt_detppal BY kunnr waers
SORT pt_detalle BY kunnr waers
SORT lt_det_auxi BY kunnr waers
SORT ggt_det_aux BY kunnr waers

lt_detppal[] = pt_detalle[].
bankl.
bankl.
bankl.
bankl.

DELETE ADJACENT DUPLICATES FROM lt_detppal COMPARING kunnr waers bankl.


LOOP AT lt_detppal INTO ls_detppal.
CLEAR: ws_waers, ws_wrbtr, ws_cant, w_sw, w_kunnr, ws_bankk, w_kunnr, ws_amo
rt.
CLEAR: w_prctr, ws_fini, ws_rebzg, ws_rebzj,lw_zsw,lw_zswp,lw_zsw_aubel,lw_z
w_div.
CLEAR: lw_minimg,lw_maximg,lw_ukurs,lw_fecval,lw_times,lw_flag.
CLEAR: ggw_kunnr, ggw_waers,ggw_bankk.
REFRESH: gt_detalle.
ggw_kunnr = ls_detppal-kunnr.
ggw_waers = ls_detppal-waers.
ggw_bankk = ls_detppal-bankl.
LOOP AT lt_det_auxi ASSIGNING <fs_det> WHERE opc
kunnr
waers
bankl

=
=
=
=

gc_x
AND
ls_detppal-kunnr AND
ls_detppal-waers AND
ls_detppal-bankl.

CLEAR: lw_flag,lw_zw_div.
CLEAR: ls_detalle.
w_kunnr = <fs_det>-kunnr.
ws_waers = <fs_det>-waers.
ws_waer2 = ws_waers.
ws_bankk = <fs_det>-bankl.
PERFORM doc_bloqueado USING <fs_det>-belnr <fs_det>-gjahr w_sw.
IF w_sw = 'X'.
CONCATENATE 'Documento' <fs_det>-belnr 'bloqueado por usuario' INTO w_me
ss SEPARATED BY space.
MESSAGE w_mess TYPE 'E'.
EXIT.
ENDIF.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
IF w_prctr IS INITIAL AND <fs_det>-prctr IS NOT INITIAL.
w_prctr = <fs_det>-prctr.
ENDIF.

IF <fs_det>-bldat > ws_fini.


ws_fini = <fs_det>-bldat.
ws_rebzg = <fs_det>-belnr.
ws_rebzj = <fs_det>-gjahr.
ws_rebzz = <fs_det>-buzei.
ENDIF.
IF ( p_vkbur EQ '3004' ) AND ( p_group-low EQ gc_group_f ).
"Calcular el tipo de cambio para el calculo de los maximos y minimos imp
ortes comprendidos
IF ws_waers EQ 'PEN'.
CLEAR: lw_fecval, lw_ukurs, lw_minimg, lw_maximg.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc
lw_minimg
lw_maximg
ENDIF.
ELSE.
lw_minimg =
lw_maximg =
ENDIF.

EQ 0.
= p_minim * lw_ukurs.
= p_maxim * lw_ukurs.
p_minim.
p_maxim.

"Calcular la cantidad de letras por factura


IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
lw_zsw = 1.
<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
LOOP AT lt_det_auxi ASSIGNING <fs_detalle> WHERE opc = gc_t.
<fs_detalle>-opc = gc_x. "Si cumple los marca definitivamente
ENDLOOP.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.
lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact
USING lw_minimg lw_maximg
lw_times lw_flag lw_zw_div
CHANGING <fs_det>.
IF lw_zw_div IS NOT INITIAL.
<fs_det>-ws_cant = lw_times.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
LOOP AT lt_det_auxi ASSIGNING <fs_detalle> WHERE opc = gc_t.
<fs_detalle>-opc = gc_x. "Si cumple los marca definitivamente
ENDLOOP.
CLEAR: ws_wrbtr.

ELSE.
<fs_det>-opc = gc_t.
"Marca el regitro tempoalmente
*
LOOP AT lt_det_auxi ASSIGNING <fs_detalle> WHERE opc = gc_t.
*
<fs_detalle>-opc = space. "Si no cumple, los reg. temporeles los
desmarca para que no sean incluidos en el batc
*
ENDLOOP.
ENDIF.
*
CLEAR: ws_wrbtr.
ELSEIF ws_wrbtr < lw_maximg.
<fs_det>-opc = gc_t.
"Marca el regitro tempoalmente
ENDIF.
ELSEIF p_vkbur EQ gc_3001.
PERFORM generar_lote_3001_aut USING gt_detalle CHANGING <fs_det> lw_zsw.
ENDIF.
ws_cant = ws_cant + <fs_det>-ws_cant.
ws_dias = <fs_det>-zbd1t.
ggt_det_aux[] = lt_det_auxi[].
pt_detalle[] = lt_det_auxi[].
ENDLOOP.
**************************************
*Genera lote 3002/3003/3004 GROUP = P
**************************************
IF ( ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3003 ) ).
PERFORM genera_lote_3_0203_aut TABLES lt_det_auxi
USING gt_detalle ls_detppal
CHANGING lw_zsw lw_zswp.
ENDIF.
IF ( ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_p ) ) .
PERFORM genera_lote_3004p_aut TABLES lt_det_auxi
USING gt_detalle ls_detppal
CHANGING lw_zsw lw_zswp .
pt_detalle[] = ggt_det_aux[].
ENDIF.
**************************
CALL FUNCTION 'DEQUEUE_ALL'.
CLEAR:ws_wrbtr.
LOOP AT gt_detalle ASSIGNING <fs_det> WHERE kunnr = ls_detppal-kunnr AND
waers = ls_detppal-waers AND
bankl = ls_detppal-bankl.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
ENDLOOP.
***********************************************************************
IF w_sw IS INITIAL.
CLEAR: t_letras, ws_acepta, ws_name1, ws_aval1, ws_name2, ws_aval2, ws_n
ame3, ws_desreg.
REFRESH: t_letras.
w_swg
= ' '.
w_gjahr = sy-datum(4).
ws_xint = ' '.
ws_xrec = 'X'.
ws_xamo = ' '.
ws_fint = ws_fini. "sy-datum.
ws_lu
= 'X'.
ws_ma
= 'X'.
ws_mi
= 'X'.
ws_ju
= 'X'.
ws_vi
= 'X'.
ws_sa
= 'X'.

ws_do
= 'X'.
ws_fe
= 'X'.
ws_inter = 0.
ws_recau = 0.
ws_total = 0.
ws_sumimp = 0.
ws_acepta = w_kunnr.
ws_total_cj = ws_wrbtr.
CONDENSE ws_cant.
SELECT SINGLE *
FROM zlpet_fi_0023
WHERE bukrs = p_bukrs.
IF sy-subrc = 0.
ws_blart = zlpet_fi_0023-blart.
ws_fdev = ws_fini + zlpet_fi_0023-diasdev.
IF ws_cutil EQ space.
ws_cutil = zlpet_fi_0023-wevwv.
ENDIF.
ws_tasa = zlpet_fi_0023-tasaint.
ws_dtasa = zlpet_fi_0023-diasint."MTC141013 ZFIPET_021_10
ENDIF.
SELECT SINGLE *
FROM zlpet_fi_0030
WHERE kunnr = ws_acepta.
IF sy-subrc = 0.
ws_aval1 = zlpet_fi_0030-aval1.
ws_aval2 = zlpet_fi_0030-aval2.
ws_tasa = zlpet_fi_0030-tasaint.
ws_dtasa = zlpet_fi_0030-diasint.
ENDIF.
w_vez = 1.
w_ucomm = space.
LOOP AT lt_det_auxi INTO ls_det_auxi WHERE opc
kunnr
waers
bankl

=
=
=
=

'X'
AND
ls_detppal-kunnr AND
ls_detppal-waers AND
ls_detppal-bankl.

ws_regio = ls_det_auxi-regio.
ws_kkber = ls_det_auxi-kkber.
EXIT.
ENDLOOP.
IF gt_detalle[] IS NOT INITIAL.
PERFORM f_proponer_valores_letra.
PERFORM f_grabar_lotes_letra.
ENDIF.
CHECK w_swe IS NOT INITIAL.
PERFORM rollback_lotes.
ENDIF.
ENDLOOP.
**************
*Validaciones
**************
IF ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_f ) AND ( gt_detalle[]
IS INITIAL ).
CLEAR gs_log_al.
gs_log_al-bukrs = p_bukrs.

gs_log_al-type
= 'I'.
gs_log_al-message = text-e09. "Los documentos no cumplen con los rangos
APPEND gs_log_al TO gt_log_al.
ENDIF.
IF ( ( p_vkbur EQ gc_3001 ) OR ( p_vkbur EQ gc_3002 ) OR ( p_vkbur EQ gc_3003
) OR ( ( p_vkbur EQ gc_3004 ) AND ( p_group-low EQ gc_group_p ) ) ) AND ( gt_det
alle[] IS INITIAL ).
CLEAR gs_log_al.
gs_log_al-bukrs = p_bukrs.
gs_log_al-type
= 'I'.
gs_log_al-message = text-e10. "Los docuementos no conetinen pedidos relacion
ados
APPEND gs_log_al TO gt_log_al.
ENDIF.
************************
*Envio EMAIL/Muestra Log
************************
PERFORM aceptacion_automatica. "Envio de EMAIL
PERFORM muestra_alv_log.
ENDFORM.
" GENERAR_LOTES_3
*&---------------------------------------------------------------------*
*&
Form GENERAR_LOTE_3001_AUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_GT_DETALLE text
*
<--P_<FS_DET> text
*
<--P_LW_ZSW text
*----------------------------------------------------------------------*
FORM generar_lote_3001_aut USING pt_detalle TYPE tab_detalle
CHANGING p_det LIKE LINE OF t_detalle lw_zsw TYPE c.
DATA: lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_times TYPE i,
lw_flag
TYPE c.
FIELD-SYMBOLS:<fs_det> LIKE LINE OF t_detalle.
DATA: ls_detalle LIKE LINE OF t_detalle.
CLEAR: ws_wrbtr, w_sw, ws_amort.
CLEAR: w_prctr,
ws_fini,
ws_rebzg,
ws_rebzj,
lw_flag.
CLEAR: ls_detalle.
ASSIGN p_det TO <fs_det>.
PERFORM doc_bloqueado USING <fs_det>-belnr <fs_det>-gjahr w_sw.
IF w_sw = gc_x.
CONCATENATE 'Documento' <fs_det>-belnr 'bloqueado por usuario' INTO w_mess S
EPARATED BY space.

MESSAGE w_mess TYPE 'E'.


EXIT.
ENDIF.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
IF w_prctr IS INITIAL AND <fs_det>-prctr IS NOT INITIAL.
w_prctr = <fs_det>-prctr.
ENDIF.
IF <fs_det>-bldat > ws_fini.
ws_fini = <fs_det>-bldat.
ws_rebzg = <fs_det>-belnr.
ws_rebzj = <fs_det>-gjahr.
ws_rebzz = <fs_det>-buzei.
ENDIF.
"Calcular el tipo de cambio para el calculo de los maximos y minimos importes
comprendidos
IF ws_waers EQ gc_pen.
CLEAR: lw_fecval, lw_ukurs, lw_minimg, lw_maximg.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc EQ 0.
lw_minimg = p_minim * lw_ukurs.
lw_maximg = p_maxim * lw_ukurs.
ENDIF.
ELSE.
lw_minimg = p_minim.
lw_maximg = p_maxim.
ENDIF.
********************************************
*Valida si es menor al minimo
********************************************
IF <fs_det>-waers EQ gc_pen.
CHECK ws_wrbtr > ( lw_ukurs * gw_minimo ).
lw_zsw
= 1.
<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO pt_detalle.
CLEAR: ws_wrbtr.
ELSE.
CHECK ws_wrbtr > gw_minimo.
lw_zsw
= 1.
<fs_det>-ws_cant = 1.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO pt_detalle.
CLEAR: ws_wrbtr.

ENDIF.
ENDFORM.
" GENERAR_LOTE_3001_AUT
*&---------------------------------------------------------------------*
*&
Form GENERA_LOTE_3_0203_AUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_PT_DETALLE text
*
-->P_GT_DETALLE text
*
<--P_LW_ZSW text
*----------------------------------------------------------------------*
FORM genera_lote_3_0203_aut TABLES pt_detalle STRUCTURE t_detalle
USING gt_detalle TYPE tab_detalle
ps_detppal LIKE LINE OF t_detalle
CHANGING p_zsw p_zswp.
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
ls_detalle TYPE ty_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat.
FIELD-SYMBOLS : <fs_det>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_deta>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
******************
*Agrupamos x AUBEL
******************
CLEAR:ws_wrbtr,p_zsw,ws_cant,ws_dias.
lt_detalle[] = pt_detalle[].
SORT lt_detalle BY opc.
DELETE lt_detalle WHERE opc <> gc_x.
SORT lt_detalle BY aubel kunnr waers bankl.
DELETE ADJACENT DUPLICATES FROM lt_detalle COMPARING
LOOP AT lt_detalle ASSIGNING <fs_deta> WHERE kunnr =
waers =
bankl =
CLEAR:ws_wrbtr,ls_detalle.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel
kunnr
waers
bankl

aubel kunnr waers bankl.


ps_detppal-kunnr AND
ps_detppal-waers AND
ps_detppal-bankl.

=
=
=
=

<fs_deta>-aubel AND
ps_detppal-kunnr AND
ps_detppal-waers AND
ps_detppal-bankl.

ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.


ENDLOOP.
IF <fs_det>-waers = gc_pen.
IF ( ws_wrbtr > ( lw_ukurs * gw_minimo ) ).
ls_detalle = <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = 1.
ws_cant = ws_cant + ls_detalle-ws_cant.
ws_dias = ls_detalle-zbd1t.
APPEND ls_detalle TO gt_detalle.
p_zsw = 1.
p_zswp = 1.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l
kunnr = ps_detppal-kun
nr
waers = ps_detppal-wae
rs
bankl = ps_detppal-ban
kl.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ELSE.
IF ( ws_wrbtr > gw_minimo ).
ls_detalle = <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = 1.
ws_cant = ws_cant + ls_detalle-ws_cant.
ws_dias = ls_detalle-zbd1t.
APPEND ls_detalle TO gt_detalle.
p_zsw = 1.
p_zswp = 1.
ELSE.
READ TABLE pt_detalle ASSIGNING <fs_det> WITH KEY aubel = <fs_deta>-aube
l
kunnr = ps_detppal-kun
nr
waers = ps_detppal-wae
rs
bankl = ps_detppal-ban
kl.
CHECK sy-subrc = 0.
<fs_det>-opc = space.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
" GENERA_LOTE_3_0203_AUT
*&---------------------------------------------------------------------*
*&
Form GENERA_LOTE_3004P_AUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_PT_DETALLE text
*
-->P_GT_DETALLE text
*
<--P_LW_ZSW text
*
<--P_LW_ZSWP text
*----------------------------------------------------------------------*

FORM genera_lote_3004p_aut TABLES pt_detalle STRUCTURE t_detalle


"Tabla con
los registros
USING gt_detalle TYPE tab_detalle
"Tabla que
se llena segun se cumplan las condiciones
ps_detppal LIKE LINE OF t_detalle "Cambia se
gun Clientes
CHANGING p_zsw p_zswp .
DATA: lt_detalle TYPE STANDARD TABLE OF ty_detalle,
ls_detalle TYPE ty_detalle.
DATA: lw_vbeln TYPE vbrp-vbeln,
lw_ukurs TYPE tcurr-ukurs,
lw_fecval TYPE bkpf-budat,
lw_cont TYPE i,
lw_minimg TYPE bsis-wrbtr,
lw_maximg TYPE bsis-wrbtr,
lw_zsw
TYPE c,
lw_times TYPE i,
lw_zsw_div TYPE c,
lw_flag
TYPE c,
lw_zw_div TYPE c.
FIELD-SYMBOLS : <fs_det>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_deta>
LIKE LINE OF t_detalle.
FIELD-SYMBOLS : <fs_vbrp_automat> LIKE LINE OF gt_vbrp_aut.
IF ws_waers EQ 'PEN'.
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
date
= sy-datum
foreign_currency = 'USD'
local_currency = 'PEN'
type_of_rate
= 'M'
IMPORTING
exchange_rate
= lw_ukurs
valid_from_date = lw_fecval
EXCEPTIONS
OTHERS
= 1.
IF sy-subrc EQ 0.
lw_minimg = p_minim * lw_ukurs.
lw_maximg = p_maxim * lw_ukurs.
ENDIF.
ELSE.
lw_minimg = p_minim.
lw_maximg = p_maxim.
ENDIF.
******************
*Agrupamos x AUBEL
******************
lt_detalle[] = pt_detalle[].
SORT lt_detalle BY aubel kunnr waers bankl.
DELETE ADJACENT DUPLICATES FROM lt_detalle COMPARING aubel kunnr waers bankl.
LOOP AT lt_detalle ASSIGNING <fs_deta> WHERE kunnr = ggw_kunnr AND
waers = ggw_waers AND
bankl = ggw_bankk.
CLEAR:ws_wrbtr,
ls_detalle,
lw_cont,

lw_times,
lw_flag,
lw_zw_div.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel AND
kunnr = ggw_kunnr
AND
waers = ggw_waers
AND
bankl = ggw_bankk.
ADD 1 TO lw_cont.
ws_wrbtr = ws_wrbtr + <fs_det>-wrbtr.
ENDLOOP.
IF <fs_det>-waers = gc_pen.
"SOLES
IF ( ws_wrbtr > ( lw_ukurs * gw_minimo ) ) AND ( lw_cont >= 2 ).
"Calcular la cantidad de letras por factura
IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
ls_detalle = <fs_det>.
lw_zsw = 1.
ls_detalle-ws_cant = 1.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.
lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact
USING lw_minimg lw_maximg
lw_times lw_flag lw_zw_div
CHANGING <fs_det>.
IF lw_zw_div IS NOT INITIAL.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = lw_times.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSE.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel
AND
kunnr = ggw_kunnr
AND
waers = ggw_waers
AND
bankl = ggw_bankk.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSEIF ws_wrbtr < lw_maximg.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel AN
D
kunnr = ggw_kunnr

AN

waers = ggw_waers

AN

D
D
bankl = ggw_bankk.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSE.

LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel


kunnr
waers
bankl
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSE.

=
=
=
=

<fs_deta>-aubel AND
ggw_kunnr
AND
ggw_waers
AND
ggw_bankk.

"DOLAR

ES
IF ( ws_wrbtr > gw_minimo ) AND ( lw_cont >= 2 ).
IF ws_wrbtr BETWEEN lw_minimg AND lw_maximg.
ls_detalle = <fs_det>.
lw_zsw = 1.
ls_detalle-ws_cant = 1.
ls_detalle-wrbtr = ws_wrbtr.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSEIF ws_wrbtr > lw_maximg.
lw_zsw
= 1.
lw_times = 2.
lw_zsw_div = 1.
PERFORM f_calculo_cant_letxfact
USING lw_minimg lw_maximg
lw_times lw_flag lw_zw_div
CHANGING <fs_det>.
IF lw_zw_div IS NOT INITIAL.
ls_detalle
= <fs_det>.
ls_detalle-wrbtr = ws_wrbtr.
ls_detalle-ws_cant = lw_times.
APPEND ls_detalle TO gt_detalle.
CLEAR: ws_wrbtr.
p_zsw = 1.
p_zswp = 1.
ELSE.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel
AND
kunnr = ggw_kunnr
AND
waers = ggw_waers
AND
bankl = ggw_bankk.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSEIF ws_wrbtr < lw_maximg.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel = <fs_deta>-aubel AN
D
kunnr = ggw_kunnr

AN

waers = ggw_waers

AN

D
D
bankl
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ELSE.
LOOP AT pt_detalle ASSIGNING <fs_det> WHERE aubel =
kunnr =
waers =

= ggw_bankk.

<fs_deta>-aubel AND
ggw_kunnr
AND
ggw_waers
AND

bankl = ggw_bankk.
<fs_det>-opc = space.
ENDLOOP.
ENDIF.
ENDIF.
ws_cant = ws_cant + ls_detalle-ws_cant.
ws_dias = ls_detalle-zbd1t.
ENDLOOP.
ggt_det_aux[] = pt_detalle[].
ENDFORM.
" GENERA_LOTE_3004P_AUT
*&---------------------------------------------------------------------*
*&
Form ROLLBACK_LOTES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM rollback_lotes .
DATA: ls_det_aux LIKE LINE OF ggt_det_aux.
*Actualizar el campo Lote en blanco
UPDATE zlpet_fi_0024
SET nlote = space
WHERE bukrs = w_bukrs
AND gjahr = w_gjahr.
COMMIT WORK.
*Eliminar Lote
DELETE FROM zlpet_fi_0022 WHERE nlote EQ w_nlotec.
COMMIT WORK.
*Eliminar Documentos originales
LOOP AT ggt_det_aux INTO ls_det_aux WHERE opc
kunnr
waers
bankl
DELETE FROM zlpet_fi_0027 WHERE bukrs
nlote
belnr
gjahr
buzei
COMMIT WORK.
ENDLOOP.

EQ
EQ
EQ
EQ
EQ

*Actualiza aceptacion de Lote


UPDATE zlpet_fi_0022
SET iacept = space
WHERE bukrs EQ p_bukrs
AND nlote EQ w_nlote.
COMMIT WORK.
ENDFORM.
" ROLLBACK_LOTES

EQ
EQ
EQ
EQ

gc_x
AND
ggw_kunnr AND
ggw_waers AND
ggw_bankk.

w_bukrs AND
w_nlotec AND
ls_det_aux-belnr AND
ls_det_aux-gjahr AND
ls_det_aux-buzei.