1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Http/Controllers/ProposalController.php

179 lines
5.6 KiB
PHP
Raw Normal View History

2018-01-30 19:58:55 +01:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateProposalRequest;
use App\Http\Requests\ProposalRequest;
use App\Http\Requests\UpdateProposalRequest;
2018-02-12 10:34:48 +01:00
use App\Jobs\SendInvoiceEmail;
2018-01-30 19:58:55 +01:00
use App\Models\Invoice;
use App\Models\Proposal;
use App\Models\ProposalTemplate;
2018-02-12 09:43:31 +01:00
use App\Ninja\Mailers\ContactMailer;
2018-01-30 19:58:55 +01:00
use App\Ninja\Datatables\ProposalDatatable;
use App\Ninja\Repositories\ProposalRepository;
use App\Services\ProposalService;
use Auth;
use Input;
use Session;
use View;
2018-02-11 15:00:03 +01:00
use mPDF;
2018-01-30 19:58:55 +01:00
class ProposalController extends BaseController
{
protected $proposalRepo;
protected $proposalService;
2018-02-12 09:43:31 +01:00
protected $contactMailer;
2018-01-30 19:58:55 +01:00
protected $entityType = ENTITY_PROPOSAL;
2018-02-12 09:43:31 +01:00
public function __construct(ProposalRepository $proposalRepo, ProposalService $proposalService, ContactMailer $contactMailer)
2018-01-30 19:58:55 +01:00
{
$this->proposalRepo = $proposalRepo;
$this->proposalService = $proposalService;
2018-02-12 09:43:31 +01:00
$this->contactMailer = $contactMailer;
2018-01-30 19:58:55 +01:00
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list_wrapper', [
'entityType' => ENTITY_PROPOSAL,
'datatable' => new ProposalDatatable(),
'title' => trans('texts.proposals'),
]);
}
public function getDatatable($expensePublicId = null)
{
$search = Input::get('sSearch');
$userId = Auth::user()->filterId();
return $this->proposalService->getDatatable($search, $userId);
}
public function create(ProposalRequest $request)
{
2018-02-12 13:55:56 +01:00
$data = array_merge($this->getViewmodel(), [
2018-01-30 19:58:55 +01:00
'proposal' => null,
'method' => 'POST',
'url' => 'proposals',
'title' => trans('texts.new_proposal'),
2018-02-09 13:31:53 +01:00
'invoices' => Invoice::scope()->with('client.contacts', 'client.country')->unapprovedQuotes()->orderBy('id')->get(),
2018-02-08 12:01:51 +01:00
'invoicePublicId' => $request->invoice_id,
2018-02-09 15:02:28 +01:00
'templatePublicId' => $request->proposal_template_id,
2018-02-12 13:55:56 +01:00
]);
2018-01-30 19:58:55 +01:00
return View::make('proposals.edit', $data);
}
2018-01-31 15:33:33 +01:00
public function show($publicId)
{
Session::reflash();
return redirect("proposals/$publicId/edit");
}
2018-01-30 19:58:55 +01:00
public function edit(ProposalRequest $request)
{
$proposal = $request->entity();
2018-03-14 11:21:50 +01:00
$data = array_merge($this->getViewmodel($proposal), [
2018-01-30 19:58:55 +01:00
'proposal' => $proposal,
2018-02-12 14:03:12 +01:00
'entity' => $proposal,
2018-01-30 19:58:55 +01:00
'method' => 'PUT',
'url' => 'proposals/' . $proposal->public_id,
'title' => trans('texts.edit_proposal'),
2018-03-14 11:21:50 +01:00
'invoices' => Invoice::scope()->with('client.contacts', 'client.country')->withActiveOrSelected($proposal->invoice_id)->unapprovedQuotes($proposal->invoice_id)->orderBy('id')->get(),
2018-02-07 15:16:31 +01:00
'invoicePublicId' => $proposal->invoice ? $proposal->invoice->public_id : null,
2018-02-04 17:42:13 +01:00
'templatePublicId' => $proposal->proposal_template ? $proposal->proposal_template->public_id : null,
2018-02-12 13:55:56 +01:00
]);
2018-01-30 19:58:55 +01:00
return View::make('proposals.edit', $data);
}
2018-03-14 11:21:50 +01:00
private function getViewmodel($proposal = false)
2018-02-12 13:55:56 +01:00
{
$account = auth()->user()->account;
2018-03-14 11:21:50 +01:00
$templates = ProposalTemplate::whereAccountId($account->id)->withActiveOrSelected($proposal ? $proposal->proposal_template_id : false)->orderBy('name')->get();
2018-02-12 13:55:56 +01:00
if (! $templates->count()) {
$templates = ProposalTemplate::whereNull('account_id')->orderBy('name')->get();
}
$data = [
'templates' => $templates,
'account' => $account,
];
return $data;
}
2018-01-30 19:58:55 +01:00
public function store(CreateProposalRequest $request)
{
$proposal = $this->proposalService->save($request->input());
2018-02-12 10:23:16 +01:00
$action = Input::get('action');
2018-01-30 19:58:55 +01:00
2018-02-12 10:23:16 +01:00
if ($action == 'email') {
2018-02-12 10:34:48 +01:00
$this->dispatch(new SendInvoiceEmail($proposal->invoice, auth()->user()->id, false, false, $proposal));
2018-02-12 10:23:16 +01:00
Session::flash('message', trans('texts.emailed_proposal'));
} else {
Session::flash('message', trans('texts.created_proposal'));
}
2018-01-30 19:58:55 +01:00
return redirect()->to($proposal->getRoute());
}
public function update(UpdateProposalRequest $request)
{
$proposal = $this->proposalService->save($request->input(), $request->entity());
$action = Input::get('action');
2018-02-12 09:43:31 +01:00
2018-01-30 19:58:55 +01:00
if (in_array($action, ['archive', 'delete', 'restore'])) {
return self::bulk();
}
2018-02-12 09:43:31 +01:00
if ($action == 'email') {
2018-02-12 10:34:48 +01:00
$this->dispatch(new SendInvoiceEmail($proposal->invoice, auth()->user()->id, false, false, $proposal));
2018-02-12 10:23:16 +01:00
Session::flash('message', trans('texts.emailed_proposal'));
2018-02-12 09:43:31 +01:00
} else {
Session::flash('message', trans('texts.updated_proposal'));
}
2018-01-30 19:58:55 +01:00
return redirect()->to($proposal->getRoute());
}
public function bulk()
{
2018-02-11 10:14:47 +01:00
$action = Input::get('bulk_action') ?: Input::get('action');
$ids = Input::get('bulk_public_id') ?: (Input::get('public_id') ?: Input::get('ids'));
2018-01-30 19:58:55 +01:00
$count = $this->proposalService->bulk($ids, $action);
if ($count > 0) {
$field = $count == 1 ? "{$action}d_proposal" : "{$action}d_proposals";
$message = trans("texts.$field", ['count' => $count]);
Session::flash('message', $message);
}
return redirect()->to('/proposals');
}
2018-02-11 12:09:29 +01:00
public function download(ProposalRequest $request)
{
$proposal = $request->entity();
2018-02-11 15:00:03 +01:00
$mpdf = new mPDF();
2018-02-13 08:15:14 +01:00
$mpdf->showImageErrors = true;
2018-02-11 15:00:03 +01:00
$mpdf->WriteHTML($proposal->present()->htmlDocument);
2018-02-13 08:15:14 +01:00
//$mpdf->Output();
2018-02-11 15:00:03 +01:00
$mpdf->Output($proposal->present()->filename, 'D');
2018-02-11 12:09:29 +01:00
}
2018-01-30 19:58:55 +01:00
}