invoiceninja/app/Http/Controllers/ProductController.php

167 lines
4.1 KiB
PHP
Raw Normal View History

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