
<style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="search/jquery.autocomplete.css" />
<script type="text/javascript" src="search/jquery.js"></script>
<script type="text/javascript" src="search/jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
 $("#tag").autocomplete("search/autocomplete_customer_all.php", {
		selectFirst: true
	});
});
</script>
<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	//$c2=$_POST['customer'];
	$n=$_POST['n'];
	$get="select `customer_id` from `customer` where customer_name='$n'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$c2=$get_row['customer_id'];
	
	$d=date('Y-m-d h:i:s');
	
    $add="INSERT INTO `sell_j`( `sell_j_customer`,  `user`, `sell_j_date`) 
	VALUES ('$c2','$login_user','$d')";
	$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'];
	$s=$_POST['s'];
	
	
	echo $add="INSERT INTO  `sell_j_item`(`sell_j`, `sell_j_i_code`, `sell_j_i_price`,  `sell_j_i_set`)  
	VALUES ('$t_id','$c','$p','$s')";
	$conn->query($add);
	
	
	
	$get="select `i_set_number` from `item_set` where `i_s_barcode`='$c'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$set_num_old=$get_row['i_set_number'];
	
	
	$new_set_num=$set_num_old-$s;
	
	$up="UPDATE `item_set` SET `i_set_number`='$new_set_num' WHERE `i_s_barcode`='$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'];
	$s=$_GET['s'];
   
   
   
   $get="select `i_set_number` from `item_set` where i_s_barcode='$c'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$num_old=$get_row['i_set_number'];
	
	$new_num=$num_old+$s;
	
	$up="UPDATE `item_set` SET `i_set_number`='$new_num' WHERE i_s_barcode='$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['rej_id']))
{
	$id=$_GET['rej_id'];
	$get_get=$conn->query("select * from `get` where `sell_j`=$id");
	while($gg_row=mysqli_fetch_array($get_get))
	{
		$g_id=$gg_row['get_id'];
		$conn->query("delete from `sell_j_inv` where `inv_code`=$g_id");
		$cust=$gg_row['get_customer'];
	}
	 $conn->query("delete from `get` where `sell_j`=$id");
	
	$conn->query("delete from `sell_j_inv` where `inv_code`=$id");
	
	$get="select `sell_j_i_number`,`sell_j_i_code` from `sell_j_item` where `sell_j`=$id";
	$get_r=$conn->query($get);
	while($get_row=mysqli_fetch_array($get_r)){
	$num=$get_row['sell_j_i_number'];
	$set=$get_row['sell_j_i_set'];
	$c=$get_row['sell_j_i_code'];
	
	$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+$num;
	$new_set=$set_old+$set;
	
	$up="UPDATE `items` SET `store_number`='$new_num',`store_set_number`='$new_set' WHERE `code`='$c'";
	$conn->query($up);
	}
	$conn->query("delete from `sell_j_item` where `sell_j`=$id");
	$conn->query("delete from `sell_j` where `sell_j_id`=$id");
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
	
}



