<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;
use App\MinuteSheet;
use App\Tax;
use App\Account;
use App\Transaction;
use EF;
use HR;

class Payroll extends Model
{

   private $generic_condition;

   private $hr_head_list;

   private $hr_account_list;

    public function __construct()
    {
           
       $this->setCondition(['is_recycle' => '0']);

       $this->hr_head_list    = HR::getHrHeadList();

       $this->hr_account_list = HR::getHrAccountList();
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

    public function storePayroll($object)
    {

          
        return \DB::transaction(function () use ($object){

   
            $payroll                     = new Payroll;
            $payment                     = new Payment;
            $subhead                     = new Account;
            $transaction                 = new Transaction;

             $generic_indexes = EF::getGenericTableIndex();

      //STORING PAYROLL RECORD

            $payroll->user_id            = $object['user_id'];
            $payroll->net_salary         = $object['net_salary'];
            $payroll->minute_sheet_id    = $object['minute_id'];
            $payroll->loan_id            = $object['loan_id'];
            $payroll->tax_id             = $object['tax_id'];
            $payroll->status             = $object['status'];
            $payroll->save();


            //PAYROLL GENERAL VOUCHER DATA       
            $payroll_data['compound_type']         = "General";
            $payroll_data['date']                  = now();
            $payroll_data['auto_jv']               = 1;
            $payroll_data['receipt']               = NULL;

            if($object['payment_mode'] == 'Cash')
            {                  
              $payroll_data['voucher_list']          = 'CPV';
              
            }
            elseif($object['payment_mode'] == 'Bank' || $object['payment_mode'] == 'Online')
            {
                
              $payroll_data['voucher_list']           = 'BPV';

            }
          
                
            for($i= 0; $i < $object['count']; $i++)
            {
                $payroll_data['booking'][$i]             = null;
                $payroll_data['user'][$i]                = $object['user_id'];
                $payroll_data['branch'][$i]              = 1;
                $payroll_data['drawn'][$i]               = $object['drawn'];
                $payroll_data['instrument'][$i]          = $object['instrument'];
                $payroll_data['instrument_date'][$i]     = $object['instrument_date'];
                $payroll_data['payroll_id'][$i]          = $payroll->id; 
                $payroll_data['deposit_no'][$i]          = null;
                $payroll_data['deposit_date'][$i]        = null;

           //ACCOUNTS FOR SALARY 

            if($i == 0)
            {
                $payroll_data['amount'][$i]              = $object['net_salary'];
                // $payroll_data['debit_subhead'][$i]    = 6;
                $payroll_data['debit_subhead'][$i]       = $this->hr_account_list['ES']; // EMPLOYEE SALARY

                $payroll_data['loan_id'][$i]             = 0;
                $payroll_data['narration'][$i]           = 'User Monthly Salary';
                $payroll_data['generic_type'][$i]        = $generic_indexes['Payroll'];
                $payroll_data['payment_type'][$i]        = 'Payroll AutoJV';
                $payroll_data['payment'][$i]             = 0;
                $payroll_data['generic'][$i]             = $payroll->id; 


                if($object['payment_mode'] == 'Cash')
                {
                  
                  $payroll_data['payment_mode'][$i]       = 'Cash';
                  // $payroll_data['credit_subhead'][$i]     = 4;
                  $payroll_data['credit_subhead'][$i]     = $this->hr_account_list['CIH']; // CASH IN HAND;

                  
                }
                elseif($object['payment_mode'] == 'Bank' || $object['payment_mode'] == 'Online')
                {
                    
                  $payroll_data['payment_mode'][$i]       = 'Bank';
                  $payroll_data['credit_head'][$i]        = $object['head_id'];
                  $payroll_data['credit_subhead'][$i]     = $object['subhead_id'];

                }
                  

            }
              //ACCOUNTS FOR LOAN INSTALLMENT

            if($i == 1)
            {
               
               // $payroll_data['credit_subhead'][$i]      = 8;
               $payroll_data['credit_subhead'][$i]         = $this->hr_account_list['LR']; // LOAN RECEIVABLE;

               $payroll_data['amount'][$i]              = $object['loan_amount'];
               $payroll_data['loan_id'][$i]             = $object['loan_id'];
               $payroll_data['narration'][$i]           = 'User Loan Return';
               $payroll_data['payment_mode'][$i]        = 'Cash';
               // $payroll_data['debit_subhead'][$i]       = 9;  
               $payroll_data['debit_subhead'][$i]       = $this->hr_account_list['LRA']; // LOAN RETURN ACCOUNT;  

               $payroll_data['generic_type'][$i]        = $generic_indexes['Loan'];
               $payroll_data['payment_type'][$i]        = 'Payroll AutoJV';
               $payroll_data['payment'][$i]             = 0;
               $payroll_data['generic'][$i]             = $object['loan_id'];          
             
            }
   
        }

           $curr_voucher                     = $payment->createVoucher($payroll_data);
           $voucher_data['status']           = 'Approve';
           $voucher_data['transaction_id']   = $curr_voucher->id;
           $voucher_data['date']             = now();

           $approved_voucher = $transaction->updateStatusByTransaction($voucher_data);

           return with($payroll);
                
        });
    } 


    public function getPayrollByUser($user_id,$minute_sheet_id)
    {
        $condition =  $this->getCondition();
        $condition += ['user_id' => $user_id, 'minute_sheet_id' => $minute_sheet_id];

        return Payroll::where($condition)->first();
    }

      public function getPayrollById($id)
    {
      
       $condition = $this->getCondition();
       $condition += ['id' => $id];
        return Payroll::where($condition)->first();

    }

      public function updatePayrollStatus($object)
  {

     return \DB::transaction(function () use ($object)
      {
        $curr_payroll = Payroll::find($object['id']);

        if (isset($curr_payroll->id)) 
        {
          
            $curr_payroll->status  =  $object['status'];
            $curr_payroll->update();

        }

        return with($curr_payroll);
        
      });
  }




   //---------------- Foriegn key relationships ----------------

    public function user()
    {
      return $this->belongsTo(User::class,'user_id')->with('designation')->withDefault();
    }

    public function minuteSheet()
    {
      return $this->belongsTo(MinuteSheet::class,'minute_sheet_id')->withDefault();
    }

    public function tax()
    {
      return $this->belongsTo(MinuteSheet::class,'tax_id')->withDefault();
    }

    //--------------- End Foriegn key relationships -------------
}
