<?php include "../../includes/start_ajax.php";?>
<?php



$cell_width="100";

 

echo "<section class='view_toolbar'>";
	echo "<div class='view_toolbar_left' style='width:50%;'>";
		   echo "<font size=5 ><span class='fa fa-bar-chart fa-1x'></span> <b>Daily Sales Summary</b></font>";

		   echo "<br>";
		  
	echo "</div>";
	echo "<div class='view_toolbar_right'>";
	
			
			echo " <input type=button class='button button-blue' value='Refresh' onclick=\"mon_send_get('mod/sales_summary_day/index.php','','div_main')\">";
	

    
	echo "</div>";
echo "</section>";





    echo "<section class='view_header'>";

    //echo $_SESSION["data_site"];
    echo "</section>";


    echo "<section class='view_content'>";



$month_text[1]="JAN";
$month_text[2]="FEB";
$month_text[3]="MAR";
$month_text[4]="APR";
$month_text[5]="MAY";
$month_text[6]="JUN";
$month_text[7]="JUL";
$month_text[8]="AUG";
$month_text[9]="SEP";
$month_text[10]="OCT";
$month_text[11]="NOV";
$month_text[12]="DEC";



$month_text2[1]="January";
$month_text2[2]="February";
$month_text2[3]="March";
$month_text2[4]="April";
$month_text2[5]="May";
$month_text2[6]="June";
$month_text2[7]="July";
$month_text2[8]="August";
$month_text2[9]="September";
$month_text2[10]="October";
$month_text2[11]="November";
$month_text2[12]="December";


//======================================================================================




//device library:
$query_device="select * from t_".$key_table_prefix."_device ";
$result_device = mysqli_query($dbconn,$query_device);
while($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
{
	
	$device_data[$line_device["dev_android_id"]]=$line_device["dev_name"];
}



//device library:
$query_staff="select * from t_".$key_table_prefix."_staff ";
$result_staff = mysqli_query($dbconn,$query_staff);
while($line_staff = mysqli_fetch_array($result_staff,MYSQLI_ASSOC))
{
	
	$staff_data[$line_staff["stf_code"]]=$line_staff["stf_name"];
}






if($_REQUEST["y"]!="")
{
	$_SESSION["calendar_y"]=$_REQUEST["y"];
}

if($_REQUEST["m"]!="")
{
	$_SESSION["calendar_m"]=$_REQUEST["m"];
}

if($_REQUEST["d"]!="")
{
	$_SESSION["calendar_d"]=$_REQUEST["d"];
}






//======================================================================================

if($_SESSION["calendar_y"]=="")
{
	$_SESSION["calendar_y"]=date("Y");
}

if($_SESSION["calendar_m"]=="")
{
	$_SESSION["calendar_m"]=date("m");
}

if($_SESSION["calendar_d"]=="")
{
	$_SESSION["calendar_d"]=date("d");
}







echo "<div class='cal_menu_panel'>";
    echo "<div class='cal_menu_box'>";
    for($y=2010;$y<=2030;$y++)
    {
        if($_SESSION["calendar_y"]==$y)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/sales_summary_day/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&y=".$y."".$tab_extra_url."','div_main')\">".$y."</div>";
    }
    echo "</div>";

    echo "<div class='cal_menu_box'>";
    for($m=1;$m<=12;$m++)
    {
        if($_SESSION["calendar_m"]==$m)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/sales_summary_day/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&m=".substr($m+100,1,2)."".$tab_extra_url."','div_main')\" >".$month_text[$m]."</div>";
    }
    echo "</div>";


    echo "<div class='cal_menu_box'>";
    for($d=1;$d<=date("t",mktime(0,0,0,$_REQUEST["m"]+0,1,$_REQUEST["y"]));$d++)
    {

        if($_SESSION["calendar_d"]==$d)
        {
            echo "<div class='cal_menu_active' ";
        }
        else
        {
            echo "<div class='cal_menu_inactive' ";
        }
        echo " onclick=\"mon_send_get('mod/sales_summary_day/index.php','?".$tab_key_id."=".$_REQUEST[$tab_key_id]."&d=".substr($d+100,1,2)."".$tab_extra_url."','div_main')\" >".$d."</div>";
    }
    echo "</div>";



echo "</div>";




$selected_date_start=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]." 00:00:00";
$selected_date_end=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]." 23:59:59";


$selected_date_start_simple=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";
$selected_date_end_simple=$_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";



$selected_date = $_SESSION["calendar_y"]."-".$_SESSION["calendar_m"]."-".$_SESSION["calendar_d"]."";

