Tuesday, March 19, 2019

Create Smartforms and send email Attachment

well, hello folks. this time i will demonstrate you about my programs of creating smartforms and sending it through email as an attachment.
my code is divided into 3 parts

  • ZMME006
  • ZMME006TOP
  • ZMME006F01
ZMME006

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
*&---------------------------------------------------------------*
*&                                                               *
*&---------------------------------------------------------------*
*& Title       : Form Reminder Email to Vendor                   *
*& Module      : MM                                              *
*& Functional  :                                                 *
*& Author      :                                                 *
*& Description :                                                 *
*& Version     : 01.00.00                                        *
*&---------------------------------------------------------------*
*& REVISION LOG                                                  *
*&                                                               *
*& VER#     DATE        AUTHOR      DESCRIPTION                  *
*& ----     ----        ------      -----------                  *
*&---------------------------------------------------------------*

REPORT zmme006.

INCLUDE zmme006top.
INCLUDE zmme006f01.

*&---------------------------------------------------------------------*
*&      Form  entry
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ENT_RETCO  text
*      -->ENT_SCREEN text
*----------------------------------------------------------------------*
FORM entry USING ent_retco ent_screen.
  TABLES  : nast.
  DATA    : lv_druvo       LIKE t166k-druvo,
            lv_from_memory,
            lwa_nast       LIKE nast,
            lwa_doc        TYPE meein_purchase_doc_print.

  DATA    : lv_fname      TYPE rs38l_fnam,
            lwa_ssfcompop TYPE ssfcompop,
            lwa_ssfctrlop TYPE ssfctrlop,
            lv_formname   TYPE tdsfname.

  DATA    : lv_email        TYPE adr6-smtp_addr,
            lv_adrnr        TYPE lfa1-adrnr,
            lv_lifnr        TYPE lfa1-lifnr,
            lv_message(100),
            lv_ebeln        TYPE ekko-ebeln.

  CLEAR ent_retco.
  IF nast-aende EQ space.
    lv_druvo = '1'.
  ELSE.
    lv_druvo = '2'.
  ENDIF.
   BREAK aby_abap2.
  p_docno = nast-objky.

  PERFORM f_get_data.

  CASE sy-tcode.
    WHEN 'ME9F'.
      CHECK sy-xcode = '9AUS' OR sy-xcode = '9ANZ'.
      IF nast-nacha = '1'.
        PERFORM f_print.
      ELSEIF nast-nacha = '5'.
        CLEAR : lv_lifnr, lv_adrnr, lv_email.
        SELECT SINGLE lifnr FROM ekko INTO lv_lifnr
          WHERE ebeln = p_docno.
        SELECT SINGLE adrnr FROM lfa1 INTO lv_adrnr
          WHERE lifnr  = lv_lifnr.
        IF sy-subrc = 0.
          SELECT SINGLE smtp_addr FROM adr6 INTO lv_email
            WHERE addrnumber = lv_adrnr.
        ENDIF.

        IF  lv_email IS NOT INITIAL.
          PERFORM f_send_mail USING nast
                                    lv_email.
        ELSE.
          CLEAR lv_message.
          CONCATENATE 'Email vendor is not available in PO' p_docno INTO lv_message SEPARATED BY space.
          MESSAGE lv_message TYPE 'S' DISPLAY LIKE 'E'.
        ENDIF.
      ENDIF.
    WHEN 'ME91F'.
      PERFORM f_print.
*      CHECK sy-xcode = '9AUS' OR sy-xcode = '9ANZ'.
      IF nast-nacha = '1'.
        PERFORM f_print.
      ELSEIF nast-nacha = '5'.
        break aby_Abap1.
        CLEAR : lv_lifnr, lv_adrnr, lv_email.
        SELECT SINGLE lifnr FROM ekko INTO lv_lifnr
          WHERE ebeln = p_docno.
        SELECT SINGLE adrnr FROM lfa1 INTO lv_adrnr
          WHERE lifnr  = lv_lifnr.
        IF sy-subrc = 0.
          SELECT SINGLE smtp_addr FROM adr6 INTO lv_email
            WHERE addrnumber = lv_adrnr.
        ENDIF.

*        IF  lv_email IS NOT INITIAL.
          PERFORM f_send_mail_nocommit USING nast
                                            lv_email.
*        ELSE.
*          CLEAR lv_message.
*          CONCATENATE 'Email vendor is not available in PO' p_docno INTO lv_message SEPARATED BY space.
*          MESSAGE lv_message TYPE 'S' DISPLAY LIKE 'E'.
*        ENDIF.
      ENDIF.
  ENDCASE.
