2020-03-03 10:51:05 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-03 10:51:05 +01:00
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
|
|
|
use App\Events\Credit\CreditWasMarkedSent;
|
|
|
|
use App\Models\Credit;
|
2023-02-03 12:54:34 +01:00
|
|
|
use App\Models\Webhook;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-03-03 10:51:05 +01:00
|
|
|
|
|
|
|
class MarkSent
|
|
|
|
{
|
|
|
|
private $client;
|
|
|
|
|
2020-03-04 00:51:50 +01:00
|
|
|
private $credit;
|
|
|
|
|
|
|
|
public function __construct($client, $credit)
|
2020-03-03 10:51:05 +01:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2020-03-04 00:51:50 +01:00
|
|
|
$this->credit = $credit;
|
2020-03-03 10:51:05 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:52 +01:00
|
|
|
public function run($fire_event = false)
|
2020-03-03 10:51:05 +01:00
|
|
|
{
|
|
|
|
/* Return immediately if status is not draft */
|
2020-03-04 00:51:50 +01:00
|
|
|
if ($this->credit->status_id != Credit::STATUS_DRAFT) {
|
|
|
|
return $this->credit;
|
2020-03-03 10:51:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 00:51:50 +01:00
|
|
|
$this->credit->markInvitationsSent();
|
2020-03-03 10:51:05 +01:00
|
|
|
|
2020-05-19 00:22:18 +02:00
|
|
|
$this->credit
|
|
|
|
->service()
|
|
|
|
->setStatus(Credit::STATUS_SENT)
|
|
|
|
->applyNumber()
|
2020-12-02 12:01:50 +01:00
|
|
|
->adjustBalance($this->credit->amount)
|
2023-09-05 03:54:05 +02:00
|
|
|
// ->deletePdf()
|
2020-05-19 00:22:18 +02:00
|
|
|
->save();
|
2020-03-03 10:51:05 +01:00
|
|
|
|
2022-08-22 00:24:36 +02:00
|
|
|
$this->client
|
|
|
|
->service()
|
|
|
|
->adjustCreditBalance($this->credit->amount)
|
|
|
|
->save();
|
2023-02-03 12:54:34 +01:00
|
|
|
|
2021-12-17 12:11:36 +01:00
|
|
|
event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-10-08 05:31:02 +02:00
|
|
|
|
2024-01-03 12:06:52 +01:00
|
|
|
if($fire_event) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-01-03 12:06:52 +01:00
|
|
|
event('eloquent.updated: App\Models\Credit', $this->credit);
|
|
|
|
$this->credit->sendEvent(Webhook::EVENT_SENT_CREDIT);
|
|
|
|
|
|
|
|
}
|
2023-02-03 12:54:34 +01:00
|
|
|
|
2020-03-04 00:51:50 +01:00
|
|
|
return $this->credit;
|
2024-01-03 12:06:52 +01:00
|
|
|
|
2020-03-03 10:51:05 +01:00
|
|
|
}
|
|
|
|
}
|