1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00

Payment Terms via API (#1808)

Added the ability to get payment_terms, payment_term and create a new payment_term via the API.
This commit is contained in:
David Bomba 2018-01-14 23:21:10 +11:00 committed by GitHub
parent d76fa94d66
commit 9d1cb37835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 258 additions and 1 deletions

View File

@ -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',

View File

@ -0,0 +1,160 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreatePaymentTermAPIRequest;
use App\Http\Requests\PaymentTermRequest;
use App\Http\Requests\UpdatePaymentTermRequest;
use App\Libraries\Utils;
use App\Models\PaymentTerm;
use App\Ninja\Repositories\PaymentTermRepository;
use Illuminate\Support\Facades\Input;
class PaymentTermApiController extends BaseAPIController
{
/**
* @var PaymentTermRepository
*/
protected $paymentTermRepo;
protected $entityType = ENTITY_PAYMENT_TERM;
/**
* PaymentTermApiController constructor.
*
* @param PaymentTermRepository $paymentTermRepo
*/
public function __construct(PaymentTermRepository $paymentTermRepo)
{
parent::__construct();
$this->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);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Http\Requests;
use App\Models\Invoice;
class CreatePaymentTermAPIRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->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;
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Http\Requests;
class PaymentTermRequest extends EntityRequest
{
protected $entityType = ENTITY_PAYMENT_TERM;
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Http\Requests;
class UpdatePaymentTermRequest extends EntityRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [];
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Ninja\Transformers;
use App\Models\PaymentTerm;
/**
* @SWG\Definition(definition="PaymentTerm", required={"payment_term_id"}, @SWG\Xml(name="PaymentTerm"))
*/
class PaymentTermTransformer extends EntityTransformer
{
/**
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
* @SWG\Property(property="num_days", type="number", format="integer", example=10, readOnly=true)
* @SWG\Property(property="name", type="string", example="Net 7")
*/
public function __construct($account = null, $serializer = null, $paymentTerm = null)
{
parent::__construct($account, $serializer);
$this->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,
]);
}
}

View File

@ -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');