ENDFORM.                    "entry
.
ZMME006TOP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
*&---------------------------------------------------------------------*
*&  Include           ZMME006TOP
*&---------------------------------------------------------------------*

DATA : gi_ekko      TYPE TABLE OF ekko,
       gwa_ekko     TYPE ekko,
       gi_ekpo      TYPE TABLE OF ekpo,
       gwa_ekpo     TYPE ekpo,
       gi_esll      TYPE TABLE OF esll,
       gwa_esll     TYPE esll,
       gi_esll2     TYPE TABLE OF esll,
       gwa_esll2    TYPE esll,
       gi_eket      TYPE TABLE OF eket,
       gwa_eket     TYPE eket,
       gi_ekbe      TYPE TABLE OF ekbe,
       gwa_ekbe     TYPE ekbe,
       gi_t001      TYPE TABLE OF t001,
       gwa_t001     TYPE t001,
       gi_t001w     TYPE TABLE OF t001w,
       gwa_t001w    TYPE t001w,
       gi_t005u     TYPE TABLE OF t005u,
       gwa_t005u    TYPE t005u,
       gi_t005t     TYPE TABLE OF t005t,
       gwa_t005t    TYPE t005t,
       gi_adrc      TYPE TABLE OF adrc,
       gwa_adrc     TYPE adrc,
       gi_lfa1      TYPE TABLE OF lfa1,
       gwa_lfa1     TYPE lfa1,
       gi_zeapppo   TYPE TABLE OF zeapppo,
       gwa_zeapppo  TYPE zeapppo.

DATA : gwa_header   TYPE zcl_mme006=>ty_header,
       gi_item      TYPE zcl_mme006=>ty_item_tab,
       gwa_item     TYPE zcl_mme006=>ty_item.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS :          p_docno LIKE ekko-ebeln,
                      p_app   LIKE t681a-kappl DEFAULT 'EF',
                      p_proc  LIKE nast-vstat DEFAULT '0'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM f_get_data.
  PERFORM f_print.

