<?php

namespace App\Bill;
use Illuminate\Database\Eloquent\Model;
use DB;
use Crypt;

class Bill_installment extends Model
{
    private $generic_condition;
    public function __construct()
    {
       $this->setCondition(['is_recycle' => '0']);
    }

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

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

    public function createInstallment($object)
    {
      return DB::transaction(function () use ($object){
        $bill_installment = new Bill_installment;


                $bill_installment->amount   =          $object['amount'];
                $bill_installment->bill_payable_id    =          $object['bill_payable_id'];
                $bill_installment->paid_amount    =          $object['paid_amount'];
                $bill_installment->to_pay    =          $object['to_pay'];
                $bill_installment->installment_no    =          $object['installment_no'];



        $bill_installment->save();

        return $bill_installment;

       });
    }

    public function getInstallmentList($payable_id)
    {
        return Bill_installment::where($this->getCondition())->where('bill_payable_id', $payable_id)->get();
    }

    public function getInstallmentLast($payable_id)
    {
        return Bill_installment::where($this->getCondition())->where('bill_payable_id', $payable_id)->latest('created_at')->first();
    }



}
