<?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);

session_start();

date_default_timezone_set("Pacific/Auckland");

if (isset($_SESSION["client_id"])){
}else{
	$_SESSION["client_id"] = 1;
	
	//get sales_id if logged in
	
	
}

		 $phxcrmclient_id = $_COOKIE['phxcrmclient_id'];


if (isset($_COOKIE["t_cont_search_type"])){
	
	
}else{
	
	setcookie("t_cont_search_type", "All", time() + (20 * 365 * 24 * 60 * 60),'/');
	
}


if (isset($_COOKIE["t_cont_sel_id"])){
	
	
}else{
	
	setcookie("t_cont_sel_id", 0, time() + (20 * 365 * 24 * 60 * 60),'/');
	
}

?>
<!doctype html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="theme-color" content="#000000">
    <title>phoenixsoftware.co.nz. Shop POS solutions.</title>
    <meta name="description" content="phoenixsoftware.co.nz. Shop POS solutions.">
    <link rel="icon" type="image/png" href="../images/logo.png" sizes="32x32">
    <link rel="apple-touch-icon" sizes="180x180" href="../images/logo.png">


	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="manifest" href="manifest.json">

</head>

<body class="" onload="javascript: get_prod_run();">
    <link rel="stylesheet" href="../assets/css/style.min.css">
			<link rel="stylesheet" type="text/css" href="jquery-ui.css">

	


	
	
    <!-- loader -->

    <!-- * loader -->

    <!-- App Header -->
    <div class="appHeader bg-primary scrolled is-active">
        <div class="left">
            <a href="#" class="headerButton" data-toggle="modal" data-target="#sidebarPanel">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>

        <div  class="center">
		
		<?php
		
		//get 
		
		
		?>
	
	PhoenixCRM - Dashboard Screen


		</div>
    </div>
    <!-- * App Header -->

    <!-- Search Component -->

    <!-- * Search Component -->

    <!-- App Capsule -->
    <div id="appCapsule">
<center>





    <div class="row" style="height: 900px;" >
	


        <div  style="border: solid 1px; border-color: #6c757d" class="col-md-12">
                 <div style="" class="section mt-6 mb-6"><br>
				 <table>
				 <tr>
				 				 						<td>
	<label class="label" for="c_region">Bill Run:</label>

    </td>
<td>
										<?

	$c_jc_name = 'All';
include("../php/configi.php");

$querybr="SELECT * FROM ".$phxcrmclient_id."_bill_run order by br_start_date";
//echo $querybr;
$resultbr=mysqli_query($conn, $querybr);

?>
<select    class="form-control"   id = "sel_br" name="sel_br" style="width: 350px;"  onchange="javascript: get_prod_run()"  >

<?php

 while($rowbr=mysqli_fetch_array($resultbr)) {


	 $br_id = $rowbr["br_id"];

	 
	 $br_start_date 	= $rowbr["br_start_date"];

	 $br_end_date	 	= $rowbr["br_end_date"];
	 
	 $br_start_date2 = date("d/m/Y", strtotime($br_start_date));
	 
	 $br_end_date2 = date("d/m/Y", strtotime($br_end_date));
	 
	 $br_current	 	= $rowbr["br_current"];
	 
	 if ($br_current == 1){
		 
		 $br_start_date3 = date("d/m/Y", strtotime($br_start_date));
	 
		$br_end_date3 = date("d/m/Y", strtotime($br_end_date));	 
		 
	 }

?>
		<option 
		<?php if ($rowbr['br_current'] == 1){echo " selected ";} ?> value="<? echo $rowbr["br_id"]; ?>"><? echo "Run " .$br_id . " (" .  $br_start_date2 . " to " .  $br_end_date2 . ")" ?></option>

		<?


}


	 
?>
</select>
</td>
				 						<td>
	<label class="label" for="c_region">Billing Status:</label>

    </td>
<td>
										<?

	$c_jc_name = 'All';
include("../php/configi.php");

$queryjc="SELECT * FROM 1_job_codes2 order by jc_order";
//echo $querycy;
$resultjc=mysqli_query($conn, $queryjc);

?>
<select    class="form-control"   id = "sel_status" name="sel_status" style="width: 150px;"  onchange="javascript: get_prod_run()"  >
	<option value="All">All</option>
<?php

 while($rowjc=mysqli_fetch_array($resultjc)) {
	 
	 $jc_name = $rowjc["jc_name"];

?>
		<option 
		<?php if ($rowjc['jc_name'] == $c_jc_name){echo " selected ";} ?> 
		value="<? echo $rowjc["jc_name"]; ?>"><? echo $jc_name; ?></option>

		<?


}


	 
?>
</select>
</td>





	<td>
	  &nbsp;&nbsp;&nbsp;   <button id="create_prod_run" onclick="create_prod_run();"   style="width: 120px" class="btn btn-outline-primary mr-1 mb-1"  type="button">Generate Run</button>
		 </td>

		 

		 
		 		 	<td>
					<div style="display: none">
					
					</div>
	  &nbsp;&nbsp;&nbsp;   <button id="create_prod_run" onclick="get_prod_run();"   style="width: 120px" class="btn btn-outline-primary mr-1 mb-1"  type="button">Get Run</button>
		 </td>
		 
