Message Error 'invoice creation is not allowed'
-
Hello!
I am creating a custom function in the shop where after the customer makes a payment, the invoice is sent immediately. I added this function in ‘Shop/onepagecontroller’.// i took this from InvoiceController.php in the store function. public function sendInvoice(int $orderId) { try { $order = $this->orderRepository->findOrFail($orderId); if (! $order->canInvoice()) { session()->flash('error', trans('admin::app.sales.invoices.create.creation-error')); return redirect()->back(); } $items = $this->prepareInvoiceData($order); $request = new \Illuminate\Http\Request($items); $data = $request->validate([ 'invoice.items' => 'required|array', 'invoice.items.*' => 'required|numeric|min:0', ]); if (! $this->invoiceRepository->haveProductToInvoice($data)) { session()->flash('error', trans('admin::app.sales.invoices.create.product-error')); return redirect()->back(); } if (! $this->invoiceRepository->isValidQuantity($data)) { session()->flash('error', trans('admin::app.sales.invoices.create.invalid-qty')); return redirect()->back(); } $this->invoiceRepository->create(array_merge($data, [ 'order_id' => $orderId, ])); session()->flash('success', 'Invoice berhasil dibuat'); } catch (\Throwable $th) { \Log::error('Send Invoice Error', [ 'order_id' => $orderId, 'message' => $th->getMessage(), 'trace' => $th->getTraceAsString(), ]); session()->flash('error', 'Terjadi kesalahan saat membuat invoice'); return redirect()->back(); } }// I took this from GenerateInvoice.php protected function prepareInvoiceData($order) { $invoiceData = ['order_id' => $order->id]; foreach ($order->items as $item) { $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice; } return $invoiceData; }However, it always returns an error with the message “invoice creation is not allowed.” I hope someone can help me solve this problem.
-
Hi,
We reviewed the invoice creation flow, and the "Order invoice creation is not allowed." error is expected system behavior.
This message is triggered when the following condition is met:
if (! $order->canInvoice()) { session()->flash('error', trans('admin::app.sales.invoices.create.creation-error')); return redirect()->back(); }The
canInvoice()method returnsfalsein the following scenarios:- The order status is Closed or Fraud.
- All order items have already been fully invoiced.
- All remaining order items have been canceled, leaving no quantity available for invoicing.
In other words, the system prevents invoice creation when there is no invoiceable quantity remaining or when the order is no longer eligible for invoicing.
We also reviewed the
sendInvoice()implementation currently being used. Since it follows the same validation flow and invokescanInvoice()before creating the invoice, this behavior is expected and is not caused by the custom implementation.To determine the exact reason for the affected order, please verify the following:
- The current order status (ensure it is not Closed or Fraud).
- The ordered, invoiced, and canceled quantities for each order item.
- Whether an invoice has already been created for the order.
Once these details are verified, the exact cause of the issue can be identified.
Thank you!
Best Regards,
Team Bagisto