<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="{{URL::to('/')}}/style.css">
        <title>{{config('settings.app_name')}} - Order {{$order->id}}</title>

        <style type="text/css">
            table {
                border: 1px dashed;
            }
            td { 
            border: 1px solid;
             }
             th {
                border: 1px solid;
             }
             tr {
                border: 0;
             }
        </style>
    </head>
    <body style="direction:rtl;">
        <div class="ticket" style="max-width: 100% !important; width: 93% !important; margin: 30px !important;">


	<?php /* 
            <div class="centered" style="margin-top: 5px; font-size:16px; "> <b> {{config('settings.app_name')}}  </b>

                <span style="font-size:15px; /overflow-x: scroll;">
                <br>{{config('settings.address')}}
		<br>
{{config('settings.phones')}} <br>

                </span>


            </div> */ ?>


          @if($order->type == 'sale' && $order->special == '0')


	<img src="<?php echo asset('header.PNG'); ?>" class="centered" style="width:100%">

	@endif       



            @if($order->special == '1')


	<img src="<?php echo asset('special.PNG'); ?>" class="centered" style="width:100%">

	@endif     


            @if($order->type == 'purchase')


	<img src="<?php echo asset('purchase.PNG'); ?>" class="centered" style="width:100%">

	    @endif


                <table border="0" style="text-align: center; width: 100%; border: 0; ">
                    <tr style="border: 0;">
                        <td style="border: 0px dashed; ">
                            الفاتورة : {{$order->id}}
                        </td>
                        <td style="border: 0px dashed; ">
                             {{$order->created_at}} 
                        </td>
                    </tr>

                    <tr style="border: 0;">


                        <td style="border: 0px dashed; ">
                         
	العميل : 
    <?php $customer_id = \App\Models\Order::where('id',$order->id)
    ->first()->customer_id;

    $cust_data = \App\Models\Customer::where('id',$customer_id)->first();

    echo $cust_data->first_name. " ".$cust_data->last_name;

      ?>
        
                        </td>


                        <td style="border: 0px dashed; ">
                            الموظف : 
<?php
  $user_data = auth()->user();
  echo $user_data->first_name." ".$user_data->last_name;
?>
                        </td>



                    </tr>
                </table>


            <table border="0" style="width:100%; max-width: 100%; border: 1px solid; line-height:20px; ">
                <thead>
                    <tr style="border: 1px solid;">
			<th class="quantity" style="width:5%;">
			#
			</th>
                        <th class="description" style="border: 1px solid;" >االصنف</th>

                        <th class="quantity">الوحدة </th>
                        <th class="quantity">الكمية</th>
                        <th class="price">السعر</th>
                        <th class="price">المجموع</th>



                    </tr>
                </thead>
                <tbody>

                    <?php
                    $get_orders = \App\Models\OrderItem::where('order_id',$order->id)
                    ->orderby('id','asc')
                    ->get();


                    $bankak = \App\Models\Payment::where('order_id',$order->id)->first()->bankak;
                    $cash = \App\Models\Payment::where('order_id',$order->id)->first()->cash;
                    $shake = \App\Models\Payment::where('order_id',$order->id)->first()->shake;




                    $paid = \App\Models\Payment::where('order_id',$order->id)->first()->amount;
                    $discount = \App\Models\Payment::where('order_id',$order->id)->first()->discount;

                    ?>

                    <?php $total = 0;
			  $number = 0;

			 ?>

                    @foreach($get_orders as $r)

                    <?php $buy_price = \App\Models\Product::where('id',$r->product_id)->first()->buy_price; ?>

			<?php $number = $number + 1; ?>

                   
                    <tr>

			<td class="quantity" style="text-align: center ; border: 1px solid;">{{$number}}</td>
                        <td class="description" style="text-align: center; border: 1px solid; "> {{$r->product->name}}</td>

              

                        <td class="quantity" style="text-align: center ; border: 1px solid;">{{$r->unit->unit}}</td>

                        <td class="quantity" style="text-align: center ; border: 1px solid;">{{$r->quantity}}</td>
                        <td class="price" style="text-align: center; border: 1px solid; ">
                            @if($r->type == "purchase") 

                            {{number_format($buy_price,0)}}

                            <?php 

                         //   $the_price = $r->price * $r->quantity;
                            $total = $total + ($buy_price * $r->quantity);
                            ?>

                            @else

                            {{number_format($r->price,0)}}

                            <?php 

                         //   $the_price = $r->price * $r->quantity;
                            $total = $total + ($r->price * $r->quantity);
                            ?>

                            @endif


                        </td>


                       
 <td style="text-align: center; ">

                            @if($r->type == "purchase") 

                            {{number_format($r->quantity * $buy_price,0)}}

                            @else

                            {{number_format($r->quantity * $r->price,0)}}

                            @endif


                        </td> 

