<?php
namespace App\Http\Controllers\Admin\accounting;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\LogActivity;
use App\Models\{
    User,
    Role,
    School,
    Plan
};
use App\Models\accounting\{
    AccountLedger,
    AccountLedgerGroup,
    AccountBank,
    AccountItem,
    AccountItemUnit,
    AccountItemCategory,
    AccountInvoiceItem,
    AccountPurchaseBillItem,
    AccountCreditNoteItem,
    AccountDebitNoteItem
};
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use App\Exports\ItemExport;
use App\Imports\ItemImport;
use Maatwebsite\Excel\Facades\Excel;
use Validator;

class InventoryController extends Controller
{
    protected $admin_id;
    protected $school_id, $res,$allData,$data,$validate;
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            // fetch session and use it in entire class with constructor
            if (session()->get('loggedIn')['user_type'] == 'admin') {
                $this->admin_id = session()->get('loggedIn')['id'];
                $this->school_id = School::where("admin_id",$this->admin_id)->pluck("id")->first();
            } else {
                $this->admin_id = session()->get('loggedIn')['admin_id'];
                $this->school_id = School::where("admin_id",$this->admin_id)->pluck("id")->first();
            }
         
            return $next($request);
        });
    }

    public function exportItems()
    {        
        $filename = "items.xlsx";
        return Excel::download(new ItemExport($this->admin_id),$filename);
        
    }

    public function importItems(Request $request)
    {
        $file = $request->file('excel_file');        

        Excel::import(new ItemImport($this->admin_id), $file);

        return response()->json(["message"=>"success"],200);
    }

     // Display all employees
     public function index()
     {
        $data['pagetitle'] = "account";
        $data['mainSuperAdminMenu'] = 'inventory';
        $data['superadminSubMenu'] = 'inventory';

       
        $search = isset($_GET["search"]) ? $_GET["search"] : null;
        $category = isset($_GET["category"]) ? $_GET["category"] : null;
       
        $query = AccountItem::where('account_items.admin_id',$this->admin_id)
        ->leftJoin("account_item_units","account_item_units.id","=","account_items.unit_id")
        ->leftJoin("account_item_categories","account_item_categories.id","=","account_items.category_id");
        
        if($search)
        {
            $query->where("account_items.name","LIKE","%".$search."%");
        }
        if($category)
        {
            $query->where("account_item_categories.name","LIKE","%".$category."%");
        }

        $data['items'] = $query->select(
            "account_items.id",
            "account_items.name",
            "account_items.item_code",
            "account_items.sales_unit_price",
            "account_items.purchase_unit_price",
            "account_items.quantity",
            "account_items.sold",
            "account_items.type",
            "account_item_units.name as unit_name",
            "account_item_categories.name as cat_name"
        )->orderBy('account_items.created_at','desc')->paginate(10);
     

        return view('accounting.inventory.list',$data);
     }
 
     // Show the form to create a new product
     public function viewSoldItem($id)
     {
       
        $data['pagetitle'] = "account";
        $data['mainSuperAdminMenu'] = 'inventory';
        $data['superadminSubMenu'] = 'inventory';

       
        $search = isset($_GET["search"]) ? $_GET["search"] : null;
        $category = isset($_GET["category"]) ? $_GET["category"] : null;
       
        $data['sold_item'] = AccountInvoiceItem::where('account_invoice_items.admin_id',$this->admin_id)
        ->where("account_invoice_items.item_id",$id)
        ->leftJoin("account_invoices","account_invoices.id","=","account_invoice_items.invoice_id")
        ->leftJoin("account_customers","account_customers.id","=","account_invoices.customer_id")
        ->select(
            "account_invoice_items.*",
            "account_customers.name as customer_name"
        )->orderBy('account_invoice_items.created_at','desc')->paginate(10);

        
        $data['purchase_item'] = AccountPurchaseBillItem::where('account_purchase_bill_items.admin_id',$this->admin_id)
        ->where("account_purchase_bill_items.item_id",$id)
        ->leftJoin("account_purchase_bills","account_purchase_bills.id","=","account_purchase_bill_items.bill_id")
        ->leftJoin("account_customers","account_customers.id","=","account_purchase_bills.vendor_id")
        ->select(
            "account_purchase_bill_items.*",
            "account_customers.name as customer_name"
        )->orderBy('account_purchase_bill_items.created_at','desc')->paginate(10);

        
        $data['credit_note'] = AccountCreditNoteItem::where('account_credit_note_items.admin_id',$this->admin_id)
        ->where("account_credit_note_items.item_id",$id)
        ->leftJoin("account_credit_notes","account_credit_notes.id","=","account_credit_note_items.credit_note_id")
        ->leftJoin("account_customers","account_customers.id","=","account_credit_notes.customer_id")
        ->select(
            "account_credit_note_items.*",
            "account_customers.name as customer_name"
        )->orderBy('account_credit_note_items.created_at','desc')->paginate(10);
     
        
        $data['debit_notes'] = AccountDebitNoteItem::where('account_debit_note_items.admin_id',$this->admin_id)
        ->where("account_debit_note_items.item_id",$id)
        ->leftJoin("account_debit_notes","account_debit_notes.id","=","account_debit_note_items.debit_note_id")
        ->leftJoin("account_customers","account_customers.id","=","account_debit_notes.customer_id")
        ->select(
            "account_debit_note_items.*",
            "account_customers.name as customer_name"
        )->orderBy('account_debit_note_items.created_at','desc')->paginate(10);

        return view('accounting.inventory.item_details',$data);

     }
 


     

}
