mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Add credit support to the API
This commit is contained in:
parent
568f9dc10a
commit
7cfcaa72ec
@ -75,7 +75,7 @@ class BaseAPIController extends Controller
|
||||
$action = $request->action;
|
||||
|
||||
if (! in_array($action, ['archive', 'delete', 'restore'])) {
|
||||
return $this->errorResponse('Action is not supported');
|
||||
return $this->errorResponse("Action [$action] is not supported");
|
||||
}
|
||||
|
||||
$repo = Utils::toCamelCase($this->entityType) . 'Repo';
|
||||
|
185
app/Http/Controllers/CreditApiController.php
Normal file
185
app/Http/Controllers/CreditApiController.php
Normal file
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\CreditRequest;
|
||||
use App\Http\Requests\CreateCreditRequest;
|
||||
use App\Http\Requests\UpdateCreditRequest;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Credit;
|
||||
use App\Ninja\Repositories\CreditRepository;
|
||||
use Input;
|
||||
use Response;
|
||||
|
||||
class CreditApiController extends BaseAPIController
|
||||
{
|
||||
protected $creditRepo;
|
||||
|
||||
protected $entityType = ENTITY_CREDIT;
|
||||
|
||||
public function __construct(CreditRepository $creditRepo)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->creditRepo = $creditRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Get(
|
||||
* path="/credits",
|
||||
* summary="List credits",
|
||||
* operationId="listCredits",
|
||||
* tags={"credit"},
|
||||
* @SWG\Response(
|
||||
* response=200,
|
||||
* description="A list of credits",
|
||||
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Credit"))
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response="default",
|
||||
* description="an ""unexpected"" error"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$credits = Credit::scope()
|
||||
->withTrashed()
|
||||
->with(['invoice'])
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
return $this->listResponse($credits);
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Get(
|
||||
* path="/credits/{credit_id}",
|
||||
* summary="Retrieve a credit",
|
||||
* operationId="getCredit",
|
||||
* tags={"credit"},
|
||||
* @SWG\Parameter(
|
||||
* in="path",
|
||||
* name="credit_id",
|
||||
* type="integer",
|
||||
* required=true
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response=200,
|
||||
* description="A single credit",
|
||||
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Credit"))
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response="default",
|
||||
* description="an ""unexpected"" error"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function show(CreditRequest $request)
|
||||
{
|
||||
return $this->itemResponse($request->entity());
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Post(
|
||||
* path="/credits",
|
||||
* summary="Create a credit",
|
||||
* operationId="createCredit",
|
||||
* tags={"credit"},
|
||||
* @SWG\Parameter(
|
||||
* in="body",
|
||||
* name="credit",
|
||||
* @SWG\Schema(ref="#/definitions/Credit")
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response=200,
|
||||
* description="New credit",
|
||||
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Credit"))
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response="default",
|
||||
* description="an ""unexpected"" error"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function store(CreateCreditRequest $request)
|
||||
{
|
||||
$credit = $this->creditRepo->save($request->input());
|
||||
|
||||
return $this->itemResponse($credit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Put(
|
||||
* path="/credits/{credit_id}",
|
||||
* summary="Update a credit",
|
||||
* operationId="updateCredit",
|
||||
* tags={"credit"},
|
||||
* @SWG\Parameter(
|
||||
* in="path",
|
||||
* name="credit_id",
|
||||
* type="integer",
|
||||
* required=true
|
||||
* ),
|
||||
* @SWG\Parameter(
|
||||
* in="body",
|
||||
* name="credit",
|
||||
* @SWG\Schema(ref="#/definitions/Credit")
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response=200,
|
||||
* description="Updated credit",
|
||||
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Credit"))
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response="default",
|
||||
* description="an ""unexpected"" error"
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @param mixed $publicId
|
||||
*/
|
||||
public function update(UpdateCreditRequest $request, $publicId)
|
||||
{
|
||||
if ($request->action) {
|
||||
return $this->handleAction($request);
|
||||
}
|
||||
|
||||
$data = $request->input();
|
||||
$data['public_id'] = $publicId;
|
||||
$credit = $this->creditRepo->save($data, $request->entity());
|
||||
|
||||
return $this->itemResponse($credit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Delete(
|
||||
* path="/credits/{credit_id}",
|
||||
* summary="Delete a credit",
|
||||
* operationId="deleteCredit",
|
||||
* tags={"credit"},
|
||||
* @SWG\Parameter(
|
||||
* in="path",
|
||||
* name="credit_id",
|
||||
* type="integer",
|
||||
* required=true
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response=200,
|
||||
* description="Deleted credit",
|
||||
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Credit"))
|
||||
* ),
|
||||
* @SWG\Response(
|
||||
* response="default",
|
||||
* description="an ""unexpected"" error"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function destroy(UpdateCreditRequest $request)
|
||||
{
|
||||
$credit = $request->entity();
|
||||
|
||||
$this->creditRepo->delete($credit);
|
||||
|
||||
return $this->itemResponse($credit);
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ class CreateCreditRequest extends CreditRequest
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'client' => 'required',
|
||||
'client_id' => 'required',
|
||||
'amount' => 'required|positive',
|
||||
];
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ class UpdateCreditRequest extends CreditRequest
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'amount' => 'required|positive',
|
||||
'amount' => 'positive',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -329,6 +329,7 @@ Route::group(['middleware' => ['lookup:api', 'api'], 'prefix' => 'api/v1'], func
|
||||
Route::resource('invoices', 'InvoiceApiController');
|
||||
Route::resource('payments', 'PaymentApiController');
|
||||
Route::resource('tasks', 'TaskApiController');
|
||||
Route::resource('credits', 'CreditApiController');
|
||||
Route::post('hooks', 'IntegrationController@subscribe');
|
||||
Route::post('email_invoice', 'InvoiceApiController@emailInvoice');
|
||||
Route::get('user_accounts', 'AccountApiController@getUserAccounts');
|
||||
|
@ -102,21 +102,29 @@ class CreditRepository extends BaseRepository
|
||||
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
|
||||
|
||||
if ($credit) {
|
||||
$credit->balance = Utils::parseFloat($input['balance']);
|
||||
// do nothing
|
||||
} elseif ($publicId) {
|
||||
$credit = Credit::scope($publicId)->firstOrFail();
|
||||
$credit->balance = Utils::parseFloat($input['balance']);
|
||||
\Log::warning('Entity not set in credit repo save');
|
||||
} else {
|
||||
$credit = Credit::createNew();
|
||||
$credit->balance = Utils::parseFloat($input['amount']);
|
||||
$credit->client_id = Client::getPrivateId($input['client']);
|
||||
$credit->client_id = Client::getPrivateId($input['client_id']);
|
||||
$credit->credit_date = date('Y-m-d');
|
||||
}
|
||||
|
||||
$credit->fill($input);
|
||||
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
|
||||
$credit->amount = Utils::parseFloat($input['amount']);
|
||||
$credit->private_notes = trim($input['private_notes']);
|
||||
|
||||
if (isset($input['credit_date'])) {
|
||||
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
|
||||
}
|
||||
if (isset($input['amount'])) {
|
||||
$credit->amount = Utils::parseFloat($input['amount']);
|
||||
}
|
||||
if (isset($input['balance'])) {
|
||||
$credit->balance = Utils::parseFloat($input['balance']);
|
||||
}
|
||||
|
||||
$credit->save();
|
||||
|
||||
return $credit;
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
|
||||
{!! Former::open($url)->addClass('col-md-10 col-md-offset-1 warn-on-exit')->method($method)->rules(array(
|
||||
'client' => 'required',
|
||||
'client_id' => 'required',
|
||||
'amount' => 'required',
|
||||
)) !!}
|
||||
|
||||
@ -22,9 +22,9 @@
|
||||
<div class="panel-body">
|
||||
|
||||
@if ($credit)
|
||||
{!! Former::plaintext()->label('client')->value($client->getDisplayName()) !!}
|
||||
{!! Former::plaintext()->label('client')->value($client->present()->link) !!}
|
||||
@else
|
||||
{!! Former::select('client')->addOption('', '')->addGroupClass('client-select') !!}
|
||||
{!! Former::select('client_id')->addOption('', '')->addGroupClass('client-select') !!}
|
||||
@endif
|
||||
|
||||
{!! Former::text('amount') !!}
|
||||
@ -63,7 +63,7 @@
|
||||
$(function() {
|
||||
|
||||
@if ( ! $credit)
|
||||
var $clientSelect = $('select#client');
|
||||
var $clientSelect = $('select#client_id');
|
||||
for (var i=0; i<clients.length; i++) {
|
||||
var client = clients[i];
|
||||
var clientName = getClientDisplayName(client);
|
||||
|
@ -82,6 +82,12 @@ class APICest
|
||||
$this->createEntity('vendor', $data);
|
||||
$this->listEntities('vendors');
|
||||
|
||||
$data = new stdClass;
|
||||
$data->client_id = $clientId
|
||||
$data->amount = 1;
|
||||
$this->createEntity('credit', $data);
|
||||
$this->listEntities('credits');
|
||||
|
||||
$this->listEntities('accounts');
|
||||
$this->listEntities('dashboard');
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user