Product Inventory Saved

This commit is contained in:
devansh bawari 2021-09-16 10:43:13 +05:30
parent 89c24e190a
commit b0c1374352
5 changed files with 126 additions and 71 deletions

View File

@ -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();
}
}

View File

@ -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');

View File

@ -1,11 +1,13 @@
<span id="product-{{ $product->id }}-quantity">
<a href="javascript:void(0);" onclick="showEditQuantityForm('{{ $product->id }}')">{{ $totalQuantity }}</a>
<a id="product-{{ $product->id }}-quantity-anchor" href="javascript:void(0);" onclick="showEditQuantityForm('{{ $product->id }}')">{{ $totalQuantity }}</a>
</span>
<span id="edit-product-{{ $product->id }}-quantity-form-block" style="display: none;">
<form id="edit-product-{{ $product->id }}-quantity-form" action="javascript:void(0);">
@csrf
@method('PUT')
@foreach ($inventorySources as $inventorySource)
@php
$qty = 0;
@ -29,7 +31,7 @@
</div>
@endforeach
<button class="btn btn-primary" onclick="saveEditQuantityForm('{{ $product->id }}')">Save</button>
<button class="btn btn-primary" onclick="saveEditQuantityForm('{{ route('admin.catalog.products.update-inventories', $product->id) }}', '{{ $product->id }}')">Save</button>
<button class="btn btn-danger" onclick="cancelEditQuantityForm('{{ $product->id }}')">Cancel</button>
</form>
</span>

View File

@ -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();
});
}
</script>
@endpush

View File

@ -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'))
);
}
}
}