src-db/database/model/functions/GL_JOURNAL_POST.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 980 f2c4a1d793cb
child 1929 ad0353d93b0f
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="FUNCTION GL_JOURNAL_POST">
    <function name="GL_JOURNAL_POST" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_gl_journal_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
  * The contents of this file are subject to the Compiere Public
  * License 1.1 ("License"); You may not use this file except in
  * compliance with the License. You may obtain a copy of the License in
  * the legal folder of your Openbravo installation.
  * Software distributed under the License is distributed on an
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  * implied. See the License for the specific language governing rights
  * and limitations under the License.
  * The Original Code is  Compiere  ERP &  Business Solution
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
  * All Rights Reserved.
  * Contributor(s): Openbravo SL
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
  *
  * Specifically, this derivative work is based upon the following Compiere
  * file and version.
  *************************************************************************
  * $Id: GL_Journal_Post.sql,v 1.2 2002/05/22 02:48:28 jjanke Exp $
  ***
  * Title: GL Journal Post (preparation)
  * Description:
  * - Check Control Amount
  ************************************************************************/
  -- Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Result NUMBER:=1; -- 0=failure
  v_Record_ID VARCHAR2(32);
  v_AD_User_ID VARCHAR2(32);
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    -- Parameter Variables
    -- Other Variables
    v_DocStatus GL_Journal.DocStatus%TYPE;
    v_DocAction GL_Journal.DocAction%TYPE;
    v_Processing GL_Journal.Processing%TYPE;
    v_Posted GL_Journal.Posted%TYPE;
    v_TotalDr GL_Journal.TotalDr%TYPE;
    v_TotalCr GL_Journal.TotalCr%TYPE;
    v_ControlAmt GL_Journal.ControlAmt%TYPE;
    FINISH_PROCESS BOOLEAN:=false;
  BEGIN
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
    IF(p_PInstance_ID IS NOT NULL) THEN
      v_ResultStr:='PInstanceNotFound';
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
      -- Get Parameters
      v_ResultStr:='ReadingParameters';
      FOR Cur_Parameter IN
        (SELECT i.Record_ID,
          i.AD_User_ID,
          p.ParameterName,
          p.P_String,
          p.P_Number,
          p.P_Date
        FROM AD_PInstance i
        LEFT JOIN AD_PInstance_Para p
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
        WHERE i.AD_PInstance_ID=p_PInstance_ID
        ORDER BY p.SeqNo
        )
      LOOP
        v_Record_ID:=Cur_Parameter.Record_ID;
        v_AD_User_ID:=Cur_Parameter.AD_User_ID;
      END LOOP; -- Get Parameter
    ELSE
      v_Record_ID:=p_GL_Journal_ID;
    END IF;
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
  BEGIN --BODY
    /**
    * Status - Actions
    * DR   CO, VO
    * CO   CL
    */
    v_ResultStr:='ReadingJournal';
    SELECT DocStatus,
      DocAction,
      Processing,
      Posted
    INTO v_DocStatus,
      v_DocAction,
      v_Processing,
      v_Posted
    FROM GL_Journal
    WHERE GL_Journal_ID=v_Record_ID  FOR UPDATE;
    /**
    * Closed, Voided or Reversed - No action possible
    */
    IF(v_DocStatus IN('CL', 'VO', 'RE')) THEN
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
    END IF;
    /**
    * Unlock
    */
    IF(NOT FINISH_PROCESS) THEN
      IF(v_DocAction='XL') THEN
        UPDATE GL_Journal
          SET Processing='N',
          DocAction='--',
          Updated=now(),
          UpdatedBy=v_AD_User_ID
        WHERE GL_Journal_ID=v_Record_ID;
        FINISH_PROCESS:=true;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      IF(v_Processing='Y') THEN
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
      END IF;
    END IF; --FINISH_PROCESS
    /**
    * Close Order
    */
    IF(NOT FINISH_PROCESS) THEN
      IF(v_DocAction='CL') THEN
        UPDATE GL_Journal
          SET DocStatus='CL',
          DocAction='--',
          Processed='Y',
          Updated=now(),
          UpdatedBy=v_AD_User_ID
        WHERE GL_Journal_ID=v_Record_ID;
        FINISH_PROCESS:=true;
      END IF;
    END IF; --FINISH_PROCESS
    /**
    * Void Order
    */
    IF(NOT FINISH_PROCESS) THEN
      IF(v_DocAction='VO' AND v_DocStatus='DR') THEN
        -- Cancel all Lines
        UPDATE GL_JournalLine
          SET IsActive='N',
          Updated=now()
        WHERE GL_Journal_ID=v_Record_ID;
        UPDATE GL_Journal
          SET DocStatus='VO',
          DocAction='--',
          Processed='Y',
          Updated=now()
        WHERE GL_Journal_ID=v_Record_ID;
        FINISH_PROCESS:=true;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      IF(v_DocAction='RE') THEN
        IF(v_Posted='Y') THEN
          RAISE_APPLICATION_ERROR(-20000, '@GLJournalDocumentPosted@') ;
        END IF;
        IF(NOT FINISH_PROCESS) THEN
            UPDATE GL_Journal
              SET DocStatus='DR',
              DocAction='CO',
              Processed='N',
              Updated=now()
            WHERE GL_Journal_ID=v_Record_ID;
            FINISH_PROCESS:=true;
        END IF;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      --  Lock journal comes here
      /**
      * Process Journal
      */
      -- Recalculate Totals
      v_ResultStr:='RecalculateTotals';
      UPDATE GL_Journal
        SET TotalDr=
        (SELECT COALESCE(SUM(AmtSourceDr), 0)
        FROM GL_JournalLine
        WHERE GL_Journal_ID=v_Record_ID
          AND IsActive='Y'
        )
        ,
        TotalCr=
        (SELECT COALESCE(SUM(AmtSourceCr), 0)
        FROM GL_JournalLine
        WHERE GL_Journal_ID=v_Record_ID
          AND IsActive='Y'
        )
      WHERE GL_Journal_ID=v_Record_ID;
      v_ResultStr:='ReadingAmounts';
      SELECT ControlAmt,
        TotalDr,
        TotalCr
      INTO v_ControlAmt,
        v_TotalDr,
        v_TotalCr
      FROM GL_Journal
      WHERE GL_Journal_ID=v_Record_ID  FOR UPDATE;
      IF(v_TotalDr<>v_TotalCr) THEN
        RAISE_APPLICATION_ERROR(-20000, '@DistinctAmtError@') ;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      -- Check ControlAmt
      IF(v_ControlAmt<>0 AND(v_ControlAmt<>v_TotalDr)) THEN
        RAISE_APPLICATION_ERROR(-20000, '@ControlAmtError@') ;
      ELSE
        UPDATE GL_Journal
          SET DocStatus='CO',
          DocAction='--',
          Processed='Y',
          Updated=now()
        WHERE GL_Journal_ID=v_Record_ID;
      END IF;
    END IF; --FINISH_PROCESS
    --<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    IF(p_PInstance_ID IS NOT NULL) THEN
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
    END IF;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  ROLLBACK;
  IF(p_PInstance_ID IS NOT NULL) THEN
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  ELSE
    RAISE_APPLICATION_ERROR(-20100, v_ResultStr) ;
  END IF;
  RETURN;
END GL_JOURNAL_POST
]]></body>
    </function>
  </database>