<?php


namespace App\Http\Controllers\report;


use App\Fun\Fun;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Model\stock_inventory\TransferStockModel;
use App\Model\setup\WarehouseModel;
use App\Model\Hr\HrBranch;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\DataTables;

class ReportTransferStockController extends Controller

{

    // function __construct()

    // {
    //     $this->middleware('permission:Report Sale  Detail List', ['only' => ['index']]);
    //     Fun::lang();
    // }
    public function index(Request $request)
    {
        $warehouse = WarehouseModel::where('branch_id', Auth::user()->branch_id)->pluck('warehouse_name', 'id');
        $branch = TransferStockModel::pluck('branch_id');
        $branch = HrBranch::whereIn('id', $branch)->pluck('name', 'id');
        if ($request->ajax()) {
            Fun::lang();
            $search = $request->input('search.value');
            $wh = $request->to_warehouse_id;
            $branch = $request->branch_id;
            $from_date = $request->start_date;
            $to_date = $request->end_date;
            $branch_id = Auth::user()->branch_id;
            $warehouse = WarehouseModel::where('branch_id',$branch_id)->pluck('id');
            $count_total = TransferStockModel::count();
            $data = TransferStockModel::where(function ($q) use($warehouse) {
                $q->orwhere('branch_id', Auth::user()->branch_id);
                $q->orwhere('user_id', Auth::user()->id);
                $q->orwhereIn('to_warehouse_id', $warehouse);
            })
                -> where(function ($query) use ($search) {
                    $query->orWhere('nso007_transfer_stock.invoice_number', 'LIKE', '%' . $search . '%');
                })
                ->where(function ($q) use ($wh) {
                    if ($wh) {
                        $q->where('nso007_transfer_stock.to_warehouse_id', $wh);
                    }
                })
                ->where(function ($q) use ($branch) {
                    if ($branch) {
                        $q->where('nso007_transfer_stock.branch_id', $branch);
                    }
                })
                ->where(function ($q) use ($from_date, $to_date) {
                    if ($from_date) {
                        $q->whereDate('nso007_transfer_stock.date', '>=', date('Y-m-d', strtotime($from_date)));
                    }
                    if ($to_date) {
                        $q->whereDate('nso007_transfer_stock.date', '<=', date('Y-m-d', strtotime($to_date)));
                    }

                })
                ->orderBy('nso007_transfer_stock.id', 'DESC');
            $count_filter = $data->count();
            if ($request->isMethod('get')) {
                return DataTables::of($data->take(20))
                    ->with([
                        "recordsTotal" => $count_total,
                        "recordsFiltered" => $count_filter,
                    ])
                    ->addColumn('from_warehouse_id', function ($item) {
                        return findWarehouse($item->from_warehouse_id)->warehouse_name??'';
                    })
                    ->addColumn('to_warehouse_id', function ($item) {
                        return findWarehouse($item->to_warehouse_id)->warehouse_name??'';
                    })
                    ->addColumn('branch_id', function ($item) {
                        return findBranch($item->branch_id)->name;
                    })

                    ->addColumn('date', function ($item) {
                        return date('d-m-Y', strtotime($item->date));
                    })
                    ->addIndexColumn()
                    ->rawColumns([])
                    ->make(true);
            }
        }

        return view('administrator.report-transfer-stock.index', compact('warehouse','branch'));
    }


}

