<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/functions.php'); # general functions

$isEdit = FALSE;

if(isset($_REQUEST['shop_id']) && $_REQUEST['shop_id']!=''){		
	$shopId = mysqli_real_escape_string($con, trim($_REQUEST['shop_id']));	
} else {
	header('Location:index.php?page=manage_purchase_partner');
}

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));
	$editRs = mysqli_query($con, "SELECT * FROM tbl_admin_shop_incomes WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'No income added';
	}	
} 

if(isset($_POST['add'])) {
	
	if(strlen(trim($_POST['description'])) > 0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}	
		
	if(strlen(trim($_POST['amount'])) > 0) {
		$amount = trim($_POST['amount']);
		if(is_numeric($amount) && $amount>0){
			//ok
		} else {
			$error = 'Please enter a valid amount';
		}
	} else {
		$error = 'Please enter amount';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "INSERT INTO tbl_admin_shop_incomes(purchase_id, amount, description, date, added_by) VALUES (
		'".$shopId."',
		 '".mysqli_real_escape_string($con, $amount)."',
		 '".mysqli_real_escape_string($con, $description)."',
		 '".$curDate."',		 
		 '".mysqli_real_escape_string($con, $_SESSION['adminId'])."')";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
						
			$success = 'Income Added Successfully';
			header('Location:index.php?page=purchase_incomes&shop_id='.$shopId.'&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(strlen(trim($_POST['description'])) > 0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}	
		
	if(strlen(trim($_POST['amount'])) > 0) {
		$amount = trim($_POST['amount']);
		if(is_numeric($amount) && $amount>0){
			//ok
		} else {
			$error = 'Please enter a valid amount';
		}
	} else {
		$error = 'Please enter amount';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "UPDATE tbl_admin_shop_incomes SET amount = '".mysqli_real_escape_string($con, $amount)."', 
		description = '".mysqli_real_escape_string($con, $description)."', 
		updated_by = '".mysqli_real_escape_string($con, $_SESSION['adminId'])."', 
		updated_on = '".$curDate."' WHERE id = '".$editId."'";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
						
			$success = 'Income Updated Successfully';
			header('Location:index.php?page=purchase_incomes&shop_id='.$shopId.'&success='.$success);
			exit();			
		}				
	} 
}

//get total amount received from shop
$incomesRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(commission_amount) AS amount FROM `tbl_purchase_order` WHERE `partner_id` = '".$shopId."' AND sold = 'yes'")); 
$receivedRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(amount) AS amount FROM `tbl_admin_shop_incomes` WHERE `purchase_id` = '".$shopId."'"));
$totalAmountPending = $incomesRec['amount']-$receivedRec['amount'];



?>