<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Lib_item_model extends CI_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function all_data($tbl="")
    {
        $this->db->select("*");
        $this->db->where("is_delete",0);
        $r=$this->db->get($tbl)->result_array();
        return $r;
    }
    
    public function all_data_order($tbl="")
    {
        $this->db->select("*");
        $this->db->where("is_delete",0);
         $this->db->ORDER_BY('name'); 
        $r=$this->db->get($tbl)->result_array();
        return $r;
    }
    
     public function all_data_last($tbl="")
    {
        $this->db->select("*");
        $this->db->limit(1);
        $this->db->order_by('id','DESC');
        $this->db->where("is_delete",0);
        $r=$this->db->get($tbl)->result_array();
        return $r;
    }
    public function all_datawithoutisdelete($tbl="")
    {
        $this->db->select("*");
        $r=$this->db->get($tbl)->result_array();
        return $r;
    }
    public function SearchById($id=0,$tbl="")
    {
        //echo $id;exit;
        $this->db->select('*');
        $ret=$this->db->where('is_delete',0)->where('id',$id)->get($tbl)->row_array();
        return $ret;
    }
     public function SearchById_last($tbl="")
    {
        $this->db->select('*');
        $this->db->limit(1);
        $this->db->order_by('id','DESC');
        $ret=$this->db->where('is_delete',0)->get($tbl)->row_array();
        return $ret;
    }
    public function SearchByIdWithOutDel($id=0,$tbl="")
    {
        $this->db->select('*');
        $ret=$this->db->where('id',$id)->get($tbl)->row_array();
        return $ret;
    }
    public function SearchByIdWithOutisb($id=0,$tbl="")
    {
        $this->db->select('*');
        $ret=$this->db->where('isbn',$id)->get($tbl)->row_array();
        return $ret;
    }
//===========================Save item===========================================
    public function save_item($data)
    {
        if($data['item_id']>0){
           
            $get_cat_name=$this->db->select('*')
                                   ->from('catagory')
                                   ->where('id',$data['cat_id'])
                                   ->get()->row_array()['name'];
            $get_type_name=$this->db->select('*')
                                   ->from('item_type')
                                   ->where('id',$data['type_id'])
                                   ->get()->row_array()['name']; 
            $get_writer_name=$this->db->select('*')
                                   ->from('writer')
                                   ->where('id',$data['writer'])
                                   ->get()->row_array()['name'];  
             $get_publisher_name=$this->db->select('*')
                                   ->from('publisher')
                                   ->where('id',$data['publisher'])
                                   ->get()->row_array()['name']; 
            $get_almirah_name=$this->db->select('*')
                                   ->from('almirah')
                                   ->where('id',$data['almirah'])
                                   ->get()->row_array()['name'];  
                                   
            $fields = array(
                    "cat_id"=>$data['cat_id'],
                    "cat_name"=>$get_cat_name,
                    "type_id"=>$data['type_id'],
                    "type_name"=>$get_type_name,
                    "writer_id"=>$data['writer'],
                    "writer_name"=>$get_writer_name,
                    "publisher_id"=>$data['publisher'],
                    "pub_name"=>$get_publisher_name,
                    "isbn"=>$data['isbn'],
                    "almirah_no"=>$data['almirah'],
                    "alm_name"=>$get_almirah_name,
                    "shelf_no"=>$data['shelf'],
                    "name"=>$data['name'],
                    "edition"=>$data['edition'],
                      "price"=>$data['price']
                    );
            $this->db->where('id',$data['item_id']);
            $this->db->update('item',$fields);
        }else{
            
            $get_cat_name=$this->db->select('*')
                                   ->from('catagory')
                                   ->where('id',$data['cat_id'])
                                   ->get()->row_array()['name'];
            $get_type_name=$this->db->select('*')
                                   ->from('item_type')
                                   ->where('id',$data['type_id'])
                                   ->get()->row_array()['name']; 
            $get_writer_name=$this->db->select('*')
                                   ->from('writer')
                                   ->where('id',$data['writer'])
                                   ->get()->row_array()['name'];  
             $get_publisher_name=$this->db->select('*')
                                   ->from('publisher')
                                   ->where('id',$data['publisher'])
                                   ->get()->row_array()['name']; 
            $get_almirah_name=$this->db->select('*')
                                   ->from('almirah')
                                   ->where('id',$data['almirah'])
                                   ->get()->row_array()['name'];                        
             $fields = array(
                    "cat_id"=>$data['cat_id'],
                    "cat_name"=>$get_cat_name,
                    "type_id"=>$data['type_id'],
                    "type_name"=>$get_type_name,
                    "writer_id"=>$data['writer'],
                    "writer_name"=>$get_writer_name,
                    "publisher_id"=>$data['publisher'],
                    "pub_name"=>$get_publisher_name,
                    "isbn"=>$data['isbn'],
                    "almirah_no"=>$data['almirah'],
                    "alm_name"=>$get_almirah_name,
                    "shelf_no"=>$data['shelf'],
                    "name"=>$data['name'],
                    "edition"=>$data['edition'],
                    "price"=>$data['price'],
                     "date"=>date('Y-m-d')
                );
        $this->db->insert('item',$fields);
        }
        return true;
    }
