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

class Dataupdate extends CI_Controller {

	function __construct()
    {
        parent::__construct();
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
    }

	function updtdblocal2server()
	{
			$response = 0;
			$post_fields = array();
			
			#check var
			$lastupdatedon = $this->input->post("lastupdatedon");
			$clientid = $this->input->post("clientid");
			$userid = $this->input->post("userid");
			$outletid = $this->input->post("outletid");	
			
			## tables		
			$users = $this->input->post("users");
			$customers = $this->input->post("customers");
			$dayoff = $this->input->post("dayoff");
			$billing = $this->input->post("billing");
			$billinginfo= $this->input->post("billinginfo");
			$post_fields['users'] = 0;
			$post_fields['customers'] = 0;
			$post_fields['dayoff'] = 0;
			$post_fields['billing'] = 0;
			$post_fields['billinginfo'] = 0;
			if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
			{				
				if(is_array($users)>0)
				{
					$index = 1;
					foreach($users as $userrow)
					{					    
						$data = array(						
							"fld_name" => $userrow['fld_name'],
							"fld_email" => $userrow['fld_email'],
							"fld_mobile" => $userrow['fld_mobile'],
							"fld_userpass" => $userrow['fld_userpass'],
							"fld_updatedby" => $userrow['fld_updatedby'],
							"fld_updatedon" => $userrow['fld_updatedon'],
							"fld_lastlogin" => $userrow['fld_lastlogin'],
							"fld_lastloginip" => $userrow['fld_lastloginip']
						);
						$this->db->select("*");						
						$this->db->where("fld_id",$userrow['fld_id']);
						$results = $this->db->get('tbl_users');						
						if($results->num_rows()>0)
						{	
						   
							$this->db->where("fld_id",$userrow['fld_id']);
							$this->db->update('tbl_users', $data);	
							$response++;
							$post_fields['users'] = $index;
							$index++;
						}

					}
				}
				
				if(is_array($dayoff)>0)
				{	
					$index = 1;
					foreach($dayoff as $dayoffrow)
					{
						$this->db->select("*");		
						$this->db->where("fld_datetime='".$dayoffrow['fld_datetime']."'");
						$this->db->where("fld_clientid",$clientid);
						$this->db->where("fld_outletid",$outletid);
						$this->db->where("fld_userid",$dayoffrow['fld_userid']);
						$results = $this->db->get('tbl_dayoff');
						//echo $this->db->last_query();	exit;
						if($results->num_rows()>0)
						{
							$post_fields['dayoff'] = $index;
							
						}
						else
						{
							$data = array(						
								"fld_outletid" => $dayoffrow['fld_outletid'],
								"fld_clientid" => $dayoffrow['fld_clientid'],
								"fld_userid" => $dayoffrow['fld_userid'],
								"fld_dayoffdate" => $dayoffrow['fld_dayoffdate'],
								"fld_amount" => $dayoffrow['fld_amount'],
								"fld_cash" => $dayoffrow['fld_cash'],														
								"fld_cc" => $dayoffrow['fld_cc'],														
								"fld_upi" => $dayoffrow['fld_upi'],														
								"fld_cash" => $dayoffrow['fld_cash'],
								"fld_nameaccount" => $dayoffrow['fld_nameaccount'],
								"fld_naremarks" => $dayoffrow['fld_naremarks'],					
								"fld_lastbillid" => $dayoffrow['fld_lastbillid'],
								"fld_datetime" => $dayoffrow['fld_datetime'],											
								"fld_del" => $dayoffrow['fld_del'],												
								"fld_updatedby" => $dayoffrow['fld_updatedby'],
								"fld_updatedon" => $dayoffrow['fld_updatedon']
							);
							$this->db->insert('tbl_dayoff', $data);
							$response++;
							$post_fields['dayoff'] = $index;
							
						}
						$index++;
					}
				}
				
				if(is_array($customers)>0)
				{
					$index = 1;
					foreach($customers as $customerrow)
					{
						$data = array(						
							"fld_name" => $customerrow['fld_name'],
							"fld_email" => $customerrow['fld_email'],
							"fld_discounttype" => $customerrow['fld_discounttype'],
							"fld_discount" => $customerrow['fld_discount'],
							"fld_givenname" => $customerrow['fld_givenname'],
							"fld_givenemail" => $customerrow['fld_givenemail'],
							"fld_givenmobile" => $customerrow['fld_givenmobile'],
							"fld_status" => $customerrow['fld_status'],						
							"fld_del" => $customerrow['fld_del'],
							"fld_updatedby" => $customerrow['fld_updatedby'],
							"fld_updatedon" => $customerrow['fld_updatedon']
						);
						$this->db->select("*");		
						$this->db->where("fld_mobile",$customerrow['fld_mobile']);
						$results = $this->db->get('tbl_customers');
						
						if($results->num_rows()>0)
						{							
							$this->db->where("fld_mobile", $customerrow['fld_mobile']);
							$this->db->update('tbl_customers', $data);	
							
						}
						else
						{
							$data["fld_clientid"] =$customerrow['fld_clientid'];
							$data["fld_outletid"] =$customerrow['fld_outletid'];
							$data["fld_userid"] =$customerrow['fld_userid'];
							$data["fld_mobile"] =$customerrow['fld_mobile'];
							$data["fld_datetime"] =$customerrow['fld_datetime'];
							$this->db->insert('tbl_customers', $data);
							$response++;
							
						}
						$post_fields['customers'] = $index;
						$index++;
					}
				}
				

				
				if(is_array($billing)>0)
				{
					$index = 1;
					$indexinfo = 1;
					foreach($billing as $billrow)
					{

						$data = array(
						"fld_mobile" => $billrow['fld_mobile'],
						"fld_name" => $billrow['fld_name'],
						"fld_otp" => $billrow['fld_otp'],
						"fld_specialdiscount" => $billrow['fld_specialdiscount'],
						"fld_givenname" => $billrow['fld_givenname'],
						"fld_givenemail" => $billrow['fld_givenemail'],
						"fld_givenmobile" => $billrow['fld_givenmobile'],
						"fld_discounttype" => $billrow['fld_discounttype'],
						"fld_discount" => $billrow['fld_discount'],
						"fld_amount" => $billrow['fld_amount'],
						"fld_netamount" => $billrow['fld_netamount'],
						"fld_giftid" => $billrow['fld_giftid'],
						"fld_gprodid" => $billrow['fld_gprodid'],
						"fld_gqty" => $billrow['fld_gqty'],
						"fld_status" => $billrow['fld_status'],
						"fld_remarks" => $billrow['fld_remarks'],
						"fld_cancellation_reason" => $billrow['fld_cancellation_reason'],
						"fld_cancellation_remarks" => $billrow['fld_cancellation_remarks'],
						"fld_cancellationdate" => $billrow['fld_cancellationdate'],
						"fld_cancelrequestby" => $billrow['fld_cancelrequestby'],						
						"fld_del" => $billrow['fld_del'],
						"fld_updatedby" => $billrow['fld_updatedby'],
						"fld_updatedon" => $billrow['fld_updatedon']
						);
						$billingid = 0;
						$this->db->select("*");		
						$this->db->where("fld_billno", $billrow['fld_billno']);
						$this->db->where("fld_clientid",$clientid);
						$this->db->where("fld_outletid",$outletid);
						$results = $this->db->get('tbl_billing');
						//echo $this->db->last_query();	exit;
						if($results->num_rows()>0)
						{
							$row  = $results->row();
							
							$this->db->where("fld_billno", $billrow['fld_billno']);
							$this->db->where("fld_clientid",$clientid);
							$this->db->where("fld_outletid",$outletid);
							$this->db->update('tbl_billing', $data);
							
							$billingid = $row->fld_id;
							
							
						}
						else
						{
							$data["fld_clientid"] =$billrow['fld_clientid'];						
							$data["fld_outletid"] =$billrow['fld_outletid'];						
							$data["fld_userid"] =$billrow['fld_userid'];						
							$data["fld_customerid"] =$billrow['fld_customerid'];						
							$data["fld_billno"] =$billrow['fld_billno'];						
							$data["fld_billdate"] =$billrow['fld_billdate'];						
							$data["fld_datetime"] =$billrow['fld_datetime'];						
							$this->db->insert('tbl_billing', $data);
							$billingid = $this->db->insert_id();

							
							
						}
						$response++;
						$post_fields['billing'] = $index;
						$index++;

						## info rows
						if($billingid>0)
						{	
							
							foreach($billinginfo[$billrow['fld_billno']] as $billinforow)
							{
								$this->db->select("fld_id");
								$this->db->where("fld_billingid", $billingid);					
								$this->db->where("fld_productid", $billinforow['fld_productid']);
								$results2 = $this->db->get('tbl_billinginfo');
								if($results2->num_rows()>0) // update if found
								{
									$row2  = $results2->row();
									$data2 = array(						
									"fld_qty" => $billinforow['fld_qty'],
									"fld_price" => $billinforow['fld_price'],
									"fld_mrp" => $billinforow['fld_mrp']
									);
									$this->db->where("fld_id", $row2->fld_id);
									$this->db->update('tbl_billinginfo', $data2);
									$infodbupdtids[] = $row2->fld_id;
								}
								else
								{
									$data2 = array(						
									"fld_billingid" => $billingid,
									"fld_productid" => $billinforow['fld_productid'],
									"fld_qty" => $billinforow['fld_qty'],
									"fld_price" => $billinforow['fld_price'],
									"fld_mrp" => $billinforow['fld_mrp']
									);

									$this->db->insert('tbl_billinginfo', $data2);								
									$infodbupdtids[] = $this->db->insert_id();
								}

							}
							$this->db->select("*");
							$this->db->where("fld_billingid",$billingid);
							$results_del = $this->db->get('tbl_billinginfo');							
							if($results_del->num_rows()>0) // already exists
							{
								foreach($results_del->result() as $results_del_row)
								{
									if(!in_array($results_del_row->fld_id,$infodbupdtids))
									{												
										$this->db->where("fld_id",$results_del_row->fld_id);
										$this->db->delete('tbl_billinginfo');										
									}
								}
							}
							$post_fields['billinginfo'] = $indexinfo;
							$indexinfo++;							
						}
						
					}
				}	
				
				if($response>0)
				{
					$lastupdatedon = date('Y-m-d H:i:s');
					$datahis = array(
					"fld_type" => 'local2server',
					"fld_clientid" => $clientid,
					"fld_outletid" => $outletid,
					"fld_updatedby" => $userid,
					"fld_updatedon" => $lastupdatedon
					);

					$this->db->insert('tbl_updatehistory', $datahis);
					
					$post_fields["lastupdatedon"] = $lastupdatedon;
					$response = json_encode($post_fields);
				}				
			}
		echo $response;		
	}

