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
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION A_ASSET_POST">
adrian@94
     3
    <function name="A_ASSET_POST" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
juanpablo@1605
     7
      <parameter name="p_asset_id" type="VARCHAR" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
carlos@0
    11
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
    12
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    13
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    14
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    15
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    16
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    17
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    18
* License for the specific  language  governing  rights  and  limitations
carlos@0
    19
* under the License.
carlos@0
    20
* The Original Code is Openbravo ERP.
carlos@0
    21
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    22
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    23
* All Rights Reserved.
carlos@0
    24
* Contributor(s):  ______________________________________.
carlos@0
    25
************************************************************************/
pablo@711
    26
pablo@711
    27
--  Logistice
carlos@0
    28
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    29
  v_Message VARCHAR2(2000):='';
juanpablo@1605
    30
  v_Record_ID VARCHAR2(32);
carlos@0
    31
  v_Result NUMBER:=1; --  Success
carlos@0
    32
  --  Parameter
carlos@0
    33
  TYPE RECORD IS REF CURSOR;
carlos@0
    34
    Cur_Parameter RECORD;
carlos@0
    35
    --  Record Info
carlos@0
    36
    v_count NUMBER;
carlos@0
    37
    v_UpdatedBy A_ASSET.UpdatedBy%TYPE;
carlos@0
    38
    v_Processing A_ASSET.Processing%TYPE;
carlos@0
    39
    v_Processed A_ASSET.Processed%TYPE;
carlos@0
    40
    v_DateAcct DATE;
carlos@0
    41
    v_DocumentNo A_ASSET.DocumentNo%TYPE;
juanpablo@1605
    42
    BPartner_ID VARCHAR2(32);
carlos@0
    43
    v_ACCTVALUEAMT NUMBER;
juanpablo@1605
    44
    v_AD_CLIENT_ID VARCHAR2(32);
juanpablo@1605
    45
    v_AD_ORG_ID VARCHAR2(32);
juanpablo@1605
    46
    v_AD_USER_ID VARCHAR2(32);
carlos@0
    47
    v_AMORTIZATIONENDDATE DATE;
carlos@0
    48
    v_AMORTIZATIONSTARTDATE DATE;
carlos@0
    49
    v_AMORTIZATIONTYPE CHAR(2) ;
carlos@0
    50
    v_AMORTIZATIONVALUEAMT NUMBER;
carlos@0
    51
    v_AMORTIZATIONPORCENTAGE NUMBER;
carlos@0
    52
    v_ASSETDEPRECIATIONDATE DATE;
carlos@0
    53
    v_ASSETVALUEAMT NUMBER;
juanpablo@1605
    54
    v_C_CURRENCY_ID VARCHAR2(32);
juanpablo@1605
    55
    v_CREATEDBY varchar2(32);
carlos@0
    56
    v_ISDEPRECIATED CHAR(1) ;
carlos@0
    57
    v_RESIDUALASSETVALUEAMT NUMBER;
carlos@0
    58
    v_USELIFEMONTHS NUMBER;
carlos@0
    59
    v_USELIFEYEARS NUMBER;
carlos@0
    60
    v_ASSETSCHEDULE CHAR(2) ;
carlos@0
    61
    v_TOTAL_DAYS NUMBER;
carlos@0
    62
    v_THIS_YEAR_DAYS NUMBER;
carlos@0
    63
    v_BEGINING_DATE DATE;
carlos@0
    64
    v_ENDING_DATE DATE;
carlos@0
    65
    v_AUXAMT NUMBER;
carlos@0
    66
    v_AMORTIZATIONAMT NUMBER;
juanpablo@1605
    67
    v_NEW_AMORTIZATION VARCHAR2(32);
carlos@0
    68
    v_LINE NUMBER;
juanpablo@1605
    69
    v_AMORTIZATIONLINE varchar2(32);
carlos@0
    70
    v_FIRST_DAY_DATE DATE;
