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

class Sales_reports extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
		$this->load->model('user_model');
		$this->load->model('master_model');
		$this->load->model('Sales_model');
		
		if(!isset($_SESSION['user_logged'])) {
			$this->session->set_flashdata("error", "please login first to view dashboard");
			redirect("Login/login");
		}
		$this->db1 = $this->load->database('otherdb', TRUE);
	}	
	// Advance Orders Reports For Chef
	public function Advance_orders()
	{
	    $user_id = $_SESSION['user_id'];
		$data['side_bar_data'] = $this->user_model->get_SideBarData($user_id);
		$data['Advance_details'] = $this->Sales_model->get_advance_ordersdetails();
		$this->load->view('Sales/advance_list',$data);
	}
	
	// advance Receipt
	public function viewadvancereceipt($bid,$branch)
	{
		$data['invoice_details'] = $this->Sales_model->get_advance_billproductdetail_toedit($bid,$branch);
		$data['total_amt'] = $this->Sales_model->get_total_amt($bid,$branch);
		$data['invoice_tax'] = $this->Sales_model->get_invoice_tax($bid,$branch);
		$data['paymode'] = $this->Sales_model->get_payment_mode($bid,$branch);
		$data['branch_info'] = $this->Sales_model->get_outlets_information($branch);
		$data['points_info'] = $this->Sales_model->get_points_information($branch);
		$data['points_logo'] = $this->Sales_model->get_points_information_logo($branch);
		$data['points_branchlist'] = $this->Sales_model->get_points_information_branch_list($branch);
		$data['points_social_list'] = $this->Sales_model->get_points_information_social_list($branch);
		$data['chef_list'] = $this->Sales_model->get_advance_cheflist();
	//	$data['receipt_view'] = $this->Sales_model->get_advance_updatedata();
		$user_id = $_SESSION['user_id'];
		$data['side_bar_data'] = $this->user_model->get_SideBarData($user_id);
		$data['pageTitle'] = 'Advance Receipt View';
		$data['pageId'] = '2';
		$data['Pagename'] = 'Advance Receipt View';
		$data['HomePage'] = 'Home';
		$this->load->view("Sales/advance_receipt", $data);
	}
	
	// change the status 
	public function change_status()
	{
	    $bil_id = $this->input->post('bill_id');
	    $branch = $this->input->post('branch');
	    $food_status = $this->input->post('food_status');
	    $assigned = $this->input->post('assigned');
	    $image_capture = $this->input->post('image_capture');
	    $weight = $this->input->post('weight');
	    $query1=$this->db1->query("SELECT branch_slno,branch FROM outlet_deatils WHERE status='Active' and  branch='$branch'");
		$total_branch = $query1->result_array();
		foreach ($total_branch as $brans) 
		{
			$branch_code = $brans['branch_slno'];
			$branches = $brans['branch'];
			if ($branch == $branches)
			{
    		    $table = 'product_billdetail'.$branch_code;
    		    if(!empty($_FILES['image_capture']['name'])){
    			$config['upload_path'] = 'assets/advanceorder_photo/';
    			$config['allowed_types'] = 'gif|jpg|png|jpeg';
    			$config['max_size'] = '200000';
    			$config['remove_spaces'] = true;
    			$config['overwrite'] = false;
    			$config['max_width'] = '';
    			$config['max_height'] = '';
    			$fname = $_FILES['image_capture']['name'];
    			$config['file_name'] = $fname;
    			$this->load->library('upload', $config);
    			$this->upload->initialize($config);
    			if($this->upload->do_upload('image_capture')){
    				$uploadData = $this->upload->data();
    				$html_file = $uploadData['file_name'];
    				}else{
    				$html_file = '';
    			}
        		}else{
        			$html_file = '';
        		}
        		
        		//echo $html_file."imagepath";
		
        	    $this->db1->query("UPDATE $table SET food_ready='$food_status', prepared_by='$assigned',capture_image='$html_file',weight='$weight' where bill_id='$bil_id'");
        	    $ids= $this->db->affected_rows();
                if($ids>0) 
                {
                    redirect('Sales_reports/Advance_orders/');  
                } else {
                    redirect('Sales_reports/Advance_orders/');  
                }
			}
		}
	}
	
}