	function updtdbserver2local()
	{
		$response = 0;
		$lastupdatedon = $this->input->post("lastupdatedon");
		$clientid = $this->input->post("clientid");
		$userid = $this->input->post("userid");
		$outletid = $this->input->post("outletid");	

		
		if($lastupdatedon!='' && $clientid>0 && $outletid>0 && $userid>0)
		{
			$arrTbls = array();
			$arrTbls[] = 'brand';
			$arrTbls[] = 'category';	
			$arrTbls[] = 'zone';
			$arrTbls[] = 'clients';
			$arrTbls[] = 'products';
			$arrTbls[] = 'price';
			$arrTbls[] = 'customers';
			$arrTbls[] = 'dayoff';
			$arrTbls[] = 'gifts';
			$arrTbls[] = 'outlets';			
			$arrTbls[] = 'prodminqty';			
			$arrTbls[] = 'users';			
			$arrTbls[] = 'billing';			
			$arrTbls[] = 'transaction';

			$post_fields = array();
			
			foreach($arrTbls as $tblnm)
			{
			
				$this->db->select("*");		
				$this->db->where("fld_updatedon >'".$lastupdatedon."'");
				if($tblnm!='brand' && $tblnm!='category' && $tblnm!='products' && $tblnm!='clients')
				{
					$this->db->where("fld_clientid",$clientid);
					if($tblnm!='zone' && $tblnm!='price' && $tblnm!='outlets')
					{						
						$this->db->where("fld_outletid",$outletid);
					}
				}
				$udtlcdb2svr_rs = $this->db->get('tbl_'.$tblnm);
				//echo "<br> ==> " .$this->db->last_query();	
				if($udtlcdb2svr_rs->num_rows()>0)
				{
					$post_fields[$tblnm] = $udtlcdb2svr_rs->result_array();				
					if($tblnm=='billing' || $tblnm=='transaction')
					{		
						$id =0;
						$billno ="";
						for($i=0;$i<count($post_fields[$tblnm]);$i++)
						{
							$id = $post_fields[$tblnm][$i]['fld_id'];
							$billno = $post_fields[$tblnm][$i]['fld_billno'];
							if($id>0)
							{
								$this->db->select("*");
								$this->db->where("fld_".$tblnm."id",$id);
								$udtlcdb2svr_rs2 = $this->db->get('tbl_'.$tblnm."info");
								//echo $this->db->last_query();	
								if($udtlcdb2svr_rs2->num_rows()>0)
								{
									$post_fields[$tblnm.'info'][$billno]= $udtlcdb2svr_rs2->result_array();
								}
							}
						}
						
					}
					$response++;
				}
				
			}
				if($response>0)
				{
					$lastupdatedon = date('Y-m-d H:i:s');
					$datahis = array(
					"fld_type" => 'server2local',
					"fld_clientid" => $clientid,
					"fld_outletid" => $outletid,
					"fld_updatedby" => $userid,
					"fld_updatedon" => $lastupdatedon

					);		
					$this->db->insert('tbl_updatehistory', $datahis);
					
					$post_fields["newlastupdatedon"] = $lastupdatedon;
					$response = json_encode($post_fields);
				}
			
			}

		//predata($post_fields);
		echo $response;
	}
}