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;
|
2015-04-13 14:00:31 +02:00
|
|
|
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;
|
2015-04-13 14:00:31 +02:00
|
|
|
use App\Models\Invitation;
|
|
|
|
use App\Models\Activity;
|
2015-06-07 10:05:30 +02:00
|
|
|
use App\Models\Invoice;
|
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;
|
|
|
|
use App\Ninja\Repositories\TaxRateRepository;
|
2015-04-13 14:00:31 +02:00
|
|
|
use App\Events\QuoteApproved;
|
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;
|
|
|
|
protected $taxRateRepo;
|
|
|
|
|
|
|
|
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, TaxRateRepository $taxRateRepo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->invoiceRepo = $invoiceRepo;
|
|
|
|
$this->clientRepo = $clientRepo;
|
|
|
|
$this->taxRateRepo = $taxRateRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
if (!Utils::isPro()) {
|
|
|
|
return Redirect::to('/invoices/create');
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'title' => trans('texts.quotes'),
|
|
|
|
'entityType' => ENTITY_QUOTE,
|
2015-09-07 11:07:55 +02:00
|
|
|
'columns' => Utils::trans(['checkbox', 'quote_number', 'client', 'quote_date', 'quote_total', 'valid_until', 'status', 'action']),
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (Invoice::scope()->where('is_recurring', '=', true)->count() > 0)
|
|
|
|
{
|
|
|
|
$data['secEntityType'] = ENTITY_RECURRING_INVOICE;
|
|
|
|
$data['secColumns'] = Utils::trans(['checkbox', 'frequency', 'client', 'start_date', 'end_date', 'quote_total', 'action']);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return View::make('list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = null;
|
2015-10-22 20:48:12 +02:00
|
|
|
$invoiceNumber = Auth::user()->account->getDefaultInvoiceNumber(true);
|
2015-03-16 22:45:25 +01:00
|
|
|
$account = Account::with('country')->findOrFail(Auth::user()->account_id);
|
|
|
|
|
|
|
|
if ($clientPublicId) {
|
|
|
|
$client = Client::scope($clientPublicId)->firstOrFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'account' => $account,
|
|
|
|
'invoice' => null,
|
|
|
|
'data' => Input::old('data'),
|
|
|
|
'invoiceNumber' => $invoiceNumber,
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => 'invoices',
|
|
|
|
'title' => trans('texts.new_quote'),
|
|
|
|
'client' => $client, );
|
|
|
|
$data = array_merge($data, self::getViewModel());
|
|
|
|
|
|
|
|
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'),
|
2015-09-10 19:50:09 +02:00
|
|
|
'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(),
|
2015-08-10 17:48:41 +02:00
|
|
|
'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()
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
|
|
|
|
|
|
|
if ($action == 'convert') {
|
|
|
|
$invoice = Invoice::with('invoice_items')->scope(Input::get('id'))->firstOrFail();
|
|
|
|
$clone = $this->invoiceRepo->cloneInvoice($invoice, $invoice->id);
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.converted_to_invoice'));
|
|
|
|
return Redirect::to('invoices/'.$clone->public_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$statusId = Input::get('statusId');
|
|
|
|
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
|
|
|
|
$count = $this->invoiceRepo->bulk($ids, $action, $statusId);
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
$key = $action == 'mark' ? "updated_quote" : "{$action}d_quote";
|
|
|
|
$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;
|
|
|
|
|
|
|
|
if ($invoice->is_quote && !$invoice->quote_invoice_id) {
|
2015-04-13 14:00:31 +02:00
|
|
|
Event::fire(new QuoteApproved($invoice));
|
|
|
|
Activity::approveQuote($invitation);
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$invoice = $this->invoiceRepo->cloneInvoice($invoice, $invoice->id);
|
|
|
|
Session::flash('message', trans('texts.converted_to_invoice'));
|
|
|
|
|
|
|
|
foreach ($invoice->invitations as $invitationClone) {
|
|
|
|
if ($invitation->contact_id == $invitationClone->contact_id) {
|
|
|
|
$invitationKey = $invitationClone->invitation_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to("view/{$invitationKey}");
|
|
|
|
}
|
|
|
|
}
|