@extends('layouts.admin')

@section('title', 'Orders List')



 

@section('content-actions')


<?php 
$last_shift = \App\shifts::orderby('id','desc')->first();
?>
@if($last_shift)
    @if($last_shift->shift_end == null) <!-- This means the shift is open -->
        <!-- If shift is open, show Close Shift button -->
<a href="{{url('admin/pos')}}" class="btn btn-primary"> نافذة البيع</a>

        <!-- If shift is closed, show Begin Shift button -->
    @endif
@endif


@endsection



@section('content')

<?php 



$all_not_done = 0;
$orders_not_done = \App\Models\OrderItem::where('status','not_done')->get();

foreach($orders_not_done as $ord)
{
  $all_not_done = $all_not_done + ($ord->price * $ord->quantity);
}















$all_customers_paid = 0;
$all_customers = \App\Models\Customer::where('id','<>','4')->get();

foreach($all_customers as $all_customers)
{
  $all_customers_paid = $all_customers_paid + $all_customers->paid;
}










/////////////////////////////////////////////////////////////////
$customer_bankak = 0;
$customer_cash = 0;
$customer_shake = 0;

$customer_bankak2 = \App\customers_payments::where('payment_method','bankak')
->where('status','not_done')
->get();
$customer_cash2 = \App\customers_payments::where('payment_method','cash')
->where('status','not_done')
->get();
$customer_shake2 = \App\customers_payments::where('payment_method','shake')
->where('status','not_done')
->get();

foreach($customer_bankak2 as $r) {
  $customer_bankak = $customer_bankak + $r->amount;
}

foreach($customer_cash2 as $r) {
  $customer_cash = $customer_cash + $r->amount;
}

foreach($customer_shake2 as $r) {
  $customer_shake = $customer_shake + $r->amount;
}

?>

















<?php
//////////////////////////// Payments Credit Sale
$bankak = 0;
$cash = 0;
$shake = 0;

$payments = \App\Models\Payment::where('type','sale')
->where('order_status','active')
->get();
?>

@foreach ($payments as $r)
    
    <?php

    $bankak = $bankak + $r->bankak;
    $cash = $cash + $r->cash;
    $shake = $shake + $r->shake;

    ?>

@endforeach





<?php
//////////////////////////// Payments Depit Purchases
$purchase_bankak = 0;
$purchase_cash = 0;
$purchase_shake = 0;

$payments = \App\Models\Payment::where('type','purchase')->get();
?>

@foreach ($payments as $r)
    
    <?php


    $purchase_bankak = $purchase_bankak + $r->bankak;
    $purchase_cash = $purchase_cash + $r->cash;
    $purchase_shake = $purchase_shake + $r->shake;

    ?>

@endforeach






<?php
//////////////////////////// Payments Depit Expeses
$expense_cash = 0;

$payments_cash = \App\expenses::where('payment_method','cash')->get();
?>

@foreach ($payments_cash as $r)
    
    <?php

    $expense_cash = $expense_cash + $r->expense_price;

    ?>

@endforeach



<?php
//////////////////////////// Payments Depit Expeses
$expense_bankak = 0;

$payments_bankak = \App\expenses::where('payment_method','bankak')->get();
?>

@foreach ($payments_bankak as $r)
    
    <?php

    $expense_bankak = $expense_bankak + $r->expense_price;

    ?>

@endforeach





<div class="row" style="text-align: center; display: none;">
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-danger">
              <div class="inner">

                <?php
                  //  dd($purchase_cash);
                ?>
                <h3>{{config('settings.currency_symbol')}} {{number_format($bankak - $expense_bankak - $purchase_bankak ,0)}}</h3>

                <p> حساب بنكك
</p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
             
            </div>
          </div>
          <!-- ./col -->

                    <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-success">
              <div class="inner">
                <h3>{{config('settings.currency_symbol')}}

                 {{number_format($cash - $expense_cash - $purchase_cash ,0) }} </h3>

                <p> Cash | الكاش  </p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              
            </div>
          </div>
          <!-- ./col -->

      </div>














<div class="row" style="text-align: center; display:none">


          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-danger">
              <div class="inner">

                <?php
                  //  dd($purchase_cash);
                ?>
                <h3>{{config('settings.currency_symbol')}}

                 {{number_format($bankak + $customer_bankak ,0)}}</h3>

                <p> حساب بنكك
