2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-24 09:26:57 +01:00
|
|
|
use Auth;
|
2015-03-26 07:24:02 +01:00
|
|
|
use Session;
|
2015-03-24 09:26:57 +01:00
|
|
|
use Utils;
|
2015-03-26 07:24:02 +01:00
|
|
|
use View;
|
2015-03-30 21:45:10 +02:00
|
|
|
use Input;
|
|
|
|
use Cache;
|
2015-03-31 11:38:24 +02:00
|
|
|
use Redirect;
|
|
|
|
use DB;
|
2015-03-31 19:42:37 +02:00
|
|
|
use Event;
|
2015-04-05 21:15:37 +02:00
|
|
|
use URL;
|
2015-04-21 22:09:45 +02:00
|
|
|
use Datatable;
|
2015-04-28 22:13:52 +02:00
|
|
|
use Request;
|
2015-06-04 22:53:58 +02:00
|
|
|
use DropdownButton;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Models\Invoice;
|
2015-03-31 11:38:24 +02:00
|
|
|
use App\Models\Invitation;
|
2015-03-30 21:45:10 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Models\Country;
|
|
|
|
use App\Models\TaxRate;
|
|
|
|
use App\Models\Currency;
|
|
|
|
use App\Models\Size;
|
|
|
|
use App\Models\Industry;
|
|
|
|
use App\Models\PaymentTerm;
|
|
|
|
use App\Models\InvoiceDesign;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Models\AccountGateway;
|
|
|
|
use App\Models\Activity;
|
2015-06-01 16:34:00 +02:00
|
|
|
use App\Models\Gateway;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Mailers\ContactMailer as Mailer;
|
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
|
|
|
use App\Ninja\Repositories\TaxRateRepository;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Events\InvoiceViewed;
|
2015-03-17 02:30:56 +01:00
|
|
|
|
2015-03-24 09:21:12 +01:00
|
|
|
class InvoiceController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
protected $mailer;
|
|
|
|
protected $invoiceRepo;
|
|
|
|
protected $clientRepo;
|
|
|
|
protected $taxRateRepo;
|
|
|
|
|
|
|
|
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, TaxRateRepository $taxRateRepo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->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']),
|
|
|
|
];
|
|
|
|
|
2015-08-03 09:15:58 +02:00
|
|
|
$recurringInvoices = Invoice::scope()->where('is_recurring', '=', true);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if (Session::get('show_trash:invoice')) {
|
|
|
|
$recurringInvoices->withTrashed();
|
2015-06-03 19:55:48 +02:00
|
|
|
} else {
|
|
|
|
$recurringInvoices->join('clients', 'clients.id', '=', 'invoices.client_id')
|
|
|
|
->where('clients.deleted_at', '=', null);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 '<input type="checkbox" name="ids[]" value="'.$model->public_id.'" '.Utils::getEntityRowClass($model).'>'; });
|
|
|
|
}
|
|
|
|
|
|
|
|
$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 '<div style="height:38px"/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = '<div class="btn-group tr-action" style="visibility:hidden;">
|
|
|
|
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
|
|
|
|
'.trans('texts.select').' <span class="caret"></span>
|
|
|
|
</button>
|
|
|
|
<ul class="dropdown-menu" role="menu">';
|
|
|
|
|
|
|
|
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
|
|
|
|
$str .= '<li><a href="'.URL::to('invoices/'.$model->public_id.'/edit').'">'.trans('texts.edit_invoice').'</a></li>
|
|
|
|
<li class="divider"></li>
|
|
|
|
<li><a href="javascript:archiveEntity('.$model->public_id.')">'.trans('texts.archive_invoice').'</a></li>
|
|
|
|
<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_invoice').'</a></li>';
|
|
|
|
} else {
|
|
|
|
$str .= '<li><a href="javascript:restoreEntity('.$model->public_id.')">'.trans('texts.restore_invoice').'</a></li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str.'</ul>
|
|
|
|
</div>';
|
|
|
|
|
|
|
|
})
|
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view($invitationKey)
|
|
|
|
{
|
2015-10-13 09:11:44 +02:00
|
|
|
$invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey);
|
2015-09-17 21:01:06 +02:00
|
|
|
$invoice = $invitation->invoice;
|
2015-03-16 22:45:25 +01:00
|
|
|
$client = $invoice->client;
|
2015-10-13 09:11:44 +02:00
|
|
|
$account = $invoice->account;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
if (!$account->checkSubdomain(Request::server('HTTP_HOST'))) {
|
2015-09-28 22:37:32 +02:00
|
|
|
app()->abort(404, trans('texts.invoice_not_found'));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
if (!Input::has('phantomjs') && !Session::has($invitationKey) && (!Auth::check() || Auth::user()->account_id != $invoice->account_id)) {
|
2015-03-16 22:45:25 +01:00
|
|
|
Activity::viewInvoice($invitation);
|
2015-03-31 19:42:37 +02:00
|
|
|
Event::fire(new InvoiceViewed($invoice));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
Session::set($invitationKey, true); // track this invitation has been seen
|
|
|
|
Session::set('invitation_key', $invitationKey); // track current invitation
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
$account->loadLocalizationSettings($client);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
|
|
|
|
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
|
2015-04-15 18:35:41 +02:00
|
|
|
$invoice->is_pro = $account->isPro();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-07-21 20:51:56 +02:00
|
|
|
if ($invoice->invoice_design_id == CUSTOM_DESIGN) {
|
|
|
|
$invoice->invoice_design->javascript = $account->custom_design;
|
2015-08-12 21:16:02 +02:00
|
|
|
} else {
|
2015-08-11 16:38:36 +02:00
|
|
|
$invoice->invoice_design->javascript = $invoice->invoice_design->pdfmake;
|
2015-07-21 20:51:56 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$contact = $invitation->contact;
|
|
|
|
$contact->setVisible([
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
2015-10-13 09:11:44 +02:00
|
|
|
'phone',
|
|
|
|
]);
|
2015-05-27 22:20:35 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
$paymentTypes = $this->getPaymentTypes($client, $invitation);
|
2015-07-02 22:21:29 +02:00
|
|
|
$paymentURL = '';
|
|
|
|
if (count($paymentTypes)) {
|
|
|
|
$paymentURL = $paymentTypes[0]['url'];
|
2015-10-13 09:11:44 +02:00
|
|
|
if (!$account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) {
|
|
|
|
$paymentURL = URL::to($paymentURL);
|
|
|
|
}
|
2015-07-02 22:21:29 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
$showApprove = $invoice->quote_invoice_id ? false : true;
|
|
|
|
if ($invoice->due_date) {
|
|
|
|
$showApprove = time() < strtotime($invoice->due_date);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array(
|
2015-09-07 11:07:55 +02:00
|
|
|
'showApprove' => $showApprove,
|
2015-03-16 22:45:25 +01:00
|
|
|
'showBreadcrumbs' => false,
|
2015-04-15 18:35:41 +02:00
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-10-06 19:55:55 +02:00
|
|
|
'hideHeader' => $account->isNinjaAccount(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'invoice' => $invoice->hidePrivateFields(),
|
|
|
|
'invitation' => $invitation,
|
2015-04-15 18:35:41 +02:00
|
|
|
'invoiceLabels' => $account->getInvoiceLabels(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'contact' => $contact,
|
2015-07-02 22:21:29 +02:00
|
|
|
'paymentTypes' => $paymentTypes,
|
2015-08-11 16:38:36 +02:00
|
|
|
'paymentURL' => $paymentURL,
|
2015-09-17 21:01:06 +02:00
|
|
|
'phantomjs' => Input::has('phantomjs'),
|
2015-03-16 22:45:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return View::make('invoices.view', $data);
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
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();
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$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 {
|
2015-08-10 17:48:41 +02:00
|
|
|
Utils::trackViewed($invoice->getDisplayName().' - '.$invoice->client->getDisplayName(), $invoice->getEntityType());
|
2015-03-16 22:45:25 +01:00
|
|
|
$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);
|
2015-09-10 19:50:09 +02:00
|
|
|
$invoice->last_sent_date = Utils::fromSqlDate($invoice->last_sent_date);
|
2015-03-16 22:45:25 +01:00
|
|
|
$invoice->is_pro = Auth::user()->isPro();
|
|
|
|
|
2015-06-04 22:53:58 +02:00
|
|
|
$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}")];
|
|
|
|
|
2015-08-30 14:08:15 +02:00
|
|
|
$lastSent = ($invoice->is_recurring && $invoice->last_sent_date) ? $invoice->recurring_invoices->last() : null;
|
2015-06-04 22:53:58 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array(
|
|
|
|
'entityType' => $entityType,
|
|
|
|
'showBreadcrumbs' => $clone,
|
|
|
|
'invoice' => $invoice,
|
|
|
|
'data' => false,
|
|
|
|
'method' => $method,
|
|
|
|
'invitationContactIds' => $contactIds,
|
|
|
|
'url' => $url,
|
|
|
|
'title' => trans("texts.edit_{$entityType}"),
|
2015-08-30 14:08:15 +02:00
|
|
|
'client' => $invoice->client,
|
2015-08-10 17:48:41 +02:00
|
|
|
'isRecurring' => $invoice->is_recurring,
|
2015-08-30 14:08:15 +02:00
|
|
|
'actions' => $actions,
|
|
|
|
'lastSent' => $lastSent);
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array_merge($data, self::getViewModel());
|
|
|
|
|
2015-09-20 23:05:02 +02:00
|
|
|
if ($clone) {
|
|
|
|
$data['formIsChanged'] = true;
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
// Set the invitation data on the client's contacts
|
2015-03-16 22:45:25 +01:00
|
|
|
if (!$clone) {
|
|
|
|
$clients = $data['clients'];
|
|
|
|
foreach ($clients as $client) {
|
2015-10-11 16:41:09 +02:00
|
|
|
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();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
|
|
|
|
break;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return View::make('invoices.edit', $data);
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:48:41 +02:00
|
|
|
public function create($clientPublicId = 0, $isRecurring = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
$client = null;
|
2015-08-10 17:48:41 +02:00
|
|
|
$invoiceNumber = $isRecurring ? microtime(true) : Auth::user()->account->getNextInvoiceNumber();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
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'),
|
2015-08-10 17:48:41 +02:00
|
|
|
'isRecurring' => $isRecurring,
|
2015-07-12 21:43:45 +02:00
|
|
|
'client' => $client);
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array_merge($data, self::getViewModel());
|
2015-07-12 21:43:45 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return View::make('invoices.edit', $data);
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:48:41 +02:00
|
|
|
public function createRecurring($clientPublicId = 0)
|
|
|
|
{
|
|
|
|
return self::create($clientPublicId, true);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
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 .= '<li>'.strip_tags($line).'</li>';
|
|
|
|
} else {
|
|
|
|
$recurringHelp .= $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 21:45:10 +02:00
|
|
|
return [
|
2015-07-24 16:13:17 +02:00
|
|
|
'account' => Auth::user()->account->load('country'),
|
2015-03-30 21:45:10 +02:00
|
|
|
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
|
2015-04-08 15:19:17 +02:00
|
|
|
'countries' => Cache::get('countries'),
|
2015-03-30 21:45:10 +02:00
|
|
|
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
|
|
|
|
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
|
|
|
|
'currencies' => Cache::get('currencies'),
|
2015-09-10 19:50:09 +02:00
|
|
|
'languages' => Cache::get('languages'),
|
2015-04-08 15:19:17 +02:00
|
|
|
'sizes' => Cache::get('sizes'),
|
|
|
|
'paymentTerms' => Cache::get('paymentTerms'),
|
|
|
|
'industries' => Cache::get('industries'),
|
2015-07-28 09:00:00 +02:00
|
|
|
'invoiceDesigns' => InvoiceDesign::getDesigns(),
|
2015-03-30 21:45:10 +02:00
|
|
|
'frequencies' => array(
|
|
|
|
1 => 'Weekly',
|
|
|
|
2 => 'Two weeks',
|
|
|
|
3 => 'Four weeks',
|
|
|
|
4 => 'Monthly',
|
|
|
|
5 => 'Three months',
|
|
|
|
6 => 'Six months',
|
|
|
|
7 => 'Annually',
|
|
|
|
),
|
2015-05-27 22:20:35 +02:00
|
|
|
'recurringHelp' => $recurringHelp,
|
|
|
|
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
|
2015-07-12 21:43:45 +02:00
|
|
|
'tasks' => Session::get('tasks') ? json_encode(Session::get('tasks')) : null,
|
2015-03-30 21:45:10 +02:00
|
|
|
];
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return InvoiceController::save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function save($publicId = null)
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
|
|
|
$entityType = Input::get('entityType');
|
2015-10-13 09:11:44 +02:00
|
|
|
$input = json_decode(Input::get('data'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if (in_array($action, ['archive', 'delete', 'mark', 'restore'])) {
|
|
|
|
return InvoiceController::bulk($entityType);
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
if ($errors = $this->invoiceRepo->getErrors($input->invoice)) {
|
2015-03-16 22:45:25 +01:00
|
|
|
Session::flash('error', trans('texts.invoice_error'));
|
|
|
|
|
|
|
|
return Redirect::to("{$entityType}s/create")
|
|
|
|
->withInput()->withErrors($errors);
|
|
|
|
} else {
|
2015-10-13 09:11:44 +02:00
|
|
|
$invoice = $this->saveInvoice($publicId, $input, $entityType);
|
|
|
|
$url = "{$entityType}s/".$invoice->public_id.'/edit';
|
2015-03-16 22:45:25 +01:00
|
|
|
$message = trans($publicId ? "texts.updated_{$entityType}" : "texts.created_{$entityType}");
|
2015-10-13 09:11:44 +02:00
|
|
|
|
|
|
|
// check if we created a new client with the invoice
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($input->invoice->client->public_id == '-1') {
|
|
|
|
$message = $message.' '.trans('texts.and_created_client');
|
2015-10-14 07:18:14 +02:00
|
|
|
$url = URL::to('clients/'.$input->invoice->client->public_id);
|
2015-03-16 22:45:25 +01:00
|
|
|
Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT, $url);
|
|
|
|
}
|
2015-04-29 03:49:28 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($action == 'clone') {
|
|
|
|
return $this->cloneInvoice($publicId);
|
|
|
|
} elseif ($action == 'convert') {
|
|
|
|
return $this->convertQuote($publicId);
|
|
|
|
} elseif ($action == 'email') {
|
2015-10-13 09:11:44 +02:00
|
|
|
return $this->emailInvoice($invoice, Input::get('pdfupload'));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-10-13 09:11:44 +02:00
|
|
|
|
|
|
|
Session::flash('message', $message);
|
|
|
|
return Redirect::to($url);
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
private function emailInvoice($invoice, $pdfUpload)
|
|
|
|
{
|
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
$pdfUpload = Utils::decodePDF($pdfUpload);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
if (!Auth::user()->confirmed) {
|
|
|
|
$errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required');
|
|
|
|
Session::flash('error', $errorMessage);
|
|
|
|
Session::flash('message', $message);
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to($url);
|
|
|
|
}
|
2015-10-13 09:11:44 +02:00
|
|
|
|
|
|
|
if ($invoice->is_recurring) {
|
|
|
|
$response = $this->emailRecurringInvoice($invoice);
|
|
|
|
} else {
|
|
|
|
$response = $this->mailer->sendInvoice($invoice, false, $pdfUpload);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($response === true) {
|
|
|
|
$message = trans("texts.emailed_{$entityType}");
|
|
|
|
Session::flash('message', $message);
|
|
|
|
} else {
|
|
|
|
Session::flash('error', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to("{$entityType}s/{$invoice->public_id}/edit");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function emailRecurringInvoice(&$invoice)
|
|
|
|
{
|
|
|
|
if (!$invoice->shouldSendToday()) {
|
|
|
|
return trans('texts.recurring_too_soon');
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch from the recurring invoice to the generated invoice
|
|
|
|
$invoice = $this->invoiceRepo->createRecurringInvoice($invoice);
|
|
|
|
|
|
|
|
// in case auto-bill is enabled then a receipt has been sent
|
|
|
|
if ($invoice->isPaid()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $this->mailer->sendInvoice($invoice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function saveInvoice($publicId, $input, $entityType)
|
|
|
|
{
|
|
|
|
$invoice = $input->invoice;
|
|
|
|
|
|
|
|
$this->taxRateRepo->save($input->tax_rates);
|
|
|
|
|
|
|
|
$clientData = (array) $invoice->client;
|
|
|
|
$client = $this->clientRepo->save($invoice->client->public_id, $clientData);
|
|
|
|
|
|
|
|
$invoiceData = (array) $invoice;
|
|
|
|
$invoiceData['client_id'] = $client->id;
|
|
|
|
$invoice = $this->invoiceRepo->save($publicId, $invoiceData, $entityType);
|
|
|
|
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
if ($account->invoice_taxes != $input->invoice_taxes
|
|
|
|
|| $account->invoice_item_taxes != $input->invoice_item_taxes
|
|
|
|
|| $account->invoice_design_id != $input->invoice->invoice_design_id
|
|
|
|
|| $account->show_item_taxes != $input->show_item_taxes) {
|
|
|
|
$account->invoice_taxes = $input->invoice_taxes;
|
|
|
|
$account->invoice_item_taxes = $input->invoice_item_taxes;
|
|
|
|
$account->invoice_design_id = $input->invoice->invoice_design_id;
|
|
|
|
$account->show_item_taxes = $input->show_item_taxes;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->load('contacts');
|
|
|
|
$sendInvoiceIds = [];
|
|
|
|
|
|
|
|
foreach ($client->contacts as $contact) {
|
|
|
|
if ($contact->send_invoice || count($client->contacts) == 1) {
|
|
|
|
$sendInvoiceIds[] = $contact->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($client->contacts as $contact) {
|
|
|
|
$invitation = Invitation::scope()->whereContactId($contact->id)->whereInvoiceId($invoice->id)->first();
|
|
|
|
|
|
|
|
if (in_array($contact->id, $sendInvoiceIds) && !$invitation) {
|
|
|
|
$invitation = Invitation::createNew();
|
|
|
|
$invitation->invoice_id = $invoice->id;
|
|
|
|
$invitation->contact_id = $contact->id;
|
|
|
|
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
|
|
|
|
$invitation->save();
|
|
|
|
} elseif (!in_array($contact->id, $sendInvoiceIds) && $invitation) {
|
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $invoice;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show($publicId)
|
|
|
|
{
|
|
|
|
Session::reflash();
|
|
|
|
|
2015-09-29 20:19:18 +02:00
|
|
|
return Redirect::to("invoices/{$publicId}/edit");
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return InvoiceController::save($publicId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function bulk($entityType = ENTITY_INVOICE)
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
|
|
|
$statusId = Input::get('statusId', INVOICE_STATUS_SENT);
|
|
|
|
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
|
|
|
|
$count = $this->invoiceRepo->bulk($ids, $action, $statusId);
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
$key = $action == 'mark' ? "updated_{$entityType}" : "{$action}d_{$entityType}";
|
|
|
|
$message = Utils::pluralize($key, $count);
|
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action == 'restore' && $count == 1) {
|
2015-08-20 10:02:03 +02:00
|
|
|
return Redirect::to("{$entityType}s/".Utils::getFirst($ids));
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
return Redirect::to("{$entityType}s");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function convertQuote($publicId)
|
|
|
|
{
|
|
|
|
$invoice = Invoice::with('invoice_items')->scope($publicId)->firstOrFail();
|
|
|
|
$clone = $this->invoiceRepo->cloneInvoice($invoice, $invoice->id);
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.converted_to_invoice'));
|
|
|
|
return Redirect::to('invoices/'.$clone->public_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cloneInvoice($publicId)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
$invoice = Invoice::with('invoice_items')->scope($publicId)->firstOrFail();
|
|
|
|
$clone = $this->invoiceRepo->cloneInvoice($invoice);
|
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.cloned_invoice'));
|
|
|
|
return Redirect::to("{$entityType}s/" . $clone->public_id);
|
|
|
|
*/
|
|
|
|
|
|
|
|
return self::edit($publicId, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-07-28 09:00:00 +02:00
|
|
|
'invoiceDesigns' => InvoiceDesign::getDesigns(),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('invoices.history', $data);
|
|
|
|
}
|
|
|
|
}
|