/** * Edit product page, quantity, price and discount. * * @group success * @group billCombined */ public function testEditProductPageProductQuantityProductPriceAndProductDiscount() { $data = $this->generateData(); // Edit product page first $post = ['product_id' => $data['product']->id, 'bill_product_id' => $data['billProduct']->id, 'product_code' => $data['product']->code, 'product_page' => rand(1, 999)]; $this->actingAs($data['user'])->post(TestUrlBuilder::editBillProductPage($data['bill']->id), $post)->seeJson(['success' => true, 'message' => trans('bill.page_updated')])->seeInDatabase('bill_products', ['id' => $data['billProduct']->id, 'page' => $post['product_page']]); // Edit product quantity unset($post['product_page']); $post['product_quantity'] = rand(1, 99); $newPrice = Products::newPrice($data['billProduct']->price, $data['billProduct']->quantity, $post['product_quantity']); $newCalculatedDiscount = Products::discount($newPrice, $data['billProduct']->discount); $newFinalPrice = $newPrice - $newCalculatedDiscount; $this->actingAs($data['user'])->post(TestUrlBuilder::editBillProductQuantity($data['bill']->id), $post)->seeJson(['success' => true, 'message' => trans('bill.quantity_updated')])->seeInDatabase('bill_products', ['id' => $data['billProduct']->id, 'price' => $newPrice, 'calculated_discount' => $newCalculatedDiscount, 'final_price' => $newFinalPrice]); $data['billProduct']->quantity = $post['product_quantity']; // Edit product price unset($post['product_quantity']); $post['product_price'] = rand(1, 200); $newCalculatedDiscount = Products::discount($post['product_price'] * $data['billProduct']->quantity, $data['billProduct']->discount); $newFinalPrice = $post['product_price'] * $data['billProduct']->quantity - $newCalculatedDiscount; $this->actingAs($data['user'])->post(TestUrlBuilder::editBillProductPrice($data['bill']->id), $post)->seeJson(['success' => true, 'message' => trans('bill.price_updated')])->seeInDatabase('bill_products', ['id' => $data['billProduct']->id, 'calculated_discount' => $newCalculatedDiscount, 'final_price' => $newFinalPrice]); $data['billProduct']->price = $post['product_price'] * $data['billProduct']->quantity; // And finally edit product discount unset($post['product_price']); $post['product_discount'] = rand(0, 100); $newCalculatedDiscount = Products::discount($data['billProduct']->price, $post['product_discount']); $this->actingAs($data['user'])->post(TestUrlBuilder::editBillProductDiscount($data['bill']->id), $post)->seeJson(['success' => true, 'message' => trans('bill.discount_updated')])->seeInDatabase('bill_products', ['id' => $data['billProduct']->id, 'calculated_discount' => $newCalculatedDiscount, 'final_price' => $data['billProduct']->price - $newCalculatedDiscount]); }
}); // Generate product $factory->define(App\Product::class, function ($faker) { return ['name' => $faker->name, 'code' => (string) $faker->numberBetween(10000, 99999)]; }); // Generate application product $factory->define(App\ApplicationProduct::class, function () use($factory) { return $factory->raw(App\Product::class); }); // Generate bill product $factory->define(App\BillProduct::class, function () use($factory) { $page = rand(1, 999); $quantity = rand(1, 99); $price = rand(1, 200) * $quantity; $discount = rand(0, 100); $calculatedDiscount = Products::discount($price, $discount); $finalPrice = $price - $calculatedDiscount; return ['page' => $page, 'quantity' => $quantity, 'price' => $price, 'discount' => $discount, 'calculated_discount' => $calculatedDiscount, 'final_price' => $finalPrice]; }); $factory->defineAs(App\BillProduct::class, 'no-discount', function () use($factory) { $billProduct = $factory->raw(\App\BillProduct::class); $billProduct['discount'] = 0; $billProduct['calculated_discount'] = 0; $billProduct['final_price'] = $billProduct['price']; return $billProduct; }); $factory->define(App\BillApplicationProduct::class, function () use($factory) { return $factory->raw(App\BillProduct::class); }); $factory->define(App\UserSetting::class, function () { return ['displayed_bills' => rand(1, 20), 'displayed_clients' => rand(1, 20), 'displayed_products' => rand(1, 20), 'displayed_custom_products' => rand(1, 20)];
/** * Handle custom product delete. * * @param string $productCode * @param DeleteProductRequest $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function delete($productCode, DeleteProductRequest $request) { return Products::delete($productCode, $request->get('id')); }
/** * Query database to return product suggestions based on given code. * * @param SuggestProductRequest $request * @return mixed */ public function suggestProducts(SuggestProductRequest $request) { return Products::suggestProducts($request->get('product_code')); }
/** * Return an array with columns to update on bill product edit. * * @param string $columnToUpdate * @param string|int $newValue * @param Product $product * @return array */ private static function getDataToUpdateOnEdit($columnToUpdate, $newValue, $product) { if ($columnToUpdate === 'page') { return [$columnToUpdate => $newValue]; } // When quantity is updated, update price, calculated discount and final price if ($columnToUpdate === 'quantity') { $toUpdate = [$columnToUpdate => $newValue]; $toUpdate['price'] = Products::newPrice($product->price, $product->quantity, $newValue); $toUpdate['calculated_discount'] = Products::discount($toUpdate['price'], $product->discount); $toUpdate['final_price'] = $toUpdate['price'] - $toUpdate['calculated_discount']; return $toUpdate; } // When price is updated, update calculated discount and final price if ($columnToUpdate === 'price') { $toUpdate = [$columnToUpdate => $newValue * $product->quantity]; $toUpdate['calculated_discount'] = Products::discount($newValue * $product->quantity, $product->discount); $toUpdate['final_price'] = $newValue * $product->quantity - $toUpdate['calculated_discount']; return $toUpdate; } // When discount is updated, update calculated discount and final price if ($columnToUpdate === 'discount') { $toUpdate = [$columnToUpdate => $newValue]; $toUpdate['calculated_discount'] = Products::discount($product->price, $newValue); $toUpdate['final_price'] = $product->price - $toUpdate['calculated_discount']; return $toUpdate; } }