2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-02-03 11:06:49 +01:00
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
2015-11-27 13:55:28 +01:00
|
|
|
use Auth;
|
2016-02-07 11:24:21 +01:00
|
|
|
use Illuminate\Http\Request;
|
2015-07-05 10:01:16 +02:00
|
|
|
use Input;
|
2015-03-17 02:30:56 +01:00
|
|
|
use Utils;
|
2015-04-08 20:19:58 +02:00
|
|
|
use Response;
|
|
|
|
use App\Models\Payment;
|
2015-07-05 10:01:16 +02:00
|
|
|
use App\Models\Invoice;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-11-27 13:55:28 +01:00
|
|
|
use App\Http\Controllers\BaseAPIController;
|
|
|
|
use App\Ninja\Transformers\PaymentTransformer;
|
2016-02-03 11:06:49 +01:00
|
|
|
use App\Ninja\Transformers\InvoiceTransformer;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
class PaymentApiController extends BaseAPIController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
protected $paymentRepo;
|
|
|
|
|
2016-02-03 11:06:49 +01:00
|
|
|
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-11-27 13:55:28 +01:00
|
|
|
parent::__construct();
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$this->paymentRepo = $paymentRepo;
|
2016-02-03 11:06:49 +01:00
|
|
|
$this->contactMailer = $contactMailer;
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/payments",
|
|
|
|
* tags={"payment"},
|
|
|
|
* summary="List of payments",
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A list with payments",
|
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-11-27 13:55:28 +01:00
|
|
|
public function index()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-11-27 13:55:28 +01:00
|
|
|
$paginator = Payment::scope();
|
2015-07-05 10:01:16 +02:00
|
|
|
$payments = Payment::scope()
|
2016-02-07 12:01:39 +01:00
|
|
|
->with('client.contacts', 'invitation', 'user', 'invoice')->withTrashed();
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
if ($clientPublicId = Input::get('client_id')) {
|
|
|
|
$filter = function($query) use ($clientPublicId) {
|
2015-09-07 11:07:55 +02:00
|
|
|
$query->where('public_id', '=', $clientPublicId);
|
2015-11-27 13:55:28 +01:00
|
|
|
};
|
|
|
|
$payments->whereHas('client', $filter);
|
|
|
|
$paginator->whereHas('client', $filter);
|
2015-09-07 11:07:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
$payments = $payments->orderBy('created_at', 'desc')->paginate();
|
|
|
|
$paginator = $paginator->paginate();
|
|
|
|
$transformer = new PaymentTransformer(Auth::user()->account, Input::get('serializer'));
|
2015-07-02 22:21:29 +02:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
$data = $this->createCollection($payments, $transformer, 'payments', $paginator);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
return $this->response($data);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:42:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Put(
|
|
|
|
* path="/payments/{payment_id",
|
|
|
|
* summary="Update a payment",
|
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
|
|
|
* name="body",
|
|
|
|
* @SWG\Schema(ref="#/definitions/Payment")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Update payment",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2016-02-07 11:24:21 +01:00
|
|
|
|
|
|
|
public function update(Request $request, $publicId)
|
2016-02-06 04:42:21 +01:00
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
$data['public_id'] = $publicId;
|
|
|
|
$error = false;
|
2016-02-07 11:24:21 +01:00
|
|
|
|
|
|
|
if ($request->action == ACTION_ARCHIVE) {
|
2016-02-07 12:01:39 +01:00
|
|
|
$payment = Payment::scope($publicId)->withTrashed()->firstOrFail();
|
2016-02-07 11:24:21 +01:00
|
|
|
$this->paymentRepo->archive($payment);
|
|
|
|
|
2016-02-07 12:30:36 +01:00
|
|
|
$invoice = Invoice::scope($data['invoice_id'])->with('client')->with(['payments' => function($query) {
|
|
|
|
$query->withTrashed();
|
|
|
|
}])->first();
|
2016-02-07 11:24:21 +01:00
|
|
|
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($invoice, $transformer, 'invoice');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:01:39 +01:00
|
|
|
$payment = $this->paymentRepo->save($data);
|
2016-02-07 11:24:21 +01:00
|
|
|
|
2016-02-06 04:42:21 +01:00
|
|
|
if ($error) {
|
|
|
|
return $error;
|
|
|
|
}
|
2016-02-07 11:24:21 +01:00
|
|
|
|
2016-02-07 12:30:36 +01:00
|
|
|
$invoice = Invoice::scope($data['invoice_id'])->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
|
|
|
|
$query->withTrashed();
|
|
|
|
}])->withTrashed()->first();
|
2016-02-06 04:42:21 +01:00
|
|
|
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($invoice, $transformer, 'invoice');
|
|
|
|
return $this->response($data);
|
|
|
|
}
|
|
|
|
|
2016-02-07 09:30:47 +01:00
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Post(
|
|
|
|
* path="/payments",
|
|
|
|
* summary="Create a payment",
|
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
|
|
|
* name="body",
|
|
|
|
* @SWG\Schema(ref="#/definitions/Payment")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New payment",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-07-05 10:01:16 +02:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
$error = false;
|
|
|
|
|
|
|
|
if (isset($data['invoice_id'])) {
|
|
|
|
$invoice = Invoice::scope($data['invoice_id'])->with('client')->first();
|
|
|
|
|
|
|
|
if ($invoice) {
|
2015-12-07 22:41:48 +01:00
|
|
|
$data['invoice_id'] = $invoice->id;
|
|
|
|
$data['client_id'] = $invoice->client->id;
|
2015-07-05 10:01:16 +02:00
|
|
|
} else {
|
|
|
|
$error = trans('validation.not_in', ['attribute' => 'invoice_id']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error = trans('validation.not_in', ['attribute' => 'invoice_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['transaction_reference'])) {
|
|
|
|
$data['transaction_reference'] = '';
|
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
if ($error) {
|
|
|
|
return $error;
|
2015-07-05 10:01:16 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
$payment = $this->paymentRepo->save($data);
|
|
|
|
|
2016-02-03 11:06:49 +01:00
|
|
|
if (Input::get('email_receipt')) {
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:30:36 +01:00
|
|
|
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
|
|
|
|
$query->withTrashed();
|
|
|
|
}])->first();
|
2016-02-03 11:06:49 +01:00
|
|
|
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($invoice, $transformer, 'invoice');
|
2015-11-27 13:55:28 +01:00
|
|
|
|
|
|
|
return $this->response($data);
|
2015-07-05 10:01:16 +02:00
|
|
|
}
|
2016-02-07 11:24:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
2016-02-07 12:01:39 +01:00
|
|
|
|
2016-02-07 11:24:21 +01:00
|
|
|
$payment = Payment::scope($publicId)->withTrashed()->first();
|
2016-02-07 12:01:39 +01:00
|
|
|
$invoiceId = $payment->invoice->public_id;
|
2016-02-07 11:24:21 +01:00
|
|
|
|
|
|
|
$this->paymentRepo->delete($payment);
|
|
|
|
|
2016-02-07 12:30:36 +01:00
|
|
|
$invoice = Invoice::scope($invoiceId)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) {
|
|
|
|
$query->withTrashed();
|
|
|
|
}])->first();
|
2016-02-07 12:01:39 +01:00
|
|
|
|
2016-02-07 11:24:21 +01:00
|
|
|
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($invoice, $transformer, 'invoice');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|