carlos@0
    71
    v_LAST_DAY_DATE DATE;
carlos@0
    72
    v_PERCENTAGE NUMBER;
carlos@0
    73
    v_AMOUNT NUMBER;
juanpablo@1605
    74
    v_CURRENCY_ID VARCHAR2(32);
carlos@0
    75
    FINISH_PROCESS BOOLEAN:=false;
antonio@735
    76
    v_DepreciatedLines NUMBER;
antonio@735
    77
    v_DepreciatedPlan NUMBER;
antonio@735
    78
    v_depreciatedValue NUMBER;
antonio@735
    79
    v_Period NUMBER;
antonio@735
    80
    v_DEPRECIATEDPREVIOUSAMT NUMBER;
carlos@0
    81
    v_AMORTIZATIONCALCTYPE varchar2(2) ;
antonio@735
    82
    v_PercentageGeneral NUMBER;
antonio@735
    83
    v_TotalAmt NUMBER;
antonio@735
    84
    v_Currency_Pre NUMBER:= 0;
antonio@735
    85
    v_Inserted NUMBER:= 0;
carlos@0
    86
    finish boolean;
carlos@0
    87
  BEGIN
carlos@0
    88
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    89
      --  Update AD_PInstance
carlos@0
    90
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    91
      v_ResultStr:='PInstanceNotFound';
carlos@0
    92
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    93
      --  Get Parameters
carlos@0
    94
      v_ResultStr:='ReadingParameters';
carlos@0
    95
      FOR Cur_Parameter IN
carlos@0
    96
        (SELECT i.Record_ID, i.AD_User_ID, p.ParameterName, p.P_String, p.P_Number, p.P_Date
carlos@0
    97
        FROM AD_PInstance i
carlos@0
    98
        LEFT JOIN AD_PInstance_Para p
carlos@0
    99
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   100
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   101
        ORDER BY p.SeqNo
carlos@0
   102
        )
carlos@0
   103
      LOOP
carlos@0
   104
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   105
      END LOOP; --  Get Parameter
carlos@0
   106
      DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
carlos@0
   107
    ELSE
gorkaion@239
   108
      DBMS_OUTPUT.PUT_LINE('--<<A_Aset_Post>>') ;
carlos@0
   109
      v_Record_ID:=p_Asset_ID;
carlos@0
   110
    END IF;
carlos@0
   111
  BEGIN --BODY
carlos@0
   112
    /**
carlos@0
   113
    *  Read Asset
carlos@0
   114
    */
carlos@0
   115
    v_ResultStr:='ReadingAsset';
carlos@0
   116
    --Updating DepreciatedPlan
carlos@0
   117
    UPDATE a_asset
carlos@0
   118
      SET DepreciatedPlan=
carlos@0
   119
      (SELECT coalesce(sum(AmortizationAmt), 0)
carlos@0
   120
      FROM A_AmortizationLine
carlos@0
   121
      WHERE A_asset_ID=v_Record_ID
carlos@0
   122
      )
carlos@0
   123
    WHERE a_Asset_ID=v_Record_ID;
carlos@674
   124
    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
carlos@0
   125
    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
carlos@0
   126
    FROM A_ASSET
carlos@0
   127
    WHERE A_ASSET_ID=v_Record_ID;
carlos@0
   128
    DBMS_OUTPUT.PUT_LINE('A_Asset_ID=' || v_Record_ID || ' - AMORTIZATIONTYPE=' || v_AMORTIZATIONTYPE) ;
carlos@0
   129
    -- Restrictions...
gorkaion@239
   130
    IF COALESCE(v_AMORTIZATIONVALUEAMT, 0)<=0 THEN
carlos@0
   131
     RAISE_APPLICATION_ERROR(-20000, '@AmountNotDefined@') ;
carlos@0
   132
    END IF;
carlos@0
   133
    IF v_AMORTIZATIONCALCTYPE='PE' AND v_AMORTIZATIONPORCENTAGE IS NULL THEN
