SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Upload file to IDoc



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Interfaces | Интерфейсы
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Mon Sep 10, 2007 8:21 pm    Post subject: Upload file to IDoc Reply with quote

Code:
REPORT ZS7BM000005 message-id ZS7.
*______________________________________________________________________

*/ Program Name: Creation of ORDRSP IDocs from file E020
*/ Description : This program reads in external file E020 containing
*                order confirmation data from internal vendors and
*                creates one ORDRSP IDoc per PO.
*/ Transaction : n/a - run from job Z_ccc_S7B_Dnnnnn,
*                where 'ccc' = 3-digit client and 'nnnnn' = zero-filled
*                sequence number.
*______________________________________________________________________
tables:  lfa1,
         e1edk01,
         e1edk02,
         e1edp01,
         e1edp19,
         e1edp20,
         e1edp02,
         e1eds01,
         zst7f_ty_vendors.

parameters:  p_path like PATH-PATHEXTERN  default '/ftp/atac/in/'.

data:  INFILE       LIKE PATH-PATHEXTERN,
       back_path(7) type c value 'backup/',
       info_no      like ekpo-infnr,
       offset       like sy-fdpos,
       p01_ctr      like sy-index,
       seg_num      like sy-index.

data:  OUTFILE LIKE PATH-PATHEXTERN,
       today(8)  type c,
       outfile_err(1) type c.

data:  begin of uty_vendors occurs 10,
          lifnr like lfa1-lifnr,
          name_abbr like zst7f_ty_vendors-name_abbr,
          site_code like zst7f_ty_vendors-site_code,
          ship_days like zst7f_ty_vendors-ship_days,
       end of uty_vendors.

data:  iZSS7B20 like ZSS7B20.

data:  idocdata like edi_dd occurs 5 with header line.
data:  iedidc like edi_dc occurs 1 with header line.

data:  begin of ie020 occurs 10,
        lifnr            like lfa1-lifnr,
        ship_days        like zst7f_ty_vendors-ship_days,
        po_number(10)    type n,
        po_lineno(5)     type n,
        slip_number      like e1edp02-belnr,
        sched_date       like e1edp02-datum,   "char 8
        sched_qty        like e1edp20-wmeng,
        cust_partno      like e1edp19-idtnr,
        vendor_partno    like e1edp19-mfrpn,
        sched_deliv_date like e1edp20-edatu,  "format OK - ccyymmdd
        uom              like e1edp01-menee,
        qty_ordered      like e1edp01-menge,
       end of ie020.

data: save_po like ie020-po_number,
      save_line like ie020-po_lineno,
      save_vend_part like ie020-vendor_partno,
      save_cust_part like ie020-cust_partno.


*------------------------------------------------------------------
* MAIN PROCESSING LOOP
*------------------------------------------------------------------

START-OF-SELECTION.

today = sy-datlo.

* find all internal vendors
select a~lifnr
       b~name_abbr b~site_code b~ship_days
   into corresponding fields of table uty_vendors
     from lfa1 as a
          inner join zst7f_ty_vendors as b
             on a~lifnr = b~lifnr
     where a~ktokk = 'ZZTY' and
           b~ship_code = ' '.

concatenate 'SAP' sy-sysid(3) into iedidc-sndpor.

* initialize control record:
move:  '2'        to  iedidc-direct,
      'ORDERS01'  to  iedidc-doctyp,
      'ORDRSP'    to  iedidc-mestyp,
      'F'         to  iedidc-std,
      'E020'      to  iedidc-stdmes,
      'LS'        to  iedidc-sndprt,
      'TY_VENDORS' to iedidc-sndprn,
      sy-datlo    to  iedidc-credat,
      sy-timlo    to  iedidc-cretim.

append iedidc.

e1edk01-action = '000'.

loop at uty_vendors.

  clear ie020. refresh ie020.

  if not uty_vendors-name_abbr is initial.
* datafiles are received with naming convention:
* E020_<customer name abbreviation>_UTY
    concatenate p_path 'E020_' uty_vendors-name_abbr '_UTY'
        into infile.
    if not sy-subrc is initial.  "pathname too long
* Filename too long: &
      message w016 with infile.
      continue.
    endif.
    condense infile.
    OPEN DATASET INFILE FOR INPUT IN TEXT MODE.
    if not sy-subrc is initial.
