From b0c13743527e34ef008d7557eb692ee7f632ae6e Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Thu, 16 Sep 2021 10:43:13 +0530 Subject: [PATCH] Product Inventory Saved --- .../Admin/src/DataGrids/ProductDataGrid.php | 29 ++-- packages/Webkul/Admin/src/Http/routes.php | 4 + .../products/datagrid/quantity.blade.php | 6 +- .../views/catalog/products/index.blade.php | 20 ++- .../Http/Controllers/ProductController.php | 138 +++++++++++------- 5 files changed, 126 insertions(+), 71 deletions(-) diff --git a/packages/Webkul/Admin/src/DataGrids/ProductDataGrid.php b/packages/Webkul/Admin/src/DataGrids/ProductDataGrid.php index 924bd4511..54e59f972 100644 --- a/packages/Webkul/Admin/src/DataGrids/ProductDataGrid.php +++ b/packages/Webkul/Admin/src/DataGrids/ProductDataGrid.php @@ -248,17 +248,11 @@ class ProductDataGrid extends DataGrid 'searchable' => false, 'filterable' => false, 'closure' => true, - 'wrapper' => function ($value) { - if (is_null($value->quantity)) { + 'wrapper' => function ($row) { + if (is_null($row->quantity)) { return 0; } else { - $product = $this->productRepository->find($value->product_id); - - $inventorySources = $this->inventorySourceRepository->findWhere(['status' => 1]); - - $totalQuantity = $value->quantity; - - return view('admin::catalog.products.datagrid.quantity', compact('product', 'inventorySources', 'totalQuantity'))->render(); + return $this->renderQuantityView($row); } }, ]); @@ -322,4 +316,21 @@ class ProductDataGrid extends DataGrid ], ]); } + + /** + * Render quantity view. + * + * @parma object $row + * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory + */ + private function renderQuantityView($row) + { + $product = $this->productRepository->find($row->product_id); + + $inventorySources = $this->inventorySourceRepository->findWhere(['status' => 1]); + + $totalQuantity = $row->quantity; + + return view('admin::catalog.products.datagrid.quantity', compact('product', 'inventorySources', 'totalQuantity'))->render(); + } } diff --git a/packages/Webkul/Admin/src/Http/routes.php b/packages/Webkul/Admin/src/Http/routes.php index 2bd12a221..0b678572d 100755 --- a/packages/Webkul/Admin/src/Http/routes.php +++ b/packages/Webkul/Admin/src/Http/routes.php @@ -304,6 +304,10 @@ Route::group(['middleware' => ['web', 'admin_locale']], function () { 'redirect' => 'admin.catalog.products.index', ])->name('admin.catalog.products.update'); + Route::put('/products/edit/{id}/inventories', 'Webkul\Product\Http\Controllers\ProductController@updateInventories')->defaults('_config', [ + 'redirect' => 'admin.catalog.products.index', + ])->name('admin.catalog.products.update-inventories'); + Route::post('/products/upload-file/{id}', 'Webkul\Product\Http\Controllers\ProductController@uploadLink')->name('admin.catalog.products.upload_link'); Route::post('/products/upload-sample/{id}', 'Webkul\Product\Http\Controllers\ProductController@uploadSample')->name('admin.catalog.products.upload_sample'); diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/datagrid/quantity.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/datagrid/quantity.blade.php index 657819340..3d93c1711 100644 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/datagrid/quantity.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/datagrid/quantity.blade.php @@ -1,11 +1,13 @@ - {{ $totalQuantity }} + {{ $totalQuantity }} \ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/index.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/index.blade.php index 312e80303..c04125f13 100755 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/index.blade.php @@ -63,17 +63,25 @@ } function cancelEditQuantityForm(productId) { - $(`#product-${productId}-quantity`).show(); - $(`#edit-product-${productId}-quantity-form-block`).hide(); + + $(`#product-${productId}-quantity`).show(); } - function saveEditQuantityForm(productId) { - let quantityFormData = $(`#edit-product-${productId}-quantity-form`).serializeArray(); + function saveEditQuantityForm(updateSource, productId) { + let quantityFormData = $(`#edit-product-${productId}-quantity-form`).serialize(); - console.log(quantityFormData); + axios + .post(updateSource, quantityFormData) + .then(function (response) { + let data = response.data; - alert(`${productId} Saved!`); + $(`#edit-product-${productId}-quantity-form-block`).hide(); + + $(`#product-${productId}-quantity-anchor`).text(data.updatedTotal); + + $(`#product-${productId}-quantity`).show(); + }); } @endpush \ No newline at end of file diff --git a/packages/Webkul/Product/src/Http/Controllers/ProductController.php b/packages/Webkul/Product/src/Http/Controllers/ProductController.php index a4dc76b91..8632b96f2 100755 --- a/packages/Webkul/Product/src/Http/Controllers/ProductController.php +++ b/packages/Webkul/Product/src/Http/Controllers/ProductController.php @@ -3,89 +3,96 @@ namespace Webkul\Product\Http\Controllers; use Exception; -use Webkul\Product\Models\Product; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Storage; -use Webkul\Product\Helpers\ProductType; -use Webkul\Core\Contracts\Validations\Slug; -use Webkul\Product\Http\Requests\ProductForm; -use Webkul\Product\Repositories\ProductRepository; -use Webkul\Category\Repositories\CategoryRepository; use Webkul\Attribute\Repositories\AttributeFamilyRepository; +use Webkul\Category\Repositories\CategoryRepository; +use Webkul\Core\Contracts\Validations\Slug; use Webkul\Inventory\Repositories\InventorySourceRepository; +use Webkul\Product\Helpers\ProductType; +use Webkul\Product\Http\Requests\ProductForm; +use Webkul\Product\Models\Product; +use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Repositories\ProductDownloadableLinkRepository; use Webkul\Product\Repositories\ProductDownloadableSampleRepository; -use Webkul\Product\Repositories\ProductAttributeValueRepository; +use Webkul\Product\Repositories\ProductInventoryRepository; +use Webkul\Product\Repositories\ProductRepository; class ProductController extends Controller { /** - * Contains route related configuration + * Contains route related configuration. * * @var array */ protected $_config; /** - * CategoryRepository object + * Category repository instance. * * @var \Webkul\Category\Repositories\CategoryRepository */ protected $categoryRepository; /** - * ProductRepository object + * Product repository instance. * * @var \Webkul\Product\Repositories\ProductRepository */ protected $productRepository; /** - * ProductDownloadableLinkRepository object + * Product downloadable link repository instance. * * @var \Webkul\Product\Repositories\ProductDownloadableLinkRepository */ protected $productDownloadableLinkRepository; /** - * ProductDownloadableSampleRepository object + * Product downloadable sample repository instance. * * @var \Webkul\Product\Repositories\ProductDownloadableSampleRepository */ protected $productDownloadableSampleRepository; /** - * AttributeFamilyRepository object + * Attribute family repository instance. * * @var \Webkul\Attribute\Repositories\AttributeFamilyRepository */ protected $attributeFamilyRepository; /** - * InventorySourceRepository object + * Inventory source repository instance. * * @var \Webkul\Inventory\Repositories\InventorySourceRepository */ protected $inventorySourceRepository; /** - * ProductAttributeValueRepository object + * Product attribute value repository instance. * * @var \Webkul\Product\Repositories\ProductAttributeValueRepository */ protected $productAttributeValueRepository; + /** + * Product inventory repository instance. + * + * @var \Webkul\Product\Repositories\ProductInventoryRepository + */ + protected $productInventoryRepository; + /** * Create a new controller instance. * - * @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository - * @param \Webkul\Product\Repositories\ProductRepository $productRepository - * @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository - * @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository - * @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository - * @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySourceRepository - * @param \Webkul\Product\Repositories\ProductAttributeValueRepository $productAttributeValueRepository - * + * @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository + * @param \Webkul\Product\Repositories\ProductRepository $productRepository + * @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository + * @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository + * @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository + * @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySourceRepository + * @param \Webkul\Product\Repositories\ProductAttributeValueRepository $productAttributeValueRepository * @return void */ public function __construct( @@ -95,9 +102,9 @@ class ProductController extends Controller ProductDownloadableSampleRepository $productDownloadableSampleRepository, AttributeFamilyRepository $attributeFamilyRepository, InventorySourceRepository $inventorySourceRepository, - ProductAttributeValueRepository $productAttributeValueRepository - ) - { + ProductAttributeValueRepository $productAttributeValueRepository, + ProductInventoryRepository $productInventoryRepository + ) { $this->_config = request('_config'); $this->categoryRepository = $categoryRepository; @@ -113,6 +120,8 @@ class ProductController extends Controller $this->inventorySourceRepository = $inventorySourceRepository; $this->productAttributeValueRepository = $productAttributeValueRepository; + + $this->productInventoryRepository = $productInventoryRepository; } /** @@ -150,14 +159,16 @@ class ProductController extends Controller */ public function store() { - if (! request()->get('family') + if ( + ! request()->get('family') && ProductType::hasVariants(request()->input('type')) && request()->input('sku') != '' ) { return redirect(url()->current() . '?type=' . request()->input('type') . '&family=' . request()->input('attribute_family_id') . '&sku=' . request()->input('sku')); } - if (ProductType::hasVariants(request()->input('type')) + if ( + ProductType::hasVariants(request()->input('type')) && (! request()->has('super_attributes') || ! count(request()->get('super_attributes'))) ) { @@ -182,8 +193,7 @@ class ProductController extends Controller /** * Show the form for editing the specified resource. * - * @param int $id - * + * @param int $id * @return \Illuminate\View\View */ public function edit($id) @@ -200,9 +210,8 @@ class ProductController extends Controller /** * Update the specified resource in storage. * - * @param \Webkul\Product\Http\Requests\ProductForm $request - * @param int $id - * + * @param \Webkul\Product\Http\Requests\ProductForm $request + * @param int $id * @return \Illuminate\Http\Response */ public function update(ProductForm $request, $id) @@ -233,7 +242,7 @@ class ProductController extends Controller } } - $product = $this->productRepository->update($data, $id); + $this->productRepository->update($data, $id); session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Product'])); @@ -241,10 +250,27 @@ class ProductController extends Controller } /** - * Uploads downloadable file + * Update inventories. * - * @param int $id + * @param int $id + * @return \Illuminate\Http\Response + */ + public function updateInventories($id) + { + $product = $this->productRepository->findOrFail($id); + + $this->productInventoryRepository->saveInventories(request()->all(), $product); + + return response()->json([ + 'message' => 'Product inventory saved successfully.', + 'updatedTotal' => $this->productInventoryRepository->where('product_id', $product->id)->sum('qty') + ]); + } + + /** + * Uploads downloadable file. * + * @param int $id * @return \Illuminate\Http\Response */ public function uploadLink($id) @@ -256,23 +282,30 @@ class ProductController extends Controller /** * Copy a given Product. + * + * @param int $productId + * @return \Illuminate\Http\Response */ public function copy(int $productId) { $originalProduct = $this->productRepository->findOrFail($productId); if (! $originalProduct->getTypeInstance()->canBeCopied()) { - session()->flash('error', + session()->flash( + 'error', trans('admin::app.response.product-can-not-be-copied', [ 'type' => $originalProduct->type, - ])); + ]) + ); return redirect()->to(route('admin.catalog.products.index')); } if ($originalProduct->parent_id) { - session()->flash('error', - trans('admin::app.catalog.products.variant-already-exist-message')); + session()->flash( + 'error', + trans('admin::app.catalog.products.variant-already-exist-message') + ); return redirect()->to(route('admin.catalog.products.index')); } @@ -289,10 +322,9 @@ class ProductController extends Controller } /** - * Uploads downloadable sample file - * - * @param int $id + * Uploads downloadable sample file. * + * @param int $id * @return \Illuminate\Http\Response */ public function uploadSample($id) @@ -305,8 +337,7 @@ class ProductController extends Controller /** * Remove the specified resource from storage. * - * @param int $id - * + * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) @@ -329,7 +360,7 @@ class ProductController extends Controller } /** - * Mass Delete the products + * Mass Delete the products. * * @return \Illuminate\Http\Response */ @@ -351,7 +382,7 @@ class ProductController extends Controller } /** - * Mass updates the products + * Mass updates the products. * * @return \Illuminate\Http\Response */ @@ -383,7 +414,7 @@ class ProductController extends Controller } /** - * To be manually invoked when data is seeded into products + * To be manually invoked when data is seeded into products. * * @return \Illuminate\Http\Response */ @@ -419,11 +450,10 @@ class ProductController extends Controller } /** - * Download image or file - * - * @param int $productId - * @param int $attributeId + * Download image or file. * + * @param int $productId + * @param int $attributeId * @return \Illuminate\Http\Response */ public function download($productId, $attributeId) @@ -437,7 +467,7 @@ class ProductController extends Controller } /** - * Search simple products + * Search simple products. * * @return \Illuminate\Http\Response */ @@ -447,4 +477,4 @@ class ProductController extends Controller $this->productRepository->searchSimpleProducts(request()->input('query')) ); } -} \ No newline at end of file +}