<?php $action=$_REQUEST['action'];?>

<Script language="javascript">
function checkKeyCode(evt)
{ var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if(event.keyCode==116)
  { evt.keyCode=0;
    return false
  }
}
document.onkeydown=checkKeyCode;
</script>

<script language="javascript">
function formcheck()
{ if(document.myform.member_id.value=="")
  { alert("Please Enter Associate ID!");
	document.myform.member_id.focus()
	return false;
  }
  if(document.myform.bill_type.value=="")
  { alert("Please Select Bill Type!");
	document.myform.bill_type.focus()
	return false;
  }
  return true;
}
</script>

<script language="javascript">
function duplicate_domain_check(member_id)
{ xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  { alert ("Browser does not support HTTP Request")
    return
  }
  var url="record.php";
  url=url+"?member_id="+member_id
  url=url+"&sid="+Math.random()
  xmlHttp.onreadystatechange=stateChanged_answer 
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

function stateChanged_answer() 
{ if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { document.getElementById("msg").innerHTML='<font size=1>'+xmlHttp.responseText+'</font>';
  }
  else
  { document.getElementById("msg").innerHTML="<img src='images/loadingAnimation.gif' border=0  height=5 width=20 >";
  }
}

function GetXmlHttpObject()
{ var xmlHttp=null;
  try
  { // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  { //Internet Explorer
    try
    { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function test()
{ alert('as');
}
</script>
<?php
							$stock_id=$_SESSION['admin_id']; 
				?>
</nav>
               </div>
                <!--  END TOPBAR  -->
            </ul>
            <ul class="navbar-item flex-row ml-auto">

                <li class="nav-item align-self-center search-animated">

                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search toggle-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>

                    <form class="form-inline search-full form-inline search" role="search">

                        <div class="search-bar">

                            <input type="text" class="form-control search-form-control  ml-lg-auto" placeholder="Search...">

                        </div>

                    </form>

                </li>

            </ul>

            <ul class="navbar-item flex-row nav-dropdowns">                    

                <li class="nav-item dropdown notification-dropdown">

                    <a href="javascript:void(0);" class="nav-link dropdown-toggle" id="notificationDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">

                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bell"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path><path d="M13.73 21a2 2 0 0 1-3.46 0"></path></svg><span class="badge badge-success"></span>

                    </a>

                    <div class="dropdown-menu position-absolute" aria-labelledby="notificationDropdown">

                        <div class="notification-scroll">

						  <?

							 $query_not = $mysqli->prepare("select heading, content from notice_shop");
											$query_not->execute();
													$query_not->bind_result($heading, $content); 
		   ?>

		   <?
		                                 while ($query_not->fetch()){

		                                  ?>

                            <div class="dropdown-item">

                                <div class="media server-log">

                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-server"><rect x="2" y="2" width="20" height="8" rx="2" ry="2"></rect><rect x="2" y="14" width="20" height="8" rx="2" ry="2"></rect><line x1="6" y1="6" x2="6" y2="6"></line><line x1="6" y1="18" x2="6" y2="18"></line></svg>

                                    <div class="media-body">

                                        <div class="data-info">

                                            <h6 class=""><? echo $heading;?></h6>

                                            <p class=""><? echo $content;?></p>

                                        </div>

                                        <div class="icon-status">

                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>

                                        </div>

                                    </div>

                                </div>

                            </div>

							<? } ?>                                                

                        </div>

                    </div>

                </li>						

                <li class="nav-item dropdown user-profile-dropdown order-lg-0 order-1">

                    <a href="javascript:void(0);" class="nav-link dropdown-toggle user" id="user-profile-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">

                        <div class="media">

                            <img src="assets/img/90x90.jpg" class="img-fluid" alt="admin-profile">

                        </div>

                    </a>

                    <div class="dropdown-menu position-absolute" aria-labelledby="userProfileDropdown">

                        <div class="user-profile-section">

                            <div class="media mx-auto">

                                <div class="media-body">

                                    <h5>Name : <? echo $name;?></h5>

                                   
                                </div>

                            </div>

                        </div>

                        <div class="dropdown-item">

                            <a href="update_profile.php">

                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg> <span>Profile</span>

                            </a>

                        </div>                    

                        <div class="dropdown-item">

                            <a href="logout.php">

                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-log-out"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg> <span>Log Out</span>

                            </a>

                        </div>

                    </div>

                </li>

            </ul>

        </header>

    </div>

    <!--  END NAVBAR  -->
    <!--  BEGIN MAIN CONTAINER  -->
    <div class="main-container" id="container">
        <div class="overlay"></div>

        <div class="search-overlay"></div>      

        <!--  BEGIN CONTENT PART  -->

        <div id="content" class="main-content">

            <div class="layout-px-spacing">

                <div class="page-header">

                    <div class="page-title">

                        <h3><? echo $name;?></h3>

                    </div>                 

                    <div class="toggle-switch">

                        <label class="switch s-icons s-outline  s-outline-secondary">

                            <input type="checkbox" checked="" class="theme-shifter">

                            <span class="slider round">

                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-sun"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>

                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-moon"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>

                            </span>

                        </label>

                    </div>

                </div>

                <div class="row layout-top-spacing">        

     <div id="basic" class="col-lg-12 col-sm-12 col-12 layout-spacing">

                            <div class="statbox widget box box-shadow">

                                <div class="widget-header">                                

                                    <div class="row">

                                        <div class="col-xl-12 col-md-12 col-sm-12 col-12">

                                            <h4>Activation </h4>

                                        </div>

                                    </div>

                                </div>

								<?php
						  if($_SESSION['msg_box2']!="")
						  {?>

                                <div class="alert alert-light-success border-0 mb-4" role="alert"> <font color="#FF3300"><? echo $_SESSION['msg_box2'];?> </font></div>
						<?	} 
						 unset($_SESSION['msg_box2']);
						 if($action==""){
							$stock_id=$_SESSION['admin_id'];
							$z=0;
							$crr=0;
							$query = $mysqli->prepare("select prod_id,current_stock from stock_record where stock_id=? and current_stock>?");
							$query->bind_param('ss', $stock_id,$crr);
							$query->execute();
							$query->bind_result($prod_id,$current_stock);
							while($query->fetch())
							{ $z++;
							  $prd[$z]=$prod_id;
							  $crt_stk[$z]=$current_stock;
							} 
							?>
							<td align="left" colspan="2"><b><b>Select Product.</b>&nbsp;</b>&nbsp;
                                        <select id="sel"  data-placeholder="Choose a name..." class="ch">
		                                    <option value="" ></option>
									<?	     for($kk=1;$kk<=$z;$kk++)
                                       		 { $prod_id=$prd[$kk];
											   $current_stock=$crt_stk[$kk];							   
											   $query_prd = $mysqli->prepare("select name,batch_no,expiry_date,mrp from product_resale where id=?");
											   $query_prd->bind_param('s', $prod_id);
											   $query_prd->execute();
											   $query_prd->bind_result($pname,$batch_no,$expiry_date,$mrp);
											   while($query_prd->fetch())
											   { ?>
												 <option value="<? echo $prod_id;?>">
													<? echo $pname;?> Batch No : 
													<? echo $batch_no;?> Expiry : 
													<? echo $expiry_date;?> MRP : 
													<? echo $mrp;?> Stock : 
													<? echo $current_stock;?>
												</option>
												<? } ?>
            <? } ?>
</select>	
<b>Enter Quantity</b>
<input type="text" value="1" placeholder="Enter Quantity" id="quan">
        <button onclick="add_prod()" type="button">Add</button>	
 <form action="manage_resale.php?action=before_process" method="post" name="myform" enctype="multipart/form-data" onSubmit="return formcheck();">
                      <table class="table table-bordered mb-4">
										<tr>
<td align="left" colspan="2"><b>Enter Member ID.</b>&nbsp;<input type="text" id="member_id" name="member_id" onblur=duplicate_domain_check(this.value)  /> &nbsp;<span class=red id=msg></span></td>
</tr>

<td colspan='10'>
	<div id="inp-grp"></div>
</td>

<tr>
	<td>Total:</td>
	<td colapan="3"><b><span id="ttl">0</span></b></td>
</tr>

<tr>
	<td width="117" align="left"><b>Bill Type</b></td>
	<td width="447" align="left"><select name="bill_type">
					<option value="">Please Select</option>
					<option value="state">State</option>
					<option value="central">central</option>
					</select>
	</td>
</tr>

<tr>
	<td align="left"><b>GST No</b></td>
	<td align="left"><input type="text" name="gst_no" id="gst_no">
</td>

</tr>
<input type="hidden" name="discount" id="discount" value="0">

<tr>
	<td align="left"><b>Entry Date</b></td>
	<td align="left"><input type="text" name="ent_date" id="ent_date" value="<?php echo date("d-m-Y");?>" readonly>
												(dd-mm-yyyy) 
	</td>
</tr>

<tr>
	<td align="right" colspan="2"><input type="submit" name="submit" value="Submit.."></td>
</tr>

</table>
</form>		
<? } ?>
<? if($action=="before_process"){ 
$member_id=$_REQUEST['member_id'];
$bill_type=$_REQUEST['bill_type'];
$delevery=$_REQUEST['delevery'];
$gst_no=$_REQUEST['gst_no'];
$qry_mem = $mysqli->prepare("select title,name from member where member_id=?");
$qry_mem->bind_param('s', $member_id);								
$qry_mem->execute();
$qry_mem->bind_result($title,$mem_name);
while ($qry_mem->fetch())
{ $title=$title;
  $mem_name=$mem_name;
}

//$name=$_REQUEST['name'];
$ent_date=$_REQUEST['ent_date'];

$qty=$_REQUEST['qty'];
$item=$_REQUEST['item'];
$dis=$_REQUEST['dis'];
$del=$_REQUEST['del'];

$discount=$_REQUEST['discount'];
$out_stock=0;
$stock_id=$_SESSION['admin_id'];

$d=0;
$query_itm1 = $mysqli->prepare("select current_stock from stock_record where stock_id=? and prod_id=?");
foreach  ($item as $v)
{
	$query_itm1->bind_param('ss',$stock_id,$v);								
	$query_itm1->execute();
	$query_itm1->bind_result($current_stock1);
	while($query_itm1->fetch())
	{
	$current_stock1=$current_stock1;
	}
	if($current_stock1<$qty[$d]){  $out_stock=1;}
	$d++;
}

if($out_stock==1)
{ $_SESSION['msg_box2']="Out Of Stock. Try Again.";
// $_SESSION['class_notification']="notification success png_bg";	
header("location:manage_resale.php?tab_id=tab1&default=manage_resale&member_id=$member_id");
}
else
{ $qq_spot = $mysqli->prepare('SELECT * FROM member where member_id=?');
  $qq_spot->bind_param('s', $member_id);
  $qq_spot->execute();
  $qq_spot->store_result();
  $no_spot=$qq_spot->num_rows;
  if($no_spot==0)
  { $_SESSION['msg_box2']="No Such Member ID Found. Try Again.";
 // $_SESSION['class_notification']="notification success png_bg";	
    header("location:manage_resale.php?tab_id=tab1&default=manage_resale&member_id=$member_id");
  }
  else
  { $query_sts = $mysqli->prepare("select active from member where member_id=?");
	$query_sts->bind_param('s', $member_id);								
    $query_sts->execute();
    $query_sts->bind_result($active);
	while($query_sts->fetch())
	{ $active=$active;
	}	
    /*
    if($active=="n")
    { $_SESSION['msg_box2']="Please Activate This Member First. Try Again.";
   // $_SESSION['class_notification']="notification success png_bg";	
      header("location:manage_resale.php?tab_id=tab1&default=manage_resale&member_id=$member_id");
    }
    else
    {*/
      ?>
     <form action="manage_resale.php?action=add_process" method="post" name="myform4" enctype="multipart/form-data">
     <table class="table table-striped table-bordered zero-configuration" >
     <tr>
		<td bgcolor="#0099FF" align="left" colspan="8"><b>  Bill Details </b></td>
	</tr>

	<tr>
		<td colspan="6"><b>Distributor Name</b></td>
		<td colspan="6"><b><? echo $mem_name;?>  </b></td>
	</tr>

	<tr>
		<td colspan="6"><b>Date </b></td>
		<td colspan="6"><b><? echo $ent_date;?> </b></td>
	</tr>

	<tr>
		<td><b>Description Of Goods</b></td>
		<td><b>HSN Code</b></td>
		<td><b>QTY</b></td>
		<td><b>Batch No</b></td>
		<td><b>UOM</b></td>
		<td><b>DP</b></td>
		<td><b>Gross Value</b></td>
		<td><b>MRP</b></td>
		<td><b>Discount</b></td>
		<td><b>Taxable Value</b></td>
		<td><table border="1"><tr><td colspan="2"><b>CGST</b></td></tr><tr><td><b>Rate</b></td><td><b>Amount</b></td></tr></table></td>
		<td><table border="1"><tr><td colspan="2"><b>SGST</b></td></tr><tr><td><b>Rate</b></td><td><b>Amount</b></td></tr></table></td>
		<td><table border="1"><tr><td colspan="2"><b>IGST</b></td></tr><tr><td><b>Rate</b></td><td><b>Amount</b></td></tr></table></td>
	</tr>
	<?
	$d=0;
	$del1=0;
	$qry = $mysqli->prepare("select code,name,rate,pv,cgst,sgst,igst,uom,batch_no,mrp,bv from product_resale where id=?");  
	foreach  ($item as $v)
	{ $qry->bind_param('s', $v);								
   	  $qry->execute();
	  $qry->bind_result($code,$prod_name,$rate,$pv,$cgst,$sgst,$igst,$uom,$batch_no,$mrp,$cc);
      while ($qry->fetch())
   	  { $code=$code;
		$batch_no=$batch_no;$mrp=$mrp;
		$prod_name=$prod_name;
		$rate=$rate;
		$pv=$pv;
		$cgst=$cgst;
		$sgst=$sgst;
		$igst=$igst;
		$uom=$uom;
		$cc=$cc;
	  }
	  $diss1=$rate*$dis[$d];
	  ?>
		<tr>
			<td><? echo $prod_name;?></td>
			<td><? echo $code;?></td>
			<td><? echo $qty[$d];?></td>
			<td><? echo $batch_no;?></td>
			<td><? echo $uom;?></td>
	  <?
		$g_val1=$rate*$qty[$d];
		$taxable_val=$g_val1+$del1-$diss1;
		$cgst_val=$taxable_val*$cgst/100;
		$igst_val=$taxable_val*$igst/100;
		$sgst_val=$taxable_val*$sgst/100;
		$gs_val1=$g_val1+$del1-$diss1;
	  ?>
	  <td><? echo $rate;?></td>
	  <td><? echo $g_val1;?></td>
	  <td><? echo $mrp;?></td>
	  <td><? echo $diss1;?></td>
	  <td><? echo $taxable_val;?></td>
	  <td><table width="102" border="1">
		  <tr><td width="35"><? if($bill_type=="state"){echo $cgst;  } ?></td>
		  <td width="50"><? if($bill_type=="state"){echo $cgst_val; } ?></td>
  		  </tr></table>
	  </td>
	  <td><table width="102" border="1">
	  	<tr><td width="35"><? if($bill_type=="state"){echo $sgst; } ?></td>
	  	<td width="50"><? if($bill_type=="state"){echo $sgst_val; } ?></td>
  		</tr></table>
	  </td>
	  <td><table width="102" border="1">
		  <tr><td width="35"><? if($bill_type=="central"){echo $igst; } ?></td>
	  	  <td width="50"><? if($bill_type=="central"){echo $igst_val; } ?></td>
  		  </tr></table>
	  </td>
	  <? if($bill_type=="central"){ $tax_rebate1=$igst_val; } ?>
	  <? if($bill_type=="state"){ $tax_rebate1=$cgst_val+$sgst_val; } ?>
	  <? 
	 $total1=$taxable_val+$tax_rebate1;?>
</tr>
<input type="hidden" name="qty[]" value="<? echo $qty[$d];?>">
<input type="hidden" name="item[]" value="<? echo $v;?>">
<input type="hidden" name="dis[]" value="<? echo $dis[$d];?>">
<input type="hidden" name="del[]" value="<? echo $del1;?>">
<?
if($bill_type=="state"){$cgs1=$cgst_val;$sgs1=$sgst_val;$igs1=0;}
if($bill_type=="central"){$cgs1=0;$sgs1=0;$igs1=$igst_val;}
$dis_pv1=$pv*$dis[$d];
$pv1=$pv*$qty[$d];
$cc1=$cc*$qty[$d];

$total+=$total1; 
				   $tot_qty+=$qty[$d]; 
				   $tot_vat+=$tax_rebate1;
				   $tot_dis_pv+=$dis_pv1;
				   $tot_pvv+=$pv1;
				   $tot_g_val+=$gs_val1;
				   $tot_cgst+=$cgs1;
				   $tot_sgst+=$sgs1;
				   $tot_igst+=$igs1;
				   $tot_cc+=$cc1;
				   $total_dp+=$g_val1;
				   
				   $d++;
 } 

$tot_pv=$tot_pvv-$tot_dis_pv;
$coupon_redem=0;
?>
<tr>
	<td colspan="9" align="right"><b>Gross Total</b></td>
	<td><?  echo $tot_qty;?></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$total_dp);?></td>
</tr>

<tr>
	<td colspan="10" align="right"><b>CGST</b></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$tot_cgst);?></td>
</tr>

<tr>
	<td colspan="10" align="right"><b>SGST</b></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$tot_sgst);?></td>
</tr>

<tr>
	<td colspan="10" align="right"><b>IGST</b></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$tot_igst);?></td>
</tr>
<?
$net_bill=$total-(float)$dis;
$dists=0;
$net_bill2=$total-$dists;
?>
<tr>
	<td colspan="10" align="right"><b>Coupon Deduction</b></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$coupon_redem);?></td>
</tr>

<?
$net_bill_amt=$total-$coupon_redem;
?>

<tr>
	<td colspan="10" align="right"><b>Net Amount</b></td>
	<td colspan="3"><?  echo sprintf("%01.2f",$net_bill_amt);?></td>
</tr>

<tr>
	<td colspan="3" align="right"><b>Total B.P </b>: <? echo $tot_pv;?></td>
	<td colspan="7" align="right"><b>Payable</b></td>
	<td colspan="3"><?  echo round($net_bill_amt);?></td>
</tr>
<input type="hidden" name="name" value="<? echo $name;?>">
<input type="hidden" name="member_id" value="<? echo $member_id;?>">
<input type="hidden" name="ent_date" value="<? echo $ent_date;?>">
<input type="hidden" name="discount" value="<? echo $dis;?>">
<input type="hidden" name="dists" value="<? echo $dists;?>">
<input type="hidden" name="total" value="<? echo $total;?>">
<input type="hidden" name="vat" value="<? echo $tot_vat;?>">
<input type="hidden" name="tot_pv" value="<? echo $tot_pv;?>">
<input type="hidden" name="tot_cc" value="<? echo $tot_cc;?>">
<input type="hidden" name="tot_g_val" value="<? echo $tot_g_val;?>">
<input type="hidden" name="tot_cgst" value="<? echo $tot_cgst;?>">
<input type="hidden" name="tot_sgst" value="<? echo $tot_sgst;?>">
<input type="hidden" name="tot_igst" value="<? echo $tot_igst;?>">
<input type="hidden" name="bill_type" value="<? echo $bill_type;?>">
<input type="hidden" name="coupon_redem" value="<? echo $coupon_redem;?>" />
<tr>
	<td align="center" colspan="13"><input type="submit" name="submit" value="Submit.."></td>
</tr>
</table>
</form>
<? //}
 }}?>
		<? } ?>	
