1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/Payment/PaymentService.php
David Bomba 43e57d0117
Fixes for self-update (#3514)
* minor fix for payment notifications

* styleci

* Limit Self updating to self hosters only
:

* Fixes for designs

* Minor fixes for self-update
2020-03-21 16:37:30 +11:00

79 lines
2.1 KiB
PHP

<?php
/**
* payment Ninja (https://paymentninja.com)
*
* @link https://github.com/paymentninja/paymentninja source repository
*
* @copyright Copyright (c) 2020. payment Ninja LLC (https://paymentninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Payment;
use App\Factory\PaymentFactory;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\Payment\UpdateInvoicePayment;
class PaymentService
{
private $payment;
public function __construct($payment)
{
$this->payment = $payment;
}
public function manualPayment($invoice) :?Payment
{
/* Create Payment */
$payment = PaymentFactory::create($invoice->company_id, $invoice->user_id);
$payment->amount = $invoice->balance;
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $invoice->client_id;
$payment->transaction_reference = ctrans('texts.manual_entry');
$payment->currency_id = $invoice->client->getSetting('currency_id');
/* Create a payment relationship to the invoice entity */
$payment->save();
$payment->invoices()->attach($invoice->id, [
'amount' => $payment->amount
]);
return $payment;
}
public function sendEmail($contact = null)
{
return (new SendEmail($this->payment))->run(null, $contact);
}
public function reversePayment()
{
$invoices = $this->payment->invoices()->get();
$client = $this->payment->client;
$invoices->each(function ($invoice) {
if ($invoice->pivot->amount > 0) {
$invoice->status_id = Invoice::STATUS_SENT;
$invoice->balance = $invoice->pivot->amount;
$invoice->save();
}
});
$this->payment->ledger()->updatePaymentBalance($this->payment->amount);
$client->service()
->updateBalance($this->payment->amount)
->updatePaidToDate($this->payment->amount*-1)
->save();
}
public function updateInvoicePayment()
{
return ((new UpdateInvoicePayment($this->payment)))->run();
}
}