创建药品订单时,新增了药品明细的实际金额字段

This commit is contained in:
wucongxing8150 2024-11-26 16:39:13 +08:00
parent d83ec385a7
commit 8afd0d91ed
4 changed files with 141 additions and 3 deletions

View File

@ -467,7 +467,44 @@ class TestController extends AbstractController
}
public function test_17(){
// 测试计算药品订单明细实际金额
// $product_datas = array(
// array(
// "product_id" => 1,
// "product_price" => 10,
// "actual_product_price" => 10,
// "actual_quantity" => 2,
// ),
// array(
// "product_id" => 2,
// "product_price" => 20,
// "actual_product_price" => 20,
// "actual_quantity" => 2,
// ),
// array(
// "product_id" => 3,
// "product_price" => 5,
// "actual_product_price" => 5,
// "actual_quantity" => 2,
// ),
// );
//
// $coupons = array(
// array(
// "application_scope" => 1,
// "coupon_price" => 3,
// "product_id" => "",
// ),
// array(
// "application_scope" => 5,
// "coupon_price" => 1,
// "product_id" => "1",
// )
// );
//
// $PatientOrderService = new PatientOrderService();
// $product_datas = $PatientOrderService->calculateProductOrderItemActualPrice($product_datas,$coupons);
// dump($product_datas);
}
// 退款

View File

@ -18,6 +18,7 @@ use Hyperf\Snowflake\Concern\Snowflake;
* @property int $product_id 商品id
* @property string $product_name 商品名称
* @property string $product_price 商品价格
* @property string $actual_product_price 实际商品价格
* @property string $product_platform_code 商品处方平台编码
* @property int $amount 数量
* @property string $manufacturer 生产厂家
@ -25,7 +26,7 @@ use Hyperf\Snowflake\Concern\Snowflake;
* @property string $product_spec 商品规格
* @property \Carbon\Carbon $created_at 创建时间
* @property \Carbon\Carbon $updated_at 修改时间
* @property-read Product $Product
* @property-read Product|null $Product
*/
class OrderProductItem extends Model
{
@ -39,7 +40,7 @@ class OrderProductItem extends Model
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['product_item_id', 'order_product_id', 'order_inquiry_id', 'order_prescription_id', 'product_id', 'product_name', 'product_price', 'product_platform_code', 'amount', 'manufacturer', 'product_cover_img', 'product_spec', 'created_at', 'updated_at'];
protected array $fillable = ['product_item_id', 'order_product_id', 'order_inquiry_id', 'order_prescription_id', 'product_id', 'product_name', 'product_price', 'actual_product_price', 'product_platform_code', 'amount', 'manufacturer', 'product_cover_img', 'product_spec', 'created_at', 'updated_at'];
protected string $primaryKey = "product_item_id";

View File

@ -176,6 +176,8 @@ class OrderPrescriptionService extends BaseService
* 上报处方平台
* @param string $order_product_id 商品订单id
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function reportPrescription(string $order_product_id): bool
{

View File

@ -1647,6 +1647,9 @@ class PatientOrderService extends BaseService
// 获取可用优惠卷总金额
$coupon_amount_total = $userCouponService->getCouponTotalPrice($user_coupons);
// 计算药品订单明细实际金额
$product_datas = $this->calculateProductOrderItemActualPrice($product_datas,$user_coupons);
// 处理运费数据
$app_env = config('app_env', 'prod');
if ($app_env != "dev") {
@ -1767,6 +1770,7 @@ class PatientOrderService extends BaseService
$data['product_id'] = $product_data['product_id'];
$data['product_name'] = $product_data['product_name'];
$data['product_price'] = bcmul((string)$product_data['product_price'], (string)$product_data['actual_quantity'], 3);
$data['actual_product_price'] = $product_data['actual_product_price'];
$data['product_platform_code'] = $product_data['product_platform_code'];
$data['amount'] = $product_data['product_num'];
$data['manufacturer'] = $product_data['manufacturer'];
@ -3404,4 +3408,98 @@ class PatientOrderService extends BaseService
return "";
}
/**
* 计算药品订单明细实际金额
* 此方法暂不考虑按品牌,按类别使用情况
* @param array $product_datas 药品数据
* @param array $coupons 优惠券数据
* @return array
*/
public function calculateProductOrderItemActualPrice(array &$product_datas, array $coupons): array
{
if (empty($coupons)){
return $product_datas;
}
foreach ($product_datas as &$product_data){
$product_data["actual_product_price"] = bcmul(
$product_data['actual_product_price'],
$product_data['actual_quantity'],
3
);
}
// 处理单一商品使用优惠券
foreach ($coupons as $coupon){
// 适用范围1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品)
if ($coupon["application_scope"] == 5){
$product_ids = explode(',',$coupon['product_id']);
foreach ($product_datas as &$product_data){
if (in_array($product_data['product_id'],$product_ids)){
$product_data["actual_product_price"] = bcsub(
$product_data['actual_product_price'],
$coupon['coupon_price'],
2
);
}
}
}
}
// 处理订单通用优惠券
$coupon_total_amount = 0; // 订单通用优惠券总金额
foreach ($coupons as $coupon){
// 适用范围1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品)
if ($coupon["application_scope"] == 6 || $coupon["application_scope"] == 1){
$coupon_total_amount = bcadd($coupon_total_amount,$coupon['coupon_price'],2);
}
}
if ($coupon_total_amount != 0){
$product_total_amount = 0; // 订单明细总金额
foreach ($product_datas as &$product_data){
$product_total_amount = bcadd(
$product_total_amount,
$product_data['actual_product_price'],
3
);
}
$used_coupon_total_amount = 0; // 已使用的优惠券金额
foreach ($product_datas as &$product_data){
// 本商品可优惠的金额 = (商品价格 / 商品总价格) * 优惠券总金额
$amount = bcmul(
bcdiv(
$product_data['actual_product_price'],
$product_total_amount,
2
),
$coupon_total_amount,
2
);
// 分配当前商品的优惠金额 = 原金额 - 本商品可优惠的金额
$product_data["actual_product_price"] = bcsub(
$product_data['actual_product_price'],
$amount,
2
);
$used_coupon_total_amount = bcadd($used_coupon_total_amount,$amount,2);
}
// 处理浮点误差,将剩余金额分配到第一个商品
$remaining_amount = bcsub($coupon_total_amount,$used_coupon_total_amount,2);
if ($remaining_amount != 0){
$product_datas[0]['actual_product_price'] = bcsub(
$product_datas[0]["actual_product_price"],
$remaining_amount,
2
);
}
}
return $product_datas;
}
}