Make your own free website on Tripod.com

Table of Contents:



REPORT zfcheque NO STANDARD PAGE HEADING LINE-SIZE 122.
*@----------------------------------------------------------------------
*@ Program id    : ZFCHEQUE
*@ Program Desc  : Cheque printing program for CAN office
*@
*@ Transaction Code - none
*@ Input files   : ID - none
*@ Output files  : ID - none
*@
*@ Tables Updated: Table - none
*@
*@ Author      : Fiona Lau
*@ Date        : 2001/11/15
*@ Requested by:
*@
*@----------------------------------------------------------------------
*@ Algorithm:
*@----------------------------------------------------------------------
**
** Parameters: 1. run_date
**             2. identification
**             3. currency (CAD/USD)
**             4. print without signature - print all cheques without
**                                          signature
**             5. view report option - display report only
**             6. output_file - for new print, save cheque info. to
**             7. input_file - for reprint, retrieve cheque info. from
**             8. reprint option: specify one cheque,
**                                specify cheque range (exclusive)
**                                all cheques
**
** For new print, cheque info. is retrieved form table reguh
** & regup base on input parameters(run_date, identification and
** currency) and is saved to the output_file. Then all cheque info. is
** retrieved again from file and sent to the printer.
** For reprint, cheque info. is retrieved form the input_file and is
** checked against the input paramters(run_date, identification and
** currency). If the file is valid, cheques are sent to the printer
** depends on the reprint option(specify one cheque/specify cheque
** range/all cheques).
**---------------------------------------------------------------------
**
** LABK932282    2002/04/04    FLAU
** Use payee's name & address instead of vendor's name & address
** for pay to.
**---------------------------------------------------------------------
** April 24, 2003 LABK934811  CFieulle
**                Allow to print cheques of pmt method 'S'. (just add)
**---------------------------------------------------------------------

INCLUDE zsmaster.

TABLES: reguh, regup.
TABLES: zfcheque.

* c_limit : limit for auto-signature
CONSTANTS: c_limit LIKE reguh-rwbtr VALUE '25000'.
* c_items : no. of item per page
CONSTANTS: c_items  TYPE p VALUE '80'.


TYPES: BEGIN OF t_reguh,
  laufd  LIKE reguh-laufd,    "run date
  laufi  LIKE reguh-laufi,    "identification
  vblnr  LIKE reguh-vblnr,    "document #
  rwbtr  LIKE reguh-rwbtr,    "amount paid in payment currency
  lifnr  LIKE reguh-lifnr,    "account # of vendor
*LABK932282->
*  name1  LIKE reguh-name1,
*  name2  LIKE reguh-name2,
*  stras  LIKE reguh-stras,    "house # & street
*  ort01  LIKE reguh-ort01,    "city
  znme1  like reguh-znme1,    "payee name1
  znme2  like reguh-znme2,    "payee name2
  zstra  like reguh-zstra,    "payee house # & street
  zort1  like reguh-zort1,    "payee city
*LABK932282<-
  zregi  LIKE reguh-zregi,    "regional code of the payee
  zpstl  LIKE reguh-zpstl,    "postal code of the payee
  zland  LIKE reguh-zland,    "country key
  waers  LIKE reguh-waers,    "currency key
  landx  LIKE t005t-landx,    "country name

  hbkid  LIKE reguh-hbkid,    "house bank key ID
  ubnks  LIKE reguh-ubnks,    "bank country key
  hktid  LIKE reguh-hktid,    "ID for account detial
  bankl  LIKE t012-bankl,     "bank #
  bankn  LIKE t012k-bankn,    "bank account #

  count(6) TYPE n,            "# of items
  pages    TYPE p,            "# of pages printed
END OF t_reguh.

TYPES: BEGIN OF t_regup,
  laufd  LIKE regup-laufd,    "run date
  laufi  LIKE regup-laufi,    "identification
  vblnr  LIKE regup-vblnr,    "doc. # (cheque #)
  lifnr  LIKE regup-lifnr,    "vendor #
  bldat  LIKE regup-bldat,    "document date
  xblnr  LIKE regup-xblnr,    "reference doc. #
  wrbtr  LIKE regup-wrbtr,    "$ in doc currency
  shkzg  LIKE regup-shkzg,    "debit/credit indicator
  bukrs  LIKE regup-bukrs,    "company code
  gjahr  LIKE regup-gjahr,    "fiscal year
  item   LIKE regup-buzei,    "item #
  belnr  LIKE regup-belnr,    "doc. #
END OF t_regup.


TYPES: BEGIN OF t_cheque,
  laufd  LIKE reguh-laufd,    "run date
  laufi  LIKE reguh-laufi,    "identification
  vblnr  LIKE reguh-vblnr,    "document #
  rwbtr  LIKE reguh-rwbtr,    "amount paid in payment currency
  lifnr  LIKE reguh-lifnr,    "account # of vendor
*LABK932282->
*  name1  LIKE reguh-name1,
*  name2  LIKE reguh-name2,
*  stras  LIKE reguh-stras,    "house # & street
*  ort01  LIKE reguh-ort01,    "city
  znme1  like reguh-znme1,    "payee name1
  znme2  like reguh-znme2,    "payee name2
  zstra  like reguh-zstra,    "payee house # & street
  zort1  like reguh-zort1,    "payee city
*LABK932282<-
  zregi  LIKE reguh-zregi,    "regional code of the payee
  zpstl  LIKE reguh-zpstl,    "postal code of the payee
  zland  LIKE reguh-zland,    "country key
  waers  LIKE reguh-waers,    "currency key
  landx  LIKE t005t-landx,    "country name

  bankl  LIKE t012-bankl,     "bank #
  bankn  LIKE t012k-bankn,    "bank account #

  count(6) TYPE n,            "# of items
  pages    TYPE p,            "# of pages printed

  bldat  LIKE regup-bldat,    "document date
  xblnr  LIKE regup-xblnr,    "reference doc. #
  wrbtr  LIKE regup-wrbtr,    "$ in doc currency
  shkzg  LIKE regup-shkzg,    "debit/credit indicator
  bukrs  LIKE regup-bukrs,    "company code
  gjahr  LIKE regup-gjahr,    "fiscal year
  item   LIKE regup-buzei,    "item #
END OF t_cheque.

DATA: i_reguh TYPE t_reguh OCCURS 100 WITH HEADER LINE.
DATA: i_regup TYPE t_regup OCCURS 100 WITH HEADER LINE.

DATA: i_cheque TYPE t_cheque OCCURS 100 WITH HEADER LINE.

DATA: msg(100).
DATA: datafile TYPE mac_t_filename.


DATA: BEGIN OF i_currency_text OCCURS 5,
        waers LIKE tcurt-waers,
        ltext LIKE tcurt-ltext,
      END OF i_currency_text.

DATA: l_linsz LIKE sy-linsz.
DATA: l_waers LIKE reguh-waers.

DATA: l_subrc       LIKE sy-subrc,
      l_rptonly     TYPE c VALUE ' ',
      l_newcheque   TYPE c VALUE 'X',
      l_newprt      TYPE c VALUE ' ',
      l_rundate     LIKE reguh-laufd,
      l_identif     LIKE reguh-laufi,
      l_currency    LIKE reguh-waers,
      l_cheque      LIKE reguh-vblnr,
      l_chequelast  LIKE reguh-vblnr,
      l_reprint     TYPE c,
      l_printed     TYPE p VALUE 0,
      l_items       TYPE p VALUE 0,
      l_totpages    TYPE p VALUE 0,
      l_form(20)    TYPE c,
      l_count       TYPE p VALUE 0,
      l_start       TYPE p VALUE 0,
      l_end         TYPE p VALUE 0,
      l_nosignature TYPE p VALUE 0,
      l_employee    TYPE p VALUE 0,
      l_other       TYPE p VALUE 0,
      l_type        TYPE c,
      l_index       TYPE p,
      l_totnosign      LIKE reguh-rwbtr,
      l_totemployee    LIKE reguh-rwbtr,
      l_totother       LIKE reguh-rwbtr.


RANGES: r_rzawe FOR reguh-rzawe.    "payment method

mac_set_range_row r_rzawe mac_c_append : 'C' ' '.
*{Start insert LABK934811
mac_set_range_row r_rzawe mac_c_append : 'S' ' '.
* End insert LABK934811}

PARAMETERS:  p_laufd LIKE reguh-laufd OBLIGATORY, "DEFAULT '20010614',
             p_laufi LIKE reguh-laufi OBLIGATORY. "DEFAULT 'wca1'.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(10) text-001.
SELECTION-SCREEN COMMENT 20(14) text-002 FOR FIELD p_cad.
PARAMETERS p_cad RADIOBUTTON GROUP opt1.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 20(14) text-003 FOR FIELD p_usd.
PARAMETERS p_usd RADIOBUTTON GROUP opt1.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN SKIP 1.

PARAMETERS p_nosign AS CHECKBOX.    "no signature
PARAMETERS p_report AS CHECKBOX.    "report only

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK 1 WITH FRAME TITLE text-004.
PARAMETERS p_ofile TYPE mac_t_filename DEFAULT 'c:\temp\zcheque.txt'.
SELECTION-SCREEN END OF BLOCK 1.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK 2 WITH FRAME TITLE text-005.

PARAMETERS p_ifile TYPE mac_t_filename.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN POSITION 1.
PARAMETERS p_one RADIOBUTTON GROUP opt3 DEFAULT 'X'.
SELECTION-SCREEN COMMENT 2(26) text-006 FOR FIELD p_one.
SELECTION-SCREEN POSITION 33.
PARAMETERS p_cheq1 LIKE reguh-vblnr.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN POSITION 1.
PARAMETERS p_range RADIOBUTTON GROUP opt3.
SELECTION-SCREEN COMMENT 2(26) text-009 FOR FIELD p_range.
SELECTION-SCREEN COMMENT 28(5) text-010.
SELECTION-SCREEN POSITION 33.
PARAMETERS p_cheq3 LIKE reguh-vblnr.
SELECTION-SCREEN COMMENT 62(6) text-011.
SELECTION-SCREEN POSITION 68.
PARAMETERS p_cheq4 LIKE reguh-vblnr.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN POSITION 1.
PARAMETERS p_all RADIOBUTTON GROUP opt3.
SELECTION-SCREEN COMMENT 3(26) text-008 FOR FIELD p_all.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK 2.

DEFINE lmac_check_file_access.
* check if file is accessible
  open dataset &1 for &2 in &3 mode message msg.
  &4 = sy-subrc.
  if sy-subrc <> 0.
    message s001(zz) with msg.
  endif.
  close dataset &1.
END-OF-DEFINITION.

DEFINE lmac_write_report_header.
  skip.
  uline.
  format color col_group intensified off.
  write: at /1(l_linsz) &1.
  format color col_heading  intensified off.
  write:  /1(10) &2,
          13(10) &3,
          25(10) &4,
          37(30) &5,
          69(15) &6 right-justified,
          86(30) &7,
          118(5) &8.
  format reset.
  uline.
END-OF-DEFINITION.

DEFINE lmac_write_report_items.
  write:  /1(10) &1,
          13(10) &2,
          25(10) &3,
          37(30) &4,
          69(15) &5,
          86(30) &6,
          118(5) &7.
END-OF-DEFINITION.

DEFINE lmac_write_report_subtotal.
  concatenate &1 &2 into l_txt.
  format color col_total intensified off.
  uline.
  write: at /1(l_linsz) l_txt.
  format reset.
END-OF-DEFINITION.


INITIALIZATION.
* Authority check
  AUTHORITY-CHECK OBJECT 'Z:CHEQUE'
           ID 'ACTVT' FIELD '35'.  "output
  IF sy-subrc NE 0.
* no authorization for printing cheques
    l_rptonly = 'X'.
    AUTHORITY-CHECK OBJECT 'Z:CHEQUE'
             ID 'ACTVT' FIELD '03'.  "display
    IF sy-subrc NE 0.
      MESSAGE s001(zz) WITH
      'You are not authorised to run program ZFCHEQUE.'.
      LEAVE PROGRAM.
    ENDIF.
  ENDIF.
* End Authority check


START-OF-SELECTION.

  IF p_ofile IS INITIAL AND p_ifile IS INITIAL.
    MESSAGE s001(zz) WITH 'Must have input file or output file.'.
    EXIT.
  ELSEIF NOT p_ofile IS INITIAL AND NOT p_ifile IS INITIAL.
   MESSAGE s001(zz) WITH 'Cannot have both input file and output file.'.
    EXIT.
  ENDIF.

  IF NOT p_ofile IS INITIAL.
* check if datafile is accessible
    lmac_check_file_access p_ofile output text l_subrc.
    IF l_subrc <> 0.
      STOP.
    ENDIF.

    datafile = p_ofile.

    l_newprt = 'X'.

  ELSE.
    IF p_one = 'X'.
      IF p_cheq1 IS INITIAL.
        MESSAGE s001(zz) WITH 'Please specify cheque no. to print'.
        EXIT.
      ENDIF.
      l_cheque = p_cheq1.
    ELSEIF p_range = 'X'.
      IF p_cheq3 IS INITIAL.
        MESSAGE s001(zz) WITH 'Please specify cheque no. range'.
        EXIT.
      ENDIF.
      l_cheque = p_cheq3.
      l_chequelast = p_cheq4.
    ENDIF.

* check if datafile is accessible
    lmac_check_file_access p_ifile input text l_subrc.
    IF l_subrc <> 0.
      STOP.
    ENDIF.

    datafile = p_ifile.
  ENDIF.


* no authorization for printing cheques
* set to view report only
  IF l_rptonly = 'X'.
    p_report = 'X'.
  ENDIF.


  IF p_cad = 'X'.
    l_waers = 'CAD'.
  ELSE.
    l_waers = 'USD'.
  ENDIF.

  l_linsz = sy-linsz.