*'Cannot open dataset & on &'
      message i013 with infile sy-datum.
    else.  "successful file open
      concatenate p_path back_path 'E020_'
          uty_vendors-name_abbr '_UTY' today
                into outfile.
      if not sy-subrc is initial.  "pathname too long
* Filename too long: &
        message w016 with outfile.
        continue.
      endif.
      condense outfile.
      OPEN DATASET OUTFILE FOR OUTPUT IN TEXT MODE.
* if the timestamped file cannot be created, do not process the
* input file, because the input file is deleted after processing,
* and there would be no record of the data.
      if not sy-subrc is initial.
*'ERROR opening file & for output'
        close dataset infile.
        message i033 with outfile.
        continue.  "process next vendor's file
      endif.
      do.
        read dataset infile into izss7b20.
        case sy-subrc.
          when 0.
            transfer izss7b20 to outfile.
            if izss7b20-datacode = 'T'. "trailer rec
              perform process_one_vendor using infile.
              exit.  "process next vendor's file
            endif.
            check izss7b20-datacode = 'D'. "data rec
            move-corresponding uty_vendors to ie020.
            move-corresponding izss7b20 to ie020.
            perform convert_po_no using izss7b20-pono_poline
                                  changing ie020-po_number
                                           ie020-po_lineno.
            perform convert_date using izss7b20-sched_date
                                       izss7b20-sched_type
                                       izss7b20-ship_method
                              changing ie020-sched_date
                                       ie020-ship_days
                                       ie020-sched_deliv_date.
            perform convert_quantities
                                using izss7b20-uom
                                      izss7b20-sched_qty
                                      izss7b20-qty_ordered
                                changing ie020-uom
                                         ie020-sched_qty
                                         ie020-qty_ordered.
            append ie020.
          when 4.  "EOF
            perform process_one_vendor using infile.
            exit.  "process next vendor's file
          when others.
*ERROR reading dataset & on &
            message w015 with infile sy-datum.
            exit.  "discontinue file reads
        endcase.
      enddo.
      close dataset: infile, outfile.
      delete dataset infile.
    endif.
  endif.
endloop. "UTY_VENDORS

*&---------------------------------------------------------------------*
*&      Form  process_one_vendor
*&---------------------------------------------------------------------*
*       Records from one vendor file are now in the internal table
*       ie020.
*----------------------------------------------------------------------*
FORM process_one_vendor using value(infile).

  sort ie020 by po_number po_lineno.
  loop at ie020.
    if sy-tabix > 1.
      if ( ie020-po_number <> save_po or
           ie020-po_lineno <> save_line ).
        perform idoc_mat_seg.
      endif.
    endif.
    if ie020-po_number <> save_po.
      if sy-tabix > 1.
        perform post_prev_idoc.
      endif.
      perform idoc_header_segs.
    endif.
    if ( ie020-po_number <> save_po or
         ie020-po_lineno <> save_line ).
      if sy-tabix > 1.
        perform idoc_item_segs.
      endif.
    endif.
    perform idoc_sched_segs.
    save_po = ie020-po_number.
    save_line = ie020-po_lineno.
    save_vend_part = ie020-vendor_partno.
    save_cust_part = ie020-cust_partno.
  endloop.
  perform idoc_mat_seg.
  perform post_prev_idoc.
* File successfully processed: &
  message s035 with infile.

ENDFORM.                    " process_one_vendor

*&---------------------------------------------------------------------*
*&      Form  convert_po_no
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM convert_po_no using value(infield)
                   changing po_number like ie020-po_number
                            po_line   like ie020-po_lineno.

data:  cpos like sy-fdpos,
       lpos like sy-fdpos,
       cline(6) type c.

* if the infield contains a hyphen, assume that the preceding characters
* represent the po number, if they are numeric. The po line number is
* assumed to be all numeric characters after the hyphen.
  if infield ca '-'.
    if infield(sy-fdpos) co ' 0123456789'.  "numeric
      po_number = infield(sy-fdpos).
      cpos = sy-fdpos + 1.
    endif.
  else.  "no hyphen - PTY
    if infield(2) = '71'.  "SAP number range
      cpos = 10.
    else.                  "SyteLine number
      cpos = 6.
    endif.
    if infield(cpos) co ' 0123456789'.  "numeric
      po_number = infield(cpos).
    endif.
  endif.
  if not po_number is initial.
    while infield+cpos(1) co '0123456789'.
      cline+lpos(1) = infield+cpos(1).
      lpos = lpos + 1.
      cpos = cpos + 1.
    endwhile.
    shift cline left deleting leading '0'.
    if not cline is initial.
      po_line = cline.
    endif.
  endif.

