<?php
global $con;
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("functions.php");
}else{
	include_once("connect.php");
	
}

$function = $_REQUEST['f'];
if($function == "getCustomerOverdueBalance"){
	
	getCustomerOverdueBalance($_REQUEST['an']);
}elseif($function == "getSalesmanWeeklyPlanner"){
	getSalesmanWeeklyPlanner($_REQUEST['sm'], $_REQUEST['fromDate'], $_REQUEST['toDate']);
}elseif($function == "deleteWeeklyPlannerVisit"){
	deleteWeeklyPlannerVisit($_REQUEST['id']);
}elseif($function == "saveWeeklyPlannerVisit"){
	saveWeeklyPlannerVisit($_REQUEST);
}
function getCustomerOverdueBalance($accountNumber){
	if($accountNumber === "" || is_null($accountNumber)){
		echo 0;
		return;
	}else{
		global $con;
		$customer = new Customer($_REQUEST['an']);
		$invoices = $customer->getInvoices();
		$lineCount = 0;
		$totalDue = 0;
		foreach($invoices as $invoice){
			$days = ceil((time() - strtotime($invoice['InvoiceDate']))/(24*60*60));
			//$discount = ($days>45?0:round($invoice['Subtotal']*.05,2));
			$discount = 0;
			$invoiceTotal = $invoice['Subtotal']-$discount+$invoice['SalesTax'];
			//only add up older that 45 invoices
			if($days > 44){
				$totalDue+= $invoiceTotal;	
			}
			
			
			$lineCount++;
		}
		echo $totalDue;
	}
	
}
function getSalesmanWeeklyPlanner($salesman, $fromDate, $toDate){
	global $con;
	$query = "INSERT IGNORE INTO WEEKLY_PLANNER_HEAD (SalesmanID, WeekStart, WeekEnd) VALUES ('$salesman','" . date("Y-m-d", strtotime($fromDate)) . "','" . date("Y-m-d", strtotime($toDate)) . "')";
	mysqli_query($con,$query);
	//$query = "SELECT *,WEEKLY_PLANNER_DETAIL.id DetailID, WEEKLY_PLANNER_HEAD.id WeekID FROM WEEKLY_PLANNER_HEAD LEFT JOIN WEEKLY_PLANNER_DETAIL ON(WEEKLY_PLANNER_HEAD.id = WEEKLY_PLANNER_DETAIL.WPID) WHERE WeekStart = '" . date("Y-m-d", strtotime($fromDate)) . "' AND WeekEnd = '" . date("Y-m-d", strtotime($toDate)) . "' AND SalesmanID LIKE '" . $salesman . "'";
	$query = "SELECT WEEKLY_PLANNER_HEAD.*, WEEKLY_PLANNER_DETAIL.* ,WEEKLY_PLANNER_DETAIL.id DetailID, WEEKLY_PLANNER_HEAD.id WeekID, IF(CashAmount+CheckAmount = 0, TRUE, FALSE) MadeReminder, IF(CashAmount+CheckAmount,TRUE,FALSE) MadePayment , IF(SalesmanOrder,TRUE,FALSE) MadeOrder, IF(PickupDate,TRUE,FALSE) MadePickup
FROM 
	WEEKLY_PLANNER_HEAD 
LEFT JOIN WEEKLY_PLANNER_DETAIL ON(WEEKLY_PLANNER_HEAD.id = WEEKLY_PLANNER_DETAIL.WPID) 
LEFT JOIN PAYMENT_HEAD 
	ON(PAYMENT_HEAD.AccountNumber = WEEKLY_PLANNER_DETAIL.AccountNumber 
		AND PAYMENT_HEAD.PaymentDate 
			BETWEEN DATE_ADD(WeekStart, INTERVAL Weekday-1 DAY) AND DATE_ADD(WeekStart, INTERVAL Weekday DAY)
	) 
LEFT JOIN ORDER_HEAD
	ON(ORDER_HEAD.AccountNumber = WEEKLY_PLANNER_DETAIL.AccountNumber 
		AND ORDER_HEAD.OrderDate 
			BETWEEN DATE_ADD(WeekStart, INTERVAL Weekday-1 DAY) AND DATE_ADD(WeekStart, INTERVAL Weekday DAY)
	) 
LEFT JOIN PICKUP_HEAD
	ON(PICKUP_HEAD.AccountNumber = WEEKLY_PLANNER_DETAIL.AccountNumber 
		AND PICKUP_HEAD.PickupDate 
			BETWEEN DATE_ADD(WeekStart, INTERVAL Weekday-1 DAY) AND DATE_ADD(WeekStart, INTERVAL Weekday DAY)
	) 

WHERE WeekStart = '" . date("Y-m-d", strtotime($fromDate)) . "' AND WeekEnd = '" . date("Y-m-d", strtotime($toDate)) . "' AND  WEEKLY_PLANNER_HEAD.SalesmanID LIKE '" . $salesman . "' GROUP BY AccountNumber, Weekday ORDER BY WEEKLY_PLANNER_DETAIL.id;";
	//echo $query;
	$result = mysqli_query($con,$query);
	@$planner = new stdClass();
	
	$planner->Salesman = $salesman;
	$planner->FromDate = $fromDate;
	$planner->ToDate = $toDate;
	$planner->WeekNumber = date("W", strtotime($fromDate));
	$planner->Visits = [];
	
	while($row = mysqli_fetch_array($result)){
		$planner->WeekID = $row['WeekID'];
		if(!is_null($row['WPID'])){
			$visit = new stdClass();
			$visit->VisitID = $row['DetailID'];
			$visit->AccountNumber = $row['AccountNumber'];
			$customer = new Customer($row['AccountNumber']);
			$visit->Name = $customer->Name;
			$visit->OverDue = $row['OverDue'];
			$visit->Comments = $row['Comments'];
			$visit->Weekday = $row['Weekday'];
			$visit->MadeReminder = $row['MadeReminder'];
			$visit->MadePayment = $row['MadePayment'];
			$visit->MadePickup = $row['MadePickup'];
			$visit->MadeOrder = $row['MadeOrder'];
			
			array_push($planner->Visits, $visit);
					
		}
		
	}
	echo json_encode($planner);
}
function saveWeeklyPlannerVisit($data){
	global $con;
	
		
		$query = "INSERT INTO WEEKLY_PLANNER_DETAIL (WPID, Weekday, AccountNumber, OverDue, Comments) VALUES (" . $data['WPID'] . "," . $data['Weekday'] . ",'" . $data['AccountNumber'] . "','" . $data['OverDue'] . "','" . $data['Comments'] . "')";
		
		$result = mysqli_query($con, $query);
		
		if($result){
			echo "true";
		}
	
	
	
}
function deleteWeeklyPlannerVisit($wpid){
	global $con;
	$query = "DELETE FROM WEEKLY_PLANNER_DETAIL WHERE id = " . $wpid;
	//echo $query;
	if(mysqli_query($con, $query)){
		echo "true";
	}else{
		echo "false";
	}
	
}
?>