echo "<font size=1 color=#34495E style='float:left;width:100%;' >Showing data of period ".$selected_date_start." - ".$selected_date_end."</font><br><br><br>";


echo "<br>&nbsp;";




//====================================================Summary by Device==============================================================
echo "<br>&nbsp;<br>&nbsp;";

echo "<div class='data_display_content'>";	


echo "<font size=3 style='float:left;padding:15px;color:#000;'><b><span class='fa fa-tablet fa-lg'></span> Sales Summary by Device</b></font>";

echo "<br>";

echo " <input  style='float:right;margin-right:5%;' type=button class='button button-blue' value='Print All' onclick=\"print_page('print/sales_report/?start=".$selected_date_start_simple."&end=".$selected_date_end_simple."')\"> ";

echo "<input style='float:left;' type=button class='button button-blue' value='Print' onclick=\"print_page('print/sales_report/?start=".$selected_date_start_simple."&end=".$selected_date_end_simple."&part=SUM_DEV')\">";

	echo "<div class='view_list_title_row'>";
		echo "<div class='view_list_title_content'>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Device</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Discount Amount</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Before Tax</b>";
			echo "</div>";
			
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Tax Amount</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Amount</b>";
			echo "</div>";
			
			
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Rounding Amount</b>";
			echo "</div>";
			
			echo "<div style='width:250px;' class='view_list_title_content_object'  >";
				echo "<b>Amount Rounded</b>";
			echo "</div>";
		echo "</div>";
	echo "</div>";
			
			
	$query="select 
			
			sum(ivh_discount_total) as ivh_discount_total,
			sum(ivh_total_before_gst) as ivh_total_before_gst,
			sum(ivh_gst_amount) as ivh_gst_amount,
			sum(ivh_total_amount) as ivh_total_amount,
			
			sum(ivh_rounding_amount) as ivh_rounding_amount,
			sum(ivh_total_amount_rounded) as ivh_total_amount_rounded,
			
			
			
			
			device_id 
			
			
			from t_".$key_table_prefix."_invoice_header 
			where ivh_status = 'POSTED'
			and ivh_date >= '".$selected_date_start_simple."'
			and ivh_date <= '".$selected_date_end_simple."'
			group by device_id
			
			";		
	$result = mysqli_query($dbconn,$query);
	while($line = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		$report_data[$line["device_id"]] = $line;		
		
	}
	
			


	$sum_discount_total=0;
	$sum_total_before_gst=0;
	$sum_gst_amount=0;
	$sum_total_amount=0;
	$sum_rounding_amount=0;
	$sum_total_amount_rounded=0;			
			
			
			
	$counter=1;
	foreach($report_data as $report_key => $report_value)
	{
	
		echo "<div class='view_row'>";
			echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>".$counter."</font>";
				echo "<font class='view_row_content_text'>";
					echo "<font color=#000>".$device_data[$report_key]."</font>";
				echo "</font><br>";			
			echo "</div>";
		
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>Discount Total</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_discount_total"];
					$sum_discount_total = $sum_discount_total + $report_value["ivh_discount_total"];
				echo "</font><br>";			
			echo "</div>";
			
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>Total Before GST</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_total_before_gst"];
					$sum_total_before_gst = $sum_total_before_gst + $report_value["ivh_total_before_gst"];
				echo "</font><br>";			
			echo "</div>";
			
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>GST Amount</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_gst_amount"];
					$sum_gst_amount = $sum_gst_amount + $report_value["ivh_gst_amount"];
				echo "</font><br>";			
			echo "</div>";
				
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>Total Amount</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_total_amount"];
					$sum_total_amount = $sum_total_amount + $report_value["ivh_total_amount"];
				echo "</font><br>";			
			echo "</div>";
			
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>Rounding Amount</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_rounding_amount"];
					$sum_rounding_amount = $sum_rounding_amount + $report_value["ivh_rounding_amount"];
				echo "</font><br>";			
			echo "</div>";
			
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'>Amound Rounded</font>";
				echo "<font class='view_row_content_text'>";
					echo $report_value["ivh_total_amount_rounded"];
					$sum_total_amount_rounded = $sum_total_amount_rounded + $report_value["ivh_total_amount_rounded"];
				echo "</font><br>";			
			echo "</div>";
			
				
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
			echo "</div>";
			
		echo "</div>";
		$counter++;
	}


	//total
	echo "<div class='view_row'>";
		echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>".$counter."</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000><b>Total</b></font>";
			echo "</font><br>";			
		echo "</div>";
	
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Discount Total</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_discount_total,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Before GST</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_total_before_gst,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>GST Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_gst_amount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
			
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_total_amount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Rounding Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_rounding_amount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Amound Rounded</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_total_amount_rounded,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
			
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
		echo "</div>";
		
	echo "</div>";
	