</tr>
</table>	

				           <div class="tab-pane fade show active" id="pilled" role="tabpanel">
						   
						   <table id='maintable' class='table table-striped'>
			<thead style=''>
			<th style='width: 110px; '></th>
		<th  style='width: 270px;'>Company Name</th>
			<th style='width: 250px;'>Contact Name</th>
			

<th  style='width: 150px;'>Status</th>
<th  style='width: 30%;'>
<table>
		<tr>
		<td style='text-align: right; width: 180px;'>Fixed</td>
		<td style='text-align: right; width: 180px;'>Variable</td>
		<td style='text-align: right; width: 180px;'>Total</td>
		</tr>
		</table>
</th>			

<th  style=''>Sel</th>
</thead>



                <div class="section full mt-0" >
                  
                    <div class="wide-block pt-0 pb-2">
											<table>
						<tr>


<td style="width: 50%">
</td>

<td style="width: 400px">
<div>

</div>
</td>
</td>



</tr>
</table>

						

                        <div class="tab-content mt-2">
                            <div class="tab-pane fade show active" id="sales" role="tabpanel">
                                
                <div class="section full mt-0">
                    

					
					                          <div id="divtodaysalesreport" style="overflow:scroll; height:700px; color: black;">
											 
						  
						  </div>

                    </div>
                </div>
				
	                            <div class="tab-pane fade show" id="salesbypymnt"  role="tabpanel">
                                
                <div class="section full mt-0">
                    					<div id="divtodaysalesreportpymnt"  style="overflow:scroll; height:700px; color: black;">
						  
						  </div>

                    </div>
                </div>			
				
		                            <div class="tab-pane fade show" id="salesbystaff" role="tabpanel">
                                
                <div class="section full mt-0">
                    					<div id="divtodaysalesreportstaff"  style="overflow:scroll; height:700px; color: black;">
						  
						  </div>
                    </div>
                </div>		

		                            <div class="tab-pane fade show" id="salesbyitem" role="tabpanel">
                                
                <div class="section full mt-0">
                    					<div id="divtodaysalesreportitem"  style="overflow:scroll; height:700px; color: black;">
						  
						  </div>
                    </div>
                </div>					
		                            <div class="tab-pane fade show" id="dailysummary" role="tabpanel">
                                
                <div class="section full mt-0">
                    					<div id="divtodaysalessummary" style="overflow:scroll; height:700px; color: black;" >
						  
						  </div>

                    </div>
                </div>				
                            </div>

                        </div>

                    </div>
                </div> </div>
				 
				 




        </div>

        </div>


    </div>

</div>




        <!-- app footer -->
        <div  class="section mt-6 mb-6">


		<center>	<small>
 </center>
	<?	
					 	 include '../php/configi.php';
						 
		//	echo "cookie".$_COOKIE['phxcrmclient_id'];			 
						 
						 
	if (isset($_COOKIE['phxcrmclient_id'])){
		 $phxcrmclient_id = $_COOKIE['phxcrmclient_id'];
		 
		 		$queryclient="SELECT * from ".$_COOKIE['phxcrmclient_id']."_client where uid_client = " . $phxcrmclient_id;
	//	echo $queryprod;
		$resultclient=mysqli_query($conn, $queryclient);

		while($rowclient=mysqli_fetch_array($resultclient)) { 

			$comp_name			 				= $rowclient['comp_name'];
			
		}

?>

<?

//echo $_COOKIE['phxcrmclient_id'];

	}else{
		
		
	//	echo $_COOKIE['phxcrmclient_id'];

		
	}
	?>
	<br>

     
            <div class="footer-title">
			<center>
                Copyright © phoenixsoftware.co.nz 2021.<br> All Rights Reserved.
            </center>
			</div>
 


        </div>
        <!-- * app footer -->

    </div>
    <!-- * App Capsule -->
<div class="modal fade dialogbox" id="DialogBasic" data-backdrop="static" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Apply Payment:</h5>
                    </div>
                    <div class="modal-body">
                      

						
                    </div>
                    <div class="modal-footer">
                        <div class="btn-inline">
                            <a href="#" class="btn btn-text-primary" data-dismiss="modal">OK</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
		

		
		        <div class="modal inset fade action-sheet ios-add-to-home" id="ios-add-to-home-screen" tabindex="-1"
            role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add to Home Screen</h5>
                        <a href="javascript:;" class="close-button" data-dismiss="modal">
                            <ion-icon name="close"></ion-icon>
                        </a>
                    </div>
                    <div class="modal-body">
                        <div class="action-sheet-content text-center">
                            <div class="mb-1"><img src="../images/logo.png" alt="image" class="imaged w48">
                            </div>
                            <h4>phoenixsoftware.co.nz</h4>
                            <div>
                                Install phoenixsoftware.co.nz on your iPhone's home screen.
                            </div>
                            <div>
                                Tap the menu for your browser. Then tap Add to homescreen.
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- * iOS Add to Home Action Sheet -->
        <!-- Android Add to Home Action Sheet -->
        <div class="modal inset fade action-sheet android-add-to-home" id="android-add-to-home-screen" tabindex="-1"
            role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add to Home Screen</h5>
                        <a href="javascript:;" class="close-button" data-dismiss="modal">
                            <ion-icon name="close"></ion-icon>
                        </a>
                    </div>
                    <div class="modal-body">
                        <div class="action-sheet-content text-center">
                            <div class="mb-1"><img src="../images/logo.png" alt="image" class="imaged w48">
                            </div>
                            <h4>phoenixsoftware.co.nz</h4>
                            <div>
                                Install phoenixsoftware.co.nz on your Android's home screen.
                            </div>
                            <div>
								Click menu for your browser then click "add app" or "install app".<br>If these options are not there - check for "add to home" option.<br> Once install is complete, go to your home screen and there should<br> be a phoenixsoftware.co.nz app installed. 
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <!-- App Bottom Menu -->
    <div class="appBottomMenu">
        <a href="index.php" class="item active">
            <div class="col">
                <ion-icon name="home-outline"></ion-icon>
            </div>
        </a>


		
		
        <a href="javascript:;" class="item" data-toggle="modal" data-target="#sidebarPanel">
            <div class="col">
                <ion-icon name="menu-outline"></ion-icon>
            </div>
        </a>
    </div>
    <!-- * App Bottom Menu -->

    <!-- App Sidebar -->
	<?php include ('../php/sidebar.php'); ?>
    <!-- * App Sidebar -->

    <!-- welcome notification  -->
    <div id="notification-welcome" class="notification-box">
        <div class="notification-dialog android-style">
            <div class="notification-header">
                <div class="in">
                    <img src="../assets/img/icon/72x72.png" alt="image" class="imaged w24">
                    <strong>phoenixsoftware.co.nz</strong>
                    <span>just now</span>
                </div>
                <a href="#" class="close-button">
                    <ion-icon name="close"></ion-icon>
                </a>
            </div>
            <div class="notification-content">
                <div class="in">
                    <h3 class="subtitle">Welcome to phoenixcrm.co.nz</h3>
                    <div class="text">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
        <div id="toast-login-successful" class="toast-box toast-bottom bg-success">
            <div style="text-align: center;" class="in">
                <div style="text-align: center;" class="text">
					<center>Login Successful</center>
                </div>
            </div>
        </div>
				        <div id="toast-login-failed" class="toast-box toast-bottom bg-danger">
            <div style="text-align: center;" class="in">
                <div style="text-align: center;" class="text">
					<center>Login Failed. Please try again</center>
                </div>
            </div>
        </div>
				<div class="modal fade" id="DialogContEdit" tabindex="-1" role="dialog" aria-labelledby="divcontedit" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">

                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit Contact Details</h5>
						 <a href="javascript:;" data-dismiss="modal">Close</a>
                    </div>
                    <form>
                        <div id="divcontedit" style="height: 700px; overflow-y: scroll" class="modal-body text-left mb-6">


                        </div>
                        <div class="modal-footer">
                            <div class="btn-inline">
                            
                                <button  type="button" data-dismiss="modal" class="btn btn-text-primary">CLOSE</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    <!-- * welcome notification -->

    <!-- ///////////// Js Files ////////////////////  -->
    <!-- Jquery -->
    <script src="../assets/js/lib/jquery-3.4.1.min.js"></script>
    <script src="../js/jquery_print.js"></script>
	
	    <script src="../js/phxpos_comm.js"></script>
		
			<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>

	<script src="../js/jquery-ui.min.js"> </script>
	
    <!-- Bootstrap-->
    <script src="../assets/js/lib/popper.min.js"></script>
    <script src="../assets/js/lib/bootstrap.min.js"></script>
    <!-- Ionicons -->
    <script type="module" src="https://unpkg.com/ionicons@5.2.3/dist/ionicons/ionicons.js"></script>
  
    <!-- Base Js File
    <script src="../assets/js/base.js"></script> -->
<script src="https://unpkg.com/sticky-table-headers"></script>

    

	
	<!-- Owl Carousel -->
	
	
    <!-- jQuery Circle Progress -->

  <script>
	
	
	function getCookie(cname) {
  let name = cname + "=";
  let ca = document.cookie.split(';');
  for(let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}



$(function() {
	
//	alert ("hello");
	
		$('#date_from').datepicker({
			dateFormat: "dd-mm-yy",
			onSelect: function(dateText) {
				
		//		get_prod_run();

			//	change_date_report();
				
			//	alert("Selected date: " + dateText + "; input's current value: " + this.value);
			}
		});
		$('#date_to').datepicker({
			dateFormat: "dd-mm-yy",
			onSelect: function(dateText) {
				
		//		get_prod_run();

			//	change_date_report();
				
			//	alert("Selected date: " + dateText + "; input's current value: " + this.value);
			}
		});

});

           



    </script>

    <script>





function logout_staff(){



	window.location.replace("logout.php");

//	alert ("logout staff");

	
}



function change_date_report(){

	sel_tab = document.getElementById('sel_tab').value;
	
	if (sel_tab == 1){
		
		load_todays_sales();
		
	}else if (sel_tab == 2){
		
		load_todays_sales_bypymnt();

	}else if (sel_tab == 3){
		
		load_todays_sales_bystaff();

	}else if (sel_tab == 4){
		
		load_todays_sales_byitem();
		
	}else if (sel_tab == 5){
		
		load_todays_sales_summary();
	}

	
}





	  function load_todays_sales() {
			
		//		var e =  document.getElementById('Outlet_ID');
		//		var outlet_id =  e.options[e.selectedIndex].value;
		
		document.getElementById('sel_tab').value = 1;
		
		document.getElementById("divtodaysalesreport").innerHTML = "loading ... ";
		
		var date_from = document.getElementById('date_from').value;
		var date_to = document.getElementById('date_to').value;
		
		var outlet_id = 1;

				var currentTime = new Date();

		        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		         document.getElementById("divtodaysalesreport").innerHTML = xmlhttp.responseText;
			}
        }
        xmlhttp.open("GET", "../php/todaysalesshop.php?outlet_id="+outlet_id+"&date_from="+date_from+"&date_to="+date_to+"&reqtime="+currentTime, true);
        xmlhttp.send();
	
}



	  function load_todays_sales_bypymnt() {
		  
		var date_from = document.getElementById('date_from').value;
		var date_to = document.getElementById('date_to').value;

		document.getElementById('sel_tab').value = 2;
		
		document.getElementById("divtodaysalesreportpymnt").innerHTML = "loading ... ";

		  
	//	  alert ("xmlhttp.responseText");
			
		//		var e =  document.getElementById('Outlet_ID');
		//		var outlet_id =  e.options[e.selectedIndex].value;
		
		var outlet_id = 1;

				var currentTime = new Date();

		        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//	alert (xmlhttp.responseText);
		         document.getElementById("divtodaysalesreportpymnt").innerHTML = xmlhttp.responseText;
			}
        }
        xmlhttp.open("GET", "../php/todaysalesshoppymnt.php?outlet_id="+outlet_id+"&date_from="+date_from+"&date_to="+date_to+"&reqtime="+currentTime, true);
        xmlhttp.send();
	
}



	  function load_todays_sales_bystaff() {
		  
  		document.getElementById('sel_tab').value = 3;
		
		document.getElementById("divtodaysalesreportstaff").innerHTML = "loading ... ";

		var date_from = document.getElementById('date_from').value;
		var date_to = document.getElementById('date_to').value;
		  
	//	  alert ("xmlhttp.responseText");
			
		//		var e =  document.getElementById('Outlet_ID');
		//		var outlet_id =  e.options[e.selectedIndex].value;
		
		var outlet_id = 1;

				var currentTime = new Date();

		        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//	alert (xmlhttp.responseText);
		         document.getElementById("divtodaysalesreportstaff").innerHTML = xmlhttp.responseText;
			}
        }
        xmlhttp.open("GET", "../php/todaysalesshopstaff.php?outlet_id="+outlet_id+"&date_from="+date_from+"&date_to="+date_to+"&reqtime=" + currentTime, true);
        xmlhttp.send();
	
}

	  function load_todays_sales_byitem() {
		  
		//  alert ("by item");
		  

		document.getElementById('sel_tab').value = 4;
		
		document.getElementById("divtodaysalesreportitem").innerHTML = "loading ... ";
	
		var date_from = document.getElementById('date_from').value;
		var date_to = document.getElementById('date_to').value;
		
	//	  alert ("xmlhttp.responseText");
			
		//		var e =  document.getElementById('Outlet_ID');
		//		var outlet_id =  e.options[e.selectedIndex].value;
		
		var outlet_id = 1;

				var currentTime = new Date();

		        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//	alert (xmlhttp.responseText);
		         document.getElementById("divtodaysalesreportitem").innerHTML = xmlhttp.responseText;
			}
        }
        xmlhttp.open("GET", "../php/todaysalesshopitem.php?outlet_id="+outlet_id+"&date_from="+date_from+"&date_to="+date_to+"&reqtime="+currentTime, true);
        xmlhttp.send();
	
}

	  function load_todays_sales_summary() {

		document.getElementById('sel_tab').value = 5;
		document.getElementById("divtodaysalessummary").innerHTML = "loading ... ";
	
		var date_from = document.getElementById('date_from').value;
		var date_to = document.getElementById('date_to').value;
		
	//	  alert ("xmlhttp.responseText");
			
		//		var e =  document.getElementById('Outlet_ID');
		//		var outlet_id =  e.options[e.selectedIndex].value;
		
		var outlet_id = 1;

				var currentTime = new Date();

		        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			//	alert (xmlhttp.responseText);
		         document.getElementById("divtodaysalessummary").innerHTML = xmlhttp.responseText;
			}
        }
        xmlhttp.open("GET", "../php/todaysalesshopsummary.php?outlet_id="+outlet_id+"&date_from="+date_from+"&date_to="+date_to+"&reqtime="+currentTime, true);
        xmlhttp.send();
	
}






	function set_search(cookie_name, value){
		
		 setCookie(cookie_name, value, 365);
		 
		 $('#example').DataTable().ajax.reload();
		
		
	}
	
	function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"+ ";Secure";
}


