• Freddie Giles
  • NEWBIE
  • -1 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
public  class InvoiceDetails_custom {
    
    @AuraEnabled
    public static string  InsertInvoiceDetails(String loanApplicationId, date InvoiceDate, string InvoiceNo,decimal CashOnHire,string InvoiceType,String ChasisNumber,String EngineNumber) {
        
        String Message1='invoice number found';
        String Message2='invoice number found';
        String Message3='inserted';
        String Message4='Locked';
        boolean found=false;
        list<Invoice_Details__c> Invdetails=[select id,Invoice_Date__c,Invoice_No__c,Invoice_Approval_Status__c from Invoice_Details__c where Loan_Application__c=:loanApplicationId];
        
        boolean isCurrentInvoiceApproved = false;   
        
        if(Invdetails != null && Invdetails.size() > 0){
            if(Invdetails[0].Invoice_Approval_Status__c != null &&  Invdetails[0].Invoice_Approval_Status__c == 'Approved' ){
                isCurrentInvoiceApproved = true;
            }
        }
        //getting the dealer details based on the passed opportunty ID
        List<string> dealername = new List<string>();
        list<opportunity> dealerDetails=[select id,Name,Dealer__c,RecordType.developername from opportunity where id=:loanApplicationId];
        String recordTypeName=dealerDetails[0].RecordType.developername;
        system.debug('recordTypeName--->' +recordTypeName);
        for(opportunity op:dealerDetails)
        {
            dealername.add(op.Dealer__c);
        }
        //get the numberof opportuniies based on the dealer name
        list<ID> opIdList= new list<ID>();
        list<opportunity> oppdetails=[select id,Name from opportunity where Dealer__c In:dealername];
        for(opportunity op1:oppdetails)
        {
            opIdList.add(op1.Id);
        }
        system.debug('list***' +opIdList.size());
        list<Invoice_Details__c> InvdetailsOnOpp=[select id,Invoice_Date__c,Invoice_No__c,Invoice_Approval_Status__c,Chasis_Number__c,Engine_number__c from Invoice_Details__c where Loan_Application__c In:opIdList];
        system.debug('++++InvdetailsOnOpp+++' +InvdetailsOnOpp.size());
        for(Invoice_Details__c inv1:InvdetailsOnOpp)
        {
            if(recordTypeName == 'Consumer_Durable' && inv1.Invoice_No__c==InvoiceNo && !isCurrentInvoiceApproved)
            {
                system.debug('inv1.Invoice_Approval_Status__c' +inv1.Invoice_Approval_Status__c);
                
                system.debug('entered Consumer_Durable state');
                found=true;
                system.debug('found' +found);
                return Message2;
            }
            else if(recordTypeName == 'Two_Wheeler' && !isCurrentInvoiceApproved && (inv1.Invoice_No__c==InvoiceNo || inv1.Chasis_Number__c ==ChasisNumber || inv1.Engine_number__c == EngineNumber))
            {
                system.debug('entered two wheeler state');
                found=true;
                system.debug('found' +found);
                return Message2;
            }
        }
        
        
        //list<Invoice_Details__c> invDetails= new list<Invoice_Details__c >();
        if(!found){
            list<Invoice_Details__c> Invdetail=[select id,Invoice_Date__c,Invoice_No__c  from Invoice_Details__c where Loan_Application__c=:loanApplicationId];
            system.debug('not found state');    
            if(Invdetail.isEmpty())
            {
                try{
                    Invoice_Details__c c = new Invoice_Details__c();
                    c.Invoice_Date__c=InvoiceDate;
                    c.Invoice_No__c=InvoiceNo;
                    c.Invoice_Type__c=InvoiceType;
                    c.Loan_Application__c=loanApplicationId;
                    c.Invoice_Amount__c=CashOnHire;
                    c.Chasis_Number__c=ChasisNumber;
                    c.Engine_number__c=EngineNumber;
                    insert c;
                    return Message3; 
                }
                catch(Exception e)
                {
                    System.debug('Exception type caught: ' + e.getTypeName());    
                    System.debug('Message: ' + e.getMessage());    
                    System.debug('Cause: ' + e.getCause());    // returns null
                    System.debug('Line number: ' + e.getLineNumber());    
                    System.debug('Stack trace: ' + e.getStackTraceString());
                    system.debug('error message' +e.getMessage());
                    return e.getMessage();
                }
                
            }
            else{
                // string Approved='Locked';
                // string Approved=Invdetail[0].Approved__c;
                Id InvoiceId = Invdetail[0].id;    
                try{
                    Invoice_Details__c c = new Invoice_Details__c();
                    Invoice_Details__c invoicedet=[select id,Invoice_Date__c,Invoice_No__c,Invoice_Type__c,Invoice_Amount__c from Invoice_Details__c where id=:InvoiceId];
                    c.Id=invoicedet.id;
                    c.Invoice_Date__c=InvoiceDate;
                    c.Invoice_No__c=InvoiceNo;
                    c.Invoice_Type__c=InvoiceType;
                    //c.Loan_Application__c=loanApplicationId;
                    c.Invoice_Amount__c=CashOnHire;
                    c.Chasis_Number__c=ChasisNumber;
                    c.Engine_number__c=EngineNumber;
                    update c;
                    return Message3;
                }
                catch(Exception e)
                {
                    System.debug('Exception type caught: ' + e.getTypeName());    
                    System.debug('Message: ' + e.getMessage());    
                    System.debug('Cause: ' + e.getCause());    // returns null
                    System.debug('Line number: ' + e.getLineNumber());    
                    System.debug('Stack trace: ' + e.getStackTraceString());
                    system.debug('error message' +e.getMessage());
                    return e.getMessage();
                    
                }
            }
        }
        
        return Message3;
    }
    
