1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/models/Account.php

255 lines
4.7 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
class Account extends Eloquent
{
protected $softDelete = true;
public function users()
{
return $this->hasMany('User');
}
public function clients()
{
return $this->hasMany('Client');
}
2013-12-01 13:22:08 +01:00
public function invoices()
{
return $this->hasMany('Invoice');
}
2013-11-26 13:45:07 +01:00
public function account_gateways()
{
return $this->hasMany('AccountGateway');
}
2013-12-29 12:28:44 +01:00
public function tax_rates()
{
return $this->hasMany('TaxRate');
}
2013-12-01 21:58:25 +01:00
public function country()
{
return $this->belongsTo('Country');
2013-12-02 13:22:29 +01:00
}
public function timezone()
{
return $this->belongsTo('Timezone');
2013-12-01 21:58:25 +01:00
}
2014-04-02 15:10:00 +02:00
public function language()
{
return $this->belongsTo('Language');
}
2013-12-15 13:55:50 +01:00
public function date_format()
{
return $this->belongsTo('DateFormat');
}
public function datetime_format()
{
return $this->belongsTo('DatetimeFormat');
}
2014-01-06 19:03:00 +01:00
public function size()
{
return $this->belongsTo('Size');
}
public function industry()
{
return $this->belongsTo('Industry');
}
2013-12-15 13:55:50 +01:00
2013-11-26 13:45:07 +01:00
public function isGatewayConfigured($gatewayId = 0)
{
2014-01-09 00:22:56 +01:00
$this->load('account_gateways');
2013-11-26 13:45:07 +01:00
if ($gatewayId)
{
return $this->getGatewayConfig($gatewayId) != false;
}
else
{
return count($this->account_gateways) > 0;
}
}
2014-01-29 11:41:38 +01:00
public function getDisplayName()
{
if ($this->name)
{
return $this->name;
}
$this->load('users');
$user = $this->users()->first();
return $user->getDisplayName();
}
2014-01-09 20:00:08 +01:00
public function getTimezone()
{
if ($this->timezone)
{
return $this->timezone->name;
}
else
{
return 'US/Eastern';
}
}
2013-11-26 13:45:07 +01:00
public function getGatewayConfig($gatewayId)
{
foreach ($this->account_gateways as $gateway)
{
if ($gateway->gateway_id == $gatewayId)
{
return $gateway;
}
}
return false;
}
public function getLogoPath()
{
2013-12-04 17:20:14 +01:00
return 'logo/' . $this->account_key . '.jpg';
2013-11-26 13:45:07 +01:00
}
public function getLogoWidth()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
2013-11-26 13:45:07 +01:00
return $width;
}
public function getLogoHeight()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $height;
2013-11-26 13:45:07 +01:00
}
2013-12-01 13:22:08 +01:00
public function getNextInvoiceNumber()
2014-01-06 19:03:00 +01:00
{
2014-02-01 21:01:32 +01:00
$invoices = Invoice::withTrashed()->scope(false, $this->id)->get(['invoice_number']);
2014-01-09 00:22:56 +01:00
$max = 0;
2014-01-09 12:15:19 +01:00
foreach ($invoices as $invoice)
2014-01-09 00:22:56 +01:00
{
2014-01-09 12:15:19 +01:00
$number = intval(preg_replace("/[^0-9]/", "", $invoice->invoice_number));
2014-01-09 00:22:56 +01:00
$max = max($max, $number);
}
2014-01-09 12:15:19 +01:00
2014-01-09 00:22:56 +01:00
if ($max > 0)
2013-12-01 13:22:08 +01:00
{
2014-01-09 00:22:56 +01:00
return str_pad($max+1, 4, "0", STR_PAD_LEFT);
2013-12-01 13:22:08 +01:00
}
else
{
return DEFAULT_INVOICE_NUMBER;
}
}
2014-01-06 19:03:00 +01:00
2014-04-02 15:10:00 +02:00
public function getLocale()
{
$language = Language::remember(DEFAULT_QUERY_CACHE)->where('id', '=', $this->account->language_id)->first();
return $language->locale;
}
2014-01-06 19:03:00 +01:00
public function loadLocalizationSettings()
{
2014-04-02 15:10:00 +02:00
$this->load('timezone', 'date_format', 'datetime_format', 'language');
2014-01-06 19:03:00 +01:00
Session::put(SESSION_TIMEZONE, $this->timezone ? $this->timezone->name : DEFAULT_TIMEZONE);
Session::put(SESSION_DATE_FORMAT, $this->date_format ? $this->date_format->format : DEFAULT_DATE_FORMAT);
Session::put(SESSION_DATE_PICKER_FORMAT, $this->date_format ? $this->date_format->picker_format : DEFAULT_DATE_PICKER_FORMAT);
Session::put(SESSION_DATETIME_FORMAT, $this->datetime_format ? $this->datetime_format->format : DEFAULT_DATETIME_FORMAT);
2014-03-23 20:38:03 +01:00
Session::put(SESSION_CURRENCY, $this->currency_id ? $this->currency_id : DEFAULT_CURRENCY);
Session::put(SESSION_LOCALE, $this->language_id ? $this->language->locale : DEFAULT_LOCALE);
2014-01-06 19:03:00 +01:00
}
2014-03-23 13:53:16 +01:00
public function getInvoiceLabels()
{
$data = [];
$fields = [
'invoice',
'invoice_date',
'due_date',
'invoice_number',
'po_number',
2014-03-23 19:19:03 +01:00
'discount',
2014-03-23 13:53:16 +01:00
'taxes',
'tax',
'item',
'description',
'unit_cost',
'quantity',
'line_total',
'subtotal',
'paid_to_date',
'balance_due',
'terms',
'your_invoice',
2014-05-20 23:40:09 +02:00
'quote',
'your_quote',
'quote_date',
'quote_number',
2014-07-23 22:28:40 +02:00
'total',
2014-03-23 13:53:16 +01:00
];
foreach ($fields as $field)
{
2014-03-27 13:25:31 +01:00
$data[$field] = trans("texts.$field");
2014-03-23 13:53:16 +01:00
}
return $data;
}
2014-06-29 19:23:10 +02:00
2014-04-12 20:55:40 +02:00
public function isPro()
{
2014-05-25 15:40:09 +02:00
if (!Utils::isNinjaProd())
2014-04-18 10:57:31 +02:00
{
return true;
}
2014-04-12 20:55:40 +02:00
if ($this->account_key == NINJA_ACCOUNT_KEY)
{
return true;
}
$datePaid = $this->pro_plan_paid;
if (!$datePaid || $datePaid == '0000-00-00')
{
return false;
}
2014-07-23 22:28:40 +02:00
else if ($datePaid == '2000-01-01')
{
return true;
}
2014-04-12 20:55:40 +02:00
$today = new DateTime('now');
$datePaid = DateTime::createFromFormat('Y-m-d', $datePaid);
$interval = $today->diff($datePaid);
return $interval->y == 0;
}
2014-07-27 22:31:41 +02:00
public function getSubscription($eventId)
{
return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->first();
}
2013-11-26 13:45:07 +01:00
}