* if this is new print, select cheques form database.
  IF l_newprt = 'X'.
    PERFORM select_cheques TABLES i_reguh i_regup
                           USING p_laufd p_laufi l_waers
                           CHANGING l_subrc.
    IF l_subrc <> 0.
      CASE l_subrc.
        WHEN '-1'.
          MESSAGE s001(zz) WITH 'Found no cheque.'.
        WHEN '-2'.
          MESSAGE s001(zz) WITH 'Error finding cheque item.'.
        WHEN '-3'.
          MESSAGE s001(zz) WITH 'Error finding country name.'.
        WHEN '-4'.
          MESSAGE s001(zz) WITH 'Error finding bank number.'.
        WHEN '-5'.
          MESSAGE s001(zz) WITH 'Error finding bank account number.'.
      ENDCASE.
      EXIT.
    ENDIF.


    PERFORM sort_cheques
            TABLES i_reguh i_regup i_cheque.

    mac_ws_download_raw_itab datafile i_cheque.
  ENDIF.
  REFRESH i_cheque.
  mac_ws_upload_tabbed_itab datafile i_cheque.
  l_rundate = i_cheque-laufd.
  l_identif = i_cheque-laufi.
  l_currency = i_cheque-waers.
  IF l_rundate <> p_laufd
     OR l_identif <> p_laufi
     OR l_currency <> l_waers.
    MESSAGE s001(zz) WITH
        'Input parameters does not match parameters in input file.'.
    EXIT.
  ENDIF.


  IF l_currency = 'USD'.
    l_form = 'ZFCHEQUE_US'.
  ELSEIF l_currency = 'CAD'.
    l_form = 'ZFCHEQUE_CA'.
  ENDIF.

* Get the text for the currency
  SELECT DISTINCT waers ltext INTO TABLE i_currency_text
    FROM tcurt
    WHERE spras = sy-langu
      AND waers = l_currency.
  READ TABLE i_currency_text WITH KEY waers = l_currency
       BINARY SEARCH.


  IF l_newprt = 'X' OR p_all = 'X'.
    l_start = 1.
    DESCRIBE TABLE i_cheque LINES l_end.
  ELSE.
    CLEAR l_start.
    CLEAR l_end.
    CLEAR l_count.
    LOOP AT i_cheque.

      l_count = l_count + 1.
      CHECK i_cheque-vblnr = l_cheque.

      IF l_start IS INITIAL.
        IF p_one = 'X'.
          l_start = l_count.
          l_end = l_count + i_cheque-count - 1.
        ELSEIF p_range = 'X'.
          l_start = l_count + i_cheque-count.
          IF l_chequelast IS INITIAL.    "print to the end
            DESCRIBE TABLE i_cheque LINES l_end.
          ELSE.
            l_cheque = l_chequelast.     "find last cheque to print
          ENDIF.
        ENDIF.
      ELSE.
        l_end = l_count - 1.
      ENDIF.
      IF NOT l_end IS INITIAL.
        EXIT.
      ENDIF.

    ENDLOOP.

    IF l_start IS INITIAL OR l_end IS INITIAL.
      MESSAGE s001(zz) WITH
          'Incorrect cheque no specified.'.
      EXIT.
    ENDIF.

  ENDIF.

*LABK931378->
  IF p_report <> 'X'.
    PERFORM open_form_cheque USING l_form.
  ENDIF.
*LABK931378<-

  l_newcheque = 'X'.
  LOOP AT i_cheque FROM l_start TO l_end.

    IF l_newcheque = 'X'.

      CLEAR zfcheque.
      MOVE-CORRESPONDING i_cheque TO zfcheque.
*    zfcheque-rwbtr = - zfcheque-rwbtr.
      WRITE zfcheque-rwbtr TO zfcheque-total_amount
            CURRENCY zfcheque-waers.
      CONDENSE zfcheque-total_amount NO-GAPS.
      CONCATENATE '$' zfcheque-total_amount '**'
                  INTO zfcheque-total_amount.

* Get the currency text
      PERFORM convert_amount_to_string
              USING zfcheque-rwbtr zfcheque-waers
              CHANGING zfcheque-line1 zfcheque-line2 zfcheque-line3.

* not report only
      IF p_report <> 'X'.

*LABK931378->
*        PERFORM open_form_cheque USING l_form.
        PERFORM start_form_cheque USING l_form.
*LABK931378<-

* signature for amount under $25,000 only
        IF zfcheque-rwbtr < c_limit AND p_nosign <>'X'.
          CALL FUNCTION 'WRITE_FORM'
               EXPORTING
                    element                  = 'SIGNATURE'
                    window                   = 'SIGN'
               EXCEPTIONS
                    element                  = 1
                    function                 = 2
                    type                     = 3
                    unopened                 = 4
                    unstarted                = 5
                    window                   = 6
                    bad_pageformat_for_print = 7
                    OTHERS                   = 8.
          IF sy-subrc <> 0.
            MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          ENDIF.
        ENDIF.
      ENDIF.    "end p_report<>'X'

      l_newcheque = ' '.
      l_items = 0.
    ENDIF.      "l_newcheque='X'

