<?php 
//error_reporting(0);
class Invoice extends CI_Controller {

public function __construct(){

        parent::__construct();
    $this->load->model('page_model');
  			$this->load->helper('url');
  	 		$this->load->model('user_model');
        $this->load->library('session');
    $this->load->model('department_model');
    $this->load->helper(array('form', 'url'));
    $this->load->library('table');
    $this->load->library('pdf');

 $this->load->library('pagination');
$user_id=$this->session->userdata('user_id');
   if(!$user_id){
     redirect('user/login_view', 'refresh');
     }

}

public function index()
{
    

}
public function address_setting(){
    $select= $this->input->post('select');
    $exits=$this->department_model->address_list($select);
     $data =array();
    $list=$this->department_model->buisness_list();
     $invoice_create=$this->department_model->invoice_create();
/*       $error['error'] ='Wrong Password';$error['invoice_create']=$invoice_create;
    $error['buisness_list']=$list;*/
    $nom= $this->input->post('nom');
    $address= $this->input->post('address');
    $rcbs=$this->input->post('rcbs');
     $pass= $this->input->post('adminpass');
    $data = array(
     'nom'=>$nom,
    'address' => $address,
    'buisness_id'=>$select,
    'rcsb'=>$rcbs);
    
    if($pass=='@admin##' and !$exits){
        $this->db->insert('address_setting',$data);
        
    }
    if($exits and $pass=='@admin##'){
        $this -> db -> where('buisness_id', $select);
        $this -> db -> update('address_setting',$data);
    }
      redirect('invoice/create', 'refresh');
    
}
public function create(){
    
  //$invoice_id=  $this->department_model->invoice();
    //$data['invoice_id']=$invoice_id;
    $b_type= $this->department_model->get_buisness_type();
    $data['buisness_list']=$this->department_model->buisness_list();
     $data['address_list']=$this->department_model->address_list($b_type);
    $data['buisness']=$b_type;
   // print_r($data['buisness_list']);
    $data['invoice_create']=$this->department_model->invoice_create();
    $this->load->view('invoice.php',$data);
     }
    public function edit(){
    $id=$this->uri->segment(3);
     $data['editinvoice']=  $this->department_model->get_edit_invoice($id);
    $this->load->view('editinvoice.php',$data);
     }
    public function value(){
        $config['upload_path']          = './invoice-uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 10000000;
                $config['max_width']            = 102400000;
                $config['max_height']           = 768000000000;

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload('file'))
                {
                      $filename='index.jpg';  // $error = array('error' => $this->upload->display_errors());

                        //$this->load->view('upload_form', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());
                    
                    $filename=$_FILES['file']['name'];
            $logoname = str_replace(' ', '_', $filename);
                }
      
        
      
       
    $typ= $this->input->post('type');
    $copy= $this->input->post('copy');
       
   if($typ){
       $type=($typ);
   }
        else{
        $type='No Type';
        }
       $taxunit=$this->input->post('taxunit');
        $from= $this->input->post('invoice[from]');
        $c_name=$this->input->post('invoice[company]');
        $fc_name=$this->input->post('invoice[fromcompany]');
        $billto= $this->input->post('invoice[billing]');
         $edit_id= $this->input->post('edit_id');
         $invoice_number= $this->input->post('invoice[number]');
        $date= $this->input->post('date');
        $condition= $this->input->post('invoice[footer_1]');
          $total= $this->input->post('total');
        $subtotal= $this->input->post('subtotal');
        $invoice_name=$this->input->post('invoice_name');
       $b_type= $this->department_model->get_buisness_type();
      
         $item= $this->input->post('invoice_item');
        $taxprice= serialize($this->input->post('taxprice'));
         $unittax= serialize($this->input->post('taxrte'));
        $item=serialize($item);
        $item=unserialize($item);
      //print_r($item);exit;
      //$count= count($item);  
       $i=0;
        foreach ($item  as $key => $itemval) {
            if(isset($itemval['amount'])){
            $qty[$i]=isset($itemval['qty'])?$itemval['qty'] :0;
            $des[$i]=isset($itemval['description'])?$itemval['description'] :0;
            $unit[$i]=isset($itemval['unitprice'])?$itemval['unitprice'] :0;
            $amount[$i]=isset($itemval['amount'])?$itemval['amount'] :0;
            $tax[$i]=isset($itemval['taxrate'])?$itemval['taxrate'] :0;
                $i++;
            }
       
    }
         $qty=serialize($qty);
        $des=serialize($des);
         $unit=serialize($unit);
       $amount=serialize($amount);
        $tax=serialize($tax);
     $datainvoice = array(
 'buisness_type'=>$b_type,
    'invoice_date' => $date,
    'invoice_id' => $invoice_number,
   'bill_from' => $from,
   'from_company'=>$fc_name,
    'bill_to' => $billto,
   'total' => $total,
    'pay_type'=>$type,
    'image'=>$filename,
    'sub_total' => $subtotal,
     'company_name'=>$c_name,
    'invoice_qty_info'=>$qty,
     'invoice_unit_info'=>$unit,
     'invoice_des_info'=>$des,
     'invoice_amount_info'=>$amount,
     'invoice_tax_info'=>$tax,
         'unit_invoice_tax_info'=>$unittax,
          'invoice_tax_price'=>$taxprice,
         'condition'=>$condition,
         'invoice_name'=>$invoice_name,
         'custom_unit'=>$taxunit,
     'status' => 1,
          
  );  
     