carlos@0
   134
      RAISE_APPLICATION_ERROR(-20000, '@PercentageNotDefined@') ;
carlos@0
   135
    END IF;
carlos@0
   136
    IF v_AMORTIZATIONCALCTYPE!='PE' AND((v_ASSETSCHEDULE!='YE' AND v_USELIFEMONTHS IS NULL) OR(v_ASSETSCHEDULE='YE' AND v_USELIFEYEARS IS NULL)) THEN
carlos@0
   137
      RAISE_APPLICATION_ERROR(-20000, '@PeriodNotDefined@') ;
carlos@0
   138
    END IF;
carlos@0
   139
    IF v_AMORTIZATIONSTARTDATE IS NULL THEN
carlos@0
   140
      RAISE_APPLICATION_ERROR(-20000, '@StartDateNotDefined@') ;
carlos@0
   141
    END IF;
carlos@0
   142
    IF(v_Processing='Y') THEN
carlos@0
   143
      RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
carlos@0
   144
    END IF;
carlos@0
   145
    /**************************************************************************
carlos@0
   146
    *  Start Processing ------------------------------------------------------
carlos@0
   147
    *************************************************************************/
carlos@0
   148
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   149
      v_ResultStr:='LockingAsset';
carlos@0
   150
      UPDATE A_ASSET  SET Processing='Y'  WHERE A_ASSET_ID=v_Record_ID;
carlos@0
   151
      --we calculate the already completed number of cycles
carlos@0
   152
      SELECT count(*)
carlos@0
   153
      INTO v_DepreciatedLines
carlos@0
   154
      FROM A_AmortizationLine al, A_Amortization am
carlos@0
   155
      WHERE A_Asset_ID=V_Record_ID  AND am.A_Amortization_ID=al.A_Amortization_ID;
carlos@674
   156
      --we get the standard precision for the selected currency
antonio@737
   157
      SELECT STDPRECISION
carlos@674
   158
      INTO v_Currency_Pre
carlos@674
   159
      FROM C_CURRENCY
carlos@674
   160
      WHERE C_CURRENCY_ID = v_C_CURRENCY_ID;
carlos@0
   161
      IF(v_AMORTIZATIONTYPE='LI') THEN
carlos@674
   162
        IF(v_ASSETSCHEDULE='YE' OR v_AMORTIZATIONCALCTYPE='PE') THEN
carlos@0
   163
          if v_USELIFEYEARS=v_DepreciatedLines then
carlos@0
   164
            FINISH_PROCESS:=true;
carlos@0
   165
          end if;
carlos@0
   166
          IF(NOT FINISH_PROCESS) THEN
gorkaion@239
   167
            if v_DepreciatedLines>0 then
carlos@0
   168
              SELECT to_number(to_char(max(startdate), 'YYYY')) - to_number(to_char(min(endDate), 'YYYY'))
carlos@0
   169
              INTO v_Period
carlos@0
   170
              FROM a_amortization am, a_amortizationline al
carlos@0
   171
              WHERE al.a_amortization_id=am.a_amortization_id  AND al.a_asset_id=V_Record_ID;
gorkaion@239
   172
              if(v_DepreciatedLines<>v_Period) and(v_DepreciatedLines<>(v_Period+1)) then
carlos@0
   173
                RAISE_APPLICATION_ERROR(-20000, '@PeriodsDontMatch@') ;
carlos@0
   174
              end if;
carlos@0
   175
            end if;
carlos@0
   176
          END IF; --FINISH_PROCESS
carlos@0
   177
          IF(NOT FINISH_PROCESS) THEN
carlos@0
   178
            if v_AMORTIZATIONCALCTYPE='PE' then
carlos@0
   179
              v_PercentageGeneral:=v_AMORTIZATIONPORCENTAGE;
carlos@0
   180
              v_UseLifeYears:=trunc(100/v_PercentageGeneral) ;
