<?php 

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

class Site_model extends CI_Model {
    public function get_details(){
		$CI =& get_instance();
		$data=$CI->data;

		//Validate This suppliers already exist or not
		$query=$this->db->query("select * from db_sitesettings order by id asc limit 1");
		$company_id = $this->session->userdata('company_id') ?: 1;
		$query1=$this->db->query("select * from db_company where id=$company_id");
		if($query->num_rows()==0){
			show_404();exit;
		}
		else{
			/* QUERY 1*/
			$query=$query->row();
			$data['q_id']=$query->id;
            $data['site_name']=$query->site_name;
            $data['logo']=$query->logo;
            $data['currency_id']=$query->currency_id;			
            $data['currency_placement']=$query->currency_placement;			
            $data['language_id']=$query->language_id;			
            $data['timezone']=$query->timezone;			
            $data['date_format']=$query->date_format;			
            $data['time_format']=$query->time_format;			
            $data['sales_discount']=$query->sales_discount;/* Default sales discount */
            $data['change_return']=$query->change_return;
            $data['sales_invoice_format_id']=$query->sales_invoice_format_id;
            $data['sales_invoice_footer_text']=$query->sales_invoice_footer_text;
            $data['round_off']=$query->round_off;
            $data['disable_tax']=$query->disable_tax;
            $data['show_upi_code']=$query->show_upi_code;
            
            /* QUERY 2*/
			$query1=$query1->row();
            $data['category_init']=$query1->category_init;
            $data['item_init']=$query1->item_init;
            $data['supplier_init']=$query1->supplier_init;
            $data['purchase_init']=$query1->purchase_init;
            $data['purchase_return_init']=$query1->purchase_return_init;
            $data['customer_init']=$query1->customer_init;
            $data['sales_init']=$query1->sales_init;
            $data['sales_return_init']=$query1->sales_return_init;
            $data['expense_init']=$query1->expense_init;
            $data['sales_terms_and_conditions']=$query1->sales_terms_and_conditions;

			return $data;
		}
	}
	public function update_site(){
		//Filtering XSS and html escape from user inputs
		extract($this->security->xss_clean(html_escape(array_merge($this->data,$_POST))));

		$company_id = $this->session->userdata('company_id') ?: 1;

		$logo_sql='';
		if(!empty($_FILES['logo']['name'])){
			$config['upload_path']          = tenant_upload_path('');
	        $config['allowed_types']        = 'gif|jpg|png|jpeg';
	        $config['max_size']             = 2048;
	        $config['max_width']            = 2048;
	        $config['max_height']           = 2048;

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

	        if ( ! $this->upload->do_upload('logo'))
	        {
	                $error = array('error' => $this->upload->display_errors());
	                print($error['error']);
	                exit();
	        }
	        else
	        {
	        	   $logo_name=$this->upload->data('file_name');
	        		$logo_sql=" ,logo='".$this->db->escape_str($logo_name)."' ";
	        }
		}

		$change_return = (isset($change_return)) ? 1 : 0;
		$show_upi_code = (isset($show_upi_code)) ? 1 : 0;
		$round_off = (isset($round_off)) ? 1 : 0;
		$disable_tax = (isset($disable_tax)) ? 1 : 0;
		$q_id = intval($q_id);
		$sales_invoice_format_id = intval($sales_invoice_format_id);
		$sales_discount = floatval($sales_discount);

        $query1="update db_sitesettings set language_id='".$this->db->escape_str($language_id)."',
        site_name='".$this->db->escape_str($site_name)."',
        currency_placement='".$this->db->escape_str($currency_placement)."',
        currency_id='".$this->db->escape_str($currency)."',
        timezone='".$this->db->escape_str($timezone)."',
        date_format='".$this->db->escape_str($date_format)."',
        time_format='".$this->db->escape_str($time_format)."',
        sales_discount='$sales_discount',
        change_return=$change_return,
        show_upi_code=$show_upi_code,
        round_off=$round_off,
        disable_tax=$disable_tax,
        sales_invoice_format_id=$sales_invoice_format_id,
        sales_invoice_footer_text='".$this->db->escape_str($sales_invoice_footer_text)."'
         $logo_sql where id=$q_id";
        $query1= $this->db->simple_query($query1);

        $query2="update db_company set
        category_init='".$this->db->escape_str($category_init)."',
        item_init='".$this->db->escape_str($item_init)."',
        supplier_init='".$this->db->escape_str($supplier_init)."',
        purchase_init='".$this->db->escape_str($purchase_init)."',
        purchase_return_init='".$this->db->escape_str($purchase_return_init)."',
        customer_init='".$this->db->escape_str($customer_init)."',
        sales_init='".$this->db->escape_str($sales_init)."',
        sales_return_init='".$this->db->escape_str($sales_return_init)."',
        sales_terms_and_conditions='".$this->db->escape_str($sales_terms_and_conditions)."',
        expense_init='".$this->db->escape_str($expense_init)."' where id=$company_id";
        $query2= $this->db->simple_query($query2);
        $this->session->unset_userdata('currency');

		if ($query1 && $query2){
		    return "success";
		}
		else{
		    return "failed";
		}
	}
}

/* End of file Site_model.php */
