<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Inventory\ProjectStock;
use App\Inventory\Stock;
use Auth;
use DB;

class Branch 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;
    }


     //RETRIEVING BRANCH BY ID
    public function getBranchById($id)
    {

       $condition = $this->getCondition();
       $condition += ['id' => $id];
        return Branch::where($condition)->first();

    }

    public function getBranchId()
    {
        return Branch::pluck('id')->all();
    }
    
    public function getBranchList()
    {
        return Branch::where($this->getCondition())->orderBy('id','desc')->get();
    }

    public function getBranchListForInvoices()
    {
        return Branch::where($this->getCondition())->whereIn('id',[1])->orderBy('id','desc')->get();
    }

    public function storeBranch($object,$branch_labels)
    {
        return DB::transaction(function () use ($object,$branch_labels)  {

            $branch             = new Branch;
            $branch->code       = $object[$branch_labels['code']];
            $branch->name       = $object[$branch_labels['name']];
            $branch->city       = $object[$branch_labels['city']];
            $branch->start_date = $object[$branch_labels['start_date']];
            $branch->end_date   = $object[$branch_labels['end_date']];
            $branch->location   = $object[$branch_labels['location']];



            // $branch->email      = $object[$branch_labels['email']];
            // $branch->contact    = $object[$branch_labels['contact']];
            // $branch->commission = $object[$branch_labels['commission']];

            $branch->save();

            return with($branch);

        });
    }

    public function updateBranch($object,$branch_labels)
    {

       return DB::transaction(function () use ($object, $branch_labels) {


            $branch = Branch::find($object[$branch_labels['branch']]);

            if(isset($branch->id))
            {
                $branch->code       = $object[$branch_labels['code']];
                $branch->name       = $object[$branch_labels['name']];
                $branch->location   = $object[$branch_labels['location']];

                $branch->city       = $object[$branch_labels['city']];
                $branch->start_date = $object[$branch_labels['start_date']];
                $branch->end_date   = $object[$branch_labels['end_date']];

                // $branch->email      = $object[$branch_labels['email']];
                // $branch->contact    = $object[$branch_labels['contact']];
                // $branch->commission = $object[$branch_labels['commission']];

                $branch->update();

                return with($branch);

            }

            return 0;
        });
    }


    public function removeBranch($id)
    {
        return DB::transaction(function () use ($id) {

            $branch = Branch::find($id);

            if(isset($branch->id))
            {

                $branch->is_recycle   = '1';

                $branch->update();


                return 1;
            }

            return 0;
        });
    }

    public function getQty($id,$project)
    {
      $in_stock = Stock::where('item_id', $id)->where('branch_id',$project)->where('out_quantity' , null)->sum('in_quantity');
      $out_stock = Stock::where('item_id', $id)->where('branch_id',$project)->where('in_quantity' , null)->sum('out_quantity');

      $in_stock2 = ProjectStock::where('item_id', $id)->where('to_branch_id',$project)->sum('stock_in_qty');
      $out_stock2 = ProjectStock::where('item_id', $id)->where('from_branch_id',$project)->sum('stock_out_qty');



      $in_stock = $in_stock+$in_stock2;
      $out_stock = $out_stock+$out_stock2;
      return $qty = $in_stock-$out_stock;
    }
}
