<?php

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




class Bill_subcategory 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 createSubCategory($object)
    {
      return DB::transaction(function () use ($object){
        $bill_subcategory = new Bill_subcategory;

        $bill_subcategory->name              =  $object['name'];
        $bill_subcategory->bill_category_id  =  $object['bill_category_id'];


        $bill_subcategory->save();

        return with($bill_subcategory);

       });
    }

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

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

    public function checkCategory($name,$id)
    {
        $mycount =  Bill_subcategory::where($this->getCondition())->where('name','ilike',$name)->where('bill_category_id', $id)->count();

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






}
