@extends('backend.layouts.master')

@section('title', 'Daily Ledger Report')

@push('css')
<!-- bootstrap-datetimepicker -->
<!--<link rel="stylesheet" href="{{ asset('myfile/backend/assets/datepicker/zebra_datepicker.min.css') }}" type="text/css">
	<link rel="stylesheet" href="{{ asset('myfile/backend/assets/datepicker/examples.css') }}" type="text/css">-->
<style>
    #search-btn {
        margin-top: 24px;
    }
    thead,tbody {
        font-size: 12px;
    }
    .text-center,h3,h4,p {
        line-height: 15px;
        font-weight: bold;
    }
    .border p {
        background: teal;
        padding: 4px;
        text-align: center;
        color: white;
        font-weight: bold;
    }
    .bg-success2 {
        background-color: #b9d8ac;
        font-size: 15px;
    }
    .bg-info2 {
        background-color: #add9ef;
        font-size: 15px;
    }
   .bg-secondary {
        background: #5a6365;
        color: white;
        font-size: 18px;
    }
</style>
@endpush
@section('content')
<div class="right_col" role="main">
    <div class="">
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2 class="widget-title">Daily Ledger</h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li>
                                <a class="collapse-link" onclick="printDiv('printableArea')">
                                    <i class="fa fa-1x fa-print"></i>
                                </a>
                            </li>
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                    aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a href="#">Settings 1</a></li>
                                    <li><a href="#">Settings 2</a></li>
                                </ul>
                            </li>
                            <li><a class="close-link"><i class="fa fa-close"></i></a></li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 col-sm-12 col-xs-12">
                            <div class="x_content" id="printableArea">
                                <div class="row">
                                    <div class="col-xs-12 col-sm-12">
                                        <div class="row">
                                            <div class="col-xs-1"></div>
                                            <div class="col-xs-1">
                                                <img src="{{ asset('storage/about/'.about()->logo)}}" class="img-responsive img-thumbnail" alt="logo" width="85px" height="75px">
                                            </div>
                                            <div class="col-xs-8">
                                                <div class="text-center">
                                                    <h3>Daily Ledger</h3>
                                                    <h4>{{about()->english_name}}</h4>
                                                    <p>Phone: {{about()->phone}}, Email: {{about()->email}}</p>
                                                    <p>Address: {{about()->address }} </p>
                                                    <p>Date: {{date('d-m-Y') }}</p>
                                                </div>
                                            </div>
                                            <div class="col-xs-2"></div>
                                        </div>
                                        <hr>
                                        <div class="border">
                                            <p>Sale Details</p>
                                        </div>
                                        <table class="table table-bordered">
                                            <thead>
                                                <tr>
                                                    <th>SL</th>
                                                    <th>Customer</th>
                                                    <th>Total Sale</th>
                                                    <th>Payment</th>
                                                    <th>Due</th>
                                                    <th>Remark</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($sales as $l=>$sale)
                                                <tr>
                                                    <td>{{$l+1}}</td>
                                                    <td>{{$sale->customer->name}}</td>
                                                    <td>{{$sale->grand_total_price}}</td>
                                                    <td>{{$sale->payment}}</td>
                                                    <td>{{$sale->due_amount}}</td>
													<td></td>
                                                </tr>
                                                @endforeach
                                            </tbody>
											<tfoot>
												<tr>
													<th colspan="2">Total:</th>
													<th>{{$sales->sum('grand_total_price')}}</th>
													<th>{{$sales->sum('payment')}}</th>
													<th>{{$sales->sum('due_amount')}}</th>
													<th></th>
												</tr>
											</tfoot>
                                        </table>
                                        <div class="row">
                                            <div class="col-xs-12 col-sm-6 col-md-6">
                                                <div class="border">
                                                    <p>Bank Transactions</p>
                                                </div>
                                                <table class="table table-bordered table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>SL</th>
                                                            <th>Bank</th>
                                                            <th>Deposit</th>
                                                            <th>Loan</th>
                                                            <th>Withdraw</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
														@php
															$dp = 0;
															$ln = 0;
															$wit = 0;
                                                        @endphp
                                                        @foreach($transactions as $w=>$wt)
                                                        @php
															$dp +=  $wt->transaction_status == 'Deposit' ? $wt->transaction_amount : 0;
															$ln += $wt->transaction_status == 'Loan' ? $wt->transaction_amount : 0;
															$wit += $wt->transaction_status == 'Withdraw' ? $wt->transaction_amount : 0;
                                                        @endphp
                                                        <tr>
                                                            <td>{{$w+1}}</td>
                                                            <td>{{$wt->bank->bank_name}}</td>
                                                            <td>{{$wt->transaction_status == 'Deposit' ? $wt->transaction_amount : 0}}
                                                            </td>
                                                            <td>{{$wt->transaction_status == 'Loan' ? $wt->transaction_amount : 0}}
                                                            </td>
                                                            <td>{{$wt->transaction_status == 'Withdraw' ? $wt->transaction_amount : 0}}
                                                            </td>
                                                        </tr>
                                                        @endforeach
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th colspan="2">Total</th>
                                                            <th>{{$dp}}</th>
                                                            <th>{{$ln}}</th>
                                                            <th>{{$wit}}</th>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-md-6">
                                                <div class="border">
                                                    <p>Expense Details</p>
                                                </div>
                                                <table class="table table-bordered table-condensed">
                                                    <thead>
                                                        <tr>
                                                            <th>SL</th>
                                                            <th>Type</th>
                                                            <th>Amount</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        @foreach($expenses as $e=>$ex)
                                                        <tr>
                                                            <td>{{$e+1}}</td>
                                                            <td>{{$ex->expensetype->type_name}}</td>
                                                            <td>{{$ex->expense_amount}}</td>
                                                        </tr>
                                                        @endforeach
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th colspan="2">Total</th>
                                                            <th>{{$expenses->sum('expense_amount')}}</th>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-xs-12 col-sm-6 col-md-6">
                                                <div class="border">
                                                    <p>Total Calculation</p>
                                                </div>
                                                <table class="table table-bordered table-condensed">
                                                    <tbody>
                                                        <tr>
                                                            <th class='bg-success'>Credit</th>
                                                            <th class='bg-success'>Amount</th>
                                                            <th class='bg-info'>Debit</th>
                                                            <th class='bg-info'>Amount</th>
                                                        </tr>
                                                        <tr>
                                                            <th class='bg-success'>Total Sale:</th>
                                                            <th class='bg-success'>{{$sales->sum('grand_total_price')}}</th>
                                                            <th class='bg-info'>Total Due:</th>
                                                            <th class='bg-info'>{{$sales->sum('due_amount')}}</th>
                                                        </tr>
                                                        <tr>
                                                            <th class='bg-success'>Last Day Cash:</th>
                                                            <th class='bg-success'>{{ $lastday_cash->balance}}</th>
                                                            <th class='bg-info'>Total Expense:</th>
                                                            <th class='bg-info'>{{ $expenses->sum('expense_amount')}}</th>
                                                        </tr>
                                                        <tr>
                                                            <th class='bg-success'>Previous Due Payment:</th>
                                                            <th class='bg-success'>{{ $cpayment - $sales->sum('payment') }}</th>
                                                            <th class='bg-info'>Supplier Payments:</th>
                                                            <th class='bg-info'>{{ $spayment}}</th>
                                                        </tr>
                                                        <tr>
                                                            <th class='bg-success'>Total Bank Withdraw + Loan:</th>
                                                            <th class='bg-success'>{{ $wit + $ln}}</th>
                                                            <th class='bg-info'>Total Bank Deposit:</th>
                                                            <th class='bg-info'>{{ $dp}}</th>
                                                        </tr>
                                                        @php
                                                            $pdp = $cpayment - $sales->sum('payment');
                                                            $debit = $expenses->sum('expense_amount') + $dp + $spayment ;
                                                            $credit = $sales->sum('payment') + $lastday_cash->balance + $pdp + $wit + $ln ;
                                                        @endphp
                                                        <tr>
                                                            <th class='bg-success2'>Total Credit</td>
                                                            <th class='bg-success2'>{{$credit}}</td>
                                                            <th class='bg-info2'>Total Debit</td>
                                                            <th class='bg-info2'>{{$debit}}</td>
                                                        </tr>
                                                        <tr class='bg-secondary'>
                                                            <th colspan='2'>Today cash in hand:</th>
                                                            <th colspan='2'>{{$credit - $debit}}</th>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                            <div class="col-xs-12 col-sm-4 col-md-4"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('js')

<!-- Jquery Plugin For Date Calender-->
<!--<script type="text/javascript" src="{{ asset('myfile/backend/assets/datepicker/zebra_datepicker.src.js') }}"></script>
<script type="text/javascript" src="{{ asset('myfile/backend/assets/datepicker/examples.js') }}"></script>-->
<script>
$(document).ready(function() {
    // select2 section
    // $('.select2').select2()	;
});

function printDiv(divName) {
    var printContents = document.getElementById(divName).innerHTML;
    var originalContents = document.body.innerHTML;

    document.body.innerHTML = printContents;

    window.print();

    document.body.innerHTML = originalContents;
}
</script>
@endpush