<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	$c2=$_POST['customer'];
    $d=$_POST['d'];
	$t=$_POST['type'];
	
	
	
	$add="INSERT INTO `sell_j`( `sell_j_customer`, `sell_j_date`,  `user`,`type`) 
	VALUES ('$c2','$d','$login_user','$t')";
	$conn->query($add);
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
}
if(isset($_POST['submit2']))
{
	$t_id=$_POST['t_id'];
    $c=$_POST['c'];
	$p=$_POST['p'];
	$n=$_POST['n'];
	$s=$_POST['s'];
	
	
	$add="INSERT INTO  `sell_j_item`(`sell_j`, `sell_j_i_code`, `sell_j_i_price`, `sell_j_i_number`, `sell_j_i_set`)  
	VALUES ('$t_id','$c','$p','$n','$s')";
	$conn->query($add);
	
	
	$get="select `store_number`,`store_set_number` from `items` where code='$c'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$num_old=$get_row['store_number'];
	$set_num_old=$get_row['store_set_number'];
	
	$new_num=$num_old-$n;
	$set_new_num=$set_num_old-$s;
	
	$up="UPDATE `items` SET `store_number`='$new_num',`store_set_number`='$set_new_num',`user`='$login_user' WHERE `code`='$c'";
	$conn->query($up);
	
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
}
if(isset($_GET['sell_j_i_id']))
{
	$sell_j_i_id=$_GET['sell_j_i_id'];
	$c=$_GET['c'];
	$n=$_GET['n'];
	$s=$_GET['s'];
   
	
	$get="select `store_number`,`store_set_number` from `items` where code='$c'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$num_old=$get_row['store_number'];
	$set_old=$get_row['store_set_number'];
	
	$new_num=$num_old+$n;
	$new_set=$set_old+$s;
	
	$up="UPDATE `items` SET `store_number`='$new_num',`store_set_number`='$new_set',`user`='$login_user' WHERE `code`='$c'";
	$conn->query($up);
	
	
	$del="delete from  `sell_j_item` where `sell_j_i_id`=$sell_j_i_id";
	$conn->query($del);
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
}
if(isset($_GET['t_id']))
{
	$t_id=$_GET['t_id'];
   
	
	$del="delete from  `sell_j_item` where `sell_j_i_id`=$t_id";
	$conn->query($del);
	$del2="delete from  `sell_j` where `sell_j_id`=$t_id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
}
if(isset($_GET['done_id']))
{
	$t_id=$_GET['done_id'];
	
	$c=$_GET['c'];
	$d=$_GET['d'];
	//$t=$_GET['t'];
	$a=$_GET['a'];
	$type=$_GET['type'];
   
	
	

	if($type=="n")
	{
		$note="نقدی";
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'sell',$t_id,'$login_user')");
		$conn->query("INSERT INTO `get`(`get_customer`,`get_date`,`note`,`amount`,`status`) VALUES ('$c','$d','$note','$a',1)");
		
		$max="select max(`get_id`) as `g` from `get`";
		$max_r=$conn->query($max);
		$max_row=mysqli_fetch_array($max_r);
		$g=$max_row['g'];
		
		
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'get',$g,'$login_user')");
		
		
		
	}
	else{
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'sell',$t_id,'$login_user')");
	}
	
	$up="update `sell_j` set status=1 where `sell_j_id`=$t_id";
	$conn->query($up);
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
	
}
$c="select * from `customer` where `active`=1";
$c_r=$conn->query($c);



