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\InvoiceRequest;
|
2015-03-27 06:02:19 +01:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Country;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Invitation;
|
|
|
|
use App\Models\Invoice;
|
2015-03-27 06:02:19 +01:00
|
|
|
use App\Models\InvoiceDesign;
|
|
|
|
use App\Models\Product;
|
|
|
|
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\Mailers\ContactMailer as Mailer;
|
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
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;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-26 07:24:02 +01:00
|
|
|
class QuoteController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
protected $mailer;
|
|
|
|
protected $invoiceRepo;
|
|
|
|
protected $clientRepo;
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $invoiceService;
|
2016-04-28 14:16:33 +02:00
|
|
|
protected $entityType = ENTITY_INVOICE;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService)
|
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-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2016-11-24 10:22:37 +01:00
|
|
|
$datatable = new InvoiceDatatable();
|
|
|
|
$datatable->entityType = ENTITY_QUOTE;
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = [
|
|
|
|
'title' => trans('texts.quotes'),
|
|
|
|
'entityType' => ENTITY_QUOTE,
|
2016-11-24 10:22:37 +01:00
|
|
|
'datatable' => $datatable,
|
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_QUOTE, $search);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 21:30:39 +02:00
|
|
|
public function create(InvoiceRequest $request, $clientPublicId = 0)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! Utils::hasFeature(FEATURE_QUOTES)) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('/invoices/create');
|
|
|
|
}
|
|
|
|
|
2015-10-25 08:13:06 +01:00
|
|
|
$account = Auth::user()->account;
|
|
|
|
$clientId = null;
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($clientPublicId) {
|
2015-10-25 08:13:06 +01:00
|
|
|
$clientId = Client::getPrivateId($clientPublicId);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-10-25 08:13:06 +01:00
|
|
|
$invoice = $account->createInvoice(ENTITY_QUOTE, $clientId);
|
2015-10-28 20:22:07 +01:00
|
|
|
$invoice->public_id = 0;
|
|
|
|
|
2015-10-23 13:55:18 +02:00
|
|
|
$data = [
|
|
|
|
'entityType' => $invoice->getEntityType(),
|
|
|
|
'invoice' => $invoice,
|
|
|
|
'data' => Input::old('data'),
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => 'invoices',
|
|
|
|
'title' => trans('texts.new_quote'),
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array_merge($data, self::getViewModel());
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return View::make('invoices.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getViewModel()
|
|
|
|
{
|
2016-04-11 10:44:53 +02:00
|
|
|
$account = Auth::user()->account;
|
2016-05-26 14:50:23 +02:00
|
|
|
|
2015-03-27 06:02:19 +01:00
|
|
|
return [
|
|
|
|
'entityType' => ENTITY_QUOTE,
|
2017-02-10 08:43:46 +01:00
|
|
|
'account' => $account,
|
2017-05-17 11:18:48 +02:00
|
|
|
'products' => Product::scope()->orderBy('product_key')->get(),
|
2017-02-10 08:43:46 +01:00
|
|
|
'taxRateOptions' => $account->present()->taxRateOptions,
|
2016-07-04 18:49:01 +02:00
|
|
|
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
|
2015-03-27 06:02:19 +01:00
|
|
|
'taxRates' => TaxRate::scope()->orderBy('name')->get(),
|
2015-04-08 15:19:17 +02:00
|
|
|
'sizes' => Cache::get('sizes'),
|
|
|
|
'paymentTerms' => Cache::get('paymentTerms'),
|
2015-07-28 09:00:00 +02:00
|
|
|
'invoiceDesigns' => InvoiceDesign::getDesigns(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'invoiceFonts' => Cache::get('fonts'),
|
2015-08-10 17:48:41 +02:00
|
|
|
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
|
|
|
|
'isRecurring' => false,
|
2017-08-06 19:16:28 +02:00
|
|
|
'expenses' => [],
|
2015-03-27 06:02:19 +01:00
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function bulk()
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
$action = Input::get('bulk_action') ?: Input::get('action');
|
|
|
|
;
|
2015-11-05 23:37:04 +01:00
|
|
|
$ids = Input::get('bulk_public_id') ?: (Input::get('public_id') ?: Input::get('ids'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($action == 'convert') {
|
2015-11-05 23:37:04 +01:00
|
|
|
$invoice = Invoice::with('invoice_items')->scope($ids)->firstOrFail();
|
2016-01-20 09:41:57 +01:00
|
|
|
$clone = $this->invoiceService->convertQuote($invoice);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
Session::flash('message', trans('texts.converted_to_invoice'));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('invoices/'.$clone->public_id);
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
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) {
|
2017-07-19 16:34:09 +02:00
|
|
|
if ($action == 'markSent') {
|
|
|
|
$key = 'updated_quote';
|
|
|
|
} elseif ($action == 'download') {
|
|
|
|
$key = 'downloaded_quote';
|
|
|
|
} else {
|
|
|
|
$key = "{$action}d_quote";
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
$message = Utils::pluralize($key, $count);
|
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
2016-09-19 15:30:46 +02:00
|
|
|
return $this->returnBulk(ENTITY_QUOTE, $action, $ids);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function approve($invitationKey)
|
|
|
|
{
|
|
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.invitations')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
2017-12-06 12:44:23 +01:00
|
|
|
if ($invoice->due_date) {
|
|
|
|
$carbonDueDate = \Carbon::parse($invoice->due_date);
|
|
|
|
if (! $carbonDueDate->isToday() && ! $carbonDueDate->isFuture()) {
|
|
|
|
return redirect("view/{$invitationKey}")->withError(trans('texts.quote_has_expired'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 21:29:56 +01:00
|
|
|
if ($invoiceInvitationKey = $this->invoiceService->approveQuote($invoice, $invitation)) {
|
|
|
|
Session::flash('message', trans('texts.quote_is_approved'));
|
|
|
|
return Redirect::to("view/{$invoiceInvitationKey}");
|
|
|
|
} else {
|
|
|
|
return Redirect::to("view/{$invitationKey}");
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|