1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Models/Account.php

381 lines
9.8 KiB
PHP
Raw Normal View History

2015-03-31 11:38:24 +02:00
<?php namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-03-23 07:52:01 +01:00
use Eloquent;
2015-03-26 04:52:42 +01:00
use Utils;
2015-03-31 19:42:37 +02:00
use Session;
2015-04-28 22:13:52 +02:00
use DateTime;
2015-03-23 07:52:01 +01:00
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2015-03-16 22:45:25 +01:00
class Account extends Eloquent
{
2015-03-31 11:38:24 +02:00
use SoftDeletes;
protected $dates = ['deleted_at'];
2015-03-16 22:45:25 +01:00
public function users()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\User');
2015-03-16 22:45:25 +01:00
}
public function clients()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\Client');
2015-03-16 22:45:25 +01:00
}
public function invoices()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\Invoice');
2015-03-16 22:45:25 +01:00
}
public function account_gateways()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\AccountGateway');
2015-03-16 22:45:25 +01:00
}
public function tax_rates()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\TaxRate');
2015-03-16 22:45:25 +01:00
}
public function country()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Country');
2015-03-16 22:45:25 +01:00
}
public function timezone()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Timezone');
2015-03-16 22:45:25 +01:00
}
public function language()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Language');
2015-03-16 22:45:25 +01:00
}
public function date_format()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\DateFormat');
2015-03-16 22:45:25 +01:00
}
public function datetime_format()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\DatetimeFormat');
2015-03-16 22:45:25 +01:00
}
public function size()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Size');
2015-03-16 22:45:25 +01:00
}
2015-04-02 15:06:16 +02:00
public function currency()
{
return $this->belongsTo('App\Models\Currency');
}
2015-03-16 22:45:25 +01:00
public function industry()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Industry');
2015-03-16 22:45:25 +01:00
}
public function isGatewayConfigured($gatewayId = 0)
{
$this->load('account_gateways');
if ($gatewayId) {
return $this->getGatewayConfig($gatewayId) != false;
} else {
return count($this->account_gateways) > 0;
}
}
public function getDisplayName()
{
if ($this->name) {
return $this->name;
}
$this->load('users');
$user = $this->users()->first();
return $user->getDisplayName();
}
public function getTimezone()
{
if ($this->timezone) {
return $this->timezone->name;
} else {
return 'US/Eastern';
}
}
public function getGatewayByType($type = PAYMENT_TYPE_ANY)
{
foreach ($this->account_gateways as $gateway) {
2015-03-17 15:43:35 +01:00
if (!$type || $type == PAYMENT_TYPE_ANY) {
2015-03-16 22:45:25 +01:00
return $gateway;
2015-04-15 18:35:41 +02:00
} elseif ($gateway->isPaymentType($type)) {
2015-03-16 22:45:25 +01:00
return $gateway;
}
}
return false;
}
public function getGatewayConfig($gatewayId)
{
foreach ($this->account_gateways as $gateway) {
if ($gateway->gateway_id == $gatewayId) {
return $gateway;
}
}
return false;
}
public function getLogoPath()
{
return 'logo/'.$this->account_key.'.jpg';
}
public function getLogoWidth()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $width;
}
public function getLogoHeight()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $height;
}
public function getNextInvoiceNumber($isQuote = false)
{
$counter = $isQuote && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
$prefix = $isQuote ? $this->quote_number_prefix : $this->invoice_number_prefix;
return $prefix.str_pad($counter, 4, "0", STR_PAD_LEFT);
}
public function incrementCounter($invoiceNumber, $isQuote = false, $isRecurring)
{
// check if the user modified the invoice number
if (!$isRecurring && $invoiceNumber != $this->getNextInvoiceNumber($isQuote)) {
$number = intval(preg_replace('/[^0-9]/', '', $invoiceNumber));
if ($isQuote && !$this->share_counter) {
$this->quote_number_counter = $number + 1;
} else {
$this->invoice_number_counter = $number + 1;
}
// otherwise, just increment the counter
} else {
if ($isQuote && !$this->share_counter) {
$this->quote_number_counter += 1;
} else {
$this->invoice_number_counter += 1;
}
}
$this->save();
}
public function getLocale()
{
2015-04-08 15:19:17 +02:00
$language = Language::where('id', '=', $this->account->language_id)->first();
2015-03-16 22:45:25 +01:00
return $language->locale;
}
public function loadLocalizationSettings()
{
$this->load('timezone', 'date_format', 'datetime_format', 'language');
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);
Session::put(SESSION_LOCALE, $this->language_id ? $this->language->locale : DEFAULT_LOCALE);
}
public function getInvoiceLabels()
{
$data = [];
$fields = [
'invoice',
'invoice_date',
'due_date',
'invoice_number',
'po_number',
'discount',
'taxes',
'tax',
'item',
'description',
'unit_cost',
'quantity',
'line_total',
'subtotal',
'paid_to_date',
'balance_due',
2015-04-16 21:57:12 +02:00
'amount_due',
2015-03-16 22:45:25 +01:00
'terms',
'your_invoice',
'quote',
'your_quote',
'quote_date',
'quote_number',
'total',
'invoice_issued_to',
];
foreach ($fields as $field) {
$data[$field] = trans("texts.$field");
}
return $data;
}
public function isPro()
{
if (!Utils::isNinjaProd()) {
return true;
}
if ($this->account_key == NINJA_ACCOUNT_KEY) {
return true;
}
$datePaid = $this->pro_plan_paid;
if (!$datePaid || $datePaid == '0000-00-00') {
return false;
} elseif ($datePaid == NINJA_DATE) {
return true;
}
$today = new DateTime('now');
$datePaid = DateTime::createFromFormat('Y-m-d', $datePaid);
$interval = $today->diff($datePaid);
return $interval->y == 0;
}
public function isWhiteLabel()
{
if (Utils::isNinjaProd()) {
return false;
}
return $this->pro_plan_paid == NINJA_DATE;
}
public function getSubscription($eventId)
{
return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->first();
}
public function hideFieldsForViz()
{
foreach ($this->clients as $client) {
$client->setVisible([
'public_id',
'name',
'balance',
'paid_to_date',
'invoices',
'contacts',
]);
foreach ($client->invoices as $invoice) {
$invoice->setVisible([
'public_id',
'invoice_number',
'amount',
'balance',
'invoice_status_id',
'invoice_items',
'created_at',
]);
foreach ($invoice->invoice_items as $invoiceItem) {
$invoiceItem->setVisible([
'product_key',
'cost',
'qty',
]);
}
}
foreach ($client->contacts as $contact) {
$contact->setVisible([
'public_id',
'first_name',
'last_name',
'email', ]);
}
}
return $this;
}
public function getEmailTemplate($entityType, $message = false)
{
$field = "email_template_$entityType";
$template = $this->$field;
if ($template) {
return $template;
}
$template = "\$client,<p/>\r\n\r\n" .
trans("texts.{$entityType}_message", ['amount' => '$amount']) . "<p/>\r\n\r\n";
if ($entityType != ENTITY_PAYMENT) {
$template .= "<a href=\"\$link\">\$link</a><p/>\r\n\r\n";
}
if ($message) {
$template .= "$message<p/>\r\n\r\n";
}
return $template . "\$footer";
}
public function getEmailFooter()
{
if ($this->email_footer) {
2015-04-22 21:21:04 +02:00
// Add line breaks if HTML isn't already being used
return strip_tags($this->email_footer) == $this->email_footer ? nl2br($this->email_footer) : $this->email_footer;
2015-03-16 22:45:25 +01:00
} else {
return "<p>" . trans('texts.email_signature') . "<br>\$account</p>";
}
}
public function showTokenCheckbox()
{
if (!$this->isGatewayConfigured(GATEWAY_STRIPE)) {
return false;
}
2015-03-16 22:45:25 +01:00
return $this->token_billing_type_id == TOKEN_BILLING_OPT_IN
|| $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
public function selectTokenCheckbox()
{
return $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
}