//===============================View All Data=========================
public function all_items()
{
    $this->db->select("catagory.name as cat_name,");
    $this->db->from('catagory');
    $this->db->join('item','item.cat_id==catagory.id');
    $res=$this->db->get()->result_array();
    var_dump($res);die();
}

//======================Delete item==========================================
    public function del_item($id=0)
    {
        $data=array('is_delete' => 1 );
        $this->db->where('id', $id);    
        $this->db->update('item', $data);
        return true;
    }
//===========================Save Type===========================================
    public function save_type($data)
    {
        if($data['type_id']>0){
            $fields = array(
                    "name"=>$data['type_name'],
                );
            $this->db->where('id',$data['type_id']);
            $this->db->update('item_type',$fields);
        }else{
             $fields = array(
                    "name"=>$data['type_name'],
                );
        $this->db->insert('item_type',$fields);
        }
        return true;
    }
//=========================Show Type==========================================
    public function show_type()
    {
        $this->db->select("*");
        $this->db->from("item_type");
        $this->db->where("is_delete",0);
        $r=$this->db->get()->result_array();
        return $r;
    }
//======================Delete Type==========================================
    public function del_type($id=0)
    {
        $data=array('is_delete' => 1 );
        $this->db->where('id', $id);    
        $this->db->update('item_type', $data);
        return true;
    }
//===========================Save Publisher===========================================
    public function save_publisher($data)
    {
        if($data['publisher_id']>0){
            $fields = array(
                    "name"=>$data['publisher_name'],
                    "contact_no"=>$data['publisher_contact'],
                    "address"=>$data['publisher_address'],
                );
            $this->db->where('id',$data['publisher_id']);
            $this->db->update('publisher',$fields);
        }else{
             $fields = array(
                    "name"=>$data['publisher_name'],
                    "contact_no"=>$data['publisher_contact'],
                    "address"=>$data['publisher_address'],
                );
        $this->db->insert('publisher',$fields);
        }
        return true;
    }
//=========================Show Publisher=========================================
    public function show_publisher()
    {
        $this->db->select("*");
        $this->db->from("publisher");
        $this->db->where("is_delete",0);
        $r=$this->db->get()->result_array();
        return $r;
    }
//======================Delete Publisher==========================================
    public function del_publisher($id=0)
    {
        $data=array('is_delete' => 1 );
        $this->db->where('id', $id);    
        $this->db->update('publisher', $data);
        return true;
    }
//===========================Save Writer===========================================
	public function save_writer($data)
	{
        if($data['writer_id']>0){
           // var_dump($data);die();
            $fields = array('name' =>$data['writer_name'] ,'picture'=>$data['picture'] );
           
            $this->db->where('id',$data['writer_id']);
            return $this->db->update('writer',$fields);
        }else{
            	return $this->db->insert('writer',$data);
        }
	}
//======================Delete Writer==========================================
    public function del_writer($id=0)
    {
        $data=array('is_delete' => 1 );
        $this->db->where('id', $id);    
        $this->db->update('writer', $data);
        return true;
    }
//===========================Save Issue Items===========================================
    public function saveIssueItem($data)
    {
        $fields = array(
            "user_type"=>$data['user_type'],
            "user_id"=>$data['personId'],
            "grno"=>$data['GrNo'],
            "item_id"=>$data['ItemId'],
            "isbn"=>$data['isbn'],
            "issue_date"=>$data['issueDate'],
        );
        $ins=$this->db->insert('issue_items',$fields);
        if($ins){
            $this->db->where('id',$data['ItemId']);
            $data=array('status' => 1 );
            $this->db->update('item',$data);
            return true;
        }else{
            return false;
        }
        
    }
