<?php

namespace App\Bill;
use Illuminate\Database\Eloquent\Model;
use DB;
use Crypt;
use App\Journal;
use App\Account;
use App\Payment;
use EF;
use App\Transaction;
use HR;

class Bill_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 createPayable($object)
    {
      return DB::transaction(function () use ($object){
        $bill_payable = new Bill_payable;
        $journal      = new Journal;
        $account      = new Account;
        $payment      = new Payment;
        $transaction  = new Transaction;

        $generic_indexes = EF::getGenericTableIndex();

        $curr_account = $account->getAccountByBillSubCategoryId($object['bill_subcategory_id']);

        $bill_payable->bill_category_id        =          $object['bill_category_id'];
        $bill_payable->bill_subcategory_id     =          $object['bill_subcategory_id'];
        $bill_payable->laibility_type          =          $object['laibility_type'];
        $bill_payable->payable_type            =          $object['payable_type'];
        $bill_payable->amount                  =          $object['amount'];
        $bill_payable->description             =          $object['description'];
        $bill_payable->paid                    =          $object['paid'];
        $bill_payable->no_of_month             =          $object['no_of_month'];
        $bill_payable->month_amount            =          $object['month_amount'];
        $bill_payable->lifetime                =          $object['lifetime'];


        $bill_payable->save();

                     
        $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]                               = 1;
        $jv_data['user'][0]                                 = 0;
        $jv_data['booking'][0]                              = 0;
        $jv_data['amount'][0]                               = $bill_payable->amount;
        $jv_data['narration'][0]                          = 'Autojv against payable';
        $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']                         = $bill_payable->id;
        $jv_data['generic_type'][0]                         = $generic_indexes['Bills'];
        $jv_data['payment_type'][0]                         = 'Bill AutoJV';
        $jv_data['payment'][0]                              = 0;
        $jv_data['generic'][0]                              = $bill_payable->id;
        $jv_data['deposit_no'][0]                           = null;
        $jv_data['deposit_date'][0]                         = null; 

        if($bill_payable->laibility_type == 'Long Term')
        {
            // $jv_data['credit_subhead'][0] = 10;

            $jv_data['credit_subhead'][0] = $this->hr_account_list['LTLFE']; //  LIABILITY FROM EXPENSE (LONG TERM)

        }
        elseif($bill_payable->laibility_type == 'Short Term')
        {

              // $jv_data['credit_subhead'][0]= 11;

              $jv_data['credit_subhead'][0]= $this->hr_account_list['STLFE']; //  LIABILITY FROM EXPENSE (SHORT TERM)


        }
        if(isset($curr_account->id))
        {

            $jv_data['debit_subhead'][0] = $curr_account->id;
        }

       $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 $bill_payable;

       });
    }

    public function getPayableList($bill_category_id)
    {
        return Bill_payable::where($this->getCondition())->where('bill_category_id', $bill_category_id)->get();
    }

    public function getPayableByID($id)
    {
        return Bill_payable::where($this->getCondition())->where('id', $id)->first();
    }

    public function category()
    {
        return $this->belongsTo(Bill_category::class,'bill_category_id');
    }
    public function subcategory()
    {
        return $this->belongsTo(Bill_subcategory::class,'bill_subcategory_id');
    }

    public function getPayableMonthly()
    {
        return Bill_payable::where($this->getCondition())->where('lifetime', 1)->get();
    }






}
