src-db/database/model/functions/GL_JOURNAL_POST.xml
author Carlos Romero <carlos.romero@openbravo.com>
Wed, 21 May 2008 15:46:39 +0000
changeset 980 f2c4a1d793cb
parent 785 8dba91261590
child 1605 8a0fe0193bef
permissions -rw-r--r--
Fixed bug [ 1811334 ] G/L Journal batch process does not report error properly
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION GL_JOURNAL_POST">
adrian@94
     3
    <function name="GL_JOURNAL_POST" type="NULL">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
antonio@735
     7
      <parameter name="p_gl_journal_id" type="NUMERIC" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
juanpablo@771
    11
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    12
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    13
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    14
  * the legal folder of your Openbravo installation.
carlos@0
    15
  * Software distributed under the License is distributed on an
carlos@0
    16
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    17
  * implied. See the License for the specific language governing rights
carlos@0
    18
  * and limitations under the License.
juanpablo@778
    19
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    20
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    21
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    22
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    23
  * All Rights Reserved.
carlos@0
    24
  * Contributor(s): Openbravo SL
juanpablo@785
    25
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    26
  *
juanpablo@771
    27
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    28
  * file and version.
carlos@0
    29
  *************************************************************************
carlos@0
    30
  * $Id: GL_Journal_Post.sql,v 1.2 2002/05/22 02:48:28 jjanke Exp $
carlos@0
    31
  ***
carlos@0
    32
  * Title: GL Journal Post (preparation)
carlos@0
    33
  * Description:
carlos@0
    34
  * - Check Control Amount
carlos@0
    35
  ************************************************************************/
carlos@0
    36
  -- Logistice
carlos@0
    37
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    38
  v_Message VARCHAR2(2000):='';
carlos@0
    39
  v_Result NUMBER:=1; -- 0=failure
carlos@0
    40
  v_Record_ID NUMBER;
carlos@0
    41
  v_AD_User_ID NUMBER;
carlos@0
    42
  -- Parameter
carlos@0
    43
  TYPE RECORD IS REF CURSOR;
carlos@0
    44
    Cur_Parameter RECORD;
carlos@0
    45
    -- Parameter Variables
carlos@0
    46
    -- Other Variables
carlos@0
    47
    v_DocStatus GL_Journal.DocStatus%TYPE;
carlos@0
    48
    v_DocAction GL_Journal.DocAction%TYPE;
carlos@0
    49
    v_Processing GL_Journal.Processing%TYPE;
carlos@0
    50
    v_Posted GL_Journal.Posted%TYPE;
carlos@0
    51
    v_TotalDr GL_Journal.TotalDr%TYPE;
carlos@0
    52
    v_TotalCr GL_Journal.TotalCr%TYPE;
carlos@0
    53
    v_ControlAmt GL_Journal.ControlAmt%TYPE;
carlos@0
    54
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    55
  BEGIN
carlos@0
    56
    --  Update AD_PInstance
carlos@0
    57
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    58
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    59
      v_ResultStr:='PInstanceNotFound';
carlos@0
    60
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    61
      -- Get Parameters
carlos@0
    62
      v_ResultStr:='ReadingParameters';
carlos@0
    63
      FOR Cur_Parameter IN
carlos@0
    64
        (SELECT i.Record_ID,
carlos@0
    65
          i.AD_User_ID,
carlos@0
    66
          p.ParameterName,
carlos@0
    67
          p.P_String,
carlos@0
    68
          p.P_Number,
carlos@0
    69
          p.P_Date
carlos@0
    70
        FROM AD_PInstance i
carlos@0
    71
        LEFT JOIN AD_PInstance_Para p
carlos@0
    72
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    73
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    74
        ORDER BY p.SeqNo
carlos@0
    75
        )
carlos@0
    76
      LOOP
carlos@0
    77
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    78
        v_AD_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    79
      END LOOP; -- Get Parameter
carlos@0
    80
    ELSE
carlos@0
    81
      v_Record_ID:=p_GL_Journal_ID;
