src-db/database/model/triggers/C_COMMISSIONAMT_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 785 8dba91261590
child 2078 cf88ca44cdd2
permissions -rw-r--r--
Merge r2.5x intro trunk
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_COMMISSIONAMT_TRG">
adrian@94
     3
    <trigger name="C_COMMISSIONAMT_TRG" table="C_COMMISSIONAMT" fires="before" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
juanpablo@771
     5
    /*************************************************************************
juanpablo@771
     6
    * The contents of this file are subject to the Compiere Public
juanpablo@771
     7
    * License 1.1 ("License"); You may not use this file except in
juanpablo@771
     8
    * compliance with the License. You may obtain a copy of the License in
juanpablo@771
     9
    * the legal folder of your Openbravo installation.
adrian@94
    10
    * Software distributed under the License is distributed on an
adrian@94
    11
    * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
adrian@94
    12
    * implied. See the License for the specific language governing rights
adrian@94
    13
    * and limitations under the License.
juanpablo@778
    14
    * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    15
    * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
adrian@94
    16
    * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
adrian@94
    17
    * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
adrian@94
    18
    * All Rights Reserved.
adrian@94
    19
    * Contributor(s): Openbravo SL
juanpablo@785
    20
    * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    21
    *
juanpablo@771
    22
    * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    23
    * file and version.
adrian@94
    24
    *************************************************************************
adrian@94
    25
    * Update Header GrandTotal
adrian@94
    26
    * Convert Amount to Commission Currrency
adrian@94
    27
    * Calculate Commission Amount
adrian@94
    28
    */
adrian@94
    29
  v_AmtSubtract NUMBER;
adrian@94
    30
  v_AmtMultiplier  NUMBER;
adrian@94
    31
  v_QtySubtract    NUMBER;
adrian@94
    32
  v_QtyMultiplier  NUMBER;
adrian@94
    33
  v_IsPositiveOnly CHAR(1) ;
adrian@94
    34
  --
adrian@94
    35
  v_Result NUMBER;
adrian@94
    36
BEGIN
adrian@94
    37
  IF(UPDATING OR DELETING) THEN
adrian@94
    38
    -- DBMS_OUTPUT.PUT_LINE('C_CommissionAmt_Trg - Subtract');
adrian@94
    39
    -- Subtract old Amount from Header
adrian@94
    40
    UPDATE C_CommissionRun
adrian@94
    41
      SET GrandTotal=GrandTotal - :old.CommissionAmt
adrian@94
    42
    WHERE C_CommissionRun_ID=:old.C_CommissionRun_ID;
adrian@94
    43
  END IF;
adrian@94
    44
  IF(INSERTING OR UPDATING) THEN
adrian@94
    45
    -- DBMS_OUTPUT.PUT_LINE('C_CommissionAmt_Trg - Get Info');
adrian@94
    46
    -- Get Info From CommissionLine
adrian@94
    47
    SELECT AmtSubtract,
adrian@94
    48
      AmtMultiplier,
adrian@94
    49
      QtySubtract,
adrian@94
    50
      QtyMultiplier,
adrian@94
    51
      IsPositiveOnly
adrian@94
    52
    INTO v_AmtSubtract,
adrian@94
    53
      v_AmtMultiplier,
adrian@94
    54
      v_QtySubtract,
adrian@94
    55
      v_QtyMultiplier,
adrian@94
    56
      v_IsPositiveOnly
adrian@94
    57
    FROM C_CommissionLine
adrian@94
    58
    WHERE C_CommissionLine_ID=:new.C_CommissionLine_ID;
adrian@94
    59
    -- Quantities
adrian@94
    60
    v_Result :=(:new.ActualQty - v_QtySubtract) * v_QtyMultiplier;
gorkaion@239
    61
    IF(v_IsPositiveOnly='Y' AND v_Result < 0) THEN
adrian@94
    62
      v_Result:=0;
adrian@94
    63
    END IF;
adrian@94
    64
    -- Amount
adrian@94
    65
    :new.CommissionAmt:=v_Result;
adrian@94
    66
    v_Result :=(:new.ConvertedAmt - v_AmtSubtract) * v_AmtMultiplier;
gorkaion@239
    67
    IF(v_IsPositiveOnly='Y' AND v_Result < 0) THEN
adrian@94
    68
      v_Result:=0;
adrian@94
    69
    END IF;
adrian@94
    70
    :new.CommissionAmt:=:new.CommissionAmt + v_Result;
adrian@94
    71
    -- Add new Amount to Header
adrian@94
    72
    -- DBMS_OUTPUT.PUT_LINE('C_CommissionAmt_Trg - Add');
adrian@94
    73
    UPDATE C_CommissionRun
adrian@94
    74
      SET GrandTotal=GrandTotal + :new.CommissionAmt
adrian@94
    75
    WHERE C_CommissionRun_ID=:new.C_CommissionRun_ID;
adrian@94
    76
  END IF;
antonio@735
    77
END C_COMMISSIONAMT_TRG
gorkaion@239
    78
]]></body>
adrian@94
    79
    </trigger>
adrian@94
    80
  </database>