<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	$c2=$_POST['store'];
	$date=$_POST['d'];
	$n=$_POST['n'];
	
	
	$target_dir = "p_fatura_image/";
	 $file_name = $timestamp . "_" . basename($_FILES["image"]["name"]);
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir . $file_name)) {
        $image = $file_name; 
    } else {
        $image = ""; 
    }
	
	$add="INSERT INTO `purchase_fatura`(`p_f_store`, `p_f_date`, `p_f_num`,`p_f_user`,`p_f_image`) 
	VALUES ('$c2','$date','$n','$login_user','$image')";
	$conn->query($add);
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
	
}
if(isset($_POST['submit2']))
{
	$barcode=$_POST['code'];
	
	 echo $check="select * from `item_set` where `i_s_barcode`='$barcode'";
	  $check_r=$conn->query($check);
	  $check_row=mysqli_fetch_array($check_r);
	  {
		  $c=$check_row['i_s_code'];
	  }
	
	
	
	$p_f=$_POST['p_f'];
    $set_n=$_POST['set_num'];
	 //$n=$_POST['num'];
	$p_p=$_POST['p_price'];
	$s_p=$_POST['s_price'];
	$note=$_POST['note'];
	
	
	$add="INSERT INTO  `purchase_fatura_item`( `p_f`, `p_f_i_code`, `p_f_i_set`, `p_price`,  `s_price`, `note`)  
	VALUES ('$p_f','$barcode','$set_n','$p_p','$s_p','$note')";
	$conn->query($add);
	
	$get="select `i_set_number` from `item_set` where `i_s_barcode`='$barcode'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$set_num_old=$get_row['i_set_number'];
	
	
	$new_set_num=$set_n+$set_num_old;
	
	$up="UPDATE `item_set` SET `i_set_number`='$new_set_num' WHERE `i_s_barcode`='$barcode'";
	$conn->query($up);
	
	$up="UPDATE `items` SET `purchase_price`='$p_p',`sell_price`='$s_p' WHERE `code`='$c'";
	$conn->query($up);
	
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
	
}
if(isset($_GET['p_f_i_id']))
{
	$p_f_i_id=$_GET['p_f_i_id'];
	$c=$_GET['c'];
	$s=$_GET['s'];
	$barcode=$_GET['barcode'];
   
	$get="select `i_set_number` from `item_set` where i_s_barcode='$barcode'";
	$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='$barcode'";
	$conn->query($up);
	
	
	$del="delete from  `purchase_fatura_item` where `p_f_i_id`=$p_f_i_id";
	$conn->query($del);
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
	
}
if(isset($_GET['t_id']))
{
	$t_id=$_GET['t_id'];
   
	
	$del="delete from  `purchase_fatura_item` where `p_f`=$t_id";
	$conn->query($del);
	$del2="delete from  `purchase_fatura` where `p_f_id`=$t_id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
	
}
if(isset($_GET['done_id']))
{
	$t_id=$_GET['done_id'];
	$c_id=$_GET['c_id'];
	$c_date=$_GET['c_date'];
   
	
	$up="update `purchase_fatura` set p_f_status=1 where `p_f_id`=$t_id";
	$conn->query($up);
	
	$conn->query("INSERT INTO `purchase_inv`(`date`,`store`, `inv_type`, `inv_code`,`p_i_user`) VALUES ('$c_date',$c_id,'purchase',$t_id,'$login_user')");
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
	
}
$c="select * from `store`where active=1";
$c_r=$conn->query($c);



