src-db/database/model/triggers/C_BPARTNER_TRG.xml
author RM packaging bot <staff.rm@openbravo.com>
Thu, 12 Dec 2019 06:09:26 +0000
changeset 36860 6630d0f5b86d
parent 34824 463468c986a3
permissions -rw-r--r--
Merge back from main
<?xml version="1.0"?>
  <database name="TRIGGER C_BPARTNER_TRG">
    <trigger name="C_BPARTNER_TRG" table="C_BPARTNER" fires="after" insert="true" update="true" delete="false" foreach="row">
      <body><![CDATA[
  /*************************************************************************
  * The contents of this file are subject to the Compiere Public
  * License 1.1 ("License"); You may not use this file except in
  * compliance with the License. You may obtain a copy of the License in
  * the legal folder of your Openbravo installation.
  * 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  Compiere  ERP &  Business Solution
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
  * All Rights Reserved.
  * Contributor(s): Openbravo SLU
  * Contributions are Copyright (C) 2001-2018 Openbravo, S.L.U.
  *
  * Specifically, this derivative work is based upon the following Compiere
  * file and version.
  *************************************************************************
  * $Id: C_BPartner_Trg.sql,v 1.4 2002/10/23 03:16:57 jjanke Exp $
  ***
  * Title: Business Partner new necord
  * Description:
  *    - create default Account records
  *    - add treenode
  ************************************************************************/

   TYPE RECORD IS REF CURSOR;
   Cur_Defaults RECORD;
   Cur_Defaults2 RECORD;

  v_BP_Acct_ID                varchar2(32);
  v_C_Receivable_Acct         varchar2(32);
  v_C_Prepayment_Acct         varchar2(32);
  v_V_Liability_Acct          varchar2(32);
  v_V_Liability_Services_Acct varchar2(32);
  v_V_Prepayment_Acct         varchar2(32);
  v_E_Expense_Acct            varchar2(32);
  v_E_Prepayment_Acct         varchar2(32);
  v_seq_name		              varchar2(32);
  v_Sequence_No		            varchar2(32);
  v_count 					  NUMBER;
  v_currency_pl							VARCHAR2(32);
  v_currency_credit					VARCHAR2(32);
  v_currency_iso						VARCHAR2(32);
BEGIN
    
    IF AD_isTriggerEnabled()='N' THEN RETURN;
    END IF;


    --  Default Accounts for all AcctSchema
	IF (INSERTING) THEN
	FOR Cur_Defaults IN
	(
		SELECT AD_IsOrgIncluded(:new.AD_Org_ID, a.AD_Org_ID, :new.AD_Client_ID), AD_IsOrgIncluded(a.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID),
    a.ad_sequence_bp_id, a.isbpnewaccount, a.acct_length, a.subacct_length, a.createdby, a.updatedby,	a.C_AcctSchema_ID, 
    d1.ad_client_id, d1.C_Receivable_Acct, d1.C_Prepayment_Acct, d1.V_Liability_Acct,	d1.V_Liability_Services_Acct, 
    d1.V_Prepayment_Acct, d1.status, d1.ISACTIVE 
   	FROM  C_BP_Group_Acct d1, ad_Org_AcctSchema a
		WHERE d1.C_BP_Group_ID=:new.C_BP_Group_ID
		AND d1.C_AcctSchema_ID = a.C_AcctSchema_ID
		AND a.ISACTIVE = 'Y'
		AND (AD_IsOrgIncluded(:new.AD_Org_ID, a.AD_Org_ID, :new.AD_Client_ID) <> -1
		OR AD_IsOrgIncluded(a.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID) <> -1)
		ORDER BY CASE WHEN AD_IsOrgIncluded(:new.AD_Org_ID, a.AD_Org_ID, :new.AD_Client_ID) <> -1
		THEN AD_IsOrgIncluded(:new.AD_Org_ID, a.AD_Org_ID, :new.AD_Client_ID)
		ELSE AD_IsOrgIncluded(a.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID)
		END
	)
	LOOP
	

		/*
		    Creating new sub account
		*/
		--  Customer
		 select count(*) into v_count from C_BP_Vendor_Acct where c_acctschema_id = Cur_Defaults.C_AcctSchema_ID AND c_bpartner_id = :new.C_BPartner_ID AND ((status IS NULL AND Cur_Defaults.status IS NULL) OR status = Cur_Defaults.status);
	IF (v_count=0) THEN
         Ad_Sequence_Next('C_BP_Customer_Acct', Cur_Defaults.C_AcctSchema_ID, v_BP_Acct_ID);
         IF (Cur_Defaults.IsBPNewAccount='Y') THEN
		
              /*
                Retrieving the sequence NUMBER for sub account.	
              */
              SELECT NAME INTO v_seq_name FROM AD_SEQUENCE WHERE AD_SEQUENCE.AD_SEQUENCE_ID=Cur_Defaults.Ad_Sequence_BP_Id;
              AD_Sequence_Doc(v_seq_name, Cur_Defaults.AD_Client_ID, 'Y', v_Sequence_No);

              /*
                  Retrieving the receivable account NUMBER
              */
              IF (Cur_Defaults.C_Receivable_Acct IS NOT NULL) THEN
                c_validcombination_clone(Cur_Defaults.C_Receivable_Acct, v_Sequence_No, :new.Name, Cur_Defaults.acct_length, Cur_Defaults.subacct_length, v_C_Receivable_Acct);
              END IF;

               /*
                  Retrieving the prepayment account NUMBER
               */
               IF (Cur_Defaults.C_Prepayment_Acct IS NOT NULL) THEN
                 c_validcombination_clone(Cur_Defaults.C_Prepayment_Acct, v_Sequence_No, :new.Name, Cur_Defaults.acct_length, Cur_Defaults.subacct_length, v_C_Prepayment_Acct);
               END IF;
			            
             /*
                  Inserting the new account NUMBER
             */
              INSERT INTO C_BP_Customer_Acct
              (C_BP_Customer_Acct_ID,C_BPartner_ID, C_AcctSchema_ID,
              AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
              C_Receivable_Acct, C_Prepayment_Acct, Status)
              VALUES
              (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
              :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
              v_C_Receivable_Acct, v_C_Prepayment_Acct, Cur_Defaults.Status);
         ELSE
        			/*
                Creating default account
              */

              INSERT INTO C_BP_Customer_Acct
              (C_BP_Customer_Acct_ID,C_BPartner_ID, C_AcctSchema_ID,
              AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
              C_Receivable_Acct, C_Prepayment_Acct, Status)
              VALUES
              (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
              :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
              Cur_Defaults.C_Receivable_Acct, Cur_Defaults.C_Prepayment_Acct, Cur_Defaults.Status);
         END IF;
			--  Vendor
              /* 
                Creating new sub account
              */
          		Ad_Sequence_Next('C_BP_Vendor_Acct', Cur_Defaults.C_AcctSchema_ID, v_BP_Acct_ID);
         IF (Cur_Defaults.IsBPNewAccount='Y') THEN         
                     
	        /*
                Retrieving the liability account NUMBER 
               */
               IF (Cur_Defaults.V_Liability_Acct IS NOT NULL) THEN
                 c_validcombination_clone(Cur_Defaults.V_Liability_Acct, v_Sequence_No, :new.Name, Cur_Defaults.acct_length, Cur_Defaults.subacct_length, v_V_Liability_Acct);
               END IF;

               /*
                 Retrieving the liability services account NUMBER
               */ 
               IF (Cur_Defaults.V_Liability_Services_Acct IS NOT NULL) THEN
                 c_validcombination_clone(Cur_Defaults.V_Liability_Services_Acct, v_Sequence_No, :new.Name, Cur_Defaults.acct_length, Cur_Defaults.subacct_length, v_V_Liability_Services_Acct);
               END IF;

	       /*
                 Retrieving the prepayment account NUMBER
               */
               IF (Cur_Defaults.V_Prepayment_Acct IS NOT NULL) THEN
                 c_validcombination_clone(Cur_Defaults.V_Prepayment_Acct, v_Sequence_No, :new.Name, Cur_Defaults.acct_length, Cur_Defaults.subacct_length, v_V_Prepayment_Acct);
               END IF;

    		/*
                 Inserting the new account NUMBER
               */
              INSERT INTO C_BP_Vendor_Acct
              (C_BP_Vendor_Acct_ID, C_BPartner_ID, C_AcctSchema_ID,
              AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
              V_Liability_Acct, V_Liability_Services_Acct, V_Prepayment_Acct, Status)
              VALUES
              (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
              :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
              v_V_Liability_Acct, v_V_Liability_Services_Acct, v_V_Prepayment_Acct, Cur_Defaults.Status);
		     ELSE
              /*
                Creating default account
              */
			        INSERT INTO C_BP_Vendor_Acct
              (C_BP_Vendor_Acct_ID, C_BPartner_ID, C_AcctSchema_ID,
              AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
              V_Liability_Acct, V_Liability_Services_Acct, V_Prepayment_Acct, Status)
              VALUES
              (v_BP_Acct_ID,:new.C_BPartner_ID, Cur_Defaults.C_AcctSchema_ID,
              :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
              Cur_Defaults.V_Liability_Acct, Cur_Defaults.V_Liability_Services_Acct, Cur_Defaults.V_Prepayment_Acct, Cur_Defaults.Status);
         END IF;
              
  END IF;
  END LOOP;
-- Employee
  v_count:=0;
              FOR Cur_Defaults2 IN (
              SELECT AD_IsOrgIncluded(:new.AD_Org_ID, oa.AD_Org_ID, :new.AD_Client_ID), AD_IsOrgIncluded(oa.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID),
              oa.ad_sequence_bp_id, oa.isbpnewaccount, oa.acct_length, oa.subacct_length, oa.createdby, oa.updatedby,
              d2.E_Expense_Acct, d2.E_Prepayment_Acct, d2.AD_Client_ID, d2.C_AcctSchema_ID, d2.ISACTIVE
              FROM C_AcctSchema_Default d2, AD_Org_AcctSchema oa 
              WHERE d2.C_AcctSchema_ID = oa.C_AcctSchema_ID
              AND oa.ISACTIVE = 'Y'
              AND oA.AD_Client_ID = d2.AD_Client_ID
              AND (AD_IsOrgIncluded(:new.AD_Org_ID, oa.AD_Org_ID, :new.AD_Client_ID) <> -1
              OR AD_IsOrgIncluded(oa.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID) <> -1)
              ORDER BY CASE WHEN AD_IsOrgIncluded(:new.AD_Org_ID, oa.AD_Org_ID, :new.AD_Client_ID) <> -1
              THEN AD_IsOrgIncluded(:new.AD_Org_ID, oa.AD_Org_ID, :new.AD_Client_ID)
              ELSE AD_IsOrgIncluded(oa.AD_Org_ID, :new.AD_Org_ID, :new.AD_Client_ID)
              END
	) 
  LOOP
	
	   /*
	    Employee
	   */
			 /*
        Creating new sub account
       */
       select count(*) into v_count from C_BP_Employee_Acct where c_acctschema_id = Cur_Defaults2.C_AcctSchema_ID AND c_bpartner_id = :new.C_BPartner_ID;
		 IF (v_count=0) THEN
         IF (Cur_Defaults2.IsBPNewAccount='Y') THEN
         
             /*
                Retrieving the Expense account NUMBER
             */
             IF (Cur_Defaults2.E_Expense_Acct IS NOT NULL) THEN
               c_validcombination_clone(Cur_Defaults2.E_Expense_Acct, v_Sequence_No, :new.Name, Cur_Defaults2.acct_length, Cur_Defaults2.subacct_length, v_E_Expense_Acct);
             END IF;

             /*
                 Retrieving the Prepayment account NUMBER
             */
             IF (Cur_Defaults2.E_Prepayment_Acct IS NOT NULL) THEN
               c_validcombination_clone(Cur_Defaults2.E_Prepayment_Acct, v_Sequence_No, :new.Name, Cur_Defaults2.acct_length, Cur_Defaults2.subacct_length, v_E_Prepayment_Acct);
             END IF;

	      /*
                 Inserting the new account NUMBER 
             */
	 		
              INSERT INTO C_BP_Employee_Acct
                (C_BP_Employee_Acct_ID, C_BPartner_ID, C_AcctSchema_ID,
                AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
                E_Expense_Acct, E_Prepayment_Acct)
               VALUES
                (get_uuid(), :new.C_BPartner_ID, Cur_Defaults2.C_AcctSchema_ID,
                :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults2.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
                v_E_Expense_Acct, v_E_Prepayment_Acct);
            ELSE
                INSERT INTO C_BP_Employee_Acct
                (C_BP_Employee_Acct_ID, C_BPartner_ID, C_AcctSchema_ID,
                AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,
                E_Expense_Acct, E_Prepayment_Acct)
               VALUES
                (get_uuid(), :new.C_BPartner_ID, Cur_Defaults2.C_AcctSchema_ID,
                :new.AD_Client_ID, :new.AD_Org_ID, Cur_Defaults2.ISACTIVE, now(), :new.CreatedBy, now(), :new.UpdatedBy,
                Cur_Defaults2.E_Expense_Acct, Cur_Defaults2.E_Prepayment_Acct);
            
            END IF;
      END IF;
    
     END LOOP;

  ELSIF (UPDATING) THEN
  
		 IF (:new.M_PRICELIST_ID <> :old.M_PRICELIST_ID) THEN
		 -- When Updating the Price List, check if the Business Partner has already generated Credit
		 -- In that case, do not allow to update to a Price List with a different Currency from the generated Credit.
       SELECT c_currency_id
       INTO v_currency_pl
       FROM m_pricelist pl
       JOIN c_currency USING (c_currency_id)
       WHERE m_pricelist_id = :new.M_PRICELIST_ID;
       
     --Currency of the generated Credit not consumed yet.
       SELECT MAX(p.c_currency_id)
       INTO v_currency_credit
       FROM fin_payment p
       WHERE p.c_bpartner_id = :new.C_BPartner_ID
       AND p.generated_credit <> 0
       AND obequals(p.generated_credit, p.used_credit) = 'N';
      
			 IF (v_currency_pl <> COALESCE(v_currency_credit, v_currency_pl)) THEN
			   SELECT iso_code
			   INTO v_currency_iso
			   FROM c_currency
			   WHERE c_currency_id = v_currency_credit;
			   
         RAISE_APPLICATION_ERROR(-20000, '@C_BPWithCreditPriceList@' || ' ' || v_currency_iso || '. ' || '@C_BPWithCreditPriceList2@');
			 END IF;
     END IF;
     IF(:old.AD_Org_ID <> :new.AD_Org_ID) THEN 
	     UPDATE C_BP_CUSTOMER_ACCT SET AD_ORG_ID = :new.AD_Org_ID
	     WHERE C_BPARTNER_ID = :new.C_BPartner_ID;
	     UPDATE C_BP_VENDOR_ACCT SET AD_ORG_ID = :new.AD_Org_ID
	     WHERE C_BPARTNER_ID = :new.C_BPartner_ID;
	     UPDATE C_BP_EMPLOYEE_ACCT SET AD_ORG_ID = :new.AD_Org_ID
	     WHERE C_BPARTNER_ID = :new.C_BPartner_ID;
	   END IF;
  END IF;
EXCEPTION
 WHEN NO_DATA_FOUND THEN
  RAISE_APPLICATION_ERROR(-20000, '@20004@');

END C_BPARTNER_TRG
]]></body>
    </trigger>
  </database>