Wednesday, February 19, 2014

General Ledger New Tables R12

General Ledger

Table Name

FUN_SEQ_ASSIGNMENTS             Setup Forms and Programs
FUN_SEQ_CONTEXTS             Setup Forms and Programs
FUN_SEQ_ENTITIES             Setup Forms and Programs
FUN_SEQ_HEADERS             Setup Forms and Programs
FUN_SEQ_REQUESTS             Setup Forms and Programs
FUN_SEQ_RULES             Setup Forms and Programs
FUN_SEQ_VERSIONS             Setup Forms and Programs
GL_ACCESS_SET_ASSIGNMENTS             Setup Forms and Programs
GL_ACCESS_SET_NORM_ASSIGN             Setup Forms and Programs
GL_ACCESS_SETS             Setup Forms and Programs
GL_AUTOREV_CRITERIA_SETS             Journal Entry
GL_BC_PACKETS_HISTS             Budgets and related objects
GL_BUDORG_BC_OPTIONS             Budgets and related objects
GL_COA_MAPPINGS             Consolidation and Eliminations
GL_DEFAS_ACCESS_SETS             Setup Forms and Programs
GL_DEFAS_ASSIGNMENTS             Setup Forms and Programs
Table Name             Feature Area
GL_DEFAS_RESP_ASSIGN             Setup Forms and Programs
GL_JE_INCLUSION_RULES             Setup Forms and Programs
GL_JE_LINES_RECON             Reconciliation
GL_JE_SEGMENT_VALUES             Journal Entry
GL_LE_VALUE_SETS             Setup Forms and Programs
GL_LEDGER_CONFIG_DETAILS             Setup Forms and Programs
GL_LEDGER_CONFIGURATIONS             Setup Forms and Programs
GL_LEDGER_NORM_SEG_VALS             Setup Forms and Programs
GL_LEDGER_RELATIONSHIPS             Setup Forms and Programs
GL_LEDGER_SEGMENT_VALUES             Setup Forms and Programs
GL_LEDGER_SET_ASSIGNMENTS             Setup Forms and Programs
GL_LEDGER_SET_NORM_ASSIGN             Setup Forms and Programs
GL_LEDGER_SET_REQUESTS             Setup Forms and Programs
GL_LEDGERS             Setup Forms and Programs
GL_LEGAL_ENTITIES_BSVS             Setup Forms and Programs
GL_MGT_SEG_UPGRADE             Other Issues
GL_MGT_SEG_UPGRADE_H             Other Issues
GL_SEG_VAL_HIERARCHIES             Setup Forms and Programs
GL_SEG_VAL_NORM_HIERARCHY             Setup Forms and Programs
GL_SUMMARY_BC_OPTIONS             Budgets and related objects

Running a Concurrent from your Script

DECLARE
l_success   NUMBER;
l_org_id    NUMBER;
BEGIN
l_org_id = 88;

   DBMS_OUTPUT.put_line ('Initializing');
   fnd_global.apps_initialize (user_id           => 111,
                               resp_id           => 100791,
                               resp_appl_id      => 222
                              );
   DBMS_OUTPUT.put_line ('Begin Submission');
   l_success :=
      fnd_request.submit_request ('AK',
                                  'AKXMOSED',
                                  NULL,
                                  SYSDATE,
                                  FALSE,
                                  l_org_id
                                 );
    DBMS_OUTPUT.put_line ('End Submission '||l_success);
COMMIT;
END;

/***************
fnd_request.submit_request ('FND',                -- application
                                  'COCN_PGM_SHORT_NAME',-- program short name
                                  '',                   -- description
                                  SYSDATE,                   -- start time
                                  FALSE,                -- sub request
                                  'Argument1',          -- argument1
                                  'Argument2',          -- argument2
                                  'N',                  -- argument3
                                  NULL,                 -- argument4
                                  NULL,                 -- argument5
                                  'Argument6',          -- argument6
                                  CHR (0)               -- represents end of arguments
                                 );
**************/


Tuesday, February 4, 2014

FND_GLOBAL.APPS_INITIALIZE

select fnd.User_id ,
       fresp.Responsibility_id,
       fresp.Responsibility_Name,
       fresp.Application_id
from   fnd_user fnd
,      fnd_responsibility_tl fresp
where  fnd.user_name = 'OPERATIONS'
And    fresp.responsibility_name like 'Vision%'
And    fresp.Language = 'US' ;


fnd_global.APPS_INITIALIZE(user_id=>l_user_id,
                           resp_id=>l_resp_id,
                           resp_appl_id=>l_resp_appl_id);


l_user_id = FND user ID
l_resp_id = responsibility ID
l_resp_appl_id = responsibility application ID 

Unaccounted Receipts with Acct Nature is Average Costing

