2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2015-04-02 15:12:12 +02:00
|
|
|
use Auth;
|
|
|
|
use URL;
|
2015-04-06 13:46:02 +02:00
|
|
|
use View;
|
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use Redirect;
|
|
|
|
use App\Models\Product;
|
2015-10-21 13:11:08 +02:00
|
|
|
use App\Models\TaxRate;
|
2015-11-05 23:37:04 +01:00
|
|
|
use App\Services\ProductService;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class ProductController
|
|
|
|
*/
|
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;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* ProductController constructor.
|
|
|
|
*
|
|
|
|
* @param ProductService $productService
|
|
|
|
*/
|
2015-11-05 23:37:04 +01:00
|
|
|
public function __construct(ProductService $productService)
|
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
$this->productService = $productService;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-10-21 13:11:08 +02:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Redirect::to('settings/' . ACCOUNT_PRODUCTS);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDatatable()
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
return $this->productService->getDatatable(Auth::user()->account_id);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function edit($publicId)
|
|
|
|
{
|
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,
|
|
|
|
'taxRates' => $account->invoice_item_taxes ? TaxRate::scope()->get(['id', 'name', 'rate']) : null,
|
|
|
|
'product' => Product::scope($publicId)->firstOrFail(),
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'products/'.$publicId,
|
|
|
|
'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,
|
|
|
|
'taxRates' => $account->invoice_item_taxes ? TaxRate::scope()->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
|
|
|
|
* @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
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
private function save($productPublicId = false)
|
|
|
|
{
|
|
|
|
if ($productPublicId) {
|
|
|
|
$product = Product::scope($productPublicId)->firstOrFail();
|
|
|
|
} else {
|
|
|
|
$product = Product::createNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
$product->product_key = trim(Input::get('product_key'));
|
|
|
|
$product->notes = trim(Input::get('notes'));
|
|
|
|
$product->cost = trim(Input::get('cost'));
|
2015-10-21 13:11:08 +02:00
|
|
|
$product->default_tax_rate_id = Input::get('default_tax_rate_id');
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$message = $productPublicId ? trans('texts.updated_product') : trans('texts.created_product');
|
|
|
|
Session::flash('message', $message);
|
|
|
|
|
2015-10-14 16:15:39 +02:00
|
|
|
return Redirect::to('settings/' . ACCOUNT_PRODUCTS);
|
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
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$action = Input::get('bulk_action');
|
|
|
|
$ids = Input::get('bulk_public_id');
|
|
|
|
$count = $this->productService->bulk($ids, $action);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
Session::flash('message', trans('texts.archived_product'));
|
|
|
|
|
2015-10-14 16:15:39 +02:00
|
|
|
return Redirect::to('settings/' . ACCOUNT_PRODUCTS);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|