<?php $__env->startSection('page-title', 'Dashboard'); ?>
<?php $__env->startPush('css'); ?>
    <style>
        .order-card {
            color: #fff;
        }

        .bg-c-blue {
            background: linear-gradient(45deg, #4099ff, #73b4ff);
        }

        .bg-c-green {
            background: linear-gradient(45deg, #2ed8b6, #59e0c5);
        }

        .bg-c-yellow {
            background: linear-gradient(45deg, #FFB64D, #ffcb80);
        }

        .bg-c-pink {
            background: linear-gradient(45deg, #FF5370, #ff869a);
        }


        .card {
            border-radius: 5px;
            -webkit-box-shadow: 0 1px 2.94px 0.06px rgba(4, 26, 55, 0.16);
            box-shadow: 0 1px 2.94px 0.06px rgba(4, 26, 55, 0.16);
            border: none;
            margin-bottom: 30px;
            -webkit-transition: all 0.3s ease-in-out;
            transition: all 0.3s ease-in-out;
        }

        .card .card-block {
            padding: 25px;
        }

        .order-card i {
            font-size: 26px;
        }

        .f-left {
            float: left;
        }

        .f-right {
            float: right;
        }
    </style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
    <?php

        $total_invoice = App\Models\Invoice::sum('total_amount');
        $total_return = App\Models\ReturnTbl::sum('total_return');
        $total_purchase = App\Models\Purchase::sum('total_amount');
        $return_purchase = App\Models\ReturnPurchase::sum('total_return');

    ?>
    <div class="mt-5">
        <form action="<?php echo e(route('dashboard')); ?>" method="GET" id="filterForm">
            <div class="row">
                <div class="col-md-9"></div>
                <div class="col-md-3">
                    <select name="filter" class="form-control" id="filterSelect">
                        <option value="today" <?php echo e($filter == 'Today' ? 'selected' : ''); ?>>Today</option>
                        <option value="yesterday" <?php echo e($filter == 'Yesterday' ? 'selected' : ''); ?>>Yesterday
                        </option>
                        <option value="this_week" <?php echo e($filter == 'This Week' ? 'selected' : ''); ?>>This Week
                        </option>
                        <option value="last_week" <?php echo e($filter == 'Last Week' ? 'selected' : ''); ?>>Last Week
                        </option>
                        <option value="this_month" <?php echo e($filter == 'This Month' ? 'selected' : ''); ?>>This
                            Month</option>
                        <option value="last_month" <?php echo e($filter == 'Last Month' ? 'selected' : ''); ?>>Last
                            Month</option>
                        <option value="this_year" <?php echo e($filter == 'This Year' ? 'selected' : ''); ?>>This Year
                        </option>
                        <option value="last_year" <?php echo e($filter == 'Last Year' ? 'selected' : ''); ?>>Last Year
                        </option>
                    </select>
                </div>
            </div>
        </form>
        <div class="row mt-3">
            <?php if(!check_permission('dashboard.dashboard')): ?>
                <div class="col-md-4 col-3">
                    <div class="card bg-c-blue order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Sale</h6>
                            
                            <h4 class="text-right"><i class="fa fa-cart-plus f-left"></i><span>৳
                                    <?php echo e(number_format($today_invoice, 2)); ?></span>
                            </h4>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-3">
                    <div class="card bg-c-green order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Purchase</h6>
                            <h4 class="text-right"><i class="fa fa-rocket f-left"></i><span>৳
                                    <?php echo e(number_format($today_purchase, 2)); ?></span></h4>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-3">
                    <div class="card bg-c-yellow order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Expense</h6>
                            <h4 class="text-right"><i class="fa fa-refresh f-left"></i><span>৳
                                    <?php echo e(number_format($expence, 2)); ?></span></h4>
                        </div>
                    </div>
                </div>
            <?php else: ?>
                <div class="col-md-4 col-xl-3">
                    <div class="card bg-c-blue order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Sale</h6>
                            
                            <h4 class="text-right"><i class="fa fa-cart-plus f-left"></i><span>৳
                                    <?php echo e(number_format($today_invoice, 2)); ?></span>
                            </h4>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-xl-3">
                    <div class="card bg-c-green order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Purchase</h6>
                            <h4 class="text-right"><i class="fa fa-rocket f-left"></i><span>৳
                                    <?php echo e(number_format($today_purchase, 2)); ?></span></h4>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-xl-3">
                    <div class="card bg-c-yellow order-card">
                        <div class="card-block">
                            <h6 class="m-b-20"><?php echo e($filter); ?> Expense</h6>
                            <h4 class="text-right"><i class="fa fa-refresh f-left"></i><span>৳
                                    <?php echo e(number_format($expence, 2)); ?></span></h4>
                        </div>
                    </div>
                </div>
                <?php if(check_permission('dashboard.dashboard')): ?>
                    <div class="col-md-4 col-xl-3">
                        <div class="card bg-c-pink order-card">
                            <div class="card-block">
                                <h6 class="m-b-20"><?php echo e($filter); ?> Profit</h6>

                                <h4 class="text-right"><i class="fa fa-credit-card f-left"></i><span>৳
                                        <?php echo e(number_format($profit, 2)); ?></span>
                                </h4>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            <?php endif; ?>

        </div>

        <div class="row">
            <div class="col-md-6 col-xl-6">
                <div class="card bg-c-blue order-card">
                    <div class="card-block">
                        <h6 class="m-b-20"><?php echo e($filter); ?> Purchase due</h6>
                        <h2 class="text-right"><i
                                class="fa fa-cart-plus f-left"></i><span><?php echo e(number_format($pur_due, 2)); ?></span> TK</h2>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col-xl-6">
                <div class="card bg-c-green order-card">
                    <div class="card-block">
                        <h6 class="m-b-20"><?php echo e($filter); ?> Sale due</h6>
                        <h2 class="text-right"><i
                                class="fa fa-rocket f-left"></i><span><?php echo e(number_format($inv_due, 2)); ?></span> TK</h2>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <?php if(check_permission('dashboard.dashboard')): ?>
                <div class="col-md-6 col-xl-6">
                    <div class="card bg-c-blue order-card">
                        <div class="card-block">
                            <h6 class="m-b-20">Total Purchase Amount</h6>
                            <h2 class="text-right"><i
                                    class="fa fa-cart-plus f-left"></i><span><?php echo e(number_format($total_purchase - $return_purchase, 2)); ?></span>
                                TK
                            </h2>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if(check_permission('dashboard.dashboard')): ?>
                <div class="col-md-6 col-xl-6">
                    <div class="card bg-c-green order-card">
                        <div class="card-block">
                            <h6 class="m-b-20">Total Sale Amount</h6>
                            <h2 class="text-right"><i
                                    class="fa fa-rocket f-left"></i><span><?php echo e(number_format($total_invoice - $total_return, 2)); ?></span>
                                TK</h2>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if(check_permission('dashboard.dashboard')): ?>
                <div class="col-md-6 col-xl-6">
                    <div class="card bg-c-blue order-card">
                        <div class="card-block">
                            <h6 class="m-b-20">Available Stock Amount</h6>



                            <?php
                                $products = App\Models\Product::with('unit.related_unit')->get();
                                $purchase_price_tk = 0;
                                $selling_price_tk = 0;
                                foreach ($products as $key => $product) {
                                    if ($product->unit?->related_unit == null) {
                                        $inv_stock = App\Models\InvoiceItem::with('invoice')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $pur_stock = App\Models\PurchaseItem::with('purchase')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $rtn_stock = App\Models\ReturnItem::with('return')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $rtn_pur = App\Models\ReturnPurchaseItem::where(
                                            'product_id',
                                            $product->id,
                                        )->sum('main_qty');
                                        $open_pro_stock = App\Models\Product::where('id', $product->id)->first()
                                            ->o_main_qty;
                                        $total_stock =
                                            (float) ($pur_stock + $open_pro_stock + $rtn_stock) -
                                            ($inv_stock + $rtn_pur);
                                        $purchase_price = $total_stock * $product->purchase_price;
                                        $selling_price = $total_stock * $product->selling_price;
                                    } else {
                                        //invoice
                                        $inv_stock_main = App\Models\InvoiceItem::with('invoice')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $inv_total_main = (float) ($inv_stock_main * $product->unit->related_value);
                                        $inv_total_sub = App\Models\InvoiceItem::with('invoice')
                                            ->where('product_id', $product->id)
                                            ->sum('sub_qty');
                                        $inv_total_stock = (float) ($inv_total_main + $inv_total_sub);
                                        //return
                                        $rtn_stock_main = App\Models\ReturnItem::with('return')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $rtn_total_main = (float) ($rtn_stock_main * $product->unit->related_value);
                                        $rtn_total_sub = App\Models\ReturnItem::with('return')
                                            ->where('product_id', $product->id)
                                            ->sum('sub_qty');
                                        $rtn_total_stock = (float) ($rtn_total_main + $rtn_total_sub);
                                        //purchase
                                        $pur_stock_main = App\Models\PurchaseItem::with('purchase')
                                            ->where('product_id', $product->id)
                                            ->sum('main_qty');
                                        $open_main_pro_stock = App\Models\Product::where('id', $product->id)->first()
                                            ->o_main_qty;
                                        $main_pro = $open_main_pro_stock + $pur_stock_main;
                                        $pur_total_main = (float) ($main_pro * $product->unit->related_value);
                                        $pur_stock_sub = App\Models\PurchaseItem::with('purchase')
                                            ->where('product_id', $product->id)
                                            ->sum('sub_qty');
                                        $open_main_pro_stock = App\Models\Product::where('id', $product->id)->first()
                                            ->o_sub_qty;
                                        $pur_total_stock =
                                            (float) ($pur_total_main + $open_main_pro_stock + $pur_stock_sub);
                                        $total_stock = $pur_total_stock + $rtn_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;
                                    }
                                    $purchase_price_tk += $purchase_price;
                                    $selling_price_tk += $selling_price;
                                }
                            ?>

                            
                            <h2 class="text-right"><i
                                    class="fa fa-cart-plus f-left"></i><span><?php echo e(number_format($purchase_price_tk, 2)); ?></span>
                                TK</h2>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if(check_permission('dashboard.dashboard')): ?>
                <div class="col-md-6 col-xl-6">
                    <div class="card bg-c-green order-card">
                        <div class="card-block">
                            <h6 class="m-b-20">Stock Sale Amount</h6>
                            <h2 class="text-right"><i
                                    class="fa fa-rocket f-left"></i><span><?php echo e(number_format($selling_price_tk, 2)); ?></span>
                                TK
                            </h2>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            $('#filterSelect').change(function() {
                $('#filterForm').submit();
            });
        });
    </script>
<?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\demo-fastit\resources\views/backend/pages/dashboard/index.blade.php ENDPATH**/ ?>