1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/controllers/InvoiceController.php

429 lines
14 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2013-12-25 22:34:42 +01:00
use ninja\mailers\ContactMailer as Mailer;
use ninja\repositories\InvoiceRepository;
use ninja\repositories\ClientRepository;
2013-12-29 12:28:44 +01:00
use ninja\repositories\TaxRateRepository;
2013-12-10 18:18:35 +01:00
2013-11-26 13:45:07 +01:00
class InvoiceController extends \BaseController {
2013-12-10 18:18:35 +01:00
protected $mailer;
2013-12-25 22:34:42 +01:00
protected $invoiceRepo;
protected $clientRepo;
2013-12-29 12:28:44 +01:00
protected $taxRateRepo;
2013-12-10 18:18:35 +01:00
2013-12-29 12:28:44 +01:00
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, TaxRateRepository $taxRateRepo)
2013-12-10 18:18:35 +01:00
{
parent::__construct();
$this->mailer = $mailer;
2013-12-25 22:34:42 +01:00
$this->invoiceRepo = $invoiceRepo;
$this->clientRepo = $clientRepo;
2013-12-29 12:28:44 +01:00
$this->taxRateRepo = $taxRateRepo;
2013-12-10 18:18:35 +01:00
}
2013-11-26 13:45:07 +01:00
public function index()
{
2013-12-11 12:11:59 +01:00
$data = [
2013-12-03 23:00:01 +01:00
'title' => '- Invoices',
2013-12-11 12:11:59 +01:00
'entityType'=>ENTITY_INVOICE,
2014-03-27 13:25:31 +01:00
'columns'=>Utils::trans(['checkbox', 'invoice_number', 'client', 'invoice_date', 'invoice_total', 'balance_due', 'due_date', 'status', 'action'])
2013-12-11 12:11:59 +01:00
];
if (Invoice::scope()->where('is_recurring', '=', true)->count() > 0)
2013-12-11 12:11:59 +01:00
{
$data['secEntityType'] = ENTITY_RECURRING_INVOICE;
2014-03-27 13:25:31 +01:00
$data['secColumns'] = Utils::trans(['checkbox', 'frequency', 'client', 'start_date', 'end_date', 'invoice_total', 'action']);
2013-12-11 12:11:59 +01:00
}
return View::make('list', $data);
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
public function getDatatable($clientPublicId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-25 22:34:42 +01:00
$query = $this->invoiceRepo->getInvoices(Auth::user()->account_id, $clientPublicId, Input::get('sSearch'));
2013-12-05 21:25:20 +01:00
$table = Datatable::query($query);
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
2013-11-29 13:09:21 +01:00
}
2013-12-04 17:20:14 +01:00
$table->addColumn('invoice_number', function($model) { return link_to('invoices/' . $model->public_id . '/edit', $model->invoice_number); });
2013-12-01 21:58:25 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
2014-02-04 14:16:22 +01:00
$table->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
2013-12-01 21:58:25 +01:00
}
2013-12-11 21:33:44 +01:00
return $table->addColumn('invoice_date', function($model) { return Utils::fromSqlDate($model->invoice_date); })
2014-02-04 14:16:22 +01:00
->addColumn('amount', function($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
2013-12-29 18:40:11 +01:00
->addColumn('balance', function($model) { return Utils::formatMoney($model->balance, $model->currency_id); })
2013-12-07 21:33:07 +01:00
->addColumn('due_date', function($model) { return Utils::fromSqlDate($model->due_date); })
2013-12-05 21:25:20 +01:00
->addColumn('invoice_status_name', function($model) { return $model->invoice_status_name; })
2013-12-01 21:58:25 +01:00
->addColumn('dropdown', function($model)
{
2013-12-05 21:25:20 +01:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-01 21:58:25 +01:00
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
2014-03-27 13:25:31 +01:00
'.trans('texts.select').' <span class="caret"></span>
2013-12-01 21:58:25 +01:00
</button>
<ul class="dropdown-menu" role="menu">
2014-03-27 13:25:31 +01:00
<li><a href="' . URL::to('invoices/'.$model->public_id.'/edit') . '">'.trans('texts.edit_invoice').'</a></li>
<li><a href="' . URL::to('payments/create/' . $model->client_public_id . '/' . $model->public_id ) . '">'.trans('texts.enter_payment').'</a></li>
2014-01-15 15:01:24 +01:00
<li class="divider"></li>
2014-03-27 13:25:31 +01:00
<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>
2013-12-01 21:58:25 +01:00
</ul>
</div>';
})
2013-11-29 13:09:21 +01:00
->make();
2013-11-26 13:45:07 +01:00
}
2013-12-11 12:11:59 +01:00
public function getRecurringDatatable($clientPublicId = null)
{
2013-12-25 22:34:42 +01:00
$query = $this->invoiceRepo->getRecurringInvoices(Auth::user()->account_id, $clientPublicId, Input::get('sSearch'));
2013-12-11 12:11:59 +01:00
$table = Datatable::query($query);
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
}
2013-12-11 21:33:44 +01:00
$table->addColumn('frequency', function($model) { return link_to('invoices/' . $model->public_id, $model->frequency); });
2013-12-11 12:11:59 +01:00
if (!$clientPublicId) {
2014-03-23 21:56:19 +01:00
$table->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
2013-12-11 12:11:59 +01:00
}
2013-12-11 21:33:44 +01:00
return $table->addColumn('start_date', function($model) { return Utils::fromSqlDate($model->start_date); })
2013-12-11 12:11:59 +01:00
->addColumn('end_date', function($model) { return Utils::fromSqlDate($model->end_date); })
2013-12-29 18:40:11 +01:00
->addColumn('total', function($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
2013-12-11 12:11:59 +01:00
->addColumn('dropdown', function($model)
{
return '<div class="btn-group tr-action" style="visibility:hidden;">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
2014-03-27 13:25:31 +01:00
'.trans('texts.select').' <span class="caret"></span>
2013-12-11 12:11:59 +01:00
</button>
<ul class="dropdown-menu" role="menu">
2014-03-27 13:25:31 +01:00
<li><a href="' . URL::to('invoices/'.$model->public_id.'/edit') . '">'.trans('texts.edit_invoice').'</a></li>
2013-12-11 12:11:59 +01:00
<li class="divider"></li>
2014-03-27 13:25:31 +01:00
<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>
2013-12-11 12:11:59 +01:00
</ul>
</div>';
})
->make();
}
2013-11-26 13:45:07 +01:00
2013-12-04 17:20:14 +01:00
public function view($invitationKey)
2013-11-26 13:45:07 +01:00
{
2014-02-18 22:56:18 +01:00
$invitation = Invitation::withTrashed()->with('user', 'invoice.invoice_items', 'invoice.account.country', 'invoice.client.contacts', 'invoice.client.country')
2013-12-31 17:38:49 +01:00
->where('invitation_key', '=', $invitationKey)->firstOrFail();
2013-12-03 18:32:33 +01:00
$invoice = $invitation->invoice;
2014-01-06 19:03:00 +01:00
2013-12-31 17:38:49 +01:00
if (!$invoice || $invoice->is_deleted)
{
2013-12-15 13:55:50 +01:00
return View::make('invoices.deleted');
}
$client = $invoice->client;
2014-01-06 19:03:00 +01:00
2013-12-31 17:38:49 +01:00
if (!$client || $client->is_deleted)
{
2013-12-15 13:55:50 +01:00
return View::make('invoices.deleted');
}
2013-11-26 22:45:10 +01:00
2014-04-02 21:03:01 +02:00
if (!Auth::check() || Auth::user()->account_id != $invoice->account_id)
{
Activity::viewInvoice($invitation);
Event::fire('invoice.viewed', $invoice);
}
2013-12-01 21:58:25 +01:00
2014-01-06 19:03:00 +01:00
$client->account->loadLocalizationSettings();
2013-12-03 18:32:33 +01:00
2014-01-09 16:26:27 +01:00
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
2013-12-07 19:45:00 +01:00
$data = array(
2014-02-17 00:09:34 +01:00
'showBreadcrumbs' => false,
2013-12-31 17:38:49 +01:00
'invoice' => $invoice->hidePrivateFields(),
2014-03-23 13:53:16 +01:00
'invitation' => $invitation,
'invoiceLabels' => $client->account->getInvoiceLabels(),
2013-12-07 19:45:00 +01:00
);
return View::make('invoices.view', $data);
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
public function edit($publicId)
2013-11-28 20:06:38 +01:00
{
2014-02-18 22:56:18 +01:00
$invoice = Invoice::scope($publicId)->withTrashed()->with('account.country', 'client.contacts', 'client.country', 'invoice_items')->firstOrFail();
2014-01-01 00:50:13 +01:00
Utils::trackViewed($invoice->invoice_number . ' - ' . $invoice->client->getDisplayName(), ENTITY_INVOICE);
2014-01-09 16:26:27 +01:00
$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);
$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');
2013-11-28 20:06:38 +01:00
$data = array(
2014-02-17 00:09:34 +01:00
'showBreadcrumbs' => false,
2013-12-01 21:58:25 +01:00
'account' => $invoice->account,
2013-11-28 20:06:38 +01:00
'invoice' => $invoice,
2014-01-01 16:23:32 +01:00
'data' => false,
2013-11-28 20:06:38 +01:00
'method' => 'PUT',
'invitationContactIds' => $contactIds,
2013-12-04 17:20:14 +01:00
'url' => 'invoices/' . $publicId,
2013-12-03 23:00:01 +01:00
'title' => '- ' . $invoice->invoice_number,
2013-12-09 10:38:49 +01:00
'client' => $invoice->client);
$data = array_merge($data, InvoiceController::getViewModel());
2013-11-28 20:06:38 +01:00
return View::make('invoices.edit', $data);
}
2013-12-04 17:20:14 +01:00
public function create($clientPublicId = 0)
2013-11-26 13:45:07 +01:00
{
$client = null;
2013-12-01 13:22:08 +01:00
$invoiceNumber = Auth::user()->account->getNextInvoiceNumber();
2013-12-03 18:32:33 +01:00
$account = Account::with('country')->findOrFail(Auth::user()->account_id);
2014-02-01 21:01:32 +01:00
if ($clientPublicId)
{
2013-12-04 17:20:14 +01:00
$client = Client::scope($clientPublicId)->firstOrFail();
2014-02-01 21:01:32 +01:00
}
2013-12-01 21:58:25 +01:00
2013-11-26 13:45:07 +01:00
$data = array(
2013-12-01 21:58:25 +01:00
'account' => $account,
2014-01-01 16:23:32 +01:00
'invoice' => null,
'data' => Input::old('data'),
2013-12-01 13:22:08 +01:00
'invoiceNumber' => $invoiceNumber,
2013-11-26 13:45:07 +01:00
'method' => 'POST',
'url' => 'invoices',
2013-12-03 23:00:01 +01:00
'title' => '- New Invoice',
2014-01-01 16:23:32 +01:00
'client' => $client);
2013-12-09 10:38:49 +01:00
$data = array_merge($data, InvoiceController::getViewModel());
2013-11-26 13:45:07 +01:00
return View::make('invoices.edit', $data);
}
2013-12-11 12:11:59 +01:00
public static function getViewModel()
2013-12-09 10:38:49 +01:00
{
return [
'account' => Auth::user()->account,
2014-01-29 11:41:38 +01:00
'products' => Product::scope()->orderBy('id')->get(array('product_key','notes','cost','qty')),
2013-12-31 00:19:17 +01:00
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-06 19:03:00 +01:00
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
2013-12-29 00:33:48 +01:00
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-06 19:03:00 +01:00
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
2013-12-31 10:43:39 +01:00
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
2014-02-18 22:56:18 +01:00
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
2014-03-23 13:53:16 +01:00
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
2013-12-09 10:38:49 +01:00
'frequencies' => array(
1 => 'Weekly',
2 => 'Two weeks',
3 => 'Four weeks',
4 => 'Monthly',
5 => 'Three months',
6 => 'Six months',
7 => 'Annually'
)
];
}
2013-11-26 13:45:07 +01:00
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
return InvoiceController::save();
}
2013-12-04 17:20:14 +01:00
private function save($publicId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-03 18:32:33 +01:00
$action = Input::get('action');
2013-12-07 19:45:00 +01:00
2013-12-05 21:25:20 +01:00
if ($action == 'archive' || $action == 'delete')
2013-12-03 18:32:33 +01:00
{
2013-12-05 21:25:20 +01:00
return InvoiceController::bulk();
2013-12-03 18:32:33 +01:00
}
2013-11-26 13:45:07 +01:00
2013-12-30 21:17:45 +01:00
$input = json_decode(Input::get('data'));
2014-02-03 23:56:23 +01:00
2014-01-01 16:23:32 +01:00
$invoice = $input->invoice;
if ($errors = $this->invoiceRepo->getErrors($invoice))
2014-03-05 21:19:12 +01:00
{
2014-04-02 14:28:23 +02:00
Session::flash('error', trans('texts.invoice_error'));
2014-03-05 21:19:12 +01:00
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices/create')
2014-01-01 16:23:32 +01:00
->withInput()->withErrors($errors);
2013-12-25 22:34:42 +01:00
}
else
{
2013-12-29 12:28:44 +01:00
$this->taxRateRepo->save($input->tax_rates);
2014-01-01 16:23:32 +01:00
$clientData = (array) $invoice->client;
$client = $this->clientRepo->save($invoice->client->public_id, $clientData);
$invoiceData = (array) $invoice;
2013-12-25 22:34:42 +01:00
$invoiceData['client_id'] = $client->id;
$invoice = $this->invoiceRepo->save($publicId, $invoiceData);
2013-12-31 20:49:54 +01:00
$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)
2013-11-26 13:45:07 +01:00
{
2013-12-31 20:49:54 +01:00
$account->invoice_taxes = $input->invoice_taxes;
$account->invoice_item_taxes = $input->invoice_item_taxes;
$account->invoice_design_id = $input->invoice->invoice_design_id;
2013-12-31 20:49:54 +01:00
$account->save();
}
2013-12-31 17:38:49 +01:00
2013-12-25 22:34:42 +01:00
$client->load('contacts');
$sendInvoiceIds = [];
foreach ($client->contacts as $contact)
{
2014-01-06 19:03:00 +01:00
if ($contact->send_invoice || count($client->contacts) == 1)
{
2013-12-25 22:34:42 +01:00
$sendInvoiceIds[] = $contact->id;
}
}
2013-12-04 17:20:14 +01:00
2013-12-25 22:34:42 +01:00
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;
2014-01-13 20:22:43 +01:00
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
$invitation->save();
}
}
2014-04-02 14:28:23 +02:00
$message = trans($publicId ? 'texts.updated_invoice' : 'texts.created_invoice');
2014-01-01 16:23:32 +01:00
if ($input->invoice->client->public_id == '-1')
2013-12-07 19:45:00 +01:00
{
2014-04-02 14:28:23 +02:00
$message = $message . ' ' . trans('texts.and_created_client');
2014-01-06 07:48:11 +01:00
2014-04-02 14:28:23 +02:00
$url = URL::to('clients/' . $client->public_id);
2014-01-01 00:50:13 +01:00
Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT, $url);
2013-11-26 13:45:07 +01:00
}
2013-12-25 22:34:42 +01:00
2013-12-11 21:33:44 +01:00
if ($action == 'clone')
{
return InvoiceController::cloneInvoice($publicId);
}
else if ($action == 'email')
2014-02-02 21:13:28 +01:00
{
if (Auth::user()->confirmed)
{
$this->mailer->sendInvoice($invoice);
2014-04-02 14:28:23 +02:00
Session::flash('message', $message);
2014-02-02 21:13:28 +01:00
}
else
{
2014-04-02 14:28:23 +02:00
$errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required');
Session::flash('error', $errorMessage);
Session::flash('message', $message);
2014-02-02 21:13:28 +01:00
}
2013-12-25 22:34:42 +01:00
}
else
{
2014-04-02 14:28:23 +02:00
Session::flash('message', $message);
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
$url = 'invoices/' . $invoice->public_id . '/edit';
2013-11-28 17:40:13 +01:00
return Redirect::to($url);
2013-11-26 13:45:07 +01:00
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function show($publicId)
2013-11-26 13:45:07 +01:00
{
2014-04-06 12:18:34 +02:00
Session::reflash();
2013-12-04 17:20:14 +01:00
return Redirect::to('invoices/'.$publicId.'/edit');
2013-11-26 13:45:07 +01:00
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function update($publicId)
2013-11-26 13:45:07 +01:00
{
2013-12-04 17:20:14 +01:00
return InvoiceController::save($publicId);
2013-11-26 13:45:07 +01:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
2013-12-01 08:33:17 +01:00
public function bulk()
2013-11-26 13:45:07 +01:00
{
2013-12-01 08:33:17 +01:00
$action = Input::get('action');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2014-01-12 19:55:33 +01:00
$count = $this->invoiceRepo->bulk($ids, $action);
2013-12-01 08:33:17 +01:00
2014-01-12 19:55:33 +01:00
if ($count > 0)
{
$message = Utils::pluralize('Successfully '.$action.'d ? invoice', $count);
Session::flash('message', $message);
2013-12-01 08:33:17 +01:00
}
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices');
}
2013-12-11 21:33:44 +01:00
public static function cloneInvoice($publicId)
{
$invoice = Invoice::with('invoice_items')->scope($publicId)->firstOrFail();
$clone = Invoice::createNew();
2014-02-20 15:42:09 +01:00
$clone->balance = $invoice->amount;
foreach (['client_id', 'discount', 'invoice_date', 'due_date', 'is_recurring', 'frequency_id', 'start_date', 'end_date', 'terms', 'public_notes', 'invoice_design_id', 'tax_name', 'tax_rate', 'amount'] as $field)
2013-12-11 21:33:44 +01:00
{
$clone->$field = $invoice->$field;
2014-02-20 15:42:09 +01:00
}
2013-12-11 21:33:44 +01:00
if (!$clone->is_recurring)
2013-12-11 21:33:44 +01:00
{
$clone->invoice_number = Auth::user()->account->getNextInvoiceNumber();
}
$clone->save();
foreach ($invoice->invoice_items as $item)
{
$cloneItem = InvoiceItem::createNew();
2013-12-30 21:17:45 +01:00
foreach (['product_id', 'product_key', 'notes', 'cost', 'qty', 'tax_name', 'tax_rate'] as $field)
2013-12-11 21:33:44 +01:00
{
$cloneItem->$field = $item->$field;
}
$clone->invoice_items()->save($cloneItem);
}
2014-04-02 14:28:23 +02:00
Session::flash('message', trans('texts.cloned_invoice'));
2013-12-11 21:33:44 +01:00
return Redirect::to('invoices/' . $clone->public_id);
}
2013-12-05 21:25:20 +01:00
}