<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use DB;
use Illuminate\Contracts\Encryption\DecryptException;
use EF;
use App\PayrollDetail;
use App\Allowance;
use App\ContractDetail;
use App\AssignAllowance;

class PayrollRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    private $parms;
  

    public function __construct($parms)
    {
        $this->parms = $parms;
        $this->setMessage = "Invalid Gross Salary";

        
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

       $minute_id = decrypt($this->parms['minute_id']);
       $user_id   = decrypt($this->parms['user_id']);

       $deduction_sum = 0;
       $allowance_sum = 0;
       $non_taxable_amount = 0;
       $tax = 0;


       $basic_salary = $this->parms['basic_salary'];

       if($this->parms['gross_salary'] < 0)
       {

        $this->setMessage("Gross Salary cannot be negative");
            return false;
       }

        if(@count($this->parms['deduction_amount'])> 0)
       {

       foreach($this->parms['deduction_amount'] as $rows)
       {
        if(is_numeric($rows))
        {
          $deduction_sum += $rows;
        }
        else
        {
           $this->setMessage("Invalid Deduction");
            return false;
        }
        
       }
       }

       if(@count($this->parms['allowance_amount'])> 0)
       {
         foreach($this->parms['allowance_amount'] as $rows)
       {
        if(is_numeric($rows))
        {
          $allowance_sum += $rows;
        }
        else
        {

          $this->setMessage("Invalid Allowance");
            return false;

        }
       }

       $non_taxable_amount = $this->calculateNonTaxableAllowance($this->parms['allowances'],$this->parms['allowance_amount']);

       

       }


       $calculated_gross = $this->calculateGross($basic_salary,$deduction_sum,$allowance_sum);

        

       if($calculated_gross != $this->parms['gross_salary'])
       {

        $this->setMessage("The Gross Salary is Invalid");
        return false;

       }

       // else
       // {
        
       //       $taxable_amount =  $calculated_gross - $non_taxable_amount;

       //       // if($this->parms['taxable_amount'] !=   $taxable_amount )
       //       // {

       //       //         $this->setMessage("The Taxable Amount is Invalid");
       //       //         return false;

       //       // }
       //       // else
       //       // {
       //       //       $tax = $this->calculateTax($taxable_amount)['monthly_tax_amount'];
       //       //      $net_salary = $calculated_gross-$tax;

       //       //      if($this->parms['net_salary'] !=   $net_salary)
       //       //      {

       //       //             $this->setMessage("The Net Salary is Invalid");
       //       //                return false;

       //       //      }
       //       // }


       

       // }

       return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
  

    // SET ERROR MESSAGE
    public function setMessage($msg)
    {
        $this->message = $msg;
    }

    // RETURN CUSTOM ERROR MESSAGE
    public function message()
    {
        return $this->message;
    }

    public function calculateGross($basic_salary,$deduction_sum,$allowance_sum)
    {

        return (($basic_salary + $allowance_sum)-$deduction_sum);

    }

   

    public function calculateNonTaxableAllowance($allowances,$allowance_amount)
    {
        $nontaxable_allowances = 0;

        for($i =0; $i< @count($allowances); $i++)
        {
            $allowance = new Allowance;
            $contract_detail = new ContractDetail;
            $assign_allowance = new AssignAllowance;

            $contract_detail_id[$i] = decrypt($allowances[$i]);

            $curr_contract_detail = $contract_detail->getContractDetailById($contract_detail_id[$i]);

            if(isset($curr_contract_detail->id))
            {
                $assign_allowance_id = $curr_contract_detail->assign_allowance_id;

                $current_assign_allowance = $assign_allowance->getAssignAllowanceById($assign_allowance_id);
                if(isset($current_assign_allowance->id))
                {
                     $allowance_id[$i] = $current_assign_allowance->allowance_id;


               

                  $curr_allowance = $allowance->getAllowanceById($allowance_id[$i]);

                        if(isset($curr_allowance->id))
                        {
                            if($curr_allowance->is_taxable == 0)
                            {
                              $nontaxable_allowances += $allowance_amount[$i];
                            }

                }

               

            }

            
        }
    }

        return $nontaxable_allowances;

    }
    public function calculateTax($taxable_salary)
    {
        $payroll_detail = new PayrollDetail;

        return $payroll_detail->calculateTax($taxable_salary);






    }


}
