1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Services/Credit/CreditService.php

132 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Credit;
use App\Models\Credit;
use App\Utils\Traits\MakesHash;
class CreditService
{
use MakesHash;
protected $credit;
public function __construct($credit)
{
$this->credit = $credit;
}
2020-11-11 01:13:39 +01:00
public function getCreditPdf($invitation)
{
2020-11-11 01:13:39 +01:00
return (new GetCreditPdf($invitation))->run();
}
/**
* Applies the invoice number.
* @return $this InvoiceService object
*/
public function applyNumber()
{
2020-02-18 21:56:21 +01:00
$this->credit = (new ApplyNumber($this->credit->client, $this->credit))->run();
return $this;
}
public function createInvitations()
{
2020-02-18 21:56:21 +01:00
$this->credit = (new CreateInvitations($this->credit))->run();
return $this;
}
public function setStatus($status)
{
$this->credit->status_id = $status;
return $this;
}
2020-10-28 00:02:32 +01:00
public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->credit, null, $contact);
return $send_email->run();
}
2020-10-23 06:18:16 +02:00
public function setCalculatedStatus()
{
2020-11-25 15:19:52 +01:00
if ((int)$this->credit->balance == 0) {
2020-10-23 06:18:16 +02:00
$this->credit->status_id = Credit::STATUS_APPLIED;
2020-11-25 15:19:52 +01:00
} elseif ((string)$this->credit->amount == (string)$this->credit->balance) {
2020-10-23 06:18:16 +02:00
$this->credit->status_id = Credit::STATUS_SENT;
2020-11-25 15:19:52 +01:00
} elseif ($this->credit->balance > 0) {
2020-10-23 06:18:16 +02:00
$this->credit->status_id = Credit::STATUS_PARTIAL;
2020-11-25 15:19:52 +01:00
}
2020-10-23 06:18:16 +02:00
return $this;
}
public function markSent()
{
$this->credit = (new MarkSent($this->credit->client, $this->credit))->run();
return $this;
}
2020-10-13 14:28:30 +02:00
public function applyPayment($invoice, $amount, $payment)
{
2020-10-14 01:53:20 +02:00
$this->credit = (new ApplyPayment($this->credit, $invoice, $amount, $payment))->run();
2020-10-13 14:28:30 +02:00
return $this;
}
2020-10-23 06:18:16 +02:00
public function adjustBalance($adjustment)
{
$this->credit->balance += $adjustment;
return $this;
}
public function fillDefaults()
{
2020-11-04 09:43:20 +01:00
$settings = $this->credit->client->getMergedSettings();
2020-11-25 15:19:52 +01:00
if (! $this->credit->design_id) {
2020-11-04 09:43:20 +01:00
$this->credit->design_id = $this->decodePrimaryKey($settings->credit_design_id);
2020-11-25 15:19:52 +01:00
}
2020-11-04 09:43:20 +01:00
2020-11-25 15:19:52 +01:00
if (!isset($this->credit->footer)) {
2020-11-04 09:43:20 +01:00
$this->credit->footer = $settings->credit_footer;
2020-11-25 15:19:52 +01:00
}
2020-11-04 09:43:20 +01:00
2020-11-25 15:19:52 +01:00
if (!isset($this->credit->terms)) {
2020-11-04 09:43:20 +01:00
$this->credit->terms = $settings->credit_terms;
2020-11-25 15:19:52 +01:00
}
2020-11-04 09:43:20 +01:00
2020-11-25 15:19:52 +01:00
return $this;
}
/**
* Saves the credit.
* @return Credit object
*/
public function save() : ?Credit
{
$this->credit->save();
return $this->credit;
}
}