

<?php $__env->startSection('title', 'المديونيات'); ?>
<?php $__env->startSection('content-header', 'المديونيات'); ?>


 

<?php $__env->startSection('content-actions'); ?>
المديونيات 
<?php $__env->stopSection(); ?>

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

        <?php
        if(isset($user_id)) { ?>
        <h3> 
            User : 
        <?php 
            echo \App\Models\User::where('id',$user_id)->first()->first_name." ";
            echo \App\Models\User::where('id',$user_id)->first()->last_name;
        ?>

  </h3>

       <?php      
        }
        ?>

<center>
    <a href="<?php echo e(route('customers.create')); ?>" class="btn btn-primary">اضافة عميل  جديد</a>
</cemter>
<div class="card">
    <div class="card-body">
        <div class="row">
            <div class="col-md-7"></div>
            <div class="col-md-5">
                <form action="<?php echo e(route('orders.index')); ?>">
                    <div class="row">
                        <div class="col-md-5">
                            <input type="date" name="start_date" class="form-control" value="<?php echo e(request('start_date')); ?>" />
                        </div>
                        <div class="col-md-5">
                            <input type="date" name="end_date" class="form-control" value="<?php echo e(request('end_date')); ?>" />
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-outline-primary" type="submit">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>




        <table class="table">
            <thead>


      <?php if(Config::get('app.locale') == "ar"): ?>
                <tr>
                    <th>#</th>
                    <th>اسم العميل</th>
                    <th>الفواتير</th>
                    <th style="display: none;">البائع</th>
                    <th>الاجمالي</th>
                    <th>المطلوب بعد الخصم </th>
                    <th>المدفوع</th>
                    <th>المتبقي</th>

                    <th>الحالة</th>
                    <th>-</th>
                    <th>التاريخ والزمن</th>
                </tr>

      <?php else: ?>
                <tr>
                    <th>ID</th>
                    <th>Customer Name</th>
                    <th>Invoices</th>
                    <th>User</th>
                    <th>Total</th>
                    <th>Paid</th>
                    <th>Reamining</th>

                    <th>Status</th>
                    <th>To Pay</th>
                    <th>Created At</th>
                </tr>

      <?php endif; ?>


            </thead>
            <tbody>

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

                ?>

                <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $r_customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>



                <?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();
                ?>


                <?php if($order->count() > 0): ?> 
                <tr>
                <?php else: ?>
                <tr style="display: none;">

                <?php endif; ?>
                    <td><?php echo e($r_customer->id); ?></td>
                    <td>
                        <a href="<?php echo e(url('customer_payments')); ?>/<?php echo e($r_customer->id); ?>"> 
                        <?php echo e($r_customer->first_name); ?> <?php echo e($r_customer->last_name); ?>


                         </td>



                    <?php 
                    

                        $mm = array();

                        foreach($order as $r2) {


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


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


                        $the_orders = \App\Models\Order::where('id',$r2->id)->get();

                        foreach($the_orders as $the_order) {
                           $the_discount = $the_discount + $the_order->discount;
                        }




                        $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;
                            }

                        }





                        }

                    
                    ?>


       
                    <td> 


                        <?php 

                        foreach($mm as $key=>$value) { 
                        ?>
                        <a href="#" onclick="window.open('<?php echo url('admin/ticket'); ?>/<?php echo $value; ?>','','width=400,height=500');">

                        <?php echo e($value); ?>

                        </a>

                    <?php } ?>


                    </td>

                

                    <td style="display: none;">

                        <?php if($order->count() > 0): ?> 
                        
                        <?php echo e($r2->first()->user->first_name); ?> <?php echo e($r2->first()->user->last_name); ?>

                        
                        <?php endif; ?>

                         </td>

                    <td><?php echo e(config('settings.currency_symbol')); ?> <?php echo e(number_format($total,0)); ?></td>

                    <td><?php echo e(config('settings.currency_symbol')); ?> <?php echo e(number_format($total - $the_discount,0)); ?></td>

                    <td><?php echo e(config('settings.currency_symbol')); ?> <?php echo e(number_format($r_customer->paid,0)); ?></td>
       


                    <td ><?php echo e(config('settings.currency_symbol')); ?> <?php echo e(number_format($total - $r_customer->paid - $the_discount,0)); ?>


                        <input type="hidden" id="remaining<?php echo $r_customer->id; ?>" value="<?php echo e(number_format($total - $r_customer->paid - $the_discount,0)); ?>" name="">
                    </td>


                    <td id="payment_status<?php echo $r_customer->id; ?>" style="color: red; font-size: 13px; font-weight: bolder;" >


  <img style="display: none;" src="" onerror=" if(document.getElementById('remaining<?php echo $r_customer->id; ?>').value <= 0) {
                            document.getElementById('payment_status<?php echo $r_customer->id; ?>').innerHTML = ' اكتمل الدفع ';
                        }
                        else
                        {
                            document.getElementById('payment_status<?php echo $r_customer->id; ?>').innerHTML = 'غير مدفوع';
                        }

                   ">

                    </td>

                    <td>

                        <a onclick="return confirm('<?php echo __('دقعيه جديده ؟') ?>');" href="<?php echo e(url('customers/paid')); ?>/<?php echo e($r_customer->id); ?>">
                          <span class="badge badge-info"><i class="fas fa-plus"></i> دفعيه جديده </span>
                        </a>


                        <a onclick="

                        if(document.getElementById('remaining<?php echo $r_customer->id; ?>').value <= 0)
                        {
                        return confirm('<?php echo __('هل انت متأكد أنه تم الدفع كامل ؟') ?>');
                        }
                        else
                        {
                            alert('ما زال هناك مبلغ متبقي  على العميل , الرجاء اضافته ثم عمل اكتمل الدفع ');
                            return false;
                        }



                        " href="<?php echo e(url('done_pay')); ?>/<?php echo e($r_customer->id); ?>">
                          <span class="badge badge-success"><i class="fas fa-check"></i> تم الدفع  </span>
                        </a>



                    </td>
                    <td>

                         <?php if($order->count() > 0): ?> 
                        <?php echo e($r2->created_at); ?>

                        <?php endif; ?>

                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </tbody>
      
        </table>
      
    </div>
</div>
<?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/orders/by_customers.blade.php ENDPATH**/ ?>