    public class InvoiceDetailswrapper{
        @AuraEnabled
        public opportunity opportuntyObject {get;set;}
        @AuraEnabled
        public list<Invoice_Details__c> invoiceList {get;set;}
    }
    @AuraEnabled
    public static InvoiceDetailswrapper  getLoanApplicationdetails(String loanApplicationId) {
        String test='test';
        InvoiceDetailswrapper invoiceWrapper = new InvoiceDetailswrapper();
        
        list<opportunity> details=[select id,Name,RecordType.developername from opportunity where id=:loanApplicationId];
        if(details.size()!= null && details.size() >0)
        {
            invoiceWrapper.opportuntyObject = details[0];
        }
        list<Invoice_Details__c> InvdetailsOnOpp=[select id,Invoice_Date__c,Invoice_No__c,Invoice_Amount__c,Loan_Application__r.name,Invoice_Type__c,Invoice_Approval_Status__c,Chasis_Number__c,Engine_number__c from Invoice_Details__c where Loan_Application__c =:loanApplicationId];
        system.debug('invdetaisl***' +InvdetailsOnOpp);
        if(InvdetailsOnOpp.size()!= null && InvdetailsOnOpp.size() >0)
        {
            invoiceWrapper.invoiceList=InvdetailsOnOpp;
        }
        
        system.debug('***loan details2' +InvdetailsOnOpp);
        return invoiceWrapper;
    }
    
    @AuraEnabled
    public static String Newinvoice(String loanApplicationId, date InvoiceDate, string InvoiceNo,decimal CashOnHire,string InvoiceType,String ChasisNumber,String EngineNumber)
    {
        
        list<Invoice_Details__c> Invdetail=[select id,Invoice_Date__c,Invoice_No__c from Invoice_Details__c where Loan_Application__c=:loanApplicationId];
        if(Invdetail.isEmpty())
        {
            try{
                Invoice_Details__c c = new Invoice_Details__c();
                c.Invoice_Date__c=InvoiceDate;
                c.Invoice_No__c=InvoiceNo;
                c.Invoice_Type__c=InvoiceType;
                c.Loan_Application__c=loanApplicationId;
                c.Invoice_Amount__c=CashOnHire;
                c.Chasis_Number__c=ChasisNumber;
                c.Engine_number__c=EngineNumber;
                
                insert c;
                String result1=Approval(loanApplicationId,c.Id); 
                return result1;
                //salesmanger details for the opporunity
            }catch(Exception e)
            {
                return e.getMessage();
            }
        }
        else{
            system.debug('***********entered update condition');
            try{
                Id InvoiceId = Invdetail[0].id;    
                Invoice_Details__c c = new Invoice_Details__c();
                Invoice_Details__c invoicedet=[select id,Invoice_Date__c,Invoice_No__c,Invoice_Type__c,Invoice_Amount__c from Invoice_Details__c where id=:InvoiceId];
                c.Id=invoicedet.id;
                c.Invoice_Date__c=InvoiceDate;
                c.Invoice_No__c=InvoiceNo;
                c.Invoice_Type__c=InvoiceType;
                //c.Loan_Application__c=loanApplicationId;
                c.Invoice_Amount__c=CashOnHire;
                c.Chasis_Number__c=ChasisNumber;
                c.Engine_number__c=EngineNumber;
                update c;
                system.debug('updated' +c.Id);
                String result2=Approval(loanApplicationId,c.Id);
                return result2;
            }catch(Exception e)
            {
                return e.getMessage();
            }
        }        
    }
    
