src-db/database/model/functions/A_ASSET_POST.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 1273 3f48377a0023
child 1895 6c92cf572f7f
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="FUNCTION A_ASSET_POST">
    <function name="A_ASSET_POST" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_asset_id" type="VARCHAR" 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 VARCHAR2(32);
  v_Result NUMBER:=1; --  Success
  --  Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    --  Record Info
    v_count NUMBER;
    v_UpdatedBy A_ASSET.UpdatedBy%TYPE;
    v_Processing A_ASSET.Processing%TYPE;
    v_Processed A_ASSET.Processed%TYPE;
    v_DateAcct DATE;
    v_DocumentNo A_ASSET.DocumentNo%TYPE;
    BPartner_ID VARCHAR2(32);
    v_ACCTVALUEAMT NUMBER;
    v_AD_CLIENT_ID VARCHAR2(32);
    v_AD_ORG_ID VARCHAR2(32);
    v_AD_USER_ID VARCHAR2(32);
    v_AMORTIZATIONENDDATE DATE;
    v_AMORTIZATIONSTARTDATE DATE;
    v_AMORTIZATIONTYPE CHAR(2) ;
    v_AMORTIZATIONVALUEAMT NUMBER;
    v_AMORTIZATIONPORCENTAGE NUMBER;
    v_ASSETDEPRECIATIONDATE DATE;
    v_ASSETVALUEAMT NUMBER;
    v_C_CURRENCY_ID VARCHAR2(32);
    v_CREATEDBY varchar2(32);
    v_ISDEPRECIATED CHAR(1) ;
    v_RESIDUALASSETVALUEAMT NUMBER;
    v_USELIFEMONTHS NUMBER;
    v_USELIFEYEARS NUMBER;
    v_ASSETSCHEDULE CHAR(2) ;
    v_TOTAL_DAYS NUMBER;
    v_THIS_YEAR_DAYS NUMBER;
    v_BEGINING_DATE DATE;
    v_ENDING_DATE DATE;
    v_AUXAMT NUMBER;
    v_AMORTIZATIONAMT NUMBER;
    v_NEW_AMORTIZATION VARCHAR2(32);
    v_LINE NUMBER;
    v_AMORTIZATIONLINE varchar2(32);
    v_FIRST_DAY_DATE DATE;
    v_LAST_DAY_DATE DATE;
    v_PERCENTAGE NUMBER;
    v_AMOUNT NUMBER;
    v_CURRENCY_ID VARCHAR2(32);
    FINISH_PROCESS BOOLEAN:=false;
    v_DepreciatedLines NUMBER;
    v_DepreciatedPlan NUMBER;
    v_depreciatedValue NUMBER;
    v_Period NUMBER;
    v_DEPRECIATEDPREVIOUSAMT NUMBER;
    v_AMORTIZATIONCALCTYPE varchar2(2) ;
    v_PercentageGeneral NUMBER;
    v_TotalAmt NUMBER;
    v_Currency_Pre NUMBER:= 0;
    v_Inserted NUMBER:= 0;
    finish boolean;
  BEGIN
    IF(p_PInstance_ID IS NOT NULL) THEN
      --  Update AD_PInstance
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
      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;
      END LOOP; --  Get Parameter
      DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<A_Aset_Post>>') ;
      v_Record_ID:=p_Asset_ID;
    END IF;
  BEGIN --BODY
    /**
    *  Read Asset
    */
    v_ResultStr:='ReadingAsset';
    --Updating DepreciatedPlan
    UPDATE a_asset
      SET DepreciatedPlan=
      (SELECT coalesce(sum(AmortizationAmt), 0)
      FROM A_AmortizationLine
      WHERE A_asset_ID=v_Record_ID
      )
    WHERE a_Asset_ID=v_Record_ID;
    SELECT ACCTVALUEAMT, AD_CLIENT_ID, AD_ORG_ID, AD_USER_ID, AMORTIZATIONENDDATE, AMORTIZATIONSTARTDATE, AMORTIZATIONTYPE, AMORTIZATIONVALUEAMT, ANNUALAMORTIZATIONPORCENTAGE, ASSETDEPRECIATIONDATE, ASSETVALUEAMT, C_CURRENCY_ID, CREATEDBY, ISDEPRECIATED, PROCESSING, RESIDUALASSETVALUEAMT, USELIFEMONTHS, USELIFEYEARS, ASSETSCHEDULE, PROCESSED, C_CURRENCY_ID, depreciatedPlan, COALESCE(depreciatedValue,0), COALESCE(DEPRECIATEDPREVIOUSAMT,0), AMORTIZATIONCALCTYPE
    INTO v_ACCTVALUEAMT, v_AD_CLIENT_ID, v_AD_ORG_ID, v_AD_USER_ID, v_AMORTIZATIONENDDATE, v_AMORTIZATIONSTARTDATE, v_AMORTIZATIONTYPE, v_AMORTIZATIONVALUEAMT, v_AMORTIZATIONPORCENTAGE, v_ASSETDEPRECIATIONDATE, v_ASSETVALUEAMT, v_C_CURRENCY_ID, v_CREATEDBY, v_ISDEPRECIATED, v_PROCESSING, v_RESIDUALASSETVALUEAMT, v_USELIFEMONTHS, v_USELIFEYEARS, v_ASSETSCHEDULE, v_PROCESSED, v_CURRENCY_ID, v_DepreciatedPlan, v_depreciatedValue, v_DEPRECIATEDPREVIOUSAMT, v_AMORTIZATIONCALCTYPE
    FROM A_ASSET
    WHERE A_ASSET_ID=v_Record_ID;
    DBMS_OUTPUT.PUT_LINE('A_Asset_ID=' || v_Record_ID || ' - AMORTIZATIONTYPE=' || v_AMORTIZATIONTYPE) ;
    -- Restrictions...
    IF COALESCE(v_AMORTIZATIONVALUEAMT, 0)<=0 THEN
     RAISE_APPLICATION_ERROR(-20000, '@AmountNotDefined@') ;
    END IF;
    IF v_AMORTIZATIONCALCTYPE='PE' AND v_AMORTIZATIONPORCENTAGE IS NULL THEN
      RAISE_APPLICATION_ERROR(-20000, '@PercentageNotDefined@') ;
    END IF;
    IF v_AMORTIZATIONCALCTYPE!='PE' AND((v_ASSETSCHEDULE!='YE' AND v_USELIFEMONTHS IS NULL) OR(v_ASSETSCHEDULE='YE' AND v_USELIFEYEARS IS NULL)) THEN
      RAISE_APPLICATION_ERROR(-20000, '@PeriodNotDefined@') ;
    END IF;
    IF v_AMORTIZATIONSTARTDATE IS NULL THEN
      RAISE_APPLICATION_ERROR(-20000, '@StartDateNotDefined@') ;
    END IF;
    IF(v_Processing='Y') THEN
      RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
    END IF;
    /**************************************************************************
    *  Start Processing ------------------------------------------------------
    *************************************************************************/
    IF(NOT FINISH_PROCESS) THEN
      v_ResultStr:='LockingAsset';
      UPDATE A_ASSET  SET Processing='Y'  WHERE A_ASSET_ID=v_Record_ID;
      --we calculate the already completed number of cycles
      SELECT count(*)
      INTO v_DepreciatedLines
      FROM A_AmortizationLine al, A_Amortization am
      WHERE A_Asset_ID=V_Record_ID  AND am.A_Amortization_ID=al.A_Amortization_ID;
      --we get the standard precision for the selected currency
      SELECT STDPRECISION
      INTO v_Currency_Pre
      FROM C_CURRENCY
      WHERE C_CURRENCY_ID = v_C_CURRENCY_ID;
      IF(v_AMORTIZATIONTYPE='LI') THEN
        IF(v_ASSETSCHEDULE='YE' OR v_AMORTIZATIONCALCTYPE='PE') THEN
          if v_USELIFEYEARS=v_DepreciatedLines then
            FINISH_PROCESS:=true;
          end if;
          IF(NOT FINISH_PROCESS) THEN
            if v_DepreciatedLines>0 then
              SELECT to_number(to_char(max(startdate), 'YYYY')) - to_number(to_char(min(endDate), 'YYYY'))
              INTO v_Period
              FROM a_amortization am, a_amortizationline al
              WHERE al.a_amortization_id=am.a_amortization_id  AND al.a_asset_id=V_Record_ID;
              if(v_DepreciatedLines<>v_Period) and(v_DepreciatedLines<>(v_Period+1)) then
                RAISE_APPLICATION_ERROR(-20000, '@PeriodsDontMatch@') ;
              end if;
            end if;
          END IF; --FINISH_PROCESS
          IF(NOT FINISH_PROCESS) THEN
            if v_AMORTIZATIONCALCTYPE='PE' then
              v_PercentageGeneral:=v_AMORTIZATIONPORCENTAGE;
              v_UseLifeYears:=trunc(100/v_PercentageGeneral) ;
            else
              --  v_PercentageGeneral := 100 / v_USELIFEYEARS;
              v_PercentageGeneral:=((v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT-v_depreciatedPlan) *100/v_AMORTIZATIONVALUEAMT) /(v_USELIFEYEARS-v_DepreciatedLines) ;
              SELECT to_number(TO_DATE(ADD_MONTHS(v_AMORTIZATIONSTARTDATE, 12*v_USELIFEYEARS)) - v_AMORTIZATIONSTARTDATE)
              INTO v_TOTAL_DAYS
              FROM DUAL;
            end if;
            v_AMORTIZATIONVALUEAMT:=v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT;
            v_Count:=coalesce(v_depreciatedLines, 0) +1;
            v_BEGINING_DATE:=v_AMORTIZATIONSTARTDATE;
            v_FIRST_DAY_DATE:=TO_DATE('01-01-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
            v_LAST_DAY_DATE:=TO_DATE('31-12-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
            if v_Count>1 then
              v_BEGINING_DATE:=TO_DATE('31-12-' || to_char(TO_number(to_char(v_BEGINING_DATE, 'yyyy')) +v_Count-1), 'DD-MM-YYYY') ;
              v_USELIFEYEARS:=v_USELIFEYEARS+1;
            end if;
            v_AUXAMT:=0;
            v_PERCENTAGE:=0;
            v_TotalAmt:=coalesce(v_DepreciatedPlan, 0) ;
            finish:=false;
            WHILE not finish
            LOOP
              IF(v_COUNT=1 AND to_number(v_BEGINING_DATE-v_FIRST_DAY_DATE)<>0) THEN
                v_Percentage:=to_number(to_number(TO_DATE('31-12-'||to_char(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') -v_AMORTIZATIONSTARTDATE) /(TO_DATE('31-12-'||to_char(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') -TO_DATE('01-01-'||to_char(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY'))) * v_PercentageGeneral;
                v_USELIFEYEARS:=v_USELIFEYEARS+1;
              ELSE
                v_Percentage:=v_PercentageGeneral;
              END IF;
              v_COUNT:=v_COUNT + 1;
              v_ENDING_DATE:=TO_DATE('31-12-' ||TO_CHAR(v_BEGINING_DATE, 'YYYY'), 'DD-MM-YYYY') ;
              SELECT COALESCE(MAX(A_AMORTIZATION_ID), '-1')
              INTO v_NEW_AMORTIZATION
              FROM A_AMORTIZATION
              WHERE STARTDATE<=v_ENDING_DATE  AND ENDDATE>=v_ENDING_DATE  AND AD_CLIENT_ID=v_AD_CLIENT_ID  AND PROCESSED='N';
              IF(v_NEW_AMORTIZATION='-1') THEN
                Ad_Sequence_Next('A_Amortization', 1000000, v_NEW_AMORTIZATION) ;
                INSERT
                INTO A_AMORTIZATION
                  (
                    A_AMORTIZATION_ID, DATEACCT, AD_CLIENT_ID, AD_ORG_ID,
                  CREATED, CREATEDBY, UPDATED, UPDATEDBY,
                  ENDDATE,
                  ISACTIVE, NAME, POSTED,
                  PROCESSED, PROCESSING, STARTDATE, C_CURRENCY_ID
                  )
                  VALUES
                  (v_NEW_AMORTIZATION, v_ENDING_DATE, v_AD_CLIENT_ID, v_AD_ORG_ID,
                  now(), v_CREATEDBY, now(), v_CREATEDBY,
                  LAST_DAY(TO_DATE('01-' || TO_CHAR(v_ENDING_DATE, 'MM') || '-'|| TO_CHAR(v_ENDING_DATE, 'YYYY'),'DD-MM-YYYY')),
                  'Y', TO_CHAR(TO_DATE(LAST_DAY(TO_DATE('01-'|| TO_CHAR(v_ENDING_DATE, 'MM') || '-' || TO_CHAR(v_ENDING_DATE, 'YYYY'))))), 'N',
                  'N', 'N',
                  TO_DATE('01-' || TO_CHAR(v_ENDING_DATE, 'MM') || '-' || TO_CHAR(v_ENDING_DATE, 'YYYY'),'DD-MM-YYYY'), v_CURRENCY_ID);
              END IF;
              SELECT COALESCE(MAX(LINE), 0) +10
              INTO v_LINE
              FROM A_AMORTIZATIONLINE
              WHERE A_AMORTIZATION_ID=v_NEW_AMORTIZATION;
              IF((((v_AMORTIZATIONVALUEAMT+v_DEPRECIATEDPREVIOUSAMT) *v_PERCENTAGE/100)>(v_AMORTIZATIONVALUEAMT -v_TotalAmt))OR(v_Inserted+1>=v_USELIFEYEARS)) THEN
                SELECT COALESCE(SUM(AMORTIZATIONAMT),0), COALESCE(SUM(AMORTIZATION_PORCENTAGE),0)
                INTO v_AMOUNT, v_PERCENTAGE
                FROM A_AMORTIZATIONLINE
                WHERE A_ASSET_ID=v_Record_ID;
                v_AMOUNT:=v_AMORTIZATIONVALUEAMT - v_AMOUNT;
                v_PERCENTAGE:=v_AMOUNT*100/(v_AMORTIZATIONVALUEAMT+v_DEPRECIATEDPREVIOUSAMT) ;
                finish:=true;
              ELSE
                v_AMOUNT:=(v_AMORTIZATIONVALUEAMT+ v_DEPRECIATEDPREVIOUSAMT) *v_PERCENTAGE/100;
              END IF;
              if v_percentage>0 then
                Ad_Sequence_Next('A_Amortizationline', 1000000, v_AMORTIZATIONLINE) ;
                INSERT
                INTO A_AMORTIZATIONLINE
                  (
                    A_AMORTIZATION_ID, A_AMORTIZATIONLINE_ID, A_ASSET_ID, AD_CLIENT_ID,
                    AD_ORG_ID, CREATED, CREATEDBY, UPDATED,
                    UPDATEDBY, AMORTIZATION_PORCENTAGE, AMORTIZATIONAMT, C_CURRENCY_ID,
                    ISACTIVE, LINE
                  )
                  VALUES
                  (v_NEW_AMORTIZATION, v_AMORTIZATIONLINE, v_Record_ID, v_AD_CLIENT_ID, v_AD_ORG_ID, now(), v_CREATEDBY, now(), v_CREATEDBY, ROUND(v_PERCENTAGE,v_Currency_Pre), ROUND(v_AMOUNT,v_Currency_Pre), v_C_CURRENCY_ID, 'Y', v_LINE) ;
                 v_Inserted := v_Inserted +1;
              end if;
              v_BEGINING_DATE:=TO_DATE('31-12-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY') + 1;
              v_TotalAmt:=v_TotalAmt + v_Amount;
            END LOOP;
            FINISH_process:=TRUE;
          END IF; --FINISH_PROCESS
        END IF;
        IF(NOT FINISH_PROCESS) THEN
          IF(v_ASSETSCHEDULE='MO') THEN
            if v_USELIFEMonths=v_DepreciatedLines then
              FINISH_PROCESS:=true;
            end if;
          END IF; --FINISH_PROCESS
          IF(NOT FINISH_PROCESS) THEN
            if v_DepreciatedLines>0 then
              SELECT trunc(months_Between(max(startdate), min(endDate))) +1
              INTO v_Period
              FROM a_amortization am, a_amortizationline al
              WHERE al.a_amortization_id=am.a_amortization_id  AND al.a_asset_id=V_Record_ID;
              if(v_DepreciatedLines<>v_Period) and(v_DepreciatedLines<>(v_Period+1)) then
                RAISE_APPLICATION_ERROR(-20000, '@PeriodsDontMatch@') ;
              end if;
            end if;
          END IF; --FINISH_PROCESS
          IF(NOT FINISH_PROCESS) THEN
            v_AMORTIZATIONVALUEAMT:=v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT;
            if v_AMORTIZATIONCALCTYPE='PE' then
              v_PercentageGeneral:=v_AMORTIZATIONPORCENTAGE/12;
              v_UseLifeMonths:=trunc(100/v_AMORTIZATIONPORCENTAGE*12) ;
            else
              --  v_PercentageGeneral := 100 / v_USELIFEYEARS;
              v_UseLifeYears:=v_UseLifeMonths/12;
              v_PercentageGeneral:=(((v_AMORTIZATIONVALUEAMT-v_depreciatedPlan) *100/v_AMORTIZATIONVALUEAMT) /(v_USELIFEYEARS-v_DepreciatedLines)) /12;
              SELECT to_number(TO_DATE(ADD_MONTHS(v_AMORTIZATIONSTARTDATE, 12*v_USELIFEYEARS)) - TO_DATE(v_AMORTIZATIONSTARTDATE))
              INTO v_TOTAL_DAYS
              FROM DUAL;
            end if;
            v_Count:=coalesce(v_depreciatedLines, 0) +1;
            v_BEGINING_DATE:=v_AMORTIZATIONSTARTDATE;
            v_FIRST_DAY_DATE:=TO_DATE('01-'|| TO_CHAR(v_AMORTIZATIONSTARTDATE, 'MM') || '-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
            v_LAST_DAY_DATE:=TO_DATE('31-12-'|| TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
            if v_Count>1 then
              v_BEGINING_DATE:=TO_DATE(ADD_MONTHS(LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY')), v_DepreciatedLines)) ;
              v_USELIFEMonths:=v_USELIFEMonths+1;
            end if;
            v_AUXAMT:=0;
            v_PERCENTAGE:=0;
            v_TotalAmt:=coalesce(v_DepreciatedPlan, 0) ;
            finish:=false;
            while not finish
            loop
              IF(v_COUNT=1 AND to_number(v_BEGINING_DATE-v_FIRST_DAY_DATE)<>0) THEN
                v_Percentage:=to_number(to_number(last_day(v_BEGINING_DATE) -v_BEGINING_DATE)) / (trunc((last_day(v_BEGINING_DATE) -(TO_DATE('01-'||to_char(TO_DATE(v_BEGINING_DATE), 'MM-YYYY'), 'DD-MM-YYYY')))) +1) * v_PercentageGeneral;
                v_USELIFEMONTHS:=v_USELIFEMONTHS+1;
              ELSE
                v_Percentage:=v_PercentageGeneral;
              END IF;
              v_COUNT:=v_COUNT + 1;
              SELECT COALESCE(MAX(A_AMORTIZATION_ID), '-1')
              INTO v_NEW_AMORTIZATION
              FROM A_AMORTIZATION
              WHERE STARTDATE<=v_BEGINING_DATE  AND ENDDATE>=v_BEGINING_DATE  AND AD_CLIENT_ID=v_AD_CLIENT_ID  AND PROCESSED='N';
              v_ENDING_DATE:= LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY')) ;
              IF(v_NEW_AMORTIZATION='-1') THEN
                Ad_Sequence_Next('A_Amortization', 1000000, v_NEW_AMORTIZATION) ;
                INSERT
                INTO A_AMORTIZATION
                  (
                    A_AMORTIZATION_ID, DATEACCT, AD_CLIENT_ID, AD_ORG_ID,
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
                    ENDDATE, ISACTIVE, NAME, POSTED,
                    PROCESSED, PROCESSING, STARTDATE, C_Currency_ID
                  )
                  VALUES
                  (v_NEW_AMORTIZATION, v_ENDING_DATE, v_AD_CLIENT_ID, v_AD_ORG_ID, now(), v_CREATEDBY, now(), v_CREATEDBY, LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY')), 'Y', TO_CHAR(TO_DATE(LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'YYYY'), 'DD-MM-YYYY')))), 'N', 'N', 'N', TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY'), v_CURRENCY_ID) ;
              END IF;
              SELECT COALESCE(MAX(LINE), 0) +10
              INTO v_LINE
              FROM A_AMORTIZATIONLINE
              WHERE A_AMORTIZATION_ID=v_NEW_AMORTIZATION;
              IF(((v_AMORTIZATIONVALUEAMT*v_PERCENTAGE/100)>(v_AMORTIZATIONVALUEAMT-v_TotalAmt))OR(v_Inserted+1>=v_USELIFEMONTHS) ) THEN
                SELECT COALESCE(SUM(AMORTIZATIONAMT),0), COALESCE(SUM(AMORTIZATION_PORCENTAGE),0)
                INTO v_AMOUNT, v_PERCENTAGE
                FROM A_AMORTIZATIONLINE
                WHERE A_ASSET_ID=v_Record_ID;
                --              v_AMOUNT := v_AMORTIZATIONVALUEAMT - v_AMOUNT;
                v_PERCENTAGE:=100 - v_PERCENTAGE;
                finish:=true;
              end if;
              v_AMOUNT:=v_AMORTIZATIONVALUEAMT*v_PERCENTAGE/100;
              IF(v_AMOUNT>0) THEN
                Ad_Sequence_Next('A_Amortizationline', 1000000, v_AMORTIZATIONLINE) ;
                INSERT
                INTO A_AMORTIZATIONLINE
                  (
                    A_AMORTIZATION_ID, A_AMORTIZATIONLINE_ID, A_ASSET_ID, AD_CLIENT_ID,
                    AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
                    AMORTIZATION_PORCENTAGE, AMORTIZATIONAMT, C_CURRENCY_ID, ISACTIVE, LINE
                  )
                  VALUES
                  (v_NEW_AMORTIZATION, v_AMORTIZATIONLINE, v_Record_ID, v_AD_CLIENT_ID, v_AD_ORG_ID, now(), v_CREATEDBY, now(), v_CREATEDBY, ROUND(v_PERCENTAGE,v_Currency_Pre), ROUND(v_AMOUNT,v_Currency_Pre), v_C_CURRENCY_ID, 'Y', v_LINE) ;
                 v_Inserted := v_Inserted +1;
                v_BEGINING_DATE:=TO_DATE(ADD_MONTHS(LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY')), 1) );
                v_TotalAmt:=v_TotalAmt + v_Amount;
              END IF;
            END LOOP;
          END IF; --FINISH_PROCESS
        END IF;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      UPDATE A_ASSET SET PROCESSED='Y', PROCESSING='N'  WHERE A_ASSET_ID=v_Record_ID;
      --Updating DepreciatedPlan
      UPDATE a_asset
        SET DepreciatedPlan=
        (SELECT sum(AmortizationAmt)
        FROM A_AmortizationLine
        WHERE A_asset_ID=v_Record_ID
        )
      WHERE a_Asset_ID=v_Record_ID;
    END IF; --FINISH_PROCESS
    --<<FINISH_PROCESS>>
    v_ResultStr:='UnLockingAsset';
    UPDATE A_ASSET
      SET Processing='N', Updated=now(), UpdatedBy=v_CREATEDBY
    WHERE A_Asset_ID=v_Record_ID;
    -- Commented by cromero 19102006 IF(p_PInstance_ID IS NOT NULL) THEN
    -- Commented by cromero 19102006   COMMIT;
    -- Commented by cromero 19102006 END IF;

    IF(p_PInstance_ID IS NOT NULL) THEN
      --  Update AD_PInstance
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
      AD_UPDATE_PINSTANCE(p_PInstance_ID, v_CREATEDBY, 'N', v_Result, v_Message) ;
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<A_Asset_Post finished>> ' || v_Message) ;
    END IF;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  IF(p_PInstance_ID IS NOT NULL) THEN
    ROLLBACK;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  ELSE
    RAISE;
  END IF;
  RAISE_APPLICATION_ERROR(-20100, v_ResultStr) ;
-- Commented by cromero 19102006 RETURN;
END A_ASSET_POST
]]></body>
    </function>
  </database>