</p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
             
            </div>
          </div>
          <!-- ./col -->

                    <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-success">
              <div class="inner">
                <h3>{{config('settings.currency_symbol')}}

                 {{number_format($cash + $customer_cash ,0) }} </h3>

                <p> Cash | الكاش  </p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              
            </div>
          </div>
          <!-- ./col -->



          <!-- ./col -->

                    <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-info">
              <div class="inner">
                <h3>{{config('settings.currency_symbol')}}

                 {{number_format($shake + $customer_shake ,0) }} </h3>

                <p> الشيكات </p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              
            </div>
          </div>
          <!-- ./col -->


          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box badge-warning">
              <div class="inner">
                <h3>{{config('settings.currency_symbol')}}

                  <?php /*
                 {{ number_format($all_total + $all_customers_paid + $all_not_done  - $discount, 2) }}

                 */ ?>

                 {{ number_format($all_total - $discount, 2) }}


               </h3>

                <p> اجمالي المبيعات  </p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              
            </div>
          </div>
          <!-- ./col -->



      </div>



@if(Config::get('app.locale') == "ar")
        <?php
        if(isset($user_id)) { ?>
        <h3 style="text-align: center; direction: rtl;"> 
             تقرير المستخدم :
        <?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      
        }
        ?>

@else
        <?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      
        }
        ?>

@endif

  
@if(Config::get('app.locale') == "ar")
  <h4 style="text-align: center; display: none;"> اجمالي المبيعات : {{ number_format($all_total - $discount, 2) }} 

<?php /*
    &nbsp;&nbsp; | &nbsp;&nbsp;  اجمالي الكاش     {{$cash}}   &nbsp;&nbsp; | &nbsp;&nbsp;  اجمالي بنكك     {{$bankak}}  */ ?>

  </h4>

@else
  <h4> Total Orders : {{ number_format($all_total - $discount, 2) }} </h4>
@endif
<div class="card">
    <div class="card-body">
        <div class="row">
            <div class="col-md-7"></div>
            <div class="col-md-5">
                <form action="{{route('orders.index')}}">
                    <div class="row">
                        <div class="col-md-5">
                            <input type="date" name="start_date" class="form-control" value="{{request('start_date')}}" />
                        </div>
                        <div class="col-md-5">
                            <input type="date" name="end_date" class="form-control" value="{{request('end_date')}}" />
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-outline-primary" type="submit">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>


     <p> &nbsp; </p>

      <div class="row">

        <div class="col-md-3"  style="text-align: left; font-size: 22px; font-weight: bolder;">
           البحث باسم العميل
        </div>
        <div class="col-md-6">
      <input type="text" style="width: 100%;" class="form-control" id="client_name">

        </div>

        <div class="col-md-3">
           
      <button class="btn btn-primary" style="width: 30%; font-size: 16px; font-weight: bolder;" 
      onclick="search_by_client(document.getElementById('client_name').value)">
        بحث 
      </button>

    </div>

     </div>

     <p> &nbsp; </p>

      <div id="order_values">

        <table class="table">
            <thead>

@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>
                    <th><i class="fa fa-trash"></i></th>
                </tr>
@else
                <tr>
                    <th>ID</th>
                    <th>Created At</th>
                    <th>Customer Name</th>
                    <th>User</th>
                    <th>Total</th>
                    <th>Discount </th>
                    <th>Paid </th>
                    <th>Profit </th>
                    <th>Status</th>
                    <th><i class="fa fa-trash"></i></th>
                </tr>
@endif


            </thead>
            <tbody >
                <?php
                $the_total = 0;
                $all_paid_total = 0;
                $the_discount = 0;
                $total_profit = 0;
                $total_customer_paid = 0;

                ?>
                @foreach ($orders as $order)

<?php 
$bankak_paid = \App\Models\Payment::where('order_id',$order->id)->first()->bankak;
$cash_paid = \App\Models\Payment::where('order_id',$order->id)->first()->cash;
$shake_paid = \App\Models\Payment::where('order_id',$order->id)->first()->shake;
$invoice_amount = \App\Models\Payment::where('order_id',$order->id)->first()->amount;


$all_paid = $bankak_paid + $cash_paid + $shake_paid;
?>


                <tr>

                    <td><a href="#" onclick="window.open('<?php echo url('admin/ticket/'); ?>/<?php echo $order->id; ?>','','width=400,height=500');">{{$order->v_id}}</a></td>

                    <td>{{$order->created_at}}</td>

                    <td>{{$order->getCustomerName()}}</td>
                    <td style="display: none;">{{$order->user->first_name}} {{$order->user->last_name}} </td>
                    <td>{{ config('settings.currency_symbol') }} {{$order->formattedTotal()}}</td>


                    <td>{{ config('settings.currency_symbol') }} {{$order->discount}}</td>

                    <td>{{ config('settings.currency_symbol') }} {{$order->total() - $order->discount}}</td>
                  

                    <td>{{ config('settings.currency_symbol') }} 


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

                      ?>

                      @if($my_customer->id == 4 or $my_customer->paid == 0)

                      <?php
                        $total_customer_paid = $total_customer_paid + $all_paid;
                      ?>

