invoiceninja/app/Services/Credit/MarkSent.php

60 lines
1.4 KiB
PHP
Raw Permalink Normal View History

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