diff --git a/app/Constants.php b/app/Constants.php index b49671d0ecb0..077775db8994 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -605,7 +605,6 @@ if (! defined('APP_NAME')) { 'dateFormats' => 'App\Models\DateFormat', 'datetimeFormats' => 'App\Models\DatetimeFormat', 'languages' => 'App\Models\Language', - 'paymentTerms' => 'App\Models\PaymentTerm', 'paymentTypes' => 'App\Models\PaymentType', 'countries' => 'App\Models\Country', 'invoiceDesigns' => 'App\Models\InvoiceDesign', diff --git a/app/Http/Controllers/PaymentTermApiController.php b/app/Http/Controllers/PaymentTermApiController.php new file mode 100644 index 000000000000..ef11a64e19f5 --- /dev/null +++ b/app/Http/Controllers/PaymentTermApiController.php @@ -0,0 +1,160 @@ +paymentTermRepo = $paymentTermRepo; + } + + /** + * @SWG\Get( + * path="/paymentTerms", + * summary="List payment terms", + * operationId="listPaymentTerms", + * tags={"payment terms"}, + * @SWG\Response( + * response=200, + * description="A list of payment terms", + * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/PaymentTerms")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + + public function index() + { + + $paymentTerms = PaymentTerm::scope() + ->orWhere('account_id',0) + ->orderBy('num_days', 'asc'); + + return $this->listResponse($paymentTerms); + } + + /** + * @SWG\Get( + * path="/paymentTerms/{payment_term_id}", + * summary="Retrieve a payment term", + * operationId="getPaymentTermId", + * tags={"payment term"}, + * @SWG\Parameter( + * in="path", + * name="payment_term_id", + * type="integer", + * required=true + * ), + * @SWG\Response( + * response=200, + * description="A single payment term", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/PaymentTerms")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + + public function show(PaymentTermRequest $request) + { + return $this->itemResponse($request->entity()); + } + + + /** + * @SWG\Post( + * path="/paymentTerms", + * summary="Create a payment Term", + * operationId="createPaymentTerm", + * tags={"payment term"}, + * @SWG\Parameter( + * in="body", + * name="payment term", + * @SWG\Schema(ref="#/definitions/PaymentTerm") + * ), + * @SWG\Response( + * response=200, + * description="New payment Term", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/PaymentTerm")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function store(CreatePaymentTermAPIRequest $request) + { + + $paymentTerm = PaymentTerm::createNew(); + + $paymentTerm->num_days = Utils::parseInt(Input::get('num_days')); + $paymentTerm->name = 'Net ' . $paymentTerm->num_days; + $paymentTerm->save(); + + return $this->itemResponse($paymentTerm); + } + + /** + * @SWG\Delete( + * path="/paymentTerm/{num_days}", + * summary="Delete a payment term", + * operationId="deletePaymentTerm", + * tags={"payment term"}, + * @SWG\Parameter( + * in="path", + * name="num_days", + * type="integer", + * required=true + * ), + * @SWG\Response( + * response=200, + * description="Deleted payment Term", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/PaymentTerm")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function destroy($numDays) + { + + $paymentTerm = PaymentTerm::where('num_days', $numDays)->first(); + + if(!$paymentTerm || $paymentTerm->account_id == 0) + return $this->errorResponse(['message'=>'Cannot delete a default or non existent Payment Term'], 400); + + $this->paymentTermRepo->archive($paymentTerm); + + return $this->itemResponse($paymentTerm); + } +} diff --git a/app/Http/Requests/CreatePaymentTermAPIRequest.php b/app/Http/Requests/CreatePaymentTermAPIRequest.php new file mode 100644 index 000000000000..44002ecf90ee --- /dev/null +++ b/app/Http/Requests/CreatePaymentTermAPIRequest.php @@ -0,0 +1,36 @@ +user()->can('create', ENTITY_PAYMENT_TERM); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + + public function rules() + { + + $rules = [ + 'num_days' => 'required|numeric|unique:payment_terms', + ]; + + + return $rules; + } +} diff --git a/app/Http/Requests/PaymentTermRequest.php b/app/Http/Requests/PaymentTermRequest.php new file mode 100644 index 000000000000..adb224eb4788 --- /dev/null +++ b/app/Http/Requests/PaymentTermRequest.php @@ -0,0 +1,8 @@ +paymentTerm = $paymentTerm; + } + + + public function transform(PaymentTerm $paymentTerm) + { + return array_merge($this->getDefaults($paymentTerm), [ + 'num_days' => (int) $paymentTerm->num_days, + 'name' => trans('texts.payment_terms_net') . ' ' . $paymentTerm->getNumDays(), + 'updated_at' => $this->getTimestamp($paymentTerm->updated_at), + 'archived_at' => $this->getTimestamp($paymentTerm->deleted_at), + 'is_default' => $paymentTerm->account_id == 0 ? 1 : 0, + ]); + } +} diff --git a/routes/api.php b/routes/api.php index ac10161238ff..daac865cabb9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -32,3 +32,4 @@ Route::resource('documents', 'DocumentAPIController'); Route::resource('vendors', 'VendorApiController'); Route::resource('expense_categories', 'ExpenseCategoryApiController'); Route::post('ios_subscription_status', 'AccountApiController@iosSubscriptionStatus'); +Route::resource('payment_terms', 'PaymentTermApiController');