$check="select * from `purchase_fatura`,`store` where `purchase_fatura`.`p_f_store`=`store`.`store_id` and `p_f_status`=0";
$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))
{ $c_id=$check_row['store_id'];
$c_date=$check_row['p_f_date'];?>
	<table width="70%" border=1 id="basic-datatables"class="display table table-striped table-hover" >
			<tr>
				<th> <a href="p_fatura_image/<?php echo $check_row['p_f_image'];?>"><img height="50px" src="p_fatura_image/<?php echo $check_row['p_f_image'];?>"><a></th>
				<th>فرۆشیار: <?php echo $check_row['store_name'];?></th>
				<th>ژمارەی وصل: <?php echo $check_row['p_f_num'];?></th>
				<th>ڕێکەوت: <?php echo $check_row['p_f_date'];?></th>
				<th>
				<?php
				$p_f=$check_row['p_f_id'];
				$check2="select * from `purchase_fatura_item` where `p_f`=$p_f";
				$check_r2=$conn->query($check2);
				$num_row=mysqli_num_rows($check_r2);
				if($num_row==0){
				?>
					<a href="purchase.php?t_id=<?php echo $check_row['p_f_id'];?>"><img src="icon/delete.png" width=20px></a>
				<?php } ?>	
				</th>
				<th><a href="purchase.php?done_id=<?php echo $check_row['p_f_id'];?>&&c_id=<?php echo $c_id;?>&&c_date=<?php echo $c_date;?>"><img src="icon/done.png" width=20px></a></th>
			</tr>
	</table>
	
	<table id="basic-datatables"class="display table table-striped table-hover" >
	<form action="" method="post">
	<input type="hidden" name="p_f" value="<?php echo $p_f=$check_row['p_f_id'];?>">
		<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>
			<td style="background-color:#d6ebff">نرخی فرۆشتن</td>
			<td style="background-color:#d6ebff">تێبینی</td><td style="background-color:#d6ebff"></td>
		</tr>
		<tr >
			<td>
				<input style="width:6em" type="text" name="code" required class="form-control">
			</td>
			<td>
				<input style="width:6em" type="number" name="set_num" required class="form-control">
			</td>
			<td>
				<input style="width:6em" type="number" name="p_price" required step="0.01"class="form-control">
			</td>
			<td style="width:8em"></td>
			
			<td>
				<input style="width:8em" type="number" name="s_price" class="form-control">
			</td>
			
			<td>
				<input style="width:10em" type="text" name="note" placeholder="Note"class="form-control">
			</td>
			
			<td>
				<input type="submit" name="submit2" value="زیادکردن"class="form-control">
			</td>
		</tr>
		<?php $gett=$conn->query("select * from `purchase_fatura_item`,`items`,`item_set` where `p_f`=$p_f and `p_f_i_code`=`i_s_barcode` and `code`=`i_s_code`");
			$total=0;$p_num=0;$p_set=0;
			while($get_row=mysqli_fetch_array($gett)){ $per=$get_row['i_s_per'];$c=$get_row['code'];?>
			<tr>
				<th><?php  echo $barcode=$get_row['p_f_i_code'];?><br><?php  echo $get_row['name'];?></th>
				<th><?php echo $set2=$get_row['p_f_i_set'];$p_set+=$set2;?></th>
				<th>$ <?php echo $p=$get_row['p_price'];?></th>
				<th>$ <?php echo $t=$p*$per*$set2; $total+=$t;?></th>
				
				<th><?php echo $p=$get_row['s_price'];?></th>
				<th><?php echo $get_row['note'];?></td>
				<td><a href="purchase.php?p_f_i_id=<?php echo $get_row['p_f_i_id'];?>&&c=<?php echo $c;?>&&barcode=<?php echo $barcode;?>&&s=<?php echo $set2;?>"><img src="icon/delete.png" width=20px></a></td>
			</tr>
			<?php }?>
			<tr bgcolor=#f9f9f9>
			<td style="background-color:#d6ebff"></td>
			<td style="background-color:#d6ebff"><?php echo $p_set;?></td>
			<td style="background-color:#d6ebff"></td>
			<th style="background-color:#d6ebff" width=150px><?php echo number_format($total,2);?> $</th>
			<td style="background-color:#d6ebff"></td><td style="background-color:#d6ebff"></td><td style="background-color:#d6ebff"></td>
		</tr>
	</table>
	
	
<?php }
else{ ?>
        <form action="" method="post"  enctype="multipart/form-data">
		<table >
			<tr>
				<th>ژمارەی وصل</th>
				<th>فرۆشیار</th>
				<th>ڕێکەوت</th>
				<th>وێنە</th>
			</tr>
			<tr>
				<td><input type="number" name="n"  required class="form-control" style="width:200px"></td>
				<td>
					<select name="store" required class="form-select"style="width:300px">
							<option value=""></option>
							<?php while($row2=mysqli_fetch_array($c_r)){ ?>
						<option value="<?php echo $row2['store_id'];?>"><?php echo $row2['store_name'];?></option>
						 <?php }?>
					</select>
				</td>
				<td><input type="date" name="d" value="<?php echo date("Y-m-d");?>" required class="form-control"> </td>
				<td> <input type="file"  name="image" class="form-control" style="width:300px"></td>
				<td><input type="submit" name="submit" value="کڕین" class="form-control"></td>
			</tr>
			</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>
			</tr>
<?php
$check2="select * from `purchase_fatura`,`store` where `purchase_fatura`.`p_f_store`=`store`.`store_id` and `p_f_status`=1 order by `p_f_id` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
?>
			<tr style="border: solid 1px red">
			<th> <a href="p_fatura_image/<?php echo $row2['p_f_image'];?>"><img height="50px" src="p_fatura_image/<?php echo $row2['p_f_image'];?>"><a></th>
				<td ><?php echo $row2['store_name'];?></td>
				<td><?php echo $row2['p_f_date'];?></td>
				<!--<th><a href="send_edit_purchase.php?id=<?php echo $row2['p_f_id'];?>"><img src="icon/edit.png" width=30px></a></th>-->
				<th><a onclick='return confirm("دڵنیای لە سڕینەوەی ئەم پسووڵەیە؟؟؟؟");' href="inv_del.php?purchase_id=<?php echo $row2['p_f_id'];?>"><img src="icon/delete.png" width=30px></a></th>

			</tr>
<?php }?>
</table>  
</div>
                  </div>
                </div>
              </div>
			  
			  </div>
          </div>
        </div>
<?php require'includes/foot.php';?>