    @AuraEnabled
    public static String Approval(String loanApplicationId,id objectID)
    {
        
        
        List<Id> Approver = new List<Id>();
        list<user> userList=[select Id,Name,Profile.UserLicense.Name,ManagerID,Manager.Name from User where  id=:UserInfo.getUserId() AND isActive=true];
        String UserLicense = userList[0].Profile.UserLicense.Name;
        String ManagerID= userList[0].ManagerID;
        //list<user> ManagerUser= [select id,ManagerID, Manager.Name from user where ManagerID =:ManagerID];
        
        
        //String managerName= u[0].Manager;
        system.debug('userlicense' +UserLicense);
        for(user op3:userList)
        {
            if(op3.Profile.UserLicense.Name == 'Salesforce' )
            {
                Approver.add(op3.Id);
                string approvedMessage = 'success';
                return approvedMessage;
            }
            else if(op3.Profile.UserLicense.Name == 'Partner Community')
            {
                
                system.debug('***Manger name-->' +op3.Profile.UserLicense.Name);
                Id UserID= getUserId(ManagerID);
                system.debug('UserID' +UserID);
                //Approver.add(op3.ManagerID);
                if(UserId == null)
                {
                    system.debug('***user id is null');
                    String message='no userId found';
                    return message;
                }
                else
                {
                    system.debug('***user id is not null');
                    Approver.add(UserId);
                    
                }
            }
            
            
        }
        system.debug('Approver---->ID' +Approver);
        //  system.debug('salesofficer' +so);
        list<Invoice_Details__c> invdetailsUpdate=[select id,Approver__c from Invoice_Details__c where Loan_Application__c=:loanApplicationId];
        user userdetails=[select id,name from user where id in:Approver];
        Invoice_Details__c c = new Invoice_Details__c();
        c.Id=objectID;
        c.Approver__c=userdetails.name;
        update c;
        system.debug('userDetails' +userdetails);
        system.debug('InvoiceDetailsResult' +c); 
        //approval process 
        // system.debug(so);    
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(objectID);
        req.setProcessDefinitionNameOrId('DuplicateInvoiceNumber');
        //List<Id> appList = new List<Id>();
        //appList.add('0056F0000082Ooo');
        req.setNextApproverIds(Approver);
        req.setSkipEntryCriteria(true);
        
        Approval.ProcessResult processResult = Approval.process(req);
        system.debug('****status' +processResult.getInstanceStatus());
        string StatusResult = processResult.getInstanceStatus();
        
        
        String message1='success';
        return message1;
    }
    
    public static Id getUserId(string ManagerID)
    {
        system.debug('***managerName**' +ManagerID);
        list<user> managerlist =[select id,Profile.UserLicense.Name,ManagerId,Manager.Name from user where Id=:ManagerID And isActive=true];
        system.debug('userList' +managerlist);
        
        if(managerlist[0].Profile.UserLicense.Name == 'salesforce')
        {
            system.debug('***manager id is null' +managerlist[0].Profile.UserLicense.Name);
            return managerlist[0].id;
        }
        else if(managerlist[0].Profile.UserLicense.Name == 'Partner Community'){
            system.debug('***partner comunity' +managerlist[0].Profile.UserLicense.Name);
            if(managerlist[0].ManagerId != null){
                system.debug('managerlist[0].Manager.Name' +managerlist[0].Manager.Name);
                getUserId(managerlist[0].ManagerId);
            }else{
                return null;
            }
            
        }
        
        return null;
    }

    @AuraEnabled
    public static list<Invoice_Details__c> getApproverDetails(String loanApplicationId)
    {
          list<Invoice_Details__c> InvApproverList=[select id,Invoice_Date__c,Invoice_No__c,Approver__c,Invoice_Approval_Status__c from Invoice_Details__c where Loan_Application__c=:loanApplicationId];
        if(InvApproverList.size() != null)
        {
          system.debug('&&&InvApproverList--' +InvApproverList);
                return InvApproverList;
            }
        else{
        return null;
        }
    }
           
}