<?php session_start();
//Fetching Values from URL


require_once __DIR__."/allfunctions.php";
$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());



if(isset($_SESSION["proditemarrayinventory"]))
{
    $itemarrayprod = $_SESSION["proditemarrayinventory"];
}
else
{
 $itemarrayprod = array();
}

if(isset($_SESSION["proditemqty"]))
{
  $itemarrayquantity = $_SESSION["proditemqty"];
}
else
{
  $itemarrayquantity = array();
}


$materialdata = "";
$ctr2 = count($itemarrayprod);
//echo $itemarray;



	if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
	{
	   $itemarray = array();
	   $memberid = "";
	  $type = "";
	  $fname = "";
	}
	else
	{
	  $memberid = $_SESSION['MEMBER_ID'];
	  $type = $_SESSION['TYPE'];
	  $fname = $_SESSION['FIRST_NAME'];
	}


//$address = $_POST['address'];
$dateToday = date('Y-m-d');
$status = "Added";
$refID = $_POST['refID'];
$soldby = $_POST['soldby'];
$agentlevel = $_POST['agentlevel'];
$agentcom = $_POST['agentcom'];
$refIDname = getRecord('name','test','id= "'.$refID.'"');
$agentID = getRecord('id','test','id= "'.$soldby.'"');
$agentName = getRecord('name','test','id= "'.$soldby.'"');
//$ref_id = getRecord('id','test','name= "'.$refID.'"');
//echo $ref_id;
            if($agentlevel == "Level 1")
            {
              $com1 = 50;
              $com2 = 0;
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 2")
            {
              $com1 = 20;
              $com2 = 50;
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 3")
            {
              $com1 = 15;
              $com2 = 20;
              $com3 = 50;
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 4")
            {
              $com1 = 10;
              $com2 = 15;
              $com3 = 20;
              $com4 = 50;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 5")
            {
              $com1 = 5;
              $com2 = 10;
              $com3 = 15;
              $com4 = 20;
              $com5 = 50;
            }
            elseif($agentlevel == "Level 6")
            {
              $com1 = 0;
              $com2 = 0;
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }



							$materialdata = "";
							$ctr2 = count($itemarrayprod);
							for ($c=0;$c<$ctr2;$c++)
							{
							//$materialdata = $itemarray[$c];
							//$materialdata = implode(",",$itemarray[$c]);
              if($agentlevel == "Level 1")
            {
              $com1 = 50*$itemarrayquantity[$c];
              $com2 = 0;
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 2")
            {
              $com1 = 20*$itemarrayquantity[$c];
              $com2 = 50*$itemarrayquantity[$c];
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 3")
            {
              $com1 = 15*$itemarrayquantity[$c];
              $com2 = 20*$itemarrayquantity[$c];
              $com3 = 50*$itemarrayquantity[$c];
              $com4 = 0;
              $com5 = 0;
            }
            elseif($agentlevel == "Level 4")
            {
              $com1 = 10*$itemarrayquantity[$c];
              $com2 = 15*$itemarrayquantity[$c];
              $com3 = 20*$itemarrayquantity[$c];
              $com4 = 50*$itemarrayquantity[$c];
              $com5 = 0;
            }
            elseif($agentlevel == "Level 5")
            {
              $com1 = 5*$itemarrayquantity[$c];
              $com2 = 10*$itemarrayquantity[$c];
              $com3 = 15*$itemarrayquantity[$c];
              $com4 = 20*$itemarrayquantity[$c];
              $com5 = 50*$itemarrayquantity[$c];
            }
            elseif($agentlevel == "Level 6")
            {
              $com1 = 0;
              $com2 = 0;
              $com3 = 0;
              $com4 = 0;
              $com5 = 0;
            }


              $total_sales=$itemarrayquantity[$c] * 350;
              $paymentstatus = "Pending";
							$insert_sql3 = "INSERT INTO sales(id,productID,amount,quantity,soldby,boughtby,datecreated,commisionsoldby,comissionboughtby,status,total_sales,level1,level2,level3,level4,level5,paymentstatus)
							VALUES(NULL,'".$itemarrayprod[$c]."','350','".$itemarrayquantity[$c]."', '".$refID."','".$soldby."','".$dateToday."','".$agentlevel."','".$agentcom."','1','" .$total_sales. "','".$com1."','".$com2."','".$com3."','".$com4."','".$com5."','".$paymentstatus."')";
							$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));


							if($rs)
							{
                $insert_sql4 = "INSERT INTO commission(id,datecreated,agentID,agentname,commissiontotal,level1,level2,level3,level4,level5,agentlevel,status,agentnamedown)
  							VALUES(NULL,'".$dateToday."','".$refID."', '".$refIDname."','".$total_sales."','".$com1."','".$com2."','".$com3."','" .$com4. "','".$com5."','".$agentlevel."','1','".$agentName."')";
  							$rs2 = mysqli_query($con,$insert_sql4) or die(mysqli_error($con));

  							  if($rs2)
    							{
                  echo 1;

                  unset($_SESSION["proditemarrayinventory"]);
                  unset($_SESSION["proditemqty"]);
                  }
                else
  							{
  								echo 0;
  							}
                //$itemarrayprod = $_SESSION["proditemarrayinventory"];
                //$itemarrayquantity = $_SESSION["proditemqty"];


							}
							else
							{
								echo 0;
							}

            }
?>
