Enable invoice emailing

This commit is contained in:
Deon George
2020-05-25 17:45:17 +10:00
parent cbffda959d
commit 32c562cf30
8 changed files with 351 additions and 78 deletions

View File

@@ -3,10 +3,13 @@
namespace App\Models;
use Carbon\Carbon;
use Clarkeash\Doorman\Facades\Doorman;
use Clarkeash\Doorman\Models\Invite;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
use App\Traits\PushNew;
use Illuminate\Support\Arr;
class Invoice extends Model
{
@@ -52,7 +55,7 @@ class Invoice extends Model
public function items()
{
return $this->hasMany(InvoiceItem::class);
return $this->hasMany(InvoiceItem::class)->where('active',1);
}
public function paymentitems()
@@ -154,6 +157,29 @@ class Invoice extends Model
return $this->account->country->currency;
}
/**
* Return a download link for non-auth downloads
*
* @return string
*/
public function download_link(): string
{
// Re-use an existing code
$io = Invite::where('for',$this->account->user->email)->first();
$tokendate = ($x=Carbon::now()->addDays(21)) > ($y=$this->due_date->addDays(21)) ? $x : $y;
// Extend the expire date
if ($io AND ($tokendate > $io->valid_until)) {
$io->valid_until = $tokendate;
$io->save();
}
$code = (! $io) ? Doorman::generate()->for($this->account->user->email)->uses(0)->expiresOn($tokendate)->make() : $io->code;
return url('u/invoice',[$this->id,'email',$code]);
}
public function products()
{
$return = collect();
@@ -196,6 +222,20 @@ class Invoice extends Model
})->filter()->sortBy('item_type');
}
/**
* @param string $key
* @return string
* @todo Ugly hack to update reminders
*/
public function reminders(string $key) {
$r = unserialize($this->reminders);
if (! Arr::get($r,$key)) {
$r[$key] = time();
return serialize($r);
}
}
/**
* Automatically set our due_date at save time.
*