* write cheque item
    zfcheque-bldat = i_cheque-bldat.
    zfcheque-xblnr = i_cheque-xblnr.
    zfcheque-wrbtr = i_cheque-wrbtr.
    WRITE zfcheque-wrbtr TO zfcheque-item_amount
          CURRENCY zfcheque-waers.
    IF i_cheque-shkzg = 'S'.
      zfcheque-item_amount+1(1) = '-'.
    ENDIF.
    CONDENSE zfcheque-item_amount NO-GAPS.
    WRITE zfcheque-item_amount TO
          zfcheque-item_amount RIGHT-JUSTIFIED.

    IF p_report <> 'X'.
      mac_write_form 'ITEMS' 'MAIN'.
    ENDIF.   "if p_report <> 'X'
    l_items = l_items + 1.

* last cheque item
    IF i_cheque-count = l_items.
*LABK931378->
      IF p_report <> 'X'.
*        PERFORM close_form.
        PERFORM end_form.
      ENDIF.
*LABK931378<-
      l_newcheque = 'X'.

      COMPUTE  i_cheque-pages = ceil( l_items / c_items ).
      l_totpages = l_totpages + i_cheque-pages.

      PERFORM write_report USING i_cheque
                          CHANGING l_nosignature l_employee l_other.
      l_printed = l_printed + 1.

    ENDIF.

  ENDLOOP.

*LABK931378->
  IF p_report <> 'X'.
    PERFORM close_form.
  ENDIF.
*LABK931378<-

  PERFORM write_report_subtotal USING 'Subtotal for all the rest:'
                                       l_totother l_waers.
  PERFORM write_summary
          USING l_totnosign l_totemployee l_totother
                l_nosignature l_employee l_other l_printed.

TOP-OF-PAGE.
  mac_default_report_header mac_c_null.
  PERFORM wirte_parameters
          USING l_rundate l_identif l_currency.

*---------------------------------------------------------------------*
*       FORM open_form_cheque                                         *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM open_form_cheque USING p_form.

  DATA: options LIKE itcpo.

  CLEAR options.
*  options-tdpreview = 'X'.    "fff
  options-tdimmed = 'X'.
  options-tddest = 'ZCHQ'.
  options-tdcopies = '1'.
  options-tddelete = ' '.

  CALL FUNCTION 'OPEN_FORM'
       EXPORTING
            device  = 'PRINTER'
            dialog  = ' '
            form    = p_form
            options = options.

ENDFORM.                               "OPEN_FORM_CHEQUE


*---------------------------------------------------------------------*
*       FORM CLOSE_FORM                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM close_form.

  CALL FUNCTION 'CLOSE_FORM'.

ENDFORM.                               " CLOSE_FORM


*---------------------------------------------------------------------*
*       FORM start_form_cheque                                        *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  p_form                                                        *
*---------------------------------------------------------------------*
FORM start_form_cheque USING p_form.
  CALL FUNCTION 'START_FORM'
       EXPORTING
            form      = p_form
            language  = sy-langu
            startpage = 'FIRST'.

ENDFORM.                               "START_FORM_CHEQUE


*---------------------------------------------------------------------*
*       FORM end_form                                                 *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM end_form.

  CALL FUNCTION 'END_FORM'.

ENDFORM.                               " END_FORM

*&---------------------------------------------------------------------*
*&      Form  CONVERT_AMOUNT_TO_STRING
*&---------------------------------------------------------------------*
FORM convert_amount_to_string USING p_total p_waers
                              CHANGING p_line1 p_line2 p_line3.

  DATA: lw_words LIKE spell.

  DATA: li_currency(20) TYPE c OCCURS 5 WITH HEADER LINE,
        li_words(20) TYPE c OCCURS 50 WITH HEADER LINE.

  DATA: l_len   TYPE i,
        l_count TYPE i,
        l_line  TYPE i VALUE 1,
        l_fraction(8) TYPE c VALUE 'DDD/FFFF',
        l_factor      TYPE i.


  DEFINE lmac_print_line.
    if not &1 is initial.
      concatenate &1 li_words into &1 separated by space.
    else.
      &1 = li_words.
    endif.
  END-OF-DEFINITION.

  CLEAR: p_line1, p_line2, p_line3.

  CALL FUNCTION 'SPELL_AMOUNT'
       EXPORTING
            amount    = p_total
            currency  = p_waers
            filler    = ' '
            language  = sy-langu
       IMPORTING
            in_words  = lw_words
       EXCEPTIONS
            not_found = 1
            too_large = 2
            OTHERS    = 3.
  IF sy-subrc = 0.

