2015-09-10 19:50:09 +02:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Models\Invoice;
|
2015-11-05 23:37:04 +01:00
|
|
|
use Utils;
|
2016-03-16 00:08:00 +01:00
|
|
|
use Auth;
|
2016-12-22 18:42:21 +01:00
|
|
|
use Exception;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Models\Account;
|
2016-03-16 03:07:11 +01:00
|
|
|
use App\Models\Client;
|
2016-05-26 21:22:09 +02:00
|
|
|
use App\Models\Activity;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2016-05-23 18:52:20 +02:00
|
|
|
use App\Ninja\Datatables\PaymentDatatable;
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class PaymentService extends BaseService
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* PaymentService constructor.
|
|
|
|
*
|
|
|
|
* @param PaymentRepository $paymentRepo
|
|
|
|
* @param AccountRepository $accountRepo
|
|
|
|
* @param DatatableService $datatableService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
PaymentRepository $paymentRepo,
|
|
|
|
AccountRepository $accountRepo,
|
|
|
|
DatatableService $datatableService
|
|
|
|
)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$this->datatableService = $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
$this->paymentRepo = $paymentRepo;
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return PaymentRepository
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->paymentRepo;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param Invoice $invoice
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function autoBillInvoice(Invoice $invoice)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/** @var \App\Models\Client $client */
|
2015-09-10 19:50:09 +02:00
|
|
|
$client = $invoice->client;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\Account $account */
|
2016-06-20 16:14:43 +02:00
|
|
|
$account = $client->account;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\Invitation $invitation */
|
2015-09-10 19:50:09 +02:00
|
|
|
$invitation = $invoice->invitations->first();
|
2016-05-15 04:38:09 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
if ( ! $invitation) {
|
2016-05-15 04:38:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-19 19:12:28 +02:00
|
|
|
if ($credits = $client->credits->sum('balance')) {
|
|
|
|
$balance = $invoice->balance;
|
|
|
|
$amount = min($credits, $balance);
|
|
|
|
$data = [
|
|
|
|
'payment_type_id' => PAYMENT_TYPE_CREDIT,
|
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'amount' => $amount,
|
|
|
|
];
|
|
|
|
$payment = $this->paymentRepo->save($data);
|
|
|
|
if ($amount == $balance) {
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_TOKEN);
|
2016-07-19 19:15:03 +02:00
|
|
|
|
|
|
|
if ( ! $paymentDriver) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$customer = $paymentDriver->customer();
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
if ( ! $customer) {
|
2015-09-10 19:50:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$paymentMethod = $customer->default_payment_method;
|
|
|
|
|
|
|
|
if ($paymentMethod->requiresDelayedAutoBill()) {
|
2016-05-26 21:22:09 +02:00
|
|
|
$invoiceDate = \DateTime::createFromFormat('Y-m-d', $invoice->invoice_date);
|
2016-05-25 04:49:06 +02:00
|
|
|
$minDueDate = clone $invoiceDate;
|
|
|
|
$minDueDate->modify('+10 days');
|
|
|
|
|
2016-05-26 21:22:09 +02:00
|
|
|
if (date_create() < $minDueDate) {
|
2016-05-25 04:49:06 +02:00
|
|
|
// Can't auto bill now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:22:09 +02:00
|
|
|
if ($invoice->partial > 0) {
|
|
|
|
// The amount would be different than the amount in the email
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$firstUpdate = Activity::where('invoice_id', '=', $invoice->id)
|
2016-05-25 04:49:06 +02:00
|
|
|
->where('activity_type_id', '=', ACTIVITY_TYPE_UPDATE_INVOICE)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($firstUpdate) {
|
|
|
|
$backup = json_decode($firstUpdate->json_backup);
|
|
|
|
|
|
|
|
if ($backup->balance != $invoice->balance || $backup->due_date != $invoice->due_date) {
|
|
|
|
// It's changed since we sent the email can't bill now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:22:09 +02:00
|
|
|
if ($invoice->payments->count()) {
|
2016-05-25 04:49:06 +02:00
|
|
|
// ACH requirements are strict; don't auto bill this
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:42:21 +01:00
|
|
|
try {
|
|
|
|
return $paymentDriver->completeOnsitePurchase(false, $paymentMethod);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-25 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
public function getDatatable($clientPublicId, $search)
|
|
|
|
{
|
2016-11-24 10:22:37 +01:00
|
|
|
$datatable = new PaymentDatatable(true, $clientPublicId);
|
2015-11-05 23:37:04 +01:00
|
|
|
$query = $this->paymentRepo->find($clientPublicId, $search);
|
|
|
|
|
2016-03-16 00:08:00 +01:00
|
|
|
if(!Utils::hasPermission('view_all')){
|
|
|
|
$query->where('payments.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
2016-11-24 10:22:37 +01:00
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
return $this->datatableService->createDatatable($datatable, $query);
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
public function bulk($ids, $action, $params = [])
|
2016-04-23 22:40:19 +02:00
|
|
|
{
|
|
|
|
if ($action == 'refund') {
|
|
|
|
if ( ! $ids ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2016-04-23 22:40:19 +02:00
|
|
|
$payments = $this->getRepo()->findByPublicIdsWithTrashed($ids);
|
2016-05-06 23:05:42 +02:00
|
|
|
$successful = 0;
|
2015-11-05 23:37:04 +01:00
|
|
|
|
2016-04-23 22:40:19 +02:00
|
|
|
foreach ($payments as $payment) {
|
2016-06-20 16:14:43 +02:00
|
|
|
if (Auth::user()->can('edit', $payment)) {
|
2016-05-06 23:05:42 +02:00
|
|
|
$amount = !empty($params['amount']) ? floatval($params['amount']) : null;
|
2016-10-06 07:44:19 +02:00
|
|
|
if ($accountGateway = $payment->account_gateway) {
|
|
|
|
$paymentDriver = $accountGateway->paymentDriver();
|
|
|
|
if ($paymentDriver->refundPayment($payment, $amount)) {
|
|
|
|
$successful++;
|
|
|
|
}
|
2016-05-17 17:32:17 +02:00
|
|
|
}
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:05:42 +02:00
|
|
|
return $successful;
|
2016-04-23 22:40:19 +02:00
|
|
|
} else {
|
|
|
|
return parent::bulk($ids, $action);
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|