Bagisto Forum

    Bagisto

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Message Error 'invoice creation is not allowed'

    General Discussion
    2
    2
    1375
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      beabay last edited by beabay

      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.

      1 Reply Last reply Reply Quote 0
      • S
        shivendra-webkul last edited by

        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 returns false in 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 invokes canInvoice() 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

        1 Reply Last reply Reply Quote 0
        • First post
          Last post