private static function getViewModel()
 {
     return ['entityType' => ENTITY_QUOTE, 'account' => Auth::user()->account, 'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')), 'countries' => Cache::get('countries'), 'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(), 'taxRates' => TaxRate::scope()->orderBy('name')->get(), 'currencies' => Cache::get('currencies'), 'sizes' => Cache::get('sizes'), 'paymentTerms' => Cache::get('paymentTerms'), 'industries' => Cache::get('industries'), 'invoiceDesigns' => InvoiceDesign::availableDesigns(), 'invoiceLabels' => Auth::user()->account->getInvoiceLabels()];
 }
 public function invoiceHistory($publicId)
 {
     $invoice = Invoice::withTrashed()->scope($publicId)->firstOrFail();
     $invoice->load('user', 'invoice_items', 'account.country', 'client.contacts', 'client.country');
     $invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
     $invoice->due_date = Utils::fromSqlDate($invoice->due_date);
     $invoice->is_pro = Auth::user()->isPro();
     $invoice->is_quote = intval($invoice->is_quote);
     $activityTypeId = $invoice->is_quote ? ACTIVITY_TYPE_UPDATE_QUOTE : ACTIVITY_TYPE_UPDATE_INVOICE;
     $activities = Activity::scope(false, $invoice->account_id)->where('activity_type_id', '=', $activityTypeId)->where('invoice_id', '=', $invoice->id)->orderBy('id', 'desc')->get(['id', 'created_at', 'user_id', 'json_backup', 'message']);
     $versionsJson = [];
     $versionsSelect = [];
     $lastId = false;
     foreach ($activities as $activity) {
         $backup = json_decode($activity->json_backup);
         $backup->invoice_date = Utils::fromSqlDate($backup->invoice_date);
         $backup->due_date = Utils::fromSqlDate($backup->due_date);
         $backup->is_pro = Auth::user()->isPro();
         $backup->is_quote = isset($backup->is_quote) && intval($backup->is_quote);
         $backup->account = $invoice->account->toArray();
         $versionsJson[$activity->id] = $backup;
         $key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . Utils::decodeActivity($activity->message);
         $versionsSelect[$lastId ? $lastId : 0] = $key;
         $lastId = $activity->id;
     }
     $versionsSelect[$lastId] = Utils::timestampToDateTimeString(strtotime($invoice->created_at)) . ' - ' . $invoice->user->getDisplayName();
     $data = ['invoice' => $invoice, 'versionsJson' => json_encode($versionsJson), 'versionsSelect' => $versionsSelect, 'invoiceDesigns' => InvoiceDesign::availableDesigns()];
     return View::make('invoices.history', $data);
 }
 public function showSection($section = ACCOUNT_DETAILS, $subSection = false)
 {
     if ($section == ACCOUNT_DETAILS) {
         $data = ['account' => Account::with('users')->findOrFail(Auth::user()->account_id), 'countries' => Cache::get('countries'), 'sizes' => Cache::get('sizes'), 'industries' => Cache::get('industries'), 'timezones' => Cache::get('timezones'), 'dateFormats' => Cache::get('dateFormats'), 'datetimeFormats' => Cache::get('datetimeFormats'), 'currencies' => Cache::get('currencies'), 'languages' => Cache::get('languages'), 'showUser' => Auth::user()->id === Auth::user()->account->users()->first()->id];
         return View::make('accounts.details', $data);
     } elseif ($section == ACCOUNT_PAYMENTS) {
         $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 < 3]);
         }
     } elseif ($section == ACCOUNT_NOTIFICATIONS) {
         $data = ['account' => Account::with('users')->findOrFail(Auth::user()->account_id)];
         return View::make('accounts.notifications', $data);
     } elseif ($section == ACCOUNT_IMPORT_EXPORT) {
         return View::make('accounts.import_export');
     } elseif ($section == ACCOUNT_ADVANCED_SETTINGS) {
         $account = Auth::user()->account;
         $data = ['account' => $account, 'feature' => $subSection];
         if ($subSection == ACCOUNT_INVOICE_DESIGN) {
             $invoice = new stdClass();
             $client = new stdClass();
             $invoiceItem = new stdClass();
             $client->name = 'Sample Client';
             $client->address1 = '';
             $client->city = '';
             $client->state = '';
             $client->postal_code = '';
             $client->work_phone = '';
             $client->work_email = '';
             $invoice->invoice_number = Auth::user()->account->getNextInvoiceNumber();
             $invoice->invoice_date = date_create()->format('Y-m-d');
             $invoice->account = json_decode(Auth::user()->account->toJson());
             $invoice->amount = $invoice->balance = 100;
             $invoiceItem->cost = 100;
             $invoiceItem->qty = 1;
             $invoiceItem->notes = 'Notes';
             $invoiceItem->product_key = 'Item';
             $invoice->client = $client;
             $invoice->invoice_items = [$invoiceItem];
             $data['invoice'] = $invoice;
             $data['invoiceDesigns'] = InvoiceDesign::availableDesigns();
         } else {
             if ($subSection == ACCOUNT_EMAIL_TEMPLATES) {
                 $data['invoiceEmail'] = $account->getEmailTemplate(ENTITY_INVOICE);
                 $data['quoteEmail'] = $account->getEmailTemplate(ENTITY_QUOTE);
                 $data['paymentEmail'] = $account->getEmailTemplate(ENTITY_PAYMENT);
                 $data['emailFooter'] = $account->getEmailFooter();
             }
         }
         return View::make("accounts.{$subSection}", $data);
     } elseif ($section == ACCOUNT_PRODUCTS) {
         $data = ['account' => Auth::user()->account];
         return View::make('accounts.products', $data);
     }
 }
 public function showSection($section = ACCOUNT_DETAILS, $subSection = false)
 {
     if ($section == ACCOUNT_DETAILS) {
         $data = ['account' => Account::with('users')->findOrFail(Auth::user()->account_id), 'countries' => Cache::get('countries'), 'sizes' => Cache::get('sizes'), 'industries' => Cache::get('industries'), 'timezones' => Cache::get('timezones'), 'dateFormats' => Cache::get('dateFormats'), 'datetimeFormats' => Cache::get('datetimeFormats'), 'currencies' => Cache::get('currencies'), 'languages' => Cache::get('languages'), 'showUser' => Auth::user()->id === Auth::user()->account->users()->first()->id, 'title' => trans('texts.company_details')];
         return View::make('accounts.details', $data);
     } elseif ($section == ACCOUNT_PAYMENTS) {
         $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 < 3, 'title' => trans('texts.online_payments')]);
         }
     } elseif ($section == ACCOUNT_NOTIFICATIONS) {
         $data = ['account' => Account::with('users')->findOrFail(Auth::user()->account_id), 'title' => trans('texts.notifications')];
         return View::make('accounts.notifications', $data);
     } elseif ($section == ACCOUNT_IMPORT_EXPORT) {
         return View::make('accounts.import_export', ['title' => trans('texts.import_export')]);
     } elseif ($section == ACCOUNT_ADVANCED_SETTINGS) {
         $account = Auth::user()->account->load('country');
         $data = ['account' => $account, 'feature' => $subSection, 'title' => trans('texts.invoice_settings')];
         if ($subSection == ACCOUNT_INVOICE_DESIGN || $subSection == ACCOUNT_CUSTOMIZE_DESIGN) {
             $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 = '';
             $invoice->invoice_number = $account->getNextInvoiceNumber();
             $invoice->invoice_date = date_create()->format('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 = '*****@*****.**';
             $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::availableDesigns($subSection == ACCOUNT_CUSTOMIZE_DESIGN);
             $design = false;
             foreach ($data['invoiceDesigns'] as $item) {
                 if ($item->id == $account->invoice_design_id) {
                     $design = $item->javascript;
                     break;
                 }
             }
             if ($subSection == ACCOUNT_CUSTOMIZE_DESIGN) {
                 $data['customDesign'] = $account->custom_design && !$design ? $account->custom_design : $design;
             }
         } else {
             if ($subSection == ACCOUNT_EMAIL_TEMPLATES) {
                 $data['invoiceEmail'] = $account->getEmailTemplate(ENTITY_INVOICE);
                 $data['quoteEmail'] = $account->getEmailTemplate(ENTITY_QUOTE);
                 $data['paymentEmail'] = $account->getEmailTemplate(ENTITY_PAYMENT);
                 $data['emailFooter'] = $account->getEmailFooter();
                 $data['title'] = trans('texts.email_templates');
             } else {
                 if ($subSection == ACCOUNT_USER_MANAGEMENT) {
                     $data['title'] = trans('texts.users_and_tokens');
                 }
             }
         }
         return View::make("accounts.{$subSection}", $data);
     } elseif ($section == ACCOUNT_PRODUCTS) {
         $data = ['account' => Auth::user()->account, 'title' => trans('texts.product_library')];
         return View::make('accounts.products', $data);
     }
 }