tylerzika
3/14/2017 - 6:57 PM

CalculateCommision2.java

/***********************************
Touchpoint Solutions
author: Tyler Zika
link: http://www.touchpointcrm.com
email: support@touchpointcrm.com
date: 03/10/17
description:
***********************************/
global class CalculateCommission2
{
    webservice static String excute(String paymentId)
    {
        String result = 'success';

		// Get all Payment_Invoice__c associated with current Payments_Received__c
		// Make sure Payment_Invoice__c are not of type 'T&E' aka Time and Expense
        List<Payment_Invoice__c> nonTEInvoices =
            [SELECT Id, Amount__c, Invoice__c, Invoice__r.Name,
             Invoice__r.Invoice_Type__c, Invoice__r.Opportunity__r.Name,
             Invoice__r.Opportunity__c, Invoice__r.Opportunity__r.Commission_Team__c,
             Invoice__r.Opportunity__r.Commission_Team__r.Sales_Rep__c,
             Invoice__r.Opportunity__r.Commission_Team__r.Overall_Pct__c,
             Payment__c
             FROM Payment_Invoice__c
             WHERE Payment__c = :paymentId
             AND Invoice__r.Invoice_Type__c != 'T&E'];

		// Make sure each nonTEInvoice has an Opportunity associated.
		// Make sure each associated Opportunity has a comission team
        for(Payment_Invoice__c nonTEInvoice : nonTEInvoices)
        {
            if(nonTEInvoice.Invoice__r.Opportunity__r.Commission_Team__c == null)
            {
                return 'There is no Commission Team for Invoice: '  + nonTEInvoice.Invoice__r.Name + ', Opportunity: ' + nonTEInvoice.Invoice__r.Opportunity__r.Name;
            }
        }

		// Get all Payment_Invoice__c associated with current Payments_Received__c
		// Make sure Payment_Invoice__c are of type 'T&E' aka Time and Expense
        List<Payment_Invoice__c> TEInvoices =
            [SELECT Id, Amount__c, Invoice__c, Invoice__r.Name,
             Invoice__r.Invoice_Type__c, Invoice__r.Opportunity__r.Name,
             Invoice__r.Opportunity__r.Commission_Team__c,
             Invoice__r.Opportunity__r.Commission_Team__r.Sales_Rep__c,
             Invoice__r.Opportunity__r.Commission_Team__r.Overall_Pct__c,
             Payment__c
             FROM Payment_Invoice__c
             WHERE Payment__c = :paymentId
             AND Invoice__r.Invoice_Type__c = 'T&E'];

        // Put all queried TEInvoice ids in a set
        Set<Id> TEInvoiceIds = new Set<Id>();
        Map<Id, Payment_Invoice__c> paymentInvoiceMap = new Map<Id, Payment_Invoice__c>();
        for(Payment_Invoice__c paymentInv : TEInvoices)
        {
            paymentInvoiceMap.put(paymentInv.Invoice__c, paymentInv);
            TEInvoiceIds.add(paymentInv.Invoice__c);
        }

		// Get all line items where time billable is true
		// and associated Invoice is the current invoice.
		// Get each project assocaited with a line item.
		// Get each opportunity associated with the project.
		// Get each comission team associated with an opportunity
        List<Invoice_Line_Item__c> invoiceLineItems =
            [SELECT Id, Invoice__r.Name, Project__c, Project__r.Name,
             Project__r.Opportunity__r.Name, Project__r.Opportunity__r.Commission_Team__c,
             Project__r.Opportunity__r.Commission_Team__r.Sales_Rep__c,
             Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c
             FROM Invoice_Line_Item__c
             WHERE Time_Billable__c = true
             AND Invoice__c in :TEInvoiceIds];

        // Check there is at least one invoice line item found that is related
        // to one of the TEInvoices, which are realted to the current Payment Recieved
        if(invoiceLineItems.size() == 0)
        {
            return 'No invoice line items found related to a T&E Invoice and the current Payment.';
        }

        // Validate that there is at least one Commission Team per
        // Invoice_Line_Item__r.Project__r.Opportunity__r
        for(Invoice_Line_Item__c invoiceLineItem : invoiceLineItems)
        {
            if(invoiceLineItem.Project__r.Opportunity__r.Commission_Team__c == null)
            {
                return 'There is no Commission Team for Invoice: ' +  invoiceLineItem.Invoice__r.Name + ', Project: ' + invoiceLineItem.Project__r.Name + ', Opportunity: ' + invoiceLineItem.Project__r.Opportunity__r.Name;
            }
        }

        // Creates the commission payment records
        List<CommissionPayment__c> commissionPayments = new List<CommissionPayment__c>();

        // First create commision payment record associated
        // with invoices that are of type != T&E

        for(Payment_Invoice__c commissionPayment : nonTEInvoices)
        {
            commissionPayments.add(new CommissionPayment__c(
                Employee__c = commissionPayment.Invoice__r.Opportunity__r.Commission_Team__r.Sales_Rep__c,
                Invoice__c = commissionPayment.Invoice__c,
                Opportunity__c = commissionPayment.Invoice__r.Opportunity__c,
                Payment_Amount__c = commissionPayment.Amount__c,
                CommissionPercent__c = commissionPayment.Invoice__r.Opportunity__r.Commission_Team__r.Overall_Pct__c,
                Commission_Amount__c = (commissionPayment.Amount__c * commissionPayment.Invoice__r.Opportunity__r.Commission_Team__r.Overall_Pct__c)
				)
           );
        }

        // Next create commision payment record associated
        // with invoices that are of type == T&E
/*        for(Payment_Invoice__c commissionPayment : TEInvoices)
        {
            commissionPayments.add(new CommissionPayment__c(
                Employee__c = teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Sales_Rep__c,
                Invoice__c = teInvLineItem.Invoice__c,
                Opportunity__c = teInvLineItem.Project__r.Opportunity__c,
                Payment_Amount__c = paymentInv.Amount__c,
                CommissionPercent__c = teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c,
                Commission_Amount__c = (((teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c != null)
                                       ? teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c:0)
                                       * (paymentInv.Amount__c != null?paymentInv.Amount__c:0))/100));

        }

/*       for(Invoice_Line_Item__c teInvLineItem : [select Id, Invoice__c, Invoice__r.Name, Project__c, Project__r.Name, Project__r.Opportunity__r.Name, Project__r.Opportunity__r.Commission_Team__c, Project__r.Opportunity__c, Project__r.Opportunity__r.Commission_Team__r.Sales_Rep__c, Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c from Invoice_Line_Item__c where Time_Billable__c = true and Invoice__c in :TEInvoiceIds])
        {
            Payment_Invoice__c paymentInv = paymentInvoiceMap.get(teInvLineItem.Invoice__c);
            commissionPayments.add(new CommissionPayment__c(Employee__c = teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Sales_Rep__c, Invoice__c = teInvLineItem.Invoice__c, Opportunity__c = teInvLineItem.Project__r.Opportunity__c, Payment_Amount__c = paymentInv.Amount__c, CommissionPercent__c = teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c, Commission_Amount__c = (((teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c != null)?teInvLineItem.Project__r.Opportunity__r.Commission_Team__r.Overall_Pct__c:0) * (paymentInv.Amount__c != null?paymentInv.Amount__c:0))/100));
        }
*/
        try
        {
            insert commissionPayments;
        }
        catch(Exception ex)
        {
            result = ex.getMessage();
        }
        return result;
    }
}