<?php
$company = \App\Models\Company::FindOrFail($supplier_k->company_id);
$extra_settings = \App\Models\ExtraSettings::where('company_id', $company->id)->first();
$currency = $extra_settings->currency;
?>

    <!DOCTYPE html>
<html>
<head>
    <title>
        كشف حساب مورد
    </title>
    <meta charset="utf-8"/>
    <link href="{{asset('app-assets/css/bootstrap.min.css')}}" rel="stylesheet"/>
    <style type="text/css" media="screen">
        @font-face {
            font-family: 'Cairo';
            src: url({{asset('fonts/Cairo.ttf')}});
        }

        body, html {
            font-family: 'Cairo' !important;
            direction: rtl !important;
            text-align: center !important;
            font-size: 13px;
        }

        h1, h2, h3, h4, h5, h6 {
            font-family: 'Cairo' !important;
        }

        .table-container {
            width: 80%;
            margin: 10px auto;
        }

        .no-print {
            bottom: 0;
            right: 30px;
            border-radius: 0;
            z-index: 9999;
        }

        table tr th, table tr td {
            text-align: center !important;
        }
    </style>
    <style type="text/css" media="print">
            @font-face {
            font-family: 'Cairo';
            src: url({{asset('fonts/Cairo.ttf')}});
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Cairo' !important;
        }
        
        body, html {
            font-family: 'Cairo' !important;
            direction: rtl !important;
            text-align: center !important;
            font-size: 13px;
            -webkit-print-color-adjust: exact !important;
            -moz-print-color-adjust: exact !important;
            print-color-adjust: exact !important;
            -o-print-color-adjust: exact !important;
        }

        table tr th, table tr td {
            text-align: center !important;
        }

        .no-print {
            display: none;
        }
    </style>