carlos@0
   181
            else
carlos@0
   182
              --  v_PercentageGeneral := 100 / v_USELIFEYEARS;
carlos@0
   183
              v_PercentageGeneral:=((v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT-v_depreciatedPlan) *100/v_AMORTIZATIONVALUEAMT) /(v_USELIFEYEARS-v_DepreciatedLines) ;
carlos@668
   184
              SELECT to_number(TO_DATE(ADD_MONTHS(v_AMORTIZATIONSTARTDATE, 12*v_USELIFEYEARS)) - v_AMORTIZATIONSTARTDATE)
carlos@0
   185
              INTO v_TOTAL_DAYS
carlos@0
   186
              FROM DUAL;
carlos@0
   187
            end if;
carlos@0
   188
            v_AMORTIZATIONVALUEAMT:=v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT;
carlos@0
   189
            v_Count:=coalesce(v_depreciatedLines, 0) +1;
carlos@0
   190
            v_BEGINING_DATE:=v_AMORTIZATIONSTARTDATE;
carlos@763
   191
            v_FIRST_DAY_DATE:=TO_DATE('01-01-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
carlos@763
   192
            v_LAST_DAY_DATE:=TO_DATE('31-12-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
gorkaion@239
   193
            if v_Count>1 then
carlos@763
   194
              v_BEGINING_DATE:=TO_DATE('31-12-' || to_char(TO_number(to_char(v_BEGINING_DATE, 'yyyy')) +v_Count-1), 'DD-MM-YYYY') ;
carlos@0
   195
              v_USELIFEYEARS:=v_USELIFEYEARS+1;
carlos@0
   196
            end if;
carlos@0
   197
            v_AUXAMT:=0;
carlos@0
   198
            v_PERCENTAGE:=0;
carlos@0
   199
            v_TotalAmt:=coalesce(v_DepreciatedPlan, 0) ;
carlos@0
   200
            finish:=false;
carlos@0
   201
            WHILE not finish
carlos@0
   202
            LOOP
carlos@550
   203
              IF(v_COUNT=1 AND to_number(v_BEGINING_DATE-v_FIRST_DAY_DATE)<>0) THEN
carlos@674
   204
                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;
carlos@0
   205
                v_USELIFEYEARS:=v_USELIFEYEARS+1;
carlos@0
   206
              ELSE
carlos@0
   207
                v_Percentage:=v_PercentageGeneral;
carlos@0
   208
              END IF;
carlos@0
   209
              v_COUNT:=v_COUNT + 1;
carlos@763
   210
              v_ENDING_DATE:=TO_DATE('31-12-' ||TO_CHAR(v_BEGINING_DATE, 'YYYY'), 'DD-MM-YYYY') ;
juanpablo@1605
   211
              SELECT COALESCE(MAX(A_AMORTIZATION_ID), '-1')
carlos@0
   212
              INTO v_NEW_AMORTIZATION
carlos@0
   213
              FROM A_AMORTIZATION
gorkaion@239
   214
              WHERE STARTDATE<=v_ENDING_DATE  AND ENDDATE>=v_ENDING_DATE  AND AD_CLIENT_ID=v_AD_CLIENT_ID  AND PROCESSED='N';
juanpablo@1605
   215
              IF(v_NEW_AMORTIZATION='-1') THEN
carlos@0
   216
                Ad_Sequence_Next('A_Amortization', 1000000, v_NEW_AMORTIZATION) ;
carlos@0
   217
                INSERT
carlos@0
   218
                INTO A_AMORTIZATION
carlos@0
   219
                  (
carlos@0
   220
                    A_AMORTIZATION_ID, DATEACCT, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   221
                  CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   222
                  ENDDATE,
carlos@0
   223
                  ISACTIVE, NAME, POSTED,
carlos@0
   224
                  PROCESSED, PROCESSING, STARTDATE, C_CURRENCY_ID
carlos@0
   225
                  )
carlos@0
   226
                  VALUES
carlos@0
   227
                  (v_NEW_AMORTIZATION, v_ENDING_DATE, v_AD_CLIENT_ID, v_AD_ORG_ID,
carlos@0
   228
                  now(), v_CREATEDBY, now(), v_CREATEDBY,
carlos@763
   229
                  LAST_DAY(TO_DATE('01-' || TO_CHAR(v_ENDING_DATE, 'MM') || '-'|| TO_CHAR(v_ENDING_DATE, 'YYYY'),'DD-MM-YYYY')),
carlos@763
   230
                  'Y', TO_CHAR(TO_DATE(LAST_DAY(TO_DATE('01-'|| TO_CHAR(v_ENDING_DATE, 'MM') || '-' || TO_CHAR(v_ENDING_DATE, 'YYYY'))))), 'N',
carlos@0
   231
                  'N', 'N',
carlos@763
   232
                  TO_DATE('01-' || TO_CHAR(v_ENDING_DATE, 'MM') || '-' || TO_CHAR(v_ENDING_DATE, 'YYYY'),'DD-MM-YYYY'), v_CURRENCY_ID);
carlos@0
   233
              END IF;
carlos@0
   234
              SELECT COALESCE(MAX(LINE), 0) +10
carlos@0
   235
              INTO v_LINE
carlos@0
   236
              FROM A_AMORTIZATIONLINE
carlos@0
   237
              WHERE A_AMORTIZATION_ID=v_NEW_AMORTIZATION;
carlos@668
   238
              IF((((v_AMORTIZATIONVALUEAMT+v_DEPRECIATEDPREVIOUSAMT) *v_PERCENTAGE/100)>(v_AMORTIZATIONVALUEAMT -v_TotalAmt))OR(v_Inserted+1>=v_USELIFEYEARS)) THEN
carlos@674
   239
                SELECT COALESCE(SUM(AMORTIZATIONAMT),0), COALESCE(SUM(AMORTIZATION_PORCENTAGE),0)
carlos@0
   240
                INTO v_AMOUNT, v_PERCENTAGE
carlos@0
   241
                FROM A_AMORTIZATIONLINE
carlos@0
   242
                WHERE A_ASSET_ID=v_Record_ID;
carlos@0
   243
                v_AMOUNT:=v_AMORTIZATIONVALUEAMT - v_AMOUNT;
carlos@0
   244
                v_PERCENTAGE:=v_AMOUNT*100/(v_AMORTIZATIONVALUEAMT+v_DEPRECIATEDPREVIOUSAMT) ;
carlos@0
   245
                finish:=true;
carlos@0
   246
              ELSE
carlos@0
   247
                v_AMOUNT:=(v_AMORTIZATIONVALUEAMT+ v_DEPRECIATEDPREVIOUSAMT) *v_PERCENTAGE/100;
carlos@0
   248
              END IF;
gorkaion@239
   249
              if v_percentage>0 then
carlos@0
   250
                Ad_Sequence_Next('A_Amortizationline', 1000000, v_AMORTIZATIONLINE) ;
carlos@0
   251
                INSERT
carlos@0
   252
                INTO A_AMORTIZATIONLINE
carlos@0
   253
                  (
carlos@0
   254
                    A_AMORTIZATION_ID, A_AMORTIZATIONLINE_ID, A_ASSET_ID, AD_CLIENT_ID,
carlos@0
   255
                    AD_ORG_ID, CREATED, CREATEDBY, UPDATED,
carlos@0
   256
                    UPDATEDBY, AMORTIZATION_PORCENTAGE, AMORTIZATIONAMT, C_CURRENCY_ID,
carlos@0
   257
                    ISACTIVE, LINE
carlos@0
   258
                  )
carlos@0
   259
                  VALUES
carlos@674
   260
                  (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) ;
carlos@668
   261
                 v_Inserted := v_Inserted +1;
carlos@0
   262
              end if;
carlos@763
   263
              v_BEGINING_DATE:=TO_DATE('31-12-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY') + 1;
carlos@0
   264
              v_TotalAmt:=v_TotalAmt + v_Amount;
carlos@0
   265
            END LOOP;
carlos@0
   266
            FINISH_process:=TRUE;
carlos@0
   267
          END IF; --FINISH_PROCESS
carlos@0
   268
        END IF;
carlos@0
   269
        IF(NOT FINISH_PROCESS) THEN
carlos@0
   270
          IF(v_ASSETSCHEDULE='MO') THEN
carlos@0
   271
            if v_USELIFEMonths=v_DepreciatedLines then
carlos@0
   272
              FINISH_PROCESS:=true;
carlos@0
   273
            end if;
carlos@0
   274
          END IF; --FINISH_PROCESS
carlos@0
   275
          IF(NOT FINISH_PROCESS) THEN
gorkaion@239
   276
            if v_DepreciatedLines>0 then
carlos@0
   277
              SELECT trunc(months_Between(max(startdate), min(endDate))) +1
carlos@0
   278
              INTO v_Period
carlos@0
   279
              FROM a_amortization am, a_amortizationline al
carlos@0
   280
              WHERE al.a_amortization_id=am.a_amortization_id  AND al.a_asset_id=V_Record_ID;
gorkaion@239
   281
              if(v_DepreciatedLines<>v_Period) and(v_DepreciatedLines<>(v_Period+1)) then
carlos@0
   282
                RAISE_APPLICATION_ERROR(-20000, '@PeriodsDontMatch@') ;
carlos@0
   283
              end if;
carlos@0
   284
            end if;
carlos@0
   285
          END IF; --FINISH_PROCESS
carlos@0
   286
          IF(NOT FINISH_PROCESS) THEN
carlos@0
   287
            v_AMORTIZATIONVALUEAMT:=v_AMORTIZATIONVALUEAMT-v_DEPRECIATEDPREVIOUSAMT;
carlos@0
   288
            if v_AMORTIZATIONCALCTYPE='PE' then
carlos@0
   289
              v_PercentageGeneral:=v_AMORTIZATIONPORCENTAGE/12;
carlos@668
   290
              v_UseLifeMonths:=trunc(100/v_AMORTIZATIONPORCENTAGE*12) ;
carlos@0
   291
            else
carlos@0
   292
              --  v_PercentageGeneral := 100 / v_USELIFEYEARS;
carlos@0
   293
              v_UseLifeYears:=v_UseLifeMonths/12;
carlos@0
   294
              v_PercentageGeneral:=(((v_AMORTIZATIONVALUEAMT-v_depreciatedPlan) *100/v_AMORTIZATIONVALUEAMT) /(v_USELIFEYEARS-v_DepreciatedLines)) /12;
carlos@668
   295
              SELECT to_number(TO_DATE(ADD_MONTHS(v_AMORTIZATIONSTARTDATE, 12*v_USELIFEYEARS)) - TO_DATE(v_AMORTIZATIONSTARTDATE))
carlos@0
   296
              INTO v_TOTAL_DAYS
carlos@0
   297
              FROM DUAL;
carlos@0
   298
            end if;
carlos@0
   299
            v_Count:=coalesce(v_depreciatedLines, 0) +1;
carlos@0
   300
            v_BEGINING_DATE:=v_AMORTIZATIONSTARTDATE;
carlos@763
   301
            v_FIRST_DAY_DATE:=TO_DATE('01-'|| TO_CHAR(v_AMORTIZATIONSTARTDATE, 'MM') || '-' || TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
carlos@763
   302
            v_LAST_DAY_DATE:=TO_DATE('31-12-'|| TO_CHAR(v_AMORTIZATIONSTARTDATE, 'YYYY'), 'DD-MM-YYYY') ;
gorkaion@239
   303
            if v_Count>1 then
carlos@668
   304
              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)) ;
carlos@0
   305
              v_USELIFEMonths:=v_USELIFEMonths+1;
carlos@0
   306
            end if;
carlos@0
   307
            v_AUXAMT:=0;
carlos@0
   308
            v_PERCENTAGE:=0;
carlos@0
   309
            v_TotalAmt:=coalesce(v_DepreciatedPlan, 0) ;
carlos@0
   310
            finish:=false;
carlos@0
   311
            while not finish
carlos@0
   312
            loop
carlos@550
   313
              IF(v_COUNT=1 AND to_number(v_BEGINING_DATE-v_FIRST_DAY_DATE)<>0) THEN
carlos@674
   314
                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;
carlos@0
   315
                v_USELIFEMONTHS:=v_USELIFEMONTHS+1;
carlos@0
   316
              ELSE
carlos@0
   317
                v_Percentage:=v_PercentageGeneral;
carlos@0
   318
              END IF;
carlos@0
   319
              v_COUNT:=v_COUNT + 1;
juanpablo@1605
   320
              SELECT COALESCE(MAX(A_AMORTIZATION_ID), '-1')
carlos@0
   321
              INTO v_NEW_AMORTIZATION
carlos@0
   322
              FROM A_AMORTIZATION
gorkaion@239
   323
              WHERE STARTDATE<=v_BEGINING_DATE  AND ENDDATE>=v_BEGINING_DATE  AND AD_CLIENT_ID=v_AD_CLIENT_ID  AND PROCESSED='N';
carlos@668
   324
              v_ENDING_DATE:= LAST_DAY(TO_DATE('01-' || TO_CHAR(v_BEGINING_DATE, 'MM') || '-' || TO_CHAR(v_BEGINING_DATE, 'yyyy'), 'DD-MM-YYYY')) ;
juanpablo@1605
   325
              IF(v_NEW_AMORTIZATION='-1') THEN
carlos@0
   326
                Ad_Sequence_Next('A_Amortization', 1000000, v_NEW_AMORTIZATION) ;
carlos@0
   327
                INSERT
carlos@0
   328
                INTO A_AMORTIZATION
carlos@0
   329
                  (
carlos@0
   330
                    A_AMORTIZATION_ID, DATEACCT, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   331
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   332
                    ENDDATE, ISACTIVE, NAME, POSTED,
carlos@0
   333
                    PROCESSED, PROCESSING, STARTDATE, C_Currency_ID
carlos@0
   334
                  )
carlos@0
   335
                  VALUES
carlos@763
   336
                  (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) ;
carlos@0
   337
              END IF;
carlos@0
   338
              SELECT COALESCE(MAX(LINE), 0) +10
carlos@0
   339
              INTO v_LINE
carlos@0
   340
              FROM A_AMORTIZATIONLINE
carlos@0
   341
              WHERE A_AMORTIZATION_ID=v_NEW_AMORTIZATION;
carlos@668
   342
              IF(((v_AMORTIZATIONVALUEAMT*v_PERCENTAGE/100)>(v_AMORTIZATIONVALUEAMT-v_TotalAmt))OR(v_Inserted+1>=v_USELIFEMONTHS) ) THEN
carlos@674
   343
                SELECT COALESCE(SUM(AMORTIZATIONAMT),0), COALESCE(SUM(AMORTIZATION_PORCENTAGE),0)
carlos@0
   344
                INTO v_AMOUNT, v_PERCENTAGE
carlos@0
   345
                FROM A_AMORTIZATIONLINE
carlos@0
   346
                WHERE A_ASSET_ID=v_Record_ID;
carlos@0
   347
                --              v_AMOUNT := v_AMORTIZATIONVALUEAMT - v_AMOUNT;
carlos@0
   348
                v_PERCENTAGE:=100 - v_PERCENTAGE;
carlos@0
   349
                finish:=true;
carlos@0
   350
              end if;
carlos@0
   351
              v_AMOUNT:=v_AMORTIZATIONVALUEAMT*v_PERCENTAGE/100;
gorkaion@239
   352
              IF(v_AMOUNT>0) THEN
carlos@0
   353
                Ad_Sequence_Next('A_Amortizationline', 1000000, v_AMORTIZATIONLINE) ;
carlos@0
   354
                INSERT
carlos@0
   355
                INTO A_AMORTIZATIONLINE
carlos@0
   356
                  (
carlos@0
   357
                    A_AMORTIZATION_ID, A_AMORTIZATIONLINE_ID, A_ASSET_ID, AD_CLIENT_ID,
carlos@0
   358
                    AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   359
                    AMORTIZATION_PORCENTAGE, AMORTIZATIONAMT, C_CURRENCY_ID, ISACTIVE, LINE
carlos@0
   360
                  )
carlos@0
   361
                  VALUES
carlos@674
   362
                  (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) ;
carlos@668
   363
                 v_Inserted := v_Inserted +1;
carlos@668
   364
                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) );