carlos@0
    82
    END IF;
carlos@0
    83
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    84
  BEGIN --BODY
carlos@0
    85
    /**
carlos@0
    86
    * Status - Actions
carlos@0
    87
    * DR   CO, VO
carlos@0
    88
    * CO   CL
carlos@0
    89
    */
carlos@0
    90
    v_ResultStr:='ReadingJournal';
carlos@0
    91
    SELECT DocStatus,
carlos@0
    92
      DocAction,
carlos@0
    93
      Processing,
carlos@0
    94
      Posted
carlos@0
    95
    INTO v_DocStatus,
carlos@0
    96
      v_DocAction,
carlos@0
    97
      v_Processing,
carlos@0
    98
      v_Posted
carlos@0
    99
    FROM GL_Journal
carlos@0
   100
    WHERE GL_Journal_ID=v_Record_ID  FOR UPDATE;
carlos@0
   101
    /**
carlos@0
   102
    * Closed, Voided or Reversed - No action possible
carlos@0
   103
    */
carlos@0
   104
    IF(v_DocStatus IN('CL', 'VO', 'RE')) THEN
carlos@0
   105
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
carlos@0
   106
    END IF;
carlos@0
   107
    /**
carlos@0
   108
    * Unlock
carlos@0
   109
    */
carlos@0
   110
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   111
      IF(v_DocAction='XL') THEN
carlos@0
   112
        UPDATE GL_Journal
carlos@0
   113
          SET Processing='N',
carlos@0
   114
          DocAction='--',
carlos@0
   115
          Updated=now(),
carlos@0
   116
          UpdatedBy=v_AD_User_ID
carlos@0
   117
        WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   118
        FINISH_PROCESS:=true;
carlos@0
   119
      END IF;
carlos@0
   120
    END IF; --FINISH_PROCESS
carlos@0
   121
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   122
      IF(v_Processing='Y') THEN
carlos@0
   123
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
carlos@0
   124
      END IF;
carlos@0
   125
    END IF; --FINISH_PROCESS
carlos@0
   126
    /**
carlos@0
   127
    * Close Order
carlos@0
   128
    */
carlos@0
   129
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   130
      IF(v_DocAction='CL') THEN
carlos@0
   131
        UPDATE GL_Journal
carlos@0
   132
          SET DocStatus='CL',
carlos@0
   133
          DocAction='--',
carlos@0
   134
          Processed='Y',
carlos@0
   135
          Updated=now(),
carlos@0
   136
          UpdatedBy=v_AD_User_ID
carlos@0
   137
        WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   138
        FINISH_PROCESS:=true;
carlos@0
   139
      END IF;
carlos@0
   140
    END IF; --FINISH_PROCESS
carlos@0
   141
    /**
carlos@0
   142
    * Void Order
carlos@0
   143
    */
carlos@0
   144
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   145
      IF(v_DocAction='VO' AND v_DocStatus='DR') THEN
carlos@0
   146
        -- Cancel all Lines
carlos@0
   147
        UPDATE GL_JournalLine
carlos@0
   148
          SET IsActive='N',
carlos@0
   149
          Updated=now()
carlos@0
   150
        WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   151
        UPDATE GL_Journal
carlos@0
   152
          SET DocStatus='VO',
carlos@0
   153
          DocAction='--',
carlos@0
   154
          Processed='Y',
carlos@0
   155
          Updated=now()
carlos@0
   156
        WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   157
        FINISH_PROCESS:=true;
carlos@0
   158
      END IF;
carlos@0
   159
    END IF; --FINISH_PROCESS
carlos@0
   160
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   161
      IF(v_DocAction='RE') THEN
carlos@0
   162
        IF(v_Posted='Y') THEN
carlos@0
   163
          RAISE_APPLICATION_ERROR(-20000, '@GLJournalDocumentPosted@') ;
carlos@0
   164
        END IF;
carlos@0
   165
        IF(NOT FINISH_PROCESS) THEN
carlos@0
   166
            UPDATE GL_Journal
