-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzcl_pp_batch_formatter.aclass
396 lines (344 loc) · 16.7 KB
/
zcl_pp_batch_formatter.aclass
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
CLASS zcl_pp_batch_formatter DEFINITION
PUBLIC
ABSTRACT
CREATE PUBLIC .
PUBLIC SECTION.
DATA: plant TYPE werks_d READ-ONLY,
material TYPE matnr READ-ONLY,
material_type TYPE mtart READ-ONLY.
CLASS-METHODS: get_instance IMPORTING i_plant TYPE werks_d
i_material TYPE matnr
RETURNING VALUE(rr_batch_formatter) TYPE REF TO zcl_pp_batch_formatter.
METHODS:
constructor IMPORTING i_plant TYPE werks_d
i_material TYPE matnr OPTIONAL
i_material_type TYPE mtart OPTIONAL,
check ABSTRACT IMPORTING is_entry TYPE zpps004
RAISING cx_bapi_error,
get_batch ABSTRACT IMPORTING i_verid TYPE verid OPTIONAL
RETURNING VALUE(r_batch) TYPE string
RAISING cx_bapi_error,
get_batch_template ABSTRACT IMPORTING i_mutable_part TYPE zbatch_mutable_part
RETURNING VALUE(r_batch_naming) TYPE zbatch_naming_rule,
set_batch_template ABSTRACT,
change_mutable_part ABSTRACT IMPORTING i_mutable_part TYPE zbatch_mutable_part
RETURNING VALUE(r_mutable_part) TYPE zbatch_mutable_part,
get_reference_date ABSTRACT RETURNING VALUE(r_date) TYPE dats,
get_reference_time ABSTRACT RETURNING VALUE(r_time) TYPE tims,
get_production_version ABSTRACT RETURNING VALUE(r_verid) TYPE verid,
get_formatter RETURNING VALUE(rr_formatter) TYPE REF TO zcl_pp_batch_formatter,
get_my_rule_type ABSTRACT RETURNING VALUE(r_rule_type) TYPE zbatch_rule_type,
to_string RETURNING VALUE(r_mytext) TYPE string.
PROTECTED SECTION.
DATA: formatter TYPE zbatch_formatter,
batch_naming TYPE zbatch_naming_rule,
prefix TYPE zbatch_prefix,
naming_template TYPE zbatch_naming_rule, "To be populated in the children.
mutable_part TYPE zbatch_mutable_part,
last_mutable_part TYPE zbatch_last_mutable_part,
implementing_time TYPE zimplementing_time,
reference_date TYPE d,
reference_time TYPE t,
since TYPE timestamp.
METHODS:
set_attributes RAISING cx_bapi_error,
set_material_type RAISING cx_sql_exception
cx_parameter_invalid,
set_bath_settings RAISING cx_sql_exception
cx_parameter_invalid,
set_template IMPORTING i_rule_type TYPE zbatch_rule_type
RAISING cx_sql_exception "To retrieve the template related with each child.
cx_parameter_invalid.
PRIVATE SECTION.
TYPES: BEGIN OF y_settings,
zzfrom TYPE timestamp,
batch_naming TYPE zbatch_naming_rule,
formatter TYPE zbatch_formatter,
prefix TYPE zbatch_prefix,
mutable_part TYPE zbatch_mutable_part,
last_mutable_part TYPE zbatch_last_mutable_part,
* INI MOD - Denilson P.Pina - 08.02.2019
impl_time TYPE zimplementing_time,
* FIM MOD - Denilson P.Pina - 08.02.2019
END OF y_settings.
METHODS:
query_by_material RAISING cx_parameter_invalid
cx_sql_exception,
query_by_material_type RAISING cx_parameter_invalid
cx_sql_exception.
ENDCLASS.
CLASS ZCL_PP_BATCH_FORMATTER IMPLEMENTATION.
METHOD constructor.
IF i_material IS INITIAL AND i_material_type IS INITIAL.
RETURN.
ENDIF.
me->plant = i_plant.
me->material = i_material.
me->material_type = i_material_type.
ENDMETHOD.
METHOD get_formatter.
IF me->formatter IS INITIAL.
TRY.
me->set_attributes( ).
* CATCH cx_bapi_error. " BAPI Error Exception: Migrated Error Messages Type "E"
CATCH cx_bapi_error INTO DATA(lx_error).
READ TABLE lx_error->status ASSIGNING FIELD-SYMBOL(<ls_return>) INDEX 1.
IF sy-subrc = 0.
MESSAGE
ID <ls_return>-id
TYPE <ls_return>-type
NUMBER <ls_return>-number
WITH <ls_return>-message_v1 <ls_return>-message_v2 <ls_return>-message_v3 <ls_return>-message_v4.
ENDIF.
RETURN.
ENDTRY.
ENDIF.
IF me->formatter IS NOT INITIAL.
CREATE OBJECT rr_formatter TYPE (me->formatter)
EXPORTING
i_plant = me->plant
i_material = me->material.
ENDIF.
ENDMETHOD.
METHOD get_instance.
DATA(lr_factory) = NEW zcl_pp_batch_factory(
i_plant = i_plant
i_material = i_material
).
rr_batch_formatter = lr_factory->get_formatter( ).
ENDMETHOD.
METHOD query_by_material.
DATA:
ls_result TYPE y_settings,
l_sql_material TYPE string,
l_current_time TYPE timestamp.
GET TIME STAMP FIELD l_current_time.
l_sql_material = `SELECT ZZFROM, BATCH_NAMING, FORMATTER, PREFIX, MUTABLE_PART, LAST_MUTABLE_PART, IMPL_TIME ` &&
`FROM ZPPT001 AS PLAN ` &&
`INNER JOIN ZPPT002 AS RULE ` &&
`ON RULE.MANDT = PLAN.MANDT ` &&
`AND RULE.RULE_TYPE = PLAN.RULE_TYPE ` &&
`WHERE PLAN.MANDT = '` && sy-mandt && `' ` &&
`AND PLANT = '` && me->plant && `' ` &&
`AND MTART = '` && me->material_type && `' ` && "This implies that the method set_material_type() is called before this one.
`AND MATNR = '` && me->material && `' ` &&
`AND ZZFROM <= ` && l_current_time &&
` AND ZZTO >= ` && l_current_time &&
` AND ACTIVE = '` && abap_true && `' ` &&
` AND DELETED = '` && abap_false && `' `.
TRY.
DATA(lr_query) = NEW cl_sql_statement(
* con_ref =
* tab_name_for_trace =
)->execute_query(
statement = l_sql_material
* hold_cursor = space
).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->set_param_struct(
EXPORTING
struct_ref = REF #( ls_result ) " Reference to Output Variable
* corresponding_fields = " List of Fields of the Output Structure
* lob_fields = " LOB Fields
).
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->next( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid_type. " Parameter with Invalid Type
lr_query->close( ).
me->batch_naming = ls_result-batch_naming.
me->formatter = ls_result-formatter.
me->prefix = ls_result-prefix.
me->mutable_part = ls_result-mutable_part.
me->last_mutable_part = ls_result-last_mutable_part.
CONVERT DATE sy-datlo TIME ls_result-impl_time INTO TIME STAMP DATA(l_timestamp) TIME ZONE 'UTC'. "In order to keep the UTC value of "Implementing time"
CONVERT TIME STAMP l_timestamp TIME ZONE sy-zonlo INTO DATE DATA(l_date) TIME me->implementing_time.
me->since = ls_result-zzfrom.
CATCH cx_sql_exception INTO DATA(lx_sql_exception).
RAISE EXCEPTION lx_sql_exception.
CATCH cx_parameter_invalid INTO DATA(lx_parameter_invalid).
RAISE EXCEPTION lx_parameter_invalid.
ENDTRY.
ENDMETHOD.
METHOD query_by_material_type.
DATA:
ls_result TYPE y_settings,
l_sql_material_type TYPE string,
l_current_time TYPE timestamp.
GET TIME STAMP FIELD l_current_time.
l_sql_material_type = `SELECT ZZFROM, BATCH_NAMING, FORMATTER, PREFIX, MUTABLE_PART, LAST_MUTABLE_PART, IMPL_TIME ` &&
`FROM ZPPT001 AS PLAN ` &&
`INNER JOIN ZPPT002 AS RULE ` &&
`ON RULE.MANDT = PLAN.MANDT ` &&
`AND RULE.RULE_TYPE = PLAN.RULE_TYPE ` &&
`WHERE PLAN.MANDT = '` && sy-mandt && `' ` &&
`AND PLANT = '` && me->plant && `' ` &&
`AND MTART = '` && me->material_type && `' ` && "This implies that the method set_material_type() is called before this one.
`AND MATNR = '` && space && `' ` &&
`AND ZZFROM <= ` && l_current_time &&
` AND ZZTO >= ` && l_current_time &&
` AND ACTIVE = '` && abap_true && `' ` &&
` AND DELETED = '` && abap_false && `' `.
TRY.
DATA(lr_query) = NEW cl_sql_statement(
* con_ref =
* tab_name_for_trace =
)->execute_query(
statement = l_sql_material_type
* hold_cursor = space
).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->set_param_struct(
EXPORTING
struct_ref = REF #( ls_result ) " Reference to Output Variable
* corresponding_fields = " List of Fields of the Output Structure
* lob_fields = " LOB Fields
).
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->next( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid_type. " Parameter with Invalid Type
lr_query->close( ).
me->batch_naming = ls_result-batch_naming.
me->formatter = ls_result-formatter.
me->prefix = ls_result-prefix.
me->mutable_part = ls_result-mutable_part.
me->last_mutable_part = ls_result-last_mutable_part.
me->implementing_time = ls_result-impl_time.
me->last_mutable_part = ls_result-last_mutable_part.
CONVERT DATE sy-datlo TIME ls_result-impl_time INTO TIME STAMP DATA(l_timestamp) TIME ZONE 'UTC'. "In order to keep the UTC value of "Implementing time"
CONVERT TIME STAMP l_timestamp TIME ZONE sy-zonlo INTO DATE DATA(l_date) TIME me->implementing_time.
me->since = ls_result-zzfrom.
CATCH cx_sql_exception INTO DATA(lx_sql_exception).
RAISE EXCEPTION lx_sql_exception.
CATCH cx_parameter_invalid INTO DATA(lx_parameter_invalid).
RAISE EXCEPTION lx_parameter_invalid.
ENDTRY.
ENDMETHOD.
METHOD set_attributes.
TRY.
IF me->material IS NOT INITIAL.
me->set_material_type( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
ENDIF.
me->set_bath_settings( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
CATCH cx_sql_exception ##NO_HANDLER
cx_parameter_invalid.
ENDTRY.
me->reference_date = sy-datlo.
me->reference_time = sy-timlo.
GET TIME STAMP FIELD DATA(l_current_timestamp).
CONVERT DATE sy-datlo TIME me->implementing_time INTO TIME STAMP DATA(l_ref_timestamp) TIME ZONE sy-zonlo.
IF l_ref_timestamp < l_current_timestamp.
me->reference_date = me->reference_date + 1.
me->reference_time = me->implementing_time.
ENDIF.
ENDMETHOD.
METHOD set_bath_settings.
TRY.
query_by_material( ).
IF me->batch_naming IS INITIAL.
query_by_material_type( ).
ENDIF.
CATCH cx_sql_exception INTO DATA(lx_sql_exception).
RAISE EXCEPTION lx_sql_exception.
CATCH cx_parameter_invalid INTO DATA(lx_parameter_invalid).
RAISE EXCEPTION lx_parameter_invalid.
ENDTRY.
CONVERT TIME STAMP me->since TIME ZONE sy-zonlo INTO DATE DATA(l_date) TIME me->reference_time.
ENDMETHOD.
METHOD set_material_type.
DATA l_sql_statement TYPE string.
l_sql_statement = `SELECT MTART ` &&
` FROM MARA ` &&
` WHERE MANDT = '` && sy-mandt && `' ` &&
`AND MATNR = '` && me->material && `' `.
TRY.
DATA(lr_query) = NEW cl_sql_statement(
* con_ref =
* tab_name_for_trace =
)->execute_query(
statement = l_sql_statement
* hold_cursor = space
).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->set_param(
EXPORTING
data_ref = REF #( me->material_type ) " Reference to Output Variable
* pos = " 2 byte integer (signed)
* ind_ref = " Reference to Indicator Variable
* is_lob = space " Is an LOB Column References on the Database?
).
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->next( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid_type. " Parameter with Invalid Type
lr_query->close( ).
CATCH cx_sql_exception INTO DATA(lx_sql_exception).
RAISE EXCEPTION lx_sql_exception.
CATCH cx_parameter_invalid INTO DATA(lx_parameter_invalid).
RAISE EXCEPTION lx_parameter_invalid.
ENDTRY.
ENDMETHOD.
METHOD set_template.
DATA l_sql_statement TYPE string.
l_sql_statement = `SELECT BATCH_NAMING ` &&
` FROM ZPPT002 ` &&
` WHERE MANDT = '` && sy-mandt && `' ` &&
`AND RULE_TYPE = '` && i_rule_type && `' `.
TRY.
DATA(lr_query) = NEW cl_sql_statement(
* con_ref =
* tab_name_for_trace =
)->execute_query(
statement = l_sql_statement
* hold_cursor = space
).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->set_param(
EXPORTING
data_ref = REF #( me->naming_template ) " Reference to Output Variable
* pos = " 2 byte integer (signed)
* ind_ref = " Reference to Indicator Variable
* is_lob = space " Is an LOB Column References on the Database?
).
* CATCH cx_parameter_invalid. " Superclass for Parameter Error
lr_query->next( ).
* CATCH cx_sql_exception. " Exception Class for SQL Error
* CATCH cx_parameter_invalid_type. " Parameter with Invalid Type
lr_query->close( ).
CATCH cx_sql_exception INTO DATA(lx_sql_exception).
RAISE EXCEPTION lx_sql_exception.
CATCH cx_parameter_invalid INTO DATA(lx_parameter_invalid).
RAISE EXCEPTION lx_parameter_invalid.
ENDTRY.
ENDMETHOD.
METHOD to_string.
IF me->material_type IS INITIAL.
TRY.
me->set_attributes( ).
* CATCH cx_bapi_error. " BAPI Error Exception: Migrated Error Messages Type "E"
CATCH cx_bapi_error INTO DATA(lx_error).
READ TABLE lx_error->status ASSIGNING FIELD-SYMBOL(<ls_return>) INDEX 1.
IF sy-subrc = 0.
MESSAGE
ID <ls_return>-id
TYPE <ls_return>-type
NUMBER <ls_return>-number
WITH <ls_return>-message_v1 <ls_return>-message_v2 <ls_return>-message_v3 <ls_return>-message_v4.
ENDIF.
RETURN.
ENDTRY.
ENDIF.
r_mytext = me->plant && '/' && me->material_type && '/' && me->material && '/' && me->batch_naming && '/' && me->formatter &&
'/' && me->prefix.
ENDMETHOD.
ENDCLASS.