 if($edit_id and !$copy){
    
      $this -> db -> where('id', $edit_id);
        $this -> db -> update('invoice',$datainvoice);
     
     redirect('invoice/pdfview/'.$edit_id.'', 'refresh');
     
 }
        else{
        
          $this->db->insert('invoice',$datainvoice);
             $invoice_id=  $this->department_model->invoice();  
             redirect('invoice/pdfview/'.$invoice_id.'', 'refresh');
            
        }
    
        
     
     }
     public function pdfview(){
          $this->load->library('table');
        
		
             
			$invoice_id = $this->uri->segment(3);
			//if(!$invoice_id){
            // $invoice_id= $pdfid;  
            //}
			   $html= $this->department_model->fetch_single_details($invoice_id);
             //$data['query']=$query;
             //$data['invoice_id']=$invoice_id;
            //$pdfview=$this->load->view('invoicepdf',$data);
            //$customPaper = array(0,0,2480,3508);
             $a4='A4';
            $this->pdf->set_paper($a4);
			$this->pdf->loadHtml($html);
			$this->pdf->render();
			$this->pdf->stream("".$invoice_id.".pdf", array("Attachment"=>0));
		
     }
    public function delete(){
        $id_post = $this->input->post('uristring'); 
      if($id_post){
          $this->db->set('status',0,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('invoice');
        
          
       
            
      }
             
    }
     public function perdelete(){
        $id_post = $this->input->post('uristring'); 
      if($id_post){
         
        $this -> db -> where('id', $id_post);
        $this -> db -> delete('invoice');
        
          echo json_encode($id_post);
       
            
      }
             
    }
     public function recover(){
        $id_post = $this->input->post('uristring'); 
      if($id_post){
          $this->db->set('status',1,false);
        $this -> db -> where('id', $id_post);
        $this -> db -> update('invoice');
        
          
       
            
      }
             
    }
    public function trash(){
                $perPage = 30;  
        $getTotaltransfer = $this->page_model->getTotaltrashinvoice();
		
		 $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
					
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'balance/transfer';
		$config['total_rows'] = $getTotaltransfer;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = true;
		
		
		//Pagination Container tag
		$config['full_tag_open'] = '<div style="margin:50px 30%;">';
		$config['full_tag_close'] = '</div>';
		
		//First and last Link Text
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		
		//First tag 
		$config['first_tag_open'] = '<span class="pagination-first-tag">';
		$config['first_tag_close'] = '</span>';
		
		//Last tag 
		$config['last_tag_open'] = '<span class="pagination-last-tag">';
		$config['last_tag_close'] = '</span>';
		
		
		//Next and Prev Link
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		
		
		//Next and Prev Link Styling
		$config['next_tag_open'] = '<span class="pagination-next-tag">';
		$config['next_tag_close'] = '</span>';
		
		$config['prev_tag_open'] = '<span class="pagination-prev-tag">';
		$config['prev_tag_close'] = '</span>';
		
		
		//Current page tag
		$config['cur_tag_open'] = '<strong class="pagination-current-tag">';
		$config['cur_tag_close'] = '</strong>';

		
		$config['num_tag_open'] = '<span class="pagination-number">';
		$config['num_tag_close'] = '</span>';

		$this->pagination->initialize($config);
	

         $invoice=  $this->department_model->get_trashinvoice();
                  
           $data['pagination'] = $this->pagination->create_links();
    $data['invoice_detail']=$invoice;
         
         
        $this->load->view('trash_invoice.php',$data);
    }
     public function details(){
  
          
          $perPage = 30;  
        $getTotaltransfer = $this->page_model->getTotalinvoice();
		
		 $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
					
		//$data["page_results"] = $this->page_model->getCustomersByLimit($perPage, $page*$perPage);
		
		
		
		//Basic configuration 
		    $config['base_url'] = base_url().'invoice/details';
		$config['total_rows'] = $getTotaltransfer;
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 3;
		$config['use_page_numbers'] = true;
		
		
		//Pagination Container tag
		$config['full_tag_open'] = '<div style="margin:50px 30%;">';
		$config['full_tag_close'] = '</div>';
		
		//First and last Link Text
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		
		//First tag 
		$config['first_tag_open'] = '<span class="pagination-first-tag">';
		$config['first_tag_close'] = '</span>';
		
		//Last tag 
		$config['last_tag_open'] = '<span class="pagination-last-tag">';
		$config['last_tag_close'] = '</span>';
		
		
		//Next and Prev Link
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		
		
		//Next and Prev Link Styling
		$config['next_tag_open'] = '<span class="pagination-next-tag">';
		$config['next_tag_close'] = '</span>';
		
		$config['prev_tag_open'] = '<span class="pagination-prev-tag">';
		$config['prev_tag_close'] = '</span>';
		
		
		//Current page tag
		$config['cur_tag_open'] = '<strong class="pagination-current-tag">';
		$config['cur_tag_close'] = '</strong>';

		
		$config['num_tag_open'] = '<span class="pagination-number">';
		$config['num_tag_close'] = '</span>';

		$this->pagination->initialize($config);
	

         $invoice=  $this->department_model->get_invoice();
                  
           $data['pagination'] = $this->pagination->create_links();
    $data['invoice_detail']=$invoice;
         
         
        $this->load->view('all_invoice.php',$data);
     }
}
    ?>