database/model/functions/A_ASSET_GROUP_ACCT_COPY.xml
author Antonio Moreno <antonio.moreno@openbravo.com>
Tue, 22 Apr 2008 13:22:11 +0000
changeset 735 daced7e311c9
parent 239 a30acae847e1
permissions -rw-r--r--
Structure after exporting from PostgreSQL with new dbsourcemanager
<?xml version="1.0"?>
  <database name="FUNCTION A_ASSET_GROUP_ACCT_COPY">
    <function name="A_ASSET_GROUP_ACCT_COPY" type="NULL">
      <parameter name="pinstance_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* 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 Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SL
* All portions are Copyright (C) 2001-2006 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  -- Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Record_ID NUMBER;
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    --
    v_A_Asset_Group_ID NUMBER;
    Cur_Acct RECORD;
    --
    v_Updated NUMBER:=0;
    v_Created NUMBER:=0;
    v_Updated_Total NUMBER:=0;
    v_Created_Total NUMBER:=0;
    rowcount NUMBER;
  BEGIN
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || PInstance_ID) ;
    v_ResultStr:='PInstanceNotFound';
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'Y', NULL, NULL) ;
  BEGIN --BODY
    -- Get Parameters
    v_ResultStr:='ReadingParameters';
    FOR Cur_Parameter IN
      (SELECT i.Record_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=PInstance_ID
      ORDER BY p.SeqNo
      )
    LOOP
      v_Record_ID:=Cur_Parameter.Record_ID;
      IF(Cur_Parameter.ParameterName='A_Asset_Group_ID') THEN
        v_A_Asset_Group_ID:=Cur_Parameter.P_Number;
        DBMS_OUTPUT.PUT_LINE('  A_Asset_Group_ID=' || v_A_Asset_Group_ID) ;
      ELSE
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
      END IF;
    END LOOP; -- Get Parameter
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
    /**
    * For all Accounting Schema of the Category
    */
    FOR Cur_Acct IN
      (SELECT *  FROM A_Asset_Group_Acct  WHERE A_Asset_Group_ID=v_A_Asset_Group_ID)
    LOOP
      -- Update existing Assets
      UPDATE A_Asset_Acct
        SET A_DEPRECIATION_ACCT=Cur_Acct.A_DEPRECIATION_ACCT, A_ACCUMDEPRECIATION_ACCT=Cur_Acct.A_ACCUMDEPRECIATION_ACCT, A_DISPOSAL_LOSS=Cur_Acct.A_DISPOSAL_LOSS, A_DISPOSAL_GAIN=Cur_Acct.A_DISPOSAL_GAIN, updated=now(), UpdatedBy=0
      WHERE A_Asset_Acct.C_AcctSchema_ID=Cur_Acct.C_AcctSchema_ID  AND exists
        (SELECT 1
        FROM A_Asset a
        WHERE a.A_Asset_ID=A_Asset_Acct.A_Asset_ID  AND a.A_Asset_Group_ID=Cur_Acct.A_Asset_Group_ID
        )
        ;
      rowcount:=SQL%ROWCOUNT;
      v_Updated:=rowcount;
      -- Insert new Assets
      INSERT
      INTO A_Asset_Acct
        (
          A_ASSET_ID, C_ACCTSCHEMA_ID, AD_CLIENT_ID, AD_ORG_ID,
          ISACTIVE, CREATED, CREATEDBY, UPDATED,
          UPDATEDBY, A_DEPRECIATION_ACCT, A_ACCUMDEPRECIATION_ACCT, A_DISPOSAL_LOSS,
          A_DISPOSAL_GAIN
        )
      SELECT a.A_ASSET_ID, Cur_Acct.C_ACCTSCHEMA_ID, a.AD_CLIENT_ID, a.AD_ORG_ID, 'Y', now(), 0, now(), 0, Cur_Acct.A_DEPRECIATION_ACCT, Cur_Acct.A_ACCUMDEPRECIATION_ACCT, Cur_Acct.A_DISPOSAL_LOSS, Cur_Acct.A_DISPOSAL_GAIN
      FROM A_Asset a
      WHERE a.A_Asset_Group_ID=Cur_Acct.A_Asset_Group_ID  AND NOT EXISTS
        (SELECT 1
        FROM a_Asset_Acct aa
        WHERE aa.A_Asset_ID=a.A_Asset_Id  AND aa.C_AcctSchema_ID=Cur_Acct.C_AcctSchema_ID
        )
        ;
      rowcount:=SQL%ROWCOUNT;
      v_Created:=rowcount;
      DBMS_OUTPUT.PUT_LINE('Asset = ' || v_Updated || ' / ' || v_Created) ;
      v_Updated_Total:=v_Updated_Total + v_Updated;
      v_Created_Total:=v_Created_Total + v_Created;
    END LOOP;
    v_Message:='@Created@=' || v_Created_Total || ', @Updated@=' || v_Updated_Total;
    --<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 1, v_Message) ;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  RETURN;
END A_ASSET_GROUP_ACCT_COPY
]]></body>
    </function>
  </database>