<?php /*
                        @if($r->type == "purchase")

                        <td style="text-align: center; font-weight:bolder;">
                            {{$r->expire_date}}
                        </td>

                        @else

                       

                        @endif */ ?>

                    </tr>
                    @endforeach


           

                    <tr>
                        <td class="quantity" colspan="3" style="text-align: center; font-weight: bolder; font-size: 16px; ">الاجمالي </td>
                       

                        <td class="price" colspan="3" style="text-align: center;  font-weight: bolder; font-size: 16px;">{{number_format($total,0)}} </td>
                    </tr>

                     <tr>
                        <td class="quantity" colspan="3" style="text-align: center; font-weight: bolder; font-size: 16px; ">التخفيض </td>
                     

                        <td class="price" colspan="3" style="text-align: center;  font-weight: bolder; font-size: 16px;">{{number_format($discount,0)}} </td>
                    </tr>

                     <tr>
                        <td class="quantity" colspan="3" style="text-align: center; font-weight: bolder; font-size: 16px; ">المدفوع </td>
                       

                        <td class="price" colspan="3" style="text-align: center;  font-weight: bolder; font-size: 16px;">{{number_format($bankak + $cash + $shake ,0)}} </td>
                    </tr>



                     <tr>
                        <td class="quantity" colspan="3" style="text-align: center; font-weight: bolder; font-size: 16px; ">المتبقي</td>
                       

                        <td class="price" colspan="3" style="text-align: center;  font-weight: bolder; font-size: 16px;">

			<?php if($total - $bankak + $cash + $shake < 0) { 
			
			echo $total; 
			
			
			 } else {

			 
			echo number_format($total - ($bankak + $cash + $shake) - $discount ,0);  	
		
				} ?>

			 </td>
                    </tr>



                </tbody>
            </table>
            <br>
  <center><span class="centered" style="
    /* visibility: hidden; */
    text-align: center !important;
">

                <?php       

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

                    <?php 
                    

                        $mm = array();

                        foreach($order as $r2) {


                        array_push($mm, $r2->id);


                        if($order->count() > 0) {

                        $order_items = \App\Models\OrderItem::where('order_id',$r2->id)
                        ->where('status','not_done')
                        ->get();


                            foreach($order_items as $r) {
                            $price = $r->price * $r->quantity;
                            $total = $total + $price;
                            }

                        }





                        }

                    
                    ?>



<?php
$the_customer = \App\Models\Customer::where('id',$customer_id)->first();
?>

اجمالي حساب العميل : {{number_format($total - $cust_data->paid ,0)}}

<br>
شكرا لكم لزيارتنا 

<br>

<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; التوقيع : _____________



                </span>


</center>
            
        </div>


<!--
        <div class="break"></div> 

    -->

        <div class="ticket" style="max-width: 100% !important; width: 100% !important; margin: 0 !important; text-align: center;
line-height: 9px;">

  


            <br>
                <span style="font-size: 9px; text-align: center; display:none">
                تطوير ايسن لتقنية المعلومات 
                  0123003721 - 0926225530
                </span>

            
        </div>


        <button id="btnPrint" style="" class="hidden-print">اطبع الفاتورة</button>
      
        <script src="{{URL::to('/')}}/script.js"></script>
    </body>
</html>