1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 09:51:35 +02:00
invoiceninja/app/Http/Controllers/QuoteController.php

175 lines
5.5 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-03-26 07:24:02 +01:00
use Auth;
use Input;
use Redirect;
2015-03-17 02:30:56 +01:00
use Utils;
2015-03-26 07:24:02 +01:00
use View;
2015-04-08 15:19:17 +02:00
use Cache;
use Session;
2015-03-27 06:02:19 +01:00
use App\Models\Account;
use App\Models\Client;
use App\Models\Country;
use App\Models\InvoiceDesign;
use App\Models\Product;
use App\Models\TaxRate;
use App\Models\Invitation;
2015-06-07 10:05:30 +02:00
use App\Models\Invoice;
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\ClientRepository;
2015-10-28 20:22:07 +01:00
use App\Services\InvoiceService;
2016-05-01 21:30:39 +02:00
use App\Http\Requests\InvoiceRequest;
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()
{
$data = [
'title' => trans('texts.quotes'),
'entityType' => ENTITY_QUOTE,
2016-03-17 23:04:03 +01:00
'sortCol' => '3',
'columns' => Utils::trans([
'checkbox',
'quote_number',
'client',
'quote_date',
'quote_total',
'valid_until',
'status',
'action'
]),
2015-03-16 22:45:25 +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_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
{
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
// Tax rate $options
$account = Auth::user()->account;
$rates = TaxRate::scope()->orderBy('name')->get();
$options = [];
$defaultTax = false;
2016-05-26 14:50:23 +02:00
2016-04-11 10:44:53 +02:00
foreach ($rates as $rate) {
2016-05-26 14:50:23 +02:00
$options[$rate->rate . ' ' . $rate->name] = $rate->name . ' ' . ($rate->rate+0) . '%';
2016-04-11 10:44:53 +02:00
// load default invoice tax
if ($rate->id == $account->default_tax_rate_id) {
$defaultTax = $rate;
}
2016-05-26 14:50:23 +02:00
}
2015-03-27 06:02:19 +01:00
return [
'entityType' => ENTITY_QUOTE,
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(['product_key', 'notes', 'cost', 'qty']),
2016-04-11 10:44:53 +02:00
'taxRateOptions' => $options,
'defaultTax' => $defaultTax,
2015-04-08 15:19:17 +02:00
'countries' => Cache::get('countries'),
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
'currencies' => Cache::get('currencies'),
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'languages' => Cache::get('languages'),
2015-04-08 15:19:17 +02:00
'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'),
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
'isRecurring' => false,
2015-03-27 06:02:19 +01:00
];
2015-03-16 22:45:25 +01:00
}
public function bulk()
{
2015-10-28 20:22:07 +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();
$clone = $this->invoiceService->convertQuote($invoice);
2015-03-16 22:45:25 +01:00
Session::flash('message', trans('texts.converted_to_invoice'));
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) {
$key = $action == 'markSent' ? 'updated_quote' : "{$action}d_quote";
2015-03-16 22:45:25 +01:00
$message = Utils::pluralize($key, $count);
Session::flash('message', $message);
}
2015-08-20 10:02:03 +02:00
if ($action == 'restore' && $count == 1) {
return Redirect::to('quotes/'.Utils::getFirst($ids));
2015-08-20 10:02:03 +02:00
} else {
return Redirect::to('quotes');
2015-08-20 10:02:03 +02:00
}
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;
2015-10-28 20:22:07 +01:00
$invitationKey = $this->invoiceService->approveQuote($invoice, $invitation);
Session::flash('message', trans('texts.quote_is_approved'));
2015-03-16 22:45:25 +01:00
return Redirect::to("view/{$invitationKey}");
}
}