<?php if($action=="voucher")
		{
				$member_id=$_REQUEST['member_id'];
				$bill_no=$_REQUEST['bill_no'];
				 ?>                        
	 <table class="table table-bordered mb-4">
		<tr>
            <td valign="top" align="center"> Invoice</td>
        </tr>

		<tr>
		   <td colspan="4" align="center">
		   <input type="button" value="PRINT Invoice" onclick="window.open('print_bill.php?bill_no=<? echo $bill_no;?>', '','width=740,height=500,scrollbars=yes,resizable=yes,toolbar=yes,directories=no,location=no,menubar=no,status=no,left=50,top=0'); return true;" />
		   </td>
	    </tr>				  
	</table>
				<?php 
				}?>
                           </div>
                       </div>
               </div>	
<?

if($action=="add_process")
{
$member_id=$_REQUEST['member_id'];
$stock_id=$_SESSION['admin_id'];
$gr_date=date("Y-m-d:his");
$coupon_redem=$_REQUEST['coupon_redem'];
$qry = $mysqli->prepare('SELECT * FROM bill where bill_no=?');
$qry->bind_param('s', $gr_date);
$qry->execute();
$qry->store_result();
$bill_entry=$qry->num_rows;
while($bill_entry == 1) // If find same user ID in DB
{
$gr_date=date("Y-m-d:his");
$qry = $mysqli->prepare('SELECT * FROM bill where bill_no=?');
$qry->bind_param('s', $gr_date);
$qry->execute();
$qry->store_result();
$bill_entry=$qry->num_rows;
}

$bill_type=$_REQUEST['bill_type'];
$total=$_REQUEST['total'];
$temp_no= mt_rand (1000000001, 9999999999); // Syntex : int mt_rand ( [int min, int max])
$temp_no=$temp_no;				
$bill_no=$gr_date.$temp_no;														
$vat_no=$temp_no;
//$doc_name=$_REQUEST['doc_name'];
//$name=$_REQUEST['name'];
$ent_date=$_REQUEST['ent_date'];
$en_dat=explode("-",$ent_date);
$en_date_val=$en_dat[2].$en_dat[1].$en_dat[0];

$typ="R";
$qq_spot = $mysqli->prepare('SELECT * FROM report where member_id=? and total=? and date=?');
$qq_spot->bind_param('sss', $member_id,$total,$ent_date);
$qq_spot->execute();
$qq_spot->store_result();
$no_spot=$qq_spot->num_rows;
if($no_spot>0)	
{ $_SESSION['msg_box2']="Please do not Refress. Try Again.";
// $_SESSION['class_notification']="notification success png_bg";	
  header("location:manage_resale.php?tab_id=tab1&default=manage_resale&member_id=$member_id");
}
else
{ 
$qty=$_REQUEST['qty'];
$item=$_REQUEST['item'];
$dis=$_REQUEST['dis'];
$del=$_REQUEST['del'];
$discount=$_REQUEST['discount'];
$dists=$_REQUEST['dists'];
$total=$_REQUEST['total'];
$vat=$_REQUEST['vat'];
$tot_pv=$_REQUEST['tot_pv'];
$tot_g_val=$_REQUEST['tot_g_val'];
$tot_cgst=$_REQUEST['tot_cgst'];
$tot_sgst=$_REQUEST['tot_sgst'];
$tot_igst=$_REQUEST['tot_igst'];
$gst_no=$_REQUEST['gst_no'];
if($gst_no==""){$gst_no="N";}

$i=0;
foreach ($item as $value)
{
	$insert1=$mysqli->prepare("insert into bill (item_code, bill_no, qty, vat_no, date, date_val, member_id, dis, del) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
	$insert1->bind_param("sssssssss", $value, $bill_no, $qty[$i], $vat_no, $ent_date, $en_date_val, $member_id, $dis[$i], $del[$i]);
	$insert1->execute();

	$remark="Debit For Bill No".$bill_no;
	$type="Dredit";

	$queryd=$mysqli->prepare("insert into stock_details2 (prod_id, qty, remark, date, date_val,type,stock_id) VALUES (?, ?, ?, ?, ?, ?,?)");
	$queryd->bind_param("sssssss", $value, $qty[$i], $remark, $ent_date, $en_date_val,$type,$stock_id);
	$queryd->execute();

	$query_stk = $mysqli->prepare("select current_stock from stock_record where stock_id=? and prod_id=?");
	$query_stk->bind_param('ss', $stock_id,$value);
	$query_stk->execute();
	$query_stk->bind_result($stock1);
	while ($query_stk->fetch())
	{ $curr_stock1=$stock1;													
	}
	$new_stock1=$curr_stock1-$qty[$i];

	$qry_stk=$mysqli->prepare("update stock_record set current_stock=? where prod_id=? and stock_id=?");
	$qry_stk->bind_param('sss', $new_stock1,$value,$stock_id);
	$qry_stk->execute();
	$qry_stk->close();

	$i++;
}
$admin_id=$_SESSION['admin_id'];  

$type="R";
$qry_ins=$mysqli->prepare("insert into report (bill_no, vat_no, total, member_id, date, date_val, discount, admin_id, gross_bill, total_pv,vat,sgst,cgst,igst,bill_type,stock_id,gst_no,type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$qry_ins->bind_param("ssssssssssssssssss", $bill_no, $vat_no, $total, $member_id, $ent_date, $en_date_val, $dists,$admin_id,$total,$tot_pv,$vat,$tot_sgst,$tot_cgst,$tot_igst,$bill_type,$stock_id,$gst_no,$type);
$qry_ins->execute();

$member_id=$_REQUEST['member_id'];

$quryinIN=$mysqli->prepare("select intro_id,position  from member where member_id=?");
$quryinIN->bind_param("s", $member_id);
$quryinIN->execute();
$quryinIN->bind_result( $intro_id,$position);
 while($quryinIN->fetch())
{
$intro_id=$intro_id;
$position=$position;
}

$active="y";
$date=date("d-m-Y");
$date_val=date("Ymd");
$activation_time=date("H:i:s");

$quryin2=$mysqli->prepare("select self_bv,self_bv_int,current_self_bv  from member where member_id=?");
$quryin2->bind_param("s", $member_id);
$quryin2->execute();
$quryin2->bind_result($self_bv,$self_bv_int,$current_self_bv);
while($quryin2->fetch())
{ $self_bv=$self_bv;
  $self_bv_int=$self_bv_int;
  $current_self_bv=$current_self_bv;
}
									
$now_rwallet=$self_bv+$total;
$now_self_bv=$self_bv_int+$tot_pv;
$ncurrent_self_bv=$current_self_bv+$tot_pv;
$up_ewt=$mysqli->prepare("update member set self_bv=?,self_bv_int=?,current_self_bv=? where member_id=?");
$up_ewt->bind_param('ssss', $now_rwallet,$now_self_bv,$ncurrent_self_bv,$member_id);
$up_ewt->execute();	

$member_id=$_REQUEST['member_id'];
$sts="n";
$qq_spot = $mysqli->prepare('SELECT * FROM resale_payment where member_id=? and status=?');
$qq_spot->bind_param('ss', $member_id,$sts);
$qq_spot->execute();
$qq_spot->store_result();
$no_spot=$qq_spot->num_rows;

if($no_spot!=0)
{


		 $qq_spot2 = $mysqli->prepare("select business from resale_payment where member_id=? and status=?");
          $qq_spot2->bind_param('ss', $member_id,$sts);
          $qq_spot2->execute();
		   $qq_spot2->bind_result($business);
		  while ($qq_spot2->fetch())
           {

            $business=$business;

			}

		   $sp_amt=$business+$tot_pv;
		   $up_spot=$mysqli->prepare("update resale_payment set business=? where member_id=? and status=?");
			$up_spot->bind_param('sss', $sp_amt, $member_id, $sts);
			$up_spot->execute();


		 }


		 else


		 {

			$inst=$mysqli->prepare("insert into resale_payment (member_id, business) VALUES (?, ?)");


	$inst->bind_param("ss", $member_id, $tot_pv);

$inst->execute();


		 }



		 



//----------------------





$new_bid=$_REQUEST['member_id'];




					while($new_bid!="ABCD1234")	



						{









												$prt_id="";

												$pos="";

												$qq=$mysqli->prepare("select parent_id,position from member where member_id=?");

													$qq->bind_param("s", $new_bid);

													$qq->execute();

													$qq->bind_result($parent_id,$position);

													while($qq->fetch())

													{

													$parent_id=$parent_id;

													 $pos=$position;

													}

													 $prt_id=$parent_id;

											$qq->close();



												$q2q=$mysqli->prepare("select left_business,right_business,total_left_business,total_right_business,left_current_business,right_current_business from member where id=?");

													$q2q->bind_param("s", $prt_id);

													$q2q->execute();

													$q2q->bind_result($left_business,$right_business,$total_left_business,$total_right_business,$left_current_business,$right_current_business);

													while($q2q->fetch())


													{

													$left_business=$left_business;
													$right_business=$right_business;
													$total_left_business=$total_left_business;
													$total_right_business=$total_right_business;
													$left_current_business=$left_current_business;
													$right_current_business=$right_current_business;
													

													

													 if($pos=="L")

														{







														$left_pt=$left_business+$tot_pv;



														$total_left_pt=$total_left_business+$tot_pv;

														$right_pt=$right_business;

														$total_right_pt=$total_right_business;
														$left_current_pt=$left_current_business+$tot_pv;
													   $right_current_pt=$right_current_business;

														

														}





													    if($pos=="R")

														{







													$left_pt=$left_business;



														$total_left_pt=$total_left_business;

														$right_pt=$right_business+$tot_pv;

														$total_right_pt=$total_right_business+$tot_pv;
														$left_current_pt=$left_current_business;
													   $right_current_pt=$right_current_business+$tot_pv;


														


														}

													}

													

													$q2q->close();

													

													$updat_mem=$mysqli->prepare("update member set left_business=?,right_business=?,total_left_business=?,total_right_business=?,left_current_business=?,right_current_business=? where id=?");

			$updat_mem->bind_param('sssssss', $left_pt,$right_pt,$total_left_pt,$total_right_pt,$left_current_pt,$right_current_pt,$prt_id);

			$updat_mem->execute();

			$updat_mem->close();

													



														

//$member_idd="";




$qq3=$mysqli->prepare("select member_id from member where id=?");

													$qq3->bind_param("s", $prt_id);

													$qq3->execute();

													$qq3->bind_result($member_id);

													while($qq3->fetch())

													{

													 $member_idd=$member_id;

													

													}

												

												





										 $new_bid=$member_idd;

										//echo "<br>";



$qq3->close();



										}



$member_id=$_REQUEST['member_id'];






$pv=$tot_pv;										







$sts="n";







 $qq_spot2 = $mysqli->prepare('SELECT * FROM bv_business_resale where status=?');







  $qq_spot2->bind_param('s',$sts);







						$qq_spot2->execute();







						$qq_spot2->store_result();







						$no_spot2=$qq_spot2->num_rows;































	if($no_spot2!=0)







		 {







		 







		 $qq_spot3 = $mysqli->prepare("select total_bv,total_amt from bv_business_resale where status=?");







          $qq_spot3->bind_param('s',$sts);







          $qq_spot3->execute();







		   $qq_spot3->bind_result($total_bv,$total_amt);







		  while ($qq_spot3->fetch())







           {







            $total_bv=$total_bv;
			$total_amt=$total_amt;

			}















		   $sts="n";







		   $sp_amt2=$total_bv+$pv;
		   $total_amt2=$total_amt+$total;






		   $up_spot2=$mysqli->prepare("update bv_business_resale set total_bv=?,total_amt=? where status=?");

			$up_spot2->bind_param('sss', $sp_amt2,$total_amt2, $sts);















			$up_spot2->execute();







		   







		







		 }







		 else







		 {







		    







		    







			







			$inst2=$mysqli->prepare("insert into bv_business_resale (total_bv,total_amt) VALUES (?, ?)");


	$inst2->bind_param("ss", $pv,$total);

$inst2->execute();







		 }







//---------------------------------	 











	



$sts="n";







 



		 







		//---------------------------------	 







$member_id=$_REQUEST['member_id'];	



















								























										







										$sts="n";



$total=$_REQUEST['total'];



 $qq_spots2 = $mysqli->prepare('SELECT * FROM stock_business where status=? and stock_id=?');







  $qq_spots2->bind_param('ss',$sts,$stock_id);







						$qq_spots2->execute();







						$qq_spots2->store_result();







						$no_spots2=$qq_spots2->num_rows;















		 







		 







		 if($no_spots2!=0)







		 {







		 







		 $qq_spot3 = $mysqli->prepare("select business,bv from stock_business where status=? and stock_id=?");







          $qq_spot3->bind_param('ss',$sts,$stock_id);







          $qq_spot3->execute();







		   $qq_spot3->bind_result($business,$bv);







		  while ($qq_spot3->fetch())







           {







            $business=$business;


 $bv=$bv;




			







			}















		   $sts="n";







		   $curr_buss=$business+$total;

 $curr_bv=$bv+$tot_pv;





		  







		   







		   $up_spot2=$mysqli->prepare("update stock_business set business=?,bv=? where status=? and stock_id=?");















			$up_spot2->bind_param('ssss', $curr_buss,$curr_bv, $sts,$stock_id);















			$up_spot2->execute();







		   







		







		 }







		 else







		 {







		    







		    







			







			$inst2=$mysqli->prepare("insert into stock_business (business,stock_id,bv) VALUES (?,?,?)");















	$inst2->bind_param("sss", $total,$stock_id,$tot_pv);















$inst2->execute();







		 }







		 







		 //-----------







	$member_id=$_REQUEST['member_id'];







$qry=$mysqli->prepare("select name,mob from member where member_id=?");



$qry->bind_param("s", $member_id);



$qry->execute();



$qry->bind_result($name,$mob);



while($qry->fetch())



{



$name=$name;







$mob=$mob;



}









	$member_id=$_REQUEST['member_id'];







$total=$_REQUEST['total'];




$quryin2=$mysqli->prepare("select name,mob  from member where member_id=?");
								 $quryin2->bind_param("s", $member_id);
								 $quryin2->execute();
								  $quryin2->bind_result($name,$mob);
								  while($quryin2->fetch())
									{

									$name=$name;
									$mob=$mob;
									
									
									}

							

/*$mob="91".$mob;


$apiKey = urlencode('NjUzNzYyNTk2MzM3Nzk2MzU2NTQ2MzQ0NGM2YzY4NGI=');
	
	// Message details
	
	$numbers = array($mob);
	$sender = urlencode('MORLIF');
	
	
	 $msgtext="Congratulations! Dear ".$name." your purchase of worth Rs. ".$total." in MORLIFE HEALTHCARE PRIVATE LIMITED is successful. Please visit morlife.in for more details.";
	$message = rawurlencode($msgtext);
 
	$numbers = implode(',', $numbers);
 
	// Prepare data for POST request
	$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
 
	// Send the POST request with cURL
	$ch = curl_init('https://api.textlocal.in/send/');
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	 $response = curl_exec($ch);
	curl_close($ch);*/



										 











										 







										 







 $_SESSION['msg_box']="<a href=# class=close><img src=resources/images/icons/cross_grey_small.png title=Close this notification alt=close /></a>







				<div>







					New Purchase Added Successfully.







				</div>";







 $_SESSION['class_notification']="notification success png_bg";	







header("location:manage_resale.php?tab_id=tab1&default=manage_resale&action=voucher&member_id=$member_id&bill_no=$bill_no");























}



}



?>
<script>
const input=document.getElementById("inp-grp");


function add_prod() 
{   
    var prod_id=document.getElementById('sel');
	var quan=document.getElementById('quan');
	var x=prod_id.value;
	var ttl=document.getElementById('ttl');
	var s=0;
	
    var tr=document.createElement("tr");
    var td=document.createElement("td");
    td.setAttribute('colspan','2');
    var td2=document.createElement("td");
    var td3=document.createElement("td");
    var td4=document.createElement("td");


	var span=document.createElement("span");
	span.innerHTML="Product Name: ";

	var span2=document.createElement("span");
	span2.innerHTML="Quantity: ";

	var span3=document.createElement("span");
	span3.innerHTML="Discount: ";
	
	var span4=document.createElement("span");

	var del=document.createElement("button");
	del.setAttribute('type','button');
	
	del.addEventListener("click",remove_prod);
    del.innerHTML="Delete";
	
	$.ajax({
		url:"get_mrp2.php",
		method:"post",
		data:{ 'id': x},
		success: function(res){
			span4.innerHTML="&nbsp;&nbsp;&nbsp;&nbsp; "+(parseInt(quan.value)*parseInt(res));
			ttl.innerHTML=parseInt(ttl.innerHTML)+(parseInt(quan.value)*parseInt(res));
			del.setAttribute('value',parseInt(quan.value)*parseInt(res));
		}
	})
    var display=document.createElement("span");
	// display.setAttribute('type','text');
	// display.setAttribute('width',200);
    // display.setAttribute('value',prod_id.selectedOptions[0].text);  
	// display.setAttribute('readonly',""); 
    display.innerHTML="<b>"+prod_id.selectedOptions[0].text+"</b>";
    
	var br=document.createElement("br");
    var name=document.createElement("input");
    name.setAttribute('type','hidden');
    name.setAttribute('value',x);
	name.setAttribute('name','item[]');

    var qty=document.createElement("input");
    qty.setAttribute('type','text');
    qty.setAttribute('placeholder','Enter Quantity');
	qty.setAttribute('readonly',""); 
    qty.setAttribute('value',quan.value);
	qty.setAttribute('name','qty[]');

    var dis=document.createElement("input");
    dis.setAttribute('type','text');
    dis.setAttribute('placeholder','Enter Discount');
    dis.setAttribute('value',"0");
	dis.setAttribute('name','dis[]');

	

    input.appendChild(tr);

    tr.appendChild(td);

	td.appendChild(span);
    td.appendChild(display);
    td.appendChild(name);

	td.appendChild(br);
    
	td.appendChild(span2);
    td.appendChild(qty);

	td.appendChild(span3);
    td.appendChild(dis);
	td.appendChild(del);
	td.appendChild(span4);
	// quan.value=1;
}

function remove_prod()
{	var ttl=document.getElementById('ttl');
	ttl.innerHTML=parseInt(ttl.innerHTML)-parseInt(this.value);
	this.parentElement.remove();
}

jQuery('.ch').chosen();
</script>