

<?php $__env->startSection('title', 'نظام المبيعات'); ?>

<?php $__env->startSection('content'); ?>

<style type="text/css">
	::placeholder {
		color: white;
	}
</style>

			
			<?php
				$invoice_id = Request::segment(3);
				$cust_id = Request::segment(4);
			?>

<script>
document.onkeypress = function(e) {



  var key = e.charCode || e.keyCode || 0;     
  


  if (key == 13) {

new_to_cart(document.getElementById('barcode').value,'0',document.getElementById('unit_id').value);
    //alert("I told you not to, why did you do it?");
    e.preventDefault();
	document.getElementById('barcode').value = "";
  }


//***** space for print
/*
   if(key == "32") {

document.getElementById('submit_btn').click();
    	//alert(document.getElementById('customer_id').value);
    	//alert(document.getElementById('cash2').value);
//confirm_order(document.getElementById('total_amount').value);

get_cart();
  }  */










}
</script>

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<script>
	$(document).ready(function() {




    $('.js-example-basic-single').select2();
});
</script>


                <?php /*
                $customers = \App\Models\Customer::where('id','<>','4')
                ->where('type','sale')
                ->get();


                ?>
                @foreach ($customers as $r_customer)



                <?php       

                   $total = 0;
                   $the_discount = 0;

                   $order = \App\Models\Order::where('customer_id',$r_customer->id)
                   ->where('id','>','1')
                   ->where('status','not_done')
                   ->where('order_status','active')
                   ->get();
                ?>


                @if($order->count() == 0) 

                <?php

                	$r_customer->paid = 0;
                	$r_customer->update();

                ?>
        
                @endif


                @endforeach

                */ ?>


<section class="content" style="min-height: 500px;">
	<img src onerror='get_cart();'>

	<div id="cart2" >
		<div class="row">
			<div class="col-md-6 col-lg-4">
				<div class="row mb-2">
					<div class="col">


						<form>
							<input type="text" id="barcode" class="form-control" 
							onmouseout="new_to_cart(this.value,'0',document.getElementById('unit_id').value);" 
							placeholder=" الباركود ...  " value="">


						</form>
					</div>
					<div class="col">




<?php if($cust_id <> "") { ?>

<input list="the_customers" value="<?php echo e($cust_id); ?>" 
 onchange="change_customer(this.value)"
 onmousemove="change_customer(this.value)"
 onmouseout="change_customer(this.value)"

  id="customer_id" name="customer_id" class="form-control" >

<?php } else { ?>

<input list="the_customers" value='4' placeholder="الرجاء اختيار العميل من هنا ..."
 onchange="change_customer(this.value)"
 onmousemove="change_customer(this.value)"
  onmouseout="change_customer(this.value)"

  id="customer_id" name="customer_id" class="form-control" >

<?php } ?>


<datalist name="the_customers" id="the_customers">

							<?php 
							$customers = \App\Models\Customer::
							where('type','sale')
							->get();							?>
							<?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
							<option value="<?php echo e($r->id); ?>"><?php echo e($r->first_name." ".$r->last_name); ?></option>
							<option value="<?php echo e($r->id); ?>"><?php echo e($r->phone); ?></option>
							<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

</datalist>  






					<?php /*

						<select onclick=""  onchange="get_cart();" name="customer_id" id="customer_id" class="form-control">
							<?php
							
						    /*
							$user_cart = User_Cart::where('user_id',auth()->user()->id);

							if($user_cart->count() > 0) {
							?>

							<option value="{{}}"></option>

							<?php

							} */

							/*
							?>

							<?php 
							$customers = \App\Models\Customer::
							where('type','sale')
							->get();							?>
							@foreach($customers as $r)
							<option value="{{$r->id}}">{{$r->first_name." ".$r->last_name}}</option>
							@endforeach
						</select>


						*/ ?>


					</div>
				</div>
				<div id="please_wait">
				</div>



<?php if($cust_id <> "") { 

$cc = \App\Models\Customer::find($cust_id);

	?>


					<input type="text" id="client_name" style="text-align: center; font-weight: bolder; font-size: 18px; " disabled="disabled" readonly="readonly" 
					value="<?php echo e($cc->first_name.' '.$cc->last_name); ?>" class="form-control">

<?php } else { ?>


					<input type="text" id="client_name" style="text-align: center; font-weight: bolder; font-size: 18px; " disabled="disabled" readonly="readonly" value="" class="form-control">

<?php } ?>
						


					<input type="text" id="customer_balance" style="text-align: center; font-weight: bolder; font-size: 18px; margin-top: 10px; " disabled="disabled" readonly="readonly" value="" class="form-control">

			



						<a id="delete_all" onclick="delete_all_cart();" style="color: red;
