예제 #1
0
 /**
  * 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]);
 }
예제 #2
0
파일: Bills.php 프로젝트: bitller/nova
 /**
  * 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;
     }
 }