src-db/database/model/functions/S_TIMEEXPENSE_POST.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 785 8dba91261590
child 2586 683779f1e610
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION S_TIMEEXPENSE_POST">
adrian@94
     3
    <function name="S_TIMEEXPENSE_POST" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
juanpablo@771
     8
  * The contents of this file are subject to the Compiere Public
juanpablo@771
     9
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    10
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    11
  * the legal folder of your Openbravo installation.
carlos@0
    12
  * Software distributed under the License is distributed on an
carlos@0
    13
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    14
  * implied. See the License for the specific language governing rights
carlos@0
    15
  * and limitations under the License.
juanpablo@778
    16
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    17
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    18
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    19
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    20
  * All Rights Reserved.
carlos@0
    21
  * Contributor(s): Openbravo SL
juanpablo@785
    22
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    23
  *
juanpablo@771
    24
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    25
  * file and version.
carlos@0
    26
  *************************************************************************
carlos@0
    27
  * $Id: S_TimeExpense_Post.sql,v 1.4 2003/02/21 06:38:16 jjanke Exp $
carlos@0
    28
  ***
carlos@0
    29
  * Title: Process Time + Expenses
carlos@0
    30
  * Description:
carlos@0
    31
  ************************************************************************/
carlos@0
    32
  -- Logistice
carlos@0
    33
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    34
  v_Message VARCHAR2(2000):='';
carlos@0
    35
  v_Result NUMBER:=1; -- 0=failure
juanpablo@1605
    36
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    37
  v_AD_User_ID VARCHAR2(32);
carlos@0
    38
  v_DayHours  NUMBER;
carlos@0
    39
  v_Processed CHAR;
juanpablo@1605
    40
  v_CInvoicelineId varchar2(32);
juanpablo@1605
    41
  v_COrderlineId varchar2(32);
rafael@597
    42
  v_ExpenseLines NUMBER;
carlos@0
    43
    -- Parameter
carlos@0
    44
  TYPE RECORD IS REF CURSOR;
carlos@0
    45
  Cur_Parameter RECORD;
carlos@0
    46
    -- Parameter Variables
carlos@0
    47
BEGIN
carlos@0
    48
  --  Update AD_PInstance
carlos@0
    49
  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    50
  v_ResultStr:='PInstanceNotFound';
antonio@735
    51
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    52
BEGIN --BODY
carlos@0
    53
  -- Get Parameters
carlos@0
    54
  v_ResultStr:='ReadingParameters';
carlos@0
    55
  FOR Cur_Parameter IN
carlos@0
    56
    (SELECT i.Record_ID,
carlos@0
    57
      i.AD_User_ID,
carlos@0
    58
      p.ParameterName,
carlos@0
    59
      p.P_String,
carlos@0
    60
      p.P_Number,
carlos@0
    61
      p.P_Date
carlos@0
    62
    FROM AD_PInstance i
carlos@0
    63
    LEFT JOIN AD_PInstance_Para p
carlos@0
    64
      ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    65
    WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    66
    ORDER BY p.SeqNo
carlos@0
    67
    )
carlos@0
    68
  LOOP
carlos@0
    69
    v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    70
    v_AD_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    71
    -- IF (p.ParameterName = 'xx') THEN
carlos@0
    72
    --  p_xx := p.P_String;
carlos@0
    73
    --  DBMS_OUTPUT.PUT_LINE('  xx=' || p_xx);
carlos@0
    74
    -- ELSE
carlos@0
    75
    --  DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || p.ParameterName);
carlos@0
    76
    --  END IF;
carlos@0
    77
  END LOOP; -- Get Parameter
carlos@0
    78
  DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    79
  DECLARE
carlos@0
    80
    CUR_te RECORD;
carlos@0
    81
    v_Hours NUMBER;
carlos@0
    82
  BEGIN
antonio@737
    83
rafael@597
    84
  /* Check if there are expense lines to process */
rafael@597
    85
  SELECT COUNT(S_TimeExpenseLine.S_TimeExpenseLine_Id)
rafael@597
    86
  INTO v_ExpenseLines
rafael@597
    87
  FROM S_TimeExpenseLine, S_TimeExpense
rafael@597
    88
  WHERE S_TimeExpense.S_TimeExpense_ID = v_Record_ID
