setCompany($request->header('company')) ->setCustomer($request->userId); // Invoices and credit notes live in one table, so each is pinned to its // own row type: the preview must never count the other kind's rows. switch ($request->key) { case 'invoice': $serial->setModel($invoice) ->setSequenceScope(['type' => Invoice::TYPE_INVOICE]); break; case 'credit_note': $serial->setModel($invoice) ->setSettingKey('credit_note_number_format') ->setSequenceScope(['type' => Invoice::TYPE_CREDIT_NOTE]); break; case 'estimate': $serial->setModel($estimate); break; case 'payment': $serial->setModel($payment); break; default: return response()->json([ 'success' => false, ]); } try { $nextNumber = $serial->setModelObject($request->model_id) ->getNextNumber($request->input('format')); } catch (\Exception $exception) { return response()->json([ 'success' => false, 'message' => $exception->getMessage(), ]); } return response()->json([ 'success' => true, 'nextNumber' => $nextNumber, ]); } /** * List the tokens a submitted format string is made of. */ public function placeholders(Request $request): JsonResponse { $format = $request->input('format'); return response()->json([ 'success' => true, 'placeholders' => $format ? SerialNumberService::getPlaceholders($format) : [], ]); } }