* Figure out the decimal to fraction
    l_factor = 10 ** lw_words-currdec.
    l_count = 3 MOD lw_words-currdec.
    DO l_count TIMES.
      SHIFT lw_words-decimal(3) RIGHT.
      lw_words-decimal(1) = 0.
    ENDDO.

    WRITE: lw_words-decimal TO l_fraction(3) NO-ZERO RIGHT-JUSTIFIED,
           l_factor TO l_fraction+4(4) NO-ZERO LEFT-JUSTIFIED.

    SHIFT l_fraction LEFT DELETING LEADING space.

    SPLIT i_currency_text AT space INTO TABLE li_currency.
    LOOP AT li_currency. ENDLOOP.
    CONCATENATE li_currency 's' INTO li_currency.

    IF NOT lw_words-decimal IS INITIAL.
      CONCATENATE lw_words-word li_currency 'AND'
                  lw_words-decword 'CENTS ONLY'
             INTO lw_words-word SEPARATED BY space.
    ELSE.
      CONCATENATE lw_words-word li_currency 'ONLY'
             INTO lw_words-word SEPARATED BY space.

    ENDIF.

    TRANSLATE lw_words-word TO UPPER CASE.

    SPLIT lw_words-word AT space INTO TABLE li_words.

    CLEAR l_count.

    LOOP AT li_words.
      l_len = strlen( li_words ).

      ADD l_len TO l_count.

      IF l_count > 45.   " line size of ZFCHQ-VALUE
        l_count = l_len.
        ADD 1 TO l_line.
      ENDIF.

      CASE l_line.

        WHEN 1.
          lmac_print_line p_line1.
        WHEN 2.
          lmac_print_line p_line2.
        WHEN 3.
          lmac_print_line p_line3.
      ENDCASE.

      ADD 1 TO l_count.  " space between words

    ENDLOOP.

  ENDIF.


ENDFORM.                    " CONVERT_AMOUNT_TO_STRING

*&---------------------------------------------------------------------*
*&      Form  WIRTE_PARAMETERS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM wirte_parameters USING    p_rundate
                            p_identif
                            p_currency.

  WRITE:/ 'Rundate:              ', p_rundate.
  WRITE:/ 'Identification:       ', p_identif.
  WRITE:/ 'Currency:             ', p_currency.
  ULINE.
ENDFORM.                    " WIRTE_PARAMETERS

*&---------------------------------------------------------------------*
*&      Form  SELECT_CHEQUES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_cheques TABLES  p_reguh STRUCTURE i_reguh
                            p_regup STRUCTURE i_regup
                    USING    p_laufd
                             p_laufi
                             p_waers
                    CHANGING p_subrc.

  DATA: l_count TYPE p,
        l_index TYPE p.
  DATA: tp_regup TYPE t_regup OCCURS 100 WITH HEADER LINE.


  SELECT laufd laufi vblnr rwbtr lifnr name1 name2
         stras ort01
         znme1  znme2  zstra zort1   "LABK932282
         zregi zpstl zland waers
         hbkid ubnks hktid
         FROM reguh INTO CORRESPONDING FIELDS OF TABLE p_reguh
         WHERE laufd = p_laufd
         AND  laufi = p_laufi
         AND  waers = p_waers
         AND  rzawe IN r_rzawe
         AND  xvorl NE 'X'           " production data
         AND  rwbtr NE '0'
         ORDER BY lifnr  vblnr.
  IF sy-subrc <> 0.
    p_subrc = -1.
    EXIT.
  ENDIF.


  LOOP AT p_reguh.
    SELECT laufd
           laufi
           vblnr
           lifnr
           bldat
           xblnr
           wrbtr
           shkzg
           bukrs
           gjahr
           belnr
           FROM regup
           INTO CORRESPONDING FIELDS OF TABLE tp_regup
           WHERE laufd = p_reguh-laufd
           AND lifnr = p_reguh-lifnr
           AND vblnr = p_reguh-vblnr.
    IF sy-subrc <> 0.
      p_subrc = -2.
      EXIT.
    ENDIF.

    LOOP AT tp_regup.

* put bseg-zuonr as invoice no. for empolyee
      IF tp_regup-lifnr(1) = 'E'.
        SELECT SINGLE zuonr FROM bseg INTO tp_regup-xblnr
               WHERE belnr = tp_regup-belnr
               AND   bukrs = tp_regup-bukrs.
      ENDIF.

      APPEND tp_regup TO p_regup.
    ENDLOOP.
  ENDLOOP.


* count items for each cheque
  SORT p_reguh BY vblnr.
  SORT p_regup BY vblnr.
  l_index = 1.
  LOOP AT p_reguh.
    l_count = 0.
    LOOP AT p_regup FROM l_index.
      IF i_regup-vblnr = p_reguh-vblnr.
        l_count = l_count + 1.
        l_index = l_index + 1.
      ELSE.
        EXIT.
      ENDIF.
    ENDLOOP.
    p_reguh-count = l_count.

