1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/controllers/AccountController.php

568 lines
15 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2013-12-26 01:36:34 +01:00
use ninja\repositories\AccountRepository;
2013-11-26 13:45:07 +01:00
class AccountController extends \BaseController {
2013-12-26 01:36:34 +01:00
protected $accountRepo;
public function __construct(AccountRepository $accountRepo)
{
parent::__construct();
$this->accountRepo = $accountRepo;
}
2013-11-26 13:45:07 +01:00
public function getStarted()
{
2013-12-07 21:33:07 +01:00
if (Auth::check())
{
return Redirect::to('invoices/create');
}
2013-11-26 13:45:07 +01:00
2013-12-07 21:33:07 +01:00
$user = false;
2013-11-26 13:45:07 +01:00
$guestKey = Input::get('guest_key');
if ($guestKey)
{
2013-12-03 18:32:33 +01:00
//$user = User::where('password', '=', $guestKey)->firstOrFail();
2013-11-26 13:45:07 +01:00
$user = User::where('password', '=', $guestKey)->first();
2013-12-03 23:00:01 +01:00
if ($user && $user->registered)
2013-11-26 13:45:07 +01:00
{
exit;
}
}
if (!$user)
{
$account = new Account;
$account->ip = Request::getClientIp();
2014-01-13 20:22:43 +01:00
$account->account_key = str_random(RANDOM_KEY_LENGTH);
2013-11-26 13:45:07 +01:00
$account->save();
2014-01-13 20:22:43 +01:00
$random = str_random(RANDOM_KEY_LENGTH);
2013-11-26 13:45:07 +01:00
$user = new User;
2013-12-03 18:32:33 +01:00
$user->password = $random;
2014-01-15 15:01:24 +01:00
$user->password_confirmation = $random;
2014-01-16 22:12:46 +01:00
$user->username = $random;
2014-01-15 15:01:24 +01:00
$account->users()->save($user);
2013-12-07 19:45:00 +01:00
Session::forget(RECENTLY_VIEWED);
2013-11-26 13:45:07 +01:00
}
2013-12-03 23:00:01 +01:00
Auth::login($user, true);
2013-12-15 13:55:50 +01:00
Event::fire('user.login');
2014-01-14 12:52:56 +01:00
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices/create');
}
2013-12-26 01:36:34 +01:00
public function getSearchData()
{
$data = $this->accountRepo->getSearchData();
return Response::json($data);
}
2013-11-26 13:45:07 +01:00
public function showSection($section = ACCOUNT_DETAILS)
{
if ($section == ACCOUNT_DETAILS)
2013-11-28 22:10:01 +01:00
{
2014-01-06 19:03:00 +01:00
$data = [
'account' => Account::with('users')->findOrFail(Auth::user()->account_id),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
];
2013-11-28 22:10:01 +01:00
2014-01-06 19:03:00 +01:00
return View::make('accounts.details', $data);
2013-11-26 13:45:07 +01:00
}
else if ($section == ACCOUNT_SETTINGS)
{
2013-12-03 18:32:33 +01:00
$account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
$accountGateway = null;
2013-11-28 22:10:01 +01:00
$config = null;
if (count($account->account_gateways) > 0)
{
$accountGateway = $account->account_gateways[0];
2013-11-28 22:10:01 +01:00
$config = $accountGateway->config;
}
$data = [
'account' => $account,
'accountGateway' => $accountGateway,
2013-11-28 22:10:01 +01:00
'config' => json_decode($config),
2013-12-31 00:19:17 +01:00
'gateways' => Gateway::remember(DEFAULT_QUERY_CACHE)->get(),
'timezones' => Timezone::remember(DEFAULT_QUERY_CACHE)->orderBy('location')->get(),
'dateFormats' => DateFormat::remember(DEFAULT_QUERY_CACHE)->get(),
'datetimeFormats' => DatetimeFormat::remember(DEFAULT_QUERY_CACHE)->get(),
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
];
2013-11-26 13:45:07 +01:00
foreach ($data['gateways'] as $gateway)
2013-11-26 13:45:07 +01:00
{
$gateway->fields = Omnipay::create($gateway->provider)->getDefaultParameters();
2013-11-26 13:45:07 +01:00
if ($accountGateway && $accountGateway->gateway_id == $gateway->id)
{
$accountGateway->fields = $gateway->fields;
}
}
return View::make('accounts.settings', $data);
2013-11-26 13:45:07 +01:00
}
else if ($section == ACCOUNT_IMPORT)
{
return View::make('accounts.import');
}
else if ($section == ACCOUNT_EXPORT)
{
return View::make('accounts.export');
}
}
public function doSection($section = ACCOUNT_DETAILS)
{
if ($section == ACCOUNT_DETAILS)
{
return AccountController::saveDetails();
}
else if ($section == ACCOUNT_SETTINGS)
{
return AccountController::saveSettings();
}
else if ($section == ACCOUNT_IMPORT)
{
return AccountController::importFile();
}
else if ($section == ACCOUNT_MAP)
{
return AccountController::mapFile();
}
else if ($section == ACCOUNT_EXPORT)
{
return AccountController::export();
}
}
private function export()
{
$output = fopen("php://output",'w') or die("Can't open php://output");
header("Content-Type:application/csv");
header("Content-Disposition:attachment;filename=export.csv");
$clients = Client::where('account_id','=',Auth::user()->account_id)->get();
AccountController::exportData($output, $clients->toArray());
$contacts = DB::table('contacts')->whereIn('client_id', function($query){
$query->select('client_id')->from('clients')->where('account_id','=',Auth::user()->account_id);
})->get();
2013-12-07 21:33:07 +01:00
AccountController::exportData($output, Utils::toArray($contacts));
2013-11-26 13:45:07 +01:00
$invoices = Invoice::where('account_id','=',Auth::user()->account_id)->get();
AccountController::exportData($output, $invoices->toArray());
$invoiceItems = DB::table('invoice_items')->whereIn('invoice_id', function($query){
$query->select('invoice_id')->from('invoices')->where('account_id','=',Auth::user()->account_id);
})->get();
2013-12-07 21:33:07 +01:00
AccountController::exportData($output, Utils::toArray($invoiceItems));
2013-11-26 13:45:07 +01:00
$payments = Payment::where('account_id','=',Auth::user()->account_id)->get();
AccountController::exportData($output, $payments->toArray());
2014-01-09 22:38:18 +01:00
$credits = Credit::where('account_id','=',Auth::user()->account_id)->get();
AccountController::exportData($output, $credits->toArray());
2013-11-26 13:45:07 +01:00
fclose($output);
exit;
}
private function exportData($output, $data)
{
if (count($data) > 0)
{
fputcsv($output, array_keys($data[0]));
}
foreach($data as $record)
{
fputcsv($output, $record);
}
fwrite($output, "\n");
}
private function importFile()
{
$data = Session::get('data');
Session::forget('data');
$map = Input::get('map');
$count = 0;
$hasHeaders = Input::get('header_checkbox');
2014-01-22 10:11:33 +01:00
$countries = Country::remember(DEFAULT_QUERY_CACHE)->get();
2013-12-01 21:58:25 +01:00
$countryMap = [];
2014-01-22 10:11:33 +01:00
foreach ($countries as $country)
{
2013-12-01 21:58:25 +01:00
$countryMap[strtolower($country->name)] = $country->id;
}
2013-11-26 13:45:07 +01:00
foreach ($data as $row)
{
if ($hasHeaders)
{
$hasHeaders = false;
continue;
}
2013-12-05 16:23:24 +01:00
$client = Client::createNew();
$contact = Contact::createNew();
2014-01-22 10:11:33 +01:00
$contact->is_primary = true;
2013-11-26 13:45:07 +01:00
$count++;
foreach ($row as $index => $value)
{
$field = $map[$index];
2013-12-07 19:45:00 +01:00
$value = trim($value);
2013-11-26 13:45:07 +01:00
2014-01-22 10:11:33 +01:00
if ($field == Client::$fieldName && !$client->name)
2013-11-26 13:45:07 +01:00
{
$client->name = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldPhone && !$client->work_phone)
2013-11-26 13:45:07 +01:00
{
$client->work_phone = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldAddress1 && !$client->address1)
2013-11-26 13:45:07 +01:00
{
$client->address1 = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldAddress2 && !$client->address2)
2013-11-26 13:45:07 +01:00
{
$client->address2 = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldCity && !$client->city)
2013-11-26 13:45:07 +01:00
{
$client->city = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldState && !$client->state)
2013-11-26 13:45:07 +01:00
{
$client->state = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldPostalCode && !$client->postal_code)
2013-11-26 13:45:07 +01:00
{
$client->postal_code = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldCountry && !$client->country_id)
2013-12-01 21:58:25 +01:00
{
$value = strtolower($value);
$client->country_id = isset($countryMap[$value]) ? $countryMap[$value] : null;
}
2014-01-22 10:11:33 +01:00
else if ($field == Client::$fieldNotes && !$client->private_notes)
2013-11-26 13:45:07 +01:00
{
2014-01-22 10:11:33 +01:00
$client->private_notes = $value;
2013-11-26 13:45:07 +01:00
}
2014-01-22 10:11:33 +01:00
else if ($field == Contact::$fieldFirstName && !$contact->first_name)
2013-11-26 13:45:07 +01:00
{
$contact->first_name = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Contact::$fieldLastName && !$contact->last_name)
2013-11-26 13:45:07 +01:00
{
$contact->last_name = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Contact::$fieldPhone && !$contact->phone)
2013-11-26 13:45:07 +01:00
{
$contact->phone = $value;
}
2014-01-22 10:11:33 +01:00
else if ($field == Contact::$fieldEmail && !$contact->email)
2013-11-26 13:45:07 +01:00
{
2014-02-09 16:38:50 +01:00
$contact->email = strtolower($value);
2013-11-26 13:45:07 +01:00
}
}
$client->save();
2013-11-26 22:45:10 +01:00
$client->contacts()->save($contact);
2013-11-26 13:45:07 +01:00
}
2013-12-07 21:33:07 +01:00
$message = Utils::pluralize('Successfully created ? client', $count);
2013-11-26 13:45:07 +01:00
Session::flash('message', $message);
return Redirect::to('clients');
}
private function mapFile()
{
$file = Input::file('file');
$name = $file->getRealPath();
require_once(app_path().'/includes/parsecsv.lib.php');
$csv = new parseCSV();
$csv->heading = false;
$csv->auto($name);
2014-02-01 21:01:32 +01:00
if (count($csv->data) + Client::scope()->count() > MAX_NUM_CLIENTS)
{
Session::flash('error', "Sorry, this wll exceed the limit of " . MAX_NUM_CLIENTS . " clients");
return Redirect::to('account/import');
}
2013-11-26 13:45:07 +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,
2013-12-01 21:58:25 +01:00
Client::$fieldCountry,
2013-11-26 13:45:07 +01:00
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,
2013-12-05 21:25:20 +01:00
'name|organization' => Client::$fieldName,
2014-01-22 10:11:33 +01:00
'street|address|address1' => Client::$fieldAddress1,
'street2|address2' => Client::$fieldAddress2,
2013-11-26 13:45:07 +01:00
'city' => Client::$fieldCity,
2014-01-22 10:11:33 +01:00
'state|province' => Client::$fieldState,
2013-11-26 13:45:07 +01:00
'zip|postal|code' => Client::$fieldPostalCode,
2013-12-01 21:58:25 +01:00
'country' => Client::$fieldCountry,
2013-11-26 13:45:07 +01:00
'note' => Client::$fieldNotes,
);
foreach ($map as $search => $column)
{
foreach(explode("|", $search) as $string)
{
2014-01-22 10:11:33 +01:00
if (strpos($title, 'sec') === 0)
{
continue;
}
2013-11-26 13:45:07 +01:00
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 saveSettings()
2013-11-26 13:45:07 +01:00
{
$rules = array();
if ($gatewayId = Input::get('gateway_id'))
{
2013-12-03 18:32:33 +01:00
$gateway = Gateway::findOrFail($gatewayId);
2013-11-26 13:45:07 +01:00
$fields = Omnipay::create($gateway->provider)->getDefaultParameters();
2013-11-26 13:45:07 +01:00
foreach ($fields as $field => $details)
{
if (!in_array($field, ['testMode', 'developerMode', 'headerImageUrl', 'solutionType', 'landingPage']))
2013-11-26 13:45:07 +01:00
{
$rules[$gateway->id.'_'.$field] = 'required';
}
}
2013-11-26 13:45:07 +01:00
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::to('account/settings')
->withErrors($validator)
->withInput();
}
else
{
2013-12-04 17:20:14 +01:00
$account = Account::findOrFail(Auth::user()->account_id);
2013-11-26 13:45:07 +01:00
$account->account_gateways()->forceDelete();
2013-12-15 13:55:50 +01:00
$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;
2014-02-02 19:14:56 +01:00
$account->currency_id = Input::get('currency_id') ? Input::get('currency_id') : 1;
2013-12-15 13:55:50 +01:00
2013-12-04 17:20:14 +01:00
$account->invoice_terms = Input::get('invoice_terms');
2014-01-29 11:41:38 +01:00
$account->email_footer = Input::get('email_footer');
2013-12-04 17:20:14 +01:00
$account->save();
2013-12-25 22:34:42 +01:00
$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->save();
Event::fire('user.refresh');
if ($gatewayId)
{
2014-01-02 09:27:48 +01:00
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = $gatewayId;
$config = new stdClass;
foreach ($fields as $field => $details)
{
2013-12-07 19:45:00 +01:00
$config->$field = trim(Input::get($gateway->id.'_'.$field));
}
2014-01-09 00:22:56 +01:00
//dd(Input::all());
//dd($config);
$accountGateway->config = json_encode($config);
2013-11-26 13:45:07 +01:00
$account->account_gateways()->save($accountGateway);
}
Session::flash('message', 'Successfully updated settings');
2013-11-26 13:45:07 +01:00
return Redirect::to('account/settings');
}
}
private function saveDetails()
{
$rules = array(
'name' => 'required',
2014-01-06 19:03:00 +01:00
'email' => 'email|required|unique:users,email,' . Auth::user()->id . ',id'
2013-11-26 13:45:07 +01:00
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::to('account/details')
->withErrors($validator)
->withInput();
}
else
{
2013-12-03 18:32:33 +01:00
$account = Account::findOrFail(Auth::user()->account_id);
2013-12-07 19:45:00 +01:00
$account->name = trim(Input::get('name'));
$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'));
2013-12-02 13:22:29 +01:00
$account->country_id = Input::get('country_id') ? Input::get('country_id') : null;
2014-01-06 19:03:00 +01:00
$account->size_id = Input::get('size_id') ? Input::get('size_id') : null;
$account->industry_id = Input::get('industry_id') ? Input::get('industry_id') : null;
2013-11-26 13:45:07 +01:00
$account->save();
2014-01-15 15:01:24 +01:00
$user = Auth::user();
2013-12-07 19:45:00 +01:00
$user->first_name = trim(Input::get('first_name'));
$user->last_name = trim(Input::get('last_name'));
2014-01-15 15:01:24 +01:00
$user->username = trim(Input::get('email'));
2014-02-09 16:38:50 +01:00
$user->email = trim(strtolower(Input::get('email')));
2014-01-15 15:01:24 +01:00
$user->phone = trim(Input::get('phone'));
2014-01-16 22:12:46 +01:00
$user->save();
2013-12-02 13:22:29 +01:00
2013-11-26 13:45:07 +01:00
/* Logo image file */
if ($file = Input::file('logo'))
{
$path = Input::file('logo')->getRealPath();
2013-12-04 17:20:14 +01:00
File::delete('logo/' . $account->account_key . '.jpg');
2014-01-06 19:03:00 +01:00
Image::make($path)->resize(120, 80, true, false)->save('logo/' . $account->account_key . '.jpg');
2013-11-26 13:45:07 +01:00
}
Session::flash('message', 'Successfully updated details');
2013-11-26 13:45:07 +01:00
return Redirect::to('account/details');
}
}
2013-12-03 18:32:33 +01:00
public function checkEmail()
{
2014-01-06 19:03:00 +01:00
$email = User::withTrashed()->where('email', '=', Input::get('email'))->where('id', '<>', Auth::user()->id)->first();
2013-12-03 18:32:33 +01:00
2014-01-06 19:03:00 +01:00
if ($email)
{
2013-12-03 18:32:33 +01:00
return "taken";
2014-01-06 19:03:00 +01:00
}
2014-01-30 23:29:09 +01:00
else
{
2013-12-03 18:32:33 +01:00
return "available";
}
}
public function submitSignup()
{
$rules = array(
2014-01-01 00:50:13 +01:00
'new_first_name' => 'required',
'new_last_name' => 'required',
'new_password' => 'required|min:6',
2014-01-16 22:12:46 +01:00
'new_email' => 'email|required|unique:users,email,' . Auth::user()->id . ',id'
2013-12-03 18:32:33 +01:00
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
2014-01-30 23:29:09 +01:00
return '';
2013-12-03 18:32:33 +01:00
}
$user = Auth::user();
2013-12-24 22:27:36 +01:00
$user->first_name = trim(Input::get('new_first_name'));
$user->last_name = trim(Input::get('new_last_name'));
2014-02-09 16:38:50 +01:00
$user->email = trim(strtolower(Input::get('new_email')));
2013-12-24 22:27:36 +01:00
$user->password = trim(Input::get('new_password'));
2014-01-13 20:22:43 +01:00
$user->password_confirmation = trim(Input::get('new_password'));
2013-12-03 18:32:33 +01:00
$user->registered = true;
2014-01-16 22:12:46 +01:00
$user->amend();
2013-12-03 18:32:33 +01:00
$activities = Activity::scope()->get();
2014-01-01 00:50:13 +01:00
foreach ($activities as $activity)
{
2013-12-03 18:32:33 +01:00
$activity->message = str_replace('Guest', $user->getFullName(), $activity->message);
$activity->save();
}
2014-01-30 23:29:09 +01:00
return "{$user->first_name} {$user->last_name}";
2013-12-03 18:32:33 +01:00
}
2013-11-26 13:45:07 +01:00
}