//===================================Update Returing status================================
public function returnitem($data)
    {
        $this->db->trans_begin();
        $itemid=$data['itemId'];
        $issueid=$data['issueId'];
        $today=date("Y-m-d");
            $fields = array(
                "return_date"=>$today,
                "total_days"=>$data['late_days'],
                "is_returned"=>1,
                "fine"=>$data['fine'],
            );
            $this->db->where('id',$issueid);
            $issue_item =$this->db->update('issue_items',$fields);
             $fields = array(
                    "status"=>0,
                );
             $this->db->where('id',$itemid);
             $issue_item =$this->db->update('item',$fields);
//=============================add record in Fine Transaction table=========================
             if($data['fine']>0){
                   $this->db->select("user_type,user_id,fine");
                   $this->db->from("fine_transaction");
                   $this->db->where('user_type',$data['user_type']);
                   $this->db->where('user_id',$data['user_id']);
                   $a=$this->db->get()->row_array();
                   if(count($a)==0){
                       $fields = array(
                            "user_type"=>$data['user_type'],
                            "user_id"=>$data['user_id'],
                            "fine"=>$data['fine'],
                        );
                        $this->db->insert('fine_transaction',$fields);
                   }else{
                       $newfine=$a['fine']+$data['fine'];
                        $fields = array(
                            "fine"=>$newfine,
                        );
                        $this->db->where('user_type',$data['user_type']);
                        $this->db->where('user_id',$data['user_id']);
                        $this->db->update('fine_transaction',$fields);
                   }
             }
            if ($this->db->trans_status() === FALSE)
                {
                        $this->db->trans_rollback();
                }else
                {
                        $this->db->trans_commit();
                }
    }
//=============================Fine View===================================
public function fineview()
    {
        $this->db->select('*');
        $this->db->from('fine_transaction');
        $this->db->where('fine >',0);
        $a=$this->db->get()->result_array();
        //var_dump($a);die();
        return $a;
    }
    
    public function fineview2()
    {
        $this->db->select('*');
        $this->db->from('fine_transaction');
       // $this->db->join('issue_items','issue_items.user_id = fine_transaction.user_id');
       // $this->db->join('item','item.isbn = issue_items.isbn');
        //$this->db->order_by('issue_items.id','desc');
        $this->db->where('fine_transaction.fine >',0);
        $a=$this->db->get()->result_array();
        //var_dump($a);die();
        return $a;
    }

public function voucherlist()
    {
        $this->db->select('*');
        $this->db->from('fine_transaction');
        $this->db->where('voucher',1);
        $a=$this->db->get()->result_array();
        //var_dump($a);die;
        return $a;
    }    
    
 
 public function getbookdetail($u_id,$u_type)
    {
        //var_dump($u_id);die;
        $this->db->select('issue_items.*,item.name');
        $this->db->from('issue_items');
        $this->db->join('item','item.isbn = issue_items.isbn');
        $this->db->where('issue_items.user_type',$u_type);
        $this->db->where('issue_items.user_id',$u_id);
        $this->db->order_by('issue_items.id','desc');
        $a=$this->db->get()->row_array();
        //var_dump($a);die;
        return $a;
    }    
    
//=================================View All Issued books=======================
public function issuebooks()
    {
        $this->db->select("*");
        $this->db->from("issue_items");
       // $this->db->where("is_returned",0);
        $ret=$this->db->get()->result_array();
        return $ret;
    }
    
public function misplace_record()
    {
        $this->db->select("*");
        $this->db->from("misplace");
        $this->db->where("is_delete",0);
        $ret=$this->db->get()->result_array();
        //var_dump($ret);die();
        
        return $ret;
    }
    
//=================================ajax showing books count with shelf=======================
public function getbookcount($shelf=0,$almirah=0)
    {
        $this->db->select("count(id)as total");
        $this->db->from('item');
        $this->db->where('shelf_no',$shelf);
        $this->db->where('almirah_no',$almirah);
        $this->db->where('is_delete',0);
        $rec=$this->db->get()->row_array()['total'];
        return $rec;
        
    }


//===================================Update Transactions Fine========================
/*public function updatetransaction($data)
    {
        $submitdate=date("Y-m-d");
        $finehistory=array(
            "user_type"=>$data['usertype'],
            "user_id"=>$data['userid'],
            "total_fine"=>$data['totalfine'],
            "fine_submitted"=>$data['pay_fine'],
            "fine_submit_date"=>$submitdate,
            );
        $ins=$this->db->insert('liberaryfinehistory',$finehistory);
        if($ins==true){
            $updatedfine=$data['totalfine']-$data['pay_fine'];
            $updatefinetransction = array(
            "fine"=>$updatedfine,
                        );
            $this->db->where('id',$data['FineTransactionId']);
            $this->db->update('fine_transaction',$updatefinetransction);
        }else{
            return false;
        }
        
        
    }*/
    

 /* public function create_voucher_list()
  {
      $user_id = $this->input->post('u_id');var_dump($user_id);die;
  }*/
  
   

   

}