<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Barcodeprint extends CI_Controller {
	
    var $main_menu_name = "purchases";
	  var $sub_menu_name = "categories";

      function __construct() 
      {
        parent::__construct();
        $this->load->model('purchases_model');
        $this->load->model('common_model');
		$this->load->model('Sequerty_Model');
        $this->load->library('form_validation');
		$this->load->model('User_Model');
		$this->load->model('Sales_Model');
		$this->load->model('Supplier_Model');
		$this->load->model('product_models');
		$this->load->model('Warehouse_Model');
		$this->load->model('category_models');
		
		
      }

    public function index($e=0)
    {
		//if (!$this->User_Model->is_logged_in_k()) {
			//$this->User_Model->checkLoginPanel_k();
		//}
     $data['sales'] = $this->Sales_Model->get_all_sales();
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
      $this->load->view('barcodeprint',$data);
    }
	
	 public function generate_text_file()
    {
		$error=0;
		$disMsg='';
		$id=0;
		
		$this->load->helper('file');
		
		/* start text create */ 
		$row=$this->input->post('row');
		
		//print_r($row);
			$rowCount=$this->input->post('rowCount');
			$warehouse_id=$this->input->post('warehouse_id');
			
			$ware_des=$this->Warehouse_Model->get_warehouse_info($warehouse_id);
			
			$ware_name=$ware_des['name'];
			
			//echo "rowCount:$rowCount";
			$data_item=array();
			$text="Company,ProductCode,ProductName,Price";
			for($i=1; $i<=$rowCount; $i++){
				//echo "/ $rowCount , Test:".$row[$i]['product_id'][0];
				if(isset($row[$i]['product_id'][0]))
				{	
					//echo "NAWODDYA,100007,NOKIA - BACK COVER - 225,250";
					
					
					$product_id=$row[$i]['product_id'][0];
					 $prodcut_des = $this->product_models->get_product_by_id($product_id);
					//$text=$text.$row[$i]['product_name'][0].",".$row[$i]['product_name'][0].",".$row[$i]['unit_price'][0];
					//print_r( $prodcut_des);
					$qty=$row[$i]['qty'][0];
					//echo "qty:".$qty;
					for($k=0; $k<$qty; $k++){
						if($text) $text=$text."\r\n"; 
					
					$text=$text.$ware_name.",";
					$text=$text.$prodcut_des->product_code.",".$prodcut_des->product_name.",".number_format($row[$i]['unit_price'][0], 0, '.', '');
					}
				
				}
				
			}
			
			
		
	//echo $text;
		/* end text create */

	//$data = 'Some file data';
	
    if ( ! write_file('./doc/barcode.txt', $text))
    {
	
            $disMsg= 'Unable to write the file';
    }
    else
    {
            $disMsg= 'Barcode generated!';
    }
	
		 echo json_encode(array('id'=>$id,'error'=>"$error",'disMsg'=>$disMsg,));
	}
	
	
	function download(){
        $yourFile = "./doc/barcode.txt";
        $file = @fopen($yourFile, "rb");

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=barcode.txt');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($yourFile));
        while (!feof($file)) {
            print(@fread($file, 1024 * 8));
            ob_flush();
            flush();
        }
}
	
}