1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/InvoiceController.php

648 lines
25 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Http\Requests\CreateInvoiceRequest;
use App\Http\Requests\InvoiceRequest;
use App\Http\Requests\UpdateInvoiceRequest;
use App\Jobs\SendInvoiceEmail;
2015-03-30 21:45:10 +02:00
use App\Models\Account;
2017-01-30 20:40:43 +01:00
use App\Models\Activity;
use App\Models\Client;
use App\Models\Expense;
2017-01-30 20:40:43 +01:00
use App\Models\Invoice;
use App\Models\InvoiceDesign;
use App\Models\Payment;
2017-01-30 20:40:43 +01:00
use App\Models\Product;
2015-03-30 21:45:10 +02:00
use App\Models\TaxRate;
2017-01-30 20:40:43 +01:00
use App\Ninja\Datatables\InvoiceDatatable;
use App\Ninja\Repositories\ClientRepository;
2016-03-23 03:23:45 +01:00
use App\Ninja\Repositories\DocumentRepository;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\InvoiceRepository;
2015-10-28 20:22:07 +01:00
use App\Services\InvoiceService;
use App\Services\PaymentService;
2015-11-05 23:37:04 +01:00
use App\Services\RecurringInvoiceService;
2017-01-30 20:40:43 +01:00
use Auth;
use Cache;
use DB;
use Input;
use Redirect;
use Session;
use URL;
use Utils;
use View;
2015-03-17 02:30:56 +01:00
class InvoiceController extends BaseController
2015-03-16 22:45:25 +01:00
{
protected $invoiceRepo;
protected $clientRepo;
2016-03-23 03:23:45 +01:00
protected $documentRepo;
2015-10-28 20:22:07 +01:00
protected $invoiceService;
protected $paymentService;
2015-11-05 23:37:04 +01:00
protected $recurringInvoiceService;
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_INVOICE;
2015-03-16 22:45:25 +01:00
2017-01-11 11:32:13 +01:00
public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService, PaymentService $paymentService)
2015-03-16 22:45:25 +01:00
{
2016-03-16 00:08:00 +01:00
// parent::__construct();
2015-03-16 22:45:25 +01:00
$this->invoiceRepo = $invoiceRepo;
$this->clientRepo = $clientRepo;
2015-10-28 20:22:07 +01:00
$this->invoiceService = $invoiceService;
2015-11-05 23:37:04 +01:00
$this->recurringInvoiceService = $recurringInvoiceService;
$this->paymentService = $paymentService;
2015-03-16 22:45:25 +01:00
}
public function index()
{
$data = [
'title' => trans('texts.invoices'),
'entityType' => ENTITY_INVOICE,
2016-11-18 14:31:43 +01:00
'statuses' => Invoice::getStatuses(),
'datatable' => new InvoiceDatatable(),
2015-03-16 22:45:25 +01:00
];
return response()->view('list_wrapper', $data);
2015-03-16 22:45:25 +01:00
}
public function getDatatable($clientPublicId = null)
{
$accountId = Auth::user()->account_id;
$search = Input::get('sSearch');
2015-11-05 23:37:04 +01:00
return $this->invoiceService->getDatatable($accountId, $clientPublicId, ENTITY_INVOICE, $search);
2015-03-16 22:45:25 +01:00
}
public function getRecurringDatatable($clientPublicId = null)
{
2015-11-05 23:37:04 +01:00
$accountId = Auth::user()->account_id;
$search = Input::get('sSearch');
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
return $this->recurringInvoiceService->getDatatable($accountId, $clientPublicId, ENTITY_RECURRING_INVOICE, $search);
2015-03-16 22:45:25 +01:00
}
2016-05-01 21:30:39 +02:00
public function edit(InvoiceRequest $request, $publicId, $clone = false)
2015-03-16 22:45:25 +01:00
{
2015-10-22 20:48:12 +02:00
$account = Auth::user()->account;
2016-05-01 21:30:39 +02:00
$invoice = $request->entity()->load('invitations', 'account.country', 'client.contacts', 'client.country', 'invoice_items', 'documents', 'expenses', 'expenses.documents', 'payments');
2016-06-02 21:03:59 +02:00
2015-03-16 22:45:25 +01:00
$entityType = $invoice->getEntityType();
2016-06-02 21:03:59 +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)
2017-11-14 09:58:08 +01:00
->select('contacts.public_id')->pluck('public_id');
2015-03-16 22:45:25 +01:00
2016-03-09 12:35:46 +01:00
$clients = Client::scope()->withTrashed()->with('contacts', 'country');
2015-03-16 22:45:25 +01:00
if ($clone) {
$entityType = $clone == INVOICE_TYPE_STANDARD ? ENTITY_INVOICE : ENTITY_QUOTE;
2015-10-28 20:22:07 +01:00
$invoice->id = $invoice->public_id = null;
$invoice->is_public = false;
2017-08-22 22:32:48 +02:00
$invoice->is_recurring = $invoice->is_recurring && $clone == INVOICE_TYPE_STANDARD;
$invoice->invoice_type_id = $clone;
2017-01-04 09:11:32 +01:00
$invoice->invoice_number = $account->getNextNumber($invoice);
$invoice->due_date = null;
$invoice->partial_due_date = null;
2015-03-16 22:45:25 +01:00
$invoice->balance = $invoice->amount;
$invoice->invoice_status_id = 0;
2017-01-30 13:40:07 +01:00
$invoice->invoice_date = date_create()->format('Y-m-d');
2017-02-20 12:47:25 +01:00
$invoice->deleted_at = null;
while ($invoice->documents->count()) {
$invoice->documents->pop();
}
while ($invoice->expenses->count()) {
$invoice->expenses->pop();
}
2015-03-16 22:45:25 +01:00
$method = 'POST';
$url = "{$entityType}s";
} else {
$method = 'PUT';
2016-05-01 21:30:39 +02:00
$url = "{$entityType}s/{$invoice->public_id}";
2016-03-09 12:35:46 +01:00
$clients->whereId($invoice->client_id);
2015-03-16 22:45:25 +01:00
}
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
2017-01-30 20:40:43 +01:00
$invoice->recurring_due_date = $invoice->due_date; // Keep in SQL form
2015-03-16 22:45:25 +01:00
$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->partial_due_date = Utils::fromSqlDate($invoice->partial_due_date);
$invoice->features = [
'customize_invoice_design' => Auth::user()->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN),
'remove_created_by' => Auth::user()->hasFeature(FEATURE_REMOVE_CREATED_BY),
'invoice_settings' => Auth::user()->hasFeature(FEATURE_INVOICE_SETTINGS),
];
2015-03-16 22:45:25 +01:00
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
2017-01-30 20:40:43 +01:00
if (! Auth::user()->hasPermission('view_all')) {
2016-03-17 15:11:14 +01:00
$clients = $clients->where('clients.user_id', '=', Auth::user()->id);
}
2016-06-02 21:03:59 +02:00
$data = [
2016-03-09 12:35:46 +01:00
'clients' => $clients->get(),
2015-03-16 22:45:25 +01:00
'entityType' => $entityType,
'showBreadcrumbs' => $clone,
'invoice' => $invoice,
'method' => $method,
'invitationContactIds' => $contactIds,
'url' => $url,
'title' => trans("texts.edit_{$entityType}"),
2015-08-30 14:08:15 +02:00
'client' => $invoice->client,
'isRecurring' => $invoice->is_recurring,
2017-01-30 20:40:43 +01:00
'lastSent' => $lastSent, ];
2016-03-29 16:30:28 +02:00
$data = array_merge($data, self::getViewModel($invoice));
2016-01-18 10:13:39 +01:00
2017-01-30 20:40:43 +01:00
if ($invoice->isSent() && $invoice->getAutoBillEnabled() && ! $invoice->isPaid()) {
2016-06-20 16:14:43 +02:00
$data['autoBillChangeWarning'] = $invoice->client->autoBillLater();
}
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
2017-01-30 17:05:31 +01:00
if (! $clone) {
2015-03-16 22:45:25 +01:00
$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) {
$hasPassword = $account->isClientPortalPasswordEnabled() && $contact->password;
2015-10-11 16:41:09 +02:00
$contact->email_error = $invitation->email_error;
$contact->invitation_link = $invitation->getLink('view', $hasPassword, $hasPassword);
2015-10-18 12:37:04 +02:00
$contact->invitation_viewed = $invitation->viewed_date && $invitation->viewed_date != '0000-00-00 00:00:00' ? $invitation->viewed_date : false;
$contact->invitation_opened = $invitation->opened_date && $invitation->opened_date != '0000-00-00 00:00:00' ? $invitation->opened_date : false;
2015-10-11 16:41:09 +02:00
$contact->invitation_status = $contact->email_error ? false : $invitation->getStatus();
2016-11-06 12:44:58 +01:00
$contact->invitation_signature_svg = $invitation->signatureDiv();
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);
}
2016-05-01 21:30:39 +02:00
public function create(InvoiceRequest $request, $clientPublicId = 0, $isRecurring = false)
2015-03-16 22:45:25 +01:00
{
2016-03-23 03:23:45 +01:00
$account = Auth::user()->account;
2016-06-02 21:03:59 +02:00
2015-10-28 20:22:07 +01:00
$entityType = $isRecurring ? ENTITY_RECURRING_INVOICE : ENTITY_INVOICE;
2015-10-25 08:13:06 +01:00
$clientId = null;
2016-01-18 10:13:39 +01:00
2016-05-01 21:30:39 +02:00
if ($request->client_id) {
$clientId = Client::getPrivateId($request->client_id);
2015-03-16 22:45:25 +01:00
}
2016-01-18 10:13:39 +01:00
2015-10-28 20:22:07 +01:00
$invoice = $account->createInvoice($entityType, $clientId);
$invoice->public_id = 0;
2017-04-04 15:57:33 +02:00
$invoice->loadFromRequest();
2016-06-02 21:03:59 +02:00
2016-03-17 15:11:14 +01:00
$clients = Client::scope()->with('contacts', 'country')->orderBy('name');
2017-01-30 20:40:43 +01:00
if (! Auth::user()->hasPermission('view_all')) {
2016-03-17 15:11:14 +01:00
$clients = $clients->where('clients.user_id', '=', Auth::user()->id);
}
2016-06-02 21:03:59 +02:00
2015-10-23 13:55:18 +02:00
$data = [
2016-03-17 15:11:14 +01:00
'clients' => $clients->get(),
2015-10-23 13:55:18 +02:00
'entityType' => $invoice->getEntityType(),
'invoice' => $invoice,
'method' => 'POST',
'url' => 'invoices',
'title' => trans('texts.new_invoice'),
];
2016-03-29 16:30:28 +02:00
$data = array_merge($data, self::getViewModel($invoice));
2016-01-18 10:13:39 +01:00
2015-03-16 22:45:25 +01:00
return View::make('invoices.edit', $data);
}
2016-05-01 21:33:01 +02:00
public function createRecurring(InvoiceRequest $request, $clientPublicId = 0)
{
2016-05-01 21:30:39 +02:00
return self::create($request, $clientPublicId, true);
}
2016-03-29 16:30:28 +02:00
private static function getViewModel($invoice)
2015-03-16 22:45:25 +01:00
{
2017-02-10 08:43:46 +01:00
$account = Auth::user()->account;
2015-03-16 22:45:25 +01:00
$recurringHelp = '';
$recurringDueDateHelp = '';
2016-09-14 09:32:22 +02:00
$recurringDueDates = [];
2016-09-26 08:54:59 +02:00
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;
}
2016-09-26 08:54:59 +02:00
}
2016-01-18 10:13:39 +01:00
2016-09-26 08:54:59 +02:00
foreach (preg_split("/((\r?\n)|(\r\n?))/", trans('texts.recurring_due_date_help')) as $line) {
$parts = explode('=>', $line);
if (count($parts) > 1) {
$line = $parts[0].' => '.Utils::processVariables($parts[0]);
$recurringDueDateHelp .= '<li>'.strip_tags($line).'</li>';
} else {
$recurringDueDateHelp .= $line;
2016-09-14 09:32:22 +02:00
}
2016-09-26 08:54:59 +02:00
}
// Create due date options
$recurringDueDates = [
trans('texts.use_client_terms') => ['value' => '', 'class' => 'monthly weekly'],
];
2016-01-18 10:13:39 +01:00
2017-01-30 20:40:43 +01:00
$ends = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'];
2017-01-30 17:05:31 +01:00
for ($i = 1; $i < 31; $i++) {
if ($i >= 11 && $i <= 13) {
$ordinal = $i. 'th';
} else {
$ordinal = $i . $ends[$i % 10];
}
2016-01-18 10:13:39 +01:00
2016-09-26 08:54:59 +02:00
$dayStr = str_pad($i, 2, '0', STR_PAD_LEFT);
2017-01-30 20:40:43 +01:00
$str = trans('texts.day_of_month', ['ordinal' => $ordinal]);
2016-01-18 10:13:39 +01:00
2016-09-26 08:54:59 +02:00
$recurringDueDates[$str] = ['value' => "1998-01-$dayStr", 'data-num' => $i, 'class' => 'monthly'];
}
$recurringDueDates[trans('texts.last_day_of_month')] = ['value' => '1998-01-31', 'data-num' => 31, 'class' => 'monthly'];
2016-01-18 10:13:39 +01:00
2016-09-26 08:54:59 +02:00
$daysOfWeek = [
trans('texts.sunday'),
trans('texts.monday'),
trans('texts.tuesday'),
trans('texts.wednesday'),
trans('texts.thursday'),
trans('texts.friday'),
trans('texts.saturday'),
];
2017-01-30 20:40:43 +01:00
foreach (['1st', '2nd', '3rd', '4th'] as $i => $ordinal) {
foreach ($daysOfWeek as $j => $dayOfWeek) {
2016-09-26 08:54:59 +02:00
$str = trans('texts.day_of_week_after', ['ordinal' => $ordinal, 'day' => $dayOfWeek]);
2017-01-30 20:40:43 +01:00
$day = $i * 7 + $j + 1;
2016-09-26 08:54:59 +02:00
$dayStr = str_pad($day, 2, '0', STR_PAD_LEFT);
$recurringDueDates[$str] = ['value' => "1998-02-$dayStr", 'data-num' => $day, 'class' => 'weekly'];
}
}
2016-03-29 16:30:28 +02:00
// Check for any taxes which have been deleted
2017-03-24 11:29:38 +01:00
$taxRateOptions = $account->present()->taxRateOptions;
2016-03-29 16:30:28 +02:00
if ($invoice->exists) {
foreach ($invoice->getTaxes() as $key => $rate) {
2017-03-24 12:59:58 +01:00
$key = '0 ' . $key; // mark it as a standard exclusive rate option
2017-03-24 11:29:38 +01:00
if (isset($taxRateOptions[$key])) {
2016-03-29 16:30:28 +02:00
continue;
2016-06-02 21:03:59 +02:00
}
2017-03-24 12:58:23 +01:00
$taxRateOptions[$key] = $rate['name'] . ' ' . $rate['rate'] . '%';
2016-03-29 16:30:28 +02:00
}
2016-03-27 17:06:02 +02:00
}
2016-06-02 21:03:59 +02:00
2015-03-30 21:45:10 +02:00
return [
2015-10-25 08:13:06 +01:00
'data' => Input::old('data'),
2015-07-24 16:13:17 +02:00
'account' => Auth::user()->account->load('country'),
'products' => Product::scope()->orderBy('product_key')->get(),
2017-03-24 11:29:38 +01:00
'taxRateOptions' => $taxRateOptions,
2015-04-08 15:19:17 +02:00
'sizes' => Cache::get('sizes'),
2015-07-28 09:00:00 +02:00
'invoiceDesigns' => InvoiceDesign::getDesigns(),
2016-01-07 08:08:30 +01:00
'invoiceFonts' => Cache::get('fonts'),
2017-01-29 16:32:59 +01:00
'frequencies' => \App\Models\Frequency::selectOptions(),
'recurringDueDates' => $recurringDueDates,
'recurringHelp' => $recurringHelp,
'recurringDueDateHelp' => $recurringDueDateHelp,
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
2017-04-27 10:12:55 +02:00
'tasks' => Session::get('tasks') ? Session::get('tasks') : null,
'expenseCurrencyId' => Session::get('expenseCurrencyId') ?: null,
2018-03-05 15:40:10 +01:00
'expenses' => Expense::scope(Session::get('expenses'))->with('documents', 'expense_category')->get(),
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
*/
2016-05-01 21:30:39 +02:00
public function store(CreateInvoiceRequest $request)
2015-03-16 22:45:25 +01:00
{
$data = $request->input();
2016-03-25 00:55:56 +01:00
$data['documents'] = $request->file('documents');
2016-06-02 21:03:59 +02:00
2015-10-28 20:22:07 +01:00
$action = Input::get('action');
$entityType = Input::get('entityType');
2016-06-02 21:03:59 +02:00
2016-04-27 22:56:14 +02:00
$invoice = $this->invoiceService->save($data);
2015-10-28 20:22:07 +01:00
$entityType = $invoice->getEntityType();
$message = trans("texts.created_{$entityType}");
$input = $request->input();
$clientPublicId = isset($input['client']['public_id']) ? $input['client']['public_id'] : false;
if ($clientPublicId == '-1') {
$message = $message.' '.trans('texts.and_created_client');
}
Session::flash('message', $message);
if ($action == 'email') {
2017-02-06 10:41:16 +01:00
$this->emailInvoice($invoice);
2015-10-28 20:22:07 +01:00
}
2016-01-18 10:13:39 +01:00
return url($invoice->getRoute());
2015-03-16 22:45:25 +01:00
}
2015-10-28 20:22:07 +01:00
/**
* Update the specified resource in storage.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2015-10-28 20:22:07 +01:00
* @return Response
*/
2016-05-01 21:30:39 +02:00
public function update(UpdateInvoiceRequest $request)
2015-03-16 22:45:25 +01:00
{
$data = $request->input();
$data['documents'] = $request->file('documents');
2015-03-16 22:45:25 +01:00
$action = Input::get('action');
$entityType = Input::get('entityType');
2018-02-14 18:58:48 +01:00
$invoice = $this->invoiceService->save($data, $request->entity());
$entityType = $invoice->getEntityType();
$message = trans("texts.updated_{$entityType}");
Session::flash('message', $message);
if ($action == 'clone_invoice') {
return url(sprintf('invoices/%s/clone', $invoice->public_id));
} else if ($action == 'clone_quote') {
return url(sprintf('quotes/%s/clone', $invoice->public_id));
2015-10-28 20:22:07 +01:00
} elseif ($action == 'convert') {
2016-05-01 21:36:12 +02:00
return $this->convertQuote($request, $invoice->public_id);
2015-10-28 20:22:07 +01:00
} elseif ($action == 'email') {
2017-02-06 10:41:16 +01:00
$this->emailInvoice($invoice);
2015-10-13 09:11:44 +02:00
}
2016-01-18 10:13:39 +01:00
return url($invoice->getRoute());
2015-10-13 09:11:44 +02:00
}
2015-03-16 22:45:25 +01:00
2017-02-06 10:41:16 +01:00
private function emailInvoice($invoice)
2015-10-13 09:11:44 +02:00
{
2017-02-06 10:41:16 +01:00
$reminder = Input::get('reminder');
$template = Input::get('template');
$pdfUpload = Utils::decodePDF(Input::get('pdfupload'));
2015-10-13 09:11:44 +02:00
$entityType = $invoice->getEntityType();
2017-02-06 10:41:16 +01:00
if (filter_var(Input::get('save_as_default'), FILTER_VALIDATE_BOOLEAN)) {
$account = Auth::user()->account;
$account->setTemplateDefaults(Input::get('template_type'), $template['subject'], $template['body']);
}
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
if (! Auth::user()->confirmed) {
2018-02-20 22:20:38 +01:00
if (Auth::user()->registered) {
$errorMessage = trans('texts.confirmation_required', ['link' => link_to('/resend_confirmation', trans('texts.click_here'))]);
} else {
$errorMessage = trans('texts.registration_required');
}
2015-10-13 09:11:44 +02:00
Session::flash('error', $errorMessage);
2017-01-30 20:40:43 +01:00
2015-10-20 10:23:38 +02:00
return Redirect::to('invoices/'.$invoice->public_id.'/edit');
2015-03-16 22:45:25 +01:00
}
2015-10-13 09:11:44 +02:00
if ($invoice->is_recurring) {
$response = $this->emailRecurringInvoice($invoice);
} else {
$userId = Auth::user()->id;
$this->dispatch(new SendInvoiceEmail($invoice, $userId, $reminder, $template));
2017-02-19 13:05:38 +01:00
$response = true;
2015-10-13 09:11:44 +02:00
}
if ($response === true) {
$message = trans("texts.emailed_{$entityType}");
Session::flash('message', $message);
} else {
Session::flash('error', $response);
}
}
private function emailRecurringInvoice(&$invoice)
{
2017-01-30 20:40:43 +01:00
if (! $invoice->shouldSendToday()) {
2015-10-15 21:37:01 +02:00
if ($date = $invoice->getNextSendDate()) {
$date = $invoice->account->formatDate($date);
$date .= ' ' . DEFAULT_SEND_RECURRING_HOUR . ':00 am ' . $invoice->account->getTimezone();
2017-01-30 20:40:43 +01:00
2015-10-15 21:37:01 +02:00
return trans('texts.recurring_too_soon', ['date' => $date]);
} else {
return trans('texts.no_longer_running');
}
2015-10-13 09:11:44 +02:00
}
// 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 {
$userId = Auth::user()->id;
$this->dispatch(new SendInvoiceEmail($invoice, $userId));
2017-02-19 13:05:38 +01:00
return true;
2015-10-13 09:11:44 +02:00
}
}
2015-03-16 22:45:25 +01:00
/**
* Display the specified resource.
*
2017-01-30 20:49:42 +01:00
* @param int $id
* @param mixed $publicId
2017-01-30 20:40:43 +01:00
*
2015-03-16 22:45:25 +01:00
* @return Response
*/
public function show($publicId)
{
Session::reflash();
2016-05-01 21:30:39 +02:00
return Redirect::to("invoices/$publicId/edit");
2015-03-16 22:45:25 +01:00
}
/**
* Remove the specified resource from storage.
*
2017-01-30 20:49:42 +01:00
* @param int $id
* @param mixed $entityType
2017-01-30 20:40:43 +01:00
*
2015-03-16 22:45:25 +01:00
* @return Response
*/
public function bulk($entityType = ENTITY_INVOICE)
{
2017-01-30 17:05:31 +01:00
$action = Input::get('bulk_action') ?: Input::get('action');
2015-10-28 20:22:07 +01:00
$ids = Input::get('bulk_public_id') ?: (Input::get('public_id') ?: Input::get('ids'));
2015-10-29 15:42:05 +01:00
$count = $this->invoiceService->bulk($ids, $action);
2015-03-16 22:45:25 +01:00
if ($count > 0) {
2016-12-14 20:47:22 +01:00
if ($action == 'markSent') {
$key = 'marked_sent_invoice';
2017-01-11 14:53:26 +01:00
} elseif ($action == 'emailInvoice') {
$key = 'emailed_' . $entityType;
2016-12-14 20:47:22 +01:00
} elseif ($action == 'markPaid') {
$key = 'created_payment';
2017-07-19 16:34:09 +02:00
} elseif ($action == 'download') {
$key = 'downloaded_invoice';
2016-12-14 20:47:22 +01:00
} else {
$key = "{$action}d_{$entityType}";
}
2015-03-16 22:45:25 +01:00
$message = Utils::pluralize($key, $count);
Session::flash('message', $message);
}
if (strpos(\Request::server('HTTP_REFERER'), 'recurring_invoices')) {
$entityType = ENTITY_RECURRING_INVOICE;
}
2016-09-19 15:30:46 +02:00
return $this->returnBulk($entityType, $action, $ids);
2015-03-16 22:45:25 +01:00
}
2016-05-01 22:55:13 +02:00
public function convertQuote(InvoiceRequest $request)
2015-03-16 22:45:25 +01:00
{
2016-05-01 22:55:13 +02:00
$clone = $this->invoiceService->convertQuote($request->entity());
2015-03-16 22:45:25 +01:00
Session::flash('message', trans('texts.converted_to_invoice'));
2016-06-02 21:03:59 +02:00
return url('invoices/' . $clone->public_id);
2015-03-16 22:45:25 +01:00
}
2016-05-01 21:30:39 +02:00
public function cloneInvoice(InvoiceRequest $request, $publicId)
2015-03-16 22:45:25 +01:00
{
return self::edit($request, $publicId, INVOICE_TYPE_STANDARD);
}
public function cloneQuote(InvoiceRequest $request, $publicId)
{
return self::edit($request, $publicId, INVOICE_TYPE_QUOTE);
2015-03-16 22:45:25 +01:00
}
2016-05-09 19:35:50 +02:00
public function invoiceHistory(InvoiceRequest $request)
2015-03-16 22:45:25 +01:00
{
2016-05-09 19:35:50 +02:00
$invoice = $request->entity();
$paymentId = $request->payment_id ? Payment::getPrivateId($request->payment_id) : false;
$invoice->load('user', 'invoice_items', 'documents', 'expenses', 'expenses.documents', 'account.country', 'client.contacts', 'client.country');
2015-03-16 22:45:25 +01:00
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
$invoice->features = [
'customize_invoice_design' => Auth::user()->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN),
'remove_created_by' => Auth::user()->hasFeature(FEATURE_REMOVE_CREATED_BY),
'invoice_settings' => Auth::user()->hasFeature(FEATURE_INVOICE_SETTINGS),
];
2016-05-26 16:56:54 +02:00
$invoice->invoice_type_id = intval($invoice->invoice_type_id);
2015-03-16 22:45:25 +01:00
2017-01-09 09:17:22 +01:00
$activities = Activity::scope(false, $invoice->account_id);
if ($paymentId) {
$activities->whereIn('activity_type_id', [ACTIVITY_TYPE_CREATE_PAYMENT])
->where('payment_id', '=', $paymentId);
} else {
$activities->whereIn('activity_type_id', [ACTIVITY_TYPE_UPDATE_INVOICE, ACTIVITY_TYPE_UPDATE_QUOTE])
->where('invoice_id', '=', $invoice->id);
}
$activities = $activities->orderBy('id', 'desc')
->get(['id', 'created_at', 'user_id', 'json_backup', 'activity_type_id', 'payment_id']);
2015-03-16 22:45:25 +01:00
$versionsJson = [];
$versionsSelect = [];
$lastId = false;
2015-03-16 22:45:25 +01:00
foreach ($activities as $activity) {
if ($backup = json_decode($activity->json_backup)) {
$backup->invoice_date = Utils::fromSqlDate($backup->invoice_date);
$backup->due_date = Utils::fromSqlDate($backup->due_date);
$backup->features = [
'customize_invoice_design' => Auth::user()->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN),
'remove_created_by' => Auth::user()->hasFeature(FEATURE_REMOVE_CREATED_BY),
'invoice_settings' => Auth::user()->hasFeature(FEATURE_INVOICE_SETTINGS),
];
$backup->invoice_type_id = isset($backup->invoice_type_id) && intval($backup->invoice_type_id) == INVOICE_TYPE_QUOTE;
$backup->account = $invoice->account->toArray();
2017-01-09 09:17:22 +01:00
$versionsJson[$paymentId ? 0 : $activity->id] = $backup;
$key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName();
$versionsSelect[$lastId ?: 0] = $key;
$lastId = $activity->id;
} else {
Utils::logError('Failed to parse invoice backup');
}
2015-03-16 22:45:25 +01:00
}
2017-01-09 09:17:22 +01:00
// Show the current version as the last in the history
2017-01-30 17:05:31 +01:00
if (! $paymentId) {
$versionsSelect[$lastId] = Utils::timestampToDateTimeString(strtotime($invoice->created_at)) . ' - ' . $invoice->user->getDisplayName();
}
2015-03-16 22:45:25 +01:00
$data = [
'invoice' => $invoice,
'versionsJson' => json_encode($versionsJson),
'versionsSelect' => $versionsSelect,
2015-07-28 09:00:00 +02:00
'invoiceDesigns' => InvoiceDesign::getDesigns(),
2016-01-07 08:08:30 +01:00
'invoiceFonts' => Cache::get('fonts'),
2017-01-09 09:17:22 +01:00
'paymentId' => $paymentId,
2015-03-16 22:45:25 +01:00
];
return View::make('invoices.history', $data);
}
2017-11-20 14:40:12 +01:00
public function deliveryNote(InvoiceRequest $request)
{
$invoice = $request->entity();
$invoice->load('user', 'invoice_items', 'documents', 'expenses', 'expenses.documents', 'account.country', 'client.contacts', 'client.country', 'client.shipping_country');
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
$invoice->features = [
'customize_invoice_design' => Auth::user()->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN),
'remove_created_by' => Auth::user()->hasFeature(FEATURE_REMOVE_CREATED_BY),
'invoice_settings' => Auth::user()->hasFeature(FEATURE_INVOICE_SETTINGS),
];
$invoice->invoice_type_id = intval($invoice->invoice_type_id);
if ($invoice->client->shipping_address1) {
foreach (['address1', 'address2', 'city', 'state', 'postal_code', 'country_id'] as $field) {
$invoice->client->$field = $invoice->client->{'shipping_' . $field};
}
}
2017-11-20 14:40:12 +01:00
$data = [
'invoice' => $invoice,
'invoiceDesigns' => InvoiceDesign::getDesigns(),
'invoiceFonts' => Cache::get('fonts'),
];
return View::make('invoices.delivery_note', $data);
}
public function checkInvoiceNumber($invoicePublicId = false)
{
$invoiceNumber = request()->invoice_number;
2016-09-19 15:30:46 +02:00
$query = Invoice::scope()
->whereInvoiceNumber($invoiceNumber)
->withTrashed();
if ($invoicePublicId) {
$query->where('public_id', '!=', $invoicePublicId);
}
$count = $query->count();
return $count ? RESULT_FAILURE : RESULT_SUCCESS;
}
2015-03-16 22:45:25 +01:00
}