<?php


namespace App\Http\Controllers\Administrator;


use App\Http\Controllers\Controller;
use App\Model\pos\SaleDetailsModel;
use App\Model\pos\SaleModel;
use App\Model\product\ProductModel;
use Auth;
use DB;
use Illuminate\Http\Request;


class DashboardAdministratorController extends Controller

{
    public function index()

    {
        return view('administrator.dashboard-administrator');
    }

    public function getAlert(Request $request)

    {
        if ($request->type == 'tra') {
            return \App\Model\stock_inventory\TransferStockModel::where(['confirm_status' => null, 'type' => 'item', 'to_branch' => myBranch()->id])->count();

        }


        if ($request->type == 'sa') {
            return \App\Model\stock_inventory\StockModel::join('nso007_products', 'nso007_products.id', 'nso007_stocks.product_id')
                ->whereColumn('nso007_products.alert_qty', '>=', 'nso007_stocks.stock_qty')
                ->count();

        }
        if ($request->type == 'pr') {
            return \App\Model\pos\SaleModel::where(function ($q) {
                if (!myBranch()->main) {
                    $q->where('nso007_sales.branch_id', myBranch()->id);
                }
                if (!Auth::user()->can('View Sale Request All Branch')) {
                    $q->where('branch_id', Auth::user()->branch_id);
                    $q->orwhere('user_id', Auth::user()->id);
                }
            })->where('status', 0)->count();

        }
        if ($request->type == 'sr') {
            return \App\Model\stock_inventory\ItemRequestModel::where('request_to', myBranch()->id)->where('status', 0)->count();

        }
        if ($request->type == 'tr') {
            $sr = \App\Model\pos\SaleModel::where(function ($q) {
                if (!myBranch()->main) {
                    $q->where('nso007_sales.branch_id', myBranch()->id);
                }
                if (!Auth::user()->can('View Sale Request All Branch')) {
                    $q->where('branch_id', Auth::user()->branch_id);
                    $q->orwhere('user_id', Auth::user()->id);
                }
            })->where('status', 0)->count();
            $pr = \App\Model\stock_inventory\ItemRequestModel::where('request_to', myBranch()->id)->where('status', 0)->count();
            return $sr + $pr;
        }
        return 0;
    }


}

