<?php


session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");

include("function2.inc.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");




$actionmode = $_GET['actionmode'];
if($actionmode == "deletesalesmencommission") {
	$salesmencommissionid = $_GET['salesmencommissionid'];
	$salesmen_code = $_GET['salesmen_code'];
	

$query = "delete from salesmen_budget where id = '".$salesmencommissionid."'";
$query2 = "delete from salesmen_budgetdt where salesmen_budgetid = '".$salesmencommissionid."'";
$insert = mysql_query($query);	
$insert2 = mysql_query($query2);

draw_salesmencommission($connection,$salesmen_code);

}


if($actionmode == "insertsalesmenbudget") {

	$salesmen_code = $_GET['salesmen_code'];
	$immediate_payout = $_GET['immediate_payout'];
	$salesmen_mst = $_GET['salesmen_mst'];
	$salesmen_cst = $_GET['salesmen_cst'];
	$salesmen_startdate = $_GET['salesmen_startdate'];
	$salesmen_enddate = $_GET['salesmen_enddate'];
	$monthly_achievmentfrom = $_GET['monthly_achievmentfrom'];
	$monthly_achievmentto = $_GET['monthly_achievmentto'];
	$salesmen_sip = $_GET['salesmen_sip'];
	

$query = "insert into salesmen_budget (salesmen_code,Immediate_payout,salesmen_mst,salesmen_cst,salesmen_startdate,salesmen_enddate,monthly_achievmentfrom,monthly_achievmentto,salesmen_sip) values ("
.GetSQLValueString($salesmen_code,'text').","
.GetSQLValueString($immediate_payout,'double').","
.GetSQLValueString($salesmen_mst,'double').","
.GetSQLValueString($salesmen_cst,'double').","
.GetSQLValueString(explodeword($salesmen_startdate),'date').","
.GetSQLValueString(explodeword($salesmen_enddate),'date').","
.GetSQLValueString($monthly_achievmentfrom,'double').","
.GetSQLValueString($monthly_achievmentto,'double').","
.GetSQLValueString($salesmen_sip,'double').")";

$insert = mysql_query($query);	
$insertid =  mysql_insert_id();	

$query2 = "select salesmen_code,Immediate_payout,salesmen_mst,salesmen_cst,salesmen_startdate,salesmen_enddate,monthly_achievmentfrom,monthly_achievmentto,salesmen_sip from salesmen_budget where id = '$insertid'";
$result = mysql_query($query2);
$row2 = mysql_fetch_row($result);
$from = $row2[6];
$to = $row2[7];
$i = 0;
$sip1 = $row2[8]/100;
while ($from <= $to) {
	 $i++;
     
	$sip = ($sip1 * $i/100 ) * $row2[2]; 
	$cip = ($sip1 * $i/100 ) * $row2[3]; 
	 
$query = "insert into salesmen_budgetdt (salesmen_budgetid,monthly_achievement,salesmen_budgetdt_sip,salesmen_budgetdt_cip) values ("

.GetSQLValueString($insertid,'int').","
.GetSQLValueString($from,'double').","
.GetSQLValueString($sip,'double').","
.GetSQLValueString($cip,'double').")";


$insert = mysql_query($query);	


   $from++;
}

draw_salesmencommission($connection,$salesmen_code);
}

if($actionmode == "editsalesmenbudget") {
	$salesmensbudgetid = $_GET['salesmensbudgetid'];
	$salesmen_code = $_GET['salesmen_code'];
	$immediate_payout = $_GET['immediate_payout'];
	$salesmen_mst = $_GET['salesmen_mst'];
	$salesmen_cst = $_GET['salesmen_cst'];
	$salesmen_startdate = $_GET['salesmen_startdate'];
	$salesmen_enddate = $_GET['salesmen_enddate'];
	$monthly_achievmentfrom = $_GET['monthly_achievmentfrom'];
	$monthly_achievmentto = $_GET['monthly_achievmentto'];
	$salesmen_sip = $_GET['salesmen_sip'];
	

$query = "update  salesmen_budget set salesmen_code="
.GetSQLValueString($salesmen_code,'text').",Immediate_payout="
.GetSQLValueString($immediate_payout,'double').",salesmen_mst="
.GetSQLValueString($salesmen_mst,'double').",salesmen_cst="
.GetSQLValueString($salesmen_cst,'double').",salesmen_startdate="
.GetSQLValueString(explodeword($salesmen_startdate),'date').",salesmen_enddate="
.GetSQLValueString(explodeword($salesmen_enddate),'date').",monthly_achievmentfrom="
.GetSQLValueString($monthly_achievmentfrom,'double').",monthly_achievmentto="
.GetSQLValueString($monthly_achievmentto,'double').",salesmen_sip="
.GetSQLValueString($salesmen_sip,'double')." where id = '$salesmensbudgetid'";
$insert = mysql_query($query);	
$query3 = "delete from salesmen_budgetdt where salesmen_budgetid = '".$salesmensbudgetid."'";

$delete2 = mysql_query($query3);

$query2 = "select salesmen_code,Immediate_payout,salesmen_mst,salesmen_cst,salesmen_startdate,salesmen_enddate,monthly_achievmentfrom,monthly_achievmentto,salesmen_sip from salesmen_budget where id = '$salesmensbudgetid'";
$result = mysql_query($query2);
$row2 = mysql_fetch_row($result);
$from = $row2[6];
$to = $row2[7];
$i = 0;
$sip1 = $row2[8]/100;
while ($from <= $to) {
	 $i++;
     
	$sip = ($sip1 * $i/100 ) * $row2[2]; 
	$cip = ($sip1 * $i/100 ) * $row2[3]; 
	 
$query = "insert into salesmen_budgetdt (salesmen_budgetid,monthly_achievement,salesmen_budgetdt_sip,salesmen_budgetdt_cip) values ("

.GetSQLValueString($salesmensbudgetid,'int').","
.GetSQLValueString($from,'double').","
.GetSQLValueString($sip,'double').","
.GetSQLValueString($cip,'double').")";


$insert = mysql_query($query);	


   $from++;
}


draw_salesmencommission($connection,$salesmen_code);
echo"[BRK]";
draw_salesmenbudgetdt($connection,$salesmensbudgetid);
}



if($actionmode=='changerelationtodetail'){ //


$vid = $_GET['vid'];

draw_salesmenbudgetdt($connection,$vid);

}

function GetSQLValueString($theValue, $theType) 
{
  $theValue = trim($theValue);	
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}

function explodeword($date)
{
if ($date!=''){
$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

return $trim;
}
else return null;
}

?>