font-weight: bolder;
cursor: pointer;
">
							x
						</a>

				<div class="user-cart2" id="user_cart">
					<div class="card">
						<table class="table table-striped">
							<thead>

								<?php if(Config::get('app.locale') == "ar"): ?>
								<tr>
									<th>المنتج</th>
									<th>الكميـــــــة</th>
									<th class="text-right">السعر</th>
								</tr>
								<?php else: ?>
								<tr>
									<th>Product Name</th>
									<th>Quantity</th>
									<th class="text-right">Price</th>
								</tr>
								<?php endif; ?>
							</thead>
							<tbody>
							
							</tbody>
						</table>
					</div>
				</div>
		

			</div>
			<div class="col-md-6 col-lg-8" >
				<div class="mb-2">
					<div class="row ">


					<div class="col-md-1" style="text-align: center; display: ;">

						<i class="fas fa-home" onclick="show_cats();" style="color: blue; display: ;
font-size: 34px;
cursor: pointer; text-align: left;float: left; "></i> 
							<button class="btn btn-primary form-control"  onclick="show_cats();" style="font-size: 14px; font-weight: bolder; display: none ;">الاقسام الرئيسية</button>
					</div>	

					<div class="col-md-10" style="margin-right: -25px; margin-left: -25px;">



						<input type="text" id="search" onclick='' 
						onkeyup ="search(this.value);" class="form-control" placeholder="<?php echo e(__('البحث ....')); ?>" style="text-align: center; border-radius: 100px;

						/* background: #3490dc; color: white !important; */">
					</div>
					<div class="col-md-1" style="text-align: center; display: ;">

						<i class="fas fa-circle" onclick="show_cats();" style="color: blue; display: ;
							font-size: 34px;
cursor: pointer; text-align: left;float: left; visibility: hidden;"></i> 
							<button class="btn btn-primary form-control"  onclick="show_cats();" style="font-size: 14px; font-weight: bolder; display: none ;">Main Categories</button>
					</div>		
					</div>			

				
				</div>
				<div class="order-product">


					<?php 
					$cats = \App\categories::all();
					?>


					<div id="all_categories">
					<?php $__currentLoopData = $cats; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

	<div style="margin: 16px; border-radius: 10px; width: 20%; border: 0; background: none;" class="item"  onclick="get_items('<?php echo $r->id; ?>')">
		<center>
		<img src="<?php /*{{asset('storage')}}/{{$r->img}}*/ ?><?php echo e(asset('storage/categories')); ?>/product.jpg" width="100%" style="width: 156px;
height: 100px;
border: 0px solid gray;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
border-radius: 10px; cursor: pointer;"  alt="">
	</center>
		<h5 style="font-size: 16px;
font-weight: bolder;
text-align: center;
padding: 10px 16px 11px 16px;
border: 0px dashed;
background: #3490dc;
color: white;

border-radius: 100px;
margin-top: 2px;

 "><?php echo e($r->title); ?></h5>
	</div>

							<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>



					<?php /*
					 <div class="item" onclick="get_items('<?php echo $r->id; ?>')"><img src="{{asset('storage')}}/{{$r->img}}" alt="">
						<h5>{{$r->title}}</h5></div>
						*/ ?>





					</div>

					<div id="all_items"></div>

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


<script type="text/javascript">

function get_discount(discount) {

	document.getElementById('total_amount2').innerHTML= parseFloat(document.getElementById('total_amount').value) - parseFloat(discount);

	if(discount == 0) {
		document.getElementById('net_profit').value = parseInt(document.getElementById('net_profit2').value);
	}
	else {
		document.getElementById('net_profit').value =
		 parseInt(document.getElementById('net_profit2').value) - parseInt(discount);
	}


}

function show_cats() {

	document.getElementById("all_items").style.display = "none"; 
	document.getElementById("all_categories").style.display = "block";

}









