src-db/database/model/functions/M_RAPPEL_INVOICE_CREATE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 1201 00f8c2619ea5
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION M_RAPPEL_INVOICE_CREATE">
adrian@94
     3
    <function name="M_RAPPEL_INVOICE_CREATE" type="NULL">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
carlos@0
     8
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     9
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    11
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    12
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    15
* License for the specific  language  governing  rights  and  limitations
carlos@0
    16
* under the License.
carlos@0
    17
* The Original Code is Openbravo ERP.
carlos@0
    18
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    19
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    20
* All Rights Reserved.
carlos@0
    21
* Contributor(s):  ______________________________________.
carlos@0
    22
************************************************************************/
carlos@0
    23
  -- Logistice
carlos@0
    24
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    25
  v_Message VARCHAR2(2000):='';
carlos@0
    26
  -- Parameter
carlos@0
    27
  TYPE RECORD IS REF CURSOR;
carlos@0
    28
    Cur_Parameter RECORD;
carlos@0
    29
    v_Record_ID NUMBER:=NULL;
carlos@0
    30
    v_AD_User_ID NUMBER:=NULL;
antonio@735
    31
    v_LimitDate DATE;
carlos@0
    32
    --
carlos@0
    33
    v_invoiceId NUMBER:=NULL;
carlos@0
    34
    v_rappelInvoiceId NUMBER:=NULL;
carlos@0
    35
    v_invoicelineId NUMBER:=NULL;
carlos@0
    36
    v_NoRecords NUMBER:=0;
carlos@0
    37
    v_DocType_ID NUMBER;
carlos@0
    38
    v_DocumentNo VARCHAR2(40) ;
carlos@0
    39
    v_seqno NUMBER(10) ;
carlos@0
    40
    v_line NUMBER(10):=0;
carlos@0
    41
    v_paymentRule CHAR(1) ;
carlos@0
    42
    v_paymenttermId NUMBER(10) ;
carlos@0
    43
    v_pricelistId NUMBER(10) ;
carlos@0
    44
    v_description VARCHAR2(100) ;
antonio@735
    45
    v_validTo DATE;
antonio@735
    46
    v_TotalLines NUMBER;
antonio@735
    47
    v_RappelID NUMBER;
carlos@0
    48
    v_Scaled char(1) ;
antonio@735
    49
    v_Discount NUMBER;
antonio@735
    50
    v_Discount1 NUMBER;
antonio@735
    51
    v_From NUMBER;
antonio@735
    52
    v_AppliedDiscount NUMBER;
carlos@0
    53
    CUR_SCALES RECORD;
carlos@0
    54
    CUR_INVOICE RECORD;
carlos@0
    55
    FINISH_PROCESS BOOLEAN:=FALSE;
carlos@0
    56
  BEGIN
carlos@0
    57
    -- PROCESS PARAMETERS
carlos@0
    58
    --  UPDATE AD_PINSTANCE
carlos@0
    59
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || P_PINSTANCE_ID) ;
carlos@0
    60
    v_ResultStr:='PInstanceNotFound';