ZMME006F01


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
*&---------------------------------------------------------------------*
*&  Include           ZMME006F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  F_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_get_data .

  DATA : lv_inc TYPE i.

  DATA : lv_name   TYPE text200,
         lv_title  TYPE text20,
         ls_tsad3t TYPE tsad3t.


  SELECT * FROM ekko
    INTO TABLE gi_ekko
    WHERE ebeln EQ p_docno.
  IF sy-subrc EQ 0.
    SELECT * FROM t001
      INTO TABLE gi_t001
      FOR ALL ENTRIES IN gi_ekko
      WHERE bukrs EQ gi_ekko-bukrs.
    IF sy-subrc EQ 0.
      SELECT * FROM adrc
        INTO TABLE gi_adrc
        FOR ALL ENTRIES IN gi_t001
        WHERE addrnumber = gi_t001-adrnr.
    ENDIF.

    SELECT * FROM lfa1
        INTO TABLE gi_lfa1
        FOR ALL ENTRIES IN gi_ekko
        WHERE lifnr = gi_ekko-lifnr.
    IF sy-subrc EQ 0.
      SELECT * FROM adrc
      APPENDING TABLE gi_adrc
      FOR ALL ENTRIES IN gi_lfa1
      WHERE addrnumber = gi_lfa1-adrnr.
    ENDIF.

    SELECT * FROM zeapppo
      INTO TABLE gi_zeapppo
      FOR ALL ENTRIES IN gi_ekko
      WHERE ekorg EQ gi_ekko-ekorg
        AND ekgrp EQ gi_ekko-ekgrp.

    READ TABLE gi_adrc INTO gwa_adrc INDEX 1.
    IF sy-subrc EQ 0.
      SELECT * FROM t005u
          INTO TABLE gi_t005u
          FOR ALL ENTRIES IN gi_adrc
          WHERE land1 = gi_adrc-country
            AND spras = sy-langu.
      SELECT * FROM t005t
        INTO TABLE gi_t005t
        FOR ALL ENTRIES IN gi_adrc
        WHERE land1 = gi_adrc-country
          AND spras = sy-langu.
    ENDIF.

    SELECT * FROM ekpo
      INTO TABLE gi_ekpo
      FOR ALL ENTRIES IN gi_ekko
      WHERE ebeln EQ gi_ekko-ebeln
        AND loekz EQ ''
        AND pstyp NE '9'
        AND ( mahn1 NE 0 OR mahn2 NE 0 OR mahn3 NE 0 ).  " Added by Baskoro 06/05/2015.
    IF sy-subrc EQ 0.
      SELECT * FROM eket
        INTO TABLE gi_eket
        FOR ALL ENTRIES IN gi_ekpo
        WHERE ebeln EQ gi_ekpo-ebeln
          AND ebelp EQ gi_ekpo-ebelp.

      SELECT * FROM ekbe
        INTO TABLE gi_ekbe
        FOR ALL ENTRIES IN gi_ekpo
        WHERE ebeln EQ gi_ekpo-ebeln
          AND ebelp EQ gi_ekpo-ebelp
          AND bwart IN ('101','102','103','104','122','123','124','125').

    ENDIF.
  ENDIF.

  "Filling Header & Item.

  LOOP AT gi_ekko INTO gwa_ekko.
    "Header
    CLEAR : gwa_header.

    gwa_header-ebeln = gwa_ekko-ebeln.
    gwa_header-bedat = gwa_ekko-bedat.
    PERFORM f_get_month USING gwa_header-bedat gwa_header-bedat_txt.
    gwa_header-date = sy-datum.
    PERFORM f_get_month USING gwa_header-date gwa_header-date_txt.

    READ TABLE gi_t001 INTO gwa_t001 WITH KEY bukrs = gwa_ekko-bukrs.
    IF sy-subrc EQ 0.
      gwa_header-com_butxt = gwa_t001-butxt.
      READ TABLE gi_adrc INTO gwa_adrc WITH KEY addrnumber = gwa_t001-adrnr.
      IF sy-subrc EQ 0.
        gwa_header-com_name1 = gwa_adrc-name1.
        gwa_header-com_street = gwa_adrc-street.
        gwa_header-com_house_num1 = gwa_adrc-house_num1.
        gwa_header-com_city1 = gwa_adrc-city1.
        gwa_header-com_city2 = gwa_adrc-city2.
        gwa_header-com_tel_number = gwa_adrc-tel_number.
        gwa_header-com_fax_number = gwa_adrc-fax_number.
        READ TABLE gi_t005u INTO gwa_t005u WITH KEY land1 = gwa_adrc-country.
        IF sy-subrc EQ 0.
          gwa_header-com_bezei = gwa_t005u-bezei.
          PERFORM f_convert_to_upper USING gwa_header-com_bezei.
        ENDIF.
        READ TABLE gi_t005t INTO gwa_t005t WITH KEY land1 = gwa_adrc-country.
        IF sy-subrc EQ 0.
          gwa_header-com_landx = gwa_t005t-landx.
        ENDIF.
      ENDIF.
    ENDIF.

    READ TABLE gi_zeapppo INTO gwa_zeapppo WITH KEY ekorg = gwa_ekko-ekorg
                                                    ekgrp = gwa_ekko-ekgrp
                                                    ypos = 'MANAGER'. " Added by Baskoro 12/05/2015
    IF sy-subrc EQ 0.
      gwa_header-com_dept = gwa_zeapppo-dept.
      gwa_header-com_ypic = gwa_zeapppo-ypic.
      gwa_header-com_ypos = gwa_zeapppo-ypos.
    ENDIF.

    IF gwa_ekko-adrnr NE ''.
      SELECT * INTO TABLE gi_adrc FROM adrc WHERE addrnumber = gwa_ekko-adrnr.

      READ TABLE gi_adrc INTO gwa_adrc INDEX 1.

      CONCATENATE gwa_adrc-name1 gwa_adrc-name2 gwa_adrc-name3 INTO lv_name.

      SELECT SINGLE * FROM tsad3t INTO ls_tsad3t WHERE title = gwa_adrc-title
                                                   AND langu = sy-langu.
      IF sy-subrc EQ 0.
        CONCATENATE ',' ls_tsad3t-title_medi INTO lv_title SEPARATED BY space.
      ENDIF.

      CONCATENATE lv_name lv_title INTO gwa_header-ven_name1.

    ELSE.

      READ TABLE gi_lfa1 INTO gwa_lfa1 WITH KEY lifnr = gwa_ekko-lifnr.
      IF sy-subrc EQ 0.

        READ TABLE gi_adrc INTO gwa_adrc WITH KEY addrnumber = gwa_lfa1-adrnr.
        IF sy-subrc EQ 0.
          CONCATENATE gwa_adrc-name1 gwa_adrc-name2 gwa_adrc-name3 INTO lv_name.

          SELECT SINGLE * FROM tsad3t INTO ls_tsad3t WHERE title = gwa_adrc-title
                                                               AND langu = sy-langu.
          IF sy-subrc EQ 0.
            CONCATENATE ',' ls_tsad3t-title_medi INTO lv_title SEPARATED BY space.
          ENDIF.

          CONCATENATE lv_name lv_title INTO gwa_header-ven_name1.
        ENDIF.
      ENDIF.
    ENDIF.

    "_Alamat
    CONCATENATE gwa_adrc-street gwa_adrc-str_suppl1 gwa_adrc-str_suppl2 INTO gwa_header-ven_street.

    gwa_header-ven_house_num1 = gwa_adrc-house_num1.
    gwa_header-ven_city1 = gwa_adrc-city1.
    PERFORM f_convert_to_upper USING gwa_header-ven_city1.
    gwa_header-ven_city2 = gwa_adrc-city2.
    PERFORM f_convert_to_upper USING gwa_header-ven_city2.
    READ TABLE gi_t005u INTO gwa_t005u WITH KEY land1 = gwa_adrc-country.
    IF sy-subrc EQ 0.
      gwa_header-ven_bezei = gwa_t005u-bezei.
      PERFORM f_convert_to_upper USING gwa_header-ven_bezei.
    ENDIF.
    READ TABLE gi_t005t INTO gwa_t005t WITH KEY land1 = gwa_adrc-country.
    IF sy-subrc EQ 0.
      gwa_header-ven_landx = gwa_t005t-landx.
    ENDIF.


    CLEAR   : lv_inc.
    REFRESH : gi_item[].
    LOOP AT gi_ekpo INTO gwa_ekpo WHERE ebeln EQ gwa_ekko-ebeln.
      ADD 1 TO lv_inc.
      CLEAR : gwa_item.