if(isset($_POST['sell_j_id']))
{
	$sell_j_id=$_POST['sell_j_id'];
	$d=$_POST['d'];
	$c=$_POST['c'];
	$amount=$_POST['amount'];
	$remain=$_POST['remain'];
	$cost=$_POST['cost'];
	if($amount==0)
	{
		
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'sell',$sell_j_id,'$login_user')");
		$conn->query("update `sell_j` set `payed`=0,`amount`=$cost,`status`=2 where `sell_j_id`=$sell_j_id");
	}
	else
	{
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'sell',$sell_j_id,'$login_user')");
		$conn->query("INSERT INTO `get`(`get_customer`,`get_date`,`note`,`amount`,`status`,`sell_j`) VALUES ('$c','$d','','$amount',1,$sell_j_id)");
		
		$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("update `sell_j` set `payed`=$amount ,amount=$cost,`status`=2  where `sell_j_id`=$sell_j_id");
		//$conn->query("update `customer` set `statement`=$remain where `customer_id`=$c");
		$conn->query("INSERT INTO `sell_j_inv`(`date`,`customer`, `inv_type`, `inv_code`,`user`) VALUES ('$d',$c,'get',$g,'$login_user')");
	}
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
}
if(isset($_GET['done_id']))
{
	$id=$_GET['done_id'];
	
	$ge=$conn->query("select * from `sell_j` where `sell_j_id`=$id");
	$g_row=mysqli_fetch_array($ge);
	$amount=$g_row['amount']-$g_row['payed'];
	$cust=$g_row['sell_j_customer'];
		
	$c=$conn->query("select * from customer  where `customer_id`=$cust");
	$c_row=mysqli_fetch_array($c);	
	$old_statement=$c_row['statement'];	
	
	$new_statement=$old_statement+$amount;
	$conn->query("update `customer` set `statement`='$new_statement' where `customer_id`=$cust");
	
	
	$up="update `sell_j` set status=1 where `sell_j_id`=$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 or `status`=2)";
$check_r=$conn->query($check);
?>
<div class="container">
          <div class="page-inner">
            <div class="page-header">
              <h3 class="fw-bold mb-3">فرۆشتن</h3>
              <ul class="breadcrumbs mb-3">
                <li class="nav-home">
                  <a href="#">
                    <i class="icon-home"></i>
                  </a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="#"></a>
                </li>
                <li class="separator">
                  <i class="icon-arrow-right"></i>
                </li>
                <li class="nav-item">
                  <a href="#"></a>
                </li>
              </ul>
            </div>
			
			<div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <div class="card-title">فرۆشتنی کاڵا</div>
                  </div>
<?php if($check_row=mysqli_fetch_array($check_r))
{ 
	$status=$check_row['status'];
	$c_id=$check_row['customer_id'];
	$st=$check_row['statement'];
	$cur=$check_row['currency'];
	$payed=$check_row['payed'];
	$sell_j_id=$check_row['sell_j_id'];?>
	<table width="70%" border=1 id="basic-datatables"class="display table table-striped table-hover">
			<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>
				<?php if($status==2){ ?>
				<th><a href="sell_j.php?done_id=<?php echo $check_row['sell_j_id'];?>"><img src="icon/done.png" width=20px></a></th>
				<th><a href="sell_j.php?rej_id=<?php echo $check_row['sell_j_id'];?>"><img src="icon/del.png" width=20px></a></th>
				<?php } ?>
			</tr>
	</table>
	
	<table id="basic-datatables"class="display table table-striped table-hover">
	<?php if(isset($_POST['code_check_barcode'])){ 
			$check_code=$_POST['code_check_barcode'];
			$check2="select * from `items`,`item_set` where `i_s_barcode`='$check_code' and `code`=`i_s_code`";
			$check_r2=$conn->query($check2);
			$check_row2=mysqli_fetch_array($check_r2);
			$set=$check_row2['i_set_number'];
			$per=$check_row2['i_s_per'];
			$s_p=$check_row2['sell_price'];
			?>
			<form action="" method="post">
	
		<tr>
			<td style="background-color:#d6ebff">کۆد</td>
			<td style="background-color:#d6ebff">ژمارەی سێت</td>
			<td style="background-color:#d6ebff">عدد</td>
			<td style="background-color:#d6ebff">نرخ</td>
		</tr>
		<tr>
		
			
			<td>
				<input type="text" name="code_check_barcode" required onchange="this.form.submit()" class="form-control"style="width:180px">
			</td>
			<td>
				<?php echo $set;?>  سیت  X<?php echo $per;?>
			</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;?> class="form-control"style="width:180px">
			</td>
			<td>
				<input type="number" name="s" required class="form-control"style="width:180px">
			</td>
			
			<td>
				<input type="number" name="p" required value=<?php echo $s_p;?> step="any" class="form-control"style="width:180px">
			</td>
			<td>
				<input type="submit" name="submit2" value="زیادکردن" class="form-control" >
			</td>
		</tr>
		
		</form>
			<?php
	}
	else
	{
			?>
	
	
	
	<form action="" method="post">
	
		<tr>
			<td style="background-color:#d6ebff">کۆد</td>
			<td style="background-color:#d6ebff">ژمارەی سێت</td>
			<td style="background-color:#d6ebff">عدد</td>
			<td style="background-color:#d6ebff">نرخ</td>
		</tr>
		<tr>
			<td>
				<input type="text" name="code_check_barcode" required onchange="this.form.submit()" class="form-control" style="width:180px">
			</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'];?>" class="form-control">
		<tr>
			
			<td>
				<input type="text" name="c" required class="form-control" style="width:180px">
			</td>
			<td>
				<input type="number" name="s" required class="form-control" style="width:180px">
			</td>
			<td>
				<input type="number" name="n" required class="form-control" style="width:180px">
			</td>
			<td>
				<input type="number" name="p" required class="form-control"  style="width:180px">
			</td>
			<td>
				<input type="submit" name="submit2" value="زیادکردن" class="form-control">
			</td>
			<td></td>
		</tr>
		
		
		</form>
	<?php }?>
		<?php $gett=$conn->query("select * from `sell_j_item`,`items`,`item_set` where `i_s_barcode`=`sell_j_i_code` and `i_s_code`=`code`  and `sell_j`=$t_id ");
			$total=0;
			$ast=1;
			while($get_row=mysqli_fetch_array($gett)){ $ast=0;
			
			?>
			<tr>
				<th style="background-color:#d6ebff"><?php  echo $get_row['sell_j_i_code'];?><?php  echo $get_row['name'];?></th>
				<th style="background-color:#d6ebff"><?php echo $set=$get_row['sell_j_i_set'];?></th>
				<th style="background-color:#d6ebff"><?php $num=$get_row['i_s_per']; echo $numall=$set*$num;?></th>
				<th style="background-color:#d6ebff"><?php echo number_format($p=$get_row['sell_j_i_price'],2);?><?php echo $cur;?></th>
				<th style="background-color:#d6ebff"><?php echo number_format($t=$p*$numall,2); $total+=$t;?> <?php echo $cur;?></th>
				<?php if($status==0){ ?><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'];?>&&s=<?php echo $get_row['sell_j_i_set'];?>"><img src="icon/delete.png" width=20px></a></td><?php } ?>
			</tr>
			<?php }?>
			<tr bgcolor=#f9f9f9>
			<td></td>
			<td></td>
			<td></td><td style="background-color:#d6ebff"></td>
			<th style="background-color:#d6ebff"width=150px><?php echo $cur." ". number_format($total,2);?></th><td></td>
			<?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>
			<tr bgcolor=#f9f9f9>
			<td></td>
			<td></td>
			<td></td><td style="background-color:#d6ebff">قەرزی کۆن</td>
			<th style="background-color:#d6ebff" width=150px><?php echo $cur." ". number_format($st,2);  ?></th>
			<td></td>
			</tr>
			<tr bgcolor=#f9f9f9>
			<td></td>
			<td></td>
			<td></td><td style="background-color:#d6ebff">کۆی قەرز</td>
			<th style="background-color:#d6ebff" width=150px><?php echo $cur." ". number_format($st+$total,2); $remain=$st+$total-$payed; ?></th>
			<td></td>
			</tr>
			<tr bgcolor=#f9f9f9>
			<td></td>
			<td></td>
			<td >واصلکردن</td>
			<form action="sell_j.php" method="post">
			<td style="background-color:#d6ebff">
				<input type="hidden" name="d" value="<?php echo date('Y-m-d');?>">
				<input type="hidden" name="sell_j_id" value="<?php echo $sell_j_id;?>">
				<input type="hidden" name="c" value="<?php echo $c_id;?>">
				<input type="hidden" name="remain" value=<?php echo $remain;?>>
				<input type="hidden" name="cost" value=<?php echo $total;?>>
			</td>
			<th style="background-color:#d6ebff"><input type="number" step="any" value=<?php echo $payed;?> name="amount" style="width: 180px;"class="form-control"></th>
			<?php if(($status==0)&&($ast==0)){ ?><th><input type="submit" name="submitinv" value="کۆتایی" class="form-control"></th><?php } ?>
			</form></tr>
			
			
	</table>
	
	
<?php }
else{ ?>
        <form action="" method="post">
		<table style="margin-right:20px">
			<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="text" name="n" id="tag" class="form-control" style="width:300px"></td>
				<td><input type="submit" name="submit" value="ناردن" class="form-control" style="width:200px"></td>
			</tr>
			
	</table>
			
			
			</table>
        </form>
<?php } ?>
</div>
</div>
</div>

<div class="row">
              <div class="col-md-12">
                <div class="card">
                  
                  <div class="card-body">
                    <div class="table-responsive">
                      <table id="basic-datatables"class="display table table-striped table-hover">
<tr bgcolor="lightblue">
				<th></th><th></th><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 and `customer_id`!=1 and `type`!='q' order by `sell_j_id` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
	$rem=$row2['amount']-$row2['payed'];
	$c_id=$row2['customer_id'];
?>
			<tr style="border: solid 1px red;"><th><a  href="sell_j_edit.php?sell_j_id=<?php echo $row2['sell_j_id'];?>"><img src="icon/edit.png" width=30px></a></th>
			<th><a href="delivery.php?sell_j_id=<?php echo $row2['sell_j_id'];?>"><img src="icon/delivery.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>
			
				<td style=" font-size:18pt"><?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'];?>&&c_id=<?php echo $c_id;?>&&rem=<?php echo $rem;?>"><img src="icon/delete.png" width=30px></a></th>
				
				
			</tr>
<?php }?>
</table>  


</div>
                  </div>
                </div>
              </div>
			  
			  </div>
          </div>
        </div>




 <footer class="footer">
          <div class="container-fluid d-flex justify-content-between">
            <nav class="pull-left">
              <ul class="nav">
                <li class="nav-item">
                  <a class="nav-link" href="#">
                    
                  </a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#"> 009647504624373 </a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#">  </a>
                </li>
              </ul>
            </nav>
            <div class="copyright">
              2026, made with <i class="fa fa-heart heart text-danger"></i> by
              <a href="#">H Group</a>
            </div>
            <div>
              Powered by
              <a target="_blank" href="#">H Group</a>.
            </div>
          </div>
        </footer>
      </div>

     
    </div>
    <!--   Core JS Files   
    <script src="assets/js/core/jquery-3.7.1.min.js"></script>-->
    <script src="assets/js/core/popper.min.js"></script>
	
	
	
    <script src="assets/js/core/bootstrap.min.js"></script>

    <!-- jQuery Scrollbar -->
    <script src="assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>

  

    <!-- Sweet Alert -->
    <script src="assets/js/plugin/sweetalert/sweetalert.min.js"></script>

    <!-- Kaiadmin JS -->
    <script src="assets/js/kaiadmin.min.js"></script>

    <!-- Kaiadmin DEMO methods, don't include it in your project! -->
    <script src="assets/js/setting-demo.js"></script>
    <script src="assets/js/demo.js"></script>
    
  </body>
</html>