function change_customer(customer_id) 
{



<?php 
	$customers = \App\Models\Customer::
	where('type','sale')
	->get();						
?>


<?php foreach($customers as $r) { ?>

	  if(customer_id == <?php echo $r->id ?> ) {

      document.getElementById("client_name").value = 
       "<?php echo $r->first_name; ?> <?php echo $r->last_name ?>";
 	
 	}

 <?php } // end foreach ?>












//***********************************************



<?php foreach($customers as $r) { ?>

	  if(customer_id == <?php echo $r->id ?> ) {

            <?php 
            $cust_id = $r->id;

            $paid = 0;
            $amount = 0;
            $balance = 0;

            $customers_payments = \App\customers_payments::where('customer_id',$cust_id)
            ->where('status','not_done')
            ->Orderby('id','desc')
            ->get();


            $customer_orders = \App\Models\Order::where('customer_id',$cust_id)
            ->where('order_status','active')
            ->where('status','not_done')
            ->get();




         foreach($customer_orders as $order) {

		$order_items = \App\Models\OrderItem::where('order_id',$order->id)->get();
		
		$invoice_amount = 0;

		foreach($order_items as $order_item) {
			$amount = $amount + ($order_item->price * $order_item->quantity);
			$invoice_amount = $invoice_amount + ($order_item->price * $order_item->quantity);
		}

		}


            foreach($customers_payments as $r)
		{
                   $paid = $paid + $r->amount; 

                   $balance = $amount - $paid;
		}

		?>


      document.getElementById("customer_balance").value = 
       "رصيد العميل <?php echo number_format($balance,0); ?>";
 	


} // end of if customer_id = $customer_id
<?php    } // end of foreach customers
            ?>

            



} // end of function change_customer





//*************************************************************************

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function change_unit(id,unit_id,cart_id) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp3 = new XMLHttpRequest();
  xhttp3.onreadystatechange = function() {

    if(ajax3.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";



    }
  };
  xhttp3.open("GET", "<?php echo e(url('/change_unit')); ?>"+"/"+id+"/"+unit_id+"/"+cart_id, true);
  xhttp3.send();

}





var ajax = false;
ajax = new XMLHttpRequest();
function get_items(id) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {

    if(ajax.readyState == 1 || 2 || 3) {
        document.getElementById("all_items").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	  document.getElementById("all_categories").style.display = "none"; 
		document.getElementById("all_items").style.display = "block"; 

      document.getElementById("all_items").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "<?php echo e(url('/all_items')); ?>"+"/"+id, true);
  xhttp.send();

}


//*************************************************************


var ajax2 = false;
ajax2 = new XMLHttpRequest();
function new_to_cart(id,quantity,unit_id,product_id) 
{


	if(id != "")
 //alert(document.getElementById('category_id').value);

  var xhttp2 = new XMLHttpRequest();
  xhttp2.onreadystatechange = function() {

    if(ajax2.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

     document.getElementById("submit_btn").style.visibility = "visible"; 

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

      //  document.getElementById("the_quantity").value = "";

document.onkeypress = function(e) {

	
  var key = e.charCode || e.keyCode || 0;     
  

  		if(key == 13) {


  		document.getElementById("search").value = "search";
  		document.getElementById("search").focus();
        document.getElementById("search").select();

     	//   document.getElementById("search").focus();
  		}
  		else {

  		}
  	
	}

	
        document.getElementById("the_quantity").focus();
        document.getElementById("the_quantity").select();




    }
  };
  xhttp2.open("GET", "<?php echo e(url('/new_to_cart')); ?>"+"/"+id+"/"+quantity+"/"+unit_id+"/"+product_id, true);
  xhttp2.send();

}


//*************************************************************************

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function delete_from_cart(id) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp3 = new XMLHttpRequest();
  xhttp3.onreadystatechange = function() {

    if(ajax3.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

    }
  };
  xhttp3.open("GET", "<?php echo e(url('/delete_from_cart')); ?>"+"/"+id, true);
  xhttp3.send();

}




//*************************************************************************

var ajax4 = false;
ajax4 = new XMLHttpRequest();
function get_cart() 
{

 //alert(document.getElementById('category_id').value);

  var xhttp4 = new XMLHttpRequest();
  xhttp4.onreadystatechange = function() {

    if(ajax4.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";

  document.getElementById("delete_all").style.display = "block";


    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

      document.getElementById('search').focus();

  document.getElementById("delete_all").style.display = "none";


    }
  };
  xhttp4.open("GET", "<?php echo e(url('/get_cart')); ?>", true);
  xhttp4.send();

}



//*************************************************************************