$check="select * from `sell_j`,`customer` where `sell_j_customer`=`customer_id` and  `status`=0";
$check_r=$conn->query($check);
?>
<h3>فرۆشتن</h3><hr>
<?php if($check_row=mysqli_fetch_array($check_r))
{ $c_id=$check_row['customer_id'];?>
	<table width="70%" border=1>
			<tr>
				<th>کڕیار: <?php echo $check_row['customer_name'];?></th>
				<th>ڕێکەوت:<?php echo $d=$check_row['sell_j_date'];$t=$check_row['type'];?></th>
				<th>
				<?php
				$s_j=$check_row['sell_j_id'];
				$check2="select * from `sell_j_item` where `sell_j`=$s_j";
				$check_r2=$conn->query($check2);
				$num_row=mysqli_num_rows($check_r2);
				if($num_row==0){
				?>
				<a href="sell_j.php?t_id=<?php echo $check_row['sell_j_id'];?>"><img src="icon/delete.png" width=20px></a>
				<?php } ?>
				
				
				
				
				
				</th>
				<!--<th><a href="sell_j.php?done_id=<?php echo $check_row['sell_j_id'];?>&&c=<?php echo $c_id;?>&&d=<?php echo $d;?>&&t=<?php echo $t;?>"><img src="icon/done.png" width=20px></a></th>-->
			</tr>
	</table>
	
	<table>
	<?php if(isset($_POST['code_check'])){ 
		$check_code=$_POST['code_check'];
			$check2="select * from `items` where `code`='$check_code'";
			$check_r2=$conn->query($check2);
			$check_row2=mysqli_fetch_array($check_r2);
			$set=$check_row2['store_set_number'];
			$numb=$check_row2['store_number'];
			$s_p=$check_row2['sell_price'];
			?>
			<form action="" method="post">
	
		<tr>
			<td>کۆد</td>
			<td>ژمارەی سێت</td>
			<td>ژمارە</td>
			<td>نرخ</td>
		</tr>
		<tr>
		
			
			<td>
				<input type="text" name="code_check" required onchange="this.form.submit()" >
			</td>
			<td>
				<?php echo $set;?>
			</td>
			<td>
				<?php echo $numb;?>
			</td>
			
			</form>
			<form action="" method="post">
			<input type="hidden" name="t_id" value="<?php echo $t_id=$check_row['sell_j_id'];$type=$check_row['type'];?>">
		</tr>
		<tr>
		
			
			<td>
				<input type="text" name="c" required value=<?php echo $check_code;?>>
			</td>
			<td>
				<input type="number" name="s" required>
			</td>
			<td>
				<input type="number" name="n" required>
			</td>
			<td>
				<input type="number" name="p" required value=<?php echo $s_p;?> step="0.01">
			</td>
			<td>
				<input type="submit" name="submit2" value="زیادکردن">
			</td>
		</tr>
		
		</form>
			<?php
	}
	else
	{
			?>
	
	
	
	<form action="" method="post">
	
		<tr>
			<td>کۆد</td>
			<td>ژمارەی سێت</td>
			<td>ژمارە</td>
			<td>نرخ</td>
		</tr>
		<tr>
			<td>
				<input type="text" name="code_check" required onchange="this.form.submit()" >
			</td>
			
			
		</tr>
		</form>
			<form action="" method="post">
			<input type="hidden" name="t_id" value="<?php echo $t_id=$check_row['sell_j_id'];$type=$check_row['type'];?>">
		<tr>
			
			<td>
				<input type="text" name="c" required>
			</td>
			<td>
				<input type="number" name="s" required>
			</td>
			<td>
				<input type="number" name="n" required>
			</td>
			<td>
				<input type="number" name="p" required>
			</td>
			<td>
				<input type="submit" name="submit2" value="زیادکردن">
			</td>
		</tr>
		
		
		</form>
	<?php }?>
		<?php $gett=$conn->query("select * from `sell_j_item`,`items` where `code`=`sell_j_i_code`  and `sell_j`=$t_id ");
			$total=0;
			while($get_row=mysqli_fetch_array($gett)){ ?>
			<tr>
				<th><?php  echo $get_row['sell_j_i_code'];?><?php  echo $get_row['name'];?></th>
				<th><?php echo $num=$get_row['sell_j_i_set'];?></th>
				<th><?php echo $num=$get_row['sell_j_i_number'];?></th>
				<th><?php echo number_format($p=$get_row['sell_j_i_price'],2);?> $</th>
				<th><?php echo number_format($t=$p*$num,2); $total+=$t;?> $</th>
				<td><a href="sell_j.php?sell_j_i_id=<?php echo $get_row['sell_j_i_id'];?>&&c=<?php echo $get_row['sell_j_i_code'];?>&&n=<?php echo $get_row['sell_j_i_number'];?>&&&&s=<?php echo $get_row['sell_j_i_set'];?>"><img src="icon/delete.png" width=20px></a></td>
			</tr>
			<?php }?>
			<tr bgcolor=#f9f9f9>
			<td></td>
			<td></td>
			<td></td><td></td>
			<th width=150px><?php echo number_format($total,2);?> $</th>
			<?php if($total!=0){ ?><th width=150px><a href="sell_j.php?done_id=<?php echo $check_row['sell_j_id'];?>&&c=<?php echo $c_id;?>&&d=<?php echo $d;?>&&type=<?php echo $type;?>&&a=<?php echo $total;?>"><img src="icon/done.png" width=20px></a></th><?php } ?>
			</tr>
			
			
	</table>
	
	
<?php }
else{ ?>
        <form action="" method="post">
		<table>
			<tr>
				<th>کڕیارەکان</th>
				<th></th>
				<th></th>
			</tr>
			<tr>
				<td>
					<select name="customer" required>
							<option value=""></option>
							<?php while($row2=mysqli_fetch_array($c_r)){ ?>
						<option value="<?php echo $row2['customer_id'];?>"><?php echo $row2['customer_name'];?></option>
						 <?php }?>
					</select>
				</td>
				<td><input type="date" name="d" value="<?php echo date("Y-m-d");?>" required></td>
				<th style="font-size:18pt"> نقد <input type="radio" name="type" value="n" required style="height:20px; width:20px;">| قەرز <input type="radio" name="type" value="q" required style="height:20px; width:20px; "></th>
				<td><input type="submit" name="submit" value="ناردن"></td>
			</tr>
			
	</table>
			
			
			</table>
        </form>
<?php } ?>
</div>
</div>



<table border=1 dir=rtl width=80% align="center" style="font-size:18pt">
<tr bgcolor="lightblue">
				<th>کڕیار</th>
				<th>ڕێکەوت</th>
				<th></th>
				<th></th>
			</tr>
<?php
$check2="select * from `sell_j`,`customer` where `sell_j_customer`=`customer_id` and `status`=1 order by `sell_j_date` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
?>
			<tr>
				<td><?php echo $row2['customer_name'];?></td>
				<td><?php echo $row2['sell_j_date'];?></td>
				<th><a onclick='return confirm("دڵنیای لە سڕینەوەی ئەم پسووڵەیە؟؟؟؟");' href="inv_del.php?sell_j_id=<?php echo $row2['sell_j_id'];?>"><img src="icon/delete.png" width=30px></a></th>
				<th><a href="sell_j_print.php?sell_j_id=<?php echo $row2['sell_j_id'];?>"><img src="icon/print.png" width=30px></a></th>
			</tr>
<?php }?>
</table>  
<div id="footer">
<span>By : H Group</span>
</div>

</div>

</div>



</body>
</html>