{{ $all_paid }} 

                      @else

                      <?php
                        $total_customer_paid = $total_customer_paid + $my_customer->paid;
                      ?>

{{  $my_customer->paid }} 




                      @endif

                    </td>


                    <td style="">{{ config('settings.currency_symbol') }} 

{{ $order->net_profit  }} 

                    </td>


                    <td>

                      
                      @if($my_customer->id == 4)


                        @if($all_paid == 0)
                            <span class="badge badge-danger">غير مدفوع</span>
                        @elseif($all_paid < $order->total() - $order->discount)
                            <span class="badge badge-warning"> عليه متبقي   </span>
                        @elseif($all_paid == ($order->total() - $order->discount))

                            <span class="badge badge-success">مدفوعة </span>

                        @elseif($all_paid > $order->total() - $order->discount)
                            <span class="badge badge-info">لديه متبقي</span>
                        @endif 



                      @else


                        @if($total_customer_paid == 0)
                            <span class="badge badge-danger">غير مدفوع</span>
                        @elseif($total_customer_paid < $order->total() - $order->discount)
                            <span class="badge badge-warning">  مدفوع جزئي    </span>
                        @elseif($total_customer_paid == ($order->total() - $order->discount))

                            <span class="badge badge-success">مدفوعة </span>

                        @elseif($total_customer_paid > $order->total() - $order->discount)
                            <span class="badge badge-info">لديه متبقي</span>
                        @endif 


                      @endif





                    </td>
                    
					
                    <td>
                        <?php /*
                        <a onclick="return confirm('<?php echo __('هل انت متأكد ؟') ?>');" href="{{url('invoice_edit')}}/{{$order->id}}">
                          <span class="badge badge-warning"><i class="fas fa-edit"></i> تعديل   الفاتورة   </span>
                        </a> */ ?>

                    </td>

                    <td>
                        <?php /*
                        <a onclick="return confirm('<?php echo __('هل انت متأكد ؟') ?>');" href="{{url('refund')}}/{{$order->id}}">
                          <span class="badge badge-danger"><i class="fas fa-close"></i> الغاء الفاتورة </span>
                        </a>
						*/ ?>
                    </td>
                    <td> 


		
      @if(auth()->user()->id  == '4') 
                            <span class="btn btn-danger btn-sm" >
                                <form action="{{url('/admin/orders/')}}/{{ $order->id }}" method="post">
                                    @method('DELETE')
                                    <button onclick="return confirm('Are you sure you want to delete this ?');" style="border: 0px; background:none; color: white">
                                        <i class="fas fa-trash"></i>
                                        Delete</button>
                                    @csrf
                                </form>


                            </span>
                       @endif 

                    </td>
                </tr>

                <?php 
                $all_paid_total = $all_paid_total + $all_paid;
                $the_total = $the_total + $order->total();
                $the_discount = $the_discount + $order->discount;
                $total_profit = $total_profit + $order->net_profit;
                ?>
                @endforeach
            </tbody>
            <tfoot>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th>{{ config('settings.currency_symbol') }} {{ number_format($total, 2) }}</th>
                    <th>{{ config('settings.currency_symbol') }} {{ number_format($the_discount, 2) }}</th>
                    <th>{{ config('settings.currency_symbol') }} {{ number_format($total - $the_discount, 2) }}</th>
                      
                      <?php /*
                    <th>{{ config('settings.currency_symbol') }} {{ number_format($all_paid_total , 2) }}</th> */ ?>

                    <th>{{ config('settings.currency_symbol') }} {{ number_format($total_customer_paid , 2) }}</th>

 <th>{{ config('settings.currency_symbol') }} {{ number_format($total_profit, 2) }}</th>

                    <th></th>
                    <th></th>
                    <th></th>
                </tr>
            </tfoot>
        </table>
        {{ $orders->render() }}


          </div>

    </div>
</div>

<script type="text/javascript">
  
//*************************************************************************

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function search_by_client(client_name) 
{

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

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

    if(ajax3.readyState == 1 || 2 || 3) {
        document.getElementById("order_values").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("order_values").innerHTML =
      this.responseText;

     

    }
  };
  xhttp3.open("GET", "{{url('/search_by_client')}}"+"/"+client_name, true);
  xhttp3.send();

}

</script>
@endsection