SELECT DISTINCT jrh.receipt_num, jrh.creation_date, jair.shipment_line_id,
                (SELECT SUM (tax_amount)
                   FROM jai_rcv_line_taxes
                  WHERE shipment_header_id = jrh.shipment_header_id
                    AND shipment_line_id = jair.shipment_line_id
                    AND UPPER (tax_type) = 'EXCISE') excise,
                (SELECT SUM (tax_amount)
                   FROM jai_rcv_line_taxes
                  WHERE shipment_header_id =
                                 jrh.shipment_header_id
                    AND shipment_line_id = jair.shipment_line_id
                    AND UPPER (tax_type) = 'EXCISE_EDUCATION_CESS')
                                                        excise_education_cess,
                (SELECT SUM (tax_amount)
                   FROM jai_rcv_line_taxes
                  WHERE shipment_header_id =
                                    jrh.shipment_header_id
                    AND shipment_line_id = jair.shipment_line_id
                    AND UPPER (tax_type) = 'EXCISE_SH_EDU_CESS')
                                                           excise_sh_edu_cess
           FROM jai_rcv_headers jrh,
                jai_rcv_lines jair,
                jai_rcv_line_taxes jtax
          WHERE jrh.online_claim_flag = 'Y'
            AND jair.shipment_header_id = jrh.shipment_header_id
            AND jair.online_claim_flag = 'Y'
            AND jtax.shipment_header_id = jrh.shipment_header_id
            AND jtax.shipment_line_id = jair.shipment_line_id
            AND jrh.organization_id = :P_ORGN_ID
            AND TRUNC (jrh.creation_date) BETWEEN :p_from_date AND :p_to_date
            --AND jrh.receipt_num = '961664'          -- IN ('961033', '961664')
            AND EXISTS (
                   SELECT '1'
                     FROM jai_rcv_journal_entries
                    WHERE shipment_line_id = jtax.shipment_line_id
                      AND acct_nature = 'Average Costing'
                      AND organization_code =:P_ORGN_CODE
                      AND transaction_type = 'DELIVER'
                      AND TRUNC (transaction_date) BETWEEN :p_from_date
                                                       AND :p_to_date
                      AND jrh.receipt_num NOT IN (
                             SELECT jrj.receipt_num
                               FROM jai_rcv_journal_entries jrj
                              WHERE acct_nature ='CENVAT'
                                AND jrj.organization_code = :P_ORGN_CODE
                                AND transaction_type = 'RECEIVE'
                                -- AND jrj.receipt_num IN (961033, 961664)
                                AND TRUNC (transaction_date)
                                       BETWEEN :p_from_date
                                           AND :p_to_date))
            AND UPPER (jtax.tax_type) IN
                    ('EXCISE', 'EXCISE_EDUCATION_CESS', 'EXCISE_SH_EDU_CESS')

CRM Call Register Query Logic


SELECT DISTINCT jtv.task_id, csivs.creation_date, hou.NAME "Operating Unit",
                TO_CHAR (csivs.creation_date,
                         'DD-MM-YYYY HH24:MI:SSAM'
                        ) "Date & Time",
                hpp.person_name "Call logged by", csstss.NAME "Type of Call",
                csisb.NAME "SR Status", csivs.incident_number "SR Number",
                hcp.phone_number, jtv.attribute8 part_replace,
                jtv.attribute9 service_charge,
                jtv.attribute10 service_charge_in_rs,
                (SELECT user_name
                   FROM fnd_user
                  WHERE user_id = csivs.last_updated_by) modify_by,
                (SELECT user_name
                   FROM fnd_user
                  WHERE user_id = csivs.created_by) created_by,
                hz.party_name "Customer Name",
                (SELECT city
                   FROM hz_locations
                  WHERE location_id =
                           (SELECT a.location_id
                              FROM hz_party_sites a,
                                   cs_incidents_all_b b
                             WHERE a.party_site_id = b.incident_location_id
                               AND b.incident_number = csivs.incident_number))
                                                                     LOCATION,
                (SELECT    address1
                        || ','
                        || address2
                        || ' , '
                        || address3
                        || ' '
                        || city
                        || ' '
                        || state
                   FROM hz_locations
                  WHERE location_id =
                           (SELECT a.location_id
                              FROM hz_party_sites a, cs_incidents_all_b b
                             WHERE a.party_site_id = b.incident_location_id
                               AND b.incident_number = csivs.incident_number))
                                                           "Customer Address",
                (SELECT DISTINCT segment1
                            FROM mtl_system_items_b
                           WHERE inventory_item_id =
                                          csivs.inventory_item_id
                             AND organization_id = csivs.inv_organization_id)
                                                                  "Item Name",
                (SELECT DISTINCT description
                            FROM mtl_system_items_b
                           WHERE inventory_item_id =
                                    csivs.inventory_item_id
                             AND organization_id = csivs.inv_organization_id)
                                                           "Item Description",
                cii.serial_number "Serial Number",
                csivs1.summary "Problem Summary",
                csivs.problem_code "Problem Codes",
                jtv.task_number "Task Number",
                TO_CHAR (jtv.creation_date,
                         'DD-MM-YYYY HH24:MI:SSAM'
                        ) "Task Date",
                jttl.NAME "Task Type", jtl.task_name "Subject",
                jtstl.NAME "Status", jnv.notes "Task Notes",
                TO_CHAR
                     (jtv.planned_start_date,
                      'DD-MM-YYYY HH24:MI:SSAM'
                     ) "Planned Start Date and Time",
                TO_CHAR
                       (jtv.planned_end_date,
                        'DD-MM-YYYY HH24:MI:SSAM'
                       ) "Planned Date End and Time",
                TO_CHAR
                   (jtv.scheduled_start_date,
                    'DD-MM-YYYY HH24:MI:SSAM'
                   ) "Scheduled Start Date and Time",
                TO_CHAR
                     (jtv.scheduled_end_date,
                      'DD-MM-YYYY HH24:MI:SSAM'
                     ) "Scheduled End Date and Time",
                TO_CHAR
                      (jtv.actual_start_date,
                       'DD-MM-YYYY HH24:MI:SSAM'
                      ) "Actual Start Date and Time",
                TO_CHAR (jtv.actual_end_date,
                         'DD-MM-YYYY HH24:MI:SSAM'
                        ) "Actual End Date and Time",
                DECODE (jtv.task_number,
                        xx_mrt1_no (csivs.incident_id), xx_mrt1
                                                            (csivs.incident_id),
                        NULL
                       ) "MTR1",
                DECODE (jtv.task_number,
                        xx_mrt2_no (csivs.incident_id), xx_mrt2
                                                            (csivs.incident_id),
                        NULL
                       ) "MTR2",
                jtv.attribute1 "Report Signed By",
                jtv.attribute2 "Contact No", jtv.attribute3 "Report Number",
                jtv.attribute4 "Report Status",
                jtv.attribute5 "Customer Remark",
                jtv.attribute6 "Engineer Remark",
                jtptl.description "Priority",
                TO_CHAR (okh.start_date,
                         'DD-MM-YYYY HH24:MI:SSAM'
                        ) "Warranty Start Date",
                TO_CHAR (okh.end_date,
                         'DD-MM-YYYY HH24:MI:SSAM'
                        ) "Warranty End date"