function update_field_date(date_type, date_value){
	
//	alert (date_type+" "+date_value);
	
	
}


		function printDiv(divName){
			
			var divContents = document.getElementById(divName).innerHTML;
			
			var a = window.open('', '', 'width=500');
            a.document.write('<html> <style type="text/css" media="print"> @page { size: auto;   /* auto is the current printer page size */ margin: 0mm;  /* this affects the margin in the printer settings */} body { background-color:#FFFFFF; border: solid 1px black ; margin: 0px;  /* the margin on the content before printing */} </style>');
	
            a.document.write('<body>');
            a.document.write(divContents);
            a.document.write('</body></html>');
            a.print();
            a.document.close();
			
		}
		

		 
 function create_prod_run(){
	 
		let sel_br = document.getElementById("sel_br").value;
		
		alert (sel_br);
	 
		document.getElementById("divtodaysalesreport").innerHTML = "Processing ...";
		
	//	return false;
		

		// add item
		
		$.ajax({
				url: '../php/createprodrunsimply.php',
				type: 'post',
				async: 'true',
				data: {"sel_br" : sel_br},
				success: function(data) {
					alert (data);

					get_prod_run();
					
	                var obj = JSON.parse( data.trim() );

				}
			});
			
			
		
	}

			 
 function get_prod_run(){
		
		
		document.getElementById("divtodaysalesreport").innerHTML = "loading ...";
	//	var date_from = document.getElementById("date_from").value;
	//	var sel_region = document.getElementById("sel_region").value;
	//	var sel_status = document.getElementById("sel_status").value;
//		var sort_order = document.getElementById("sort_order").value;
	
		let sel_br = document.getElementById("sel_br").value;
		
	//	alert (sel_br);	
		
		
//		alert (date_from + " " + sel_region);
		
	//	var output = date_from + " " + sel_region;
		
		
	//	document.getElementById("divtodaysalesreport").innerHTML = output;
		
	//	return false;
		

		//alert (tagsprodcont);
		
		
	//	var sales_id =  document.getElementById('currentsalesid').value;
		var sales_id = 0;
		// add item
		
		$.ajax({
				url: '../php/getprodrunsimply.php',
				type: 'post',
				async: 'true',
				data: {"sel_br" : sel_br},
				success: function(data) {
					
			//		alert (data);
					
					document.getElementById("divtodaysalesreport").innerHTML = data;
					
					var export_link = '<a class="noprint" style="text-decoration: underline; color: rgb(30, 116, 253)" href="../php/prodreport.php?date+from='+date_from+'&amp;sel_region='+sel_region+'">Export confirmed orders to Excel</a>';

					var export_link2 = '<a class="noprint" style="text-decoration: underline; color: rgb(30, 116, 253)" href="../php/prodreportall.php?date+from='+date_from+'&amp;sel_region='+sel_region+'">Export all customers to Excel</a>';
					
					
					document.getElementById("exportbtn").innerHTML = export_link;

					document.getElementById("exportbtn2").innerHTML = export_link2;
					
					
				//	var xero_link = '<a href= "" onclick="javascript: xero_invoice_run();"    target="_blank" ><img style="height: 50px" src="../images/xero.png"></a>';
					
					var xero_link = '<a id="callxero" onclick="javascript: xero_invoice_run();"  href= "../xero"  target="_blank" ><img style="height: 50px" src="../images/xero.png"></a>';
					
										
					if (sel_status == "Ready to Invoice"){
					
						document.getElementById("xerobtn").innerHTML = xero_link;
						
					}else{
						
						document.getElementById("xerobtn").innerHTML = "";
						
					}


//					

					get_totals();
				//	$('maintable').stickyTableHeaders();
				
//				alert ("done");



				}
			});
			
			
		
	}	
	
	
	 function get_totals(){
		
		
		//document.getElementById("divtodaysalesreport").innerHTML = "loading ...";
		var date_from = document.getElementById("date_from").value;
		var sel_region = document.getElementById("sel_region").value;
		var sel_status = document.getElementById("sel_status").value;
		
//		alert (date_from + " " + sel_region);
		
		var output = date_from + " " + sel_region;
		
		
	//	document.getElementById("divtodaysalesreport").innerHTML = output;
		
	//	return false;
		

		//alert (tagsprodcont);
		
		
	//	var sales_id =  document.getElementById('currentsalesid').value;
		var sales_id = 0;
		// add item
		
		$.ajax({
				url: '../php/getprodruntotalssimply.php',
				type: 'post',
				async: 'true',
				data: {"date_from" : date_from, "sel_region" : sel_region, "sel_status" : sel_status},
				success: function(data) {
					
				//	alert (data);
					
					var obj = JSON.parse( data.trim() );
					
					var ttl100 = obj.ttl100;
					var ttl101 = obj.ttl101;
					var ttl102 = obj.ttl102;
					var ttl103 = obj.ttl103;
					
					
					document.getElementById("ttl_commit_r").value = ttl100;
					document.getElementById("ttl_commit_a").value = ttl102;
					document.getElementById("ttl_commit_fb").value = ttl101;
					document.getElementById("ttl_commit_l").value = ttl103;
						
						
					

				//	alert (data);
					
				//	$('maintable').stickyTableHeaders();
				
//				alert ("done");



				}
			});
			
			
		
	}

	
		
	$('#DialogContEdit').on('shown.bs.modal', function () {

	//alert ("shown");
//		$('#tagsstaffpw').value = "";
	//	$('#Bullets').value = "";
	//	clear_charp();
		$('#comp_name_edit').focus();
		
	//	alert ("done");
	

}) 


	$('#DialogContEdit').on('hide.bs.modal', function () {

	//alert ("shown");
//		$('#tagsstaffpw').value = "";
	//	$('#Bullets').value = "";
	//	clear_charp();
	//	$('#comp_name_edit').focus();
		
	//	alert ("done");
	
	//	 $('#example').DataTable().ajax.reload( null, false )
	
	get_prod_run();
	

}) 
	
			function get_contact_edit(uid_client){
		
	//	alert ("edit"+uid_client);
		
		
			var uid_client = uid_client;
			
	
			$.ajax({
				url: '../php/custedittrades.php',
				type: 'post',
				async: 'false',
				data: {"uid_client" : uid_client},
				success: function(data) {
					
				//	alert (data);
					var data = data.trim();
					
					document.getElementById("divcontedit").innerHTML = data;
					
					$("#c_cust_since").datepicker({ dateFormat: 'dd-mm-yy' });
					
												 
CKEDITOR.replace('c_favcmts', {
		           on: {
            instanceReady: function (ev) {
                this.dataProcessor.writer.setRules('p', {
                    breakAfterClose: false
                });
            }
			   },
	 toolbar: [
   [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ],
   { name: 'insert', items: [ 'Image', 'Table', 'HorizontalRule', 'SpecialChar' ] },
   { name: 'font', items: ['Font','FontSize'] },
   '/',
   { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Underline','-','JustifyLeft','JustifyCenter','JustifyRight','-','NumberedList','BulletedList','-','Link','Unlink','-','Source' ] },
   { name: 'colors', items: [ 'TextColor', 'BGColor' ] },
   { name: 'tools', items: [ 'Maximize' ] }
  ]
}
);

												 
CKEDITOR.replace('c_popup', {
		           on: {
            instanceReady: function (ev) {
                this.dataProcessor.writer.setRules('p', {
                    breakAfterClose: false
                });
            }
			   },
	 toolbar: [
   [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ],
   { name: 'insert', items: [ 'Image', 'Table', 'HorizontalRule', 'SpecialChar' ] },
   { name: 'font', items: ['Font','FontSize'] },
   '/',
   { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Underline','-','JustifyLeft','JustifyCenter','JustifyRight','-','NumberedList','BulletedList','-','Link','Unlink','-','Source' ] },
   { name: 'colors', items: [ 'TextColor', 'BGColor' ] },
   { name: 'tools', items: [ 'Maximize' ] }
  ]
}
);


				CKEDITOR.instances['c_favcmts'].on('blur', function(e) {
					
			//		alert ("blur");
					
				    if (e.editor.checkDirty()) {
						
						update_field_contact('c_favcmts', 'ckeditor', '')
					
					//	 update_field_product('prod_descr', 'ckeditor', '');
      
				//	alert("editjd2");
				}
				});

				CKEDITOR.instances['c_popup'].on('blur', function(e) {
				    if (e.editor.checkDirty()) {
						
						update_field_contact('c_popup', 'ckeditor', '')
					
					//	 update_field_product('prod_descr', 'ckeditor', '');
      
				//	alert("editjd2");
				}
				});				
					
				}
			});	
		
		
	}
	
	
	function change_st_status(st_id, sel_status){
		
	//	alert (st_id + " " + sel_status);
		
		var st_id = st_id;
		var sel_status = sel_status;
		
		
				
		$.ajax({
				url: '../php/updjobstatus.php',
				type: 'post',
				async: 'true',
				data: {"st_id" : st_id, "sel_status" : sel_status},
				success: function(data) {

					
					var obj = JSON.parse( data.trim() );
					
					if (obj.phx_result == "successful"){

						get_prod_run();
					}else{
					
						alert ("an error occured");
						
					}
					


				}
			});
			
		
	}
	
	
		
						
	function upd_cart_qty(st_id, cart_function, cart_uid, newqty){
		
	//	alert ("sales_id"+st_id);
		
		
		var ref_val = 1;

		var newqty = newqty;
		
		if (cart_function == "addqty"){
			
			ref_val = 1;
			
			var field_name = "qty_"+st_id+ "_"+cart_uid;
			
			var newqty = Number(document.getElementById(field_name).value) + 1;

			ref_val = 1;
			cart_function = "newqty";
			
		//	alert (newqty);
			
		}else if (cart_function == "subqty"){
			
			ref_val = 1;
			
			var field_name = "qty_"+st_id+ "_"+cart_uid;
			
			var newqty = Number(document.getElementById(field_name).value) - 1;

		
			cart_function = "newqty";
			
		//	alert (newqty);
			
		}

		// add item
		
		$.ajax({
				url: '../php/updcart2.php',
				type: 'post',
				async: 'false',
				data: {"st_id" : st_id, "cart_function" : cart_function, "cart_uid" : cart_uid, "newqty" : newqty},
				success: function(data) {
					
			//		alert (data);
					
					var data = data.trim();
					
	                var obj = JSON.parse( data );
 
					var phx_result 	  		= obj.phx_result;
					var sales_id 	  		= obj.sales_id;
					var comp_stock_type		= obj.comp_stock_type;
					var new_line_amount   	= "$"+obj.new_line_amount;
					

//alert (phx_result);

					

					
					if (phx_result == ""){
						
					//	alert (ref_val);
						
						if (ref_val == 1){
							
							var field_name = "qty_"+st_id+ "_"+cart_uid;
			
							document.getElementById(field_name).value = newqty;
							
							get_totals();
							
							
						}
						
								//update item line qty
					//		var line_amount_field = "line_amount_"+cart_uid;
							
						//	alert (item_amount_field);
							
					//		document.getElementById(line_amount_field).value = new_line_amount;
								
					//		load_cart_totals(sales_id);
							

					}else{
												
						alert ("An error ocurred adding item "+data);
					
					}

					//		document.getElementById("divsearchresults").innerHTML = data;

				}
			});
			
			
			
	//	alert ("add to cart"+qty);
	

				
	}
	
	
		 function update_field_salesnotes(st_id, field_value) {

//			alert ("update"+field_name+field_type+field_value+field_value2);
			
		//	return false;
	//		alert ("update ");
			
		//	var uid_client			= document.getElementById('cv_uid_client').value;
			
		//	alert (uid_client);
			

	//		var element_je_job_outlet_name = document.getElementById('je_job_outlet_name')
			var outlet_id 			= 1;			
			var old_status			= "";

			var c_favcmts 			= "";

			
			//set defaults
			var c_favcmts = "";
			var c_popup = "";
			

			var field_name = "sales_comments";
	
				
			var field_type			= "input";
			var field_name1_db		= field_name;
			var field_name2_db		= "";
			var field_value1 		= field_value;
			var field_value2 		= "";

					
		
			var jobfields = {};

			jobfields.sales_id	 = st_id;			
			jobfields.field_name = field_name;
			jobfields.field_type = field_type;

			var sales_comments = field_value;
			
			
			$.ajax({
				url: '../php/orderupdsalescmts.php',
				type: 'post',
				data: {"jobfields" : JSON.stringify(jobfields), "st_id" : st_id,"sales_comments": sales_comments},
				success: function(data) {
				//	alert ("success"+data);
				//
				if (data == "Update Successful"){
				}else{
					alert ("Update failed error details are"+data);
				}
//				alert (data);

//					alert (field_name+data);
					// Do something with data that came back. 
				}
			});			
			
		//	alert (	jobfields.sales_id+jobfields.field_name+jobfields.field_type+jobfields.field_name1_db+jobfields.field_name2_db+jobfields.field_value1+jobfields.field_value2);


			
		 }
		 

		
		 function update_field_clientorderno(st_id, field_value) {

//			alert ("update"+field_name+field_type+field_value+field_value2);
			
		//	return false;
	//		alert ("update ");
			
		//	var uid_client			= document.getElementById('cv_uid_client').value;
			
		//	alert (uid_client);
			

	//		var element_je_job_outlet_name = document.getElementById('je_job_outlet_name')
			var outlet_id 			= 1;			
			var old_status			= "";

			var c_favcmts 			= "";

			
			//set defaults
			var c_favcmts = "";
			var c_popup = "";
			

			var field_name = "client_order_no";
	
				
			var field_type			= "input";
			var field_name1_db		= field_name;
			var field_name2_db		= "";
			var field_value1 		= field_value;
			var field_value2 		= "";

					
		
			var jobfields = {};

			jobfields.sales_id	 = st_id;			
			jobfields.field_name = field_name;
			jobfields.field_type = field_type;

			var client_order_no = field_value;
			
			
			$.ajax({
				url: '../php/orderupdclientorderno.php',
				type: 'post',
				data: {"jobfields" : JSON.stringify(jobfields), "st_id" : st_id,"client_order_no": client_order_no},
				success: function(data) {
				//	alert ("success"+data);
				//
				if (data == "Update Successful"){
				}else{
					alert ("Update failed error details are"+data);
				}
//				alert (data);

//					alert (field_name+data);
					// Do something with data that came back. 
				}
			});			
			
		//	alert (	jobfields.sales_id+jobfields.field_name+jobfields.field_type+jobfields.field_name1_db+jobfields.field_name2_db+jobfields.field_value1+jobfields.field_value2);


			
		 }
		 	
	
		 function xero_invoice_run() {
			 
			//	alert ("xero");
		var date_from = document.getElementById("date_from").value;
		var sel_region = document.getElementById("sel_region").value;
		var sel_status = document.getElementById("sel_status").value;


				$.ajax({
				url: '../php/addpayrun.php',
				type: 'post',
				async: 'true',
				data: {"date_from" : date_from, "sel_region" : sel_region, "sel_status" : sel_status},
				success: function(data) {
					
			//		alert (data);
			//		
				//	 window.location.replace = ("https://app.phoenixcrm.co.nz/crm/xero");
					 
				//	 document.getElementById("callxero").click();
					

				}
			});
			
			
		
		
		
			 
		 }
		 
		 		 function update_field_contact_recurr(field_name, field_type, field_value, uid_r) {

//			alert ("update"+field_name+field_type+field_value+field_value2);
			
		//	return false;
	//		alert ("update ");
			
			var uid_client			= document.getElementById('cv_uid_client').value;
			
		//	alert (uid_client);
			

	//		var element_je_job_outlet_name = document.getElementById('je_job_outlet_name')
			var outlet_id 			= 1;			
			var old_status			= "";

		//	var c_favcmts 			= "";

			
			//set defaults
		//	var c_favcmts = "";
			var c_popup = "";


				
		//		alert ("dave"+c_popup);

			if (field_name == "r_unit"){

				var field_type			= "inputdouble";
				var field_name1_db		= "r_unit";
				var field_name2_db		= "";
				var field_value1 		= field_value;
				var field_value2 		= "";


			}else if (field_name == "r_qty"){

				var field_type			= "inputdouble";
				var field_name1_db		= "r_qty";
				var field_name2_db		= "";
				var field_value1 		= field_value;
				var field_value2 		= "";
				
			}else if (field_name == "r_line_amt"){

				var field_type			= "inputdouble";
				var field_name1_db		= "r_line_amt";
				var field_name2_db		= "";
				var field_value1 		= field_value;
				var field_value2 		= "";				


			}else{
	
				
				var field_type			= "input";
				var field_name1_db		= field_name;
				var field_name2_db		= "";
				var field_value1 		= field_value;
				var field_value2 		= "";

			}		
		
			var jobfields = {};

			jobfields.sales_id	 = 0;			
			jobfields.field_name = field_name;
			jobfields.field_type = field_type;
			jobfields.field_name1_db = field_name1_db;
			jobfields.field_name2_db = field_name2_db;
			jobfields.field_value1 = field_value1;
			jobfields.field_value2 = field_value2;
			jobfields.old_status = old_status;
			jobfields.uid_client = uid_client;
			jobfields.outlet_id = outlet_id;			
			jobfields.uid_r = uid_r;
			
	//		alert (jobfields.field_name);
	//		alert ("about to ajax");


			
	//		alert (JSON.stringify(jobfields));
			
//			alert (c_favcmts);
			
			
			
			$.ajax({
				url: '../php/orderupdcontactrecurr.php',
				type: 'post',
				data: {"jobfields" : JSON.stringify(jobfields) },
				success: function(data) {

		//		alert (data);

			//		alert ("success"+data);
				//
				if (data == "Update Successful"){
				}else{
					alert ("Update failed error details are"+data);
				}

//					alert (field_name+data);
					// Do something with data that came back. 
				}
			});			
			
		//	alert (	jobfields.sales_id+jobfields.field_name+jobfields.field_type+jobfields.field_name1_db+jobfields.field_name2_db+jobfields.field_value1+jobfields.field_value2);


			
		 }

    </script>

</body>

</html>