carlos@0
   365
                v_TotalAmt:=v_TotalAmt + v_Amount;
carlos@0
   366
              END IF;
carlos@0
   367
            END LOOP;
carlos@0
   368
          END IF; --FINISH_PROCESS
carlos@0
   369
        END IF;
carlos@0
   370
      END IF;
carlos@0
   371
    END IF; --FINISH_PROCESS
carlos@0
   372
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   373
      UPDATE A_ASSET SET PROCESSED='Y', PROCESSING='N'  WHERE A_ASSET_ID=v_Record_ID;
carlos@0
   374
      --Updating DepreciatedPlan
carlos@0
   375
      UPDATE a_asset
carlos@0
   376
        SET DepreciatedPlan=
carlos@0
   377
        (SELECT sum(AmortizationAmt)
carlos@0
   378
        FROM A_AmortizationLine
carlos@0
   379
        WHERE A_asset_ID=v_Record_ID
carlos@0
   380
        )
carlos@0
   381
      WHERE a_Asset_ID=v_Record_ID;
carlos@0
   382
    END IF; --FINISH_PROCESS
gorkaion@239
   383
    --<<FINISH_PROCESS>>
carlos@0
   384
    v_ResultStr:='UnLockingAsset';
carlos@0
   385
    UPDATE A_ASSET
