<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\DB;

class Bank 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 getAllBanks()
  {
		return Bank::where($this->getCondition())->get();
	}

	public function storeBank($object)
	{
      return \DB::transaction(function () use ($object)
      {
        
        $bank = new Bank;

        $bank->branch_code  =  $object['branch_code'];
        $bank->swift_code   =  $object['swift_code'];
        $bank->title        =  $object['title'];
        $bank->account_no   =  $object['account_no'];
        $bank->bank_name    =  $object['bank_name'];
        $bank->location     =  $object['location'];

        $bank->save();

        return with($bank);
      });
	}

	public function updateBank($object)
  {

     return \DB::transaction(function () use ($object)
     	{
     		$bank = Bank::find($object['id']);

     		if (isset($bank->id)) 
        {
          
         		$bank->branch_code  =  $object['branch_code'];
            $bank->swift_code   =  $object['swift_code'];
            $bank->title        =  $object['title'];
            $bank->account_no   =  $object['account_no'];
            $bank->bank_name    =  $object['bank_name'];
            $bank->location     =  $object['location'];	
     		
            $bank->update();

     		}

        return with($bank);
     		
     	});
	}

	public function removeBank($id)
    {
       
       return \DB::transaction(function () use ($id)
       {
            $bank = Bank::find($id);

            if (isset($bank->id)) 
            {
            	
            	$bank->is_recycle = '1';

            	$bank->update();

            }

              return with($bank);
            
       });
          
    }

    public function getBankById($id)
    {
        $condition = $this->getCondition();
        $condition  += ['id'=>$id];

        return Bank::where($condition)->first();
    }
}