echo "</div>";





echo "<br>&nbsp;<br>&nbsp;";
echo "<br>&nbsp;<br>&nbsp;";



//====================================================Summary by Staff==============================================================
echo "<br>&nbsp;<br>&nbsp;";

echo "<div class='data_display_content'>";	


echo "<font size=3 style='float:left;padding:15px;color:#000;'><b><span class='fa fa-male fa-lg'></span> Sales Summary by Staff</b></font>";
echo "<br>";
echo "<input style='float:left;' type=button class='button button-blue' value='Print' onclick=\"print_page('print/sales_report/?start=".$selected_date_start_simple."&end=".$selected_date_end_simple."&part=SUM_STF')\">";


	echo "<div class='view_list_title_row'>";
		echo "<div class='view_list_title_content'>";
			
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Staff Code</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Quantity</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Discount</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Amount</b>";
			echo "</div>";
			
		echo "</div>";
	echo "</div>";
			
			
			
	unset($report_data_salesperson);	
			
	$query="select 
			
			sum(ivi_quantity) as ivi_quantity,
			sum(ivi_discount_total) as ivi_discount_total,
			sum(ivi_total_amount) as ivi_total_amount,
			
			
			ivi_salesperson
			
			
			from t_".$key_table_prefix."_invoice_item
			where ivi_status = 'POSTED'
			and created_date >= '".$selected_date_start."'
			and created_date <= '".$selected_date_end."'
			group by ivi_salesperson
			order by ivi_salesperson
			
			";		
	$result = mysqli_query($dbconn,$query);
	while($line = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		if($line["ivi_salesperson"]=="")
		{
			$line["ivi_salesperson"]="Unspecified";
		}
		$report_data_salesperson[$line["ivi_salesperson"]] = $line;		
		
	}

			


		$sum_product_quantity=0;
		$sum_product_discount=0;
		$sum_product_amount=0;
		
		
			
	$counter=1;
	foreach($report_data_salesperson as $salesperson_key => $report_data_product2)
	{
	
		

			echo "<div class='view_row'>";
				echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$counter."</font>";
					echo "<font class='view_row_content_text'>";
						echo "".$salesperson_key."";
					echo "</font><br>";			
				echo "</div>";
			
			
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Before GST</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product2["ivi_quantity"];
						$sum_product_quantity = $sum_product_quantity + $report_data_product2["ivi_quantity"];
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Discount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product2["ivi_discount"];
					
						$sum_product_discount = $sum_product_discount + $report_data_product2["ivi_discount"];
					echo "</font><br>";			
				echo "</div>";
				
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product2["ivi_total_amount"];
					
						$sum_product_amount = $sum_product_amount+ $report_data_product2["ivi_total_amount"];
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
				echo "</div>";
				
			echo "</div>";
			$counter++;
			
			
	}








	//total
	echo "<div class='view_row'>";
		echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>".$counter."</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000><b>Total</b></font>";
			echo "</font><br>";			
		echo "</div>";

	
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Quantity</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_quantity,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>GST Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_discount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
			
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_amount,2,".",",")."</b></font>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
		echo "</div>";
		
	echo "</div>";

	
echo "</div>";





echo "<br>&nbsp;<br>&nbsp;";
echo "<br>&nbsp;<br>&nbsp;";



//====================================================Summary by Product==============================================================


echo "<div class='data_display_content'>";	

