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

class Company_model extends CI_Model {

	//Get suppliers_details
	public function get_details(){
		$data=$this->data;

		//Validate This suppliers already exist or not
		$company_id = $this->session->userdata('company_id') ?: 1;
		$query=$this->db->query("select * from db_company where id=$company_id");
		if($query->num_rows()==0){
			show_404();exit;
		}
		else{
			$query=$query->row();
			$data['q_id']=$query->id;
			$data['company_name']=$query->company_name;
			$data['website']=$query->company_website;
			$data['mobile']=$query->mobile;
			$data['phone']=$query->phone;
			$data['email']=$query->email;
			$data['country']=$query->country;
			$data['state']=$query->state;
			$data['city']=$query->city;
			$data['postcode']=$query->postcode;
			$data['address']=$query->address;
			$data['gstin']=$query->gst_no;
			$data['vat']=$query->vat_no;
			$data['website']=$query->website;
			$data['pan']=$query->pan_no;
			$data['bank_details']=$query->bank_details;
			$data['upi_id']=$query->upi_id;
			$data['company_logo']=(!empty($query->company_logo)) ? $query->company_logo : '';
			$data['upi_code']=(!empty($query->upi_code)) ? resolve_upload_url($query->upi_code, 'upi/') : base_url('theme/images/no_image2.png');
			

			return $data;
		}
	}
	public function update_company(){

		//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 uploader*/
		$logo_sql='';
		if(!empty($_FILES['company_logo']['name'])){
			$config['upload_path']          = tenant_upload_path('company/');
	        $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('company_logo'))
	        {
	                $error = array('error' => $this->upload->display_errors());
	                print($error['error']);
	                exit();
	        }
	        else
	        {
	        	   $logo_name=$this->upload->data('file_name');
	        		$logo_sql=" ,company_logo='".$this->db->escape_str($logo_name)."' ";
	        }
		}
		/*End*/
		/*UPI Code uploader*/
		$upi_sql='';
		if(!empty($_FILES['upi_code']['name'])){
			$config2['upload_path']          = tenant_upload_path('upi/');
	        $config2['allowed_types']        = 'gif|jpg|png|jpeg';
	        $config2['max_size']             = 2048;
	        $config2['max_width']            = 2048;
	        $config2['max_height']           = 2048;

	        $this->upload->initialize($config2);

	        if ( ! $this->upload->do_upload('upi_code'))
	        {
	                $error = array('error' => $this->upload->display_errors());
	                print($error['error']);
	                exit();
	        }
	        else
	        {
	        	   $upi_name=$this->upload->data('file_name');
	        		$upi_sql=" ,upi_code='".$this->db->escape_str($upi_name)."' ";
	        }
		}
		/*End*/
		$query1="update db_company set
		company_name='".$this->db->escape_str($company_name)."',
		mobile='".$this->db->escape_str($mobile)."',
		phone='".$this->db->escape_str($phone)."',
		email='".$this->db->escape_str($email)."',
		country='".$this->db->escape_str($country)."',
		state='".$this->db->escape_str($state)."',
		city='".$this->db->escape_str($city)."',
		postcode='".$this->db->escape_str($postcode)."',
		address='".$this->db->escape_str($address)."',
		gst_no='".$this->db->escape_str($gstin)."',
		vat_no='".$this->db->escape_str($vat)."',
		website='".$this->db->escape_str($website)."',
		pan_no='".$this->db->escape_str($pan)."',
		bank_details='".$this->db->escape_str($bank_details)."',
		upi_id='".$this->db->escape_str($upi_id)."'
		$logo_sql $upi_sql where id=$company_id";

		if ($this->db->simple_query($query1)){
		        return "success";
		}
		else{
		        return "failed";
		}
	}
	

}