* Put out a warning in the job log, but create the IDoc to save the data
  if ( po_number is initial or
       po_line is initial ).
* PO number - line item conversion failed: &
    message w016 with infield.
  endif.

ENDFORM.                    " convert_po_no

*&---------------------------------------------------------------------*
*&      Form  convert_date
*&---------------------------------------------------------------------*
*       Convert yymmdd to ccyymmdd for SAP and
*       convert ship date to delivery date
*----------------------------------------------------------------------*
FORM convert_date USING value(DATE6)
                        value(DATE_TYPE)
                        value(i_ship_code)

               CHANGING DATE8         like IE020-SCHED_DATE
                        SHIP_DAYS     like ie020-ship_days
                        DELIVERY_DATE like ie020-sched_deliv_date.

data:  cent(2) type c,
       d_date  type d.

* add century to schedule date
  check date6 cn ' /0'.

  if date6(2) < '90'.
    cent = '20'.
  else.
    cent = '19'.
  endif.
  concatenate cent date6 into date8.

* convert ship date to delivery date (if necessary)
  if date_type <> 'ETA'. " delivery date not sent
    if not i_ship_code is initial.  "ship method sent
      select single ship_days from zst7f_ty_vendors
              into ship_days
          where lifnr = ie020-lifnr
            and ship_code = i_ship_code.
    endif.
* defaut ship_days are passed in and will be changed only
* if a number specific to the shipping method is found in
* the select above
    if ( ship_days > 0 and
         not delivery_date is initial ).
* put the date into a date field, so SAP will do date math
      d_date = delivery_date.
      d_date = d_date + ship_days.
      delivery_date = d_date.
      shift delivery_date left deleting leading ' '.
    endif.
  endif.

ENDFORM.                    " convert_date

*&---------------------------------------------------------------------*
*&      Form  quantity_conversion
*&---------------------------------------------------------------------*
*       The quantities in the input file are implied 3-decimal,
*       so need to be converted into a "real" number.
*       Also, the unit of measure may be 'KP' indicating that the qty
*       is given in thousands.
*----------------------------------------------------------------------*
FORM convert_quantities USING value(i_UOM)
                              value(i_confirm_qty)
                              value(i_QTY_ORDERED)
                     CHANGING o_uom like iE020-UOM
                              o_confirm_qty like IE020-SCHED_QTY
                              o_qty_ordered like IE020-QTY_ORDERED.

constants: thou type p decimals 3 value '1000'.

data:  n_confirm_qty like ekpo-menge,
       n_qty_ordered like ekpo-menge.

  if ( i_confirm_qty co ' 0123456789' and
       i_confirm_qty cn ' 0' ).
    n_confirm_qty = i_confirm_qty.
    n_confirm_qty = n_confirm_qty / thou.
  endif.
  if ( i_qty_ordered co ' 0123456789' and
       i_qty_ordered cn ' 0' ).
    n_qty_ordered = i_qty_ordered.
    n_qty_ordered = n_qty_ordered / thou.
  endif.

  if i_uom = 'KP'.
    n_confirm_qty = n_confirm_qty / thou.
    n_qty_ordered = n_qty_ordered / thou.
  endif.

  o_uom = 'PCE'.
  if not n_confirm_qty is initial.
    o_confirm_qty = n_confirm_qty.
    shift o_confirm_qty left deleting leading space.
  else.
    clear o_confirm_qty.
  endif.
  if not n_qty_ordered is initial.
    o_qty_ordered = n_qty_ordered.
    shift o_qty_ordered left deleting leading space.
  else.
    clear o_qty_ordered.
  endif.

ENDFORM.                    " convert_quantities

*&---------------------------------------------------------------------*
*&      Form  idoc_header_segs
*&---------------------------------------------------------------------*
*       create internal table entries for PO header segments:
*          E1EDK01
*          E1EDK02
*----------------------------------------------------------------------*
FORM idoc_header_segs.

  clear seg_num.
  idocdata-segnam = 'E1EDK01'.
  idocdata-sdata = e1edk01.
  perform append_idoc_rec.

  clear e1edk02.
  idocdata-segnam = 'E1EDK02'.
  e1edk02-qualf = '001'.
  e1edk02-belnr = ie020-po_number.
  idocdata-sdata = e1edk02.
  perform append_idoc_rec.

