Delete a payment from the API

This commit is contained in:
David Bomba 2016-02-07 21:24:21 +11:00
parent b3346011bc
commit 42f3cffca8
3 changed files with 62 additions and 13 deletions

View File

@ -51,8 +51,8 @@ class ClientApiController extends BaseAPIController
public function index() public function index()
{ {
$clients = Client::scope() $clients = Client::scope()
->with($this->getIncluded()) ->with($this->getIncluded())
->orderBy('created_at', 'desc')->withTrashed(); ->orderBy('created_at', 'desc')->withTrashed();
// Filter by email // Filter by email
if (Input::has('email')) { if (Input::has('email')) {
@ -100,8 +100,8 @@ class ClientApiController extends BaseAPIController
$client = $this->clientRepo->save($request->input()); $client = $this->clientRepo->save($request->input());
$client = Client::scope($client->public_id) $client = Client::scope($client->public_id)
->with('country', 'contacts', 'industry', 'size', 'currency') ->with('country', 'contacts', 'industry', 'size', 'currency')
->first(); ->first();
$transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($client, $transformer, ENTITY_CLIENT); $data = $this->createItem($client, $transformer, ENTITY_CLIENT);
@ -156,4 +156,7 @@ class ClientApiController extends BaseAPIController
return $this->response($data); return $this->response($data);
} }
} }

View File

@ -2,7 +2,7 @@
use App\Ninja\Mailers\ContactMailer; use App\Ninja\Mailers\ContactMailer;
use Auth; use Auth;
use Illuminate\Support\Facades\Log; use Illuminate\Http\Request;
use Input; use Input;
use Utils; use Utils;
use Response; use Response;
@ -87,18 +87,31 @@ class PaymentApiController extends BaseAPIController
* ) * )
* ) * )
*/ */
public function update($publicId)
public function update(Request $request, $publicId)
{ {
$data = Input::all(); $data = Input::all();
$data['public_id'] = $publicId; $data['public_id'] = $publicId;
$invoice = Invoice::scope($data['invoice_id'])->with('client')->first();
$error = false; $error = false;
$payment = $this->paymentRepo->save($data);
if ($request->action == ACTION_ARCHIVE) {
$payment = Payment::scope($publicId)->firstOrFail();
$this->paymentRepo->archive($payment);
$invoice = Invoice::scope($data['invoice_id'])->with('client')->first();
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($invoice, $transformer, 'invoice');
return $this->response($data);
}
$this->paymentRepo->save($data);
if ($error) { if ($error) {
return $error; return $error;
} }
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
$invoice = Invoice::scope($data['invoice_id'])->with('client', 'invoice_items', 'invitations','payments')->first();
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($invoice, $transformer, 'invoice'); $data = $this->createItem($invoice, $transformer, 'invoice');
return $this->response($data); return $this->response($data);
@ -165,11 +178,44 @@ class PaymentApiController extends BaseAPIController
*/ */
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first(); $invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($invoice, $transformer, 'invoice'); $data = $this->createItem($invoice, $transformer, 'invoice');
return $this->response($data); return $this->response($data);
} }
/**
* @SWG\Delete(
* path="/payments/{payment_id}",
* summary="Delete a payment",
* tags={"payment"},
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Payment")
* ),
* @SWG\Response(
* response=200,
* description="Delete payment",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy($publicId)
{
$payment = Payment::scope($publicId)->withTrashed()->first();
$invoice = Invoice::scope($payment->invoice->public_id)->first();
$this->paymentRepo->delete($payment);
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($invoice, $transformer, 'invoice');
return $this->response($data);
}
} }

View File

@ -179,7 +179,7 @@ class Invoice extends EntityModel implements BalanceAffecting
public function payments() public function payments()
{ {
return $this->hasMany('App\Models\Payment', 'invoice_id', 'id'); return $this->hasMany('App\Models\Payment', 'invoice_id', 'id')->withTrashed();
} }
public function recurring_invoice() public function recurring_invoice()