*      gwa_item-no_item = lv_inc.
      gwa_item-ebelp   = gwa_ekpo-ebelp.
      gwa_item-matnr   = gwa_ekpo-matnr.
      gwa_item-meins   = gwa_ekpo-meins.
      gwa_item-menge   = gwa_ekpo-menge.

      LOOP AT gi_ekbe INTO gwa_ekbe WHERE ebeln EQ gwa_ekpo-ebeln
                                      AND ebelp EQ gwa_ekpo-ebelp.
        IF gwa_ekbe-shkzg EQ 'S'.
*          ADD gwa_ekbe-menge TO gwa_item-menge.
          gwa_item-menge = gwa_item-menge - gwa_ekbe-wesbs. "gwa_ekbe-menge.
        ELSE.
          ADD gwa_ekbe-wesbs TO gwa_item-menge. "gwa_ekbe-menge
*          gwa_item-menge = gwa_item-menge - gwa_ekbe-menge.
        ENDIF.
      ENDLOOP.

      READ TABLE gi_eket INTO gwa_eket WITH KEY ebeln = gwa_ekpo-ebeln
                                                ebelp = gwa_ekpo-ebelp.
      IF sy-subrc EQ 0.
        gwa_item-eindt = gwa_eket-eindt.
      ENDIF.

*      IF gwa_ekpo-pstyp NE 9.
      gwa_item-deskripsi = gwa_ekpo-txz01.
      APPEND gwa_item TO gi_item.
*      ELSE.
*        APPEND gwa_item TO gi_item.
*        READ TABLE gi_esll INTO gwa_esll WITH KEY packno = gwa_ekpo-packno.
*        IF sy-subrc EQ 0.
*          CLEAR : gwa_item-no_item.
*          LOOP AT gi_esll2 INTO gwa_esll2 WHERE packno EQ gwa_esll-sub_packno.
*            gwa_item-matnr      = gwa_esll2-srvpos.
*            gwa_item-deskripsi  = gwa_esll2-ktext1.
*            APPEND gwa_item TO gi_item.
*          ENDLOOP.
*        ENDIF.
*      ENDIF.

    ENDLOOP.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  F_PRINT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_print .

  DATA: lv_fname       TYPE rs38l_fnam,
        lwa_ctrl_param TYPE ssfctrlop,
        lwa_out_opt    TYPE ssfcompop,
        lw_tdsfname    TYPE tdsfname.

  READ TABLE gi_lfa1 INTO gwa_lfa1 INDEX 1. "add by ivan ER1K925407
  IF gwa_lfa1-land1 NE 'ID'.
*  IF gwa_lfa1-ktokk = 'AP02' AND gwa_lfa1-land1 NE 'ID'.
    lw_tdsfname = 'ZSF_MME006_EN'.
  ELSE.
    lw_tdsfname = 'ZSF_MME006'. "add by ivan ER1K925407
  ENDIF.


  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
