2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-05-09 00:35:49 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-05-09 00:35:49 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
use App\Models\Credit;
|
2020-10-13 14:28:30 +02:00
|
|
|
use App\Services\Credit\ApplyPayment;
|
2020-03-03 10:44:26 +01:00
|
|
|
use App\Services\Credit\CreateInvitations;
|
|
|
|
use App\Services\Credit\MarkSent;
|
2020-10-28 00:02:32 +01:00
|
|
|
use App\Services\Credit\SendEmail;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
class CreditService
|
|
|
|
{
|
|
|
|
protected $credit;
|
|
|
|
|
|
|
|
public function __construct($credit)
|
|
|
|
{
|
|
|
|
$this->credit = $credit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCreditPdf($contact)
|
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
return (new GetCreditPdf($this->credit, $contact))->run();
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Applies the invoice number.
|
2020-02-15 10:01:15 +01:00
|
|
|
* @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();
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createInvitations()
|
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
$this->credit = (new CreateInvitations($this->credit))->run();
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
if((int)$this->credit->balance == 0)
|
|
|
|
$this->credit->status_id = Credit::STATUS_APPLIED;
|
|
|
|
elseif((string)$this->credit->amount == (string)$this->credit->balance)
|
|
|
|
$this->credit->status_id = Credit::STATUS_SENT;
|
|
|
|
elseif($this->credit->balance > 0)
|
|
|
|
$this->credit->status_id = Credit::STATUS_PARTIAL;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
public function markSent()
|
|
|
|
{
|
2020-03-04 00:51:50 +01:00
|
|
|
$this->credit = (new MarkSent($this->credit->client, $this->credit))->run();
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the credit.
|
2020-02-15 10:01:15 +01:00
|
|
|
* @return Credit object
|
|
|
|
*/
|
|
|
|
public function save() : ?Credit
|
|
|
|
{
|
|
|
|
$this->credit->save();
|
2020-02-22 03:25:49 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
return $this->credit;
|
|
|
|
}
|
|
|
|
}
|