FROM            cs_incidents_b_sec csivs,
                hz_parties hz,
                hr_operating_units hou,
                cs_incident_statuses_tl csisb,
                cs_incident_types_tl csstss,
                jtf_tasks_b jtv,
                jtf_task_types_tl jttl,
                jtf_tasks_tl jtl,
                jtf_task_statuses_tl jtstl,
                jtf_task_priorities_tl jtptl,
                jtf_notes_vl jnv,
                jtf_task_all_assignments jtaa,
                jtf_rs_resource_extns jrd,
                csi_item_instances cii,
                cs_incidents_all_tl csivs1,
                cs_hz_sr_contact_points chsc,
                hz_person_profiles hpp,
                okc_k_headers_all_b okh,
                hz_party_sites hzp,
                hz_locations hzl,
                fnd_user fs,
                hz_contact_points hcp
WHERE           TRUNC (csivs.creation_date)
                   BETWEEN NVL (:xx_dat_from, TRUNC (csivs.creation_date))
                       AND NVL (:xx_dat_to, TRUNC (csivs.creation_date))
            AND hou.organization_id = NVL (:xx_og, hou.organization_id)
            AND hz.party_id = NVL (:xx_pn, hz.party_id)
            AND csivs.incident_number = NVL (:xx_in, csivs.incident_number)
            AND (csivs.created_by = :xx_owner OR :xx_owner IS NULL)
            AND (cii.serial_number = (:xx_sno) OR (:xx_sno IS NULL))
            AND (csivs.inventory_item_id = :xx_iid OR :xx_iid IS NULL)
            AND csivs.incident_type_id =
                                       NVL (:xx_it_id, csivs.incident_type_id)
            AND csivs.incident_status_id =
                                     NVL (:xx_it_ty, csivs.incident_status_id)
            AND (csivs.problem_code = :xx_pc OR :xx_pc IS NULL)
            AND (   jtv.task_priority_id =
                                        NVL (:xx_t_prio, jtv.task_priority_id)
                 OR :xx_t_prio IS NULL
                )
            AND (   jtv.task_type_id = NVL (:xx_t_type, jtv.task_type_id)
                 OR :xx_t_type IS NULL
                )
            AND (   jtv.task_status_id =
                                        NVL (:xx_t_status, jtv.task_status_id)
                 OR :xx_t_status IS NULL
                )
            AND (   jtf_task_utl.get_owner (jtv.owner_type_code, jtv.owner_id) =
                                                                   :xx_j_owner
                 OR :xx_j_owner IS NULL
                )
            AND (jrd.resource_id = :xx_ass OR :xx_ass IS NULL)
            AND (   TRUNC (jtv.scheduled_start_date) =
                           NVL (:xx_sc_from, TRUNC (jtv.scheduled_start_date))
                 OR :xx_sc_from IS NULL
                )
            AND (   TRUNC (jtv.scheduled_end_date) =
                               NVL (:xx_sc_to, TRUNC (jtv.scheduled_end_date))
                 OR :xx_sc_to IS NULL
                )
            AND csivs.customer_id = hz.party_id
            AND csivs.org_id = hou.organization_id
            AND csivs.incident_status_id = csisb.incident_status_id
            AND csivs.incident_type_id = csstss.incident_type_id
            AND jtv.source_object_id(+) = csivs.incident_id
            AND jtv.task_type_id = jttl.task_type_id(+)
            AND jtv.task_id = jtl.task_id(+)
            AND jtv.task_status_id = jtstl.task_status_id(+)
            AND jtv.task_priority_id = jtptl.task_priority_id(+)
            AND jtv.task_number = jnv.source_number(+)
            AND jtv.task_id = jtaa.task_id(+)
            AND (   jtaa.assignee_role IN ('ASSIGNEE', 'OWNER')
                 OR jtaa.assignee_role IS NULL
                )
            AND jtaa.resource_id = jrd.resource_id(+)
            AND csivs.incident_id = csivs1.incident_id
            AND csivs1.LANGUAGE = USERENV ('LANG')
            AND csivs.customer_product_id = cii.instance_id(+)
            AND csivs.contract_id = okh.ID(+)
            AND chsc.incident_id(+) = csivs.incident_id
            AND (chsc.primary_flag = 'Y' OR chsc.primary_flag IS NULL)
            AND (chsc.party_id - 1) = hpp.party_id(+)
            AND hzp.party_site_id(+) = csivs.ship_to_site_id
            AND hzp.location_id = hzl.location_id(+)
            AND hcp.contact_point_id(+) = chsc.contact_point_id
            AND csivs.created_by = fs.user_id
       ORDER BY csivs.creation_date, jtv.task_id