*     formname           = 'ZSF_MME006'
      formname           = lw_tdsfname
    IMPORTING
      fm_name            = lv_fname
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*    lwa_ctrl_param-no_dialog  = 'X'.
*  lwa_ctrl_param-getotf     = 'X'.
*  lwa_ctrl_param-preview    = 'X'.
*  lwa_out_opt-tdnoprint  = 'X'.
**  lwa_out_opt-tdnoprev   = 'X'.
*  lwa_out_opt-tddest     = 'ab'.

  CALL FUNCTION lv_fname
    EXPORTING
      control_parameters = lwa_ctrl_param
      output_options     = lwa_out_opt
      gwa_header         = gwa_header
      gi_item            = gi_item
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  IF sy-subrc NE 0 .
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
  ENDIF .

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  F_SEND_MAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_NAST  text
*      -->P_LV_EMAIL  text
*----------------------------------------------------------------------*
FORM f_send_mail  USING fu_nast STRUCTURE nast
                        fu_email.
  DATA: lv_fmname       TYPE rs38l_fnam,
        lwa_options     TYPE ssfcompop,
        lwa_control     TYPE ssfctrlop,
        lwa_output_info TYPE ssfcrescl.

  DATA :
         lv_file TYPE  XSTRING,
         lv_size  TYPE i,
         li_otf   TYPE TABLE OF itcoo,
         li_lines TYPE TABLE OF tline WITH HEADER LINE.

  DATA : li_usr21   TYPE STANDARD TABLE OF usr21 WITH HEADER LINE,
         li_addr_cc TYPE STANDARD TABLE OF adr6 WITH HEADER LINE.

  DATA : li_record    LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         li_objpack   LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
         li_objtxt    TYPE TABLE OF solisti1,
         li_objbin    LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         li_reclist   LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
         lwa_objtxt   TYPE solisti1,
         lwa_objhead  TYPE soli_tab,
         lwa_doc_chng TYPE sodocchgi1,
         lwa_buffer   TYPE string,
         lv_lines_txt TYPE i,
         lv_lines_bin TYPE i,
         lv_subject   TYPE so_obj_des.

*  Get Function Module name base on SmartForms name
  lwa_control-no_dialog  = 'X'.
  lwa_control-getotf     = 'X'.
  lwa_control-preview    = space.
  lwa_options-tdnoprint  = 'X'.
  lwa_options-tdnoprev   = 'X'.
  lwa_options-tddest     = fu_nast-ldest.

  break abyor.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZSF_MME006'
    IMPORTING
      fm_name            = lv_fmname
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.

  CALL FUNCTION lv_fmname
    EXPORTING
      control_parameters = lwa_control
      output_options     = lwa_options
      user_settings      = space
      gwa_header         = gwa_header
      gi_item            = gi_item
    IMPORTING
      job_output_info    = lwa_output_info
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  IF sy-subrc NE 0 .
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
  ENDIF .

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  li_otf[] = lwa_output_info-otfdata[].
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
*      max_linewidth         = 999
      max_linewidth         = 132
    IMPORTING
      bin_filesize          = lv_size
      BIN_FILE              = lv_file "  XSTRING
    TABLES
      otf                   = li_otf
      lines                 = li_lines
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      OTHERS                = 4.

"remark to avoid inconsistencies data in pdf
*  LOOP AT li_lines.
*    TRANSLATE li_lines USING '~'.
*    CONCATENATE lwa_buffer li_lines INTO lwa_buffer SEPARATED BY space.
*  ENDLOOP.
*  TRANSLATE lwa_buffer USING '~'.

  IF lv_file IS NOT INITIAL.
  CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'
    EXPORTING
*     FROM_CODEPAGE       = '8500'
      in_xstring          = lv_file
      OUT_LEN             = LV_SIZE
    IMPORTING
      OUT_STRING          = lwa_buffer
            .

*  lwa_buffer = lv_file.
  ENDIF.
  DO.
    li_record = lwa_buffer.
    APPEND li_record.
    SHIFT lwa_buffer LEFT BY 255 PLACES.
    IF lwa_buffer IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

  REFRESH : li_reclist,
            li_objtxt,
            li_objbin,
            li_objpack.

  CLEAR lwa_objhead[].

  li_objbin[] = li_record[].

* Populate the subject/generic message attributes
  CONCATENATE 'Purchase Order No.' fu_nast-objky INTO lv_subject SEPARATED BY space.
  lwa_doc_chng-obj_langu   = sy-langu.
  lwa_doc_chng-obj_name    = 'SAPRPT'.
  lwa_doc_chng-obj_descr   = lv_subject .
  lwa_doc_chng-sensitivty  = 'F'.

* Create body email
  PERFORM f_text_body_email TABLES li_objtxt.
