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-30 21:45:10 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Product;
|
2016-03-24 23:15:52 +01:00
|
|
|
use App\Models\Expense;
|
2015-03-30 21:45:10 +02:00
|
|
|
use App\Models\TaxRate;
|
|
|
|
use App\Models\InvoiceDesign;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Models\Activity;
|
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;
|
2016-03-23 03:23:45 +01:00
|
|
|
use App\Ninja\Repositories\DocumentRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Services\InvoiceService;
|
2015-11-05 23:37:04 +01:00
|
|
|
use App\Services\RecurringInvoiceService;
|
2016-05-01 21:30:39 +02:00
|
|
|
|
|
|
|
use App\Http\Requests\InvoiceRequest;
|
|
|
|
use App\Http\Requests\CreateInvoiceRequest;
|
|
|
|
use App\Http\Requests\UpdateInvoiceRequest;
|
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;
|
2016-03-23 03:23:45 +01:00
|
|
|
protected $documentRepo;
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $invoiceService;
|
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
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService)
|
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->mailer = $mailer;
|
|
|
|
$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;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'title' => trans('texts.invoices'),
|
|
|
|
'entityType' => ENTITY_INVOICE,
|
2016-03-17 23:04:03 +01:00
|
|
|
'sortCol' => '3',
|
2015-10-29 18:21:00 +01:00
|
|
|
'columns' => Utils::trans([
|
|
|
|
'checkbox',
|
|
|
|
'invoice_number',
|
|
|
|
'client',
|
|
|
|
'invoice_date',
|
|
|
|
'invoice_total',
|
|
|
|
'balance_due',
|
|
|
|
'due_date',
|
|
|
|
'status',
|
2015-11-06 00:14:00 +01:00
|
|
|
''
|
2015-10-29 18:21:00 +01:00
|
|
|
]),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
2015-10-29 18:21:00 +01:00
|
|
|
return response()->view('list', $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-03-16 00:08:00 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$entityType = $invoice->getEntityType();
|
2016-05-01 21:30:39 +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) {
|
2015-10-28 20:22:07 +01:00
|
|
|
$invoice->id = $invoice->public_id = null;
|
2015-10-23 13:55:18 +02:00
|
|
|
$invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
|
2015-03-16 22:45:25 +01:00
|
|
|
$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';
|
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);
|
2016-01-01 05:59:02 +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-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')];
|
|
|
|
}
|
2016-03-22 14:17:46 +01:00
|
|
|
|
|
|
|
foreach ($invoice->payments as $payment) {
|
|
|
|
$label = trans("texts.view_payment");
|
|
|
|
if (count($invoice->payments) > 1) {
|
|
|
|
$label .= ' - ' . $account->formatMoney($payment->amount, $invoice->client);
|
|
|
|
}
|
|
|
|
$actions[] = ['url' => $payment->present()->url, 'label' => $label];
|
|
|
|
}
|
2015-06-04 22:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-03-17 15:11:14 +01:00
|
|
|
if(!Auth::user()->hasPermission('view_all')){
|
|
|
|
$clients = $clients->where('clients.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array(
|
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,
|
2015-08-30 14:08:15 +02:00
|
|
|
'actions' => $actions,
|
|
|
|
'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
|
|
|
|
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();
|
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();
|
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-05-01 21:30:39 +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;
|
2016-03-24 23:15:52 +01:00
|
|
|
|
2016-05-01 21:30:39 +02:00
|
|
|
if (Session::get('expenses')) {
|
2016-03-24 23:15:52 +01:00
|
|
|
$invoice->expenses = Expense::scope(Session::get('expenses'))->with('documents')->get();
|
|
|
|
}
|
|
|
|
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-03-17 15:11:14 +01:00
|
|
|
$clients = Client::scope()->with('contacts', 'country')->orderBy('name');
|
2016-05-01 21:30:39 +02:00
|
|
|
if (!Auth::user()->hasPermission('view_all')) {
|
2016-03-17 15:11:14 +01:00
|
|
|
$clients = $clients->where('clients.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-01-01 05:59:02 +01:00
|
|
|
$recurringDueDateHelp = '';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-01-01 23:37:47 +01:00
|
|
|
// Create due date options
|
|
|
|
$recurringDueDates = array(
|
|
|
|
trans('texts.use_client_terms') => array('value' => '', 'class' => 'monthly weekly'),
|
|
|
|
);
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-01-01 23:37:47 +01:00
|
|
|
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
|
|
|
|
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-01-01 23:37:47 +01:00
|
|
|
$dayStr = str_pad($i, 2, '0', STR_PAD_LEFT);
|
|
|
|
$str = trans('texts.day_of_month', array('ordinal'=>$ordinal));
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-01-01 23:37:47 +01:00
|
|
|
$recurringDueDates[$str] = array('value' => "1998-01-$dayStr", 'data-num' => $i, 'class' => 'monthly');
|
|
|
|
}
|
|
|
|
$recurringDueDates[trans('texts.last_day_of_month')] = array('value' => "1998-01-31", 'data-num' => 31, 'class' => 'monthly');
|
2016-01-18 10:13:39 +01:00
|
|
|
|
|
|
|
|
2016-01-01 23:37:47 +01:00
|
|
|
$daysOfWeek = array(
|
|
|
|
trans('texts.sunday'),
|
|
|
|
trans('texts.monday'),
|
|
|
|
trans('texts.tuesday'),
|
|
|
|
trans('texts.wednesday'),
|
|
|
|
trans('texts.thursday'),
|
|
|
|
trans('texts.friday'),
|
|
|
|
trans('texts.saturday'),
|
|
|
|
);
|
|
|
|
foreach(array('1st','2nd','3rd','4th') as $i=>$ordinal){
|
|
|
|
foreach($daysOfWeek as $j=>$dayOfWeek){
|
|
|
|
$str = trans('texts.day_of_week_after', array('ordinal' => $ordinal, 'day' => $dayOfWeek));
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-01-01 23:37:47 +01:00
|
|
|
$day = $i * 7 + $j + 1;
|
|
|
|
$dayStr = str_pad($day, 2, '0', STR_PAD_LEFT);
|
|
|
|
$recurringDueDates[$str] = array('value' => "1998-02-$dayStr", 'data-num' => $day, 'class' => 'weekly');
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2016-03-29 16:30:28 +02:00
|
|
|
// Tax rate $options
|
|
|
|
$account = Auth::user()->account;
|
2016-03-27 17:06:02 +02:00
|
|
|
$rates = TaxRate::scope()->orderBy('name')->get();
|
2016-03-29 16:30:28 +02:00
|
|
|
$options = [];
|
|
|
|
$defaultTax = false;
|
|
|
|
|
2016-03-27 17:06:02 +02:00
|
|
|
foreach ($rates as $rate) {
|
2016-03-29 16:30:28 +02:00
|
|
|
$options[$rate->rate . ' ' . $rate->name] = $rate->name . ' ' . ($rate->rate+0) . '%';
|
|
|
|
|
|
|
|
// load default invoice tax
|
|
|
|
if ($rate->id == $account->default_tax_rate_id) {
|
|
|
|
$defaultTax = $rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any taxes which have been deleted
|
|
|
|
if ($invoice->exists) {
|
|
|
|
foreach ($invoice->getTaxes() as $key => $rate) {
|
|
|
|
if (isset($options[$key])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$options[$key] = $rate['name'] . ' ' . $rate['rate'] . '%';
|
|
|
|
}
|
2016-03-27 17:06:02 +02:00
|
|
|
}
|
2016-03-29 16:30:28 +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'),
|
2016-01-27 21:38:21 +01:00
|
|
|
'products' => Product::scope()->with('default_tax_rate')->orderBy('product_key')->get(),
|
2016-03-29 16:30:28 +02:00
|
|
|
'taxRateOptions' => $options,
|
|
|
|
'defaultTax' => $defaultTax,
|
2015-03-30 21:45:10 +02:00
|
|
|
'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(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'invoiceFonts' => Cache::get('fonts'),
|
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',
|
|
|
|
),
|
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(),
|
2015-07-12 21:43:45 +02:00
|
|
|
'tasks' => Session::get('tasks') ? json_encode(Session::get('tasks')) : null,
|
2016-01-31 14:10:33 +01:00
|
|
|
'expenseCurrencyId' => Session::get('expenseCurrencyId') ?: 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
|
|
|
|
*/
|
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-05-02 19:42:13 +02:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
$action = Input::get('action');
|
|
|
|
$entityType = Input::get('entityType');
|
2016-01-26 21:22:33 +01: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}");
|
|
|
|
|
|
|
|
// check if we created a new client with the invoice
|
|
|
|
// TODO: replace with HistoryListener
|
|
|
|
$input = $request->input();
|
|
|
|
$clientPublicId = isset($input['client']['public_id']) ? $input['client']['public_id'] : false;
|
|
|
|
if ($clientPublicId == '-1') {
|
|
|
|
$message = $message.' '.trans('texts.and_created_client');
|
|
|
|
$trackUrl = URL::to('clients/' . $invoice->client->public_id);
|
|
|
|
Utils::trackViewed($invoice->client->getDisplayName(), ENTITY_CLIENT, $trackUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::flash('message', $message);
|
|
|
|
|
|
|
|
if ($action == 'email') {
|
|
|
|
return $this->emailInvoice($invoice, Input::get('pdfupload'));
|
|
|
|
}
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return redirect()->to($invoice->getRoute());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @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-05-02 19:42:13 +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
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
if ($action == 'clone') {
|
2016-05-01 21:36:12 +02:00
|
|
|
return $this->cloneInvoice($request, $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') {
|
|
|
|
return $this->emailInvoice($invoice, Input::get('pdfupload'));
|
2015-10-13 09:11:44 +02:00
|
|
|
}
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return redirect()->to($invoice->getRoute());
|
2015-10-13 09:11:44 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +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);
|
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 {
|
|
|
|
$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()) {
|
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();
|
|
|
|
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 {
|
|
|
|
return $this->mailer->sendInvoice($invoice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function bulk($entityType = ENTITY_INVOICE)
|
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$action = Input::get('bulk_action') ?: Input::get('action');;
|
|
|
|
$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) {
|
2015-10-29 15:42:05 +01:00
|
|
|
$key = $action == 'markSent' ? "updated_{$entityType}" : "{$action}d_{$entityType}";
|
2015-03-16 22:45:25 +01:00
|
|
|
$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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-01 22:55:13 +02:00
|
|
|
|
|
|
|
return Redirect::to('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
|
|
|
{
|
2016-05-01 21:30:39 +02:00
|
|
|
return self::edit($request, $publicId, true);
|
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();
|
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),
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
$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')
|
2015-10-28 20:22:07 +01:00
|
|
|
->get(['id', 'created_at', 'user_id', 'json_backup']);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$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);
|
2016-05-09 19:35:50 +02:00
|
|
|
$backup->features = [
|
2016-04-19 04:35:18 +02:00
|
|
|
'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
|
|
|
$backup->is_quote = isset($backup->is_quote) && intval($backup->is_quote);
|
|
|
|
$backup->account = $invoice->account->toArray();
|
|
|
|
|
|
|
|
$versionsJson[$activity->id] = $backup;
|
2015-10-28 20:22:07 +01:00
|
|
|
$key = Utils::timestampToDateTimeString(strtotime($activity->created_at)) . ' - ' . $activity->user->getDisplayName();
|
2015-03-16 22:45:25 +01:00
|
|
|
$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(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'invoiceFonts' => Cache::get('fonts'),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('invoices.history', $data);
|
|
|
|
}
|
|
|
|
}
|