<?php $__env->startSection('section-title', 'Report'); ?>
<?php $__env->startSection('page-title', 'Profit Loss Report'); ?>

<?php $__env->startPush('css'); ?>
    <style>
        @media print {
            table,
            table th,
            table td {
                color: black !important;
            }
            #h-hide {
                display: none;
            }
        }
    </style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="card card-body card_style mb-2" style="margin-top: -5px;" id="h-hide">
                <form action="<?php echo e(route('report.profit-loss')); ?>">
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <input type="month" name="start_month" placeholder="Enter Start Month" class="form-control"
                                value="<?php echo e(isset($sdate) ? date('Y-m', strtotime($sdate)) : ''); ?>">
                        </div>
                        <div class="form-group col-md-6">
                            <input type="month" name="end_month" placeholder="Enter End Month" class="form-control"
                                value="<?php echo e(isset($edate) ? date('Y-m', strtotime($edate)) : ''); ?>">
                        </div>
                    </div>
                    <div class="form-row mt-2">
                        <div class="form-group col-12">
                            <button class="btn add_list_btn" type="submit">
                                <i class="fa fa-sliders"></i> Filter
                            </button>
                            <a href="<?php echo e(route('report.profit-loss')); ?>" class="btn add_list_btn_reset">Reset</a>
                            <a href="" class="btn add_list_btn float-right" onclick="window.print()">Print</a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="card m-b-30 card_style print_area">
                <div class="card-header">
                    <h4 style="text-align: center; font-weight:bold; margin-top:25px;font-size:30px">Profit Loss
                        (<?php echo e(isset($sdate) ? date('F Y', strtotime($sdate)) : ''); ?> -
                        <?php echo e(isset($edate) ? date('F Y', strtotime($edate)) : ''); ?>)</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <?php if(isset($groupedDates)): ?>
                            <table class="table">
                                <thead>
                                    <tr class="header_bg">
                                        <th class="header_style_left">Month</th>
                                        <th>Sales</th>
                                        <th>Cost of Goods Sold</th>
                                        <th>Gross Profit </th>
                                        <th>Expenses</th>
                                        <th class="header_style_right">Net Profit</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__empty_1 = true; $__currentLoopData = $groupedDates; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                        <?php
                                            $sale_amount = App\Models\Invoice::whereMonth(
                                                'date',
                                                '=',
                                                date('m', strtotime($key)),
                                            )
                                                ->whereYear('date', '=', date('Y', strtotime($key)))
                                                ->sum('total_amount');
                                            $invoiceItem = App\Models\InvoiceItem::with('invoice')
                                                ->whereHas('invoice', function ($query) use ($key) {
                                                    $query
                                                        ->whereMonth('date', '=', date('m', strtotime($key)))
                                                        ->whereYear('date', '=', date('Y', strtotime($key)));
                                                })
                                                ->get();
                                            $p_price = 0;
                                            $profit = 0;
                                            foreach ($invoiceItem as $invoice) {
                                                $product = App\Models\Product::with('unit.related_unit')
                                                    ->where('id', $invoice->product_id)
                                                    ->first();
                                                if ($product->unit->related_unit == null) {
                                                    $inv_stock = $invoice->main_qty;
                                                    $purchase_price = $inv_stock * $product->purchase_price;
                                                    $selling_price = $inv_stock * $product->selling_price;
                                                    $profits = $selling_price - $purchase_price;
                                                } else {
                                                    //invoice
                                                    $inv_stock_main = $invoice->main_qty;
                                                    $inv_total_main =
                                                        (float) ($inv_stock_main * $product->unit->related_value);
                                                    $inv_total_sub = $invoice->sub_qty;
                                                    $inv_total_stock = (float) ($inv_total_main + $inv_total_sub);
                                                    $total_stock = $inv_total_stock;
                                                    $p_value = $product->purchase_price / $product->unit->related_value;
                                                    $s_value = $product->selling_price / $product->unit->related_value;
                                                    $purchase_price = $total_stock * $p_value;
                                                    $selling_price = $total_stock * $s_value;
                                                    $profits = $selling_price - $purchase_price;
                                                }
                                                $p_price += $purchase_price;
                                                $profit += $profits;
                                            }
                                            $expense = App\Models\Expense::whereMonth(
                                                'date',
                                                '=',
                                                date('m', strtotime($key)),
                                            )
                                                ->whereYear('date', '=', date('Y', strtotime($key)))
                                                ->sum('amount');
                                            $net_profit = (float) ($profit - $expense);
                                        ?>
                                        <tr>
                                            <td class="table_data_style_left"><?php echo e(date('F Y', strtotime($key))); ?></td>
                                            <td><?php echo e(number_format($sale_amount, 2)); ?> TK</td>
                                            <td><?php echo e(number_format($p_price, 2)); ?> TK</td>
                                            <td><?php echo e(number_format($profit, 2)); ?> TK</td>
                                            <td><?php echo e(number_format($expense, 2)); ?> TK</td>
                                            <td class="table_data_style_right"><?php echo e($net_profit >= 0 ? number_format($net_profit, 2) : '(' . number_format(abs($net_profit), 2) . ')'); ?>

                                                TK</td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                        <tr>
                                            <td colspan="10" class="text-center text-danger no_data_style">No Data Found</td>
                                        </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        <?php else: ?>
                            <div class="col-md-12" style="padding-bottom: 30px;">
                                <div class="alert alert-danger text-center" role="alert"> Please Select Start and End
                                    Month</div>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('backend.layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\laragon\www\new demo multi-bank\resources\views/backend/pages/report/profit_loss.blade.php ENDPATH**/ ?>