</head>
<body style="background: #fff">
<table class="table table-bordered table-container">
    <tbody>
    <tr>
        <td class="thisTD">
            <h3 class="alert alert-sm alert-light text-center" style="margin:20px auto;">
                كشف حساب مورد 
                <br>
                <br>
                 @if(empty($to_date) || empty($from_date))
                 كامل
                 @else
                  من تاريخ {{$from_date}} إلي تاريخ {{$to_date}}
                 @endif
            </h3>
            @if(isset($supplier_k) && !empty($supplier_k))
                <p class="alert alert-sm alert-danger text-center">
                    عرض بيانات المورد
                </p>
                <div class="table-responsive">
                    <table class="table table-condensed table-striped table-bordered text-center table-hover">
                        <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>
                            <th class="text-center">الرقم الضريبى</th>
                            <th class="text-center">رصيد البداية</th>
                            <th class="text-center"> مديونية</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>{{ $supplier_k->id }}</td>
                            <td>{{ $supplier_k->supplier_name }}</td>
                            <td>{{ $supplier_k->supplier_category }}</td>
                            <td>{{ $supplier_k->addresses[0]->supplier_address }}</td>
                            <td>{{ $supplier_k->shop_name }}</td>
                            <td>{{ $supplier_k->supplier_email }}</td>
                            <td>{{ $supplier_k->phones[0]->supplier_phone }}</td>
                            <td>{{ $supplier_k->tax_number }}</td>
                            <td>
                                @if($supplier_k->balance_start > 0 )
                                له
                                {{number_format($supplier_k->balance_start)}}
    
                                 @elseif($supplier_k->balance_start < 0)
                                علية
                                {{number_format(abs($supplier_k->balance_start))}}
                                 @else
                                 0
                                @endif                        
                            </td>
                            <td>
                            @if($supplier_k->balance > 0 )
                            له
                            {{number_format($supplier_k->balance)}}

                             @elseif($supplier_k->balance < 0)
                            علية
                            {{number_format(abs($supplier_k->balance))}}
                             @else
                             0
                            @endif                        
                        </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            @endif
            <div class="clearfix"></div>
            @if(isset($buyBills) && !$buyBills->isEmpty())
                <p class="alert alert-sm alert-primary mt-3 text-center">
                    فواتير المشتريات لهذا المورد
                </p>
                <div class="table-responsive">
                    <table class='table table-condensed table-striped table-bordered'>
                        <thead class="text-center">
                        <th>#</th>
                        <th>رقم الفاتورة</th>
                        <th>تاريخ الفاتورة</th>
                        <th> وقت الفاتورة</th>
                        <th>الاجمالى النهائى</th>
                        <th>المدفوع</th>
                        <th>عدد العناصر</th>
                        </thead>
                        <tbody>
                        <?php $i = 0; $total = 0; ?>
                        @foreach($buyBills as $buy_bill)
                            <tr>
                                <td>{{++$i}}</td>
                                <td><a href="{{route('client.buy_bills.print',$buy_bill->buy_bill_number)}}">{{$buy_bill->buy_bill_number}}</a></td>
                                <td>{{$buy_bill->date}}</td>
                                <td>{{$buy_bill->time}}</td>
                                <td>
                                    {{number_format($buy_bill->final_total)}} {{$currency}}
                                </td>
                                <td>
                                    {{number_format($buy_bill->paid)}} {{$currency}}
                                </td>
                                <td>{{$buy_bill->elements->count()}}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>

            @endif
            <div class="clearfix"></div>
            @if(isset($returns) && !$returns->isEmpty())
                <p class="alert alert-sm alert-dark text-center">
                    مرتجعات المورد
                </p>
                <div class="table-responsive">
                    <table class="table table-condensed table-striped table-bordered text-center table-hover">
                        <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>
                            <th class="text-center"> مستحقات المورد قبل الارتجاع</th>
                            <th class="text-center"> مستحقات المورد بعد الارتجاع</th>
                            <th class="text-center"> رصيد المنتج قبل الارتجاع</th>
                            <th class="text-center"> رصيد المنتج بعد الارتجاع</th>
                        </tr>
                        </thead>
                        <tbody>
                        @php
                            $i=0;
                        @endphp
                        @foreach ($returns as $key => $return)
                            <tr>
                                <td>{{ $return->bill_id }}</td>
                                <td>{{ $return->product->product_name}}</td>
                                <td>{{number_format( $return->return_quantity  )}}</td>
                                <td>{{ $return->date}}</td>
                                <td>{{ $return->time}}</td>
                                <td>{{number_format( $return->product_price  )}}</td>
                                <td>{{number_format( $return->quantity_price  )}}</td>

                                <td>{{number_format( $return->balance_before  )}}</td>
                                <td>{{number_format( $return->balance_after  )}}</td>

                                <td>{{number_format( $return->before_return  )}}</td>
                                <td>{{number_format( $return->after_return  )}}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            <div class="clearfix"></div>
            @if(isset($buyBorrows) && !$buyBorrows->isEmpty())
                <p class="alert alert-sm alert-warning mt-3 text-center">
                    سلفيات من المورد
                </p>
                <div class="table-responsive">
                    <table class="table table-condensed table-striped table-bordered text-center table-hover">
                        <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>
                            <th class="text-center">خزنة الدفع</th>
                        </tr>
                        </thead>
                        <tbody>
                        @php
                            $i=0;
                        @endphp
                        @foreach ($buyBorrows as $key => $cash)
                            <tr>
                                <td>{{ $cash->cash_number }}</td>
                                <td>{{number_format( abs($cash->amount)  )}}</td>
                                <td>{{number_format( $cash->balance_before  )}}</td>
                                <td>{{number_format( $cash->balance_after  )}}</td>
                                <td> سلفة نقدية </td>
                                <td>{{ $cash->date }}</td>
                                <td>{{ $cash->time }}</td>
                                <td>{{ $cash->safe->safe_name }}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            @if(isset($buyCashs) && !$buyCashs->isEmpty())
                <p class="alert alert-sm alert-warning mt-3 text-center">
                    مدفوعات نقدية لهذا المورد
                </p>
                <div class="table-responsive">
                    <table class="table table-condensed table-striped table-bordered text-center table-hover">
                        <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>
                            <th class="text-center">خزنة الدفع</th>
                        </tr>
                        </thead>
                        <tbody>
                        @php
                            $i=0;
                        @endphp
                        @foreach ($buyCashs as $key => $cash)
                            <tr>
                                <td>{{ $cash->cash_number }}</td>
                                <td>{{number_format( $cash->amount  )}}</td>
                                <td>{{number_format( $cash->balance_before  )}}</td>
                                <td>{{number_format( $cash->balance_after  )}}</td>
                                <td>@if(!empty($cash->bill_id))  فاتورة # {{$cash->bill_id}} @else تسديد نقدي @endif</td>
                                <td>{{ $cash->date }}</td>
                                <td>{{ $cash->time }}</td>
                                <td>{{ $cash->safe->safe_name }}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            @if(isset($bankbuyCashs) && !$bankbuyCashs->isEmpty())
                <p class="alert alert-sm alert-warning mt-3 text-center">
                    مدفوعات بنكية لهذا المورد
                </p>
                <div class="table-responsive">
                    <table class="table table-condensed table-striped table-bordered text-center table-hover">
                        <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>
                            <th class="text-center">البنك</th>
                            <th class="text-center">رقم المعاملة</th>
                            <th class="text-center">ملاحظات</th>
                        </tr>
                        </thead>
                        <tbody>
                        @php
                            $i=0;
                        @endphp
                        @foreach ($bankbuyCashs as $key => $cash)
                            <tr>
                                <td>{{ $cash->cash_number }}</td>
                                <td>{{number_format( $cash->amount  )}}</td>
                                <td>{{number_format( $cash->balance_before  )}}</td>
                                <td>{{number_format( $cash->balance_after  )}}</td>
                                <td>@if(!empty($cash->bill_id))  فاتورة # {{$cash->bill_id}}  @else تسديد بنكي @endif</td>
                                <td>{{ $cash->date }}</td>
                                <td>{{ $cash->time }}</td>
                                <td>{{ $cash->bank->bank_name }}</td>
                                <td>{{ $cash->bank_check_number }}</td>
                                <td>{{ $cash->notes }}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
            @if(isset($supplier_k) && !empty($supplier_k))
                <div class="col-lg-12 text-center mt-3 mb-3">
                            <span class="alert alert-secondary text-center ">
                                مستحقات المورد الحالية

                                @if($supplier_k->balance > 0 )
                                له
                                {{number_format($supplier_k->balance)}}
    
                                 @elseif($supplier_k->balance < 0)
                                علية
                                {{number_format(abs($supplier_k->balance))}}
                                 @else
                                 0
                                @endif    
                                
                                {{$currency}}
                            </span>
                </div>
            @endif
            @auth('client-web')
            <div class="row mt-1 mb-1 text-center no-print">
                <div class="col-lg-12 text-center">
                    <button onclick="window.print()" type="button" class="btn btn-md btn-info">
                        <i class="fa fa-print"></i>
                        طباعة تقرير كشف الحساب
                    </button>
                    @if(isset($_GET['ref']) && $_GET['ref'] == "email")

                    @else
                        @if(isset($supplier_k) && !empty($supplier_k))
                            @if(!empty($supplier_k->supplier_email))
                                <form class="d-inline" action="{{route('supplier.summary.send')}}" method="post">
                                    @csrf
                                    @method('POST')
                                    <input type="hidden" value="{{url()->full().'&ref=email'}}" name="url"/>
                                    <input type="hidden" value="{{$supplier_k->id}}" name="id"/>
                                    <button type="submit" class="btn btn-md btn-warning">
                                        <i class="fa fa-envelope-o"></i>
                                        ارسال كشف الحساب الى بريد المورد
                                    </button>
                                </form>
                            @else
                                <span class="alert alert-sm alert-warning text-center">
                                    خانه البريد الالكترونى للمورد فارغة
                                </span>
                            @endif
                            @if(!$supplier_k->phones->isEmpty())
                                <?php
                                $url = url()->full() . '&ref=email';
                                $text = "مرفق رابط لكشف حسابك " . "%0a" . $url;
                                $text = str_replace("&", "%26", $text);
                                $phone_number = $supplier_k->phones[0]->supplier_phone;
                                ?>
                                <a class="btn btn-success btn-md" target="_blank"
                                   href="https://wa.me/{{$phone_number}}?text={{$text}}">
                                    ارسال الى واتساب المورد
                                </a>
                            @else
                                <span class="alert alert-sm alert-warning text-center">
                                    خانه رقم الهاتف للمورد فارغة
                                </span>
                            @endif
                        @endif
                    @endif
                </div>
            </div>
            @endauth
        </td>
    </tr>
    </tbody>
</table>
<script src="{{asset('app-assets/js/jquery.min.js')}}"></script>
<script type="text/javascript">

</script>
</body>
</html>
