From 8afd0d91ed8829b920219cf676745156c219f421 Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Tue, 26 Nov 2024 16:39:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=8D=AF=E5=93=81=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=97=B6=EF=BC=8C=E6=96=B0=E5=A2=9E=E4=BA=86=E8=8D=AF?= =?UTF-8?q?=E5=93=81=E6=98=8E=E7=BB=86=E7=9A=84=E5=AE=9E=E9=99=85=E9=87=91?= =?UTF-8?q?=E9=A2=9D=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/TestController.php | 39 ++++++++- app/Model/OrderProductItem.php | 5 +- app/Services/OrderPrescriptionService.php | 2 + app/Services/PatientOrderService.php | 98 +++++++++++++++++++++++ 4 files changed, 141 insertions(+), 3 deletions(-) diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index 137a70f..09aa401 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -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); } // 退款 diff --git a/app/Model/OrderProductItem.php b/app/Model/OrderProductItem.php index dd6b378..178d17e 100644 --- a/app/Model/OrderProductItem.php +++ b/app/Model/OrderProductItem.php @@ -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"; diff --git a/app/Services/OrderPrescriptionService.php b/app/Services/OrderPrescriptionService.php index 8b19e87..63e65d3 100644 --- a/app/Services/OrderPrescriptionService.php +++ b/app/Services/OrderPrescriptionService.php @@ -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 { diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index c3b3da8..017347e 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -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; + } } \ No newline at end of file