DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_WORKFLOW

Source


1 PACKAGE BODY inv_workflow AS
2   /* $Header: INVFBWFB.pls 120.6 2011/04/18 21:08:37 uphadtar ship $ */
3 
4   --
5   --  Global Variables
6   --
7   g_version_printed   BOOLEAN      := FALSE;
8   g_pkg_name CONSTANT VARCHAR2(50) := 'INV_WORKFLOW';
9 
10   PROCEDURE print_debug(p_message IN VARCHAR2, p_module IN VARCHAR2) IS
11   BEGIN
12     IF NOT g_version_printed THEN
13       inv_log_util.TRACE('$Header: INVFBWFB.pls 120.6 2011/04/18 21:08:37 uphadtar ship $', g_pkg_name, 9);
14       g_version_printed  := TRUE;
15     END IF;
16 
17     inv_log_util.TRACE(p_message, g_pkg_name || '.' || p_module);
18   END;
19 
20   FUNCTION call_generate_cogs(
21     c_fb_flex_num               IN            NUMBER DEFAULT 101
22   , c_ic_customer_id            IN            NUMBER DEFAULT NULL
23   , c_ic_item_id                IN            NUMBER DEFAULT NULL
24   , c_ic_order_header_id        IN            NUMBER DEFAULT NULL
25   , c_ic_order_line_id          IN            NUMBER DEFAULT NULL
26   , c_ic_order_type_id          IN            NUMBER DEFAULT NULL
27   , c_ic_sell_oper_unit         IN            NUMBER DEFAULT NULL
28   , c_v_ccid                    IN OUT NOCOPY NUMBER
29   , c_fb_flex_seg               IN OUT NOCOPY VARCHAR2
30   , c_fb_error_msg              IN OUT NOCOPY VARCHAR2
31   , c_ic_to_inv_organization_id IN            NUMBER DEFAULT NULL  -- Bug: 4474976.
32   )
33     RETURN BOOLEAN IS
34     l_success BOOLEAN := TRUE;
35   BEGIN
36     l_success  :=
37       inv_workflow.generate_cogs(
38         fb_flex_num                  => c_fb_flex_num
39       , ic_customer_id               => TO_CHAR(c_ic_customer_id)
40       , ic_item_id                   => TO_CHAR(c_ic_item_id)
41       , ic_order_header_id           => TO_CHAR(c_ic_order_header_id)
42       , ic_order_line_id             => TO_CHAR(c_ic_order_line_id)
43       , ic_order_type_id             => TO_CHAR(c_ic_order_type_id)
44       , ic_sell_oper_unit            => TO_CHAR(c_ic_sell_oper_unit)
45       , v_ccid                       => c_v_ccid
46       , fb_flex_seg                  => c_fb_flex_seg
47       , fb_error_msg                 => c_fb_error_msg
48       , ic_to_inv_organization_id    => TO_CHAR(c_ic_to_inv_organization_id)  -- Bug: 4474976.
49       );
50     RETURN l_success;
51   END call_generate_cogs;
52 
53   FUNCTION generate_cogs(
54     fb_flex_num               IN            NUMBER DEFAULT 101
55   , ic_customer_id            IN            VARCHAR2 DEFAULT NULL
56   , ic_item_id                IN            VARCHAR2 DEFAULT NULL
57   , ic_order_header_id        IN            VARCHAR2 DEFAULT NULL
58   , ic_order_line_id          IN            VARCHAR2 DEFAULT NULL
59   , ic_order_type_id          IN            VARCHAR2 DEFAULT NULL
60   , ic_sell_oper_unit         IN            VARCHAR2 DEFAULT NULL
61   , v_ccid                    IN OUT NOCOPY NUMBER
62   , fb_flex_seg               IN OUT NOCOPY VARCHAR2
63   , fb_error_msg              IN OUT NOCOPY VARCHAR2
64   , ic_to_inv_organization_id IN            NUMBER DEFAULT NULL  -- Bug: 4474976.
65   )
66     RETURN BOOLEAN IS
67     v_item_cogs          NUMBER         := NULL;
68     v_organization_cogs  NUMBER         := NULL;
69     v_order_type_cogs    NUMBER;
70     v_concat_ids         VARCHAR2(2000);
71     v_concat_segs        VARCHAR2(2000);
72     v_concat_descrs      VARCHAR2(2000);
73     error_message        VARCHAR2(500);
74     v_generate_success   BOOLEAN        := TRUE;
75     v_itemkey            VARCHAR2(100);
76     ic_items_cogs        NUMBER         := NULL;
77     ic_organization_cogs NUMBER         := NULL;
78     ic_order_type_cogs   NUMBER         := NULL;
79     translated_mesg      VARCHAR2(1500) := NULL;
80     v_buffer             VARCHAR2(1000) := NULL;
81     l_err_pt             VARCHAR2(10)   := NULL;
82     v_doc_type_id        NUMBER         := NULL;
83     l_debug              NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
84     l_function_name      VARCHAR2(20)   := 'GENERATE_COGS';
85     --
86     -- Bug 6065114 added following variables for OPM purposes.
87     --
88     v_process_enabled_flag mtl_parameters.process_enabled_flag%TYPE := NULL;
89     v_requisition_line_id  NUMBER       := NULL;
90 
91     /* Begin Bug 11823085 */
92     l_gmf_patch_level 	 fnd_product_installations.patch_level%TYPE;
93 
94     CURSOR cur_gmf_patch_level IS
95     SELECT patch_level
96     FROM   fnd_product_installations
97     WHERE  application_id = 555;  --GMF product
98     /* End Bug 11823085 */
99 
100     -- Bug 8896018
101     l_chart_of_accounts_id NUMBER       := NULL;
102     l_ship_ou_id           NUMBER       := NULL;
103     l_order_src_id         NUMBER       := NULL;
104     -- End of Bug 8896018
105   BEGIN
106     l_err_pt            := '10';
107 
108     -- fnd_flex_workflow.debug_on; -- To print the FND Account Generator debug messages.
109 
110     IF (l_debug = 1) THEN
111       print_debug('FB_FLEX_NUM: ' || TO_CHAR(fb_flex_num), l_function_name);
112       print_debug('IC_CUSTOMER_ID: ' || ic_customer_id, l_function_name);
113       print_debug('IC_ITEM_ID: ' || ic_item_id, l_function_name);
114       print_debug('IC_ORDER_HEADER_ID: ' || ic_order_header_id, l_function_name);
115       print_debug('IC_ORDER_LINE_ID: ' || ic_order_line_id, l_function_name);
116       print_debug('IC_ORDER_TYPE_ID: ' || ic_order_type_id, l_function_name);
117       print_debug('IC_SELL_OPER_UNIT: ' || ic_sell_oper_unit, l_function_name);
118       print_debug('IC_TO_INV_ORGANIZATION_ID: ' || ic_to_inv_organization_id, l_function_name);
119       print_debug('V_CCID: ' || TO_CHAR(v_ccid), l_function_name);
120       print_debug('FB_FLEX_SEG: ' || fb_flex_seg, l_function_name);
121       print_debug('FB_ERROR_MSG: ' || fb_error_msg, l_function_name);
122       print_debug('Calling Workflow Initialize', l_function_name);
123     END IF;
124 
125     wf_item.clearcache;
126     v_itemkey           :=
127                          fnd_flex_workflow.initialize(appl_short_name => 'SQLGL', code => 'GL#', num => fb_flex_num, itemtype => 'INVFLXWF');
128     print_debug('Created itemkey :' || v_itemkey, l_function_name);
129 
130     IF (ic_items_cogs IS NULL AND (ic_item_id IS NOT NULL AND ic_to_inv_organization_id IS NOT NULL)) THEN
131       BEGIN
132         -- Bug: 4474976
133         -- Replaced the local variable ic_sell_oper_unit with ic_to_inv_organization_id
134         -- in the WHERE clause of the following SELECT Statment.
135         -- Removed NVL function from the SELECT statement.
136         SELECT cost_of_sales_account
137           INTO v_item_cogs
138           FROM mtl_system_items
139          WHERE inventory_item_id = ic_item_id
140            AND organization_id = ic_to_inv_organization_id;
141       EXCEPTION
142         WHEN NO_DATA_FOUND THEN
143           NULL;
144       /*
145             FND_MESSAGE.SET_NAME('FND', 'FLEX-BUILD INVALID VALUE');
146             FND_MESSAGE.SET_TOKEN('VALUE', IC_ITEM_ID);
147             FND_MESSAGE.SET_TOKEN('VSET_ID', '103099');
148             FB_ERROR_MSG := FND_MESSAGE.GET_ENCODED;
149             return FALSE; */
150       END;
151     END IF;
152 
153     IF (l_debug = 1) THEN
154       print_debug('IC_ITEMS_COGS: ' || v_item_cogs, l_function_name);
155     END IF;
156 
157     l_err_pt            := '10.5';
158 
159     /* Bug3393516 : When the profile Tax: Invoice Freight as Revenue = 'Yes', then there will be atleast 2 lines in
160     ra_customer_trx_lines_all (After Autoinoice Import) and the order line_id corresponding to the freight line will be 0.
161     As a result the following code (without the if statement) fails and INCIAP ends up giving a warning */
162     IF (ic_order_line_id <> 0) THEN
163       BEGIN
164         SELECT source_document_type_id
165           INTO v_doc_type_id
166           FROM oe_order_lines_all
167          WHERE line_id = TO_NUMBER(ic_order_line_id);
168       EXCEPTION
169         WHEN NO_DATA_FOUND THEN
170           fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
171           fnd_message.set_token('VALUE', ic_order_line_id);
172           fnd_message.set_token('VSET_ID', '000000');
173           fb_error_msg  := fnd_message.get_encoded;
174           RETURN FALSE;
175       END;
176     END IF;
177 
178     IF (l_debug = 1) THEN
179       print_debug('Found v_doc_type_id: ' || v_doc_type_id, l_function_name);
180     END IF;
181 
182     l_err_pt            := '20';
183 
184     /* Derive Source parameter IC_ORDER_TYPE_COGS */
185     /* If source document type ID = 10, the order is from an internal requisition.
186     Use receiving organization's purchasing accrual account instead of order type
187     COGS. */
188     IF (v_doc_type_id = 10) THEN
189       BEGIN
190         SELECT mp.ap_accrual_account, mp.process_enabled_flag, prl.requisition_line_id
191           INTO v_order_type_cogs, v_process_enabled_flag, v_requisition_line_id
192           FROM mtl_parameters mp, po_requisition_lines_all prl, oe_order_lines_all ool
193          WHERE ool.line_id = TO_NUMBER(ic_order_line_id)
194            AND prl.requisition_line_id = ool.source_document_line_id
195            AND mp.organization_id = prl.destination_organization_id;
196 
197         print_debug('Found v_acrual_acct: ' || v_order_type_cogs, l_function_name);
198       EXCEPTION
199         WHEN NO_DATA_FOUND THEN
200           fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
201           fnd_message.set_token('VALUE', ic_order_type_id);
202           fnd_message.set_token('VSET_ID', '103100');
203           fb_error_msg  := fnd_message.get_encoded;
204           RETURN FALSE;
205       END;
206 
207       /* Bug 11823085 */
208       OPEN  cur_gmf_patch_level;
209       FETCH cur_gmf_patch_level INTO l_gmf_patch_level;
210       CLOSE cur_gmf_patch_level;
211 
212       print_debug('GMF patch level: ' || l_gmf_patch_level, l_function_name);
213       print_debug('Process Enabled Flag: ' || v_process_enabled_flag, l_function_name);
214 
215       IF l_gmf_patch_level = '11i.GMF.L' THEN      /* Bug 11823085 */
216          --
217          -- Bug 6065114 added following for OPM purposes.
218          --
219          IF (NVL(v_process_enabled_flag,'N') = 'Y' AND v_requisition_line_id IS NOT NULL) THEN
220             -- for process enabled organization get AAP from po_req_distributions_all
221             BEGIN
222               SELECT accrual_account_id
223                 INTO v_order_type_cogs
224                 FROM po_req_distributions_all
225                WHERE requisition_line_id = v_requisition_line_id
226                  AND rownum < 2;
227                print_debug('for OPM Found v_acrual_acct: ' || v_order_type_cogs, l_function_name);
228             EXCEPTION
229                WHEN OTHERS THEN -- possible no_data_found or multiple_rows
230                  fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
231                  fnd_message.set_token('VALUE', ic_order_type_id||'-OPM');
232                  fnd_message.set_token('VSET_ID', '103100');
233                  fb_error_msg  := fnd_message.get_encoded;
234                  RETURN FALSE;
235              END;
236           END IF;
237           -- End bug 6065114
238       END IF;   /* Bug 11823085 */
239     ELSE
240       l_err_pt  := '20.5';
241        -- Bug 8896018, comment the following statements
242 
243      /* IF (ic_order_type_cogs IS NULL) THEN
244         BEGIN
245           SELECT NVL(cost_of_goods_sold_account, 0)
246             INTO v_order_type_cogs
247             FROM oe_transaction_types_all
248            WHERE transaction_type_id = TO_NUMBER(ic_order_type_id)a
249              AND 1 = 1;
250 
251           print_debug('Found v_order_type_cogs: ' || v_order_type_cogs, l_function_name);
252         EXCEPTION
253           WHEN NO_DATA_FOUND THEN
254             fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
255             fnd_message.set_token('VALUE', ic_order_type_id);
256             fnd_message.set_token('VSET_ID', '103100');
257             fb_error_msg  := fnd_message.get_encoded;
258             RETURN FALSE;
259         END;
260       END IF;*/
261       -- End of bug 8896018
262 
263       -- Bug 8896018
264        IF (ic_order_type_cogs IS NULL) AND (nvl(ic_order_line_id,0) <> 0) THEN
265         BEGIN
266           SELECT NVL(cost_of_goods_sold_account, 0)
267             INTO v_order_type_cogs
268             FROM oe_transaction_types_all
269            WHERE transaction_type_id = TO_NUMBER(ic_order_type_id)
270              AND 1 = 1;
271 
272           print_debug('Found v_order_type_cogs: ' || v_order_type_cogs, l_function_name);
273         EXCEPTION
274           WHEN NO_DATA_FOUND THEN
275             fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
276             fnd_message.set_token('VALUE', ic_order_type_id);
277             fnd_message.set_token('VSET_ID', '103100');
278             fb_error_msg  := fnd_message.get_encoded;
279             RETURN FALSE;
280         END;
281       END IF;
282 
283       IF (ic_order_type_cogs IS NULL) AND (nvl(ic_order_line_id,0) = 0) THEN
284         BEGIN
285           SELECT NVL(cost_of_goods_sold_account, 0)
286             INTO v_order_type_cogs
287             FROM oe_transaction_types_all
288            WHERE transaction_type_id = TO_NUMBER(ic_order_type_id)
289              AND 1 = 1;
290 
291           SELECT org_id, order_source_id
292             INTO l_ship_ou_id, l_order_src_id
293             FROM oe_order_headers_all
294            WHERE header_id = to_number(ic_order_header_id);
295 
296            IF (nvl(l_order_src_id,0) = 10) AND (v_order_type_cogs <> 0 )  THEN
297               SELECT chart_of_accounts_id
298                 INTO l_chart_of_accounts_id
299                 FROM gl_code_combinations
300                WHERE code_combination_id = v_order_type_cogs;
301 
302               IF (l_chart_of_accounts_id <> nvl(fb_flex_num, -1)) AND (l_ship_ou_id is NOT NULL) THEN
303                SELECT NVL(freight_code_combination_id, 0)
304                  INTO v_order_type_cogs
305                  FROM mtl_intercompany_parameters
306                 WHERE ship_organization_id = l_ship_ou_id
307                   AND sell_organization_id = to_number(ic_sell_oper_unit)
308                   AND flow_type = 1;
309               END IF;
310             END IF;
311 
312                print_debug('Found v_order_type_cogs: ' || v_order_type_cogs, l_function_name);
313         EXCEPTION
314           WHEN NO_DATA_FOUND THEN
315             fnd_message.set_name('FND', 'FLEX-BUILD INVALID VALUE');
316             fnd_message.set_token('VALUE', ic_order_type_id);
317             fnd_message.set_token('VSET_ID', '103100');
318             fb_error_msg  := fnd_message.get_encoded;
319             RETURN FALSE;
320         END;
321       END IF;
322       -- End of bug 8896018
323 
324     END IF;
325 
326     l_err_pt            := '30';
327 
328     IF (ic_organization_cogs IS NULL AND ic_to_inv_organization_id IS NOT NULL) THEN
329       BEGIN
330         -- Bug: 4474976
331         -- Replaced the local variable ic_sell_oper_unit with ic_to_inv_organization_id
332         -- in the WHERE clause of the following SQL Statment.
333         -- Removed NVL function from the SELECT statement.
334         SELECT cost_of_sales_account
335           INTO v_organization_cogs
336           FROM mtl_parameters
337          WHERE organization_id = ic_to_inv_organization_id;
338       EXCEPTION
339         WHEN NO_DATA_FOUND THEN
340            NULL;
341       /* FND_MESSAGE.SET_NAME('FND', 'FLEX-BUILD INVALID VALUE');
342          FND_MESSAGE.SET_TOKEN('VALUE', IC_SELL_OPER_UNIT);
343          FND_MESSAGE.SET_TOKEN('VSET_ID', '103101');
344          FB_ERROR_MSG := FND_MESSAGE.GET_ENCODED;
345          return FALSE;*/
346       END;
347     END IF;
348 
349     IF (l_debug = 1) THEN
350       print_debug('V_ORGANIZATION_COGS: ' || TO_CHAR(v_organization_cogs), l_function_name);
351     END IF;
352 
353     l_err_pt            := '40';
354 
355     /*
356     +-------------------------------------------------------
357     | Now set atributes for all raw and derived parameters  |
358     +-------------------------------------------------------+*/
359     IF (l_debug = 1) THEN
360       print_debug('Initilizing Workflow Item Attributes', l_function_name);
361     END IF;
362 
363     IF (ic_customer_id IS NOT NULL) THEN
364       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_CUSTOMER_ID', avalue => ic_customer_id);
365     END IF;
366 
367     l_err_pt            := '41';
368 
369     IF (ic_item_id IS NOT NULL) THEN
370       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ITEM_ID', avalue => ic_item_id);
371     END IF;
372 
373     l_err_pt            := '42';
374 
375     IF (ic_order_header_id IS NOT NULL) THEN
376       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ORDER_HEADER_ID', avalue => ic_order_header_id);
377     END IF;
378 
379     l_err_pt            := '43';
380 
381     IF (ic_order_line_id IS NOT NULL) THEN
382       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ORDER_LINE_ID', avalue => ic_order_line_id);
383     END IF;
384 
385     l_err_pt            := '44';
386 
387     IF (ic_order_type_id IS NOT NULL) THEN
388       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ORDER_TYPE_ID', avalue => ic_order_type_id);
389     END IF;
390 
391     l_err_pt            := '45';
392 
393     IF (ic_sell_oper_unit IS NOT NULL) THEN
394       wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_SELL_OPER_UNIT', avalue => ic_sell_oper_unit);
395     END IF;
396 
397     l_err_pt            := '46';
398 
399     IF (v_item_cogs IS NOT NULL) THEN
400       wf_engine.setitemattrnumber(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ITEMS_COGS', avalue => v_item_cogs);
401     END IF;
402 
403     l_err_pt            := '47';
404 
405     IF (v_organization_cogs IS NOT NULL) THEN
406       wf_engine.setitemattrnumber(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ORGANIZATION_COGS', avalue => v_organization_cogs);
407     END IF;
408 
409     l_err_pt            := '48';
410 
411     IF (v_order_type_cogs IS NOT NULL) THEN
412       wf_engine.setitemattrnumber(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'IC_ORDER_TYPE_COGS', avalue => v_order_type_cogs);
413     END IF;
414 
415     l_err_pt            := '49';
416 
417     IF (fb_flex_num IS NOT NULL) THEN
418       wf_engine.setitemattrnumber(itemtype => 'INVFLXWF', itemkey => v_itemkey, aname => 'CHART_OF_ACCOUNTS_ID', avalue => fb_flex_num);
419     END IF;
420 
421     l_err_pt            := '50';
422     /*+--------------------------------------------------+
423     | Now call the generate function which will kickoff  |
424     | the workflow.                                      |
425     +----------------------------------------------------*/
426     v_ccid              := v_order_type_cogs;
427 
428     --Begin Bug 9878196 Do not call fnd_flex_workflow.generate for process orgs for internal order flow for process org.
429     IF NVL(v_process_enabled_flag,'N') = 'Y' AND l_gmf_patch_level = '11i.GMF.L' THEN  /* Bug 11823085 */
430       -- If process organization then
431        IF ( v_doc_type_id = 10 AND
432           v_requisition_line_id IS NOT NULL ) THEN
433          --Begin Bug 7518712
434          --For process enabled organization ignore ccid returned from fnd_flex_workflow.generate
435             v_ccid := v_order_type_cogs;
436        ELSE
437           IF (l_debug = 1) THEN
438               print_debug('Calling FND_ELEX_WORKFLOW.GENERATE with Parameters:', l_function_name);
439               print_debug('Itemtype:' || ' INVFLXWF', l_function_name);
440               print_debug('itemkey: ' || v_itemkey, l_function_name);
441               print_debug('ccid: ' || TO_CHAR(v_ccid), l_function_name);
442           END IF;
443         v_generate_success  :=
444         fnd_flex_workflow.generate(
445           itemtype                     => 'INVFLXWF'
446         , itemkey                      => v_itemkey
447         , ccid                         => v_ccid
448         , concat_segs                  => v_concat_segs
449         , concat_ids                   => v_concat_ids
450         , concat_descrs                => v_concat_descrs
451         , error_message                => fb_error_msg
452         );
453         print_debug('FND_ELEX_WORKFLOW.GENERATE returned ccid' || TO_CHAR(v_ccid), l_function_name);
454         print_debug('FND_ELEX_WORKFLOW.GENERATE returned v_concat_segs' || v_concat_segs, l_function_name);
455        END IF;
456     ELSE
457        IF (l_debug = 1) THEN
458          print_debug('Calling FND_ELEX_WORKFLOW.GENERATE with Parameters:', l_function_name);
459          print_debug('Itemtype:' || ' INVFLXWF', l_function_name);
460          print_debug('itemkey: ' || v_itemkey, l_function_name);
461          print_debug('ccid: ' || TO_CHAR(v_ccid), l_function_name);
462        END IF;
463 
464        v_generate_success  :=
465           fnd_flex_workflow.generate(
466             itemtype                     => 'INVFLXWF'
467           , itemkey                      => v_itemkey
468           , ccid                         => v_ccid
469           , concat_segs                  => v_concat_segs
470           , concat_ids                   => v_concat_ids
471           , concat_descrs                => v_concat_descrs
472           , error_message                => fb_error_msg
473           );
474         print_debug('FND_ELEX_WORKFLOW.GENERATE returned ccid' || TO_CHAR(v_ccid), l_function_name);
475         print_debug('FND_ELEX_WORKFLOW.GENERATE returned v_concat_segs' || v_concat_segs, l_function_name);
476     END IF;
477      --End Bug 7518712
478      --End Bug 9878196
479 
480     IF (v_generate_success) THEN
481       fb_flex_seg  := v_concat_segs;
482 
483       IF (v_ccid = -1) THEN
484         l_err_pt  := '51';
485         v_ccid    :=
486           fnd_flex_ext.get_ccid(
487             application_short_name       => 'SQLGL'
488           , key_flex_code                => 'GL#'
489           , structure_number             => fb_flex_num
490           , validation_date              => TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS')
491           , concatenated_segments        => v_concat_segs
492           );
493         print_debug('fnd_flex_ext.get_ccid returned' || TO_CHAR(v_ccid), l_function_name);
494 
495         IF (v_ccid = 0) THEN
496           fb_error_msg  := SUBSTR(fnd_message.get_encoded, 1, 240);
497           print_debug('fnd_flex_ext.get_ccid returned no ccid.', l_function_name);
498           RETURN FALSE;
499         END IF;
500       END IF;
501 
502       RETURN TRUE;
503     ELSE
504       l_err_pt      := '52';
505       fb_flex_seg   := v_concat_segs;
506       fb_error_msg  := SUBSTR(fb_error_msg, 1, 240);
507       print_debug('FND_FLEX_WORKFLOW.GENERATE returned FALSE with error: ' || fb_error_msg, l_function_name);
508       RETURN FALSE;
509     END IF;
510   EXCEPTION
511     WHEN OTHERS THEN
512       l_err_pt      := '53';
513       print_debug('Unexpected error at l_err_pt: ' || l_err_pt, l_function_name);
514       fnd_message.set_name('INV', 'INV_UNHANDLED_ERR');
515       fnd_message.set_token('ENTITY1', l_function_name);
516       v_buffer      := TO_CHAR(SQLCODE) || ' ' || SUBSTR(SQLERRM, 1, 150);
517       fnd_message.set_token('ENTITY2', v_buffer);
518       fb_error_msg  := SUBSTR(fnd_message.get_encoded, 1, 240);
519       print_debug('Error message: ' || fb_error_msg, l_function_name);
520       RETURN FALSE;
521   END generate_cogs;
522 
523   PROCEDURE invoke_build(itemtype IN VARCHAR2, itemkey IN VARCHAR2, actid IN NUMBER, funcmode IN VARCHAR2, RESULT OUT NOCOPY VARCHAR2) IS
524     build_success          BOOLEAN        := TRUE;
525     p_fb_flex_num          NUMBER         DEFAULT 101;
526     p_ic_customer_id       VARCHAR2(100)  DEFAULT NULL;
527     p_ic_item_id           VARCHAR2(100)  DEFAULT NULL;
528     p_ic_order_header_id   VARCHAR2(100)  DEFAULT NULL;
529     p_ic_order_line_id     VARCHAR2(100)  DEFAULT NULL;
530     p_ic_order_type_id     VARCHAR2(100)  DEFAULT NULL;
531     p_ic_sell_oper_unit    VARCHAR2(100)  DEFAULT NULL;
532     p_fb_flex_seg          VARCHAR2(2000);
533     p_fb_error_msg         VARCHAR2(2000);
534     p_ic_organization_cogs NUMBER;
535     p_ic_items_cogs        NUMBER;
536     p_ic_order_type_cogs   NUMBER;
537     p_ic_structure_id      NUMBER;
538   BEGIN
539     IF (funcmode = 'RUN') THEN
540       -- call flexbuilder build here
541       -- then load segments and etc
542 
543       -- now copy attributes to local variables to pass to build function
544       p_ic_customer_id        := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_CUSTOMER_ID');
545       p_ic_item_id            := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ITEM_ID');
546       p_ic_order_header_id    := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ORDER_HEADER_ID');
547       p_ic_order_line_id      := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ORDER_LINE_ID');
548       p_ic_order_type_id      := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ORDER_TYPE_ID');
549       p_ic_sell_oper_unit     := wf_engine.getitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_SELL_OPER_UNIT');
550       p_ic_items_cogs         := wf_engine.getitemattrnumber(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ITEMS_COGS');
551       p_ic_organization_cogs  := wf_engine.getitemattrnumber(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ORGANIZATION_COGS');
552       p_ic_order_type_cogs    := wf_engine.getitemattrnumber(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'IC_ORDER_TYPE_COGS');
553       p_ic_structure_id       := wf_engine.getitemattrnumber(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'CHART_OF_ACCOUNTS_ID');
554       build_success           :=
555         inv_intercompany_cogs.BUILD(
556           fb_flex_num                  => p_ic_structure_id
557         , ic_customer_id               => p_ic_customer_id
558         , ic_item_id                   => p_ic_item_id
559         , ic_order_header_id           => p_ic_order_header_id
560         , ic_order_line_id             => p_ic_order_line_id
561         , ic_order_type_id             => p_ic_order_type_id
562         , ic_sell_oper_unit            => p_ic_sell_oper_unit
563         , fb_flex_seg                  => p_fb_flex_seg
564         , fb_error_msg                 => p_fb_error_msg
565         );
566       -- Now load segment s into workflow attrubutes
567       fnd_flex_workflow.load_concatenated_segments(itemtype => itemtype, itemkey => itemkey, concat_segs => p_fb_flex_seg);
568 
569       IF (NOT build_success) THEN
570         RESULT  := 'COMPLETE:FAILURE';
571         wf_engine.setitemattrtext(itemtype => 'INVFLXWF', itemkey => itemkey, aname => 'ERROR_MESSAGE', avalue => p_fb_error_msg);
572         RETURN;
573       ELSE
574         RESULT  := 'COMPLETE:SUCCESS';
575         RETURN;
576       END IF;
577     END IF;
578 
579     IF (funcmode = 'CANCEL') THEN
580       RESULT  := 'COMPLETE';
581     END IF;
582   EXCEPTION
583     WHEN OTHERS THEN
584       wf_core.CONTEXT('INV_WORKFLOW', 'INVOKE_BUILD', itemtype, itemkey, TO_CHAR(actid), funcmode);
585       RAISE;
586   END invoke_build;
587 END inv_workflow;