* Main text
  CLEAR lv_lines_txt.
  DESCRIBE TABLE li_objtxt LINES lv_lines_txt.
  CLEAR li_objpack-transf_bin.
  li_objpack-head_start   = 1.
  li_objpack-head_num     = 0.
  li_objpack-body_start   = 1.
  li_objpack-body_num     = lv_lines_txt.
  li_objpack-doc_type     = 'RAW'.
  APPEND li_objpack.

* Attachment (PDF-attachment)
  li_objpack-transf_bin   = 'X'.
  li_objpack-head_start   = 1.
  li_objpack-head_num     = 0.
  li_objpack-body_start   = 1.
  DESCRIBE TABLE li_objbin LINES lv_lines_bin.
  READ TABLE li_objbin INDEX lv_lines_bin.
  li_objpack-doc_size     =  lv_lines_bin * 255 .
  li_objpack-body_num     = lv_lines_bin.
  li_objpack-doc_type     = 'PDF'.
  li_objpack-obj_name     = 'smart'.
  li_objpack-obj_descr    = p_docno.
  APPEND li_objpack.

* Recipient: vendor mail
  CLEAR li_reclist.
  li_reclist-receiver     = fu_email. "'rizqimiftah@gmail.com'.
  li_reclist-rec_type     = 'U'.
  li_reclist-com_type     = ''.
  APPEND li_reclist.

*  cc: for eksekutor
  SELECT * INTO TABLE li_usr21
    FROM usr21
    WHERE bname EQ sy-uname.

  SELECT * INTO TABLE li_addr_cc
       FROM adr6
       FOR ALL ENTRIES IN li_usr21
       WHERE addrnumber EQ li_usr21-addrnumber
         AND persnumber EQ li_usr21-persnumber.  "add by rachmad 25112015

  DELETE ADJACENT DUPLICATES FROM li_addr_cc COMPARING addrnumber.
  READ TABLE li_addr_cc INDEX 1.

  CLEAR li_reclist.
  li_reclist-receiver     = li_addr_cc-smtp_addr.
  li_reclist-rec_type     = 'U'.
  li_reclist-copy         = 'X'.
  li_reclist-com_type     = ''.
  APPEND li_reclist.

* Send email
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = lwa_doc_chng
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = li_objpack
      object_header              = lwa_objhead
      contents_bin               = li_objbin
      contents_txt               = li_objtxt
      receivers                  = li_reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.

  IF sy-subrc = 0.
    MESSAGE 'Send Email is success' TYPE 'S'.
  ELSE.
    MESSAGE 'Send Email is fail' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  F_TEXT_BODY_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_OBJTXT  text
*----------------------------------------------------------------------*
FORM f_text_body_email  TABLES  ft_objtxt STRUCTURE solisti1.

  DATA lwa_objtxt TYPE solisti1.

  CLEAR lwa_objtxt.
  CONCATENATE 'Tanggal      :' gwa_header-date_txt
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  lwa_objtxt-line = 'Perihal      : Penyerahan (Supply) Barang'.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  lwa_objtxt-line = ''.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  lwa_objtxt-line = 'Kepada Yth'.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  lwa_objtxt-line = gwa_header-ven_name1.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  CONCATENATE gwa_header-ven_street gwa_header-ven_house_num1
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  CONCATENATE gwa_header-ven_city2 gwa_header-ven_city1
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  CONCATENATE gwa_header-ven_bezei gwa_header-ven_landx
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = 'Dengan hormat,'.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = ''.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  CONCATENATE 'Menunjuk Purchase Order(PO) nomor' gwa_header-ebeln
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  CLEAR lwa_objtxt.
  CONCATENATE 'Tanggal' gwa_header-bedat_txt
  INTO lwa_objtxt SEPARATED BY space.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = 'Perlu kami ingatkan bahwa batas waktu pengiriman barang yang ditetapkan'.
  APPEND lwa_objtxt TO ft_objtxt.
  lwa_objtxt-line = 'dalam PO tersebut diatas, akan segera berakhir.'.
  APPEND lwa_objtxt TO ft_objtxt.
  lwa_objtxt-line = 'Detail informasi terdapat dalam file terlampir'.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = ''.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = gwa_header-com_butxt.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = gwa_header-com_dept.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = ''.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = gwa_header-com_ypic.
  APPEND lwa_objtxt TO ft_objtxt.

  lwa_objtxt-line = gwa_header-com_ypos.
  APPEND lwa_objtxt TO ft_objtxt.

ENDFORM.                    " F_TEXT_BODY_EMAIL