carlos@0
   386
      SET Processing='N', Updated=now(), UpdatedBy=v_CREATEDBY
carlos@0
   387
    WHERE A_Asset_ID=v_Record_ID;
carlos@0
   388
    -- Commented by cromero 19102006 IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   389
    -- Commented by cromero 19102006   COMMIT;
carlos@0
   390
    -- Commented by cromero 19102006 END IF;
carlos@0
   391
carlos@0
   392
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   393
      --  Update AD_PInstance
carlos@0
   394
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
carlos@0
   395
      AD_UPDATE_PINSTANCE(p_PInstance_ID, v_CREATEDBY, 'N', v_Result, v_Message) ;
carlos@0
   396
    ELSE
gorkaion@239
   397
      DBMS_OUTPUT.PUT_LINE('--<<A_Asset_Post finished>> ' || v_Message) ;
carlos@0
   398
    END IF;
carlos@0
   399
    RETURN;
carlos@0
   400
  END; --BODY
carlos@0
   401
EXCEPTION
carlos@0
   402
WHEN OTHERS THEN
carlos@0
   403
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   404
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   405
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   406
    ROLLBACK;
carlos@0
   407
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   408
  ELSE
carlos@0
   409
    RAISE;
carlos@0
   410
  END IF;
carlos@0
   411
  RAISE_APPLICATION_ERROR(-20100, v_ResultStr) ;
carlos@0
   412
-- Commented by cromero 19102006 RETURN;
antonio@735
   413
END A_ASSET_POST
antonio@735
   414
]]></body>
adrian@94
   415
    </function>
adrian@94
   416
  </database>