Untitled

 avatar
unknown
plain_text
a year ago
7.5 kB
8
Indexable
public static void CreateBillingCreditMemo(Decimal refundAmount,Decimal penaltyAmount,Payment_Summary__c paymentSummary ) {
        
        decimal refundingAmount = 0;
        
        if(penaltyAmount>0 && refundAmount>penaltyAmount){
            refundingAmount =- penaltyAmount;
        }else{
            refundingAmount = refundAmount;
        }
       
        AcctSeed__Billing__c billing 	= [SELECT AcctSeed__Credit_Memo_Reason__c, AcctSeed__Accounting_Period__c, AcctSeed__Accounting_Year__c, AcctSeed__Age__c, AcctSeed__Balance__c, AcctSeed__Billing_Cash_Receipt_Count__c, AcctSeed__Billing_City__c, AcctSeed__Billing_Comment__c, AcctSeed__Billing_Contact__c, AcctSeed__Billing_Terms_Name__c, AcctSeed__Billing_Street__c, AcctSeed__Billing_State__c, AcctSeed__Billing_PostalCode__c, AcctSeed__Billing_Format__c, AcctSeed__Billing_Cycle_Start_Date__c, AcctSeed__Billing_Cycle_End_Date__c, AcctSeed__Billing_Country__c, AcctSeed__Cash_Application_Adjustment_Amount__c, AcctSeed__Closed_Accounting_Period__c, AcctSeed__Credit_Memo_Applied_Amount__c, AcctSeed__Currency_Conversion_Rate__c, AcctSeed__Customer__c, AcctSeed__Date__c, AcctSeed__Destination_Address_Override__c, AcctSeed__Discount_Amount__c, AcctSeed__Discount_Due_Date__c, AcctSeed__Discount_Percent__c, AcctSeed__Due_Date2__c, AcctSeed__Funded_Amount__c, AcctSeed__Ledger_Amount__c, AcctSeed__Ledger__c, AcctSeed__Line_Count__c, AcctSeed__Locked_In_AvaTax__c, AcctSeed__Opportunity__c, AcctSeed__Origin_Address_Override__c, AcctSeed__PDF_Email_Status__c, AcctSeed__PO_Number__c, AcctSeed__Paid_Date__c, AcctSeed__Payment_Link__c, AcctSeed__Payment_Site_URL__c, AcctSeed__Payment_Processor__c, AcctSeed__Proprietary_Billing_Number__c, AcctSeed__Received_Amount__c, AcctSeed__Recurring_Billing__c, AcctSeed__Sales_Tax2__c, AcctSeed__Sales_Tax3__c, AcctSeed__Shipping_City__c, AcctSeed__Shipping_Contact__c, AcctSeed__Shipping_Country__c, AcctSeed__Shipping_PostalCode__c, AcctSeed__Shipping_Street__c, AcctSeed__Shipping_State__c, AcctSeed__Status__c, AcctSeed__Sub_Total__c, AcctSeed__Total__c, AcctSeed__Type__c, AcctSeed__VAT_Line_Count__c, Payment_Summary__c, Name, Id
                                        	FROM AcctSeed__Billing__c WHERE Payment_Summary__c = :paymentSummary.Id LIMIT 1];
        AcctSeed__Billing__c newBilling = billing.clone(false, false, false, false);
       
        insert newBilling;
        
        system.debug('newBilling : '+newBilling.Id);
        
        List<AcctSeed__Billing_Line__c> listBillingLines =  [SELECT Id, AcctSeed__Billing_Has_Applied_Records__c, AcctSeed__Billing__c, AcctSeed__Combined_Tax_Rate__c, AcctSeed__Comment__c, AcctSeed__Date__c, AcctSeed__Discount_Amt__c, AcctSeed__Discount_Pct__c, AcctSeed__Employee__c, AcctSeed__Expense_GL_Account__c, AcctSeed__GL_Account_Variable_1__c, AcctSeed__GL_Account_Variable_2__c, AcctSeed__GL_Account_Variable_4__c, AcctSeed__GL_Account_Variable_3__c, AcctSeed__Hours_Units__c, AcctSeed__Inventory_GL_Account__c, AcctSeed__Ledger__c, AcctSeed__Is_VAT_Related__c, AcctSeed__Line_Destination_Address_Override__c, AcctSeed__Line_Origin_Address_Override__c, AcctSeed__Opportunity_Product_Id__c, AcctSeed__List_Price__c, AcctSeed__Parent_Status__c, AcctSeed__Product_Tax_Type__c, AcctSeed__Product_Unit_Cost__c, AcctSeed__Product__c, AcctSeed__Project_Task__c, AcctSeed__Project__c, AcctSeed__Rate__c, AcctSeed__Recurring_Billing_Line__c, AcctSeed__Revenue_GL_Account__c, AcctSeed__Sub_Total__c, AcctSeed__Tax_Amount2__c, AcctSeed__Tax_Group__c, AcctSeed__Tax_Line__c, AcctSeed__Tax_Inclusive__c, AcctSeed__Tax_Rate__c, AcctSeed__Time_Card_Variable_1__c, AcctSeed__Time_Card_Variable_2__c, AcctSeed__Total__c, Inspection_Type__c, Property_Address__City__s, Property_Address__CountryCode__s, Property_Address__GeocodeAccuracy__s, Property_Address__Latitude__s, Property_Address__Longitude__s, Property_Address__PostalCode__s, Property_Address__StateCode__s, Property_Address__Street__s, Property_Address__c
                                                             FROM AcctSeed__Billing_Line__c WHERE AcctSeed__Billing__c = :billing.Id];
        
        List<AcctSeed__Billing_Line__c> newBillingLineItems = new List<AcctSeed__Billing_Line__c>();
        
        for (AcctSeed__Billing_Line__c originalItem : listBillingLines) {
            AcctSeed__Billing_Line__c newItem 	= originalItem.clone(false, false, false, false);
            newItem.AcctSeed__Billing__c 		= newBilling.Id;
            newItem.AcctSeed__Rate__c 			= - originalItem.AcctSeed__Rate__c;
            newBillingLineItems.add(newItem);  
        }
        
        if (!newBillingLineItems.isEmpty()) {
            insert newBillingLineItems;
        }
       
        AcctSeed__Cash_Receipt__c cashReceipt  			= new AcctSeed__Cash_Receipt__c();
        cashReceipt.AcctSeed__Account__c 				= newBilling.AcctSeed__Customer__c;
        cashReceipt.AcctSeed__Amount__c 				= -refundingAmount;
        cashReceipt.AcctSeed__Payment_Reference__c 	    = newBilling.Name+'-'+'Refund';
        cashReceipt.AcctSeed__Receipt_Date__c 			= Date.Today();
        cashReceipt.AcctSeed__Purpose__c 				= 'Customer Refund';
        cashReceipt.Payment_Summary__c					= newBilling.Payment_Summary__c;  
        
        insert cashReceipt;
        
        
        system.debug('cashReceipt : '+cashReceipt.Id);
        
        AcctSeed__Billing_Cash_Receipt__c billingCashReceipt 	= New AcctSeed__Billing_Cash_Receipt__c();
        
        billingCashReceipt.AcctSeed__Billing__c 				= newBilling.Id;
        billingCashReceipt.AcctSeed__Cash_Receipt__c 			= cashReceipt.Id;
        billingCashReceipt.AcctSeed__Applied_Amount__c 			= cashReceipt.AcctSeed__Amount__c ;
        
        insert billingCashReceipt; 
        
        system.debug('billingCashReceipt : '+billingCashReceipt.Id);
        
        if(penaltyAmount>0){
            AcctSeed__Billing__c penaltyBilling    		= new AcctSeed__Billing__c();
            penaltyBilling.AcctSeed__Customer__c 		= paymentSummary.Account__c;
            penaltyBilling.AcctSeed__Date__c 			= Date.Today();
            penaltyBilling.Payment_Summary__c			= paymentSummary.Id;
            
            insert penaltyBilling;
            
            AcctSeed__Billing_Line__c penaltybl 		= new AcctSeed__Billing_Line__c();
            penaltybl.AcctSeed__Billing__c 				= penaltyBilling.Id;
            penaltybl.AcctSeed__Rate__c					= penaltyAmount;
            
            
            
            
            AcctSeed__Cash_Receipt__c penaltyCashReceipt  		= new AcctSeed__Cash_Receipt__c();
            penaltyCashReceipt.AcctSeed__Account__c 			= paymentSummary.Account__c;
            penaltyCashReceipt.AcctSeed__Amount__c 				= penaltyAmount;
            penaltyCashReceipt.AcctSeed__Payment_Reference__c 	= '';
            penaltyCashReceipt.AcctSeed__Receipt_Date__c 		= Date.Today();
            penaltyCashReceipt.Payment_Summary__c				= paymentSummary.Id;
            
            insert penaltyCashReceipt;
            
            
            AcctSeed__Billing_Cash_Receipt__c penaltybillingCashReceipt = New AcctSeed__Billing_Cash_Receipt__c();
            
            penaltybillingCashReceipt.AcctSeed__Billing__c 			= penaltyBilling.Id;
            penaltybillingCashReceipt.AcctSeed__Cash_Receipt__c 	= penaltyCashReceipt.Id;
            penaltybillingCashReceipt.AcctSeed__Applied_Amount__c 	= penaltyCashReceipt.AcctSeed__Amount__c ;
            
            insert billingCashReceipt; 
            
                    
        }
Editor is loading...
Leave a Comment