@extends('client.layouts.app')
@section('title','جرد المخازن')
@section('content')
 <!-- BEGIN: Content-->
 <div class="app-content content ">
    <div class="content-overlay"></div>
    <div class="header-navbar-shadow"></div>
    <div class="content-wrapper container-xxl p-0">
      <div class="content-header row">
        <div class="content-header-left col-md-9 col-12 mb-2">
          <div class="row breadcrumbs-top">
            <div class="col-12">
              <h2 class="content-header-title float-start mb-0">المخازن</h2>
              <div class="breadcrumb-wrapper">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="index.html">لوحة التحكم</a>
                  </li>
                  <li class="breadcrumb-item"><a href="{{route('client.home')}}">المخازن</a>
                  </li>
                  <li class="breadcrumb-item active">جرد
                  </li>
                </ol>
              </div>
            </div>
          </div>
        </div>
        <div class="content-header-right text-md-end col-md-3 col-12 d-md-block d-none">
          <div class="mb-1 breadcrumb-right">
            <div class="dropdown">
              @can('عرض مخازن الشركة')
                <a  href="{{ route('client.stores.index') }}">
                    <button class="btn-icon btn btn-primary" type="button"><i data-feather='eye'> </i></button>
                </a>
              @endcan
            </div>
          </div>
        </div>
      </div>
      <div class="content-body">
        <!-- Basic Horizontal form layout section start -->
        <!-- Basic multiple Column Form section start -->
<section id="multiple-column-form">
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">جرد مخازن الشركة</h4>
      </div>
      <div class="card-body">
        <form action="{{route('client.inventory.post')}}" class="no-print" method="POST">
            @csrf
            <div class="row">
                <div class="col-md-3 col-sm-12 col-xs-12">
              <div class="mb-1">

                    <select class="form-control" name="store_id" id="store_id" 
                                    data-style="btn-info" data-live-search="true" >
                                <option
                                    @if(isset($store_id) && $store_id == "all")
                                    selected
                                    @endif
                                    value="all">كل المخازن
                                </option>
                                @foreach($stores as $store)
                                    <option
                                        @if(isset($store_id) && $store->id == $store_id)
                                        selected
                                        @endif
                                        value="{{$store->id}}">{{$store->store_name}}</option>
                                @endforeach
                            </select>
              </div>
                </div>
                <div class="col-md-3 col-sm-12 col-xs-12">
              <div class="mb-1">
                    <input class="form-control" type="date" name="from_date"  @if(isset($from_date) && !empty($from_date)) value="{{$from_date}}"@endif>
              </div>
                </div>
                <div class="col-md-3 col-sm-12 col-xs-12">
              <div class="mb-1">
                    <input class="form-control" type="date" name="to_date" @if(isset($to_date) && !empty($to_date)) value="{{$to_date}}"@endif >
              </div>
                </div>
                <div class="col-md-3 col-sm-12 col-xs-12">
              <div class="mb-1">
                    <button type="submit" class="btn btn-success me-1">عرض التقرير</button>
              </div>
                </div>
            </div>
          
          
        </form>
    <div class="clearfix"></div>
    @if(isset($products_k) && !empty($products_k))
    <div class="divider divider-primary">
        <div class="divider-text">تقرير حركة مخزن</div>
      </div>
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th class="text-center">اسم المنتج</th>
                    <th class="text-center"> الكود</th>
                    <th class="text-center"> سعر الشراء</th>
                    <th class="text-center"> مشتريات</th>
                    <th class="text-center"> مبيعات</th>
                    <th class="text-center"> كمية حالية</th>
                    <th class="text-center">  رصيد البداية </th>

                </tr>
                </thead>
                <tbody>
                <?php $sum = 0 ;?>
                @foreach($products_k as $product_k)
                    <tr>
                        <td>{{ $product_k->product_name }}</td>
                        <td>{{ $product_k->main_unit->barcode }}</td>
                        <td>{{number_format( $product_k->purchasing_price  )}}</td>
                        <td>
                            <?php
                             $buy_elements = \App\Models\BuyBillElement::where('company_id',$company->id)->with('BuyBill')->where('product_id', $product_k->id)->get();
                            $total_buy_elements = 0;
                            foreach ($buy_elements as $buy_element) {
                              if($buy_element->BuyBill->status == 'done'){
                                $total_buy_elements += $buy_element->quantity* $buy_element->elements_unit->quantity;
                              }
                            }
                            echo number_format($total_buy_elements)." "; echo $product_k->unit->unit_name;
                            ?>
                        </td>
                        <td>
                            <?php
                            $sale_elements = \App\Models\SaleBillElement::where('company_id',$company->id)->with('SaleBill')->where('product_id', $product_k->id)->get();
                            $total_sale_elements = 0;
                            foreach ($sale_elements as $sale_element) {
                              if($sale_element->SaleBill->status == 'done'){
                                $total_sale_elements +=  $sale_element->quantity * $sale_element->elements_unit->quantity;
                              }
                            }
                            echo number_format($total_sale_elements)." "; echo $product_k->unit->unit_name;
                            ?>
                        </td>
                        <td>{{number_format( $product_k->balance  )}} {{$product_k->unit->unit_name}}</td>
                    <td>{{ $product_k->balance_start }} {{$product_k->unit->unit_name}}</td>

                    </tr>
                    <?php $total = $product_k->balance*$product_k->purchasing_price;
                    $sum = $sum + $total;
                    ?>
                @endforeach
                </tbody>
            </table>
        </div>

    @endif

      </div>
    </div>
    @if(isset($products_k) && !empty($products_k))
    <center>
      <div class="col-lg-4 col-md-12"  id="additionalData">
        <div class="card" >
          <div class="card-header"  >
            <h4 class="card-title"> ملخص التقرير </h4>
          </div>
          <div class="card-body">
            <p class="card-text">
             
            </p>
            <ul class="list-group">
              <li class="list-group-item list-group-item-warning">
                إجمالي تكلفة المنتجات المتوفرة  : 
                <span class="badge bg-warning rounded-pill" id=""> {{number_format($sum) }} </span>
                {{$company->currency}}
              </li>
            </ul>
          </div>
        </div>
      </div>
      </center>
      @endif
  </div>
</div>
</section>
<!-- Basic Floating Label Form section end -->

      </div>
    </div>
  </div>
@endsection

