Move email/ resources to mail/, added invoice generated email to admin, updated email template

This commit is contained in:
2024-08-03 10:06:25 +10:00
parent f8453ae391
commit 0469d64577
40 changed files with 439 additions and 213 deletions

View File

@@ -8,12 +8,14 @@ use Clarkeash\Doorman\Models\Invite;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
use Leenooks\Casts\LeenooksCarbon;
use Leenooks\Traits\ScopeActive;
use App\Casts\CollectionOrNull;
use App\Interfaces\IDs;
use App\Traits\PushNew;
use App\Mail\{InvoiceEmail,InvoiceGeneratedAdmin};
use App\Traits\{PushNew,SiteID};
/**
* Class Invoice
@@ -36,7 +38,7 @@ use App\Traits\PushNew;
*/
class Invoice extends Model implements IDs
{
use PushNew,ScopeActive;
use PushNew,ScopeActive,SiteID;
protected $casts = [
'created_at' => 'datetime:Y-m-d',
@@ -95,8 +97,8 @@ class Invoice extends Model implements IDs
],
];
// Array of items that can be updated with PushNew
protected $pushable = ['items'];
// Our related items that need to be updated when we call pushNew()
protected $pushable = ['items_active'];
protected $with = [
'items_active:id,start_at,stop_at,quantity,price_base,discount_amt,item_type,product_id,service_id,invoice_id',
@@ -108,6 +110,21 @@ class Invoice extends Model implements IDs
/* STATIC METHODS */
public static function boot()
{
parent::boot();
static::created(function($model) {
// Send an email to an admin that the invoice was created
$uo = User::where('email',config('osb.admin'))->sole();
Mail::to($uo->email)
->send(new InvoiceGeneratedAdmin($model));
// @todo Queue an email to the user
});
}
/**
* This works out what multiplier to use to change billing periods
*
@@ -565,6 +582,28 @@ class Invoice extends Model implements IDs
return parent::save($options);
}
/**
* Record the invoice being sent
*
* @return int
*/
public function send(): int
{
$result = Mail::to($this->account->user->email)
->send(new InvoiceEmail($this));
$this->print_status = TRUE;
if ($this->reminders->has('sent'))
$this->reminders->put('sent',collect($this->reminders->get('sent')));
else
$this->reminders->put('sent',collect());
$this->reminders->get('sent')->push(Carbon::now());
return $result;
}
/**
* Group the invoice items by product ID, returning the number of products and total
*
@@ -574,12 +613,20 @@ class Invoice extends Model implements IDs
{
$return = collect();
foreach ($this->items_active->groupBy('product_id') as $o) {
$po = $o->first()->product;
foreach ($this->items_active->groupBy('product_id') as $id => $o) {
if (! $id) {
$po = new Product;
$po->translate = new ProductTranslate;
$po->translate->name_detail = 'Miscellanious';
} else {
$po = $o->first()->product;
}
$po->count = count($o->pluck('service_id')->unique());
$return->push([
'product' => $o->first()->product,
'product' => $po,
'services' => $o->pluck('service_id')->unique(),
'sub_total' => $o->sum('sub_total'),
'tax_total' => $o->sum('tax'),
@@ -589,4 +636,21 @@ class Invoice extends Model implements IDs
return $return->sortBy('product.name');
}
public function summary_other(): Collection
{
$result = collect();
foreach ($this->items_active->whereNull('service_id') as $o) {
dd($o);
$result->push([
'description' => 'Account Items',
'sub_total' => $o->sub_total,
'tax_total' => $o->tax,
'total' => $o->total,
]);
}
return $result;
}
}