<?php

namespace App\Inventory;

use Illuminate\Database\Eloquent\Model;
use EF;
use App\Payment;
use App\Transaction;
use App\Account;
use HR;

class Payable 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 getAllPayable($condition = [])
    {
      $condition += $this->getCondition();
      return Payable::where($condition)->get();
    }

    

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

    public function getPayableListByGRN($payable_condition = [])
    {
      $condition = $this->getCondition();
      $condition += $payable_condition;
      return Payable::where($condition)->orderBy('receiving_note_id', 'asc')->get();
    }

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


          $payable                                        = new Payable;
          $payment                                        = new Payment;
          $transaction                                    = new Transaction;
          $account                                        = new Account;

          $generic_indexes                                = EF::getGenericTableIndex();
          $payable->purchase_order_id                     = $object['po_id'];
          $payable->receiving_note_id                     = $object['receiving_note_id'];
          $payable->total_amount                          = $object['total_amount'];
          $payable->received_amount                       = $object['received_amount'];
          $payable->save();


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

          $branch = $payable->purchaseOrder->boq->branch;

        //   if(isset($branch->id)  && $account->isFoundBranchSubhead($branch->id) == 0)
        //   {
            
        //        // $account_data['head_id']             = 10;
        //        $account_data['head_id']             = $this->hr_head_list['IP']; // INVENTORY PURCHASES

        //        $account_data['name']                = $branch->name;
        //        $account_data['user_id']             = 0;
        //        $account_data['scale_id']            = 0;
        //        $account_data['designation_id']      = 0;
        //        $account_data['branch_id']           = $branch->id;
        //        $account_data['flag']                = 1;
        //        $account_data['type']                = 'General';



        //        $curr_account_purchase = $account->storeSubHead($account_data);

        //        // $account_data['head_id'] = 11;

        //        $account_data['head_id'] = $this->hr_head_list['IE']; // INVENTORY EXPENSES


        //        $curr_account_expense = $account->storeSubHead($account_data);
        //   }

     

          // $curr_subhead_purchase = $account->getSubHeadByBranchId($curr_branch_id,10);
          // $curr_subhead_expense  = $account->getSubHeadByBranchId($curr_branch_id,11);

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

        //   if(isset($curr_subhead_purchase->id)&& isset($curr_subhead_expense->id))
        //   {

        //      $jv_data['debit_subhead'][0]       = $curr_subhead_expense->id;
        //      $jv_data['credit_subhead'][0]      = $curr_subhead_purchase->id;

        //      $jv_data['compound_type']                           = "General";
        //       $jv_data['voucher_list']                            = 'JV';
        //       $jv_data['date']                                    = now();
        //       $jv_data['auto_jv']                                 = 1;
        //       $jv_data['receipt']                                 = NULL;
        //       $jv_data['branch'][0]                               = $branch->id;
        //       $jv_data['user'][0]                                 = 0;
        //       $jv_data['booking'][0]                              = 0;
        //       $jv_data['amount'][0]                               = $payable->total_amount;
        //       $jv_data['narration'][0]                            = 'Inventory Payable against '.$payable->grn->grn_number;
        //       $jv_data['payment_mode'][0]                         = 'Cash';
        //       $jv_data['instrument'][0]                           = NULL;
        //       $jv_data['instrument_date'][0]                      = NULL;
        //       $jv_data['drawn'][0]                                = NULL;
        //       $jv_data['bill_payable_id']                         = 0;
        //       $jv_data['generic_type'][0]                         = $generic_indexes['Inventory'];
        //       $jv_data['payment_type'][0]                         = 'Inventory AutoJV';
        //       $jv_data['payment'][0]                              = 0;
        //       $jv_data['generic'][0]                              = $payable->id;
        //       $jv_data['deposit_no'][0]                           = null;
        //       $jv_data['deposit_date'][0]                         = null; 
             
            

        //      $curr_voucher                     = $payment->createVoucher($jv_data);

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

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

        //  }
        
        


            return with($payable);


        });
    }


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


          $payable                                        = new Payable;
          $payment                                        = new Payment;
          $transaction                                    = new Transaction;
          $account                                        = new Account;

          $generic_indexes                                = EF::getGenericTableIndex();
          $payable->purchase_order_id                     = $object['id'];
          $payable->receiving_note_id                     = NULL;
          $payable->total_amount                          = $object['total_amount'];
          $payable->received_amount                       = $object['received_amount'];
          
          $payable->save();

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

          $branch = $payable->purchaseOrder->boq->branch;


            return with($payable);


        });
    }


        public function updatePayableByPayment($id,$new_amount,$old_amount)
         {

        return \DB::transaction(function () use ($id,$new_amount,$old_amount)  {
        $payable = Payable::find($id);

        if(isset($payable->id))
        {
            // FOR REBACK INSTALLMENT
            if($old_amount > 0)
            {
             
               $payable->received_amount -= $old_amount;
            }
            // FOR NEWER INSTALLMENT
            if($new_amount > 0)
            {
               $payable->received_amount += $new_amount;

            }
          
            // THIS IF FIND INSALLMENT IS COMPLETED PAID OR NOT
            if($payable->received_amount >= $payable->total_amount)
            {
                $payable->status = 'Approved';
            }
            else
            {
                $payable->status = 'Pending';
            }

            // dd($payable);
            $payable->update();
         

            return with($payable);
        }

      });
    }

    public function purchaseOrder()
    {
        return $this->belongsTo(PurchaseOrder::class,'purchase_order_id')->withDefault();
    }

     public function grn()
    {
        return $this->belongsTo(ReceivingNote::class,'receiving_note_id')->withDefault();
    }
}
