Bagisto Forum

    Bagisto

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

    Message Error 'invoice creation is not allowed'

    General Discussion
    1
    1
    131
    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
      • First post
        Last post