Rupees to Words PL/SQL

CREATE OR REPLACE FUNCTION APPS.ruppee_to_word100 (amount IN NUMBER)
   RETURN VARCHAR2
AS
   v_length   INTEGER         := 0;
   v_num2     VARCHAR2 (50)   := NULL;
   v_amount   VARCHAR2 (50)   := TO_CHAR (TRUNC (amount));
   v_word     VARCHAR2 (4000) := NULL;
   v_word1    VARCHAR2 (4000) := NULL;

   TYPE myarray IS TABLE OF VARCHAR2 (255);

   v_str      myarray         := myarray (' Thousand ', ' Lakh ', ' Crore ');
BEGIN
   IF ((amount = 0) OR (amount IS NULL))
   THEN
      v_word := 'zero';
   ELSIF (TO_CHAR (amount) LIKE '%.%')
   THEN
      IF (SUBSTR (amount, INSTR (amount, '.') + 1) > 0)
      THEN
         v_num2 := SUBSTR (amount, INSTR (amount, '.') + 1);

         IF (LENGTH (v_num2) < 2)
         THEN
            v_num2 := v_num2 * 10;
         END IF;

         v_word1 :=
               'Rupees AND '
            || (TO_CHAR (TO_DATE (SUBSTR (v_num2, LENGTH (v_num2) - 1, 2),
                                  'J'),
                         'JSP'
                        )
               )
            || ' paise ';
         v_amount := SUBSTR (amount, 1, INSTR (amount, '.') - 1);
         v_word :=
               TO_CHAR (TO_DATE (SUBSTR (v_amount, LENGTH (v_amount) - 2, 3),
                                 'J'
                                ),
                        'Jsp'
                       )
            || v_word;
         v_amount := SUBSTR (v_amount, 1, LENGTH (v_amount) - 3);

         FOR i IN 1 .. v_str.COUNT
         LOOP
            EXIT WHEN (v_amount IS NULL);
            v_word :=
                  TO_CHAR (TO_DATE (SUBSTR (v_amount, LENGTH (v_amount) - 1,
                                            2),
                                    'J'
                                   ),
                           'Jsp'
                          )
               || v_str (i)
               || v_word;
            v_amount := SUBSTR (v_amount, 1, LENGTH (v_amount) - 2);
         END LOOP;
      END IF;
   ELSE
      v_word := TO_CHAR (TO_DATE (TO_CHAR (amount, '999999999'), 'J'), 'JSP');
   END IF;

   v_word := v_word || ' ' || v_word1 || ' only ';
   v_word := REPLACE (RTRIM (v_word), ' ', ' ');
   v_word := REPLACE (RTRIM (v_word), '-', ' ');
   RETURN INITCAP (v_word);
END ruppee_to_word100;
/

WIP Query

SELECT   we.wip_entity_name job, ood.organization_name organization_name,
         msib1.segment1 fg_item,
         (SELECT stragg (DISTINCT c.item_cost)
            FROM apps.cst_item_costs c
           WHERE c.inventory_item_id = msib1.inventory_item_id
             AND c.organization_id = msib1.organization_id
             ) fg_cost,
         wro.operation_seq_num operation, bso.operation_code,
         bso.operation_description, wro.quantity_issued qty_completed,
         msib.segment1 rm_item, msib.description rm_desc,
         cic.item_cost rm_cost,
         (wro.quantity_issued * cic.item_cost) consumed_rm_cost
    FROM apps.wip_entities we,
         apps.org_organization_definitions ood,
         apps.wip_requirement_operations wro,
         apps.mtl_system_items_b msib,
         apps.mtl_system_items_b msib1,
         apps.cst_item_costs cic,
         apps.wip_operations wo,
         apps.bom_standard_operations bso
   WHERE 1 = 1
     AND we.organization_id = ood.organization_id
     AND wro.wip_entity_id = we.wip_entity_id
     AND wro.organization_id = we.organization_id
     AND msib.inventory_item_id = wro.inventory_item_id
     AND msib.organization_id = wro.organization_id
     AND msib1.inventory_item_id = we.primary_item_id
     AND msib1.organization_id = we.organization_id
     AND cic.inventory_item_id = msib.inventory_item_id
     AND cic.organization_id = msib.organization_id
     AND bso.standard_operation_id(+) = wo.standard_operation_id
     AND wo.wip_entity_id = wro.wip_entity_id
     AND we.wip_entity_name = NVL (:p_job, we.wip_entity_name)
     AND ood.organization_id = NVL (:orga, ood.organization_id)
ORDER BY wro.operation_seq_num

GL_ACCESS_SET_LEDGERS

SELECT  glasna.access_set_id    AS ACCESS_SET_ID,
               DECODE(gllsa.ledger_id,
                      NULL, glasna.ledger_id,
                      gllsa.ledger_id) AS LEDGER_ID,
               DECODE(MIN(DECODE(glasna.all_segment_value_flag,
                                 'Y',
                                 DECODE(glasna.access_privilege_code,
                                        'B', 1, 'R', 3),
                                 DECODE(glasna.access_privilege_code,
                                        'B', 2, 'R', 3))),                    
                       1, 'F',
                       2, 'B',
                       3, 'R')  AS ACCESS_PRIVILEGE_CODE,
               MAX(glasna.LAST_UPDATE_DATE) AS LAST_UPDATE_DATE,
               0 AS LAST_UPDATED_BY,
               MAX(glasna.CREATION_DATE) AS CREATION_DATE,
               0 AS CREATED_BY,
               0 AS LAST_UPDATE_LOGIN,
               TO_DATE(NULL)                   AS START_DATE,
               TO_DATE(NULL)                   AS END_DATE
       FROM    GL_ACCESS_SETS glas,
               GL_ACCESS_SET_NORM_ASSIGN glasna,
               GL_LEDGER_SET_ASSIGNMENTS gllsa                                
       WHERE   glas.automatically_created_flag = 'N'
       AND     glasna.access_set_id = glas.access_set_id
       AND     NVL(glasna.status_code, 'X') <> 'I'
       AND     gllsa.ledger_set_id (+) = glasna.ledger_id
       GROUP BY glasna.access_set_id,
                DECODE(gllsa.ledger_id, NULL,
                       glasna.ledger_id, gllsa.ledger_id)
       UNION ALL
       Select  glas.access_set_id AS ACCESS_SET_ID,
       DECODE(gll.object_type_code,'S',gllsa.ledger_id,glasna.ledger_id) AS LEDGER_ID,
       DECODE(MAX(DECODE(gll.object_type_code,'S',1,DECODE(gllsa.ledger_id,NULL,
                                                      DECODE(glasna.all_segment_value_flag,'Y',
                                                             DECODE(glasna.access_privilege_code,'R',3,'B',1),
                                                             DECODE(glasna.access_privilege_code,'R',3,'B',2)),
                                                      DECODE(glasna2.all_segment_value_flag,'Y',
                                                             DECODE(glasna2.access_privilege_code,'R',3,'B',1),
                                                             DECODE(glasna2.access_privilege_code,'R',3,'B',2))))),
                                         1,'F',2,'B',3,'R') AS ACCESS_PRIVILEGE_CODE,
   MAX(glasna2.LAST_UPDATE_DATE) AS LAST_UPDATE_DATE,
   0 AS LAST_UPDATED_BY,
   MAX(glasna2.CREATION_DATE) AS CREATION_DATE,
   0 AS CREATED_BY,
   0 AS LAST_UPDATE_LOGIN,
   TO_DATE(NULL) AS START_DATE,
   TO_DATE(NULL) AS END_DATE  
FROM   gl_ledgers gll,
       gl_access_sets glas,
       gl_access_set_norm_assign glasna,
       gl_ledger_set_assignments gllsa,
       gl_access_set_norm_assign glasna2
WHERE  glas.access_set_id = gll.implicit_access_set_id
AND    glas.automatically_created_flag = 'Y'
AND    glasna.access_set_id = glas.access_set_id
AND    NVL(glasna.status_code,'X') <> 'I'
AND    gllsa.ledger_set_id (+) = glasna.ledger_id
AND    NVL(gllsa.status_code (+),'X') <> 'I'
AND    glasna2.access_set_id = DECODE(gllsa.ledger_set_id , NULL, glasna.access_set_id, glasna.access_set_id)
AND    glasna2.ledger_id = DECODE(gll.object_type_code,'S',glasna.ledger_id,NVL(gllsa.ledger_id,glasna.ledger_id))
AND    NVL(glasna2.status_code, 'X') <> 'I'
GROUP BY   glas.access_set_id,
           DECODE(gll.object_type_code,'S',gllsa.ledger_id,glasna.ledger_id)
         
         

Thursday, May 23, 2013

CENVAT CLAIM - INDIA TAX PART


SELECT   ROWNUM, bb.receipt_num, bb.dat, bb.vendor_name,
         bb.excise_duty_reg_no, bb.vendor_type, bb.cenvat_amount, bb.se,
         bb.CV, bb.ae, bb.ec, bb.sc, bb.organization_id, bb.description,
         bb.unit_of_measure, bb.item_code, bb.excise_invoice_no,
         bb.excise_invoice_date, bb.quantity_shipped, bb.inventory_item_id,
         CASE
            WHEN bb.vendor_type = 'Manufacturer'
               THEN bb.cenvat_amount
            WHEN bb.vendor_type IS NULL
               THEN bb.cenvat_amount
            ELSE 0
         END mfg_amt_cen,
         CASE
            WHEN bb.vendor_type = 'First Stage Dealer'
               THEN bb.cenvat_amount
            WHEN bb.vendor_type = 'Second Stage Dealer'
               THEN bb.cenvat_amount
            ELSE 0
         END dea_amt_cen,
         CASE
            WHEN bb.vendor_type = 'Manufacturer'
               THEN bb.CV
            WHEN bb.vendor_type IS NULL
               THEN bb.CV
            ELSE 0
         END mfg_amt_cvd,
         CASE
            WHEN bb.vendor_type = 'First Stage Dealer'
               THEN bb.CV
            WHEN bb.vendor_type = 'Second Stage Dealer'
               THEN bb.CV
            ELSE 0
         END dea_amt_cvd,
         CASE
            WHEN bb.vendor_type = 'Manufacturer'
               THEN bb.ec
            WHEN bb.vendor_type IS NULL
               THEN bb.ec
            ELSE 0
         END mfg_amt_ec,
         CASE
            WHEN bb.vendor_type = 'First Stage Dealer'
               THEN bb.ec
            WHEN bb.vendor_type = 'Second Stage Dealer'
               THEN bb.ec
            ELSE 0
         END dea_amt_ec,
         CASE
            WHEN bb.vendor_type = 'Manufacturer'
               THEN bb.sc
            WHEN bb.vendor_type IS NULL
               THEN bb.sc
            ELSE 0
         END mfg_amt_sc,
         CASE
            WHEN bb.vendor_type = 'First Stage Dealer'
               THEN bb.sc
            WHEN bb.vendor_type = 'Second Stage Dealer'
               THEN bb.sc
            ELSE 0
         END dea_amt_sc,
         CASE
            WHEN bb.vendor_type = 'Manufacturer'
            AND bb.unit_of_measure = 'NOS'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type IS NULL AND bb.unit_of_measure = 'NOS'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'Each'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'SET'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'SET of 2'
               THEN bb.quantity_shipped
            ELSE 0
         END nos_m,
         CASE
            WHEN bb.unit_of_measure = 'KGM'
            AND bb.vendor_type = 'Manufacturer'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type IS NULL AND bb.unit_of_measure = 'KGM'
               THEN bb.quantity_shipped
            ELSE 0
         END kgs_m,
         CASE
            WHEN bb.unit_of_measure = 'PKT'
            AND bb.vendor_type = 'Manufacturer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'PKT' AND bb.vendor_type IS NULL
               THEN bb.quantity_shipped
            ELSE 0
         END pcs_m,
         CASE
            WHEN bb.unit_of_measure = 'MTR'
            AND bb.vendor_type = 'Manufacturer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'MTR' AND bb.vendor_type IS NULL
               THEN bb.quantity_shipped
            ELSE 0
         END mtr_m,
         CASE
            WHEN bb.unit_of_measure = 'Litre'
            AND bb.vendor_type = 'Manufacturer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'Litre' AND bb.vendor_type IS NULL
               THEN bb.quantity_shipped
            ELSE 0
         END ltr_m,
         CASE                                                  --DEALER
            WHEN bb.vendor_type = 'First Stage Dealer'
            AND bb.unit_of_measure = 'NOS'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'First Stage Dealer'
            AND bb.unit_of_measure = 'Each'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'First Stage Dealer'
            AND bb.unit_of_measure = 'SET'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'First Stage Dealer'
            AND bb.unit_of_measure = 'SET of 2'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'Second Stage Dealer'
            AND bb.unit_of_measure = 'NOS'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'Second Stage Dealer'
            AND bb.unit_of_measure = 'Each'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'Second Stage Dealer'
            AND bb.unit_of_measure = 'SET'
               THEN bb.quantity_shipped
            WHEN bb.vendor_type = 'Second Stage Dealer'
            AND bb.unit_of_measure = 'SET of 2'
               THEN bb.quantity_shipped
            ELSE 0
         END nos_d,
         CASE
            WHEN bb.unit_of_measure = 'KGM'
            AND bb.vendor_type = 'First Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'KGM'
            AND bb.vendor_type = 'Second Stage Dealer'
               THEN bb.quantity_shipped
            ELSE 0
         END kgs_d,
         CASE
            WHEN bb.unit_of_measure = 'PKT'
            AND bb.vendor_type = 'First Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'PKT'
            AND bb.vendor_type = 'Second Stage Dealer'
               THEN bb.quantity_shipped
            ELSE 0
         END pcs_d,
         CASE
            WHEN bb.unit_of_measure = 'MTR'
            AND bb.vendor_type = 'First Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'SHE'
            AND bb.vendor_type = 'First Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'MTR'
            AND bb.vendor_type = 'Second Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'SHE'
            AND bb.vendor_type = 'Second Stage Dealer'
               THEN bb.quantity_shipped
            ELSE 0
         END mtr_d,
         CASE
            WHEN bb.unit_of_measure = 'Litre'
            AND bb.vendor_type = 'First Stage Dealer'
               THEN bb.quantity_shipped
            WHEN bb.unit_of_measure = 'Litre'
            AND bb.vendor_type = 'Second Stage Dealer'
               THEN bb.quantity_shipped
            ELSE 0
         END ltr_d
    FROM (SELECT DISTINCT b.receipt_num, b.dat, b.vendor_name,
                          b.excise_duty_reg_no, b.vendor_type,
                          b.cenvat_amount, SUM (b.sed) se, SUM (b.cvd) CV,
                          SUM (b.aed) ae, SUM (b.edu_cess) ec,
                          SUM (b.she_cess) sc, b.organization_id,
                          b.description, b.unit_of_measure, b.item_code,
                          b.inventory_item_id, b.excise_invoice_no,
                          b.excise_invoice_date, b.quantity_shipped
                     FROM (SELECT DISTINCT rsh.receipt_num,
                                           jcr2a.transaction_date dat,
                                           ap.vendor_name,
                                           jcvs.excise_duty_reg_no,
                                           NVL (jcvs.vendor_type,
                                                NULL
                                               ) vendor_type,
                                           jrcc.cenvat_amount,
                                           rsl.quantity_shipped, NULL sed,
                                           (SELECT SUM (tax_amount)
                                              FROM jai_rcv_line_taxes
                                             WHERE shipment_header_id =
                                                      rsl.shipment_header_id
                                               AND tax_type LIKE 'CVD%') cvd,
                                           (SELECT SUM (tax_amount)
                                              FROM jai_rcv_line_taxes
                                             WHERE shipment_header_id =
                                                      rsl.shipment_header_id
                                               AND tax_type = 'ADDITIONAL_CVD')
                                                                          aed,
                                           (SELECT SUM (tax_amount)
                                              FROM jai_rcv_line_taxes
                                             WHERE shipment_header_id =
                                                        rsl.shipment_header_id
                                               AND tax_type =
                                                       'EXCISE_EDUCATION_CESS')
                                                                     edu_cess,
                                           (SELECT SUM (tax_amount)
                                              FROM jai_rcv_line_taxes
                                             WHERE shipment_header_id =
                                                        rsl.shipment_header_id
                                               AND tax_type =
                                                          'EXCISE_SH_EDU_CESS')
                                                                     she_cess,
                                           jrl.organization_id,
                                           msib.description,
                                           rsl.unit_of_measure,
                                           msib.segment1 item_code,
                                           jrl.excise_invoice_no,
                                           jrl.excise_invoice_date,
                                           msib.inventory_item_id
                                      FROM rcv_shipment_headers rsh,
                                           rcv_shipment_lines rsl,
                                           jai_rcv_lines jrl,
                                           jai_rcv_headers jrh,
                                           ap_suppliers ap,
                                           jai_cmn_vendor_sites jcvs,
                                           po_lines_all pol,
                                           jai_rcv_cenvat_claims jrcc,
                                           jai_rcv_line_taxes jrlt,
                                           mtl_system_items_b msib,
                                           jai_cmn_rg_23ac_ii_trxs jcr2a
                                     WHERE rsh.shipment_header_id =
                                                        rsl.shipment_header_id
                                       AND jcr2a.inventory_item_id =
                                                        msib.inventory_item_id
                                       AND TO_CHAR (jcr2a.transaction_date) =
                                                  TO_CHAR (jrcc.creation_date)
                                       AND jcr2a.organization_id =
                                                           jrl.organization_id
                                       AND jrl.excise_invoice_no =
                                                       jcr2a.excise_invoice_no
                                       AND jrl.claim_modvat_flag = 'Y'
                                       AND jrh.shipment_header_id =
                                                        rsh.shipment_header_id
                                       AND jrl.shipment_header_id =
                                                        jrh.shipment_header_id
                                       AND jrl.shipment_line_id =
                                                          rsl.shipment_line_id
                                       AND ap.vendor_id = rsh.vendor_id
                                       AND jcvs.vendor_id = ap.vendor_id
                                       AND jcvs.vendor_site_id = 0
                                       AND pol.po_header_id = rsl.po_header_id
                                       AND jrcc.shipment_line_id =
                                                          rsl.shipment_line_id
                                       AND jrlt.shipment_header_id =
                                                        rsh.shipment_header_id
                                       AND msib.inventory_item_id =
                                                                   rsl.item_id
                                       AND jrl.organization_id =
                                                          msib.organization_id
                                       AND jrl.organization_id =
                                              NVL (:p_org,
                                                   jrl.organization_id)
                                       AND TRUNC (jcr2a.transaction_date)
                                              BETWEEN NVL
                                                        (TO_DATE
                                                                (:p_start_date),
                                                         TRUNC
                                                            (jcr2a.transaction_date
                                                            )
                                                        )
                                                  AND NVL
                                                         (TO_DATE (:p_end_date),
                                                          TRUNC (SYSDATE)
                                                         )) b
                 GROUP BY b.receipt_num,
                          b.dat,
                          b.vendor_name,
                          b.excise_duty_reg_no,
                          b.vendor_type,
                          b.cenvat_amount,
                          b.sed,
                          b.cvd,
                          b.aed,
                          b.edu_cess,
                          b.she_cess,
                          b.organization_id,
                          b.description,
                          b.unit_of_measure,
                          b.item_code,                
                          b.excise_invoice_no,
                          b.excise_invoice_date,
                          b.quantity_shipped,
                          b.inventory_item_id
                 ORDER BY b.receipt_num) bb
