


<?php
require('includes/head.php');
if(isset($_POST['submit']))
{
	$c2=$_POST['customer'];
	$n=$_POST['note'];
	//$d=$_POST['d'];
	$d=date("Y-m-d h:i:s");
	$a=$_POST['amount'];
	
	$add="INSERT INTO `get`(`get_customer`, `get_date`, `note`, `amount`) 
	VALUES ('$c2','$d','$n','$a')";
	$conn->query($add);
	echo "<script type='text/javascript'>window.location.href = 'get.php'</script>";	
	
}


if(isset($_GET['t_id']))
{
	$t_id=$_GET['t_id'];
   
	
	$del2="delete from  `get` where `get_id`=$t_id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'get.php'</script>";	
	
}
if(isset($_GET['done_id']))
{
	$t_id=$_GET['done_id'];
	$c_id=$_GET['c_id'];
	$c_date=$_GET['c_date'];
	$a=$_GET['amount'];
	
	
	$s_j=$conn->query("select * from `customer` where `customer_id`=$c_id ");
		$s_j_row=mysqli_fetch_array($s_j);
		$old_num=$s_j_row['statement'];
		$new_num=$old_num-$a;
		
		$up=$conn->query("update customer set `statement`='$new_num' where `customer_id`=$c_id ");
	
	$up="update `get` set status=1 where `get_id`=$t_id";
	$conn->query($up);
	
	$conn->query("INSERT INTO `sell_j_inv`( `inv_type`, `inv_code`, `customer`, `date`, `user`) 
										VALUES ('get',$t_id,'$c_id','$c_date','$login_user')");
	echo "<script type='text/javascript'>window.location.href = 'get.php'</script>";	
	
}
$c="select * from `customer` where active=1";
$c_r=$conn->query($c);


$check="select * from `get`,`customer` where `get_customer`=`customer_id` and `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['customer_id'];
$c_date=$check_row['get_date'];?>
	<table
                        id="basic-datatables"
                        class="display table table-striped table-hover"
                      >
			<tr>
				<th style="background-color:#d6ebff; font-size:16pt" >کڕیار: <?php echo $check_row['customer_name'];?></th>
				<th style="background-color:#d6ebff; font-size:16pt">بڕی: <?php echo $check_row['amount']."$ /".$check_row['note'];$amount=$check_row['amount']?></th>
				<th style="background-color:#d6ebff; font-size:16pt"><a href="get.php?t_id=<?php echo $check_row['get_id'];?>"><img src="icon/delete.png" width=20px></a></th>
			
				<th style="background-color:#d6ebff; font-size:16pt"><a href="get.php?done_id=<?php echo $check_row['get_id'];?>&&amount=<?php echo $amount;?>&&c_id=<?php echo $c_id;?>&&c_date=<?php echo $c_date;?>"><img src="icon/done.png" width=20px></a></th>
			</tr>
	</table>
	
	
	
	
<?php }
else{ 
if(isset($_POST['search'])){ 
	$c_name=$_POST['n'];
	$get="select `customer_id`,`statement`,`currency` from `customer` where customer_name='$c_name'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$c_id=$get_row['customer_id'];
	$st=$get_row['statement'];
	$cur=$get_row['currency'];
	


?>


			<table style="text-align:center;font-size:10pt" bgcolor="#d1eaf0" id='mydiv' width='740px' dir=rtl align="center">					
				<tr bgcolor="#708090" style="color:#ffffff">
					<td>مامەڵە</td>
					<td>بەروار</td>
					<td>ژمارەی وصل</td>
					
					
					<td>بڕی پارە</td>
					<td>باقی پارە</td>
				</tr>
				<?php
				$count=1;
				
				$get2="select * from `sell_j_inv` where  `customer`=$c_id order by `date`";
				$get_r2=$conn->query($get2);
				$sum=0;
				$sum_w=0;
				$sum_t=0;
				while($get_row2=mysqli_fetch_array($get_r2))
				{ 
				if($count%2==0) $col="#f9f9f9"; else $col="white"; 
				$count++;
					$type=$get_row2['inv_type'];
					$code=$get_row2['inv_code'];
					if($type=="sell")
					{
						$transfer="select * from `sell_j` where `sell_j_id`=$code";
						$transfer_r=$conn->query($transfer);
						$transfer_row=mysqli_fetch_array($transfer_r);
						$sell_j=$transfer_row['sell_j_id'];
					
								$p="select * from `sell_j_item` where `sell_j`='$sell_j'";
								$p_r=$conn->query($p);
								$tt=0;
								while($p_row=mysqli_fetch_array($p_r))
								{
									$ss=$p_row['sell_j_i_set'];
									$pp=$p_row['sell_j_i_price'];
									$codee=$p_row['sell_j_i_code'];
									$gg="select `i_s_per` as `per` from `item_set` where `i_s_barcode`='$codee'";
									$get_per=$conn->query($gg);
									$row_per=mysqli_fetch_array($get_per);
									$per=$row_per['per'];
									
									$t=$ss*$pp*$per;
									$tt+=$t;
								}
					
						
						?>
						<tr bgcolor="<?php echo $col;?>">
							<th style="color:#7A7DCE">فرۆشتن</th>
							<td style="color:#bb141c;background-color:#f0f0f0; font-size:8pt"><?php echo $transfer_row['sell_j_date'];?></td>
							<td><a href="sell_j_print.php?sell_j_id=<?php echo $transfer_row['sell_j_id'];?>"><?php echo $transfer_row['sell_j_id'];?></td>
							
							
							<td><?php echo $cur;?> <?php echo number_format($tt,2); $sum+=$tt;?></td>
							
							
							
							
							<th width=120px dir=ltr bgcolor="#a9a9a9"> <?php echo number_format($sum,2);?> <?php echo $cur;?></th>
						</tr>
						<?php 
				}
					elseif($type=="get")
					{
						$get="select * from `get` where `get_id`=$code";
						$get_r=$conn->query($get);
						$get_row=mysqli_fetch_array($get_r);
					?>
						<tr bgcolor="<?php echo $col;?>">
							<th style="color:green">پارە وەرگرتن</th>
							<td style="color:#bb141c;background-color:#f0f0f0;font-size:8pt"><?php echo $get_row['get_date'];?></td>
							<td><a href="pay_print_get.php?id=<?php echo $get_row['get_id'];?>"><?php echo $get_row['get_id'];?></a></td>
							<th dir=ltr style="color:#bb141c" bgcolor="#cccccc"> <?php echo number_format($p=$get_row['amount'],2); $sum-=$p;?> <?php echo $cur;?></th>
							<th dir=ltr bgcolor="#a9a9a9"> <?php echo number_format($sum,2);?> <?php echo $cur;?></th>
						</tr>
					<?php
					}
					
					elseif($type=="debt")
					{
						$transfer="select * from `sell_j` where `sell_j_id`=$code";
						$transfer_r=$conn->query($transfer);
						$transfer_row=mysqli_fetch_array($transfer_r);
						$sell_j=$transfer_row['sell_j_id'];
					?>
						<tr bgcolor="<?php echo $col;?>">
							<th style="color:green">قەرزی کۆن</th>
							<td style="color:#bb141c;background-color:#f0f0f0;font-size:8pt"><?php echo $transfer_row['sell_j_date'];?></td>
							<td><?php echo $transfer_row['sell_j_id'];?></td>
							<th dir=ltr style="color:green" bgcolor="#ffffff"> <?php echo number_format($p=$transfer_row['amount'],2); $sum+=$p;?> <?php echo $cur;?></th>
							<th dir=ltr bgcolor="#a9a9a9"> <?php echo number_format($sum,2);?> <?php echo $cur;?></th>
						</tr>
					<?php
					}
				
					
				
					
				}?>
				<tr bgcolor="#f9f9f9">
					
					<th colspan=2>کۆی گشتی باقی پارە</th>
					<th colspan=2><?php if($sum<0) echo "ماوە لای ئێمە"; elseif($sum>2) { echo "ماوە لای ئەوان";}?></th>
					<th dir=ltr style="color:#ffffff" bgcolor="#708090"> <?php echo number_format($sum,2);?> <?php echo $cur;?></th>
					
				</tr>
			</table>



        <form action="" method="post">
		<input type="hidden" name="customer" value=<?php echo $c_id;?>>
		 <table
                        id="basic-datatables"
                        class="display table table-striped table-hover"
                      >
			
			<tr>
				
				<td  style="background-color:#d6ebff; font-size:16pt">
					<input type="text" style=" font-size:16pt" value="<?php echo $c_name;?>" disabled class="form-control">
				</td>
				
				<td style="background-color:#d6ebff; font-size:16pt">
					<input step="any" type="number" name="amount" placeholder="بڕی پارە" class="form-control">
				</td>
				<!--<td style="background-color:#d6ebff; font-size:16pt"><input type="date" name="d" value="<?php echo date("Y-m-d h:i:s");?>" required class="form-control"></td>-->
				<td style="background-color:#d6ebff; font-size:16pt"><input type="text" name="note" class="form-control"></td>

			

				<td style="background-color:#d6ebff; font-size:16pt"><input type="submit" name="submit" value="واصلکردن" class="form-control"></td>
			</tr>
			</table>
        </form>
<?php 
}
else{ ?>
	 <form action="" method="post">
		<table>
			<tr>
				<th>کڕیارەکان</th>
			</tr>
			<tr>
				<td><td><div class="form-group">
					<input type="text" name="n" id="tag" class="form-control"></div>
				</td>
				

			

				<td><input type="submit" name="search" 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>
				<th>بڕی پارە</th>
				
			</tr>
<?php
$check2="select * from `get`,`customer` where `get`.`get_customer`=`customer`.`customer_id` and `status`=1 and `customer_id`!=1 order by `get_date` desc";
$check_r2=$conn->query($check2);
while($row2=mysqli_fetch_array($check_r2)){
?>
			<tr style="border: solid 1px red;"><th><a href="pay_print_get.php?id=<?php echo $row2['get_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['get_date'];?></td>
				<td><?php echo $row2['amount'];?></td>
				<td><?php echo $row2['note'];?></td>
				
				
			<th><a onclick='return confirm("دڵنیای لە سڕینەوەی ئەم پسووڵەیە؟؟؟؟");' href="inv_del.php?get_id=<?php echo $row2['get_id'];?>&&amount=<?php echo $row2['amount'];?>&&c_id=<?php echo $row2['customer_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';?>

<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>