echo "<font size=3 style='float:left;padding:15px;color:#000;'><b><span class='fa fa-cubes'></span> Sales Summary by Device/Product</b></font>";
echo "<br>";
echo "<input style='float:left;' type=button class='button button-blue' value='Print' onclick=\"print_page('print/sales_report/?start=".$selected_date_start_simple."&end=".$selected_date_end_simple."&part=SUM_DEV_PDT')\">";




	echo "<div class='view_list_title_row'>";
		echo "<div class='view_list_title_content'>";
			
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Product Code</b>";
			echo "</div>";
			
			echo "<div style='width:200px;' class='view_list_title_content_object'  >";
				echo "<b>Product Name</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Quantity</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Discount</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Amount</b>";
			echo "</div>";
			
		echo "</div>";
	echo "</div>";
			
			
			
			
			
	$query="select 
			
			sum(ivi_quantity) as ivi_quantity,
			sum(ivi_discount_total) as ivi_discount_total,
			sum(ivi_total_amount) as ivi_total_amount,
			
			
			device_id, ivi_product_code, ivi_product_name
			
			
			from t_".$key_table_prefix."_invoice_item
			where ivi_status = 'POSTED'
			and created_date >= '".$selected_date_start."'
			and created_date <= '".$selected_date_end."'
			group by device_id, ivi_product_code, ivi_product_name
			order by device_id, ivi_product_code, ivi_product_name
			
			";		
	$result = mysqli_query($dbconn,$query);
	while($line = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		
		$report_data_product[$line["device_id"]][$line["ivi_product_code"]] = $line;		
		
	}
	
			

	
			$sum_product_quantity=0;
			$sum_product_discount=0;
			$sum_product_amount=0;
			
			
	$counter=1;
	foreach($report_data_product as $device_key => $report_data_product2)
	{
		
		echo "<div class='view_row'>";
			echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'></font>";
				echo "<font class='view_row_content_text'>";
					echo "<font color=#000>".$device_data[$device_key]."</font>";
				echo "</font><br>";			
			echo "</div>";
		
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
			echo "</div>";
			
		echo "</div>";
		
		
		$sub_sum_product_quantity=0;
		$sub_sum_product_discount=0;
		$sub_sum_product_amount=0;
		
		
		
		foreach($report_data_product[$device_key] as $product_code => $report_data_product3)
		{
	
			echo "<div class='view_row'>";
				echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$counter."</font>";
					echo "<font class='view_row_content_text'>";
						echo "".$product_code."";
					echo "</font><br>";			
				echo "</div>";
			
				echo "<div style='width:200px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Product Name</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_product_name"];
						
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Before GST</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_quantity"];
						$sum_product_quantity = $sum_product_quantity + $report_data_product3["ivi_quantity"];
						$sub_sum_product_quantity = $sub_sum_product_quantity + $report_data_product3["ivi_quantity"];
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Discount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_discount"];
						$sum_product_discount = $sum_product_discount + $report_data_product3["ivi_discount"];
						$sub_sum_product_discount = $sub_sum_product_discount + $report_data_product3["ivi_discount"];
					echo "</font><br>";			
				echo "</div>";
				
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_total_amount"];
						$sum_product_amount = $sum_product_amount+ $report_data_product3["ivi_total_amount"];
						$sub_sum_product_amount = $sub_sum_product_amount+ $report_data_product3["ivi_total_amount"];
					echo "</font><br>";			
				echo "</div>";
				
					
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
				echo "</div>";
				
			echo "</div>";
			$counter++;
			
		}
		
		
		
			//sub_total
			echo "<div class='view_row'>";
				echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$counter."</font>";
					echo "<font class='view_row_content_text'>";
					
					echo "</font><br>";			
				echo "</div>";
			
				echo "<div style='width:200px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'></font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000><b>Sub Total</b></font>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Quantity</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_quantity,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>GST Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_discount,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
					
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_amount,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
				echo "</div>";
				
			echo "</div>";
			
			
			
			
			
	}


	//total
	echo "<div class='view_row'>";
		echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>".$counter."</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000><b>Total</b></font>";
			echo "</font><br>";			
		echo "</div>";
	
		echo "<div style='width:200px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'></font>";
			echo "<font class='view_row_content_text'>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Quantity</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_quantity,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>GST Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_discount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
			
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_amount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
		echo "</div>";
		
	echo "</div>";
	
echo "</div>";



echo "<br>&nbsp;<br>&nbsp;";
echo "<br>&nbsp;<br>&nbsp;";

//====================================================Summary by Staff==============================================================