var ajax5 = false;
ajax5 = new XMLHttpRequest();
function confirm_order(amount) 
{

 document.getElementById("submit_btn").style.visibility = "hidden"; 


if(amount == 0) {
	alert('الرجاء اخيار  الطلبات أولا ثم الاعتماد ');
}

var bankak2 = document.getElementById('bankak').value;
var cash2 = document.getElementById('cash').value;
var shake2 = document.getElementById('shake').value;
var c_id2 = document.getElementById('customer_id').value;
var special2 = document.getElementById('special').value;
var the_discount = document.getElementById('discount').value;


if((parseInt(bankak2 + cash2 + shake2) < parseInt(amount)) && c_id2 == 4) {

//	alert(parseInt(bankak2 + cash2 + shake2)+ " -- amount " + parseInt(amount - the_discount));
alert('لا يمكنك اعتماد الفاتورة   , المبلغ المدفوع اقل من المبلغ المطلوب , الرجاء اختيار العميل أولا');
}


/*
if(special == 1 && c_id == 4) {
	alert('لقد اخترت فاتورة خاصة الرجاء اختيار اسم العميل ');
}*/

//if(amount > 0 && special == 0 || (special == 1 && c_id2 != 4))
if(amount > 0) 

if(parseInt(bankak2) + parseInt(cash2) + parseInt(shake2) >=  parseInt(amount - the_discount) || c_id2 > 4)
{
	
//	if( confirm('هل أنت متأكد من كل البيانات وترغب باعتماد  الطلب ؟'))

var c_id = document.getElementById('customer_id').value;
var discount = document.getElementById('discount').value;
var net_profit = document.getElementById('net_profit').value;

var bankak = document.getElementById('bankak').value;
var cash = document.getElementById('cash').value;
var shake = document.getElementById('shake').value;
var special = document.getElementById('special').value;

  var xhttp5 = new XMLHttpRequest();
  xhttp5.onreadystatechange = function() {

    if(ajax5.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }

    if (this.readyState == 4 && this.status == 200) {

var win = window.open('<?php echo url('admin/last_ticket'); ?>', '', 'width=400,height=500');

// Wait until the new window is fully loaded
win.onload = function() {
    win.focus();  // Make the new window focused (important for IE >= 10)
    win.print();  // Trigger the print dialog

    // When the printing is done, close the new window
    win.onafterprint = function() {
        win.close();  // Close the print window
    };
};

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
 		get_cart();

      document.getElementById("please_wait").innerHTML =
      "";


    }
  };

<?php if($invoice_id == null) { ?>

  xhttp5.open("GET", "<?php echo e(url('/confirm_order')); ?>" +"/"+c_id+"/"+amount+"/"+discount+"/"+net_profit+"/"+bankak+"/"+cash+"/"+shake+"/"+special+"/"+0, true);
  xhttp5.send();

<?php } else { ?>

  xhttp5.open("GET", "<?php echo e(url('/confirm_order')); ?>" +"/"+c_id+"/"+amount+"/"+discount+"/"+net_profit+"/"+bankak+"/"+cash+"/"+shake+"/"+special+"/"+"<?php echo $invoice_id; ?>", true);
  xhttp5.send();

<?php } ?> 


}
else {
alert('لا يمكنك اعتماد الفاتورة   , المبلغ المدفوع اقل من المبلغ المطلوب , الرجاء اختيار العميل أولا');

}





}




//*************************************************************************

var ajax6 = false;
ajax6 = new XMLHttpRequest();
function delete_all_cart() 
{


if( confirm('هل أنت متأكدد أنك تريد الغاء جميع   الطلبات ؟ '))


 //alert(document.getElementById('category_id').value);

  var xhttp6 = new XMLHttpRequest();
  xhttp6.onreadystatechange = function() {

    if(ajax6.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

    }
  };
  xhttp6.open("GET", "<?php echo e(url('/delete_all_cart')); ?>", true);
  xhttp6.send();

}


//*************************************************************************

var ajax7 = false;
ajax7 = new XMLHttpRequest();
function search(name) 
{

	if(name == "") {
		show_cats();
	}

		if(name != "")
 //alert(document.getElementById('category_id').value);

  var xhttp7 = new XMLHttpRequest();
  xhttp7.onreadystatechange = function() {

    if(ajax.readyState == 1 || 2 || 3) {
        document.getElementById("all_items").innerHTML =
      ""; 
    }
    if (this.readyState == 4 && this.status == 200) {

 	  document.getElementById("all_categories").style.display = "none"; 
		document.getElementById("all_items").style.display = "block"; 

      document.getElementById("all_items").innerHTML =
      this.responseText;
    }

  };
  xhttp7.open("GET", "<?php echo e(url('/search')); ?>"+"/"+name, true);
  xhttp7.send();

}

function pay_cash() {

   document.getElementById("submit_btn").style.visibility = "visible"; 

document.getElementById('cash').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('bankak').value = 0;
					document.getElementById('shake').value = 0;


}
function pay_bankak() {

   document.getElementById("submit_btn").style.visibility = "visible"; 

document.getElementById('bankak').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('cash').value = 0;
					document.getElementById('shake').value = 0;


					
}
function pay_shake() {

    document.getElementById("submit_btn").style.visibility = "visible"; 

document.getElementById('shake').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('cash').value = 0;
					document.getElementById('bankak').value = 0;


}

</script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\php\pear\~\resources\views/cart/index2.blade.php ENDPATH**/ ?>