*&---------------------------------------------------------------------*
*&      Form  F_GET_MONTH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GWA_VBRK_FKDAT  text
*      -->P_GWA_HEADER_TANGGAL_TRANSAKSI  text
*----------------------------------------------------------------------*
FORM f_get_month  USING    fu_date
                           fu_text.

  DATA : lv_month(10) TYPE c.

  CLEAR : lv_month.
  CASE fu_date+4(2).
    WHEN '01'.
      lv_month = 'Januari'.
    WHEN '02'.
      lv_month = 'Februari'.
    WHEN '03'.
      lv_month = 'Maret'.
    WHEN '04'.
      lv_month = 'April'.
    WHEN '05'.
      lv_month = 'Mei'.
    WHEN '06'.
      lv_month = 'Juni'.
    WHEN '07'.
      lv_month = 'Juli'.
    WHEN '08'.
      lv_month = 'Agustus'.
    WHEN '09'.
      lv_month = 'September'.
    WHEN '10'.
      lv_month = 'Oktober'.
    WHEN '11'.
      lv_month = 'November'.
    WHEN '12'.
      lv_month = 'Desember'.
    WHEN OTHERS.
  ENDCASE.

  CONCATENATE fu_date+6(2) lv_month fu_date(4)
  INTO fu_text SEPARATED BY space.

ENDFORM.                    " F_GET_MONTH
*&---------------------------------------------------------------------*
*&      Form  F_CONVERT_TO_UPPER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GWA_HEADER_COM_BEZEI  text
*----------------------------------------------------------------------*
FORM f_convert_to_upper  USING fu_text.

  CALL FUNCTION 'ISP_CONVERT_FIRSTCHARS_TOUPPER'
    EXPORTING
      input_string  = fu_text
*     SEPARATORS    = ' -.,;:'
    IMPORTING
      output_string = fu_text.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  F_SEND_MAIL_NOCOMMIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_NAST  text
*      -->P_LV_EMAIL  text
*----------------------------------------------------------------------*
FORM f_send_mail_nocommit  USING     fu_nast STRUCTURE nast
                        fu_email.
  DATA: lv_fmname       TYPE rs38l_fnam,
        lwa_options     TYPE ssfcompop,
        lwa_control     TYPE ssfctrlop,
        lwa_output_info TYPE ssfcrescl.

  DATA : li_usr21   TYPE STANDARD TABLE OF usr21 WITH HEADER LINE,
         li_addr_cc TYPE STANDARD TABLE OF adr6 WITH HEADER LINE.

  DATA : lt_bapiret2 TYPE TABLE OF bapiret2 WITH HEADER LINE,
         lt_email    TYPE TABLE OF bapiadsmtp WITH HEADER LINE.

  DATA :

         lv_file TYPE  XSTRING,
         lv_size  TYPE i,
         li_otf   TYPE TABLE OF itcoo,
         li_lines TYPE TABLE OF tline WITH HEADER LINE.

  DATA : li_record    LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         li_objpack   LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
         li_objtxt    TYPE TABLE OF solisti1,
         li_objbin    LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         li_reclist   LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
         lwa_objtxt   TYPE solisti1,
         lwa_objhead  TYPE soli_tab,
         lwa_doc_chng TYPE sodocchgi1,
         lwa_buffer   TYPE string,
         lv_lines_txt TYPE i,
         lv_lines_bin TYPE i,
         lv_subject   TYPE so_obj_des.

*  Get Function Module name base on SmartForms name
  lwa_control-no_dialog  = 'X'.
  lwa_control-getotf     = 'X'.
  lwa_control-preview    = space.
  lwa_options-tdnoprint  = 'X'.
  lwa_options-tdnoprev   = 'X'.
  lwa_options-tddest     = fu_nast-ldest.

  break abyor.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZSF_MME006'
    IMPORTING
      fm_name            = lv_fmname
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.

  CALL FUNCTION lv_fmname
    EXPORTING
      control_parameters = lwa_control
      output_options     = lwa_options
      user_settings      = space
      gwa_header         = gwa_header
      gi_item            = gi_item
    IMPORTING
      job_output_info    = lwa_output_info
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.

  IF sy-subrc NE 0 .
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
  ENDIF .

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  li_otf[] = lwa_output_info-otfdata[].
*  CALL FUNCTION 'CONVERT_OTF'
*    EXPORTING
*      format                = 'PDF'
*      max_linewidth         = 132
*    IMPORTING
*      bin_filesize          = lv_size
*    TABLES
*      otf                   = li_otf
*      lines                 = li_lines
*    EXCEPTIONS
*      err_max_linewidth     = 1
*      err_format            = 2
*      err_conv_not_possible = 3
*      OTHERS                = 4.