echo "<div class='data_display_content'>";	
echo "<font size=3 style='float:left;padding:15px;color:#000;'><b><span class='fa fa-cubes'></span> Sales Summary by Staff/Product</b></font>";
echo "<br>";
echo "<input style='float:left;' type=button class='button button-blue' value='Print' onclick=\"print_page('print/sales_report/?start=".$selected_date_start_simple."&end=".$selected_date_end_simple."&part=SUM_STF_PDT')\">";

	echo "<div class='view_list_title_row'>";
		echo "<div class='view_list_title_content'>";
			
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Staff Code</b>";
			echo "</div>";
			
			echo "<div style='width:200px;' class='view_list_title_content_object'  >";
				echo "<b>Product Name</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Quantity</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Discount</b>";
			echo "</div>";
			
			echo "<div style='width:100px;' class='view_list_title_content_object'  >";
				echo "<b>Total Amount</b>";
			echo "</div>";
			
		echo "</div>";
	echo "</div>";
			
			
			
	unset($report_data_salesperson);		
			
	$query="select 
			
			sum(ivi_quantity) as ivi_quantity,
			sum(ivi_discount_total) as ivi_discount_total,
			sum(ivi_total_amount) as ivi_total_amount,
			
			
			ivi_salesperson, ivi_product_code, ivi_product_name
			
			
			from t_".$key_table_prefix."_invoice_item
			where ivi_status = 'POSTED'
			and created_date >= '".$selected_date_start."'
			and created_date <= '".$selected_date_end."'
			group by ivi_salesperson, ivi_product_code, ivi_product_name
			order by ivi_salesperson, ivi_product_code, ivi_product_name
			
			";		
	$result = mysqli_query($dbconn,$query);
	while($line = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		
		$report_data_salesperson[$line["ivi_salesperson"]][$line["ivi_product_code"]] = $line;		
		
	}
	
			

	
		$sum_product_quantity=0;
		$sum_product_discount=0;
		$sum_product_amount=0;
		
		
			
	$counter=1;
	foreach($report_data_salesperson as $salesperson_key => $report_data_product2)
	{
		
		echo "<div class='view_row'>";
			echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
				echo "<font class='view_row_content_label'></font>";
				echo "<font class='view_row_content_text'>";
					echo "<font color=#000>".$salesperson_key." ".$staff_data[$salesperson_key]."</font>";
				echo "</font><br>";			
			echo "</div>";
		
			echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
			echo "</div>";
			
		echo "</div>";
		
	
		$sub_sum_product_quantity=0;
		$sub_sum_product_discount=0;
		$sub_sum_product_amount=0;
		
		
		
		
		foreach($report_data_salesperson[$salesperson_key] as $product_code => $report_data_product3)
		{
	
			echo "<div class='view_row'>";
				echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$counter."</font>";
					echo "<font class='view_row_content_text'>";
						echo "".$product_code."";
					echo "</font><br>";			
				echo "</div>";
			
				echo "<div style='width:200px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Product Name</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_product_name"];
						
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Before GST</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_quantity"];
						$sub_sum_product_quantity = $sub_sum_product_quantity + $report_data_product3["ivi_quantity"];
						$sum_product_quantity = $sum_product_quantity + $report_data_product3["ivi_quantity"];
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Discount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_discount"];
						$sub_sum_product_discount = $sub_sum_product_discount + $report_data_product3["ivi_discount"];
						$sum_product_discount = $sum_product_discount + $report_data_product3["ivi_discount"];
					echo "</font><br>";			
				echo "</div>";
				
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo $report_data_product3["ivi_total_amount"];
						$sub_sum_product_amount = $sub_sum_product_amount+ $report_data_product3["ivi_total_amount"];
						$sum_product_amount = $sum_product_amount+ $report_data_product3["ivi_total_amount"];
					echo "</font><br>";			
				echo "</div>";
				
					
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
				echo "</div>";
				
			echo "</div>";
			$counter++;
			
		}
		
		
		
			//sub_total
			echo "<div class='view_row'>";
				echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>".$counter."</font>";
					echo "<font class='view_row_content_text'>";
					
					echo "</font><br>";			
				echo "</div>";
			
				echo "<div style='width:200px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'></font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000><b>Sub Total</b></font>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Quantity</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_quantity,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>GST Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_discount,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
					
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
					echo "<font class='view_row_content_label'>Total Amount</font>";
					echo "<font class='view_row_content_text'>";
						echo "<font color=#000 ><b>".number_format($sub_sum_product_amount,2,".",",")."</b>";
					echo "</font><br>";			
				echo "</div>";
				
				echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
				echo "</div>";
				
			echo "</div>";
			
			
			
			
	}



	
	
	
	
	
	//total
	echo "<div class='view_row'>";
		echo "<div style='width:100px;text-align:left;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>".$counter."</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000><b>Total</b></font>";
			echo "</font><br>";			
		echo "</div>";
	
		echo "<div style='width:200px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'></font>";
			echo "<font class='view_row_content_text'>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Quantity</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_quantity,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>GST Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_discount,2,".",",")."</b>";
			echo "</font><br>";			
		echo "</div>";
			
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";
			echo "<font class='view_row_content_label'>Total Amount</font>";
			echo "<font class='view_row_content_text'>";
				echo "<font color=#000 ><b>".number_format($sum_product_amount,2,".",",")."</b></font>";
			echo "</font><br>";			
		echo "</div>";
		
		echo "<div style='width:".$cell_width."px;text-align:right;' class='view_row_content_object'  >";	
		echo "</div>";
		
	echo "</div>";
	
echo "</div>";


echo "<br>&nbsp;<br>&nbsp;";
echo "<br>&nbsp;<br>&nbsp;";
echo "<br>&nbsp;<br>&nbsp;";

    echo "</section>";





?>
<?php include "../../includes/end_ajax.php";?>
