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

165 lines
5.1 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 Event;
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\Currency;
use App\Models\Industry;
use App\Models\InvoiceDesign;
use App\Models\PaymentTerm;
use App\Models\Product;
use App\Models\Size;
use App\Models\TaxRate;
use App\Models\Invitation;
use App\Models\Activity;
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\Events\QuoteInvitationWasApproved;
use App\Services\InvoiceService;
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;
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
{
parent::__construct();
$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()
{
if (!Utils::isPro()) {
return Redirect::to('/invoices/create');
}
$data = [
'title' => trans('texts.quotes'),
'entityType' => ENTITY_QUOTE,
'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');
return $this->invoiceRepo->getDatatable($accountId, $clientPublicId, ENTITY_QUOTE, $search);
}
public function create($clientPublicId = 0)
{
if (!Utils::isPro()) {
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());
2015-10-23 13:55:18 +02:00
2015-03-16 22:45:25 +01:00
return View::make('invoices.edit', $data);
}
private static function getViewModel()
{
2015-03-27 06:02:19 +01:00
return [
'entityType' => ENTITY_QUOTE,
'account' => Auth::user()->account,
'products' => Product::scope()->orderBy('id')->get(array('product_key', 'notes', 'cost', 'qty')),
2015-04-08 15:19:17 +02:00
'countries' => Cache::get('countries'),
2015-03-27 06:02:19 +01:00
'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(),
'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(),
'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-03-16 22:45:25 +01:00
if ($action == 'convert') {
$invoice = Invoice::with('invoice_items')->scope(Input::get('id'))->firstOrFail();
2015-10-28 20:22:07 +01:00
$clone = $this->invoiceService->approveQuote($invoice);
2015-03-16 22:45:25 +01:00
Session::flash('message', trans('texts.converted_to_invoice'));
return Redirect::to('invoices/'.$clone->public_id);
}
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) {
2015-10-29 15:42:05 +01:00
$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));
} else {
return Redirect::to("quotes");
}
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.converted_to_invoice'));
2015-03-16 22:45:25 +01:00
return Redirect::to("view/{$invitationKey}");
}
}