mailer = $mailer; $this->invoiceRepo = $invoiceRepo; $this->clientRepo = $clientRepo; $this->taxRateRepo = $taxRateRepo; } public function index() { $data = [ 'title' => trans('texts.invoices'), 'entityType' => ENTITY_INVOICE, 'columns' => Utils::trans(['checkbox', 'invoice_number', 'client', 'invoice_date', 'invoice_total', 'balance_due', 'due_date', 'status', 'action']), ]; $recurringInvoices = Invoice::scope()->where('is_recurring', '=', true); if (Session::get('show_trash:invoice')) { $recurringInvoices->withTrashed(); } else { $recurringInvoices->join('clients', 'clients.id', '=', 'invoices.client_id') ->where('clients.deleted_at', '=', null); } if ($recurringInvoices->count() > 0) { $data['secEntityType'] = ENTITY_RECURRING_INVOICE; $data['secColumns'] = Utils::trans(['checkbox', 'frequency', 'client', 'start_date', 'end_date', 'invoice_total', 'action']); } return View::make('list', $data); } public function getDatatable($clientPublicId = null) { $accountId = Auth::user()->account_id; $search = Input::get('sSearch'); return $this->invoiceRepo->getDatatable($accountId, $clientPublicId, ENTITY_INVOICE, $search); } public function getRecurringDatatable($clientPublicId = null) { $query = $this->invoiceRepo->getRecurringInvoices(Auth::user()->account_id, $clientPublicId, Input::get('sSearch')); $table = Datatable::query($query); if (!$clientPublicId) { $table->addColumn('checkbox', function ($model) { return ''; }); } $table->addColumn('frequency', function ($model) { return link_to('invoices/'.$model->public_id, $model->frequency); }); if (!$clientPublicId) { $table->addColumn('client_name', function ($model) { return link_to('clients/'.$model->client_public_id, Utils::getClientDisplayName($model)); }); } return $table->addColumn('start_date', function ($model) { return Utils::fromSqlDate($model->start_date); }) ->addColumn('end_date', function ($model) { return Utils::fromSqlDate($model->end_date); }) ->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id); }) ->addColumn('dropdown', function ($model) { if ($model->is_deleted) { return '
'; } $str = ' '; }) ->make(); } public function view($invitationKey) { $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey); $invoice = $invitation->invoice; $client = $invoice->client; $account = $invoice->account; if (!$account->checkSubdomain(Request::server('HTTP_HOST'))) { app()->abort(404, trans('texts.invoice_not_found')); } if (!Input::has('phantomjs') && !Session::has($invitationKey) && (!Auth::check() || Auth::user()->account_id != $invoice->account_id)) { Activity::viewInvoice($invitation); Event::fire(new InvoiceViewed($invoice)); } Session::set($invitationKey, true); // track this invitation has been seen Session::set('invitation_key', $invitationKey); // track current invitation $account->loadLocalizationSettings($client); $invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date); $invoice->due_date = Utils::fromSqlDate($invoice->due_date); $invoice->is_pro = $account->isPro(); if ($invoice->invoice_design_id == CUSTOM_DESIGN) { $invoice->invoice_design->javascript = $account->custom_design; } else { $invoice->invoice_design->javascript = $invoice->invoice_design->pdfmake; } $contact = $invitation->contact; $contact->setVisible([ 'first_name', 'last_name', 'email', 'phone', ]); $paymentTypes = $this->getPaymentTypes($client, $invitation); $paymentURL = ''; if (count($paymentTypes)) { $paymentURL = $paymentTypes[0]['url']; if (!$account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) { $paymentURL = URL::to($paymentURL); } } $showApprove = $invoice->quote_invoice_id ? false : true; if ($invoice->due_date) { $showApprove = time() < strtotime($invoice->due_date); } $data = array( 'showApprove' => $showApprove, 'showBreadcrumbs' => false, 'hideLogo' => $account->isWhiteLabel(), 'hideHeader' => $account->isNinjaAccount(), 'invoice' => $invoice->hidePrivateFields(), 'invitation' => $invitation, 'invoiceLabels' => $account->getInvoiceLabels(), 'contact' => $contact, 'paymentTypes' => $paymentTypes, 'paymentURL' => $paymentURL, 'phantomjs' => Input::has('phantomjs'), ); return View::make('invoices.view', $data); } private function getPaymentTypes($client, $invitation) { $paymentTypes = []; $account = $client->account; if ($client->getGatewayToken()) { $paymentTypes[] = [ 'url' => URL::to("payment/{$invitation->invitation_key}/token"), 'label' => trans('texts.use_card_on_file') ]; } foreach(Gateway::$paymentTypes as $type) { if ($account->getGatewayByType($type)) { $typeLink = strtolower(str_replace('PAYMENT_TYPE_', '', $type)); $url = URL::to("/payment/{$invitation->invitation_key}/{$typeLink}"); // PayPal doesn't allow being run in an iframe so we need to open in new tab if ($type === PAYMENT_TYPE_PAYPAL && $account->iframe_url) { $url = 'javascript:window.open("'.$url.'", "_blank")'; } $paymentTypes[] = [ 'url' => $url, 'label' => trans('texts.'.strtolower($type)) ]; } } return $paymentTypes; } public function edit($publicId, $clone = false) { $invoice = Invoice::scope($publicId)->withTrashed()->with('invitations', 'account.country', 'client.contacts', 'client.country', 'invoice_items')->firstOrFail(); $entityType = $invoice->getEntityType(); $contactIds = DB::table('invitations') ->join('contacts', 'contacts.id', '=', 'invitations.contact_id') ->where('invitations.invoice_id', '=', $invoice->id) ->where('invitations.account_id', '=', Auth::user()->account_id) ->where('invitations.deleted_at', '=', null) ->select('contacts.public_id')->lists('public_id'); if ($clone) { $invoice->id = null; $invoice->invoice_number = Auth::user()->account->getNextInvoiceNumber($invoice->is_quote); $invoice->balance = $invoice->amount; $invoice->invoice_status_id = 0; $invoice->invoice_date = date_create()->format('Y-m-d'); $method = 'POST'; $url = "{$entityType}s"; } else { Utils::trackViewed($invoice->getDisplayName().' - '.$invoice->client->getDisplayName(), $invoice->getEntityType()); $method = 'PUT'; $url = "{$entityType}s/{$publicId}"; } $invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date); $invoice->due_date = Utils::fromSqlDate($invoice->due_date); $invoice->start_date = Utils::fromSqlDate($invoice->start_date); $invoice->end_date = Utils::fromSqlDate($invoice->end_date); $invoice->last_sent_date = Utils::fromSqlDate($invoice->last_sent_date); $invoice->is_pro = Auth::user()->isPro(); $actions = [ ['url' => 'javascript:onCloneClick()', 'label' => trans("texts.clone_{$entityType}")], ['url' => URL::to("{$entityType}s/{$entityType}_history/{$invoice->public_id}"), 'label' => trans("texts.view_history")], DropdownButton::DIVIDER ]; if ($invoice->invoice_status_id < INVOICE_STATUS_SENT && !$invoice->is_recurring) { $actions[] = ['url' => 'javascript:onMarkClick()', 'label' => trans("texts.mark_sent")]; } if ($entityType == ENTITY_QUOTE) { if ($invoice->quote_invoice_id) { $actions[] = ['url' => URL::to("invoices/{$invoice->quote_invoice_id}/edit"), 'label' => trans("texts.view_invoice")]; } else { $actions[] = ['url' => 'javascript:onConvertClick()', 'label' => trans("texts.convert_to_invoice")]; } } elseif ($entityType == ENTITY_INVOICE) { if ($invoice->quote_id) { $actions[] = ['url' => URL::to("quotes/{$invoice->quote_id}/edit"), 'label' => trans("texts.view_quote")]; } if (!$invoice->is_recurring && $invoice->balance > 0) { $actions[] = ['url' => 'javascript:onPaymentClick()', 'label' => trans('texts.enter_payment')]; } } if (count($actions) > 3) { $actions[] = DropdownButton::DIVIDER; } $actions[] = ['url' => 'javascript:onArchiveClick()', 'label' => trans("texts.archive_{$entityType}")]; $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans("texts.delete_{$entityType}")]; $lastSent = ($invoice->is_recurring && $invoice->last_sent_date) ? $invoice->recurring_invoices->last() : null; $data = array( 'entityType' => $entityType, 'showBreadcrumbs' => $clone, 'invoice' => $invoice, 'data' => false, 'method' => $method, 'invitationContactIds' => $contactIds, 'url' => $url, 'title' => trans("texts.edit_{$entityType}"), 'client' => $invoice->client, 'isRecurring' => $invoice->is_recurring, 'actions' => $actions, 'lastSent' => $lastSent); $data = array_merge($data, self::getViewModel()); if ($clone) { $data['formIsChanged'] = true; } // Set the invitation data on the client's contacts if (!$clone) { $clients = $data['clients']; foreach ($clients as $client) { if ($client->id != $invoice->client->id) { continue; } foreach ($invoice->invitations as $invitation) { foreach ($client->contacts as $contact) { if ($invitation->contact_id == $contact->id) { $contact->email_error = $invitation->email_error; $contact->invitation_link = $invitation->getLink(); $contact->invitation_viewed = $invitation->viewed_date; $contact->invitation_status = $contact->email_error ? false : $invitation->getStatus(); } } } break; } } return View::make('invoices.edit', $data); } public function create($clientPublicId = 0, $isRecurring = false) { $client = null; $invoiceNumber = $isRecurring ? microtime(true) : Auth::user()->account->getNextInvoiceNumber(); if ($clientPublicId) { $client = Client::scope($clientPublicId)->firstOrFail(); } $data = array( 'entityType' => ENTITY_INVOICE, 'invoice' => null, 'data' => Input::old('data'), 'invoiceNumber' => $invoiceNumber, 'method' => 'POST', 'url' => 'invoices', 'title' => trans('texts.new_invoice'), 'isRecurring' => $isRecurring, 'client' => $client); $data = array_merge($data, self::getViewModel()); return View::make('invoices.edit', $data); } public function createRecurring($clientPublicId = 0) { return self::create($clientPublicId, true); } private static function getViewModel() { $recurringHelp = ''; foreach (preg_split("/((\r?\n)|(\r\n?))/", trans('texts.recurring_help')) as $line) { $parts = explode("=>", $line); if (count($parts) > 1) { $line = $parts[0].' => '.Utils::processVariables($parts[0]); $recurringHelp .= '