2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Repositories;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-31 11:38:24 +02:00
|
|
|
use App\Models\Credit;
|
2016-07-21 14:35:23 +02:00
|
|
|
use App\Models\Invoice;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Payment;
|
|
|
|
use DB;
|
|
|
|
use Utils;
|
2017-03-31 13:32:41 +02:00
|
|
|
use Auth;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class PaymentRepository extends BaseRepository
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Payment';
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function find($clientPublicId = null, $filter = null)
|
|
|
|
{
|
2015-12-07 14:34:55 +01:00
|
|
|
$query = DB::table('payments')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'payments.account_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->join('clients', 'clients.id', '=', 'payments.client_id')
|
|
|
|
->join('invoices', 'invoices.id', '=', 'payments.invoice_id')
|
|
|
|
->join('contacts', 'contacts.client_id', '=', 'clients.id')
|
2016-04-23 22:40:19 +02:00
|
|
|
->join('payment_statuses', 'payment_statuses.id', '=', 'payments.payment_status_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
|
2015-06-04 22:53:58 +02:00
|
|
|
->leftJoin('account_gateways', 'account_gateways.id', '=', 'payments.account_gateway_id')
|
|
|
|
->leftJoin('gateways', 'gateways.id', '=', 'account_gateways.gateway_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->where('payments.account_id', '=', \Auth::user()->account_id)
|
|
|
|
->where('contacts.is_primary', '=', true)
|
|
|
|
->where('contacts.deleted_at', '=', null)
|
2015-11-18 17:08:37 +01:00
|
|
|
->where('invoices.is_deleted', '=', false)
|
2015-10-30 13:56:25 +01:00
|
|
|
->select('payments.public_id',
|
2015-12-07 14:34:55 +01:00
|
|
|
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
|
2015-10-30 13:56:25 +01:00
|
|
|
'payments.transaction_reference',
|
2016-04-27 10:49:54 +02:00
|
|
|
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
|
2015-10-30 13:56:25 +01:00
|
|
|
'clients.public_id as client_public_id',
|
2016-03-16 03:07:11 +01:00
|
|
|
'clients.user_id as client_user_id',
|
2015-10-30 13:56:25 +01:00
|
|
|
'payments.amount',
|
2017-03-28 11:40:53 +02:00
|
|
|
DB::raw("CONCAT(payments.payment_date, payments.created_at) as date"),
|
2015-10-30 13:56:25 +01:00
|
|
|
'payments.payment_date',
|
2016-04-23 22:40:19 +02:00
|
|
|
'payments.payment_status_id',
|
|
|
|
'payments.payment_type_id',
|
2016-12-15 14:20:36 +01:00
|
|
|
'payments.payment_type_id as source',
|
2015-10-30 13:56:25 +01:00
|
|
|
'invoices.public_id as invoice_public_id',
|
2016-03-16 03:07:11 +01:00
|
|
|
'invoices.user_id as invoice_user_id',
|
2015-10-30 13:56:25 +01:00
|
|
|
'invoices.invoice_number',
|
2016-12-15 14:20:36 +01:00
|
|
|
'invoices.invoice_number as invoice_name',
|
2015-10-30 13:56:25 +01:00
|
|
|
'contacts.first_name',
|
|
|
|
'contacts.last_name',
|
|
|
|
'contacts.email',
|
2016-12-15 14:20:36 +01:00
|
|
|
'payment_types.name as method',
|
2015-10-30 13:56:25 +01:00
|
|
|
'payment_types.name as payment_type',
|
|
|
|
'payments.account_gateway_id',
|
|
|
|
'payments.deleted_at',
|
|
|
|
'payments.is_deleted',
|
2016-03-16 00:08:00 +01:00
|
|
|
'payments.user_id',
|
2016-04-23 22:40:19 +02:00
|
|
|
'payments.refunded',
|
2016-04-24 04:10:51 +02:00
|
|
|
'payments.expiration',
|
|
|
|
'payments.last4',
|
2016-05-07 04:33:03 +02:00
|
|
|
'payments.email',
|
2016-04-29 23:50:21 +02:00
|
|
|
'payments.routing_number',
|
2016-05-25 21:04:58 +02:00
|
|
|
'payments.bank_name',
|
2017-10-14 19:55:37 +02:00
|
|
|
'payments.private_notes',
|
2015-10-30 13:56:25 +01:00
|
|
|
'invoices.is_deleted as invoice_is_deleted',
|
2016-04-23 22:40:19 +02:00
|
|
|
'gateways.name as gateway_name',
|
|
|
|
'gateways.id as gateway_id',
|
2016-12-25 22:43:57 +01:00
|
|
|
'payment_statuses.name as status'
|
2015-10-30 13:56:25 +01:00
|
|
|
);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-11-18 14:31:43 +01:00
|
|
|
$this->applyFilters($query, ENTITY_PAYMENT);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($clientPublicId) {
|
|
|
|
$query->where('clients.public_id', '=', $clientPublicId);
|
2016-11-27 13:20:58 +01:00
|
|
|
} else {
|
|
|
|
$query->whereNull('clients.deleted_at');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($filter) {
|
|
|
|
$query->where(function ($query) use ($filter) {
|
2016-04-24 15:34:48 +02:00
|
|
|
$query->where('clients.name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('invoices.invoice_number', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('payments.transaction_reference', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('gateways.name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('payment_types.name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('contacts.email', 'like', '%'.$filter.'%');
|
2015-03-16 22:45:25 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findForContact($contactId = null, $filter = null)
|
|
|
|
{
|
2015-12-07 14:34:55 +01:00
|
|
|
$query = DB::table('payments')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'payments.account_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->join('clients', 'clients.id', '=', 'payments.client_id')
|
|
|
|
->join('invoices', 'invoices.id', '=', 'payments.invoice_id')
|
|
|
|
->join('contacts', 'contacts.client_id', '=', 'clients.id')
|
2016-04-23 22:40:19 +02:00
|
|
|
->join('payment_statuses', 'payment_statuses.id', '=', 'payments.payment_status_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->leftJoin('invitations', function ($join) {
|
|
|
|
$join->on('invitations.invoice_id', '=', 'invoices.id')
|
|
|
|
->on('invitations.contact_id', '=', 'contacts.id');
|
|
|
|
})
|
|
|
|
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
|
|
|
|
->where('clients.is_deleted', '=', false)
|
|
|
|
->where('payments.is_deleted', '=', false)
|
|
|
|
->where('invitations.deleted_at', '=', null)
|
2016-11-22 20:17:32 +01:00
|
|
|
->where('invoices.is_deleted', '=', false)
|
2016-12-04 22:26:38 +01:00
|
|
|
->where('invoices.is_public', '=', true)
|
2015-03-16 22:45:25 +01:00
|
|
|
->where('invitations.contact_id', '=', $contactId)
|
2015-12-07 14:34:55 +01:00
|
|
|
->select(
|
|
|
|
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
|
|
|
|
'invitations.invitation_key',
|
|
|
|
'payments.public_id',
|
|
|
|
'payments.transaction_reference',
|
2016-04-27 10:49:54 +02:00
|
|
|
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
|
2015-12-07 14:34:55 +01:00
|
|
|
'clients.public_id as client_public_id',
|
|
|
|
'payments.amount',
|
|
|
|
'payments.payment_date',
|
2016-04-29 23:50:21 +02:00
|
|
|
'payments.payment_type_id',
|
2015-12-07 14:34:55 +01:00
|
|
|
'invoices.public_id as invoice_public_id',
|
|
|
|
'invoices.invoice_number',
|
|
|
|
'contacts.first_name',
|
|
|
|
'contacts.last_name',
|
|
|
|
'contacts.email',
|
|
|
|
'payment_types.name as payment_type',
|
2016-04-23 22:40:19 +02:00
|
|
|
'payments.account_gateway_id',
|
|
|
|
'payments.refunded',
|
2016-04-24 04:10:51 +02:00
|
|
|
'payments.expiration',
|
|
|
|
'payments.last4',
|
2016-05-07 04:33:03 +02:00
|
|
|
'payments.email',
|
2016-04-29 23:50:21 +02:00
|
|
|
'payments.routing_number',
|
2016-05-25 21:04:58 +02:00
|
|
|
'payments.bank_name',
|
2016-04-23 22:40:19 +02:00
|
|
|
'payments.payment_status_id',
|
2016-04-28 04:13:51 +02:00
|
|
|
'payment_statuses.name as payment_status_name'
|
2015-12-07 14:34:55 +01:00
|
|
|
);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($filter) {
|
|
|
|
$query->where(function ($query) use ($filter) {
|
|
|
|
$query->where('clients.name', 'like', '%'.$filter.'%');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public function save($input, $payment = null)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$publicId = isset($input['public_id']) ? $input['public_id'] : false;
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
if ($payment) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($publicId) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$payment = Payment::scope($publicId)->firstOrFail();
|
2016-10-10 10:40:04 +02:00
|
|
|
if (Utils::isNinjaDev()) {
|
|
|
|
\Log::warning('Entity not set in payment repo save');
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
$payment = Payment::createNew();
|
2017-03-31 13:32:41 +02:00
|
|
|
|
2017-04-13 09:25:49 +02:00
|
|
|
if (Auth::check() && Auth::user()->account->payment_type_id) {
|
2017-03-31 13:32:41 +02:00
|
|
|
$payment->payment_type_id = Auth::user()->account->payment_type_id;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 10:40:04 +02:00
|
|
|
if ($payment->is_deleted) {
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
2015-06-10 10:34:20 +02:00
|
|
|
$paymentTypeId = false;
|
|
|
|
if (isset($input['payment_type_id'])) {
|
|
|
|
$paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null;
|
|
|
|
$payment->payment_type_id = $paymentTypeId;
|
|
|
|
}
|
|
|
|
|
2015-07-05 10:01:16 +02:00
|
|
|
if (isset($input['payment_date_sql'])) {
|
|
|
|
$payment->payment_date = $input['payment_date_sql'];
|
|
|
|
} elseif (isset($input['payment_date'])) {
|
|
|
|
$payment->payment_date = Utils::toSqlDate($input['payment_date']);
|
|
|
|
} else {
|
|
|
|
$payment->payment_date = date('Y-m-d');
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2015-11-18 15:40:50 +01:00
|
|
|
if (isset($input['transaction_reference'])) {
|
|
|
|
$payment->transaction_reference = trim($input['transaction_reference']);
|
|
|
|
}
|
2017-05-16 15:47:20 +02:00
|
|
|
if (isset($input['private_notes'])) {
|
|
|
|
$payment->private_notes = trim($input['private_notes']);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $publicId) {
|
2015-12-07 22:41:48 +01:00
|
|
|
$clientId = $input['client_id'];
|
2015-03-16 22:45:25 +01:00
|
|
|
$amount = Utils::parseFloat($input['amount']);
|
|
|
|
|
|
|
|
if ($paymentTypeId == PAYMENT_TYPE_CREDIT) {
|
|
|
|
$credits = Credit::scope()->where('client_id', '=', $clientId)
|
|
|
|
->where('balance', '>', 0)->orderBy('created_at')->get();
|
|
|
|
|
2016-08-09 08:39:20 +02:00
|
|
|
$remaining = $amount;
|
2015-03-16 22:45:25 +01:00
|
|
|
foreach ($credits as $credit) {
|
2016-08-09 08:39:20 +02:00
|
|
|
$remaining -= $credit->apply($remaining);
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $remaining) {
|
2015-03-16 22:45:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 22:41:48 +01:00
|
|
|
$payment->invoice_id = $input['invoice_id'];
|
2015-03-16 22:45:25 +01:00
|
|
|
$payment->client_id = $clientId;
|
|
|
|
$payment->amount = $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
2015-10-30 13:56:25 +01:00
|
|
|
|
2016-07-14 23:20:16 +02:00
|
|
|
public function delete($payment)
|
2015-10-30 13:56:25 +01:00
|
|
|
{
|
|
|
|
if ($payment->invoice->is_deleted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::delete($payment);
|
|
|
|
}
|
|
|
|
|
2016-07-14 23:20:16 +02:00
|
|
|
public function restore($payment)
|
2015-10-30 13:56:25 +01:00
|
|
|
{
|
|
|
|
if ($payment->invoice->is_deleted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::restore($payment);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|