accountRepo = $accountRepo; $this->userMailer = $userMailer; $this->contactMailer = $contactMailer; } public function getStarted() { if (Auth::check()) { return Redirect::to('invoices/create'); } $user = false; $guestKey = Input::get('guest_key'); if ($guestKey) { $user = User::where('password', '=', $guestKey)->first(); if ($user && $user->registered) { exit; } } if (!$user) { $account = $this->accountRepo->create(); $user = $account->users()->first(); Session::forget(RECENTLY_VIEWED); } Auth::login($user, true); Event::fire('user.login'); return Redirect::to('invoices/create'); } public function enableProPlan() { $invoice = $this->accountRepo->enableProPlan(); if ($invoice) { $this->contactMailer->sendInvoice($invoice); } return RESULT_SUCCESS; } public function setTrashVisible($entityType, $visible) { Session::put('show_trash', $visible == 'true'); return Redirect::to("{$entityType}s"); } public function getSearchData() { $data = $this->accountRepo->getSearchData(); return Response::json($data); } public function showSection($section = ACCOUNT_DETAILS) { if ($section == ACCOUNT_DETAILS) { $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('id')->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(), 'languages' => Language::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), ]; return View::make('accounts.details', $data); } else if ($section == ACCOUNT_PAYMENTS) { $account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id); $accountGateway = null; $config = null; $configFields = null; if (count($account->account_gateways) > 0) { $accountGateway = $account->account_gateways[0]; $config = $accountGateway->config; $configFields = json_decode($config); foreach($configFields as $configField => $value) { $configFields->$configField = str_repeat('*', strlen($value)); } } $recommendedGateways = Gateway::remember(DEFAULT_QUERY_CACHE) ->where('recommended', '=', '1') ->orderBy('sort_order') ->get(); $recommendedGatewayArray = array(); foreach($recommendedGateways as $recommendedGateway) { $arrayItem = array( 'value' => $recommendedGateway->id, 'other' => 'false', 'data-imageUrl' => $recommendedGateway->getLogoUrl(), 'data-siteUrl' => $recommendedGateway->site_url ); $recommendedGatewayArray[$recommendedGateway->name] = $arrayItem; } $otherItem = array( 'value' => 1000000, 'other' => 'true', 'data-imageUrl' => '', 'data-siteUrl' => '' ); $recommendedGatewayArray['Other Options'] = $otherItem; $data = [ 'account' => $account, 'accountGateway' => $accountGateway, 'config' => $configFields, 'gateways' => Gateway::remember(DEFAULT_QUERY_CACHE) ->orderBy('name') ->get(), 'dropdownGateways' => Gateway::remember(DEFAULT_QUERY_CACHE) ->where('recommended', '=', '0') ->orderBy('name') ->get(), 'recommendedGateways' => $recommendedGatewayArray, ]; foreach ($data['gateways'] as $gateway) { $paymentLibrary = $gateway->paymentlibrary; $gateway->fields = $gateway->getFields(); if ($accountGateway && $accountGateway->gateway_id == $gateway->id) { $accountGateway->fields = $gateway->fields; } } return View::make('accounts.payments', $data); } else if ($section == ACCOUNT_NOTIFICATIONS) { $data = [ 'account' => Account::with('users')->findOrFail(Auth::user()->account_id), ]; return View::make('accounts.notifications', $data); } else if ($section == ACCOUNT_IMPORT_EXPORT) { return View::make('accounts.import_export'); } else if ($section == ACCOUNT_CUSTOM_FIELDS) { $data = [ 'account' => Auth::user()->account ]; return View::make('accounts.custom_fields', $data); } else if ($section == ACCOUNT_PRODUCTS) { $data = [ 'account' => Auth::user()->account ]; return View::make('accounts.products', $data); } } public function getProducts() { $query = DB::table('products') ->where('products.account_id', '=', Auth::user()->account_id) ->where('products.deleted_at', '=', null) ->select('products.public_id', 'products.product_key', 'products.notes', 'products.cost'); return Datatable::query($query) ->addColumn('product_key', function($model) { return link_to('company/products/' . $model->public_id . '/edit', $model->product_key); }) ->addColumn('notes', function($model) { return $model->notes; }) ->addColumn('cost', function($model) { return Utils::formatMoney($model->cost); }) ->addColumn('dropdown', function($model) { return '
'; }) ->make(); } public function doSection($section = ACCOUNT_DETAILS) { if ($section == ACCOUNT_DETAILS) { return AccountController::saveDetails(); } else if ($section == ACCOUNT_PAYMENTS) { return AccountController::savePayments(); } else if ($section == ACCOUNT_IMPORT_EXPORT) { return AccountController::importFile(); } else if ($section == ACCOUNT_MAP) { return AccountController::mapFile(); } else if ($section == ACCOUNT_NOTIFICATIONS) { return AccountController::saveNotifications(); } else if ($section == ACCOUNT_EXPORT) { return AccountController::export(); } else if ($section == ACCOUNT_CUSTOM_FIELDS) { return AccountController::saveCustomFields(); } else if ($section == ACCOUNT_PRODUCTS) { return AccountController::saveProducts(); } } public function showProduct($productPublicId) { $data = [ 'product' => Product::scope($productPublicId)->firstOrFail(), 'url' => 'company/products/' . $productPublicId, 'title' => trans('texts.edit_product') ]; return View::make('accounts.product', $data); } public function createProduct() { $data = [ 'product' => null, 'url' => 'company/products/', 'title' => trans('texts.create_product') ]; return View::make('accounts.product', $data); } public function saveProduct($productPublicId = false) { if ($productPublicId) { $product = Product::scope($productPublicId)->firstOrFail(); } else { $product = Product::createNew(); } $product->product_key = trim(Input::get('product_key')); $product->notes = trim(Input::get('notes')); $product->cost = trim(Input::get('cost')); $product->save(); $message = $productPublicId ? trans('texts.updated_product') : trans('texts.created_product'); Session::flash('message', $message); return Redirect::to('company/products'); } public function archiveProduct($productPublicId) { $product = Product::scope($productPublicId)->firstOrFail(); $product->delete(); Session::flash('message', trans('texts.archived_product')); return Redirect::to('company/products'); } 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')); return Redirect::to('company/products'); } private function saveCustomFields() { $account = Auth::user()->account; $account->custom_label1 = Input::get('custom_label1'); $account->custom_value1 = Input::get('custom_value1'); $account->custom_label2 = Input::get('custom_label2'); $account->custom_value2 = Input::get('custom_value2'); $account->custom_client_label1 = Input::get('custom_client_label1'); $account->custom_client_label2 = Input::get('custom_client_label2'); $account->save(); Session::flash('message', trans('texts.updated_settings')); return Redirect::to('company/custom_fields'); } 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(); AccountController::exportData($output, $clients->toArray()); $contacts = Contact::scope()->get(); AccountController::exportData($output, $contacts->toArray()); $invoices = Invoice::scope()->get(); AccountController::exportData($output, $invoices->toArray()); $invoiceItems = InvoiceItem::scope()->get(); AccountController::exportData($output, $invoiceItems->toArray()); $payments = Payment::scope()->get(); AccountController::exportData($output, $payments->toArray()); $credits = Credit::scope()->get(); AccountController::exportData($output, $credits->toArray()); 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'); $countries = Country::remember(DEFAULT_QUERY_CACHE)->get(); $countryMap = []; foreach ($countries as $country) { $countryMap[strtolower($country->name)] = $country->id; } foreach ($data as $row) { if ($hasHeaders) { $hasHeaders = false; continue; } $client = Client::createNew(); $contact = Contact::createNew(); $contact->is_primary = true; $count++; foreach ($row as $index => $value) { $field = $map[$index]; $value = trim($value); if ($field == Client::$fieldName && !$client->name) { $client->name = $value; } else if ($field == Client::$fieldPhone && !$client->work_phone) { $client->work_phone = $value; } else if ($field == Client::$fieldAddress1 && !$client->address1) { $client->address1 = $value; } else if ($field == Client::$fieldAddress2 && !$client->address2) { $client->address2 = $value; } else if ($field == Client::$fieldCity && !$client->city) { $client->city = $value; } else if ($field == Client::$fieldState && !$client->state) { $client->state = $value; } else if ($field == Client::$fieldPostalCode && !$client->postal_code) { $client->postal_code = $value; } else if ($field == Client::$fieldCountry && !$client->country_id) { $value = strtolower($value); $client->country_id = isset($countryMap[$value]) ? $countryMap[$value] : null; } else if ($field == Client::$fieldNotes && !$client->private_notes) { $client->private_notes = $value; } else if ($field == Contact::$fieldFirstName && !$contact->first_name) { $contact->first_name = $value; } else if ($field == Contact::$fieldLastName && !$contact->last_name) { $contact->last_name = $value; } else if ($field == Contact::$fieldPhone && !$contact->phone) { $contact->phone = $value; } else if ($field == Contact::$fieldEmail && !$contact->email) { $contact->email = strtolower($value); } } $client->save(); $client->contacts()->save($contact); Activity::createClient($client); } $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')); return Redirect::to('company/import_export'); } $name = $file->getRealPath(); require_once(app_path().'/includes/parsecsv.lib.php'); $csv = new parseCSV(); $csv->heading = false; $csv->auto($name); if (count($csv->data) + Client::scope()->count() > Auth::user()->getMaxNumClients()) { $message = Utils::pluralize('limit_clients', Auth::user()->getMaxNumClients()); Session::flash('error', $message); return Redirect::to('company/import_export'); } 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