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;
|
2016-03-24 23:15:52 +01:00
|
|
|
use App\Models\Expense;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceDesign;
|
2017-01-08 22:34:44 +01:00
|
|
|
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;
|
2015-03-24 09:21:12 +01:00
|
|
|
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;
|
2016-05-25 17:07:20 +02:00
|
|
|
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
|
|
|
|
2015-03-24 09:21:12 +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;
|
2016-05-25 17:07:20 +02:00
|
|
|
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;
|
2016-05-25 17:07:20 +02:00
|
|
|
$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(),
|
2016-11-24 10:22:37 +01:00
|
|
|
'datatable' => new InvoiceDatatable(),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
2016-11-24 10:22:37 +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)
|
|
|
|
->select('contacts.public_id')->lists('public_id');
|
|
|
|
|
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) {
|
2017-08-22 17:35:05 +02:00
|
|
|
$entityType = $clone == INVOICE_TYPE_STANDARD ? ENTITY_INVOICE : ENTITY_QUOTE;
|
2015-10-28 20:22:07 +01:00
|
|
|
$invoice->id = $invoice->public_id = null;
|
2016-12-04 22:26:38 +01:00
|
|
|
$invoice->is_public = false;
|
2017-08-22 17:35:05 +02:00
|
|
|
$invoice->invoice_type_id = $clone;
|
2017-01-04 09:11:32 +01:00
|
|
|
$invoice->invoice_number = $account->getNextNumber($invoice);
|
2017-07-04 21:58:33 +02:00
|
|
|
$invoice->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;
|
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);
|
2015-09-10 19:50:09 +02:00
|
|
|
$invoice->last_sent_date = Utils::fromSqlDate($invoice->last_sent_date);
|
2016-04-19 04:35:18 +02:00
|
|
|
$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
|
|
|
|
2016-07-03 18:11:58 +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,
|
2015-08-10 17:48:41 +02:00
|
|
|
'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();
|
2016-05-25 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-04-14 12:55:32 +02:00
|
|
|
$hasPassword = $account->isClientPortalPasswordEnabled() && $contact->password;
|
2015-10-11 16:41:09 +02:00
|
|
|
$contact->email_error = $invitation->email_error;
|
2017-04-14 12:55:32 +02:00
|
|
|
$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;
|
2016-04-09 22:36:32 +02:00
|
|
|
$contact->invitation_openend = $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)
|
2015-08-10 17:48:41 +02:00
|
|
|
{
|
2016-05-01 21:30:39 +02:00
|
|
|
return self::create($request, $clientPublicId, true);
|
2015-08-10 17:48:41 +02:00
|
|
|
}
|
|
|
|
|
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 = '';
|
2016-01-01 05:59:02 +01:00
|
|
|
$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-01-01 05:59:02 +01:00
|
|
|
}
|
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-01-01 23:37:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 11:26:18 +02:00
|
|
|
|
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'),
|
2017-05-17 11:18:48 +02:00
|
|
|
'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(),
|
2016-01-01 23:37:47 +01:00
|
|
|
'recurringDueDates' => $recurringDueDates,
|
2015-05-27 22:20:35 +02:00
|
|
|
'recurringHelp' => $recurringHelp,
|
2016-01-01 05:59:02 +01:00
|
|
|
'recurringDueDateHelp' => $recurringDueDateHelp,
|
2015-05-27 22:20:35 +02:00
|
|
|
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
|
2017-04-27 10:12:55 +02:00
|
|
|
'tasks' => Session::get('tasks') ? Session::get('tasks') : null,
|
2016-01-31 14:10:33 +01:00
|
|
|
'expenseCurrencyId' => Session::get('expenseCurrencyId') ?: null,
|
2016-08-21 17:25:35 +02:00
|
|
|
'expenses' => Session::get('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
|
|
|
{
|
2016-03-16 03:07:11 +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
|
|
|
|
2016-09-25 12:41:38 +02: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
|
|
|
{
|
2016-03-16 03:07:11 +01:00
|
|
|
$data = $request->input();
|
2016-03-24 20:13:54 +01:00
|
|
|
$data['documents'] = $request->file('documents');
|
2016-09-25 19:36:15 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$action = Input::get('action');
|
|
|
|
$entityType = Input::get('entityType');
|
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
$invoice = $this->invoiceService->save($data, $request->entity());
|
2015-10-28 20:22:07 +01:00
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
$message = trans("texts.updated_{$entityType}");
|
|
|
|
Session::flash('message', $message);
|
2015-04-29 03:49:28 +02:00
|
|
|
|
2017-08-22 17:35:05 +02:00
|
|
|
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
|
|
|
|
2016-09-25 12:41:38 +02: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) {
|
2015-10-13 09:11:44 +02:00
|
|
|
$errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required');
|
|
|
|
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 {
|
2017-04-25 14:05:41 +02:00
|
|
|
$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 {
|
2017-04-25 14:05:41 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2017-03-13 20:27:32 +01:00
|
|
|
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
|
|
|
|
2016-09-25 12:41:38 +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
|
|
|
{
|
2017-08-22 17:35:05 +02: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();
|
2017-01-08 22:34:44 +01:00
|
|
|
$paymentId = $request->payment_id ? Payment::getPrivateId($request->payment_id) : false;
|
|
|
|
|
2016-03-24 23:15:52 +01:00
|
|
|
$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);
|
2016-04-19 04:35:18 +02:00
|
|
|
$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;
|
2017-07-04 21:58:33 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
foreach ($activities as $activity) {
|
2016-08-28 16:27:45 +02:00
|
|
|
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;
|
2016-08-28 16:27:45 +02:00
|
|
|
$key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName();
|
2017-01-08 22:34:44 +01:00
|
|
|
$versionsSelect[$lastId ?: 0] = $key;
|
2016-08-28 16:27:45 +02:00
|
|
|
$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) {
|
2016-08-28 16:27:45 +02:00
|
|
|
$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);
|
|
|
|
}
|
2016-07-27 14:46:05 +02:00
|
|
|
|
2016-09-25 19:36:15 +02:00
|
|
|
public function checkInvoiceNumber($invoicePublicId = false)
|
2016-07-27 14:46:05 +02:00
|
|
|
{
|
2016-09-18 11:26:18 +02:00
|
|
|
$invoiceNumber = request()->invoice_number;
|
2016-09-19 15:30:46 +02:00
|
|
|
|
2016-09-25 19:36:15 +02:00
|
|
|
$query = Invoice::scope()
|
2016-07-27 14:46:05 +02:00
|
|
|
->whereInvoiceNumber($invoiceNumber)
|
2016-09-25 19:36:15 +02:00
|
|
|
->withTrashed();
|
|
|
|
|
|
|
|
if ($invoicePublicId) {
|
|
|
|
$query->where('public_id', '!=', $invoicePublicId);
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = $query->count();
|
2016-07-27 14:46:05 +02:00
|
|
|
|
|
|
|
return $count ? RESULT_FAILURE : RESULT_SUCCESS;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|