/home/awneajlw/.trash/app.2/Http/Controllers/SubAdmin/SubInventoryController.php
<?php

namespace App\Http\Controllers\SubAdmin;

use Carbon\Carbon;
use App\Models\Branch;
use App\Models\Inventory;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;
use App\Models\InventoryCategory;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SubInventoryController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        $inventory = Inventory::where('branch_id',$user->branch_id)->count();

        // $branches = Branch::all();
        $inventoryCategory = InventoryCategory::all();

        return view('admin.subAdmin.inventory',compact('inventory','inventoryCategory'));
    }

    // yajra pagination
    public function getInventory(Request $request)
    {
        if ($request->ajax()) {

        $user = Auth::user();

            $fromDate = $request->input('from_date');
            $toDate = $request->input('to_date');

            if($fromDate == null && $toDate == null){

                $data = Inventory::where('branch_id',$user->branch_id)->with('branch','category')->get();
            }

            else{
                //Date Filter
                $data = Inventory::where('branch_id',$user->branch_id)->with('branch','category')->whereBetween('created_at', [$fromDate, $toDate])->get();
            }

            return Datatables::of($data)
                ->addIndexColumn()

                ->addColumn('action', function ($row) {

                    $btn = '<button class="btn btn-sm  btn-info ml-2 my-3 edit" value="' . $row->id . '" type="button"><i class="fa fa-edit"></i></button>';
                    $btn = $btn .  '<a class="btn btn-sm btn-danger ml-2 my-3" href="'.url('/sub-inventory-delete/'.$row->id).'" role="button"><i class="fa fa-trash"></i></a>';

                    return $btn;
                })
                ->addColumn('created_by', function ($row) {

                    $created_at = $row->created_at;

                    $formatted_date = Carbon::parse($created_at)->format('Y-m-d H:i:s');
                    return $formatted_date;
                })
                ->rawColumns(['action', 'created_by'])
                ->make(true);

        }
    }
    public function addinventory(Request $req){
        $user = Auth::user();
        Inventory::insert([
            'name' => $req->name,
            'description' => $req->description,
            'quantity' => $req->quantity,
            'price' => $req->price,
            'branch_id' =>   $user->branch_id,
            'category_id' => $req->category_id,

            'created_at' => now(),
        ]);

        session()->flash('message', 'Inventory add successfully!');


        return redirect()->back();
    }

    // edit Inventory
    public function editinventory($id){

        $inventory= Inventory::find($id);

        return response()->json([
            'inventory'=>$inventory,
        ]);
    }



    public function updateinventory(Request $req) {
        $inventory = Inventory::find($req->inventoryId);

        if ($inventory) {
            $inventory->name = $req->name;
            $inventory->description = $req->update_description;
            $inventory->quantity = $req->update_quantity;
            $inventory->price = $req->update_price;

            $inventory->category_id = $req->update_category_id;

            $inventory->save();
        }

        session()->flash('message', 'Inventory Update successful!');


        return redirect()->back();
    }

    // delete inventory
    public function deleteinventory($id){

        $inventory=Inventory::find($id);

        if ($inventory) {
            $inventory->delete();
        }

        session()->flash('message', 'Inventory deleted successfully!');

        return redirect()->back();
    }
}