More fixes for invoice_to, fix PushNew on belongs to relations

This commit is contained in:
Deon George 2020-07-06 16:02:59 +10:00
parent 878277907a
commit b0f9d07af9
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
5 changed files with 20 additions and 11 deletions

View File

@ -2,15 +2,13 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey; use App\Traits\NextKey;
use App\Traits\PushNew;
class Account extends Model class Account extends Model
{ {
use NextKey,PushNew; use NextKey;
const RECORD_ID = 'account'; const RECORD_ID = 'account';
public $incrementing = FALSE; public $incrementing = FALSE;

View File

@ -24,6 +24,9 @@ class Invoice extends Model
protected $dates = ['date_orig','due_date']; protected $dates = ['date_orig','due_date'];
public $dateFormat = 'U'; public $dateFormat = 'U';
// Array of items that can be updated with PushNew
protected $pushable = ['items'];
protected $appends = [ protected $appends = [
'date_due', 'date_due',
'due', 'due',

View File

@ -23,6 +23,9 @@ class InvoiceItem extends Model
protected $dates = ['date_start','date_stop']; protected $dates = ['date_start','date_stop'];
public $dateFormat = 'U'; public $dateFormat = 'U';
// Array of items that can be updated with PushNew
protected $pushable = ['taxes'];
private $_tax = 0; private $_tax = 0;
public function invoice() public function invoice()

View File

@ -547,37 +547,37 @@ class Service extends Model
switch ($this->recur_schedule) { switch ($this->recur_schedule) {
// Weekly // Weekly
case 0: case 0:
$d = $this->invoice_next->addWeek()->diff($this->invoice_next_end->startOfWeek())->days; $d = $this->invoice_next->addWeek()->startOfWeek()->diff($this->invoice_next_end->startOfWeek())->days;
break; break;
// Monthly // Monthly
case 1: case 1:
$d = $this->invoice_next->addMonth()->diff($this->invoice_next_end->startOfMonth())->days; $d = $this->invoice_next->addMonth()->startOfMonth()->diff($this->invoice_next_end->startOfMonth())->days;
break; break;
// Quarterly // Quarterly
case 2: case 2:
$d = $this->invoice_next->addQuarter()->diff($this->invoice_next_end->startOfQuarter())->days; $d = $this->invoice_next->addQuarter()->startOfQuarter()->diff($this->invoice_next_end->startOfQuarter())->days;
break; break;
// Half Yearly // Half Yearly
case 3: case 3:
$d = $this->invoice_next->addHalf()->diff($this->invoice_next_end->startOfHalf())->days; $d = $this->invoice_next->addHalf()->startOfHalf()->diff($this->invoice_next_end->startOfHalf())->days;
break; break;
// Yearly // Yearly
case 4: case 4:
$d = $this->invoice_next->addYear()->diff($this->invoice_next_end->startOfYear())->days; $d = $this->invoice_next->addYear()->startOfYear()->diff($this->invoice_next_end->startOfYear())->days;
break; break;
// Two Yearly // Two Yearly
case 5: case 5:
$d = $this->invoice_next->addYear(2)->diff($this->invoice_next_end->subyear(2))->days-1; $d = $this->invoice_next->addYear(2)->startOfYear()->diff($this->invoice_next_end->subyear(2))->days-1;
break; break;
// Three Yearly // Three Yearly
case 6: case 6:
$d = $this->invoice_next->addYear(3)->diff($this->invoice_next_end->subyear(3))->days-1; $d = $this->invoice_next->addYear(3)->startOfYear()->diff($this->invoice_next_end->subyear(3))->days-1;
break; break;
default: throw new Exception('Unknown recur_schedule'); default: throw new Exception('Unknown recur_schedule');

View File

@ -13,13 +13,18 @@ trait PushNew
{ {
public function pushNew() { public function pushNew() {
if (! $this->save()) { if (! $this->save()) {
// We dont traverse this models relations
return false; return false;
} }
// To sync all of the relationships to the database, we will simply spin through // To sync all of the relationships to the database, we will simply spin through
// the relationships and save each model via this "pushNew" method, which allows // the relationships and save each model via this "pushNew" method, which allows
// us to recurse into all of these nested relations for the model instance. // us to recurse into all of these nested relations for the model instance.
foreach ($this->relations as $models) { foreach ($this->relations as $key => $models) {
// If we are not pushable, jump to the next item.
if (! is_array($this->pushable) OR ! in_array($key,$this->pushable))
continue;
$models = $models instanceof Collection $models = $models instanceof Collection
? $models->all() : [$models]; ? $models->all() : [$models];