invoiceninja/app/models/Account.php

163 lines
3 KiB
PHP
Raw Normal View History

2013-11-26 14:45:07 +02: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 14:22:08 +02:00
public function invoices()
{
return $this->hasMany('Invoice');
}
2013-11-26 14:45:07 +02:00
public function account_gateways()
{
return $this->hasMany('AccountGateway');
}
2013-12-29 13:28:44 +02:00
public function tax_rates()
{
return $this->hasMany('TaxRate');
}
2013-12-01 22:58:25 +02:00
public function country()
{
return $this->belongsTo('Country');
2013-12-02 14:22:29 +02:00
}
public function timezone()
{
return $this->belongsTo('Timezone');
2013-12-01 22:58:25 +02:00
}
2013-12-15 14:55:50 +02:00
public function date_format()
{
return $this->belongsTo('DateFormat');
}
public function datetime_format()
{
return $this->belongsTo('DatetimeFormat');
}
2014-01-06 20:03:00 +02:00
public function size()
{
return $this->belongsTo('Size');
}
public function industry()
{
return $this->belongsTo('Industry');
}
2013-12-15 14:55:50 +02:00
2013-11-26 14:45:07 +02:00
public function isGatewayConfigured($gatewayId = 0)
{
2014-01-08 23:22:56 +00:00
$this->load('account_gateways');
2013-11-26 14:45:07 +02:00
if ($gatewayId)
{
return $this->getGatewayConfig($gatewayId) != false;
}
else
{
return count($this->account_gateways) > 0;
}
}
2014-01-29 12:41:38 +02:00
public function getDisplayName()
{
if ($this->name)
{
return $this->name;
}
$this->load('users');
$user = $this->users()->first();
return $user->getDisplayName();
}
2014-01-09 19:00:08 +00:00
public function getTimezone()
{
if ($this->timezone)
{
return $this->timezone->name;
}
else
{
return 'US/Eastern';
}
}
2013-11-26 14:45:07 +02: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 18:20:14 +02:00
return 'logo/' . $this->account_key . '.jpg';
2013-11-26 14:45:07 +02:00
}
public function getLogoWidth()
{
list($width, $height) = getimagesize($this->getLogoPath());
return $width;
}
public function getLogoHeight()
{
list($width, $height) = getimagesize($this->getLogoPath());
return $height;
}
2013-12-01 14:22:08 +02:00
public function getNextInvoiceNumber()
2014-01-06 20:03:00 +02:00
{
2014-02-01 22:01:32 +02:00
$invoices = Invoice::withTrashed()->scope(false, $this->id)->get(['invoice_number']);
2014-01-08 23:22:56 +00:00
$max = 0;
2014-01-09 11:15:19 +00:00
foreach ($invoices as $invoice)
2014-01-08 23:22:56 +00:00
{
2014-01-09 11:15:19 +00:00
$number = intval(preg_replace("/[^0-9]/", "", $invoice->invoice_number));
2014-01-08 23:22:56 +00:00
$max = max($max, $number);
}
2014-01-09 11:15:19 +00:00
2014-01-08 23:22:56 +00:00
if ($max > 0)
2013-12-01 14:22:08 +02:00
{
2014-01-08 23:22:56 +00:00
return str_pad($max+1, 4, "0", STR_PAD_LEFT);
2013-12-01 14:22:08 +02:00
}
else
{
return DEFAULT_INVOICE_NUMBER;
}
}
2014-01-06 20:03:00 +02:00
public function loadLocalizationSettings()
{
$this->load('timezone', 'date_format', 'datetime_format');
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);
Session::put(SESSION_CURRENCY, $this->currency_id ? $this->currency_id : DEFAULT_CURRENCY);
}
2013-11-26 14:45:07 +02:00
}