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

225 lines
4.1 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-01 21:58:25 +01:00
public function account()
{
return $this->belongsTo('Account');
}
2013-11-26 13:45:07 +01:00
2014-05-25 18:23:07 +02:00
public function user()
{
return $this->belongsTo('User');
}
2013-11-26 13:45:07 +01:00
public function client()
{
2014-02-19 14:28:29 +01:00
return $this->belongsTo('Client')->withTrashed();
2013-11-26 13:45:07 +01:00
}
public function invoice_items()
{
return $this->hasMany('InvoiceItem');
}
2013-12-01 08:33:17 +01:00
public function invoice_status()
{
return $this->belongsTo('InvoiceStatus');
}
public function invitations()
{
return $this->hasMany('Invitation');
}
2013-12-01 21:58:25 +01:00
public function getName()
{
return $this->invoice_number;
}
2014-02-16 21:32:25 +01:00
public function getLink()
{
return link_to('invoices/' . $this->public_id, $this->invoice_number);
}
2013-12-01 21:58:25 +01:00
public function getEntityType()
{
2014-05-20 23:40:09 +02:00
return $this->is_quote ? ENTITY_QUOTE : ENTITY_INVOICE;
2013-12-01 21:58:25 +01:00
}
2014-01-06 07:48:11 +01:00
2013-12-11 12:11:59 +01:00
public function isSent()
{
return $this->invoice_status_id >= INVOICE_STATUS_SENT;
}
2013-12-31 20:49:54 +01:00
public function isViewed()
{
return $this->invoice_status_id >= INVOICE_STATUS_VIEWED;
}
2014-04-06 12:18:34 +02:00
public function isPaid()
{
return $this->invoice_status_id >= INVOICE_STATUS_PAID;
}
2013-12-31 17:38:49 +01:00
public function hidePrivateFields()
{
2014-04-21 23:39:42 +02:00
$this->setVisible([
'invoice_number',
'discount',
'po_number',
'invoice_date',
'due_date',
'terms',
'public_notes',
'amount',
'balance',
'invoice_items',
'client',
'tax_name',
'tax_rate',
'account',
2014-05-04 19:11:27 +02:00
'invoice_design_id',
2014-05-20 23:40:09 +02:00
'is_pro',
2014-07-20 12:38:42 +02:00
'is_quote',
'custom_value1',
'custom_value2',
'custom_taxes1',
'custom_taxes2']);
2013-12-31 20:49:54 +01:00
2014-04-21 23:39:42 +02:00
$this->client->setVisible([
'name',
'address1',
'address2',
'city',
'state',
'postal_code',
'work_phone',
'payment_terms',
'contacts',
'country',
'currency_id',
'custom_value1',
'custom_value2']);
$this->account->setVisible([
'name',
'address1',
'address2',
'city',
'state',
'postal_code',
'work_phone',
'work_email',
'country',
'currency_id',
'custom_label1',
'custom_value1',
'custom_label2',
'custom_value2',
'custom_client_label1',
2014-05-01 21:02:36 +02:00
'custom_client_label2',
'primary_color',
2014-07-20 12:38:42 +02:00
'secondary_color',
'hide_quantity',
2014-07-23 22:28:40 +02:00
'hide_paid_to_date',
'custom_invoice_label1',
'custom_invoice_label2']);
2013-12-31 17:38:49 +01:00
foreach ($this->invoice_items as $invoiceItem)
{
2014-04-21 23:39:42 +02:00
$invoiceItem->setVisible([
'product_key',
'notes',
'cost',
'qty',
'tax_name',
'tax_rate']);
2013-12-31 17:38:49 +01:00
}
foreach ($this->client->contacts as $contact)
{
2014-04-21 23:39:42 +02:00
$contact->setVisible([
'first_name',
'last_name',
'email',
'phone']);
2013-12-31 17:38:49 +01:00
}
return $this;
}
2013-12-11 12:11:59 +01:00
public function shouldSendToday()
{
2014-05-01 21:02:36 +02:00
if (!$this->start_date || strtotime($this->start_date) > strtotime('now'))
{
return false;
}
2014-05-01 21:54:02 +02:00
if ($this->end_date && strtotime($this->end_date) < strtotime('now'))
{
return false;
}
2013-12-11 12:11:59 +01:00
$dayOfWeekToday = date('w');
$dayOfWeekStart = date('w', strtotime($this->start_date));
$dayOfMonthToday = date('j');
$dayOfMonthStart = date('j', strtotime($this->start_date));
2014-01-09 14:44:55 +01:00
if (!$this->last_sent_date)
{
2014-05-01 21:02:36 +02:00
return true;
2014-01-09 14:44:55 +01:00
}
else
{
2013-12-11 12:11:59 +01:00
$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');
2014-01-09 14:44:55 +01:00
if ($daysSinceLastSent == 0)
{
2013-12-11 12:11:59 +01:00
return false;
}
}
2014-01-09 14:44:55 +01:00
2013-12-11 12:11:59 +01:00
switch ($this->frequency_id)
{
case FREQUENCY_WEEKLY:
2014-05-01 21:02:36 +02:00
return $daysSinceLastSent >= 7;
2013-12-11 12:11:59 +01:00
case FREQUENCY_TWO_WEEKS:
2014-05-01 21:02:36 +02:00
return $daysSinceLastSent >= 14;
2013-12-11 12:11:59 +01:00
case FREQUENCY_FOUR_WEEKS:
2014-05-01 21:02:36 +02:00
return $daysSinceLastSent >= 28;
2013-12-11 12:11:59 +01:00
case FREQUENCY_MONTHLY:
2014-05-01 21:02:36 +02:00
return $monthsSinceLastSent >= 1;
2013-12-11 12:11:59 +01:00
case FREQUENCY_THREE_MONTHS:
2014-05-01 21:02:36 +02:00
return $monthsSinceLastSent >= 3;
2013-12-11 12:11:59 +01:00
case FREQUENCY_SIX_MONTHS:
2014-05-01 21:02:36 +02:00
return $monthsSinceLastSent >= 6;
2013-12-11 12:11:59 +01:00
case FREQUENCY_ANNUALLY:
2014-05-01 21:02:36 +02:00
return $monthsSinceLastSent >= 12;
2013-12-11 12:11:59 +01:00
default:
2014-05-01 21:02:36 +02:00
return false;
2013-12-11 12:11:59 +01:00
}
return false;
}
2013-11-26 22:45:10 +01:00
}
Invoice::created(function($invoice)
{
Activity::createInvoice($invoice);
2013-12-30 21:17:45 +01:00
});
Invoice::updating(function($invoice)
{
Activity::updateInvoice($invoice);
2014-01-09 11:39:20 +01:00
});
Invoice::deleting(function($invoice)
{
Activity::archiveInvoice($invoice);
2013-11-26 22:45:10 +01:00
});