1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/VendorController.php

186 lines
4.7 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2016-01-06 15:23:58 +01:00
use App\Http\Requests\CreateVendorRequest;
use App\Http\Requests\UpdateVendorRequest;
2017-01-30 20:40:43 +01:00
use App\Http\Requests\VendorRequest;
use App\Models\Account;
use App\Models\Vendor;
use App\Ninja\Datatables\VendorDatatable;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\VendorRepository;
use App\Services\VendorService;
use Auth;
use Cache;
use Input;
use Redirect;
use Session;
use URL;
use Utils;
use View;
2016-04-28 14:16:33 +02:00
2016-01-06 15:23:58 +01:00
class VendorController extends BaseController
{
protected $vendorService;
protected $vendorRepo;
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_VENDOR;
2016-01-06 15:23:58 +01:00
public function __construct(VendorRepository $vendorRepo, VendorService $vendorService)
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2016-01-06 15:23:58 +01:00
$this->vendorRepo = $vendorRepo;
$this->vendorService = $vendorService;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list_wrapper', [
2016-01-06 15:23:58 +01:00
'entityType' => 'vendor',
'datatable' => new VendorDatatable(),
2016-01-06 15:23:58 +01:00
'title' => trans('texts.vendors'),
]);
2016-01-06 15:23:58 +01:00
}
public function getDatatable()
{
return $this->vendorService->getDatatable(Input::get('sSearch'));
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(CreateVendorRequest $request)
{
2016-05-01 13:31:10 +02:00
$vendor = $this->vendorService->save($request->input());
2016-01-06 15:23:58 +01:00
Session::flash('message', trans('texts.created_vendor'));
2016-01-06 15:23:58 +01:00
return redirect()->to($vendor->getRoute());
}
/**
* Display the specified resource.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2016-01-06 15:23:58 +01:00
* @return Response
*/
2016-05-01 13:31:10 +02:00
public function show(VendorRequest $request)
2016-01-06 15:23:58 +01:00
{
2016-05-01 13:31:10 +02:00
$vendor = $request->entity();
2016-09-19 15:30:46 +02:00
2016-01-06 15:23:58 +01:00
$actionLinks = [
2017-01-30 20:40:43 +01:00
['label' => trans('texts.new_vendor'), 'url' => URL::to('/vendors/create/' . $vendor->public_id)],
2016-01-06 15:23:58 +01:00
];
$data = [
2017-01-30 20:40:43 +01:00
'actionLinks' => $actionLinks,
'showBreadcrumbs' => false,
'vendor' => $vendor,
'title' => trans('texts.view_vendor'),
'hasRecurringInvoices' => false,
'hasQuotes' => false,
'hasTasks' => false,
];
2016-01-06 15:23:58 +01:00
return View::make('vendors.show', $data);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
2016-05-01 13:31:10 +02:00
public function create(VendorRequest $request)
2016-01-06 15:23:58 +01:00
{
if (Vendor::scope()->count() > Auth::user()->getMaxNumVendors()) {
return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of ".Auth::user()->getMaxNumVendors().' vendors']);
2016-01-06 15:23:58 +01:00
}
$data = [
'vendor' => null,
'method' => 'POST',
'url' => 'vendors',
'title' => trans('texts.new_vendor'),
];
$data = array_merge($data, self::getViewModel());
return View::make('vendors.edit', $data);
}
/**
* Show the form for editing the specified resource.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2016-01-06 15:23:58 +01:00
* @return Response
*/
2016-05-01 13:31:10 +02:00
public function edit(VendorRequest $request)
2016-01-06 15:23:58 +01:00
{
2016-05-01 13:31:10 +02:00
$vendor = $request->entity();
2016-09-01 17:52:26 +02:00
2016-01-06 15:23:58 +01:00
$data = [
'vendor' => $vendor,
'method' => 'PUT',
2016-05-01 13:31:10 +02:00
'url' => 'vendors/'.$vendor->public_id,
2016-01-06 15:23:58 +01:00
'title' => trans('texts.edit_vendor'),
];
$data = array_merge($data, self::getViewModel());
if (Auth::user()->account->isNinjaAccount()) {
if ($account = Account::whereId($client->public_id)->first()) {
$data['planDetails'] = $account->getPlanDetails(false, false);
2016-01-06 15:23:58 +01:00
}
}
return View::make('vendors.edit', $data);
}
private static function getViewModel()
{
return [
'data' => Input::old('data'),
'account' => Auth::user()->account,
'currencies' => Cache::get('currencies'),
];
}
/**
* Update the specified resource in storage.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2016-01-06 15:23:58 +01:00
* @return Response
*/
public function update(UpdateVendorRequest $request)
{
$vendor = $this->vendorService->save($request->input(), $request->entity());
2016-01-06 15:23:58 +01:00
Session::flash('message', trans('texts.updated_vendor'));
2016-01-06 15:23:58 +01:00
return redirect()->to($vendor->getRoute());
}
public function bulk()
{
$action = Input::get('action');
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
$count = $this->vendorService->bulk($ids, $action);
$message = Utils::pluralize($action.'d_vendor', $count);
Session::flash('message', $message);
2016-09-19 15:30:46 +02:00
return $this->returnBulk($this->entityType, $action, $ids);
2016-01-06 15:23:58 +01:00
}
}