carlos@0
    61
    AD_UPDATE_PINSTANCE(P_PINSTANCE_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    62
  BEGIN --BODY
carlos@0
    63
    -- GET PARAMETERS
carlos@0
    64
    v_ResultStr:='ReadingParameters';
carlos@0
    65
    FOR CUR_PARAMETER IN
carlos@0
    66
      (SELECT I.RECORD_ID,
carlos@0
    67
        I.AD_USER_ID,
carlos@0
    68
        P.PARAMETERNAME,
carlos@0
    69
        P.P_STRING,
carlos@0
    70
        P.P_NUMBER,
carlos@0
    71
        P.P_DATE
carlos@0
    72
      FROM AD_PINSTANCE I
carlos@0
    73
      LEFT JOIN AD_PINSTANCE_PARA P
carlos@0
    74
        ON I.AD_PINSTANCE_ID=P.AD_PINSTANCE_ID
carlos@0
    75
      WHERE I.AD_PINSTANCE_ID=p_PInstance_ID
carlos@0
    76
      ORDER BY P.SEQNO
carlos@0
    77
      )
carlos@0
    78
    LOOP
carlos@0
    79
      v_Record_ID:=CUR_PARAMETER.RECORD_ID;
carlos@0
    80
      v_AD_User_ID:=CUR_PARAMETER.AD_USER_ID;
carlos@0
    81
      IF(CUR_PARAMETER.PARAMETERNAME='LimitDate') THEN
carlos@0
    82
        v_LimitDate:=CUR_PARAMETER.P_DATE;
carlos@0
    83
        DBMS_OUTPUT.PUT_LINE(' LimitDate=' || v_LimitDate) ;
carlos@0
    84
      END IF;
carlos@0
    85
    END LOOP; -- GET PARAMETER
carlos@0
    86
    /**
carlos@0
    87
    *
carlos@0
    88
    */
carlos@0
    89
    SELECT VALIDTO
carlos@0
    90
    INTO v_validTo
carlos@0
    91
    FROM M_RAPPEL_BPARTNER
carlos@0
    92
    WHERE M_RAPPEL_BPARTNER_ID=v_Record_ID;
carlos@0
    93
    IF v_LimitDate IS NOT NULL AND v_validTo IS NOT NULL THEN
gorkaion@239
    94
      IF v_validTo>v_LimitDate THEN
carlos@0
    95
        v_LimitDate:=v_validTo;
carlos@0
    96
      END IF;
carlos@0
    97
    ELSIF v_validTo IS NOT NULL AND v_LimitDate IS NULL THEN
carlos@0
    98
      v_LimitDate:=v_validTo;
carlos@0
    99
    ELSE
gorkaion@280
   100
      v_LimitDate:=COALESCE(v_LimitDate, TO_DATE('31-12-9999','DD-MM-YYYY')) ;
carlos@0
   101
    END IF;
carlos@0
   102
    -- CALCULATE DISCOUNT
carlos@0
   103
  BEGIN
carlos@0
   104
    SELECT COALESCE(SUM(IL.LINENETAMT), 0)
carlos@0
   105
    INTO v_TotalLines
carlos@0
   106
    FROM C_INVOICE I,
carlos@0
   107
      C_INVOICELINE IL,
carlos@0
   108
      M_RAPPEL_BPARTNER RP,
carlos@0
   109
      C_BPARTNER_LOCATION PL,
carlos@0
   110
      M_RAPPEL R
carlos@0
   111
    WHERE I.C_INVOICE_ID=IL.C_INVOICE_ID
carlos@0
   112
      AND R.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   113
      AND R.C_CURRENCY_ID=I.C_CURRENCY_ID
carlos@0
   114
      AND I.PROCESSED='Y'
gorkaion@239
   115
      AND I.DATEINVOICED>=RP.VALIDFROM
gorkaion@239
   116
      AND I.DATEINVOICED<v_LimitDate+1
carlos@0
   117
      AND I.C_BPARTNER_ID=RP.C_BPARTNER_ID
carlos@0
   118
      AND I.C_BPARTNER_ID = PL.C_BPARTNER_ID
carlos@0
   119
      /*Commented by Carlos ROmero
carlos@0
   120
      AND IL.M_PRODUCT_ID IN (SELECT P.M_PRODUCT_ID
carlos@0
   121
      FROM M_RAPPEL         R,
carlos@0
   122
      M_PRODUCT        P
carlos@0
   123
      WHERE R.M_RAPPEL_ID = RP.M_RAPPEL_ID AND
carlos@0
   124
      ((COALESCE(R.INCLUDE_PRODUCT_CATEGORY,'Y') = 'N'
carlos@0
   125
      AND P.M_PRODUCT_CATEGORY_ID IN (SELECT M_PRODUCT_CATEGORY_ID
carlos@0
   126
      FROM M_RAPPEL_PRODUCTCATEGORY RC
carlos@0
   127
      WHERE RC.M_RAPPEL_ID = R.M_RAPPEL_ID))
carlos@0
   128
      OR (COALESCE(R.INCLUDE_PRODUCT_CATEGORY,'Y') = 'Y'
carlos@0
   129
      AND P.M_PRODUCT_CATEGORY_ID NOT IN (SELECT M_PRODUCT_CATEGORY_ID
carlos@0
   130
      FROM M_RAPPEL_PRODUCTCATEGORY RC
carlos@0
   131
      WHERE RC.M_RAPPEL_ID = R.M_RAPPEL_ID)))
carlos@0
   132
      UNION
carlos@0
   133
      SELECT M_PRODUCT_ID
carlos@0
   134
      FROM M_RAPPEL_PRODUCT RP,
carlos@0
   135
      M_RAPPEL R1
carlos@0
   136
      WHERE R1.M_RAPPEL_ID = RP.M_RAPPEL_ID AND
carlos@0
   137
      R.M_RAPPEL_ID = RP.M_RAPPEL_ID
carlos@0
   138
      AND COALESCE(R1.INCLUDE_PRODUCT,'Y') = 'N'
carlos@0
   139
      MINUS
carlos@0
   140
      SELECT M_PRODUCT_ID
carlos@0
   141
      FROM M_RAPPEL_PRODUCT RP,
carlos@0
   142
      M_RAPPEL R1
carlos@0
   143
      WHERE R1.M_RAPPEL_ID = RP.M_RAPPEL_ID AND
carlos@0
   144
      R.M_RAPPEL_ID = RP.M_RAPPEL_ID
carlos@0
   145
      AND COALESCE(R1.INCLUDE_PRODUCT,'Y') = 'Y' )
carlos@0
   146
      AND I.C_BPARTNER_ID = PL.C_BPARTNER_ID
carlos@0
   147
      AND NOT EXISTS (SELECT 1
carlos@0
   148
      FROM M_RAPPEL_INVOICE RI
carlos@0
   149
      WHERE RI.M_RAPPEL_BPARTNER_ID = RP.M_RAPPEL_BPARTNER_ID
carlos@0
   150
      AND RI.C_INVOICE_ID = I.C_INVOICE_ID)*/
carlos@0
   151
      AND((COALESCE(R.INCLUDE_PRODUCT_CATEGORY, 'Y')='N' -- Just selected
carlos@0
   152
      AND EXISTS
carlos@0
   153
      (SELECT 1
carlos@0
   154
      FROM M_RAPPEL_PRODUCTCATEGORY RC,
carlos@0
   155
        M_PRODUCT PROD
carlos@0
   156
      WHERE PROD.M_PRODUCT_ID=IL.M_PRODUCT_ID
carlos@0
   157
        AND RC.M_PRODUCT_CATEGORY_ID=PROD.M_PRODUCT_CATEGORY_ID
carlos@0
   158
      ))
carlos@0
   159
      OR (COALESCE(R.INCLUDE_PRODUCT_CATEGORY, 'Y')='Y' -- Everyone except selected
carlos@0
   160
      AND NOT EXISTS
carlos@0
   161
      (SELECT 1
carlos@0
   162
      FROM M_RAPPEL_PRODUCTCATEGORY RC,
carlos@0
   163
        M_PRODUCT PROD
carlos@0
   164
      WHERE PROD.M_PRODUCT_ID=IL.M_PRODUCT_ID
carlos@0
   165
        AND RC.M_PRODUCT_CATEGORY_ID=PROD.M_PRODUCT_CATEGORY_ID
carlos@0
   166
      )))
carlos@0
   167
      AND((COALESCE(R.INCLUDE_PRODUCT, 'Y')='N' -- Just selected
carlos@0
   168
      AND EXISTS
carlos@0
   169
      (SELECT 1 FROM M_RAPPEL_PRODUCT RP WHERE RP.M_PRODUCT_ID=IL.M_PRODUCT_ID))
carlos@0
   170
      OR (COALESCE(R.INCLUDE_PRODUCT, 'Y')='Y' -- Everyone except selected
carlos@0
   171
      AND NOT EXISTS
carlos@0
   172
      (SELECT 1 FROM M_RAPPEL_PRODUCT RP  WHERE RP.M_PRODUCT_ID=IL.M_PRODUCT_ID)))
carlos@0
   173
      AND RP.ISSOTRX=I.ISSOTRX
carlos@0
   174
      AND RP.M_RAPPEL_BPARTNER_ID=v_Record_ID;
carlos@0
   175
  EXCEPTION
carlos@0
   176
  WHEN NO_DATA_FOUND THEN
carlos@0
   177
    v_TotalLines:=0;
carlos@0
   178
  END;
carlos@0
   179
  IF v_TotalLines=0 THEN
carlos@0
   180
    V_MESSAGE:='@NoInvoicesForRappel@';
carlos@0
   181
    FINISH_PROCESS:=TRUE;
carlos@0
   182
  END IF;
carlos@0
   183
  IF(NOT FINISH_PROCESS) THEN
carlos@0
   184
    SELECT COALESCE(SCALED, 'N'),
carlos@0
   185
      R.M_RAPPEL_ID
carlos@0
   186
    INTO v_Scaled,
carlos@0
   187
      v_RappelID
carlos@0
   188
    FROM M_RAPPEL R,
carlos@0
   189
      M_RAPPEL_BPARTNER RP
carlos@0
   190
    WHERE RP.M_RAPPEL_BPARTNER_ID=v_Record_ID
carlos@0
   191
      AND RP.M_RAPPEL_ID=R.M_RAPPEL_ID;
carlos@0
   192
    IF v_Scaled='N' THEN
carlos@0
   193
      SELECT MAX(DISCOUNT)
carlos@0
   194
      INTO v_Discount
carlos@0
   195
      FROM M_RAPPEL_SCALE
carlos@0
   196
      WHERE M_RAPPEL_ID=v_RappelID
carlos@0
   197
        AND v_TotalLines BETWEEN AMNT_FROM AND COALESCE(AMNT_TO, 9999999999) ;
carlos@0
   198
    ELSE
carlos@0
   199
      v_Discount1:=0;
carlos@0
   200
      FOR CUR_SCALES IN
carlos@0
   201
        (SELECT DISCOUNT,
carlos@0
   202
          AMNT_FROM,
carlos@0
   203
          AMNT_TO
carlos@0
   204
        FROM M_RAPPEL_SCALE
gorkaion@239
   205
        WHERE AMNT_TO<v_TotalLines
carlos@0
   206
          AND M_RAPPEL_ID=v_RappelID
carlos@0
   207
        )
carlos@0
   208
      LOOP
carlos@0
   209
        v_Discount1:=v_Discount1 +(CUR_SCALES.DISCOUNT *(CUR_SCALES.AMNT_TO-CUR_SCALES.AMNT_FROM)) ;
carlos@0
   210
      END LOOP;
carlos@0
   211
    BEGIN
carlos@0
   212
      SELECT MIN(DISCOUNT),
carlos@0
   213
        MIN(AMNT_FROM)
carlos@0
   214
      INTO v_Discount,
carlos@0
   215
        v_From
carlos@0
   216
      FROM M_RAPPEL_SCALE
carlos@0
   217
      WHERE M_RAPPEL_ID=v_RappelID
carlos@0
   218
        AND v_TotalLines BETWEEN AMNT_FROM AND COALESCE(AMNT_TO, 9999999999) ;
carlos@0
   219
    EXCEPTION
carlos@0
   220
    WHEN NO_DATA_FOUND THEN
carlos@0
   221
      v_Discount:=0;
carlos@0
   222
    END;
carlos@0
   223
    v_Discount1:=v_Discount1 +(v_Discount *(v_TotalLines-v_From)) ;
carlos@0
   224
    v_Discount:=v_Discount1/v_TotalLines; --TOTAL DISCOUNT
carlos@0
   225
  END IF;
carlos@0
   226
  SELECT COALESCE(SUM(LINENETAMT), 0)
carlos@0
   227
  INTO v_AppliedDiscount
carlos@0
   228
  FROM M_RAPPEL_INVOICE RI,
carlos@0
   229
    C_INVOICELINE IL
carlos@0
   230
  WHERE RI.M_RAPPEL_BPARTNER_ID=v_Record_ID
carlos@0
   231
    AND RI.C_INVOICE_ID=IL.C_INVOICE_ID;
carlos@0
   232
  v_AppliedDiscount:=(-100) *v_AppliedDiscount/v_TotalLines;
carlos@0
   233
  v_Discount:=v_Discount - v_AppliedDiscount;
carlos@0
   234
  ---------------------
carlos@0
   235
  --V_MESSAGE := v_RappelID||'#'||v_Record_ID||'#'||v_TotalLines||'#'||v_Discount||'#'||v_Discount1;
carlos@0
   236
  IF v_Discount!=0 THEN
carlos@0
   237
    FOR CUR_INVOICE IN
carlos@0
   238
      (SELECT I.AD_CLIENT_ID,
carlos@0
   239
        I.AD_ORG_ID,
carlos@0
   240
        IL.M_PRODUCT_ID,
carlos@0
   241
        RP.ISSOTRX,
carlos@0
   242
        RP.C_BPARTNER_ID,
carlos@0
   243
        MAX(PL.C_BPARTNER_LOCATION_ID) AS C_BPARTNER_LOCATION_ID,
carlos@0
   244
        MAX(I.C_CURRENCY_ID) AS C_CURRENCY_ID,
carlos@0
   245
        MIN(I.C_DOCTYPETARGET_ID) AS C_DOCTYPETARGET_ID,
carlos@0
   246
        MAX(I.C_DOCTYPE_ID) AS C_DOCTYPE_ID,
carlos@0
   247
        SUM(IL.QTYINVOICED) AS QTYINVOICED,
carlos@0
   248
        SUM(IL.LINENETAMT) AS TOTALLINES,
carlos@0
   249
        MAX(IL.C_UOM_ID) AS C_UOM_ID,
carlos@0
   250
        IL.C_TAX_ID,
carlos@0
   251
        NULL AS RAPPELINVOICED,
carlos@0
   252
        (-v_Discount) *SUM(IL.LINENETAMT) /100 AS LINENET --    SUM(IL.LINENETAMT)
carlos@0
   253
      FROM C_INVOICE I,
carlos@0
   254
        C_INVOICELINE IL,
carlos@0
   255
        M_RAPPEL_BPARTNER RP,
carlos@0
   256
        C_BPARTNER_LOCATION PL,
carlos@0
   257
        M_RAPPEL R
carlos@0
   258
      WHERE I.C_INVOICE_ID=IL.C_INVOICE_ID
carlos@0
   259
        AND R.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   260
        AND R.C_CURRENCY_ID=I.C_CURRENCY_ID
carlos@0
   261
        AND I.PROCESSED='Y'
gorkaion@239
   262
        AND I.DATEINVOICED>=RP.VALIDFROM
gorkaion@239
   263
        AND I.DATEINVOICED<v_LimitDate+1
carlos@0
   264
        AND I.C_BPARTNER_ID=RP.C_BPARTNER_ID
carlos@0
   265
        AND IL.M_PRODUCT_ID IN
carlos@0
   266
        (SELECT P.M_PRODUCT_ID
carlos@0
   267
        FROM M_RAPPEL R,
carlos@0
   268
          M_PRODUCT P
carlos@0
   269
        WHERE R.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   270
          AND ((COALESCE(R.INCLUDE_PRODUCT_CATEGORY, 'Y')='N'
carlos@0
   271
          AND P.M_PRODUCT_CATEGORY_ID IN
carlos@0
   272
          (SELECT M_PRODUCT_CATEGORY_ID
carlos@0
   273
          FROM M_RAPPEL_PRODUCTCATEGORY RC
carlos@0
   274
          WHERE RC.M_RAPPEL_ID=R.M_RAPPEL_ID
carlos@0
   275
          ))
carlos@0
   276
          OR(COALESCE(R.INCLUDE_PRODUCT_CATEGORY, 'Y')='Y'
carlos@0
   277
          AND P.M_PRODUCT_CATEGORY_ID NOT IN
carlos@0
   278
          (SELECT M_PRODUCT_CATEGORY_ID
carlos@0
   279
          FROM M_RAPPEL_PRODUCTCATEGORY RC
carlos@0
   280
          WHERE RC.M_RAPPEL_ID=R.M_RAPPEL_ID
carlos@0
   281
          )))
carlos@0
   282
        UNION
carlos@0
   283
        SELECT M_PRODUCT_ID
carlos@0
   284
        FROM M_RAPPEL_PRODUCT RP,
carlos@0
   285
          M_RAPPEL R1
carlos@0
   286
        WHERE R.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   287
          AND  R1.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   288
          AND COALESCE(R1.INCLUDE_PRODUCT, 'Y')='N' MINUS
carlos@0
   289
        SELECT M_PRODUCT_ID
carlos@0
   290
        FROM M_RAPPEL_PRODUCT RP,
carlos@0
   291
          M_RAPPEL R1
carlos@0
   292
        WHERE R.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   293
          AND  R1.M_RAPPEL_ID=RP.M_RAPPEL_ID
carlos@0
   294
          AND COALESCE(R1.INCLUDE_PRODUCT, 'Y')='Y'
carlos@0
   295
        )
carlos@0
   296
        AND I.C_BPARTNER_ID=PL.C_BPARTNER_ID
carlos@0
   297
        AND NOT EXISTS
carlos@0
   298
        (SELECT 1
carlos@0
   299
        FROM M_RAPPEL_INVOICE RI
carlos@0
   300
        WHERE RI.M_RAPPEL_BPARTNER_ID=RP.M_RAPPEL_BPARTNER_ID
carlos@0
   301
          AND RI.C_INVOICE_ID=I.C_INVOICE_ID
carlos@0
   302
        )
carlos@0
   303
        AND RP.ISSOTRX=I.ISSOTRX
carlos@0
   304
        AND RP.M_RAPPEL_BPARTNER_ID=v_Record_ID
carlos@0
   305
      GROUP BY I.AD_CLIENT_ID,
carlos@0
   306
        I.AD_ORG_ID,
carlos@0
   307
        IL.M_PRODUCT_ID,
carlos@0
   308
        RP.ISSOTRX,
carlos@0
   309
        RP.C_BPARTNER_ID,
carlos@0
   310
        IL.C_TAX_ID
carlos@0
   311
      )
carlos@0
   312
    LOOP
carlos@0
   313
      IF(v_invoiceId IS NULL) THEN
carlos@0
   314
        --
carlos@0
   315
        AD_SEQUENCE_NEXT('C_Invoice', CUR_INVOICE.AD_CLIENT_ID, v_invoiceId) ;
carlos@0
   316
        AD_SEQUENCE_DOCTYPE(CUR_INVOICE.C_DOCTYPE_ID, CUR_INVOICE.AD_CLIENT_ID, 'Y', v_DocumentNo) ;
carlos@0
   317
        IF(v_DocumentNo IS NULL) THEN
carlos@0
   318
          AD_SEQUENCE_DOC('DocumentNo_C_Invoice', CUR_INVOICE.AD_CLIENT_ID, 'Y', v_DocumentNo) ;
carlos@0
   319
        END IF;
carlos@0
   320
        SELECT(
carlos@0
   321
          CASE CUR_INVOICE.ISSOTRX WHEN 'Y' THEN C_BPARTNER.PAYMENTRULE ELSE C_BPARTNER.PAYMENTRULEPO
carlos@0
   322
          END
carlos@0
   323
          ), (
carlos@0
   324
          CASE CUR_INVOICE.ISSOTRX WHEN 'Y' THEN C_BPARTNER.C_PAYMENTTERM_ID ELSE C_BPARTNER.PO_PAYMENTTERM_ID
carlos@0
   325
          END
carlos@0
   326
          ), (
carlos@0
   327
          CASE CUR_INVOICE.ISSOTRX WHEN 'Y' THEN C_BPARTNER.M_PRICELIST_ID ELSE C_BPARTNER.PO_PRICELIST_ID
carlos@0
   328
          END
carlos@0
   329
          )
carlos@0
   330
        INTO v_paymentRule,
carlos@0
   331
          v_paymenttermId,
carlos@0
   332
          v_pricelistId
carlos@0
   333
        FROM C_BPARTNER
carlos@0
   334
        WHERE C_BPARTNER.C_BPARTNER_ID=CUR_INVOICE.C_BPARTNER_ID;
carlos@0
   335
        INSERT
carlos@0
   336
        INTO C_INVOICE
carlos@0
   337
          (
carlos@0
   338
            C_INVOICE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   339
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   340
            ISSOTRX, DOCUMENTNO, DOCSTATUS, DOCACTION,
carlos@0
   341
            PROCESSING, PROCESSED, POSTED, C_DOCTYPE_ID,
asier@799
   342
            C_DOCTYPETARGET_ID, C_ORDER_ID, DESCRIPTION,
asier@799
   343
            ISPRINTED, SALESREP_ID, DATEINVOICED,
carlos@0
   344
            DATEPRINTED, DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID,
carlos@0
   345
            POREFERENCE, ISDISCOUNTPRINTED, DATEORDERED, C_CURRENCY_ID,
carlos@0
   346
            PAYMENTRULE, C_PAYMENTTERM_ID, C_CHARGE_ID, CHARGEAMT,
carlos@0
   347
            TOTALLINES, GRANDTOTAL, M_PRICELIST_ID, ISTAXINCLUDED,
asier@799
   348
            C_CAMPAIGN_ID, C_PROJECT_ID, C_ACTIVITY_ID,
asier@799
   349
            CREATEFROM, GENERATETO, AD_USER_ID,
carlos@0
   350
            COPYFROM, ISSELFSERVICE, AD_ORGTRX_ID, USER1_ID,
carlos@0
   351
            USER2_ID
carlos@0
   352
          )
carlos@0
   353
          VALUES
carlos@0
   354
          (
carlos@0
   355
            v_invoiceId, CUR_INVOICE.AD_CLIENT_ID, CUR_INVOICE.AD_ORG_ID, 'Y',
antonio@735
   356
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   357
            CUR_INVOICE.ISSOTRX, v_DocumentNo, 'DR', 'CO',
carlos@0
   358
             'N', 'N', 'N', CUR_INVOICE.C_DOCTYPE_ID,
asier@799
   359
            CUR_INVOICE.C_DOCTYPETARGET_ID, NULL, NULL,
asier@799
   360
            'N', NULL, now(),
antonio@735
   361
            NULL, now(), CUR_INVOICE.C_BPARTNER_ID, CUR_INVOICE.C_BPARTNER_LOCATION_ID,
carlos@0
   362
            NULL, 'N', NULL, CUR_INVOICE.C_CURRENCY_ID,
carlos@0
   363
            v_paymentRule, v_paymenttermId, NULL, 0,
carlos@0
   364
            0, 0, v_pricelistId, 'N',
asier@799
   365
            NULL, NULL, NULL,
asier@799
   366
             'N', 'N', v_AD_User_ID,
carlos@0
   367
             'N', 'N', NULL, NULL,
carlos@0
   368
            NULL
carlos@0
   369
          )
carlos@0
   370
          ;
carlos@0
   371
        AD_SEQUENCE_NEXT('M_Rappel_Invoice', CUR_INVOICE.AD_CLIENT_ID, v_rappelInvoiceId) ;
carlos@0
   372
carlos@0
   373
        SELECT COALESCE(MAX(M_RAPPEL_INVOICE.SEQNO), 0) + 10
carlos@0
   374
        INTO v_seqno
carlos@0
   375
        FROM M_RAPPEL_INVOICE
carlos@0
   376
        WHERE M_RAPPEL_INVOICE.M_RAPPEL_INVOICE_ID=v_rappelInvoiceId;
carlos@0
   377
carlos@0
   378
        INSERT
carlos@0
   379
        INTO M_RAPPEL_INVOICE
carlos@0
   380
          (
carlos@0
   381
            M_RAPPEL_INVOICE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   382
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   383
            M_RAPPEL_BPARTNER_ID, C_INVOICE_ID, SEQNO)
carlos@0
   384
          VALUES
carlos@0
   385
          (
carlos@0
   386
            v_rappelInvoiceId, CUR_INVOICE.AD_CLIENT_ID, CUR_INVOICE.AD_ORG_ID, 'Y',
antonio@735
   387
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   388
            v_Record_ID, v_invoiceId, v_seqno)
carlos@0
   389
          ;
carlos@0
   390
      END IF;
carlos@0
   391
      AD_SEQUENCE_NEXT('C_InvoiceLine', CUR_INVOICE.AD_CLIENT_ID, v_invoicelineId) ;
carlos@0
   392
      v_description:='('||CUR_INVOICE.TOTALLINES||'*'||'('||v_Discount||'/100)) - '||COALESCE(CUR_INVOICE.RAPPELINVOICED, 0) ;
carlos@0
   393
      v_line:=v_line + 10;
carlos@0
   394
      INSERT
carlos@0
   395
      INTO C_INVOICELINE
carlos@0
   396
        (
carlos@0
   397
          C_INVOICELINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   398
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   399
          C_INVOICE_ID, C_ORDERLINE_ID, M_INOUTLINE_ID, LINE,
carlos@0
   400
          DESCRIPTION, M_PRODUCT_ID, QTYINVOICED, PRICELIST,
carlos@0
   401
          PRICEACTUAL, PRICELIMIT, LINENETAMT, C_CHARGE_ID,
carlos@0
   402
          CHARGEAMT, C_UOM_ID, C_TAX_ID, S_RESOURCEASSIGNMENT_ID,
asier@799
   403
          TAXAMT, M_ATTRIBUTESETINSTANCE_ID, ISDESCRIPTION,
carlos@0
   404
          QUANTITYORDER, M_PRODUCT_UOM_ID, PRICESTD, M_OFFER_ID
carlos@0
   405
        )
carlos@0
   406
        VALUES
carlos@0
   407
        (
carlos@0
   408
          v_invoicelineId, CUR_INVOICE.AD_CLIENT_ID, CUR_INVOICE.AD_ORG_ID, 'Y',
antonio@735
   409
          now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   410
          v_invoiceId, NULL, NULL, v_line,
carlos@0
   411
          v_description, CUR_INVOICE.M_PRODUCT_ID, 0, 0,
carlos@0
   412
          0, 0, CUR_INVOICE.LINENET, NULL,
carlos@0
   413
          0, CUR_INVOICE.C_UOM_ID, CUR_INVOICE.C_TAX_ID, NULL,
asier@799
   414
          0, NULL, 'N',
carlos@0
   415
          NULL, NULL, 0, NULL
carlos@0
   416
        )
carlos@0
   417
        ;
carlos@0
   418
    END LOOP;
carlos@0
   419
  END IF;
carlos@0
   420
 END IF;--FINISH_PROCESS
gorkaion@239
   421
 --<<FINISH_PROCESS>>
carlos@0
   422
 --  UPDATE AD_PINSTANCE
carlos@0
   423
 DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || V_MESSAGE) ;
carlos@0
   424
 AD_UPDATE_PINSTANCE(P_PINSTANCE_ID, NULL, 'N', 1, V_MESSAGE) ;
carlos@0
   425
 END; --BODY
carlos@0
   426
EXCEPTION
carlos@0
   427
WHEN OTHERS THEN
carlos@0
   428
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   429
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   430
  ROLLBACK;
carlos@0
   431
  AD_UPDATE_PINSTANCE(P_PINSTANCE_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   432
  RETURN;
adrian@94
   433
END M_RAPPEL_INVOICE_CREATE
gorkaion@239
   434
]]></body>
adrian@94
   435
    </function>
adrian@94
   436
  </database>