diff --git a/app/Models/Account.php b/app/Models/Account.php index ce67475..975a5da 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -2,15 +2,13 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use App\Traits\NextKey; -use App\Traits\PushNew; class Account extends Model { - use NextKey,PushNew; + use NextKey; const RECORD_ID = 'account'; public $incrementing = FALSE; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 5ec8fc5..bc6e4b0 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -24,6 +24,9 @@ class Invoice extends Model protected $dates = ['date_orig','due_date']; public $dateFormat = 'U'; + // Array of items that can be updated with PushNew + protected $pushable = ['items']; + protected $appends = [ 'date_due', 'due', diff --git a/app/Models/InvoiceItem.php b/app/Models/InvoiceItem.php index 1697d04..9029bef 100644 --- a/app/Models/InvoiceItem.php +++ b/app/Models/InvoiceItem.php @@ -23,6 +23,9 @@ class InvoiceItem extends Model protected $dates = ['date_start','date_stop']; public $dateFormat = 'U'; + // Array of items that can be updated with PushNew + protected $pushable = ['taxes']; + private $_tax = 0; public function invoice() diff --git a/app/Models/Service.php b/app/Models/Service.php index 35400f2..6591eb6 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -547,37 +547,37 @@ class Service extends Model switch ($this->recur_schedule) { // Weekly 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; // Monthly 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; // Quarterly 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; // Half Yearly 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; // Yearly 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; // Two Yearly 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; // Three Yearly 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; default: throw new Exception('Unknown recur_schedule'); diff --git a/app/Traits/PushNew.php b/app/Traits/PushNew.php index 656f2f4..3e76a17 100644 --- a/app/Traits/PushNew.php +++ b/app/Traits/PushNew.php @@ -13,13 +13,18 @@ trait PushNew { public function pushNew() { if (! $this->save()) { + // We dont traverse this models relations return false; } // 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 // 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->all() : [$models];