* get country name from country code
    SELECT SINGLE landx
           FROM t005t INTO p_reguh-landx
           WHERE land1 = p_reguh-zland
           AND spras = sy-langu.
    IF sy-subrc <> 0.
      p_subrc = -3.
      EXIT.
    ENDIF.


    TRANSLATE p_reguh-landx TO UPPER CASE.

* get bank #
    SELECT SINGLE bankl
           FROM t012 INTO p_reguh-bankl
           WHERE hbkid = p_reguh-hbkid
           AND banks = p_reguh-ubnks.
    IF sy-subrc <> 0.
      p_subrc = -4.
      EXIT.
    ENDIF.


* get bank account #
    SELECT SINGLE bankn
           FROM t012k INTO p_reguh-bankn
           WHERE hbkid = p_reguh-hbkid
           AND hktid = p_reguh-hktid.
    IF sy-subrc <> 0.
      p_subrc = -5.
      EXIT.
    ENDIF.


    MODIFY p_reguh.

  ENDLOOP.

  p_subrc = 0.

ENDFORM.                    " SELECT_CHEQUES

*&---------------------------------------------------------------------*
*&      Form  WRITE_SUMMARY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_summary USING    p_totnosign p_totemployee p_totother
                            p_nosignature
                            p_employee
                            p_other
                            p_printed.

  DATA: l_grandtotal LIKE reguh-rwbtr.

  l_grandtotal  =  p_totnosign + p_totemployee + p_totother.

  PERFORM write_report_subtotal USING 'Grand total for all cheques: '
                                       l_grandtotal l_waers.

  SKIP.
  ULINE.
  FORMAT COLOR COL_GROUP INTENSIFIED OFF.
  WRITE: AT /(l_linsz) 'Summary'.
  FORMAT RESET.
  ULINE.
  WRITE: / 'Total cheques issued for amount >= $25,000: ',
           l_nosignature.
  WRITE: / 'Total cheques issued for employee:          ',
           l_employee.
  WRITE: / 'Total cheques issued for the rest:          ',
           l_other.
  ULINE.
  WRITE: / 'Total cheques issued:                       ',
           l_printed.
  WRITE: / 'Total cheque paper used:                    ',
           l_totpages.

* no authorization to print cheques.
  IF l_rptonly = 'X'.
    SKIP.
    FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
    WRITE: / 'You are not authorised to print cheques.'.
    FORMAT RESET.
  ENDIF.

ENDFORM.                    " WRITE_SUMMARY


*&---------------------------------------------------------------------*
*&      Form  SORT_CHEQUES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sort_cheques TABLES   p_reguh STRUCTURE i_reguh
                           p_regup STRUCTURE i_regup
                           p_cheque STRUCTURE i_cheque.


  DATA: wa_cheque LIKE LINE OF i_cheque.

  DATA:   wa_reguh  TYPE t_reguh,
          it_nosign LIKE STANDARD TABLE OF wa_reguh,
          it_employee LIKE STANDARD TABLE OF wa_reguh,
          it_other LIKE STANDARD TABLE OF wa_reguh.

  DATA: l_count TYPE p.

* separate the cheques into 3 groups:
* reguh_nosignature : amount >= 25000
* reguh_employee : for employees
* reguh_other : other vernders.
  LOOP AT p_reguh INTO wa_reguh.
    wa_reguh-rwbtr = - wa_reguh-rwbtr.
    IF wa_reguh-rwbtr >= c_limit.
      APPEND wa_reguh TO it_nosign.
    ELSEIF wa_reguh-lifnr(1) = 'E'.
      APPEND wa_reguh TO it_employee.
    ELSE.
      APPEND wa_reguh TO it_other.
    ENDIF.
  ENDLOOP.

  SORT it_nosign BY rwbtr.
*LABK932282  SORT it_employee BY name2 name1 vblnr.
  SORT it_employee BY znme2 znme1 vblnr.
  SORT it_other BY lifnr vblnr.

  REFRESH p_reguh.
  REFRESH p_cheque.

  LOOP AT it_nosign INTO wa_reguh.
    MOVE-CORRESPONDING wa_reguh TO wa_cheque.
    PERFORM match_cheque_items TABLES p_cheque p_regup
    USING wa_cheque.
  ENDLOOP.

  LOOP AT it_employee INTO wa_reguh.
    MOVE-CORRESPONDING wa_reguh TO wa_cheque.
    PERFORM match_cheque_items TABLES p_cheque p_regup
                                  USING wa_cheque.
  ENDLOOP.

  LOOP AT it_other INTO wa_reguh.
    MOVE-CORRESPONDING wa_reguh TO wa_cheque.
    PERFORM match_cheque_items  TABLES p_cheque p_regup
                              USING wa_cheque.
  ENDLOOP.

