<?php
require_once ("secure_area.php");
class Forceupload extends Secure_area
{
	function __construct()
	{
		parent::__construct('forceupload');
	}

	function index()
	{
		$this->load->view("forceupload");
	}
	function createforceupload(){

		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){


			$api_data = array();
			$api_data['api_user'] = API_USER;
			$api_data['api_password'] = API_PASSWORD;
			$api_data['location_code'] =LOCATION_CODE;

			$this->db->trans_start();

			///START::do_regular_sync_customer_card_update//

			$sql = "SELECT id,state FROM pos_ospos_customers WHERE state !=''";
			$query = $this->db->query($sql);
			$people_data=array();
			foreach ($query->result() as $row) {
				$people_data[]=$row;
				$success =$this->db->query("UPDATE pos_ospos_customers SET state='' WHERE id='".$row->id."'");
			}

			$this->db->trans_complete();

			if(count($people_data)){
				$api_data['new_customer'] =$people_data;
			}

			///END::do_regular_sync_customer_card_update//


			///START::do_regular_sync_customer_add//

			$this->db->trans_start();
			///user sync//
			$this->db->from('people');
			$this->db->where(array('is_customer'=>1,'is_sync'=>0));
			$people_data_result= $this->db->get();

			$people_data=array();
			foreach($people_data_result->result_array() as $row)
			{
				$people_data[]=$row;
				$this->db->query("DELETE FROM ospos_people WHERE person_id='".$row['person_id']."'");
			}
			$this->db->trans_complete();

			if(count($people_data)){
				$api_data['new_customer_add'] =$people_data;
			}

			///END::do_regular_sync_customer_add//

			///START::do_regular_sync_local_sale//


			$has_sale_data=0;

			$this->db->trans_start();
			$this->db->set_dbprefix('ospos_');

			$this->db->from('sales');
			$this->db->where(array('is_sync'=>0));
			$ospos_sales_result= $this->db->get();

			$ospos_sales_data=array();
			$ospos_sales_items_data=array();
			$ospos_sales_payments_data=array();



			foreach($ospos_sales_result->result_array() as $row){
				$ospos_sales_data[$row['sale_id']]=$row;
				$this->db->from('sales_items');
				$this->db->where(array('sale_id'=>$row['sale_id']));
				$ospos_sales_items_result= $this->db->get();

				foreach($ospos_sales_items_result->result_array() as $item_row){
					$ospos_sales_items_data[$row['sale_id']][]=$item_row;
				}
				$this->db->from('sales_payments');
				$this->db->where(array('sale_id'=>$row['sale_id']));
				$ospos_sales_payments_result= $this->db->get();

				foreach($ospos_sales_payments_result->result_array() as $payment_row){
					$ospos_sales_payments_data[$row['sale_id']][]=$payment_row;
				}
				$has_sale_data=1;

				$update_query="UPDATE ospos_sales SET is_sync=1 WHERE receipt_no='".$row['receipt_no']."'";
				$this->db->query($update_query);
			}

			$this->db->trans_complete();

			if(count($ospos_sales_payments_data)){
				$api_data['ospos_sales'] =$ospos_sales_data;
				$api_data['ospos_sales_items'] =$ospos_sales_items_data;
				$api_data['ospos_sales_payments'] =$ospos_sales_payments_data;
			}

			///END::do_regular_sync_local_sale//


			if($has_sale_data==1){


				///START::do_per_slot_sync//

				$this->db->trans_start();
				$this->db->set_dbprefix('ospos_');
				$this->db->from('clr_locwise');
				$this->db->where('is_sync',0);
				$employee_data_result= $this->db->get();

				$employee_data=array();
				foreach($employee_data_result->result_array() as $row)
				{
					$employee_data[]=$row;
					$update_query_permission="UPDATE ospos_clr_locwise SET is_sync=1 WHERE id='".$row['id']."'";
					$this->db->query($update_query_permission);
				}
				$this->db->trans_complete();

				if(count($employee_data)){
					$api_data['ospos_clr_locwise'] =$employee_data;
				}
				///END::do_per_slot_sync//



				///START::do_per_slot_others_sync//

				$this->db->trans_start();
				$this->db->set_dbprefix('ospos_');
				$this->db->from('clr_locwise_others');
				$this->db->where('is_sync',0);
				$employee_data_result= $this->db->get();

				$employee_data=array();
				foreach($employee_data_result->result_array() as $row)
				{
					$employee_data[]=$row;
					$update_query_permission="UPDATE ospos_clr_locwise_others SET is_sync=1 WHERE id='".$row['id']."'";
					$this->db->query($update_query_permission);
				}
				$this->db->trans_complete();

				if(count($employee_data)){
					$api_data['ospos_clr_locwise_others'] =$employee_data;
				}
				///END::do_per_slot_others_sync//



				///START::do_permission_sync_customer//

				$this->db->trans_start();
				$this->db->set_dbprefix('ospos_');
				$this->db->from('permissions_relations_customer');
				$this->db->where('is_sync',0);
				$employee_data_result= $this->db->get();

				$employee_data=array();
				foreach($employee_data_result->result_array() as $row)
				{
					$employee_data[]=$row;
					$update_query_permission="UPDATE ospos_permissions_relations_customer SET is_sync=1 WHERE sales_id='".$row['sales_id']."'";
					$this->db->query($update_query_permission);
				}
				$this->db->trans_complete();

				if(count($employee_data)){
					$api_data['ospos_permissions_relations_customer'] =$employee_data;
				}
				///END::do_permission_sync_customer//

				$api_data_final = json_encode($api_data);

				//////////////////////////make a file//////
				$current_directory=getcwd();
				$file_name_with_date=date("Ymd_His", time())."_".LOCATION_CODE;
				$filename = $current_directory.'\database_nonsync_backup\\'.$file_name_with_date.".txt";


				if (!file_exists($filename)) {
					$fhandle = fopen($filename,"w");
					fwrite($fhandle,$api_data_final);
					fclose($fhandle);
				}

				$zip = new ZipArchive();
				$zip->open($current_directory.'\database_nonsync_backup\\'.$file_name_with_date.".zip", ZipArchive::CREATE);

				$zip->addFile($filename,basename($filename));
				//$zip->addFile($filename);
				$zip->close();
				unlink($filename);

				$data['message']= array('success'=>true,'message'=>'Non sync database has been created.'." File Name: ".$file_name_with_date.".zip");

			}else{
				$data['message']= array('success'=>false,'message'=>'All non sync data already uploaded in live server database.');
			}
		}
		$this->load->view("forceupload",$data);

	}

	function deletefile($OriginalFile){
		$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

		if(!$pageWasRefreshed){
			$OriginalFileRoot = "database_nonsync_backup";
			$DestinationRoot = "database_nonsync_deleted";
			$Date = date('d_m_Y');
			
			$FileExt = pathinfo($OriginalFileRoot."\\".$OriginalFile, PATHINFO_EXTENSION); // Get the file extension
			$Filename = basename($OriginalFile, ".".$FileExt); // Get the filename
			$DestinationFile = $DestinationRoot."\\".$Filename.'_dod_'.$Date.".".$FileExt; // Create the destination filename
			
			rename($OriginalFileRoot."\\".$OriginalFile, $DestinationFile); // rename the file


			if(!is_dir($DestinationRoot)){
				mkdir($DestinationRoot,0777,true);
			}
			rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));

			$data['message']= array('success'=>true,'message'=>"Non sync database has been deleted. "." File Name: ".$OriginalFile);
		}
		$this->load->view("forceupload",$data);

	}
}
?>