File manager - Edit - /var/www/payraty/inventory_main/app/Http/Controllers/Admin/Invoice/InvoicesController.php
Back
<?php namespace App\Http\Controllers\Admin\Invoice; use App\Services\Warehouse\WarehouseService; use Throwable; use Illuminate\Http\Request; use App\DataTables\InvoiceDataTable; use App\Http\Controllers\Controller; use App\Http\Requests\InvoiceRequest; use App\Services\Invoice\InvoiceService; use App\Services\Product\ProductService; use App\Services\Customer\CustomerService; use App\Services\Product\ProductCategoryService; use App\Models\Invoice; use App\Models\Organisation; use Illuminate\Support\Facades\Auth; use PDF; use Excel; use App\Exports\InvoicesExport; use Illuminate\Support\Facades\DB; class InvoicesController extends Controller { protected $invoiceService; protected $customerService; protected $productService; protected $categoryService; protected $warehouseService; /** * __construct * * @return void */ public function __construct( InvoiceService $invoiceService, CustomerService $customerService, ProductService $productService, ProductCategoryService $categoryService, WarehouseService $warehouseService ) { $this->invoiceService = $invoiceService; $this->customerService = $customerService; $this->productService = $productService; $this->categoryService = $categoryService; $this->warehouseService = $warehouseService; // $this->middleware(['permission:List Invoice'])->only(['index']); $this->middleware(['permission:Add Invoice'])->only(['create']); $this->middleware(['permission:Edit Invoice'])->only(['edit']); $this->middleware(['permission:Show Invoice'])->only(['show']); $this->middleware(['permission:Delete Invoice'])->only(['destroy']); $this->middleware(['permission:View Payment Invoice'])->only(['getPayments']); // $this->middleware(['permission:Add Payment Invoice'])->only(['addPayment']); // $this->middleware(['permission:Make Payment Invoice'])->only(['makePayment']); // $this->middleware(['permission:Download Invoice'])->only(['download']); // $this->middleware(['permission:Send Invoice'])->only(['sendInvoice']); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(InvoiceDataTable $dataTable) { set_page_meta(__('custom.invoice')); $user = Auth::user(); $orgId = $user->organisation_id; $invoices = Invoice::where("organisation_id", $orgId)->orderBy('created_at', 'desc')->with('warehouse')->paginate(10); // return $dataTable->render('admin.invoices.index'); $profile = DB::table('profiles')->where('user_id', $user->id)->first(); return view('admin.invoices.index', compact('invoices', 'profile')); } public function filter(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $period = $request->get('period', 'all'); $query = Invoice::where('organisation_id', $orgId)->with('warehouse'); switch ($period) { case 'monthly': $query->whereMonth('created_at', now()->month); break; case 'yearly': $query->whereYear('created_at', now()->year); break; // 'all' case doesn't need additional filtering } $invoices = $query->orderBy('created_at', 'desc')->paginate(10); if ($request->ajax()) { return response()->json([ 'invoices' => $invoices ]); } // Fallback for non-AJAX requests return redirect()->route('admin.invoices.index'); } public function search(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $searchTerm = $request->get('q'); // Default pagination without filtering $invoices = Invoice::where('organisation_id', $orgId)->orderBy('created_at', 'desc')->paginate(10); // If it's an AJAX request (e.g. for live search), filter in the DB if ($request->ajax()) { $search = strtolower($searchTerm); $filteredInvoices = Invoice::with('warehouse')->where('organisation_id', $orgId) ->where(function ($query) use ($search) { $query->whereRaw("LOWER(JSON_UNQUOTE(JSON_EXTRACT(customer, '$.full_name'))) LIKE ?", ["%{$search}%"]) ->orWhereRaw("LOWER(JSON_UNQUOTE(JSON_EXTRACT(customer, '$.phone'))) LIKE ?", ["%{$search}%"]); }) ->orderBy('created_at', 'desc') ->paginate(10); return response()->json([ 'invoices' => $filteredInvoices ]); } // For non-AJAX (e.g. page load), return unfiltered paginated results return view('admin.invoices.index', compact('invoices')); } /** * exportInvoices * * @param mixed $request * @return void */ public function exportInvoices(Request $request) { $invoices = []; $type = $request->type; $user = Auth::user(); $orgId = $user->organisation_id; $invoices = Invoice::where('organisation_id', $orgId)->get(); $name = 'Invoice_' . now()->format('YmdHis'); if ($type == 'pdf') { $pdf = PDF::loadView('admin.invoices.pdf.index', ['invoices' => $invoices]); return $pdf->download($name . '.pdf'); } else if ($type == 'csv') { return Excel::download(new InvoicesExport($invoices), $name . '.csv'); } else if ($type == 'excel') { return Excel::download(new InvoicesExport($invoices), $name . '.xlsx'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $user = Auth::user(); $orgId = $user->organisation_id; $organisation = Organisation::where("id", $orgId)->first(); $warehouses = $this->warehouseService->pluck(); $warehouse = count($warehouses) > 1 && \request('warehouse') ? $this->warehouseService->getWareHouse(\request('warehouse')) : $this->warehouseService->firstWarehouse(); if (!$warehouse) { flash("Please create a warehouse before creating an invoice")->error(); return back(); // return response()->json(['success' => false, 'message' => "Please create a warehouse before creating an invoice"], 422); } $all_status = $this->invoiceService->getAllStatus(); $customers = $this->customerService->getActiveData(null, ['b_country_data', 'b_state_data', 'b_city_data']); // $products = $this->productService->wareHouseWiseProducts('warehouseStockQty', $warehouse);/*$this->productService->get(null, [], 20);*/ $productStocks = $this->productService->wareHouseWiseAllProductStocks(['product', 'attribute', 'attributeItem'], $warehouse); $categories = $this->categoryService->getParents('subCategory'); $formatted_category = []; $array_key = 0; foreach ($categories as $key => $category) { $formatted_category[$array_key] = [ 'id' => $category->id, 'text' => $category->name, 'image' => $category->image, ]; $array_key++; foreach ($category->subCategory as $subCategory) { $formatted_category[$array_key] = [ 'id' => $subCategory->id, 'text' => ' --' . $subCategory->name, 'image' => $subCategory->image, ]; $array_key++; foreach ($subCategory->subCategory as $subSubCategory) { $formatted_category[$array_key] = [ 'id' => $subSubCategory->id, 'text' => ' ---' . $subSubCategory->name, 'image' => $subCategory->image, ]; $array_key++; } } } array_unshift($formatted_category, ['id' => 'all', 'text' => 'All Categories']); set_page_meta(__('custom.create_invoice')); $user = Auth::user(); $profile = DB::table('profiles')->where('user_id', $user->id)->first(); return view('admin.invoices.create', [ 'customers' => $customers, 'product_stocks' => $productStocks, 'categories' => $formatted_category, 'warehouses' => $warehouses, 'warehouse' => $warehouse, 'all_status' => $all_status, "organisation" => $organisation, 'profile' => $profile ]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(InvoiceRequest $request) { $data = $request->validated(); // Customer if (!$data['customer_id']) { $data['customer'] = $data['walkin_customer']; } else { $customer = $this->customerService->get($data['customer_id']); $data['customer'] = $customer; } try { $invoice = $this->invoiceService->storeOrUpdate($data); flash(__('custom.invoice_created_successful'))->success(); return response()->json(['success' => true, 'invoice' => $invoice->id], 200); } catch (Throwable $th) { flash(__('custom.invoice_created_failed'))->success(); return response()->json(['success' => false, 'message' => $th->getMessage()], 422); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $invoice = $this->invoiceService->get($id, ['items', 'payments', 'customerInfo', 'saleReturns']); set_page_meta(__('custom.show_invoice')); return view('admin.invoices.show', compact('invoice')); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $invoice = $this->invoiceService->get($id, ['items']); $wareHouseId = $invoice->warehouse_id ?? $this->warehouseService->defaultWareHouse()->id; $warehouse = $this->warehouseService->get($wareHouseId); $all_status = $this->invoiceService->getAllStatus(); $customers = $this->customerService->getActiveData(null, ['b_country_data', 'b_state_data', 'b_city_data']); // $products = $this->productService->wareHouseWiseProducts('warehouseStockQty', $warehouse);//$this->productService->get(null, [], 20); $productStocks = $this->productService->wareHouseWiseAllProductStocks(['product', 'attribute', 'attributeItem'], $warehouse); $categories = $this->categoryService->getParents('subCategory'); $formatted_category = []; $array_key = 0; foreach ($categories as $category) { $formatted_category[$array_key] = [ 'id' => $category->id, 'text' => $category->name, ]; $array_key++; foreach ($category->subCategory as $subCategory) { $formatted_category[$array_key] = [ 'id' => $subCategory->id, 'text' => ' --' . $subCategory->name, ]; $array_key++; foreach ($subCategory->subCategory as $subSubCategory) { $formatted_category[$array_key] = [ 'id' => $subSubCategory->id, 'text' => ' ---' . $subSubCategory->name, ]; $array_key++; } } } array_unshift($formatted_category, ['id' => 'all', 'text' => 'All Categories']); set_page_meta(__('custom.edit_invoice')); return view('admin.invoices.edit', [ 'invoice' => $invoice, 'customers' => $customers, 'product_stocks' => $productStocks, 'categories' => $formatted_category, 'all_status' => $all_status, 'warehouse' => $warehouse, ]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(InvoiceRequest $request, $id) { $data = $request->validated(); // Customer if (!$data['customer_id']) { $data['customer'] = $data['walkin_customer']; } else { $customer = $this->customerService->get($data['customer_id']); $data['customer'] = $customer; } try { $invoice = $this->invoiceService->storeOrUpdate($data, $id); flash(__('custom.invoice_updated_successful'))->success(); return response()->json(['success' => true, 'invoice' => $invoice->id], 200); } catch (Throwable $th) { flash(__('custom.invoice_updated_failed'))->success(); return response()->json(['success' => false, 'message' => $th->getMessage()], 422); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $this->invoiceService->deleteInvoice($id); return redirect()->route('admin.invoices.index'); } /** * download * * @param mixed $id * @return void */ public function download($id) { return $this->invoiceService->download($id); } public function print($id) { return view('admin.invoices.thermal-print', [ 'invoice' => $this->invoiceService->get($id) ]); } /** * addPayment * * @param mixed $request * @return void */ public function addPayment(Request $request) { $data = $this->validate($request, [ 'invoice_id' => 'required|numeric', 'date' => 'required', 'payment_type' => 'required|max:50', 'amount' => 'required|numeric', 'notes' => 'nullable|max:200' ]); try { $this->invoiceService->addPayment($data); flash(__('custom.payment_added_successful'))->success(); return redirect()->route('admin.invoices.index'); } catch (Throwable $th) { flash(__('custom.payment_added_failed'))->error(); return redirect()->route('admin.invoices.index'); } } /** * getPayments * * @param mixed $invoice_id * @return void */ public function getPayments($invoice_id) { return $this->invoiceService->getPayments($invoice_id); } /** * invoiceCustomerEmail * * @param mixed $id * @return void */ public function invoiceCustomerEmail($id) { $email = $this->invoiceService->invoiceCustomerEmail($id); return response()->json($email); } /** * deletePayment * * @param mixed $id * @return void */ public function deletePayment($id) { $this->invoiceService->deletePayment($id); flash(__('custom.payment_deleted_successful'))->success(); return redirect()->route('admin.invoices.index'); } /** * sendInvoice * * @param mixed $request * @return void */ public function sendInvoice(Request $request) { $data = $this->validate($request, [ 'invoice_id' => 'required|numeric', 'email' => 'required|email', ]); try { $this->invoiceService->sendInvoice($data); flash(__('custom.invoice_added_successful'))->success(); return redirect()->route('admin.invoices.index'); } catch (Throwable $th) { flash(__('custom.invoice_added_failed'))->error(); return redirect()->route('admin.invoices.index'); } } /** * getByToken * * @param mixed $token * @return void */ public function getByToken($token) { try { $invoice = $this->invoiceService->getByToken($token); if (!$invoice) abort(404); return view('admin.invoices.live_url', compact('invoice')); } catch (Throwable $th) { abort(404); } } /** * payStripe * * @param mixed $request * @return void */ public function payStripe(Request $request) { try { $payment_url = $this->invoiceService->payByStripe($request->invoice_id); return redirect($payment_url); } catch (\Throwable $th) { flash(__('custom.stripe_setup_message'))->warning(); return back(); } } /** * stripeSuccess * * @param mixed $request * @return void */ public function stripeSuccess(Request $request) { $this->invoiceService->stripePaymentSuccess($request->invoice_id); return redirect()->route('payment.success'); } /** * stripeCancel * * @param mixed $request * @return void */ public function stripeCancel(Request $request) { // TODO: handle cancel } /** * paypalSuccess * * @param mixed $request * @return void */ public function paypalSuccess(Request $request) { $this->invoiceService->paypalPaymentSuccess($request->invoice_id, $request->order_id); return redirect()->route('payment.success'); } /** * paymentSuccess * * @return void */ public function paymentSuccess() { return view('admin.invoices.payment_success'); } /** * makePayment * * @param mixed $id * @return void */ public function makePayment($id) { $invoice = $this->invoiceService->get($id, ['items']); set_page_meta(__('custom.make_invoice')); return view('admin.invoices.make_payment', compact('invoice')); } /** * makePaymentPost * * @param mixed $request * @param mixed $id * @return void */ public function makePaymentPost(Request $request, $id) { $this->invoiceService->makePayment($id, $request->all()); flash(__('custom.payment_make_successfully'))->success(); return redirect()->route('admin.invoices.show', $id); } public function deliveryStatusChange($id, $status) { $this->invoiceService->deliveryStatusChange($id, $status); flash(__('custom.invoice_delivered_status_changed_successfully'))->success(); return redirect()->back(); } public function getAllInvoiceAPI(Request $request) { try { $user = Auth::user(); $orgId = $user->organisation_id; $perPage = $request->query('limit', 10); $page = $request->query('page', 1); $paymentType = $request->query('paymentType', 'cash'); $status = $request->query('status', 'paid'); $query = Invoice::where('organisation_id', $orgId) ->with('warehouse') ->when($paymentType, function ($q) use ($paymentType) { return $q->where('payment_type', $paymentType); }) ->when($status, function ($q) use ($status) { return $q->where('status', $status); }); $invoices = $query->paginate($perPage, ['*'], 'page', $page); return response()->json([ "status" => 'Success', "message" => 'Invoices returned successfully', "data" => $invoices ]); } catch (\Exception $e) { return response()->json([ 'status' => 'failed', 'message' => $e->getMessage(), ], 500); } } public function getSingleInvoice(Request $request) { try { $user = Auth::user(); $orgId = $user->organisation_id; $invoiceId = $request->invoiceId; $invoices = Invoice::where('organisation_id', $orgId)->where('id', $invoiceId) ->with('warehouse')->first(); if (empty($invoices)) { return response()->json([ "status" => 'failed', "message" => 'Invoice not found', ], 404); } return response()->json([ "status" => 'Success', "message" => 'Invoice returned successfully', "data" => $invoices ]); } catch (\Exception $e) { return response()->json([ 'status' => 'failed', 'message' => $e->getMessage(), ], 500); } } public function deleteInvoice(Request $request) { try { $user = Auth::user(); $orgId = $user->organisation_id; $invoiceId = $request->invoiceId; $invoices = Invoice::where('organisation_id', $orgId)->where('id', $invoiceId)->first(); if (empty($invoices)) { return response()->json([ "status" => 'failed', "message" => 'Invoice not found', ], 404); } //Delete Invoice $invoices->delete(); return response()->json([ "status" => 'Success', "message" => 'Invoice deleted successfully', "data" => $invoices ]); } catch (\Exception $e) { return response()->json([ 'status' => 'failed', 'message' => $e->getMessage(), ], 500); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings