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

1107 lines
40 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-03-26 07:24:02 +01:00
use Auth;
2015-04-06 07:45:27 +02:00
use File;
use Image;
2015-03-26 07:24:02 +01:00
use Input;
use Redirect;
use Session;
use Utils;
2015-04-01 21:57:02 +02:00
use Validator;
2015-04-06 07:45:27 +02:00
use View;
2015-04-01 21:57:02 +02:00
use stdClass;
2015-04-08 15:19:17 +02:00
use Cache;
2015-04-16 19:12:56 +02:00
use Response;
2015-04-28 22:13:52 +02:00
use parseCSV;
2015-05-10 21:02:35 +02:00
use Request;
use App\Models\Affiliate;
use App\Models\License;
2015-04-05 21:15:37 +02:00
use App\Models\User;
2015-04-28 22:13:52 +02:00
use App\Models\Client;
use App\Models\Contact;
use App\Models\Invoice;
use App\Models\InvoiceItem;
2015-04-05 21:15:37 +02:00
use App\Models\Activity;
2015-04-28 22:13:52 +02:00
use App\Models\Payment;
use App\Models\Credit;
2015-03-26 07:24:02 +01:00
use App\Models\Account;
use App\Models\Country;
use App\Models\Currency;
use App\Models\DateFormat;
use App\Models\DatetimeFormat;
use App\Models\Language;
use App\Models\Size;
2015-09-20 23:05:02 +02:00
use App\Models\Gateway;
2015-03-26 07:24:02 +01:00
use App\Models\Timezone;
use App\Models\Industry;
2015-04-01 21:57:02 +02:00
use App\Models\InvoiceDesign;
2015-10-21 13:11:08 +02:00
use App\Models\TaxRate;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Repositories\ClientRepository;
2015-11-01 19:21:11 +01:00
use App\Ninja\Repositories\ReferralRepository;
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Mailers\ContactMailer;
2015-09-25 11:57:40 +02:00
use App\Events\UserSignedUp;
2015-03-31 19:42:37 +02:00
use App\Events\UserLoggedIn;
use App\Events\UserSettingsChanged;
2015-10-11 16:41:09 +02:00
use App\Services\AuthService;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
use App\Commands\CreateClient;
2015-03-26 07:24:02 +01:00
class AccountController extends BaseController
2015-03-16 22:45:25 +01:00
{
protected $accountRepo;
protected $userMailer;
protected $contactMailer;
2015-11-01 19:21:11 +01:00
protected $referralRepository;
2015-03-16 22:45:25 +01:00
2015-11-01 19:21:11 +01:00
public function __construct(AccountRepository $accountRepo, UserMailer $userMailer, ContactMailer $contactMailer, ReferralRepository $referralRepository)
2015-03-16 22:45:25 +01:00
{
parent::__construct();
$this->accountRepo = $accountRepo;
$this->userMailer = $userMailer;
$this->contactMailer = $contactMailer;
2015-11-01 19:21:11 +01:00
$this->referralRepository = $referralRepository;
2015-03-16 22:45:25 +01:00
}
public function demo()
{
$demoAccountId = Utils::getDemoAccountId();
if (!$demoAccountId) {
return Redirect::to('/');
}
$account = Account::find($demoAccountId);
$user = $account->users()->first();
Auth::login($user, true);
return Redirect::to('invoices/create');
}
public function getStarted()
{
$user = false;
$guestKey = Input::get('guest_key'); // local storage key to login until registered
$prevUserId = Session::pull(PREV_USER_ID); // last user id used to link to new account
2015-03-16 22:45:25 +01:00
if (Auth::check()) {
return Redirect::to('invoices/create');
}
if (!Utils::isNinja() && (Account::count() > 0 && !$prevUserId)) {
2015-06-16 21:35:35 +02:00
return Redirect::to('/login');
}
2015-07-07 22:08:16 +02:00
if ($guestKey && !$prevUserId) {
2015-03-16 22:45:25 +01:00
$user = User::where('password', '=', $guestKey)->first();
if ($user && $user->registered) {
return Redirect::to('/');
}
}
if (!$user) {
$account = $this->accountRepo->create();
$user = $account->users()->first();
Session::forget(RECENTLY_VIEWED);
2015-07-07 22:08:16 +02:00
if ($prevUserId) {
$users = $this->accountRepo->associateAccounts($user->id, $prevUserId);
Session::put(SESSION_USER_ACCOUNTS, $users);
}
2015-03-16 22:45:25 +01:00
}
Auth::login($user, true);
2015-09-25 11:57:40 +02:00
event(new UserLoggedIn());
2015-04-30 19:54:19 +02:00
2015-09-29 16:16:19 +02:00
$redirectTo = Input::get('redirect_to') ?: 'invoices/create';
return Redirect::to($redirectTo)->with('sign_up', Input::get('sign_up'));
2015-03-16 22:45:25 +01:00
}
public function enableProPlan()
{
$invitation = $this->accountRepo->enableProPlan();
return $invitation->invitation_key;
}
public function setTrashVisible($entityType, $visible)
{
Session::put("show_trash:{$entityType}", $visible == 'true');
if ($entityType == 'user') {
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/'.ACCOUNT_USER_MANAGEMENT);
2015-03-16 22:45:25 +01:00
} elseif ($entityType == 'token') {
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/'.ACCOUNT_API_TOKENS);
2015-03-16 22:45:25 +01:00
} else {
return Redirect::to("{$entityType}s");
}
}
public function getSearchData()
{
$data = $this->accountRepo->getSearchData();
return Response::json($data);
}
2015-10-14 16:15:39 +02:00
public function showSection($section = false)
2015-03-16 22:45:25 +01:00
{
2015-10-14 16:15:39 +02:00
if (!$section) {
return Redirect::to('/settings/' . ACCOUNT_COMPANY_DETAILS, 301);
}
2015-10-11 16:41:09 +02:00
2015-10-14 16:15:39 +02:00
if ($section == ACCOUNT_COMPANY_DETAILS) {
return self::showCompanyDetails();
} elseif ($section == ACCOUNT_USER_DETAILS) {
return self::showUserDetails();
} elseif ($section == ACCOUNT_LOCALIZATION) {
return self::showLocalization();
} elseif ($section == ACCOUNT_PAYMENTS) {
return self::showOnlinePayments();
2015-10-22 20:48:12 +02:00
} elseif ($section == ACCOUNT_INVOICE_SETTINGS) {
return self::showInvoiceSettings();
2015-10-14 16:15:39 +02:00
} elseif ($section == ACCOUNT_IMPORT_EXPORT) {
return View::make('accounts.import_export', ['title' => trans('texts.import_export')]);
} elseif ($section == ACCOUNT_INVOICE_DESIGN || $section == ACCOUNT_CUSTOMIZE_DESIGN) {
return self::showInvoiceDesign($section);
} elseif ($section === ACCOUNT_TEMPLATES_AND_REMINDERS) {
return self::showTemplates();
} elseif ($section === ACCOUNT_PRODUCTS) {
return self::showProducts();
2015-10-21 13:11:08 +02:00
} elseif ($section === ACCOUNT_TAX_RATES) {
return self::showTaxRates();
2015-11-04 14:57:59 +01:00
} elseif ($section === ACCOUNT_SYSTEM_SETTINGS) {
return self::showSystemSettings();
2015-10-14 16:15:39 +02:00
} else {
2015-03-16 22:45:25 +01:00
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
2015-10-14 16:15:39 +02:00
'title' => trans("texts.{$section}"),
'section' => $section
2015-03-16 22:45:25 +01:00
];
2015-10-14 16:15:39 +02:00
return View::make("accounts.{$section}", $data);
}
}
2015-03-16 22:45:25 +01:00
2015-11-04 14:57:59 +01:00
private function showSystemSettings()
{
if (Utils::isNinjaProd()) {
return Redirect::to('/');
}
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'title' => trans("texts.system_settings"),
'section' => ACCOUNT_SYSTEM_SETTINGS,
];
return View::make("accounts.system_settings", $data);
}
2015-10-22 20:48:12 +02:00
private function showInvoiceSettings()
{
$account = Auth::user()->account;
$recurringHours = [];
for ($i=0; $i<24; $i++) {
if ($account->military_time) {
$format = 'H:i';
} else {
$format = 'g:i a';
}
$recurringHours[$i] = date($format, strtotime("{$i}:00"));
}
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'title' => trans("texts.invoice_settings"),
'section' => ACCOUNT_INVOICE_SETTINGS,
'recurringHours' => $recurringHours
];
return View::make("accounts.invoice_settings", $data);
}
2015-10-14 16:15:39 +02:00
private function showCompanyDetails()
{
2015-11-16 19:02:04 +01:00
// check that logo is less than the max file size
$account = Auth::user()->account;
if ($account->hasLogo()) {
$filename = $account->getLogoPath();
$bytes = File::size($filename);
if ($bytes > MAX_LOGO_FILE_SIZE * 1000) {
$bytes /= 1000;
Session::flash('warning', trans('texts.logo_too_large', ['size' => round($bytes) . 'KB']));
}
}
2015-10-14 16:15:39 +02:00
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'countries' => Cache::get('countries'),
'sizes' => Cache::get('sizes'),
'industries' => Cache::get('industries'),
'title' => trans('texts.company_details'),
];
return View::make('accounts.details', $data);
}
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
private function showUserDetails()
{
$oauthLoginUrls = [];
foreach (AuthService::$providers as $provider) {
$oauthLoginUrls[] = ['label' => $provider, 'url' => '/auth/' . strtolower($provider)];
}
2015-11-01 19:21:11 +01:00
2015-10-14 16:15:39 +02:00
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'title' => trans('texts.user_details'),
'user' => Auth::user(),
'oauthProviderName' => AuthService::getProviderName(Auth::user()->oauth_provider_id),
'oauthLoginUrls' => $oauthLoginUrls,
2015-11-01 19:21:11 +01:00
'referralCounts' => $this->referralRepository->getCounts(Auth::user()->id),
2015-10-14 16:15:39 +02:00
];
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
return View::make('accounts.user_details', $data);
}
2015-07-21 20:51:56 +02:00
2015-10-14 16:15:39 +02:00
private function showLocalization()
{
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'timezones' => Cache::get('timezones'),
'dateFormats' => Cache::get('dateFormats'),
'datetimeFormats' => Cache::get('datetimeFormats'),
'currencies' => Cache::get('currencies'),
'languages' => Cache::get('languages'),
'title' => trans('texts.localization'),
];
return View::make('accounts.localization', $data);
}
private function showOnlinePayments()
{
$account = Auth::user()->account;
$account->load('account_gateways');
$count = count($account->account_gateways);
if ($count == 0) {
return Redirect::to('gateways/create');
} else {
return View::make('accounts.payments', [
'showAdd' => $count < count(Gateway::$paymentTypes),
'title' => trans('texts.online_payments')
]);
}
}
private function showProducts()
{
2015-10-21 13:11:08 +02:00
$columns = ['product', 'description', 'unit_cost'];
if (Auth::user()->account->invoice_item_taxes) {
$columns[] = 'tax_rate';
}
$columns[] = 'action';
2015-10-14 16:15:39 +02:00
$data = [
'account' => Auth::user()->account,
'title' => trans('texts.product_library'),
2015-10-21 13:11:08 +02:00
'columns' => Utils::trans($columns),
2015-10-14 16:15:39 +02:00
];
return View::make('accounts.products', $data);
}
2015-10-21 13:11:08 +02:00
private function showTaxRates()
{
$data = [
'account' => Auth::user()->account,
'title' => trans('texts.tax_rates'),
'taxRates' => TaxRate::scope()->get(['id', 'name', 'rate']),
];
return View::make('accounts.tax_rates', $data);
}
2015-10-14 16:15:39 +02:00
private function showInvoiceDesign($section)
{
$account = Auth::user()->account->load('country');
$invoice = new stdClass();
$client = new stdClass();
$contact = new stdClass();
$invoiceItem = new stdClass();
$client->name = 'Sample Client';
$client->address1 = '';
$client->city = '';
$client->state = '';
$client->postal_code = '';
$client->work_phone = '';
$client->work_email = '';
2015-10-23 13:55:18 +02:00
$invoice->invoice_number = '0000';
2015-10-14 16:15:39 +02:00
$invoice->invoice_date = Utils::fromSqlDate(date('Y-m-d'));
$invoice->account = json_decode($account->toJson());
$invoice->amount = $invoice->balance = 100;
$invoice->terms = trim($account->invoice_terms);
$invoice->invoice_footer = trim($account->invoice_footer);
$contact->email = 'contact@gmail.com';
$client->contacts = [$contact];
$invoiceItem->cost = 100;
$invoiceItem->qty = 1;
$invoiceItem->notes = 'Notes';
$invoiceItem->product_key = 'Item';
$invoice->client = $client;
$invoice->invoice_items = [$invoiceItem];
$data['account'] = $account;
$data['invoice'] = $invoice;
$data['invoiceLabels'] = json_decode($account->invoice_labels) ?: [];
$data['title'] = trans('texts.invoice_design');
$data['invoiceDesigns'] = InvoiceDesign::getDesigns();
$data['section'] = $section;
$design = false;
foreach ($data['invoiceDesigns'] as $item) {
if ($item->id == $account->invoice_design_id) {
$design = $item->javascript;
break;
2015-03-16 22:45:25 +01:00
}
2015-10-14 16:15:39 +02:00
}
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
if ($section == ACCOUNT_CUSTOMIZE_DESIGN) {
$data['customDesign'] = ($account->custom_design && !$design) ? $account->custom_design : $design;
}
return View::make("accounts.{$section}", $data);
}
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
private function showTemplates()
{
$account = Auth::user()->account->load('country');
$data['account'] = $account;
$data['templates'] = [];
$data['defaultTemplates'] = [];
foreach ([ENTITY_INVOICE, ENTITY_QUOTE, ENTITY_PAYMENT, REMINDER1, REMINDER2, REMINDER3] as $type) {
$data['templates'][$type] = [
'subject' => $account->getEmailSubject($type),
'template' => $account->getEmailTemplate($type),
];
$data['defaultTemplates'][$type] = [
'subject' => $account->getDefaultEmailSubject($type),
'template' => $account->getDefaultEmailTemplate($type),
];
2015-03-16 22:45:25 +01:00
}
2015-10-14 16:15:39 +02:00
$data['emailFooter'] = $account->getEmailFooter();
$data['title'] = trans('texts.email_templates');
return View::make('accounts.templates_and_reminders', $data);
2015-03-16 22:45:25 +01:00
}
2015-10-14 16:15:39 +02:00
public function doSection($section = ACCOUNT_COMPANY_DETAILS)
2015-03-16 22:45:25 +01:00
{
2015-10-14 16:15:39 +02:00
if ($section === ACCOUNT_COMPANY_DETAILS) {
2015-03-16 22:45:25 +01:00
return AccountController::saveDetails();
2015-10-14 16:15:39 +02:00
} elseif ($section === ACCOUNT_USER_DETAILS) {
return AccountController::saveUserDetails();
} elseif ($section === ACCOUNT_LOCALIZATION) {
return AccountController::saveLocalization();
} elseif ($section === ACCOUNT_IMPORT_EXPORT) {
2015-03-16 22:45:25 +01:00
return AccountController::importFile();
2015-10-14 16:15:39 +02:00
} elseif ($section === ACCOUNT_MAP) {
2015-03-16 22:45:25 +01:00
return AccountController::mapFile();
2015-10-14 16:15:39 +02:00
} elseif ($section === ACCOUNT_NOTIFICATIONS) {
2015-03-16 22:45:25 +01:00
return AccountController::saveNotifications();
2015-10-14 16:15:39 +02:00
} elseif ($section === ACCOUNT_EXPORT) {
2015-03-16 22:45:25 +01:00
return AccountController::export();
2015-10-14 16:15:39 +02:00
} elseif ($section === ACCOUNT_INVOICE_SETTINGS) {
return AccountController::saveInvoiceSettings();
} elseif ($section === ACCOUNT_INVOICE_DESIGN) {
return AccountController::saveInvoiceDesign();
} elseif ($section === ACCOUNT_CUSTOMIZE_DESIGN) {
return AccountController::saveCustomizeDesign();
} elseif ($section === ACCOUNT_TEMPLATES_AND_REMINDERS) {
return AccountController::saveEmailTemplates();
} elseif ($section === ACCOUNT_PRODUCTS) {
2015-03-16 22:45:25 +01:00
return AccountController::saveProducts();
2015-10-21 13:11:08 +02:00
} elseif ($section === ACCOUNT_TAX_RATES) {
return AccountController::saveTaxRates();
2015-03-16 22:45:25 +01:00
}
}
2015-07-21 20:51:56 +02:00
private function saveCustomizeDesign() {
if (Auth::user()->account->isPro()) {
$account = Auth::user()->account;
$account->custom_design = Input::get('custom_design');
$account->invoice_design_id = CUSTOM_DESIGN;
$account->save();
Session::flash('message', trans('texts.updated_settings'));
}
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_CUSTOMIZE_DESIGN);
2015-07-21 20:51:56 +02:00
}
2015-03-16 22:45:25 +01:00
private function saveEmailTemplates()
{
if (Auth::user()->account->isPro()) {
$account = Auth::user()->account;
2015-09-17 21:01:06 +02:00
foreach ([ENTITY_INVOICE, ENTITY_QUOTE, ENTITY_PAYMENT, REMINDER1, REMINDER2, REMINDER3] as $type) {
$subjectField = "email_subject_{$type}";
2015-09-20 23:05:02 +02:00
$subject = Input::get($subjectField, $account->getEmailSubject($type));
$account->$subjectField = ($subject == $account->getDefaultEmailSubject($type) ? null : $subject);
2015-09-17 21:01:06 +02:00
$bodyField = "email_template_{$type}";
2015-09-20 23:05:02 +02:00
$body = Input::get($bodyField, $account->getEmailTemplate($type));
$account->$bodyField = ($body == $account->getDefaultEmailTemplate($type) ? null : $body);
2015-09-17 21:01:06 +02:00
}
foreach ([REMINDER1, REMINDER2, REMINDER3] as $type) {
$enableField = "enable_{$type}";
$account->$enableField = Input::get($enableField) ? true : false;
2015-09-20 23:05:02 +02:00
if ($account->$enableField) {
$numDaysField = "num_days_{$type}";
$account->$numDaysField = Input::get($numDaysField);
}
2015-09-17 21:01:06 +02:00
}
2015-03-16 22:45:25 +01:00
$account->save();
Session::flash('message', trans('texts.updated_settings'));
}
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_TEMPLATES_AND_REMINDERS);
2015-03-16 22:45:25 +01:00
}
2015-10-21 13:11:08 +02:00
private function saveTaxRates()
{
$account = Auth::user()->account;
$account->invoice_taxes = Input::get('invoice_taxes') ? true : false;
$account->invoice_item_taxes = Input::get('invoice_item_taxes') ? true : false;
$account->show_item_taxes = Input::get('show_item_taxes') ? true : false;
$account->default_tax_rate_id = Input::get('default_tax_rate_id');
$account->save();
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('settings/' . ACCOUNT_TAX_RATES);
}
2015-03-16 22:45:25 +01:00
private function saveProducts()
{
$account = Auth::user()->account;
$account->fill_products = Input::get('fill_products') ? true : false;
$account->update_products = Input::get('update_products') ? true : false;
$account->save();
Session::flash('message', trans('texts.updated_settings'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_PRODUCTS);
2015-03-16 22:45:25 +01:00
}
private function saveInvoiceSettings()
{
if (Auth::user()->account->isPro()) {
2015-10-25 12:08:11 +01:00
$rules = [
'invoice_number_pattern' => 'has_counter',
'quote_number_pattern' => 'has_counter',
];
$user = Auth::user();
$iframeURL = preg_replace('/[^a-zA-Z0-9_\-\:\/\.]/', '', substr(strtolower(Input::get('iframe_url')), 0, MAX_IFRAME_URL_LENGTH));
2015-10-13 09:11:44 +02:00
$iframeURL = rtrim($iframeURL, "/");
$subdomain = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', substr(strtolower(Input::get('subdomain')), 0, MAX_SUBDOMAIN_LENGTH));
if ($iframeURL || !$subdomain || in_array($subdomain, ['www', 'app', 'mail', 'admin', 'blog', 'user', 'contact', 'payment', 'payments', 'billing', 'invoice', 'business', 'owner'])) {
$subdomain = null;
}
if ($subdomain) {
$rules['subdomain'] = "unique:accounts,subdomain,{$user->account_id},id";
2015-03-16 22:45:25 +01:00
}
$validator = Validator::make(Input::all(), $rules);
2015-03-16 22:45:25 +01:00
if ($validator->fails()) {
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_INVOICE_SETTINGS)
->withErrors($validator)
->withInput();
2015-03-16 22:45:25 +01:00
} else {
$account = Auth::user()->account;
$account->subdomain = $subdomain;
$account->iframe_url = $iframeURL;
$account->custom_label1 = trim(Input::get('custom_label1'));
$account->custom_value1 = trim(Input::get('custom_value1'));
$account->custom_label2 = trim(Input::get('custom_label2'));
$account->custom_value2 = trim(Input::get('custom_value2'));
$account->custom_client_label1 = trim(Input::get('custom_client_label1'));
$account->custom_client_label2 = trim(Input::get('custom_client_label2'));
$account->custom_invoice_label1 = trim(Input::get('custom_invoice_label1'));
$account->custom_invoice_label2 = trim(Input::get('custom_invoice_label2'));
$account->custom_invoice_taxes1 = Input::get('custom_invoice_taxes1') ? true : false;
$account->custom_invoice_taxes2 = Input::get('custom_invoice_taxes2') ? true : false;
2015-10-11 16:41:09 +02:00
$account->custom_invoice_text_label1 = trim(Input::get('custom_invoice_text_label1'));
$account->custom_invoice_text_label2 = trim(Input::get('custom_invoice_text_label2'));
$account->invoice_number_counter = Input::get('invoice_number_counter');
$account->quote_number_prefix = Input::get('quote_number_prefix');
$account->share_counter = Input::get('share_counter') ? true : false;
$account->pdf_email_attachment = Input::get('pdf_email_attachment') ? true : false;
2015-10-29 15:42:05 +01:00
$account->invoice_terms = Input::get('invoice_terms');
$account->invoice_footer = Input::get('invoice_footer');
$account->quote_terms = Input::get('quote_terms');
2015-10-22 20:48:12 +02:00
if (Input::has('recurring_hour')) {
$account->recurring_hour = Input::get('recurring_hour');
}
if (!$account->share_counter) {
$account->quote_number_counter = Input::get('quote_number_counter');
}
2015-10-22 20:48:12 +02:00
if (Input::get('invoice_number_type') == 'prefix') {
$account->invoice_number_prefix = trim(Input::get('invoice_number_prefix'));
$account->invoice_number_pattern = null;
} else {
$account->invoice_number_pattern = trim(Input::get('invoice_number_pattern'));
$account->invoice_number_prefix = null;
}
if (Input::get('quote_number_type') == 'prefix') {
$account->quote_number_prefix = trim(Input::get('quote_number_prefix'));
$account->quote_number_pattern = null;
} else {
$account->quote_number_pattern = trim(Input::get('quote_number_pattern'));
$account->quote_number_prefix = null;
}
if (!$account->share_counter && $account->invoice_number_prefix == $account->quote_number_prefix) {
Session::flash('error', trans('texts.invalid_counter'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_INVOICE_SETTINGS)->withInput();
} else {
$account->save();
Session::flash('message', trans('texts.updated_settings'));
}
2015-03-16 22:45:25 +01:00
}
}
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_INVOICE_SETTINGS);
2015-03-16 22:45:25 +01:00
}
private function saveInvoiceDesign()
{
if (Auth::user()->account->isPro()) {
$account = Auth::user()->account;
$account->hide_quantity = Input::get('hide_quantity') ? true : false;
$account->hide_paid_to_date = Input::get('hide_paid_to_date') ? true : false;
$account->primary_color = Input::get('primary_color');
$account->secondary_color = Input::get('secondary_color');
2015-09-07 11:07:55 +02:00
$account->invoice_design_id = Input::get('invoice_design_id');
2015-05-22 09:22:24 +02:00
if (Input::has('font_size')) {
$account->font_size = intval(Input::get('font_size'));
}
2015-09-07 11:07:55 +02:00
$labels = [];
2015-10-16 07:32:02 +02:00
foreach (['item', 'description', 'unit_cost', 'quantity', 'line_total'] as $field) {
$labels[$field] = trim(Input::get("labels_{$field}"));
}
$account->invoice_labels = json_encode($labels);
2015-03-16 22:45:25 +01:00
$account->save();
Session::flash('message', trans('texts.updated_settings'));
}
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_INVOICE_DESIGN);
2015-03-16 22:45:25 +01:00
}
private function export()
{
$output = fopen('php://output', 'w') or Utils::fatalError();
header('Content-Type:application/csv');
header('Content-Disposition:attachment;filename=export.csv');
$clients = Client::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $clients->toArray());
2015-03-16 22:45:25 +01:00
$contacts = Contact::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $contacts->toArray());
2015-03-16 22:45:25 +01:00
$invoices = Invoice::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $invoices->toArray());
2015-03-16 22:45:25 +01:00
$invoiceItems = InvoiceItem::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $invoiceItems->toArray());
2015-03-16 22:45:25 +01:00
$payments = Payment::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $payments->toArray());
2015-03-16 22:45:25 +01:00
$credits = Credit::scope()->get();
2015-04-30 19:54:19 +02:00
Utils::exportData($output, $credits->toArray());
2015-03-16 22:45:25 +01:00
fclose($output);
exit;
}
private function importFile()
{
$data = Session::get('data');
Session::forget('data');
$map = Input::get('map');
$count = 0;
$hasHeaders = Input::get('header_checkbox');
2015-04-08 15:19:17 +02:00
$countries = Cache::get('countries');
2015-03-16 22:45:25 +01:00
$countryMap = [];
foreach ($countries as $country) {
$countryMap[strtolower($country->name)] = $country->id;
}
foreach ($data as $row) {
if ($hasHeaders) {
$hasHeaders = false;
continue;
}
2015-10-28 20:22:07 +01:00
$data = [
'contacts' => [[]]
];
2015-03-16 22:45:25 +01:00
foreach ($row as $index => $value) {
$field = $map[$index];
2015-10-28 20:22:07 +01:00
if ( ! $value = trim($value)) {
continue;
}
if ($field == Client::$fieldName) {
$data['name'] = $value;
} elseif ($field == Client::$fieldPhone) {
$data['work_phone'] = $value;
} elseif ($field == Client::$fieldAddress1) {
$data['address1'] = $value;
} elseif ($field == Client::$fieldAddress2) {
$data['address2'] = $value;
} elseif ($field == Client::$fieldCity) {
$data['city'] = $value;
} elseif ($field == Client::$fieldState) {
$data['state'] = $value;
} elseif ($field == Client::$fieldPostalCode) {
$data['postal_code'] = $value;
} elseif ($field == Client::$fieldCountry) {
2015-03-16 22:45:25 +01:00
$value = strtolower($value);
2015-10-28 20:22:07 +01:00
$data['country_id'] = isset($countryMap[$value]) ? $countryMap[$value] : null;
} elseif ($field == Client::$fieldNotes) {
$data['private_notes'] = $value;
} elseif ($field == Contact::$fieldFirstName) {
$data['contacts'][0]['first_name'] = $value;
} elseif ($field == Contact::$fieldLastName) {
$data['contacts'][0]['last_name'] = $value;
} elseif ($field == Contact::$fieldPhone) {
$data['contacts'][0]['phone'] = $value;
} elseif ($field == Contact::$fieldEmail) {
$data['contacts'][0]['email'] = strtolower($value);
2015-03-16 22:45:25 +01:00
}
}
2015-10-28 20:22:07 +01:00
$rules = [
'contacts' => 'valid_contacts',
];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
continue;
}
$clientRepository = new ClientRepository();
$clientRepository->save($data);
2015-10-28 20:22:07 +01:00
$count++;
2015-03-16 22:45:25 +01:00
}
$message = Utils::pluralize('created_client', $count);
Session::flash('message', $message);
return Redirect::to('clients');
}
private function mapFile()
{
$file = Input::file('file');
if ($file == null) {
Session::flash('error', trans('texts.select_file'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_IMPORT_EXPORT);
2015-03-16 22:45:25 +01:00
}
$name = $file->getRealPath();
2015-03-17 02:30:56 +01:00
require_once app_path().'/Includes/parsecsv.lib.php';
2015-03-16 22:45:25 +01:00
$csv = new parseCSV();
$csv->heading = false;
$csv->auto($name);
if (count($csv->data) + Client::scope()->count() > Auth::user()->getMaxNumClients()) {
$message = trans('texts.limit_clients', ['count' => Auth::user()->getMaxNumClients()]);
Session::flash('error', $message);
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_IMPORT_EXPORT);
2015-03-16 22:45:25 +01:00
}
Session::put('data', $csv->data);
$headers = false;
$hasHeaders = false;
$mapped = array();
$columns = array('',
Client::$fieldName,
Client::$fieldPhone,
Client::$fieldAddress1,
Client::$fieldAddress2,
Client::$fieldCity,
Client::$fieldState,
Client::$fieldPostalCode,
Client::$fieldCountry,
Client::$fieldNotes,
Contact::$fieldFirstName,
Contact::$fieldLastName,
Contact::$fieldPhone,
Contact::$fieldEmail,
);
if (count($csv->data) > 0) {
$headers = $csv->data[0];
foreach ($headers as $title) {
if (strpos(strtolower($title), 'name') > 0) {
$hasHeaders = true;
break;
}
}
for ($i = 0; $i<count($headers); $i++) {
$title = strtolower($headers[$i]);
$mapped[$i] = '';
if ($hasHeaders) {
$map = array(
'first' => Contact::$fieldFirstName,
'last' => Contact::$fieldLastName,
'email' => Contact::$fieldEmail,
'mobile' => Contact::$fieldPhone,
'phone' => Client::$fieldPhone,
'name|organization' => Client::$fieldName,
'street|address|address1' => Client::$fieldAddress1,
'street2|address2' => Client::$fieldAddress2,
'city' => Client::$fieldCity,
'state|province' => Client::$fieldState,
'zip|postal|code' => Client::$fieldPostalCode,
'country' => Client::$fieldCountry,
'note' => Client::$fieldNotes,
);
foreach ($map as $search => $column) {
foreach (explode("|", $search) as $string) {
if (strpos($title, 'sec') === 0) {
continue;
}
if (strpos($title, $string) !== false) {
$mapped[$i] = $column;
break(2);
}
}
}
}
}
}
$data = array(
'data' => $csv->data,
'headers' => $headers,
'hasHeaders' => $hasHeaders,
'columns' => $columns,
'mapped' => $mapped,
);
return View::make('accounts.import_map', $data);
}
private function saveNotifications()
{
$user = Auth::user();
$user->notify_sent = Input::get('notify_sent');
$user->notify_viewed = Input::get('notify_viewed');
$user->notify_paid = Input::get('notify_paid');
$user->notify_approved = Input::get('notify_approved');
2015-03-16 22:45:25 +01:00
$user->save();
Session::flash('message', trans('texts.updated_settings'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_NOTIFICATIONS);
2015-03-16 22:45:25 +01:00
}
private function saveDetails()
{
$rules = array(
'name' => 'required',
2015-11-16 19:02:04 +01:00
'logo' => 'sometimes|max:' . MAX_LOGO_FILE_SIZE . '|mimes:jpeg,gif,png',
2015-03-16 22:45:25 +01:00
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_COMPANY_DETAILS)
2015-03-16 22:45:25 +01:00
->withErrors($validator)
->withInput();
} else {
$account = Auth::user()->account;
$account->name = trim(Input::get('name'));
$account->id_number = trim(Input::get('id_number'));
$account->vat_number = trim(Input::get('vat_number'));
$account->work_email = trim(Input::get('work_email'));
$account->work_phone = trim(Input::get('work_phone'));
$account->address1 = trim(Input::get('address1'));
$account->address2 = trim(Input::get('address2'));
$account->city = trim(Input::get('city'));
$account->state = trim(Input::get('state'));
$account->postal_code = trim(Input::get('postal_code'));
$account->country_id = Input::get('country_id') ? Input::get('country_id') : null;
$account->size_id = Input::get('size_id') ? Input::get('size_id') : null;
$account->industry_id = Input::get('industry_id') ? Input::get('industry_id') : null;
2015-10-29 15:42:05 +01:00
$account->email_footer = Input::get('email_footer');
2015-03-16 22:45:25 +01:00
$account->save();
/* Logo image file */
if ($file = Input::file('logo')) {
$path = Input::file('logo')->getRealPath();
File::delete('logo/'.$account->account_key.'.jpg');
2015-07-26 22:05:38 +02:00
File::delete('logo/'.$account->account_key.'.png');
2015-03-16 22:45:25 +01:00
$mimeType = $file->getMimeType();
2015-07-26 22:05:38 +02:00
if ($mimeType == 'image/jpeg') {
2015-11-03 12:20:49 +01:00
$path = 'logo/' . $account->account_key . '.jpg';
2015-03-16 22:45:25 +01:00
$file->move('logo/', $account->account_key . '.jpg');
} else if ($mimeType == 'image/png') {
2015-11-03 12:20:49 +01:00
$path = 'logo/' . $account->account_key . '.png';
2015-07-24 16:13:17 +02:00
$file->move('logo/', $account->account_key . '.png');
2015-03-16 22:45:25 +01:00
} else {
2015-08-14 14:04:33 +02:00
if (extension_loaded('fileinfo')) {
$image = Image::make($path);
$image->resize(200, 120, function ($constraint) {
$constraint->aspectRatio();
});
2015-11-03 12:20:49 +01:00
$path = 'logo/'.$account->account_key.'.jpg';
2015-08-14 14:04:33 +02:00
Image::canvas($image->width(), $image->height(), '#FFFFFF')
2015-11-03 12:20:49 +01:00
->insert($image)->save($path);
2015-08-14 14:04:33 +02:00
} else {
Session::flash('warning', 'Warning: To support gifs the fileinfo PHP extension needs to be enabled.');
}
2015-03-16 22:45:25 +01:00
}
2015-11-03 12:20:49 +01:00
// make sure image isn't interlaced
if (extension_loaded('fileinfo')) {
$img = Image::make($path);
$img->interlace(false);
$img->save();
}
2015-03-16 22:45:25 +01:00
}
2015-11-16 19:02:04 +01:00
event(new UserSettingsChanged());
2015-03-16 22:45:25 +01:00
Session::flash('message', trans('texts.updated_settings'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_COMPANY_DETAILS);
2015-03-16 22:45:25 +01:00
}
}
2015-10-14 16:15:39 +02:00
private function saveUserDetails()
{
$user = Auth::user();
$rules = ['email' => 'email|required|unique:users,email,'.$user->id.',id'];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('settings/' . ACCOUNT_USER_DETAILS)
->withErrors($validator)
->withInput();
} else {
$user->first_name = trim(Input::get('first_name'));
$user->last_name = trim(Input::get('last_name'));
$user->username = trim(Input::get('email'));
$user->email = trim(strtolower(Input::get('email')));
$user->phone = trim(Input::get('phone'));
if (Utils::isNinja()) {
if (Input::get('referral_code') && !$user->referral_code) {
$user->referral_code = $this->accountRepo->getReferralCode();
}
}
if (Utils::isNinjaDev()) {
$user->dark_mode = Input::get('dark_mode') ? true : false;
}
$user->save();
2015-11-16 19:02:04 +01:00
event(new UserSettingsChanged());
2015-10-14 16:15:39 +02:00
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('settings/' . ACCOUNT_USER_DETAILS);
}
}
private function saveLocalization()
{
$account = Auth::user()->account;
$account->timezone_id = Input::get('timezone_id') ? Input::get('timezone_id') : null;
$account->date_format_id = Input::get('date_format_id') ? Input::get('date_format_id') : null;
$account->datetime_format_id = Input::get('datetime_format_id') ? Input::get('datetime_format_id') : null;
$account->currency_id = Input::get('currency_id') ? Input::get('currency_id') : 1; // US Dollar
$account->language_id = Input::get('language_id') ? Input::get('language_id') : 1; // English
$account->military_time = Input::get('military_time') ? true : false;
$account->save();
2015-11-16 19:02:04 +01:00
event(new UserSettingsChanged());
2015-10-14 16:15:39 +02:00
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('settings/' . ACCOUNT_LOCALIZATION);
}
2015-03-16 22:45:25 +01:00
public function removeLogo()
{
File::delete('logo/'.Auth::user()->account->account_key.'.jpg');
2015-07-26 22:05:38 +02:00
File::delete('logo/'.Auth::user()->account->account_key.'.png');
2015-03-16 22:45:25 +01:00
Session::flash('message', trans('texts.removed_logo'));
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_COMPANY_DETAILS);
2015-03-16 22:45:25 +01:00
}
public function checkEmail()
{
$email = User::withTrashed()->where('email', '=', Input::get('email'))->where('id', '<>', Auth::user()->id)->first();
if ($email) {
return "taken";
} else {
return "available";
}
}
public function submitSignup()
{
$rules = array(
'new_first_name' => 'required',
'new_last_name' => 'required',
'new_password' => 'required|min:6',
'new_email' => 'email|required|unique:users,email,'.Auth::user()->id.',id',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return '';
}
$user = Auth::user();
$user->first_name = trim(Input::get('new_first_name'));
$user->last_name = trim(Input::get('new_last_name'));
$user->email = trim(strtolower(Input::get('new_email')));
$user->username = $user->email;
2015-04-13 17:05:34 +02:00
$user->password = bcrypt(trim(Input::get('new_password')));
2015-03-16 22:45:25 +01:00
$user->registered = true;
2015-10-11 16:41:09 +02:00
$user->save();
2015-03-16 22:45:25 +01:00
if (Input::get('go_pro') == 'true') {
Session::set(REQUESTED_PRO_PLAN, true);
}
2015-10-11 16:41:09 +02:00
event(new UserSignedUp());
2015-03-16 22:45:25 +01:00
return "{$user->first_name} {$user->last_name}";
}
public function doRegister()
{
$affiliate = Affiliate::where('affiliate_key', '=', SELF_HOST_AFFILIATE_KEY)->first();
2015-06-12 10:39:53 +02:00
$email = trim(Input::get('email'));
2015-09-25 11:57:40 +02:00
if (!$email || $email == TEST_USERNAME) {
return RESULT_FAILURE;
2015-06-12 10:39:53 +02:00
}
2015-03-16 22:45:25 +01:00
$license = new License();
$license->first_name = Input::get('first_name');
$license->last_name = Input::get('last_name');
2015-06-12 10:39:53 +02:00
$license->email = $email;
2015-03-16 22:45:25 +01:00
$license->transaction_reference = Request::getClientIp();
$license->license_key = Utils::generateLicense();
$license->affiliate_id = $affiliate->id;
$license->product_id = PRODUCT_SELF_HOST;
$license->is_claimed = 1;
$license->save();
2015-09-25 11:57:40 +02:00
return RESULT_SUCCESS;
2015-03-16 22:45:25 +01:00
}
public function cancelAccount()
{
if ($reason = trim(Input::get('reason'))) {
$email = Auth::user()->email;
$name = Auth::user()->getDisplayName();
$data = [
'text' => $reason,
];
$this->userMailer->sendTo(CONTACT_EMAIL, $email, $name, 'Invoice Ninja Feedback [Canceled Account]', 'contact', $data);
}
2015-10-18 09:30:28 +02:00
$user = Auth::user();
2015-03-16 22:45:25 +01:00
$account = Auth::user()->account;
2015-10-18 09:30:28 +02:00
\Log::info("Canceled Account: {$account->name} - {$user->email}");
2015-07-07 22:08:16 +02:00
$this->accountRepo->unlinkAccount($account);
2015-03-16 22:45:25 +01:00
$account->forceDelete();
2015-03-29 14:37:42 +02:00
Auth::logout();
2015-06-16 21:35:35 +02:00
Session::flush();
2015-03-16 22:45:25 +01:00
return Redirect::to('/')->with('clearGuestKey', true);
}
2015-04-13 14:49:40 +02:00
public function resendConfirmation()
{
$user = Auth::user();
$this->userMailer->sendConfirmation($user);
2015-10-14 16:15:39 +02:00
return Redirect::to('/settings/' . ACCOUNT_COMPANY_DETAILS)->with('message', trans('texts.confirmation_resent'));
}
public function redirectLegacy($section, $subSection = false)
{
if ($section === 'details') {
$section = ACCOUNT_COMPANY_DETAILS;
} elseif ($section === 'payments') {
$section = ACCOUNT_PAYMENTS;
} elseif ($section === 'advanced_settings') {
$section = $subSection;
if ($section === 'token_management') {
$section = ACCOUNT_API_TOKENS;
}
}
if (!in_array($section, array_merge(Account::$basicSettings, Account::$advancedSettings))) {
$section = ACCOUNT_COMPANY_DETAILS;
}
return Redirect::to("/settings/$section/", 301);
2015-04-13 14:49:40 +02:00
}
2015-03-16 22:45:25 +01:00
}