<?php echo $__env->make('admin::layouts.header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php
use App\Models\AdminDetails;
use App\Models\ManagerDetails;
use App\Models\CashierDetails;;
use App\Models\User;
use App\Models\CustomerDetails;
use App\Models\ShiftCashFlow;
use App\Models\Waiters;
use App\Models\Riders;
?>
<main id="main-container">
        <!-- Hero -->
        <div class="bg-body-light">
          <div class="content content-full">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center">
              <h1 class="flex-grow-1 fs-3 fw-semibold my-2 my-sm-3">Sales Reports</h1>
              <nav class="flex-shrink-0 my-2 my-sm-0 ms-sm-3" aria-label="breadcrumb">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item">Dashboard</li>
                  <li class="breadcrumb-item">Report</li>
                  <li class="breadcrumb-item active" aria-current="page">Sales Reports</li>
                </ol>
              </nav>
            </div>
          </div>
        </div>
        <!-- END Hero -->

        <!-- Page Content -->
        <div class="content">
          <!-- Your Block -->
          <div class="block block-rounded">
            <div class="block-header block-header-default">
              <h3 class="block-title">Sales Reports</h3>
            </div>
            <div class="block-content">
            <?php if(Session::has('success')): ?> <div class="alert alert-success mt-2 mb-2"><?php echo e(Session::get('success')); ?></div><?php endif; ?>
            <?php if(Session::has('fail')): ?> <div class="alert alert-danger mt-2 mb-2"><?php echo e(Session::get('fail')); ?></div><?php endif; ?>

            <form method="GET" action="<?php echo e(url('admin/sales-report')); ?>" class=" mb-5">
              <div class="row mt-4 mb-4">
                <?php
                    $defaultStart = \Carbon\Carbon::now()->startOfDay()->format('Y-m-d\TH:i');
                ?>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Start Date</label>
                      <input type="datetime-local" class="form-control" name="start_date" value="<?php echo e(request('start_date', $defaultStart)); ?>" >
                      <?php if($errors->has('start_date')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('start_date')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">End Date</label>
                      <input type="datetime-local" class="form-control" name="end_date" value="<?php echo e(request('end_date', $defaultStart)); ?>" >
                      <?php if($errors->has('end_date')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('end_date')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Cashier</label>
                      <select class="js-select2 form-select" name="cashier" style="width: 100%;" data-placeholder="Select a cashier..">
                          <option value="all">All</option>
                          <?php $__currentLoopData = $cashiers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cashier): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                              <option value="<?php echo e($cashier->user_id); ?>" <?php echo e(request('cashier') == $cashier->user_id ? 'selected' : ''); ?>>
                                  <?php echo e($cashier->name); ?>

                              </option>
                          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                      </select>
                      <?php if($errors->has('cashier')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('cashier')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Invoice Type</label>
                      <select class="js-select2 form-select" name="invoice_type" style="width: 100%;" data-placeholder="Select a invoice type..">
                          <option value="all" <?php echo e(request('invoice_type') == 'all' ? 'selected' : ''); ?>>All</option>
                          <option value="takeaway" <?php echo e(request('invoice_type') == 'takeaway' ? 'selected' : ''); ?>>Takeaway</option>
                          <option value="table" <?php echo e(request('invoice_type') == 'table' ? 'selected' : ''); ?>>Dine In</option>
                          <option value="delivery" <?php echo e(request('invoice_type') == 'delivery' ? 'selected' : ''); ?>>Delivery</option>
                     
                      </select>
                      <?php if($errors->has('invoice_type')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('invoice_type')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Customer</label>
                      <select class="js-select2 form-select" name="customer" style="width: 100%;" data-placeholder="Select a customer..">
                          <option value="all">All</option>
                          <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                              <option value="<?php echo e($customer->id); ?>" <?php echo e(request('customer') == $customer->id ? 'selected' : ''); ?>>
                                  <?php echo e($customer->customerDetails->customer_name); ?>

                              </option>
                          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                      </select>
                      <?php if($errors->has('customer')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('customer')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Waiter</label>
                      <select class="js-select2 form-select" name="waiter" style="width: 100%;" data-placeholder="Select a waiter..">
                          <option value="all">All</option>
                          <?php $__currentLoopData = $waiters; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $waiter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                              <option value="<?php echo e($waiter->id); ?>" <?php echo e(request('waiter') == $waiter->id ? 'selected' : ''); ?>>
                                  <?php echo e($waiter->name); ?>

                              </option>
                          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                      </select>
                      <?php if($errors->has('waiter')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('waiter')); ?></div> <?php endif; ?>
                  </div>
                  <div class="col-md-3 mb-3">
                      <label class="form-label">Rider</label>
                      <select class="js-select2 form-select" name="rider" style="width: 100%;" data-placeholder="Select a rider..">
                          <option value="all">All</option>
                          <?php $__currentLoopData = $riders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $rider): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                              <option value="<?php echo e($rider->id); ?>" <?php echo e(request('rider') == $rider->id ? 'selected' : ''); ?>>
                                  <?php echo e($rider->name); ?>

                              </option>
                          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                      </select>
                      <?php if($errors->has('rider')): ?> <div class="alert alert-danger mt-2"><?php echo e($errors->first('rider')); ?></div> <?php endif; ?>
                  </div>
              </div>
              <button type="submit" class="btn btn-primary">Filter</button>
              <a href="<?php echo e(url('admin/sales-report')); ?>"><button type="button" class="btn btn-success">Clear</button></a>
          </form>
            <table  id="example" class="custom-datatable display  nowrap" style="width:100%">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>Invoice No</th>
                    <th>Invoice Type</th>
                    <th>Cashier</th>
                    <th>Customer</th>
                    <th>Shift ID</th>
                    <th>Token No</th>
                    <th>Waiter/Rider</th>
                    <th>Gross Total</th>
                    <th>Service Charge/Delivery Charge</th>
                    <th>Discount</th>
                    <th>Total</th>
                    <th>Cash</th>
                    <th>Card</th>
                    <th>Online</th>
                    <th>Credit</th>
                    <th>Credit Paid</th>
                    <th>Card Ref</th>
                  </tr>
                </thead>
 <?php
    $total_sub_total = 0;
    $total_total = 0;
    $total_cash_paid = 0;
    $total_card_paid = 0;
    $total_online_paid = 0;
    $total_credit = 0;
    $total_credit_paid = 0;
    $total_discount = 0;
    $total_service_or_delivery = 0;
    $no = 1;
  ?>
<tbody>
<?php foreach($orders as $order) {

  $cashier_role=User::withTrashed()->where('id', $order->sales_person)->value('user_role');
  $cashier_name = '-';
  $customer_name= CustomerDetails::withTrashed()->where('user_id', $order->customer)->value('customer_name');
  $shift_id = ShiftCashFlow::where('order_id', $order->id)->value('shfit_id');
  if($cashier_role == 1){
      $cashier_name=AdminDetails::withTrashed()->where('user_id', $order->sales_person)->value('name');
  }
  if($cashier_role == 3){
      $cashier_name=ManagerDetails::withTrashed()->where('user_id', $order->sales_person)->value('name');
  }
  if($cashier_role == 4){
      $cashier_name=CashierDetails::withTrashed()->where('user_id', $order->sales_person)->value('name');
  }
  
if($order->order_type == 'takeaway'){
    $invoice_type = "Take Away";
}
elseif($order->order_type == 'table'){
    $invoice_type = "Dine In";
}
else{
    $invoice_type = "Delivery";
}
$total_sub_total += $order->sub_total;
$total_total += $order->total;
$total_cash_paid += $order->cash_paid_amount; 
$total_card_paid += $order->card_paid_amount;
$total_online_paid += $order->online_amount;
$total_credit += $order->credit_amount;
$total_credit_paid += $order->paid_credit;
$total_discount += ($order->sub_total / 100) * $order->discount;

if($order->order_type == 'table'){
    $total_service_or_delivery += ($order->sub_total / 100) * ($order->service_charges ?? 0);
} elseif($order->order_type == 'delivery'){
    $total_service_or_delivery += $order->delivery_charges;
}
  
?>
<tr>
    <td class="fw-semibold"><?php echo e($no); ?></td>
    <td class="fw-semibold"><?php echo e($order->created_at->format('Y-m-d')); ?></td>
    <td class="fw-semibold"><?php echo e($order->created_at->format('H:i')); ?></td>
    <td class="fw-semibold"><?php echo e($order->sales_order_number); ?></td>
    <td class="fw-semibold"><?php echo e($invoice_type); ?></td>
    <td class="fw-semibold"><?php echo e($cashier_name); ?></td>
    <td class="fw-semibold"><?php echo e($customer_name); ?></td>
    <td class="fw-semibold">SHFT-000<?php echo e($shift_id); ?></td>
    <td class="fw-semibold"><?php echo e($order->token); ?></td>
    <td class="fw-semibold">
    <?php if($order->order_type == 'table'){ 
    ?> 
    <?php echo e(Waiters::where('id', $order->waiter)->value('name') ?? '-'); ?>

    <?php }elseif($order->order_type == 'delivery'){
    ?> 
    <?php echo e(Riders::where('id', $order->rider)->value('name') ?? '-'); ?>

    <?php }else{?> 
    -
    <?php } ?>
    </td>
    <td class="fw-semibold  text-end"> <?php echo e(number_format($order->sub_total, 2)); ?></td>
    <td class="fw-semibold  text-end">
    <?php if($order->order_type == 'table'){ 
    ?> 
    <?php echo e(number_format(($order->sub_total / 100) * ($order->service_charges ?? 0), 2)); ?>

    <?php }elseif($order->order_type == 'delivery'){
    ?> 
    <?php echo e(number_format($order->delivery_charges, 2)); ?>

    <?php }else{?> 
    0
    <?php } ?>
    </td>
    <td class="fw-semibold text-end"> <?php echo e(number_format(($order->sub_total/100)*$order->discount, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->total, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->cash_paid_amount, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->card_paid_amount, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->online_amount, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->credit_amount, 2)); ?></td>
    <td class="fw-semibold text-end"> <?php echo e(number_format($order->paid_credit, 2)); ?></td>
    <td class="fw-semibold text-end"><?php echo e($order->card_ref_no); ?></td>
  </tr>
<?php 
$no =$no+1;
} ?>
</tbody>


<tfoot>
        <tr>
            <th colspan="10" class="text-end">Total</th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_sub_total, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_service_or_delivery, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_discount, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_total, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_cash_paid, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_card_paid, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_online_paid, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_credit, 2)); ?></th>
            <th class="fw-semibold  text-end">LKR <?php echo e(number_format($total_credit_paid, 2)); ?></th>
            <th class="fw-semibold  text-end">-</th>
        </tr>
    </tfoot>
              </table>
            </div>
          </div>
          <!-- END Your Block -->
        </div>
        <!-- END Page Content -->
</main>
<!-- END Main Container -->
<?php echo $__env->make('admin::layouts.footer', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php /**PATH /home/zone1futurepos/indianheavenrestaurant.zone1.futurepos.app/Modules/Admin/resources/views/report/sales_reports.blade.php ENDPATH**/ ?>