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

class Store_profile_model extends CI_Model {

	public function update_store(){

		//Filtering XSS and html escape from user inputs 
		extract($this->security->xss_clean(html_escape(array_merge($this->data,$_POST,$_GET))));
		 	//echo "<pre>";print_r($this->security->xss_clean(html_escape(array_merge($this->data,$_POST))));exit();

		//if not admin
		if(!is_admin()){
			if($q_id!=get_current_store_id()){
				echo "Something Went Wrong";exit();
			}
		}

		// Check if currency is being changed and if there are existing transactions
		$current_store = $this->db->query("SELECT currency_id FROM db_store WHERE id = $q_id")->row();
		if ($current_store && $current_store->currency_id != $currency) {
			// Currency is being changed, check for existing transactions
			if (has_existing_transactions($q_id)) {
				// Return JSON response for toast notification
				header('Content-Type: application/json');
				echo json_encode([
					'status' => 'error',
					'message' => 'Cannot change base currency! There are existing transactions in the system. Please delete all transactions first before changing the base currency.',
					'toast_type' => 'error',
					'toast_title' => 'Currency Change Blocked',
					'toast_message' => 'Cannot change base currency when there are existing transactions. Please delete all transactions first.'
				]);
				exit();
			}
		}

		$this->db->trans_begin();
		
		$store_logo='';
		if(!empty($_FILES['store_logo']['name'])){
			$config['upload_path']          = './uploads/store/';
	        $config['allowed_types']        = 'gif|jpg|jpeg|png';
	        $config['max_size']             = 1000;
	        $config['max_width']            = 1000;
	        $config['max_height']           = 1000;

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

	        if ( ! $this->upload->do_upload('store_logo'))
	        {
	                $error = array('error' => $this->upload->display_errors());
	                return $error['error'];
	                exit();
	        }
	        else
	        {
	        	   $store_logo='uploads/store/'.$this->upload->data('file_name');
	        }
		}

		$signature='';
		if(!empty($_FILES['signature']['name'])){
			$config['upload_path']          = './uploads/signature/';
	        $config['allowed_types']        = 'gif|jpg|jpeg|png';
	        $config['max_size']             = 1000;
	        $config['max_width']            = 1000;
	        $config['max_height']           = 1000;

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

	        if ( ! $this->upload->do_upload('signature'))
	        {
	                $error = array('error' => $this->upload->display_errors());
	                return $error['error'];
	                exit();
	        }
	        else
	        {
	        	   $signature='uploads/signature/'.$this->upload->data('file_name');
	        }
		}


		$change_return = (isset($change_return)) ? 1 : 0;
		$mrp_column = (isset($mrp_column)) ? 1 : 0;
		$show_signature = (isset($show_signature)) ? 1 : 0;
		$previous_balance_bit = (isset($previous_balance_bit)) ? 1 : 0;
		$round_off = (isset($round_off)) ? 1 : 0;
		$enable_wholesale_pricing = (isset($enable_wholesale_pricing)) ? 1 : 0;
		
		// Handle checkbox values - explicitly set to 0 if not in POST (unchecked)
		// Checkboxes only send values when checked, so we need to explicitly handle unchecked state
		// Cast to integer to ensure proper database storage
		$show_logo_on_pos_invoice = (isset($show_logo_on_pos_invoice) && $show_logo_on_pos_invoice != '' && $show_logo_on_pos_invoice != '0') ? (int)1 : (int)0;
		$show_logo_on_invoice = (isset($show_logo_on_invoice) && $show_logo_on_invoice != '' && $show_logo_on_invoice != '0') ? (int)1 : (int)0;
		$show_logo_on_reports = (isset($show_logo_on_reports) && $show_logo_on_reports != '' && $show_logo_on_reports != '0') ? (int)1 : (int)0;
		
		// Check if the columns exist before trying to update them
		$column_exists_pos = $this->db->query("SHOW COLUMNS FROM `db_store` LIKE 'show_logo_on_pos_invoice'")->num_rows() > 0;
		$column_exists_invoice = $this->db->query("SHOW COLUMNS FROM `db_store` LIKE 'show_logo_on_invoice'")->num_rows() > 0;
		$column_exists_reports = $this->db->query("SHOW COLUMNS FROM `db_store` LIKE 'show_logo_on_reports'")->num_rows() > 0;

		

		$data = array(
		    				'store_code'				=> $store_code,
		    				'store_name'				=> $store_name,
		    				'store_website'				=> $store_website,
		    				'mobile'					=> $mobile,
		    				'phone'						=> $phone,
		    				'email'						=> $email,
		    				'country'					=> $country,
		    				'state'						=> $state,
		    				'city'						=> $city,
		    				'address'					=> $address,
		    				'postcode'					=> $postcode,
		    				'bank_details'				=> $bank_details,
		    				'category_init'				=> $category_init,
		    				'item_init'					=> $item_init,
		    				'supplier_init'				=> $supplier_init,
		    				'purchase_init'				=> $purchase_init,
		    				'purchase_return_init'		=> $purchase_return_init,
		    				'customer_init'				=> $customer_init,
		    				'sales_init'				=> $sales_init,
		    				'sales_return_init'			=> $sales_return_init,
		    				'expense_init'				=> $expense_init,
		    				'quotation_init'			=> $quotation_init,
		    				'money_transfer_init'		=> $money_transfer_init,
		    				'accounts_init'				=> $accounts_init,
		    				'currency_id'				=> $currency,
		    				'currency_placement'		=> $currency_placement,
		    				'timezone'					=> $timezone,
		    				'date_format'				=> $date_format,
		    				'time_format'				=> $time_format,
		    				'sales_discount'			=> $sales_discount,
		    				'sales_discount'			=> $sales_discount,
		    				'change_return'				=> $change_return,
		    				'sales_invoice_format_id'	=> $sales_invoice_format_id,
		    				'pos_invoice_format_id'		=> $pos_invoice_format_id,
		    				'sales_invoice_footer_text'	=> $sales_invoice_footer_text,
		    				'invoice_terms'				=> $invoice_terms,
		    				'round_off'					=> $round_off,
		    				'language_id'				=> $language_id,
		    				'decimals'					=> $decimals,
		    				'qty_decimals'					=> $qty_decimals,
		    				'inactivity_timeout'		=> (isset($inactivity_timeout) && !empty($inactivity_timeout)) ? (intval($inactivity_timeout) * 60) : 600, // Convert minutes to seconds, default 600 (10 minutes)
		    				'sales_payment_init'		=> $sales_payment_init,
		    				'sales_return_payment_init'	=> $sales_return_payment_init,
		    				'purchase_payment_init'		=> $purchase_payment_init,
		    				'purchase_return_payment_init'	=> $purchase_return_payment_init,
		    				'expense_payment_init'	=> $expense_payment_init,
		    				'cust_advance_init'	=> $cust_advance_init,
		    				'mrp_column'	=> $mrp_column,
		    				'show_signature'	=> $show_signature,
		    				'previous_balance_bit'	=> $previous_balance_bit,
		    				't_and_c_status'	=> $t_and_c_status,
		    				't_and_c_status_pos'	=> $t_and_c_status_pos,
		    				'number_to_words'	=> $number_to_words,
		    				'default_account_id'	=> (isset($default_account_id) && !empty($default_account_id))?$default_account_id:null,
		    				'default_price_type'	=> (isset($default_price_type)) ? $default_price_type : 'retail',
		    				'enable_wholesale_pricing'	=> $enable_wholesale_pricing,
		    			);
		    			
		    			// Only include logo display fields if columns exist
		    			if($column_exists_pos) {
		    				$data['show_logo_on_pos_invoice'] = $show_logo_on_pos_invoice;
		    			}
		    			if($column_exists_invoice) {
		    				$data['show_logo_on_invoice'] = $show_logo_on_invoice;
		    			}
		    			if($column_exists_reports) {
		    				$data['show_logo_on_reports'] = $show_logo_on_reports;
		    			}

		if(!empty($store_logo)){
			$data['store_logo']=$store_logo;
		}
		if(!empty($signature)){
			$data['signature']=$signature;
		}
		/*custom helper*/
		if(gst_number()){
			$data['gst_no']=$gst_no;
		}
		if(vat_number()){
			$data['vat_no']=$vat_no;
		}
		if(pan_number()){
			$data['pan_no']=$pan_no;
		}
		/*end*/

		
			$store_code_count=$this->db->query("select count(*) as store_code_count from db_store where upper(store_code)=upper('$store_code') and id<>$q_id")->row()->store_code_count;
			if($store_code_count>0){
				echo "Sorry! Store Code Already Exist!\nPlease Change Store Code";exit();
			}

			$q1 = $this->db->where('id',$q_id)->update('db_store', $data);
			if($q1){
				$this->db->trans_commit();
				$this->session->unset_userdata('currency');
				//$this->session->set_flashdata('success', 'Success!! Record Updated Successfully! ');
				echo "success";
			}

		

		exit();
	}

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

		$query1=$this->db->query("select * from db_store where upper(id)=upper('$id')");
		if($query1->num_rows()==0){
			show_404();exit;
		}
		else{
			/* QUERY 1*/
			$data['q_id']=$query1->row()->id;
			return array_merge($data,$query1->row_array());
			return $data;
		}
	}

}
