diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index baf03996b3..f747d25a96 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -13,6 +13,7 @@ namespace App\Http\Controllers; use App\Factory\ProductFactory; use App\Filters\ProductFilters; +use App\Http\Requests\Product\BulkProductRequest; use App\Http\Requests\Product\CreateProductRequest; use App\Http\Requests\Product\DestroyProductRequest; use App\Http\Requests\Product\EditProductRequest; @@ -455,21 +456,32 @@ class ProductController extends BaseController * ), * ) */ - public function bulk() + public function bulk(BulkProductRequest $request) { - $action = request()->input('action'); + $action = $request->input('action'); - $ids = request()->input('ids'); + $ids = $request->input('ids'); - $products = Product::withTrashed()->whereIn('id', $this->transformKeys($ids))->cursor(); + $products = Product::withTrashed()->whereIn('id', $ids); - $products->each(function ($product, $key) use ($action) { + nlog($products->count()); + + if($action == 'set_tax_id'){ + + $tax_id = $request->input('tax_id'); + + $products->update(['tax_id' => $tax_id]); + + return $this->listResponse(Product::withTrashed()->whereIn('id', $ids)); + } + + $products->cursor()->each(function ($product, $key) use ($action) { if (auth()->user()->can('edit', $product)) { $this->product_repo->{$action}($product); } }); - return $this->listResponse(Product::withTrashed()->whereIn('id', $this->transformKeys($ids))); + return $this->listResponse(Product::withTrashed()->whereIn('id', $ids)); } /** diff --git a/app/Http/Requests/Product/BulkProductRequest.php b/app/Http/Requests/Product/BulkProductRequest.php new file mode 100644 index 0000000000..3fe3bb164a --- /dev/null +++ b/app/Http/Requests/Product/BulkProductRequest.php @@ -0,0 +1,51 @@ + ['required','bail','array',Rule::exists('products', 'id')->where('company_id', auth()->user()->company()->id)], + 'action' => 'in:archive,restore,delete,set_tax_id', + 'tax_id' => 'nullable|required_if:action,set_tax_id,in:1,2,3,4,5,6,7,8,9', + ]; + } + + public function prepareForValidation() + { + $input = $this->all(); +nlog($input); + if (isset($input['ids'])) { + $input['ids'] = $this->transformKeys($input['ids']); + } + + $this->replace($input); + } +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index ca66833b3d..5f7b84cbc9 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -33,6 +33,7 @@ class ProductFactory extends Factory 'custom_value3' => $this->faker->text(20), 'custom_value4' => $this->faker->text(20), 'is_deleted' => false, + 'tax_id' => 1, ]; } } diff --git a/tests/Feature/ProductTest.php b/tests/Feature/ProductTest.php index c503fed90d..bc41769e64 100644 --- a/tests/Feature/ProductTest.php +++ b/tests/Feature/ProductTest.php @@ -45,6 +45,43 @@ class ProductTest extends TestCase ); $this->makeTestData(); + $this->withoutExceptionHandling(); + + } + + public function testSetTaxId() + { + $p = Product::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id + ]); + + + $this->assertEquals(1, $p->tax_id); + + $update = [ + 'ids' => [$p->hashed_id], + 'action' => 'set_tax_id', + 'tax_id' => 6, + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/products/bulk', $update) + ->assertStatus(200); + } + catch(\Exception $e){ + + } + + $p = $p->fresh(); + + $this->assertEquals(6, $p->tax_id); + } public function testProductGetProductKeyFilter()