ENDFORM.                    " SORT_CHEQUES

*&---------------------------------------------------------------------*
*&      Form  MATCH_CHEQUE_ITEMS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM match_cheque_items TABLES   p_cheque STRUCTURE i_cheque
                                 p_regup STRUCTURE i_regup
                        USING    p_wa_cheque STRUCTURE i_cheque.

  DATA: l_count TYPE p.
  DATA: tmp_regup TYPE t_regup OCCURS 100 WITH HEADER LINE.

  REFRESH tmp_regup.
  l_count = 0.
  LOOP AT p_regup.
    IF p_regup-vblnr = p_wa_cheque-vblnr
       AND p_regup-laufd = p_wa_cheque-laufd
       AND p_regup-laufi = p_wa_cheque-laufi
       AND p_regup-lifnr = p_wa_cheque-lifnr.

      tmp_regup = p_regup.
      l_count = l_count + 1.
      tmp_regup-item = l_count.
      APPEND tmp_regup.
    ENDIF.
  ENDLOOP.

  p_wa_cheque-count = l_count.

  SORT tmp_regup BY vblnr bldat xblnr wrbtr.

  LOOP AT tmp_regup.
    MOVE-CORRESPONDING tmp_regup TO p_wa_cheque.
    APPEND p_wa_cheque TO p_cheque.
  ENDLOOP.

ENDFORM.                    " MATCH_CHEQUE_ITEMS

*&---------------------------------------------------------------------*
*&      Form  WRITE_REPORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_report USING    p_cheque STRUCTURE i_cheque
                CHANGING l_nosignature l_employee l_other.

*count # of cheque printed for each category
*and write report
  IF p_cheque-rwbtr >= c_limit.
    IF l_nosignature = 0.
      lmac_write_report_header 'Cheques for amount >= $25,000:'
                                'Cheque No.' 'Date' 'Vender No.'
                                'Vender Name' 'Amount' ' ' 'Pages'.
    ENDIF.
    lmac_write_report_items p_cheque-vblnr p_cheque-laufd
                            p_cheque-lifnr p_cheque-znme1
*LABK932282                 p_cheque-name1
                            p_cheque-rwbtr ' ' p_cheque-pages.
    l_nosignature = l_nosignature + 1.
    l_totnosign = l_totnosign + p_cheque-rwbtr.
  ELSEIF p_cheque-lifnr(1) = 'E'.
    IF l_employee = 0.
      PERFORM write_report_subtotal
              USING 'Subtotal for amount >= $25,000:'
                    l_totnosign l_waers.
      lmac_write_report_header 'Cheques for employees:'
                               'Cheque No.' 'Date' 'Vender No.'
                               'Vender Name' 'Amount' 'Base' 'Pages'.
    ENDIF.
    lmac_write_report_items p_cheque-vblnr p_cheque-laufd
                            p_cheque-lifnr p_cheque-znme1
*LABK932282                 p_cheque-name1
                            p_cheque-rwbtr p_cheque-znme2
*LABK932282                 p_cheque-name2
                            p_cheque-pages.
    l_employee = l_employee + 1.
    l_totemployee = l_totemployee + p_cheque-rwbtr.
  ELSE.
    IF l_other = 0.
      IF l_employee = 0.
        PERFORM write_report_subtotal
                USING 'Subtotal for amount >= $25,000:'
                                   l_totnosign l_waers.
      ELSE.
        PERFORM write_report_subtotal
                USING 'Subtotal for employees:'
                                   l_totemployee l_waers.
      ENDIF.

      lmac_write_report_header 'Cheques for all the rest:'
                               'Cheque No.' 'Date' 'Vender No.'
                               'Vender Name' 'Amount' ' ' 'Pages'.
    ENDIF.
    lmac_write_report_items p_cheque-vblnr p_cheque-laufd
                        p_cheque-lifnr p_cheque-znme1
*LABK932282             p_cheque-name1
                        p_cheque-rwbtr ' '
                        p_cheque-pages.
    l_other = l_other + 1.
    l_totother = l_totother + p_cheque-rwbtr.
  ENDIF.

ENDFORM.                    " WRITE_REPORT

*&---------------------------------------------------------------------*
*&      Form  WRITE_REPORT_SUBTOTAL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_report_subtotal USING    p_title
                                    p_total
                                    p_waers.
  DATA:   l_txt(100)    TYPE c.
  DATA:   l_total(15)  TYPE c.

  WRITE p_total TO l_total CURRENCY p_waers.
  CONCATENATE p_title l_total INTO l_txt.
  FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
  ULINE.
  WRITE: /1 p_title,
          69(15) l_total,
          118(5) ' '.
  FORMAT RESET.

ENDFORM.                    " WRITE_REPORT_SUBTOTAL