1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 14:42:42 +01:00
invoiceninja/app/models/Invoice.php

90 lines
2.3 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2013-12-04 17:20:14 +01:00
class Invoice extends EntityModel
2013-11-26 13:45:07 +01:00
{
2013-12-05 16:23:24 +01:00
protected $hidden = array('id', 'account_id', 'client_id', 'created_at', 'updated_at', 'deleted_at', 'viewed_date');
2013-12-03 18:32:33 +01:00
2013-12-01 21:58:25 +01:00
public function account()
{
return $this->belongsTo('Account');
}
2013-11-26 13:45:07 +01:00
public function client()
{
return $this->belongsTo('Client');
}
public function invoice_items()
{
return $this->hasMany('InvoiceItem');
}
2013-12-01 08:33:17 +01:00
public function invoice_status()
{
return $this->belongsTo('InvoiceStatus');
}
2013-12-01 21:58:25 +01:00
public function getName()
{
return $this->invoice_number;
}
public function getEntityType()
{
return ENTITY_INVOICE;
}
2013-12-09 10:38:49 +01:00
public function isRecurring()
{
2013-12-10 18:18:35 +01:00
return $this->how_often || $this->start_date || $this->end_date;
2013-12-09 10:38:49 +01:00
}
2013-12-10 18:18:35 +01:00
public function shouldSendToday()
2013-11-26 13:45:07 +01:00
{
2013-12-10 18:18:35 +01:00
$dayOfWeekToday = date('w');
$dayOfWeekStart = date('w', strtotime($this->start_date));
2013-11-26 13:45:07 +01:00
2013-12-10 18:18:35 +01:00
$dayOfMonthToday = date('j');
$dayOfMonthStart = date('j', strtotime($this->start_date));
if (!$this->last_sent_date) {
$daysSinceLastSent = 0;
$monthsSinceLastSent = 0;
} else {
$date1 = new DateTime($this->last_sent_date);
$date2 = new DateTime();
$diff = $date2->diff($date1);
$daysSinceLastSent = $diff->format("%a");
$monthsSinceLastSent = ($diff->format('%y') * 12) + $diff->format('%m');
if ($daysSinceLastSent == 0) {
return false;
}
}
switch ($this->how_often)
2013-11-26 13:45:07 +01:00
{
2013-12-10 18:18:35 +01:00
case FREQUENCY_WEEKLY:
return $dayOfWeekStart == $dayOfWeekToday;
case FREQUENCY_TWO_WEEKS:
return $dayOfWeekStart == $dayOfWeekToday && (!$daysSinceLastSent || $daysSinceLastSent == 14);
case FREQUENCY_FOUR_WEEKS:
return $dayOfWeekStart == $dayOfWeekToday && (!$daysSinceLastSent || $daysSinceLastSent == 28);
case FREQUENCY_MONTHLY:
return $dayOfMonthStart == $dayOfMonthToday || $daysSinceLastSent > 31;
case FREQUENCY_THREE_MONTHS:
return ($dayOfMonthStart == $dayOfMonthToday && (!$daysSinceLastSent || $monthsSinceLastSent == 3)) || $daysSinceLastSent > (3 * 31);
case FREQUENCY_SIX_MONTHS:
return ($dayOfMonthStart == $dayOfMonthToday && (!$daysSinceLastSent || $monthsSinceLastSent == 6)) || $daysSinceLastSent > (6 * 31);
case FREQUENCY_ANNUALLY:
return ($dayOfMonthStart == $dayOfMonthToday && (!$daysSinceLastSent || $monthsSinceLastSent == 12)) || $daysSinceLastSent > (12 *31);
2013-11-26 13:45:07 +01:00
}
2013-12-10 18:18:35 +01:00
return false;
2013-11-26 13:45:07 +01:00
}
2013-11-26 22:45:10 +01:00
}
Invoice::created(function($invoice)
{
Activity::createInvoice($invoice);
});