ORDER BY ROWNUM

Sales Collection Analysis Report Query (Aging Style)



SELECT
sal_per_name,
sb0,
sb1,
sb2,
sb3,
sb4,
sb5,
sb6,
(sb0+sb1+sb2+sb3+sb4+sb5+sb6)g_t
from(
select
sal_per_name,
sum(b0) sb0,
sum(b1) sb1,
sum(b2) sb2,
sum(b3) sb3,
sum(b4) sb4,
sum(b5) sb5,
sum(b6) sb6
from
(select
sal_per_name,
case
when days_past_due between 0 and 30 then amt else 0 end b0,
case
when days_past_due between 31 and 60 then amt else 0 end b1,
case
when days_past_due between 61 and 90 then amt else 0 end b2,
case
when days_past_due between 91 and 120 then amt else 0 end b3,
case
when days_past_due between 121 and 180 then amt else 0 end b4,
case
when days_past_due between 181 and 365 then amt else 0 end b5,
case
when days_past_due between 366 and 9999 then amt else 0 end b6
from
(
select
sal_per_name ,
amt,
days_past_due
from
(
select distinct
nvl( jre.resource_name,arpv.attribute2) sal_per_name, arpv.receipt_number,
arpv.amount_applied amt,
case
when ceil(sysdate-ps.due_date)>0 then ceil(sysdate-ps.due_date) else 0  end days_past_due
from
hr_operating_units hou,
ar_cash_receipts_all acra,
ar_payment_schedules_all ps,
ar_receivable_applications_v arpv,
ra_customer_trx_all rct,
ra_cust_trx_line_salesreps_all rcsa,
ra_salesreps_all rsa,
jtf_rs_resource_extns_vl jre
where
acra.cash_receipt_id=arpv.cash_receipt_id
and arpv.trx_number=rct.trx_number
and rct.customer_trx_id=rcsa.customer_trx_id
and rcsa.salesrep_id=rsa.salesrep_id
and rsa.resource_id=jre.resource_id
and acra.status='APP'
 and ps.trx_number = arpv.trx_number
and hou.organization_id=acra.org_id
and hou.organization_id=nvl(:xx_org_id,hou.organization_id)
and trunc(acra.receipt_date) between nvl(:date_from,trunc(acra.receipt_date)) and nvl(:date_to,trunc(acra.receipt_date))
union
select distinct
nvl(jre.resource_name,acra.attribute1) sal_per_name ,acra.receipt_number,
 acra.applied_amount amt,
case
when ceil(to_date(sysdate)-ps.due_date)>0 then ceil(to_date(sysdate)-ps.due_date) else 0  end days_past_due
from
ar_cash_receipts_v acra,ar_payment_schedules_all ps,jtf_rs_resource_extns_vl jre,ra_salesreps_all rsa,ra_cust_trx_line_salesreps_all rcsa,
ra_customer_trx_all rct,ar_receivable_applications_v arpv
where
 acra.receipt_status='UNAPP'
 and acra.org_id = nvl(:xx_org_id,acra.org_id)
 and ps.trx_number = arpv.trx_number
and trunc(acra.receipt_date ) between nvl(:date_from,trunc(acra.receipt_date)) and nvl(:date_to,trunc(acra.receipt_date))
and acra.applied_amount <> 0
and rsa.resource_id=jre.resource_id
and rcsa.salesrep_id=rsa.salesrep_id
and rct.customer_trx_id=rcsa.customer_trx_id
and acra.cash_receipt_id=arpv.cash_receipt_id
and arpv.trx_number=rct.trx_number
))
)
group by sal_per_name)