*  LOOP AT li_lines.
*    TRANSLATE li_lines USING '~'.
*    CONCATENATE lwa_buffer li_lines INTO lwa_buffer.
*  ENDLOOP.
*  TRANSLATE lwa_buffer USING '~'.

  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
*      max_linewidth         = 999
      max_linewidth         = 132
    IMPORTING
      bin_filesize          = lv_size
      BIN_FILE              = lv_file "  XSTRING
    TABLES
      otf                   = li_otf
      lines                 = li_lines
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      OTHERS                = 4.

"remark to avoid inconsistencies data in pdf
*  LOOP AT li_lines.
*    TRANSLATE li_lines USING '~'.
*    CONCATENATE lwa_buffer li_lines INTO lwa_buffer SEPARATED BY space.
*  ENDLOOP.
*  TRANSLATE lwa_buffer USING '~'.

  IF lv_file IS NOT INITIAL.
  CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'
    EXPORTING
*     FROM_CODEPAGE       = '8500'
      in_xstring          = lv_file
      OUT_LEN             = LV_SIZE
    IMPORTING
      OUT_STRING          = lwa_buffer
            .
*  lwa_buffer = lv_file.
  ENDIF.

  DO.
    li_record = lwa_buffer.
    APPEND li_record.
    SHIFT lwa_buffer LEFT BY 255 PLACES.
    IF lwa_buffer IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

  REFRESH : li_reclist,
            li_objtxt,
            li_objbin,
            li_objpack.

  CLEAR lwa_objhead[].

  li_objbin[] = li_record[].

* Populate the subject/generic message attributes
  CONCATENATE 'Purchase Order No.' fu_nast-objky INTO lv_subject SEPARATED BY space.
  lwa_doc_chng-obj_langu   = sy-langu.
  lwa_doc_chng-obj_name    = 'SAPRPT'.
  lwa_doc_chng-obj_descr   = lv_subject .
  lwa_doc_chng-sensitivty  = 'F'.

* Create body email
  PERFORM f_text_body_email TABLES li_objtxt.
* Main text
  CLEAR lv_lines_txt.
  DESCRIBE TABLE li_objtxt LINES lv_lines_txt.
  CLEAR li_objpack-transf_bin.
  li_objpack-head_start   = 1.
  li_objpack-head_num     = 0.
  li_objpack-body_start   = 1.
  li_objpack-body_num     = lv_lines_txt.
  li_objpack-doc_type     = 'RAW'.
  APPEND li_objpack.

* Attachment (PDF-attachment)
  li_objpack-transf_bin   = 'X'.
  li_objpack-head_start   = 1.
  li_objpack-head_num     = 0.
  li_objpack-body_start   = 1.
  DESCRIBE TABLE li_objbin LINES lv_lines_bin.
  READ TABLE li_objbin INDEX lv_lines_bin.
  li_objpack-doc_size     =  lv_lines_bin * 255 .
  li_objpack-body_num     = lv_lines_bin.
  li_objpack-doc_type     = 'PDF'.
  li_objpack-obj_name     = 'smart'.
  li_objpack-obj_descr    = p_docno.
  APPEND li_objpack.

* Recipient: vendor mail
  CLEAR li_reclist.
  li_reclist-receiver     = fu_email. "'rizqimiftah@gmail.com'.
  li_reclist-rec_type     = 'U'.
  li_reclist-com_type     = ''.
  APPEND li_reclist.

*  CC: For Eksekutor
  CALL FUNCTION 'BAPI_USER_GET_DETAIL'
    EXPORTING
      username = sy-uname
    TABLES
      return   = lt_bapiret2
      addsmtp  = lt_email.


  READ TABLE lt_email INDEX 1.

  CLEAR li_reclist.
  li_reclist-receiver     = lt_email-e_mail.
  li_reclist-rec_type     = 'U'.
  li_reclist-copy         = 'X'.
  APPEND li_reclist.

* Send email
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = lwa_doc_chng
      put_in_outbox              = 'X'
      commit_work                = ''
    TABLES
      packing_list               = li_objpack
      object_header              = lwa_objhead
      contents_bin               = li_objbin
      contents_txt               = li_objtxt
      receivers                  = li_reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.

  IF sy-subrc = 0.
    MESSAGE 'Send Email is success' TYPE 'S'.
  ELSE.
    MESSAGE 'Send Email is fail' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

ENDFORM.


Well, thats it for today, and see you again.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Hi, I would like to know what kind of object are:

    DATA : gwa_header TYPE zcl_mme006=>ty_header,
    gi_item TYPE zcl_mme006=>ty_item_tab,
    gwa_item TYPE zcl_mme006=>ty_item.

    And how can I call or populate NAST tab in memory.

    ReplyDelete