<?php

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




class Bill_category 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 createCategory($object)
    {
      return DB::transaction(function () use ($object){
        $bill_category = new Bill_category;

        $bill_category->name              =  $object['name'];
        $bill_category->expense_type      =  $object['expense_type'];


        $bill_category->save();

        return $bill_category;

       });
    }

    public function getCategoryList($type)
    {
        return Bill_category::where($this->getCondition())->where('expense_type', $type)->get();
    }

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

    public function checkCategory($name,$type)
    {
        $mycount =  Bill_category::where($this->getCondition())->where('name','ilike',$name)->where('expense_type', $type)->count();

        if($mycount > 0)
        {
          return 1;
        }
        else{
          return 0;
        }
    }

}
