2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
2015-03-17 02:30:56 +01:00
|
|
|
|
2018-03-18 10:09:24 +01:00
|
|
|
use App\Http\Requests\ProductRequest;
|
2015-04-06 13:46:02 +02:00
|
|
|
use App\Models\Product;
|
2015-10-21 13:11:08 +02:00
|
|
|
use App\Models\TaxRate;
|
2016-11-24 10:22:37 +01:00
|
|
|
use App\Ninja\Datatables\ProductDatatable;
|
2017-03-09 10:33:55 +01:00
|
|
|
use App\Ninja\Repositories\ProductRepository;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Services\ProductService;
|
|
|
|
use Auth;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
|
|
|
use URL;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class ProductController.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-04-02 15:12:12 +02:00
|
|
|
class ProductController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var ProductService
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $productService;
|
|
|
|
|
2017-03-09 10:33:55 +01:00
|
|
|
/**
|
|
|
|
* @var ProductRepository
|
|
|
|
*/
|
|
|
|
protected $productRepo;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* ProductController constructor.
|
2016-09-23 16:00:47 +02:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param ProductService $productService
|
|
|
|
*/
|
2017-03-09 10:33:55 +01:00
|
|
|
public function __construct(ProductService $productService, ProductRepository $productRepo)
|
2015-11-05 23:37:04 +01:00
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
$this->productService = $productService;
|
2017-03-09 10:33:55 +01:00
|
|
|
$this->productRepo = $productRepo;
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-10-21 13:11:08 +02:00
|
|
|
public function index()
|
|
|
|
{
|
2016-11-24 10:22:37 +01:00
|
|
|
return View::make('list_wrapper', [
|
2016-09-23 16:00:47 +02:00
|
|
|
'entityType' => ENTITY_PRODUCT,
|
2016-11-24 10:22:37 +01:00
|
|
|
'datatable' => new ProductDatatable(),
|
2016-09-23 16:00:47 +02:00
|
|
|
'title' => trans('texts.products'),
|
2016-11-18 14:31:43 +01:00
|
|
|
'statuses' => Product::getStatuses(),
|
2016-09-23 16:00:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show($publicId)
|
|
|
|
{
|
|
|
|
Session::reflash();
|
|
|
|
|
|
|
|
return Redirect::to("products/$publicId/edit");
|
2015-10-21 13:11:08 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDatatable()
|
|
|
|
{
|
2016-10-18 20:15:08 +02:00
|
|
|
return $this->productService->getDatatable(Auth::user()->account_id, Input::get('sSearch'));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 10:09:24 +01:00
|
|
|
public function cloneProduct(ProductRequest $request, $publicId)
|
|
|
|
{
|
|
|
|
return self::edit($request, $publicId, true);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2018-03-20 12:54:24 +01:00
|
|
|
public function edit(ProductRequest $request, $publicId, $clone = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-21 13:11:08 +02:00
|
|
|
$account = Auth::user()->account;
|
2016-10-18 16:55:07 +02:00
|
|
|
$product = Product::scope($publicId)->withTrashed()->firstOrFail();
|
2015-10-21 13:11:08 +02:00
|
|
|
|
2018-03-18 10:09:24 +01:00
|
|
|
if ($clone) {
|
|
|
|
$product->id = null;
|
|
|
|
$product->public_id = null;
|
|
|
|
$product->deleted_at = null;
|
|
|
|
$url = 'products';
|
|
|
|
$method = 'POST';
|
|
|
|
} else {
|
|
|
|
$url = 'products/'.$publicId;
|
|
|
|
$method = 'PUT';
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = [
|
2015-10-21 13:11:08 +02:00
|
|
|
'account' => $account,
|
2017-05-17 11:18:48 +02:00
|
|
|
'taxRates' => $account->invoice_item_taxes ? TaxRate::scope()->whereIsInclusive(false)->get() : null,
|
2016-10-18 16:55:07 +02:00
|
|
|
'product' => $product,
|
|
|
|
'entity' => $product,
|
2018-03-18 10:09:24 +01:00
|
|
|
'method' => $method,
|
|
|
|
'url' => $url,
|
2015-10-21 13:11:08 +02:00
|
|
|
'title' => trans('texts.edit_product'),
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return View::make('accounts.product', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function create()
|
|
|
|
{
|
2015-10-21 13:11:08 +02:00
|
|
|
$account = Auth::user()->account;
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = [
|
2015-10-21 13:11:08 +02:00
|
|
|
'account' => $account,
|
2017-01-02 12:38:58 +01:00
|
|
|
'taxRates' => $account->invoice_item_taxes ? TaxRate::scope()->whereIsInclusive(false)->get(['id', 'name', 'rate']) : null,
|
2015-10-14 16:15:39 +02:00
|
|
|
'product' => null,
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => 'products',
|
|
|
|
'title' => trans('texts.create_product'),
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return View::make('accounts.product', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param bool $productPublicId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
private function save($productPublicId = false)
|
|
|
|
{
|
|
|
|
if ($productPublicId) {
|
2016-10-10 10:40:04 +02:00
|
|
|
$product = Product::scope($productPublicId)->withTrashed()->firstOrFail();
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
$product = Product::createNew();
|
|
|
|
}
|
|
|
|
|
2017-03-09 10:33:55 +01:00
|
|
|
$this->productRepo->save(Input::all(), $product);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$message = $productPublicId ? trans('texts.updated_product') : trans('texts.created_product');
|
|
|
|
Session::flash('message', $message);
|
|
|
|
|
2018-03-18 10:09:24 +01:00
|
|
|
$action = request('action');
|
|
|
|
if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
|
2017-11-03 10:25:14 +01:00
|
|
|
return self::bulk();
|
|
|
|
}
|
|
|
|
|
2018-03-18 10:09:24 +01:00
|
|
|
if ($action == 'clone') {
|
|
|
|
return redirect()->to(sprintf('products/%s/clone', $product->public_id));
|
|
|
|
} else {
|
|
|
|
return redirect()->to("products/{$product->public_id}/edit");
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
public function bulk()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-09-23 16:00:47 +02:00
|
|
|
$action = Input::get('action');
|
|
|
|
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
|
2017-11-03 09:19:03 +01:00
|
|
|
|
|
|
|
if ($action == 'invoice') {
|
|
|
|
$products = Product::scope($ids)->get();
|
2017-11-03 10:25:14 +01:00
|
|
|
$data = [];
|
2017-11-03 09:19:03 +01:00
|
|
|
foreach ($products as $product) {
|
|
|
|
$data[] = $product->product_key;
|
|
|
|
}
|
|
|
|
return redirect("invoices/create")->with('selectedProducts', $data);
|
|
|
|
} else {
|
|
|
|
$count = $this->productService->bulk($ids, $action);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-11-29 18:47:26 +01:00
|
|
|
$message = Utils::pluralize($action.'d_product', $count);
|
|
|
|
Session::flash('message', $message);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-09-23 16:00:47 +02:00
|
|
|
return $this->returnBulk(ENTITY_PRODUCT, $action, $ids);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|