Cast Invoice::reminders to json

This commit is contained in:
Deon George 2022-06-13 19:58:53 +10:00
parent d1fbb4b42b
commit 642446e592
1 changed files with 12 additions and 6 deletions

View File

@ -35,6 +35,10 @@ class Invoice extends Model implements IDs
{ {
use PushNew,ScopeActive; use PushNew,ScopeActive;
protected $casts = [
'reminders'=>'json',
];
protected $dates = [ protected $dates = [
'due_at', 'due_at',
]; ];
@ -414,16 +418,17 @@ class Invoice extends Model implements IDs
/** /**
* @param string $key * @param string $key
* @return string * @return array
* @todo Ugly hack to update reminders * @todo Ugly hack to update reminders
*/ */
public function reminders(string $key) { public function reminders(string $key): array
$r = json_decode($this->reminders); {
$r = $this->reminders;
if (! Arr::get($r,$key)) { if (! Arr::get($r,$key)) {
$r[$key] = time(); $r[$key] = time();
return json_encode($r);
} }
return $r;
} }
/** /**
@ -432,7 +437,8 @@ class Invoice extends Model implements IDs
* @param array $options * @param array $options
* @return bool * @return bool
*/ */
public function save(array $options = []) { public function save(array $options = [])
{
// Automatically set the date_due attribute for new records. // Automatically set the date_due attribute for new records.
if (! $this->exists AND ! $this->due_at) { if (! $this->exists AND ! $this->due_at) {
$this->due_at = $this->items->min('start_at'); $this->due_at = $this->items->min('start_at');