2015-03-18 00:39:03 +01:00
|
|
|
<?php namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-05-10 21:02:35 +02:00
|
|
|
use DateTime;
|
2015-03-31 11:38:24 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
class Invoice extends EntityModel
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
2015-04-21 22:09:45 +02:00
|
|
|
protected $casts = [
|
|
|
|
'is_recurring' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Account');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2015-03-23 07:52:01 +01:00
|
|
|
return $this->belongsTo('App\Models\User');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function client()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_items()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->hasMany('App\Models\InvoiceItem')->orderBy('id');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_status()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\InvoiceStatus');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_design()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\InvoiceDesign');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invitations()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->hasMany('App\Models\Invitation')->orderBy('invitations.contact_id');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->invoice_number;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:40:21 +02:00
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
$entityType = $this->getEntityType();
|
|
|
|
return trans("texts.$entityType") . '_' . $this->invoice_number . '.pdf';
|
|
|
|
}
|
|
|
|
|
2015-05-09 20:25:16 +02:00
|
|
|
public function getPDFPath()
|
|
|
|
{
|
|
|
|
return storage_path() . '/pdfcache/cache-' . $this->id . '.pdf';
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getLink()
|
|
|
|
{
|
|
|
|
return link_to('invoices/'.$this->public_id, $this->invoice_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return $this->is_quote ? ENTITY_QUOTE : ENTITY_INVOICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isSent()
|
|
|
|
{
|
|
|
|
return $this->invoice_status_id >= INVOICE_STATUS_SENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isViewed()
|
|
|
|
{
|
|
|
|
return $this->invoice_status_id >= INVOICE_STATUS_VIEWED;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPaid()
|
|
|
|
{
|
|
|
|
return $this->invoice_status_id >= INVOICE_STATUS_PAID;
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:57:12 +02:00
|
|
|
public function getRequestedAmount()
|
|
|
|
{
|
|
|
|
return $this->partial > 0 ? $this->partial : $this->balance;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function hidePrivateFields()
|
|
|
|
{
|
|
|
|
$this->setVisible([
|
|
|
|
'invoice_number',
|
|
|
|
'discount',
|
|
|
|
'is_amount_discount',
|
|
|
|
'po_number',
|
|
|
|
'invoice_date',
|
|
|
|
'due_date',
|
|
|
|
'terms',
|
|
|
|
'invoice_footer',
|
|
|
|
'public_notes',
|
|
|
|
'amount',
|
|
|
|
'balance',
|
|
|
|
'invoice_items',
|
|
|
|
'client',
|
|
|
|
'tax_name',
|
|
|
|
'tax_rate',
|
|
|
|
'account',
|
|
|
|
'invoice_design',
|
|
|
|
'invoice_design_id',
|
|
|
|
'is_pro',
|
|
|
|
'is_quote',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_taxes1',
|
2015-03-15 13:01:13 +01:00
|
|
|
'custom_taxes2',
|
2015-04-16 19:12:56 +02:00
|
|
|
'partial',
|
2015-03-15 13:01:13 +01:00
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$this->client->setVisible([
|
|
|
|
'name',
|
|
|
|
'id_number',
|
|
|
|
'vat_number',
|
|
|
|
'address1',
|
|
|
|
'address2',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'postal_code',
|
|
|
|
'work_phone',
|
|
|
|
'payment_terms',
|
|
|
|
'contacts',
|
|
|
|
'country',
|
|
|
|
'currency_id',
|
|
|
|
'custom_value1',
|
2015-03-15 13:01:13 +01:00
|
|
|
'custom_value2',
|
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$this->account->setVisible([
|
|
|
|
'name',
|
|
|
|
'id_number',
|
|
|
|
'vat_number',
|
|
|
|
'address1',
|
|
|
|
'address2',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'postal_code',
|
|
|
|
'work_phone',
|
|
|
|
'work_email',
|
|
|
|
'country',
|
|
|
|
'currency_id',
|
|
|
|
'custom_label1',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_label2',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_client_label1',
|
|
|
|
'custom_client_label2',
|
|
|
|
'primary_color',
|
|
|
|
'secondary_color',
|
|
|
|
'hide_quantity',
|
|
|
|
'hide_paid_to_date',
|
|
|
|
'custom_invoice_label1',
|
2015-03-15 13:01:13 +01:00
|
|
|
'custom_invoice_label2',
|
|
|
|
'pdf_email_attachment',
|
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
foreach ($this->invoice_items as $invoiceItem) {
|
|
|
|
$invoiceItem->setVisible([
|
|
|
|
'product_key',
|
|
|
|
'notes',
|
|
|
|
'cost',
|
|
|
|
'qty',
|
|
|
|
'tax_name',
|
2015-03-15 13:01:13 +01:00
|
|
|
'tax_rate',
|
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->client->contacts as $contact) {
|
|
|
|
$contact->setVisible([
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
2015-03-15 13:01:13 +01:00
|
|
|
'phone',
|
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldSendToday()
|
|
|
|
{
|
|
|
|
if (!$this->start_date || strtotime($this->start_date) > strtotime('now')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->end_date && strtotime($this->end_date) < strtotime('now')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dayOfWeekToday = date('w');
|
|
|
|
$dayOfWeekStart = date('w', strtotime($this->start_date));
|
|
|
|
|
|
|
|
$dayOfMonthToday = date('j');
|
|
|
|
$dayOfMonthStart = date('j', strtotime($this->start_date));
|
|
|
|
|
|
|
|
if (!$this->last_sent_date) {
|
|
|
|
return true;
|
|
|
|
} 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->frequency_id) {
|
|
|
|
case FREQUENCY_WEEKLY:
|
|
|
|
return $daysSinceLastSent >= 7;
|
|
|
|
case FREQUENCY_TWO_WEEKS:
|
|
|
|
return $daysSinceLastSent >= 14;
|
|
|
|
case FREQUENCY_FOUR_WEEKS:
|
|
|
|
return $daysSinceLastSent >= 28;
|
|
|
|
case FREQUENCY_MONTHLY:
|
|
|
|
return $monthsSinceLastSent >= 1;
|
|
|
|
case FREQUENCY_THREE_MONTHS:
|
|
|
|
return $monthsSinceLastSent >= 3;
|
|
|
|
case FREQUENCY_SIX_MONTHS:
|
|
|
|
return $monthsSinceLastSent >= 6;
|
|
|
|
case FREQUENCY_ANNUALLY:
|
|
|
|
return $monthsSinceLastSent >= 12;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoice::created(function ($invoice) {
|
|
|
|
$invoice->account->incrementCounter($invoice->invoice_number, $invoice->is_quote, $invoice->recurring_invoice_id);
|
|
|
|
Activity::createInvoice($invoice);
|
|
|
|
});
|
|
|
|
|
|
|
|
Invoice::updating(function ($invoice) {
|
|
|
|
Activity::updateInvoice($invoice);
|
|
|
|
});
|
|
|
|
|
|
|
|
Invoice::deleting(function ($invoice) {
|
|
|
|
Activity::archiveInvoice($invoice);
|
|
|
|
});
|
|
|
|
|
2015-04-12 16:18:05 +02:00
|
|
|
Invoice::restoring(function ($invoice) {
|
2015-03-16 22:45:25 +01:00
|
|
|
Activity::restoreInvoice($invoice);
|
|
|
|
});
|