<?php

namespace App\Http\Controllers\Inventory;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\DecryptException;
use App\Inventory\ReceivingNote;
use App\Inventory\Payable;
use App\Account;
use App\Payment;
use App\Transaction;
use App\Journal;
use EF;
use HR;

class PayableController extends Controller
{
    private $hr_head_list;

    private $hr_account_list;

    public function __construct()
    {
       $this->hr_head_list    = HR::getHrHeadList();

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

    }

  public function view()
    {

      $payable = new Payable;
      $account = new Account;
  
      $payable_list = $payable->getAllPayable();
        $curr_accounts = $account->getSubheadByType('Bank');

          return view('inventory_module.payable.managePayable' , 
            [
              'payable_list' =>  $payable_list,
                'curr_accounts'    => $curr_accounts

            ]);
        }

    public function storePayable(Request $request)
    {

      try
      {

          $id = decrypt($request->grn);
          $grn = new ReceivingNote;
          $curr_grn = $grn->getGrnById($id);

          if(isset($curr_grn->id))
          {

            $grn->updateGrnStatus($id);
            $payable = new Payable;

            $payable_data['po_id']             = $curr_grn->purchaseOrder->id;
            $payable_data['receiving_note_id'] = $curr_grn->id;
            $payable_data['received_amount']   = 0;
            $payable_data['total_amount']      = ($curr_grn->purchaseOrder->unit_price) * ($curr_grn->accepted_quantity);


            $curr_payable = $payable->storePayable($payable_data);
          if(isset($curr_payable->id))
          {
               parent::successMessage('Payable has been generated');
          }

          }

        
    }

     
      catch(DecryptException $e)
      {
        
      }
      return redirect()->back();
    }

   
     public function payPayable(Request $request)
    {

     
        try
        {
            $payable         = new Payable;
            $payment         = new Payment;
            $transaction     = new Transaction;
            $account         = new Account;
            $journal         = new Journal;

            $data            = $request->input();



            $payable_id    = decrypt($data['payable']);
            $subhead_id    = decrypt($data['drawn_on']);


            $curr_payable    =  Payable::find($payable_id);

            $curr_subhead    = Account::find($subhead_id);

            $generic_indexes = EF::getGenericTableIndex();


            if($data['payment_mode'] == 'Cash')
            {

                $form_collect['drawn'][0]              = null;
                $form_collect['instrument'][0]         = null;
                $form_collect['instrument_date'][0]    = null;
                $form_collect['deposit_no']            = null;
                $form_collect['deposit_date']          = null;
                $form_collect['voucher_list']          = "CPV";
                $form_collect['payment_mode'][0]       = "Cash";
                // $form_collect['credit_subhead'][0]     = 4;
                $form_collect['credit_subhead'][0]     = $this->hr_account_list['CIH']; // CASH IN HAND


            }
            elseif(isset($curr_subhead->id) && $data['payment_mode'] != 'Cash')
            {
              

                $form_collect['drawn'][0]              = $curr_subhead->name;
                $form_collect['instrument'][0]         = $data['instrument'];
                $form_collect['instrument_date'][0]    = $data['instrument_date'];
                $form_collect['voucher_list']          = "BPV";
                $form_collect['payment_mode'][0]       = $data['payment_mode'];
                $form_collect['credit_head'][0]        = $curr_subhead->head_id;
                $form_collect['credit_subhead'][0]     = $curr_subhead->id;
                $form_collect['deposit_no']            = null;
                $form_collect['deposit_date']          = null;

            }

            $form_collect['auto_jv'] = 1;

            $form_collect['receipt'] = NULL;
          
            if(isset($curr_payable->id))
            {
              $pending_amount = $curr_payable->total_amount- $curr_payable->received_amount;

              if($data['payable_amount'] > $pending_amount)
              {

                parent::errorMessage('Invalid Payable Amount');

                return redirect()->back();
              }

                $user_id = 0;

                $curr_branch_id = $curr_payable->purchaseOrder->boq->branch_id;

                $branch_id = $curr_branch_id;

                // IP =>  INVENTORY PURCHASES
                $curr_subhead_purchase = $account->getSubHeadByBranchId($curr_branch_id,$this->hr_head_list['IP']);

                if(isset($curr_subhead_purchase->id))
                  {
                       $form_collect['debit_subhead'][0]      = $curr_subhead_purchase->id;
                  }

                $form_collect['compound_type']         = "General";
                $form_collect['date']                  = now();
                $form_collect['payment_type']          = null;
                $form_collect['booking'][0]            = null;       
                $form_collect['user'][0]               = $user_id;
                $form_collect['branch'][0]             = $branch_id;
             

                $form_collect['generic_type'][0]      = $generic_indexes['Inventory Payable'];

                $form_collect['payment_type'][0]      = 'Inventory Payable';
                $form_collect['payment'][0]           = 0;

              
         
                $form_collect['narration'][0]          = "Inventory payable amount ".$data['payable_amount']." is paid against GRN ". $curr_payable->grn->grn_number;
                
                $form_collect['loan_id'][0]            = 0;
                $form_collect['amount'][0]             = $data['payable_amount']; 
                $form_collect['generic'][0]            = $curr_payable->id;


            $curr_voucher          = $payment->createVoucher($form_collect);

            if(isset($curr_voucher->id))
            {

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


           $approved_voucher = $transaction->updateStatusByTransaction($voucher_data);
                parent::successMessage('Payable has been successfully paid');
            }

            }

        }
         catch(DecryptException $e)
        {
           
        }
           return redirect()->back();
    }

}