rafael@597
    89
    AND S_TimeExpenseLine.S_TimeExpense_Id = S_TimeExpense.S_TimeExpense_ID
rafael@597
    90
    AND S_TimeExpenseLine.IsActive = 'Y';
rafael@597
    91
  IF (v_ExpenseLines = 0) THEN
rafael@597
    92
    RAISE_APPLICATION_ERROR(-20000, '@NoExpenseLines@'||'.');
rafael@597
    93
  END IF;
antonio@737
    94
carlos@508
    95
    -- For ease id having access to all variables
rafael@597
    96
  SELECT S_TimeExpense.Processed,
rafael@597
    97
  MAX(S_TimeExpenseLine.C_InvoiceLine_ID), MAX(S_TimeExpenseLine.C_OrderLine_ID)
rafael@597
    98
  INTO v_Processed, v_CInvoicelineId, v_COrderlineId
rafael@597
    99
  FROM S_TimeExpense LEFT JOIN S_TimeExpenseLine ON S_TimeExpense.S_TimeExpense_ID = S_TimeExpenseLine.S_TimeExpense_ID
rafael@597
   100
  WHERE S_TimeExpense.S_TimeExpense_ID = v_Record_ID
rafael@597
   101
  GROUP BY S_TimeExpense.PROCESSED;
rafael@597
   102
  IF (v_Processed = 'Y') THEN
carlos@0
   103
    IF (v_CInvoicelineId IS NULL AND v_COrderlineId IS NULL) THEN
carlos@0
   104
        UPDATE S_TimeExpense SET Processed = 'N' WHERE S_TimeExpense_ID = v_Record_ID;
carlos@0
   105
    ELSE
carlos@0
   106
        v_Message:='@DocumentAssociated@';
carlos@0
   107
     v_Result:=0;
carlos@0
   108
    END IF;
rafael@597
   109
  ELSE
carlos@0
   110
     FOR CUR_te IN
carlos@0
   111
       (SELECT te.S_TimeExpense_ID, te.DateReport, te.C_BPartner_ID, te.Description, SUM(CASE tel.ISTIMEREPORT WHEN 'Y' THEN tel.qty ELSE 0 END) AS totalhours
carlos@0
   112
        FROM S_TimeExpense te, S_TimeExpenseLine tel
carlos@0
   113
        WHERE te.S_TimeExpense_ID=v_Record_ID
carlos@0
   114
          AND te.S_TimeExpense_ID=tel.S_TimeExpense_ID
carlos@0
   115
          AND te.Processed='N'
carlos@0
   116
        GROUP BY te.S_TimeExpense_ID, te.DateReport, te.C_BPartner_ID, te.Description
carlos@0
   117
       )
carlos@0
   118
     LOOP
carlos@0
   119
       v_ResultStr:='S_TimeExpense_ID=' || CUR_te.S_TimeExpense_ID;
carlos@0
   120
       DBMS_OUTPUT.PUT_LINE('S_TimeExpense_ID='||CUR_te.S_TimeExpense_ID||', DateReport='||CUR_te.DateReport||', C_BPartner_ID='||CUR_te.C_BPartner_ID||', Description='||CUR_te.Description) ;
carlos@0
   121
       -- Checks come here
carlos@0
   122
       -- Complete
carlos@0
   123
         UPDATE S_TimeExpense
carlos@0
   124
           SET Processed='Y',
antonio@735
   125
           Updated=now(),
carlos@0
   126
           UpdatedBy=v_AD_User_ID
carlos@0
   127
         WHERE S_TimeExpense_ID=CUR_te.S_TimeExpense_ID;
carlos@0
   128
     END LOOP;
rafael@597
   129
  END IF;
carlos@0
   130
  END;
gorkaion@239
   131
  --<<FINISH_PROCESS>>
carlos@0
   132
  --  Update AD_PInstance
carlos@0
   133
  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
antonio@735
   134
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   135
  RETURN;
carlos@0
   136
END; --BODY
carlos@0
   137
EXCEPTION
carlos@0
   138
WHEN OTHERS THEN
carlos@0
   139
   v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   140
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   141
  ROLLBACK;
antonio@735
   142
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   143
  RETURN;
antonio@735
   144
END S_TIMEEXPENSE_POST
gorkaion@239
   145
]]></body>
adrian@94
   146
    </function>
adrian@94
   147
  </database>