Search This Blog

Thursday, June 16, 2016

Query Payroll to reconcile last Month with current Month After Payroll is run

SELECT   employee_number, assignment_id, person_id,
--action_type,
                                                   full_name,-- reporting_name,
         element_name, element_type_id,
--input_value_id,
                                       classification_id, classification_name,
         SUM (NVL (curr_amt, 0)) curr_amt,
         SUM (NVL (prev_amt, 0)) prev_amt,
         SUM (NVL (curr_nonrec, 0)) curr_nonrec,
         SUM (NVL (curr_recur_payvalue, 0)) curr_recur_payvalue,
           SUM (NVL (curr_recur_ent, 0))
         + SUM (NVL (curr_recur_deduction, 0)) curr_recur_ent,
         SUM (NVL (prev_nonrec, 0)) rev_nonrec,
         SUM (NVL (prev_recur_payvalue, 0)) prev_recur_payvalue,
           SUM (NVL (prev_recur_ent, 0))
         + SUM (NVL (prev_recur_deduction, 0)) prev_recur_ent,
         SUM (NVL (curr_recur_deduction, 0)) curr_recur_deduction,
         SUM (NVL (prev_recur_deduction, 0)) prev_recur_deduction,
         ROUND (SUM (NVL (curr_amt, 0)) - SUM (NVL (prev_amt, 0)), 2) var_amt
    FROM (
    SELECT                         -- lpad(PEOPLE.employee_number,6,' ')
                 distinct people.employee_number employee_number, people.full_name,
                 assign.assignment_id assignment_id,
                 assign.person_id person_id, st.user_status action_type,
                 petl.element_name,-- pet.reporting_name,
                 pet.element_type_id element_type_id,
                 piv.input_value_id input_value_id,
                 pet.classification_id classification_id, classification_name,                
--DECODE(CLASSIFICATION_ID,107,TO_NUMBER(prrv.RESULT_VALUE),100,-TO_NUMBER(prrv.RESULT_VALUE),99,-TO_NUMBER(prrv.RESULT_VALUE),TO_NUMBER(prrv.RESULT_VALUE)) curr_amt,
                 DECODE (piv.NAME,
                         'Pay Value', DECODE (classification_name,
                                              'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                              'Voluntary Deductions', -TO_NUMBER
                                                                          (prrv.result_value
                                                                          ),
                                              'Involuntary Deductions', -TO_NUMBER
                                                                            (prrv.result_value
                                                                            ),
                                              TO_NUMBER (prrv.result_value)
                                             )
                        ) curr_amt,
                 DECODE
                    (pet.processing_type,
                     'N', DECODE (piv.NAME,
                                  'Pay Value', DECODE
                                               (classification_name,
                                                'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                'Voluntary Deductions', -TO_NUMBER
                                                                            (prrv.result_value
                                                                            ),
                                                'Involuntary Deductions', -TO_NUMBER
                                                                              (prrv.result_value
                                                                              ),
                                                TO_NUMBER (prrv.result_value)
                                               )
                                 )
                    ) curr_nonrec,
                 DECODE
                    (pet.processing_type,
                     'R', DECODE (piv.NAME,
                                  'Pay Value', DECODE
                                                (classification_name,
                                                 'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                 0
                                                )
                                 )
                    ) curr_recur_payvalue,
                 DECODE
                    (prr.entry_type,
                     'D', 0,
                     (DECODE (pet.processing_type,
                              'R', DECODE
                                        (piv.NAME,
                                         'Entitlement', DECODE
                                                (classification_name,
                                                 'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                 0
                                                )
                                        )
                             )
                     )
                    ) curr_recur_ent,
                 (DECODE (pet.processing_type,
                          'R', DECODE (piv.NAME,
                                       'Pay Value', DECODE
                                               (classification_name,
                                                'Involuntary Deductions', -TO_NUMBER
                                                                              (prrv.result_value
                                                                              ),
                                                0
                                               )
                                      )
                         )
                 ) curr_recur_deduction,
                 0 prev_amt, 0 prev_nonrec, 0 prev_recur_payvalue,
                 0 prev_recur_ent, 0 prev_recur_deduction
            FROM per_people_f people,
                 per_all_assignments_f assign,
                 pay_payroll_actions pact,
                 pay_assignment_actions asact,
                 pay_run_results prr,
                 pay_run_result_values prrv,
                 pay_element_types_f pet,
                 pay_element_types_f_tl petl,
                 pay_input_values_f piv,
                 hr_lookups hr,
                 per_assignment_status_types st,
                 pay_element_classifications pec,
                 hr_all_organization_units haou,
                 hr_assignment_set_amendments haam
           WHERE people.business_group_id = nvl(:p_business_group_id,people.business_group_id)
--           and employee_number = '9152'
             AND haou.organization_id = assign.organization_id
             and  petl.element_type_id = pet.element_type_id and petl.language = userenv('lang')
             AND haou.organization_id =
                                       NVL (:p_org_name, haou.organization_id)
             AND assign.assignment_id = haam.assignment_id(+)
             AND people.person_id = NVL (:p_person_id, people.person_id)
             AND hr.lookup_code = st.per_system_status
             AND st.assignment_status_type_id =
                                              assign.assignment_status_type_id
             AND LAST_DAY (pact.effective_date)
                    BETWEEN people.effective_start_date
                        AND people.effective_end_date
             AND people.person_id = assign.person_id
             AND LAST_DAY (pact.effective_date)
                    BETWEEN assign.effective_start_date
                        AND assign.effective_end_date
             AND assign.payroll_id = pact.payroll_id
             and  PACT.CONSOLIDATION_SET_ID = 62--IN (1063,1064,1070)
             AND pact.payroll_action_id = asact.payroll_action_id
             AND assign.assignment_id = asact.assignment_id
             AND LAST_DAY (pact.effective_date) =
                                  TO_DATE (:p_payroll_end_date, 'DD-MM-RRRR')
             AND pact.action_type != 'V'
             AND assign.payroll_id = :p_payroll_id
             AND asact.assignment_action_id = prr.assignment_action_id
             AND prr.element_type_id = pet.element_type_id
             AND LAST_DAY (pact.effective_date) BETWEEN pet.effective_start_date
                                                    AND pet.effective_end_date
             AND prr.run_result_id = prrv.run_result_id
             AND prrv.input_value_id = piv.input_value_id
             AND LAST_DAY (pact.effective_date) BETWEEN piv.effective_start_date
                                                    AND piv.effective_end_date
             AND pet.processing_type IN ('R', 'N')
--and xx_hr_reports.show_person(:P_SEC_PROFILE_ID,PEOPLE.person_id) =1
--AND   PET.CLASSIFICATION_ID in (107,99,100)
             AND pec.classification_id = NVL (pet.classification_id, 0)
             AND pec.classification_name IN
                    ('Earnings',
                     'Voluntary Deductions',
                     'Involuntary Deductions'
                    )
             AND UPPER (piv.NAME) IN ('PAY VALUE', 'ENTITLEMENT')           
          UNION ALL        
          SELECT                         -- lpad(PEOPLE.employee_number,6,' ')
                 distinct people.employee_number employee_number, people.full_name,
                 assign.assignment_id assignment_id,
                 assign.person_id person_id,
--xxsse_hr_custom.employee_pay_status(ASSIGN.person_id,:p_payroll_end_date) action_type,
                                            st.user_status action_type,
                 petl.element_name,
--PEOPLE.first_name||' '||last_name full_name,
                                  --pet.reporting_name,
                 pet.element_type_id element_type_id,
                 piv.input_value_id input_value_id,
                 pet.classification_id classification_id, classification_name,
                 0 curr_amt,
--DECODE(CLASSIFICATION_ID,107,TO_NUMBER(prrv.RESULT_VALUE),100,-TO_NUMBER(prrv.RESULT_VALUE),99,-TO_NUMBER(prrv.RESULT_VALUE),TO_NUMBER(prrv.RESULT_VALUE)) prev_amt
                 0, 0, 0, 0,
                 DECODE (piv.NAME,
                         'Pay Value', DECODE (classification_name,
                                              'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                              'Voluntary Deductions', -TO_NUMBER
                                                                          (prrv.result_value
                                                                          ),
                                              'Involuntary Deductions', -TO_NUMBER
                                                                            (prrv.result_value
                                                                            ),
                                              TO_NUMBER (prrv.result_value)
                                             )
                        ) prev_amt,
                 DECODE
                    (pet.processing_type,
                     'N', DECODE (piv.NAME,
                                  'Pay Value', DECODE
                                               (classification_name,
                                                'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                'Voluntary Deductions', -TO_NUMBER
                                                                            (prrv.result_value
                                                                            ),
                                                'Involuntary Deductions', -TO_NUMBER
                                                                              (prrv.result_value
                                                                              ),
                                                TO_NUMBER (prrv.result_value)
                                               )
                                 )
                    ) prev_nonrec,
                 DECODE
                    (pet.processing_type,
                     'R', DECODE (piv.NAME,
                                  'Pay Value', DECODE
                                                (classification_name,
                                                 'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                 0
                                                )
                                 )
                    ) prev_recur_payvalue,
                 DECODE
                    (prr.entry_type,
                     'D', 0,
                     DECODE (pet.processing_type,
                             'R', DECODE
                                        (piv.NAME,
                                         'Entitlement', DECODE
                                                (classification_name,
                                                 'Earnings', TO_NUMBER
                                                            (prrv.result_value),
                                                 0
                                                )
                                        )
                            )
                    ) prev_recur_ent,
                 (DECODE (pet.processing_type,
                          'R', DECODE (piv.NAME,
                                       'Pay Value', DECODE
                                               (classification_name,
                                                'Involuntary Deductions', -TO_NUMBER
                                                                              (prrv.result_value
                                                                              ),
                                                0
                                               )
                                      )
                         )
                 ) curr_recur_deduction
            FROM per_people_f people,
                 per_all_assignments_f assign,
                 pay_payroll_actions pact,
                 pay_assignment_actions asact,
                 pay_run_results prr,
                 pay_run_result_values prrv,
                 pay_element_types_f pet,
                pay_element_types_f_tl petl,
                 pay_input_values_f piv,
                 hr_lookups hr,
                 per_assignment_status_types st,
                 pay_element_classifications pec,
                 hr_all_organization_units haou,
                 hr_assignment_set_amendments haam
           WHERE people.business_group_id = nvl(:p_business_group_id,people.business_group_id)
--           and employee_number = '9152'
             AND haou.organization_id = assign.organization_id
             and  petl.element_type_id = pet.element_type_id and petl.language = userenv('lang')
             AND haou.organization_id =
                                       NVL (:p_org_name, haou.organization_id)
             AND assign.assignment_id = haam.assignment_id(+)
             AND people.person_id = NVL (:p_person_id, people.person_id)
             AND hr.lookup_code = st.per_system_status
             AND st.assignment_status_type_id =
                                              assign.assignment_status_type_id
             AND LAST_DAY (pact.effective_date)
                    BETWEEN people.effective_start_date
                        AND people.effective_end_date
             AND people.person_id = assign.person_id
            and  PACT.CONSOLIDATION_SET_ID = 62--IN (1063,1064,1070)
             AND LAST_DAY (pact.effective_date)
                    BETWEEN assign.effective_start_date
                        AND assign.effective_end_date
             AND assign.payroll_id = pact.payroll_id
             AND pact.payroll_action_id = asact.payroll_action_id
             AND assign.assignment_id = asact.assignment_id
             AND LAST_DAY (pact.effective_date) =
                    ADD_MONTHS (TO_DATE (:p_payroll_end_date, 'DD-MM-RRRR'),
                                -1
                               )
             AND pact.action_type != 'V'
             AND assign.payroll_id = :p_payroll_id
             AND asact.assignment_action_id = prr.assignment_action_id
             AND prr.element_type_id = pet.element_type_id
             AND LAST_DAY (pact.effective_date) BETWEEN pet.effective_start_date
                                                    AND pet.effective_end_date
             AND prr.run_result_id = prrv.run_result_id
             AND prrv.input_value_id = piv.input_value_id
             AND LAST_DAY (pact.effective_date) BETWEEN piv.effective_start_date
                                                    AND piv.effective_end_date
             AND pet.processing_type IN ('R', 'N')
--and xx_hr_reports.show_person(:P_SEC_PROFILE_ID,PEOPLE.person_id) =1
--AND   PET.CLASSIFICATION_ID in (107,99,100)
             AND pec.classification_id = NVL (pet.classification_id, 0)
             AND pec.classification_name IN
                    ('Earnings',
                     'Voluntary Deductions',
                     'Involuntary Deductions'
                    )
             AND UPPER (piv.NAME) IN ('PAY VALUE', 'ENTITLEMENT')          
 ) Q1      
      
GROUP BY employee_number,
         assignment_id,
         person_id,
--         reporting_name,
         element_name,
--action_type,
         full_name,
         element_type_id,
--input_value_id,
         classification_id,
         classification_name
HAVING ROUND(SUM(NVL(curr_amt,0)) - SUM(NVL(prev_amt,0)),2) <> 0
--AND SUM(NVL(curr_amt,0))>0
/*HAVING   (   ROUND (SUM (NVL (curr_nonrec, 0)), 2) <> 0
          OR (ROUND (SUM (NVL (curr_recur_payvalue, 0)), 2) <>
                                      ROUND (SUM (NVL (curr_recur_ent, 0)), 2)
             )
          OR (ROUND (SUM (NVL (curr_recur_ent, 0)), 2) <>
                                      ROUND (SUM (NVL (prev_recur_ent, 0)), 2)
             )
          OR ((  SUM (NVL (prev_recur_ent, 0))
               + SUM (NVL (prev_recur_deduction, 0))
              ) <>
                 (  SUM (NVL (curr_recur_ent, 0))
                  + SUM (NVL (curr_recur_deduction, 0))
                 )
             )
         )*/
ORDER BY (Q1.employee_number)

No comments:

Post a Comment