carlos@0
   167
              SET DocStatus='DR',
carlos@0
   168
              DocAction='CO',
carlos@0
   169
              Processed='N',
carlos@0
   170
              Updated=now()
carlos@0
   171
            WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   172
            FINISH_PROCESS:=true;
carlos@0
   173
        END IF;
carlos@0
   174
      END IF;
carlos@0
   175
    END IF; --FINISH_PROCESS
carlos@0
   176
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   177
      --  Lock journal comes here
carlos@0
   178
      /**
carlos@0
   179
      * Process Journal
carlos@0
   180
      */
carlos@0
   181
      -- Recalculate Totals
carlos@0
   182
      v_ResultStr:='RecalculateTotals';
carlos@0
   183
      UPDATE GL_Journal
carlos@0
   184
        SET TotalDr=
carlos@0
   185
        (SELECT COALESCE(SUM(AmtSourceDr), 0)
carlos@0
   186
        FROM GL_JournalLine
carlos@0
   187
        WHERE GL_Journal_ID=v_Record_ID
carlos@0
   188
          AND IsActive='Y'
carlos@0
   189
        )
carlos@0
   190
        ,
carlos@0
   191
        TotalCr=
carlos@0
   192
        (SELECT COALESCE(SUM(AmtSourceCr), 0)
carlos@0
   193
        FROM GL_JournalLine
carlos@0
   194
        WHERE GL_Journal_ID=v_Record_ID
carlos@0
   195
          AND IsActive='Y'
carlos@0
   196
        )
carlos@0
   197
      WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   198
      v_ResultStr:='ReadingAmounts';
carlos@0
   199
      SELECT ControlAmt,
carlos@0
   200
        TotalDr,
carlos@0
   201
        TotalCr
carlos@0
   202
      INTO v_ControlAmt,
carlos@0
   203
        v_TotalDr,
carlos@0
   204
        v_TotalCr
carlos@0
   205
      FROM GL_Journal
carlos@0
   206
      WHERE GL_Journal_ID=v_Record_ID  FOR UPDATE;
gorkaion@239
   207
      IF(v_TotalDr<>v_TotalCr) THEN
carlos@0
   208
        RAISE_APPLICATION_ERROR(-20000, '@DistinctAmtError@') ;
carlos@0
   209
      END IF;
carlos@0
   210
    END IF; --FINISH_PROCESS
carlos@0
   211
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   212
      -- Check ControlAmt
gorkaion@239
   213
      IF(v_ControlAmt<>0 AND(v_ControlAmt<>v_TotalDr)) THEN
carlos@0
   214
        RAISE_APPLICATION_ERROR(-20000, '@ControlAmtError@') ;
carlos@0
   215
      ELSE
carlos@0
   216
        UPDATE GL_Journal
carlos@0
   217
          SET DocStatus='CO',
carlos@0
   218
          DocAction='--',
carlos@0
   219
          Processed='Y',
carlos@0
   220
          Updated=now()
carlos@0
   221
        WHERE GL_Journal_ID=v_Record_ID;
carlos@0
   222
      END IF;
carlos@0
   223
    END IF; --FINISH_PROCESS
gorkaion@239
   224
    --<<FINISH_PROCESS>>
carlos@0
   225
    --  Update AD_PInstance
carlos@0
   226
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   227
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   228
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   229
    END IF;
carlos@0
   230
    RETURN;
carlos@0
   231
  END; --BODY
carlos@0
   232
EXCEPTION
carlos@0
   233
WHEN OTHERS THEN
carlos@0
   234
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   235
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   236
  ROLLBACK;
carlos@0
   237
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   238
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   239
  ELSE
carlos@980
   240
    RAISE_APPLICATION_ERROR(-20100, v_ResultStr) ;
carlos@0
   241
  END IF;
carlos@0
   242
  RETURN;
antonio@735
   243
END GL_JOURNAL_POST
gorkaion@239
   244
]]></body>
adrian@94
   245
    </function>
adrian@94
   246
  </database>