diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 276a3516e8..30b5bd9f54 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\ProductRequest; use App\Models\Product; use App\Models\TaxRate; use App\Ninja\Datatables\ProductDatatable; @@ -71,23 +72,39 @@ class ProductController extends BaseController return $this->productService->getDatatable(Auth::user()->account_id, Input::get('sSearch')); } + public function cloneProduct(ProductRequest $request, $publicId) + { + return self::edit($request, $publicId, true); + } + /** * @param $publicId * * @return \Illuminate\Contracts\View\View */ - public function edit($publicId) + public function edit($publicId, $clone = false) { $account = Auth::user()->account; $product = Product::scope($publicId)->withTrashed()->firstOrFail(); + if ($clone) { + $product->id = null; + $product->public_id = null; + $product->deleted_at = null; + $url = 'products'; + $method = 'POST'; + } else { + $url = 'products/'.$publicId; + $method = 'PUT'; + } + $data = [ 'account' => $account, 'taxRates' => $account->invoice_item_taxes ? TaxRate::scope()->whereIsInclusive(false)->get() : null, 'product' => $product, 'entity' => $product, - 'method' => 'PUT', - 'url' => 'products/'.$publicId, + 'method' => $method, + 'url' => $url, 'title' => trans('texts.edit_product'), ]; @@ -149,11 +166,16 @@ class ProductController extends BaseController $message = $productPublicId ? trans('texts.updated_product') : trans('texts.created_product'); Session::flash('message', $message); - if (in_array(request('action'), ['archive', 'delete', 'restore', 'invoice'])) { + $action = request('action'); + if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) { return self::bulk(); } - return Redirect::to("products/{$product->public_id}/edit"); + if ($action == 'clone') { + return redirect()->to(sprintf('products/%s/clone', $product->public_id)); + } else { + return redirect()->to("products/{$product->public_id}/edit"); + } } /** diff --git a/app/Ninja/Datatables/ProductDatatable.php b/app/Ninja/Datatables/ProductDatatable.php index 33c691806b..38354fb3e3 100644 --- a/app/Ninja/Datatables/ProductDatatable.php +++ b/app/Ninja/Datatables/ProductDatatable.php @@ -68,6 +68,15 @@ class ProductDatatable extends EntityDatatable return URL::to("products/{$model->public_id}/edit"); }, ], + [ + trans('texts.clone_product'), + function ($model) { + return "javascript:submitForm_product('clone', {$model->public_id})"; + }, + function ($model) { + return Auth::user()->can('create', ENTITY_PRODUCT); + }, + ], [ trans('texts.invoice_product'), function ($model) { diff --git a/app/Ninja/Presenters/ProductPresenter.php b/app/Ninja/Presenters/ProductPresenter.php index 07351e1c08..6f7659c082 100644 --- a/app/Ninja/Presenters/ProductPresenter.php +++ b/app/Ninja/Presenters/ProductPresenter.php @@ -27,10 +27,16 @@ class ProductPresenter extends EntityPresenter public function moreActions() { $product = $this->entity; + $actions = []; if (! $product->trashed()) { + if (auth()->user()->can('create', ENTITY_PRODUCT)) { + $actions[] = ['url' => 'javascript:submitAction("clone")', 'label' => trans('texts.clone_product')]; + } if (auth()->user()->can('create', ENTITY_INVOICE)) { $actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_product')]; + } + if (count($actions)) { $actions[] = DropdownButton::DIVIDER; } $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans("texts.archive_product")]; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 8bbb55856e..5e0df93ec7 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2789,6 +2789,7 @@ $LANG = array( 'purge_client' => 'Purge Client', 'purged_client' => 'Successfully purged client', 'purge_client_warning' => 'All related records (invoices, tasks, expenses, documents, etc) will also be deleted.', + 'clone_product' => 'Clone Product', ); diff --git a/routes/web.php b/routes/web.php index 34eb3be847..2a19444f96 100644 --- a/routes/web.php +++ b/routes/web.php @@ -247,6 +247,7 @@ Route::group(['middleware' => ['lookup:user', 'auth:user']], function () { Route::get('api/credits/{client_id?}', 'CreditController@getDatatable'); Route::post('credits/bulk', 'CreditController@bulk'); + Route::get('products/{products}/clone', 'ProductController@cloneProduct'); Route::get('api/products', 'ProductController@getDatatable'); Route::resource('products', 'ProductController'); Route::post('products/bulk', 'ProductController@bulk');