ENDFORM.                    " idoc_header_segs

*&---------------------------------------------------------------------*
*&      Form  idoc_item_segs
*&---------------------------------------------------------------------*
*       create internal table entries for PO item header segments:
*          E1EDP01
*          E1EDP02
*----------------------------------------------------------------------*
FORM idoc_item_segs.

  clear e1edp01.
  idocdata-segnam = 'E1EDP01'.
  e1edp01-menee = ie020-uom.
  e1edp01-menge = ie020-qty_ordered.
  idocdata-sdata = e1edp01.
  perform append_idoc_rec.

  clear e1edp02.
  idocdata-segnam = 'E1EDP02'.
  e1edp02-qualf = '001'.
  e1edp02-belnr = ie020-po_number.
  e1edp02-zeile = ie020-po_lineno.
  idocdata-sdata = e1edp02.
  perform append_idoc_rec.

  idocdata-segnam = 'E1EDP02'.
  clear e1edp02.
  e1edp02-qualf = '002'.
  e1edp02-belnr = ie020-slip_number.
  e1edp02-datum = ie020-sched_date.
  idocdata-sdata = e1edp02.
  perform append_idoc_rec.

  p01_ctr = p01_ctr + 1.
ENDFORM.                    " idoc_item_segs

*&---------------------------------------------------------------------*
*&      Form  idoc_sched_segs
*&---------------------------------------------------------------------*
*       create internal table entries for PO item schedule segments:
*          E1EDP20
*----------------------------------------------------------------------*
FORM idoc_sched_segs.

  clear e1edp20.
  idocdata-segnam = 'E1EDP20'.
  e1edp20-edatu = ie020-sched_deliv_date.
  e1edp20-wmeng = ie020-sched_qty.
  idocdata-sdata = e1edp20.
  perform append_idoc_rec.

ENDFORM.                    " idoc_sched_segs

*&---------------------------------------------------------------------*
*&      Form  idoc_mat_seg
*&---------------------------------------------------------------------*
*       create internal table entries for PO item material no. segments:
*          E1EDP19
*----------------------------------------------------------------------*
FORM idoc_mat_seg.

tables:  ekpo.

  clear e1edp19.
  idocdata-segnam = 'E1EDP19'.
  e1edp19-qualf = '002'.
  e1edp19-idtnr = save_vend_part.
  e1edp19-mfrpn = save_cust_part.
  idocdata-sdata = e1edp19.
  perform append_idoc_rec.

  clear e1edp19.
  select single matnr from ekpo into e1edp19-idtnr
         where ebeln = save_po and
               ebelp = save_line.
  if not e1edp19-idtnr is initial.
    idocdata-segnam = 'E1EDP19'.
    e1edp19-qualf = '001'.
    idocdata-sdata = e1edp19.
    perform append_idoc_rec.
  endif.

ENDFORM.                    " idoc_mat_seg

***********************************************************************
*&      Form  post_prev_idoc
*&---------------------------------------------------------------------*
*       create a database idoc from the idocdata table and clear table.
*----------------------------------------------------------------------*
FORM post_prev_idoc.

  clear e1eds01.
  idocdata-segnam = 'E1EDS01'.
  e1eds01-sumid = '001'.
  e1eds01-summe = p01_ctr.
  shift e1eds01-summe left deleting leading space.
  idocdata-sdata = e1eds01.
  perform append_idoc_rec.

  CALL FUNCTION 'INBOUND_IDOC_PROCESS'
    TABLES
      IDOC_CONTROL       =  iedidc
      IDOC_DATA          =  idocdata.

  commit work.

  refresh idocdata.
  clear:
    idocdata,
    p01_ctr,
    save_po,
    save_line,
    save_vend_part,
    save_cust_part.

ENDFORM.                    " post_prev_idoc

*&---------------------------------------------------------------------*
*&      Form  append_idoc_rec
*&---------------------------------------------------------------------*
*       add a data record to the IDoc internal table
*----------------------------------------------------------------------*
FORM append_idoc_rec.

idocdata-tabnam = idocdata-segnam.
seg_num = seg_num + 1.
idocdata-segnum = seg_num.
shift idocdata-segnum left deleting leading space.
append idocdata.
clear idocdata.

ENDFORM.                    " append_idoc_rec
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Interfaces | Интерфейсы All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.