IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Stock/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Stock/StockReportController.php

<?php

namespace App\Http\Controllers\School\Stock;

use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use Auth;
use DB;
use Carbon\Carbon;
use Session;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;

class StockReportController extends SchoolController {
    
    
    public function stockpayReport(Request $request){
         $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        $categories =  ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $data = DB::table('shop_transaction_details')->join('products','shop_transaction_details.idProduct','=','products.idProduct')->join('students','shop_transaction_details.idStudent','=','students.idStudent')->join('shop_transaction','shop_transaction_details.idTransaction','=','shop_transaction.idTransaction')->select('shop_transaction_details.qty','shop_transaction_details.unitPrice','shop_transaction_details.amountPaid','shop_transaction_details.paymentDate','ecNo','firstName','middleName','lastName','productName');
        if ($request->has('products') && count($request->products) > 0) {
            $data = $data->whereIn('shop_transaction_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $data = $data->where('products.idProductCategory', '=', $request->idProductCategory);
        }
        
        if ($request->toDate != null && $request->fromDate != null) {
                $data = $data->whereBetween('shop_transaction_details.paymentDate', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
            } else if ($request->has('fromDate') && $request->fromDate != null) {
                $data = $data->whereBetween('shop_transaction_details.paymentDate', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
            } else if ($request->has('toDate') && $request->toDate != null) {
                $data = $data->whereDate('shop_transaction_details.paymentDate', '<=', $to_date);
            }
            
        $data = $data->where('shop_transaction.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('shop_transaction.idFinancialYear', Session::get('idFinancialYear'))->get();    
        return view('schools.stock.stock_pay_report', compact('data','categories'));
    }

    public function deviceReport(Request $request){
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }

        if($request->has('type') && $request->type != null){
            $stock = DB::table('product_received_details')
            ->join('products', 'product_received_details.idProduct', '=', 'products.idProduct')
            ->join('product_received', 'product_received_details.idProductReceived', '=', 'product_received.idProductReceived')
            ->where('product_received.idSchool', '=', Auth::guard('school')->user()->idSchool)
            ->whereNotNull('serialNo');
            if ($request->toDate != null && $request->fromDate != null) {
                $stock = $stock->whereBetween('receivedDate', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
            } else if ($request->has('fromDate') && $request->fromDate != null) {
                $stock = $stock->whereBetween('receivedDate', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
            } else if ($request->has('toDate') && $request->toDate != null) {
                $stock = $stock->whereDate('receivedDate', '<=', $to_date);
            }
            $stock = $stock->get();

            $serialNo = DB::table('invoice_serial')->join('stock_invoicepayment', 'invoice_serial.idInvoiceDetial', '=', 'stock_invoicepayment.idInvoicePayment')
            ->where('stock_invoicepayment.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('serialNo','!=','')->where('is_return','N')->where('paymentStatus','!=','Cancelled')
            ->get()->pluck('serialNo')->toArray();

            return view('schools.stock.device_stock_report', compact('stock','serialNo'));
        }
        $customer = ['' => '---Select---'] +DB::table('customers')->where('idSchool',Auth::guard('school')->user()->idSchool)->pluck('customerName','idCustomer')->toArray();
        $invoice = DB::table('invoice_serial')->join('invoices', 'invoice_serial.idInvoice', '=', 'invoices.idInvoice')
        ->join('stock_invoicepayment', 'invoice_serial.idInvoiceDetial', '=', 'stock_invoicepayment.idInvoicePayment')
        ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('serialNo','!=','')->where('is_return','N')->where('paymentStatus','!=','Cancelled');
        if ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        if ($request->has('customer') && $request->customer != null) {
            $invoice = $invoice->where('invoices.idCustomer', $request->customer );
        }
        if ($request->has('serialNo') && $request->serialNo != null) {
            $invoice = $invoice->where('invoice_serial.serialNo', $request->serialNo );
        }
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->get();
        return view('schools.stock.device_report', compact('invoice','customer'));
    }


    public function updateReturn(Request $request){
        if ($request->has('id') && $request->id != null) {
            try {
                $decrypted = Crypt::decryptString($request->id);
                $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
                $invoice = DB::table('invoice_serial')->where('id',$decrypted)->first();
                if($invoice != null){
                    $file = null;
                    if ($request->hasFile('utr_file')) {
                        $photo = 'utr_file_' . $decrypted. '.' . $request->file('utr_file')->getClientOriginalExtension();
                        $request->file('utr_file')->storeAs('public/schools/' . $school->idSchool . '/utr/', $photo);
                        $file = $photo;
                    }
                    $pending = $invoice->return_amount - $request->paid_amount;
                    DB::table('invoice_serial')->where('id',$decrypted)->update(
                        [
                            'paid_amount' => $request->paid_amount,
                            'utr_number' => $request->utr_number,
                            'paid_date' => $request->paid_date,
                            'utr_file' => $file,
                            'pending_amount' => $pending
                        ]
                    );
                }
                return redirect('/school/return-report')->with('success','Return entry updated');
            } catch (DecryptException $e) {
                return redirect()->back()->with('error','Invalid invoice details please check');
            }
        }

        return redirect()->back()->with('error','Invalid invoice details please check');
    }
    public function showReturn(Request $request){
        if ($request->has('id') && $request->id != null) {
            try {
                $decrypted = Crypt::decryptString($request->get('id') );
                $invoice = DB::table('invoice_serial')->join('invoices', 'invoice_serial.idInvoice', '=', 'invoices.idInvoice')
                ->join('stock_invoicepayment', 'invoice_serial.idInvoiceDetial', '=', 'stock_invoicepayment.idInvoicePayment')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('is_return','Y')->where('paymentStatus','!=','Cancelled')
                ->where('id',$decrypted)->first();
                return view('schools.stock.return_index',compact('invoice'));
            } catch (DecryptException $e) {
                return redirect('/school/return-report');
            }
        }

        return redirect('/school/return-report');
    }

    public function amountReturnReport(Request $request){
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        $invoice = DB::table('invoice_serial')->join('invoices', 'invoice_serial.idInvoice', '=', 'invoices.idInvoice')
        ->join('stock_invoicepayment', 'invoice_serial.idInvoiceDetial', '=', 'stock_invoicepayment.idInvoicePayment')
        ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('is_return','Y')->where('paymentStatus','!=','Cancelled');
        if ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->get();
        return view('schools.stock.return_report', compact('invoice'));
    }

    public function collectionReport(Request $request){
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        $invoice = DB::table('stock_invoicepayment')->join('invoices', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
        ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->where('paymentMode','!=','Unpaid')->where('paymentStatus','!=','Cancelled');
        if ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->get();
        return view('schools.stock.collection_report', compact('invoice'));
    }

    public function stockReport(Request $request) {
        $categories = ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $products = DB::table('stockledger')
                ->join('products', 'stockledger.idProduct', '=', 'products.idProduct')
                ->join('product_categories', 'products.idProductCategory', '=', 'product_categories.idProductCategory')
                ->where('stockledger.idSchool', '=', Auth::guard('school')->user()->idSchool);
        if ($request->has('products') && count($request->products) > 0) {
            $products = $products->whereIn('stockledger.idProduct', $request->products)
                    ->where('stockledger.idFinancialYear', '=', $request->idFinancialYear)
                    ->get();
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $products = $products->where('stockledger.idFinancialYear', '=', $request->idFinancialYear)
                    ->where('products.idProductCategory', '=', $request->idProductCategory)
                    ->get();
        } elseif ($request->idFinancialYear != null) {
            $products = $products->where('stockledger.idFinancialYear', '=', $request->idFinancialYear)->get();
        } else {
            $products = $products->where('stockledger.idFinancialYear', '=', Session::get('idFinancialYear'))->get();
        }
        return view('schools.stock.stock_report', compact('products', 'categories'));
    }

    public function purchaseReport(Request $request) {
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        $categories = ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $suppliers = ['' => '---Select---'] + \App\Supplier::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('bussinessName', 'idSupplier')
                        ->toArray();
        $goods = DB::table('product_received')
                ->join('product_received_details', 'product_received_details.idProductReceived', '=', 'product_received.idProductReceived')
                ->join('product_payment', 'product_payment.idProductReceived', '=', 'product_received.idProductReceived')
                ->join('purchase_orders', 'product_received.idPurchaseOrder', '=', 'purchase_orders.idPurchaseOrder')
                ->join('products', 'product_received_details.idProduct', '=', 'products.idProduct')
                ->join('suppliers', 'product_received.idSupplier', '=', 'suppliers.idSupplier')
                ->where('product_received.idSchool', '=', Auth::guard('school')->user()->idSchool)
                ->groupBy('product_received.idProductReceived')
                ->select('product_received.*', 'product_payment.*', 'idProductCategory', 'product_received.idSupplier', 'product_received.receiptNo', 'bussinessName', 'poNo', DB::raw('group_concat(productName)AS products'));

        if ($request->has('idProductCategory') && $request->idProductCategory != null && $request->has('idSupplier') && $request->idSupplier != null) {
            $goods = $goods->where('product_received.idSupplier', '=', $request->idSupplier)
                    //->where('purchase_orders.status','=',$request->status)
                    ->where('products.idProductCategory', '=', $request->idProductCategory)
                    ->get();
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $goods = $goods->where('products.idProductCategory', '=', $request->idProductCategory)
                    // ->where('purchase_orders.status','=',$request->status)
                    ->get();
        } elseif ($request->has('idSupplier') && $request->idSupplier != null) {
            $goods = $goods->where('product_received.idSupplier', '=', $request->idSupplier)
                    // ->where('purchase_orders.status','=',$request->status)
                    ->get();
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $goods = $goods->whereBetween('product_received.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"])->get();
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $goods = $goods->whereBetween('product_received.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"])->get();
        } else if ($request->has('toDate') && $request->toDate != null) {
            $goods = $goods->whereDate('product_received.created_at', '<=', $to_date)->get();
        } else {
            $goods = $goods->get();
        }
        return view('schools.stock.purchase_report', compact('categories', 'suppliers', 'goods'));
    }

    public function inventoryReport(Request $request) {
        $categories = ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        /*$invoice_employees = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftjoin('employees', 'invoices.idCustomer', '=', 'employees.idEmployee')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                 ->select('enrollmentNo as ecNo', 'firstName', 'lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'staff')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool) ;
        if ($request->has('products') && count($request->products) > 0) {
            $invoice_employees = $invoice_employees->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoice_employees = $invoice_employees->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice_employees = $invoice_employees->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice_employees = $invoice_employees->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice_employees = $invoice_employees->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoice_employees = $invoice_employees;
        }
        $invoices_employees = $invoice_employees
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"));

        $invoice = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('students', 'invoices.idCustomer', '=', 'students.idStudent')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                ->select('ecNo', 'firstName', 'lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'Student')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool);
        if ($request->has('products') && count($request->products) > 0) {
            $invoice = $invoice->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoice = $invoice->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoice = $invoice;
        }

        $invoiceCustomer = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('customers', 'invoices.idCustomer', '=', 'customers.idCustomer')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                ->select('customerNumber as ecNo','customerName as firstName','gstin as lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'Customer')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool);
        if ($request->has('products') && count($request->products) > 0) {
            $invoiceCustomer = $invoiceCustomer->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoiceCustomer = $invoiceCustomer->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoiceCustomer = $invoiceCustomer;
        }


        $invoices = $invoice
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"))
                ->union($invoices_employees)
                ->groupBy('invoices.idInvoice')
                ->get();

        $invoiceCustomer = $invoiceCustomer
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"))
                ->groupBy('invoices.idInvoice')
                ->get();  */      
       
        $invoice = DB::table('invoices')
            ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool) ;

        if ($request->has('products') && count($request->products) > 0) {
            $idInvoice = DB::table('invoice_details')->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->whereIn('invoice_details.idProduct', $request->products)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $idInvoice = DB::table('invoice_details') ->join('products','invoice_details.idProduct','=','products.idProduct')
            ->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->where('products.idProductCategory', '=', $request->idProductCategory)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->get();
        return view('schools.stock.inventory_report', compact('invoice', 'categories'));
    }

    public function stockTally(Request $request){
        $categories = ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }
        $invoice = DB::table('invoices')
        ->select('invoices.idCustomer','customerType')
        ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool) ;

        if ($request->has('products') && count($request->products) > 0) {
            $idInvoice = DB::table('invoice_details')->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->whereIn('invoice_details.idProduct', $request->products)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $idInvoice = DB::table('invoice_details') ->join('products','invoice_details.idProduct','=','products.idProduct')
            ->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->where('products.idProductCategory', '=', $request->idProductCategory)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->groupBy('invoices.idCustomer','customerType')
                    ->get(); 
        return view('schools.stock.tally_inventory_report', compact('invoice', 'categories'));
    }

    public function inventoryDueReport(Request $request) {
        $customer = ['' => '---Select---'] +DB::table('customers')->where('idSchool',Auth::guard('school')->user()->idSchool)->pluck('customerName','idCustomer')->toArray();
        $categories = ['' => '---Select---'] + \App\ProductCategory::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->pluck('categoryName', 'idProductCategory')
                        ->toArray();
        $today_date = Carbon::today()->format('Y-m-d');
        if ($request->has('fromDate') && $request->fromDate != null) {
            $from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
        }
        if ($request->has('toDate') && $request->toDate != null) {
            $to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
        }

        /*$invoice_employees = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftjoin('employees', 'invoices.idCustomer', '=', 'employees.idEmployee')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                 ->select('enrollmentNo as ecNo', 'firstName', 'lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'staff')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool) ;
        if ($request->has('products') && count($request->products) > 0) {
            $invoice_employees = $invoice_employees->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoice_employees = $invoice_employees->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice_employees = $invoice_employees->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice_employees = $invoice_employees->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice_employees = $invoice_employees->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoice_employees = $invoice_employees;
        }
        $invoices_employees = $invoice_employees
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"));

        $invoice = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('students', 'invoices.idCustomer', '=', 'students.idStudent')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                ->select('ecNo', 'firstName', 'lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'Student')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool);
        if ($request->has('products') && count($request->products) > 0) {
            $invoice = $invoice->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoice = $invoice->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoice = $invoice;
        }

        $invoiceCustomer = DB::table('invoices')
                ->join('invoice_details', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
                ->join('customers', 'invoices.idCustomer', '=', 'customers.idCustomer')
                ->join('products','invoice_details.idProduct','=','products.idProduct')
                ->leftJoin('stock_invoicepayment', 'stock_invoicepayment.idInvoice', '=', 'invoices.idInvoice')
                ->select('customerNumber as ecNo','customerName as firstName','gstin as lastName', 'invoices.*', 'stock_invoicepayment.*')
                ->where('customerType', '=', 'Customer')
                ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool);
        if ($request->has('products') && count($request->products) > 0) {
            $invoiceCustomer = $invoiceCustomer->whereIn('invoice_details.idProduct', $request->products);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $invoiceCustomer = $invoiceCustomer->where('products.idProductCategory', '=', $request->idProductCategory);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoiceCustomer = $invoiceCustomer->whereDate('invoices.created_at', '<=', $to_date);
        } else {
            $invoiceCustomer = $invoiceCustomer;
        }


        $invoices = $invoice
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"))
                ->union($invoices_employees)
                ->groupBy('invoices.idInvoice')
                ->get();

        $invoiceCustomer = $invoiceCustomer
                ->addSelect(DB::raw(" (SELECT group_concat(quantity) FROM invoice_details where invoice_details.idInvoice = invoices.idInvoice)  AS quantity") )
                ->addSelect(DB::raw(" (SELECT SUM(paidAmount) FROM stock_invoicepayment where stock_invoicepayment.idInvoice = invoices.idInvoice) as amountPaid ") )
                ->addSelect(DB::raw(" (SELECT group_concat(productName) FROM invoice_details JOIN products ON products.idProduct = invoice_details.idProduct where invoice_details.idInvoice = invoices.idInvoice)  AS products"))
                ->groupBy('invoices.idInvoice')
                ->get();     */
        $invoice = DB::table('invoices')
            ->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool) ;

        if ($request->has('products') && count($request->products) > 0) {
            $idInvoice = DB::table('invoice_details')->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->whereIn('invoice_details.idProduct', $request->products)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->has('idProductCategory') && $request->idProductCategory != null) {
            $idInvoice = DB::table('invoice_details') ->join('products','invoice_details.idProduct','=','products.idProduct')
            ->join('invoices', 'invoice_details.idInvoice', '=', 'invoices.idInvoice')
            ->where('products.idProductCategory', '=', $request->idProductCategory)->where('invoices.idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('idInvoice')->toArray();
            $invoice = $invoice->whereIn('invoice_details.idInvoice', $idInvoice);
        } elseif ($request->toDate != null && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"]);
        } else if ($request->has('fromDate') && $request->fromDate != null) {
            $invoice = $invoice->whereBetween('invoices.created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"]);
        } else if ($request->has('toDate') && $request->toDate != null) {
            $invoice = $invoice->whereDate('invoices.created_at', '<=', $to_date);
        }
        if ($request->has('customer') && $request->customer != null) {
            $invoice = $invoice->where('invoices.idCustomer', $request->customer );
        }
        $invoice = $invoice->orderBy('invoices.idInvoice','DESC')
                    ->get();
                    
        return view('schools.stock.inventory_due_report', compact('invoice', 'categories','customer'));   
       
        //return view('schools.stock.inventory_due_report', compact('invoices', 'categories','invoiceCustomer'));
    }

}

Copyright © 2021 - 2025 IMMREX7