Saturday, November 14, 2009

Expesne Reports to Invoice - Line Justification

When payables imports expense reports into the invoice, expense report line justification is not carried over. A simple trigger on invoice distribution could solve the problem.

Here is the trigger, note the difference between 1159 and 11510 versions
.
CREATE OR REPLACE TRIGGER copy_ap_expense_rpt_just
--
-- Trigger on Expense Lines table to copy Justification to Item Description 
--
  BEFORE INSERT ON ap_invoice_distributions_all
  REFERENCING NEW AS NEW OLD AS OLD
  FOR EACH ROW
DECLARE
  lv_justification VARCHAR2(240);
BEGIN
    --
    -- For 1159
    --
    IF INSERTING THEN
      SELECT EXPRL.justification
        INTO lv_justification
        FROM ap_expense_report_lines_all   EXPRL
            ,ap_expense_report_headers_all EXPRH
            ,ap_invoices_all               APINV
       WHERE EXPRL.report_header_id    = EXPRH.report_header_id
         AND EXPRH.invoice_num         = APINV.invoice_num
         AND APINV.invoice_id          = :NEW.invoice_id
         AND EXPRL.code_combination_id = :NEW.dist_code_combination_id
         AND EXPRL.item_description    = :NEW.description
         AND EXPRL.start_expense_date  = :NEW.start_expense_date
         AND EXPRL.amount              = :NEW.amount;

         :NEW.description := lv_justification;
    END IF;
    --
    -- For 11510
    --
    IF INSERTING THEN
      SELECT EXPRL.justification
        INTO lv_justification
        FROM ap_expense_report_lines_all   EXPRL
            ,ap_expense_report_headers_all EXPRH
            ,ap_invoices_all               APINV
            ,ap_exp_report_dists_all       EXPDIST
       WHERE EXPRL.report_header_id      = EXPRH.report_header_id
         AND EXPRH.invoice_num           = APINV.invoice_num
         AND EXPRL.report_line_id        = EXPDIST.report_line_id
         AND APINV.invoice_id            = :NEW.invoice_id
         AND EXPDIST.code_combination_id = :NEW.dist_code_combination_id
         AND EXPRL.item_description      = :NEW.description
         AND EXPRL.start_expense_date    = :NEW.start_expense_date
         AND EXPRL.amount                = :NEW.amount;

      :NEW.description := lv_justification;
    END IF;

EXCEPTION
   WHEN NO_DATA_FOUND THEN
     NULL; -- Do nothing as this is not a ER Invoice
   WHEN OTHERS THEN
     fnd_log.string(fnd_log.level_exception
                   ,'copy_ap_expense_rpt_just'
                   ,'Exception ' ||SQLERRM);   
END copy_ap_expense_rpt_just;

No comments: