修正获取服务包内某一药品的剩余数量计算

This commit is contained in:
wucongxing8150 2024-04-24 09:44:49 +08:00
parent ed39096790
commit 955b24a205
9 changed files with 205 additions and 66 deletions

View File

@ -480,4 +480,9 @@ class TestController extends AbstractController
}
}
// 退款
public function refund(){
}
}

View File

@ -13,13 +13,13 @@ use Hyperf\Snowflake\Concern\Snowflake;
/**
* @property int $service_product_id 主键id
* @property int $order_service_id 订单-服务包id
* @property int $order_product_id 订单-商品id
* @property string $order_product_no 订单-商品系统编号
* @property int $product_item_id 订单-商品明细id
* @property int $product_id 商品id
* @property string $product_name 商品名称
* @property int $quantity 商品数量
* @property int $used_quantity 已使用数量
* @property string $discount_product_price 折扣商品价格
* @property \Carbon\Carbon $created_at 创建时间
* @property \Carbon\Carbon $updated_at 修改时间
* @property int $used_quantity 订单使用数量
* @property Carbon $created_at 创建时间
* @property Carbon $updated_at 修改时间
*/
class OrderServicePackageProduct extends Model
{
@ -33,7 +33,7 @@ class OrderServicePackageProduct extends Model
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['service_product_id', 'order_service_id', 'product_id', 'product_name', 'quantity', 'used_quantity', 'discount_product_price', 'created_at', 'updated_at'];
protected array $fillable = ['service_product_id', 'order_service_id', 'order_product_id', 'order_product_no', 'product_item_id', 'product_id', 'used_quantity', 'created_at', 'updated_at'];
protected string $primaryKey = "service_product_id";
@ -79,4 +79,14 @@ class OrderServicePackageProduct extends Model
{
return self::where($params)->update($data);
}
/**
* 删除
* @param array $params
* @return int|mixed
*/
public static function deleteOrderServicePackageProduct(array $params): mixed
{
return self::where($params)->delete();
}
}

View File

@ -537,22 +537,20 @@ class ImService extends BaseService
}else{
// 健康包
if ($order_inquiry['inquiry_mode'] == 8){
// 检测患者服务包内是否还存在剩余药品
$res = $OrderServicePackageService->checkOrderServiceRemainingProduct($order_service_no);
if ($res){
// 获取剩余药品数量
// 获取服务包内药品
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)){
$remaining_quantity = 0;
foreach ($order_service_package_products as $order_service_package_product){
// 获取服务包内某一药品的总数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($order_service_package_product['product_id']);
// 健康包商品数据
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)){
foreach ($order_service_package_products as $order_service_package_product){
$remaining_quantity += $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
if ($remaining_quantity < 0){
$remaining_quantity = 0;
}
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($order_service_package['order_service_id'],$order_service_package_product['product_id'],$total_quantity);
if ($remaining_quantity > 0){
break;
}
}

View File

@ -1553,16 +1553,18 @@ class InquiryService extends BaseService
// 获取剩余药品数量
$remaining_quantity = 0;
if ($order_service_package['order_service_type'] == 1) {
// 健康包商品数据
$OrderServicePackageService = new OrderServicePackageService();
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)) {
foreach ($order_service_package_products as $order_service_package_product) {
$remaining_quantity += $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
if ($remaining_quantity < 0) {
$remaining_quantity = 0;
}
if (!empty($order_service_package_products)){
foreach ($order_service_package_products as $order_service_package_product){
// 获取服务包内某一药品的总数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($order_service_package_product['product_id']);
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($order_service_package['order_service_id'],$order_service_package_product['product_id'],$total_quantity);
}
}
}

View File

@ -3284,16 +3284,18 @@ class MessagePush extends BaseService
// 获取剩余药品数量
$remaining_quantity = 0;
if ($this->order_service_package['order_service_type'] == 1){
// 健康包商品数据
$OrderServicePackageService = new OrderServicePackageService();
$params = array();
$params['order_service_id'] = $this->order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)){
foreach ($order_service_package_products as $order_service_package_product){
$remaining_quantity += $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
if ($remaining_quantity < 0){
$remaining_quantity = 0;
}
// 获取服务包内某一药品的总数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($order_service_package_product['product_id']);
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($this->order_service_package['order_service_id'],$order_service_package_product['product_id'],$total_quantity);
}
}
}
@ -3479,19 +3481,22 @@ class MessagePush extends BaseService
return;
}
$OrderServicePackageService = new OrderServicePackageService();
// 获取剩余药品数量
$remaining_quantity = 0;
if ($this->order_service_package['order_service_type'] == 1){
// 健康包商品数据
$params = array();
$params['order_service_id'] = $this->order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)){
foreach ($order_service_package_products as $order_service_package_product){
$remaining_quantity += $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
if ($remaining_quantity < 0){
$remaining_quantity = 0;
}
$params = array();
$params['order_service_id'] = $this->order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (!empty($order_service_package_products)){
foreach ($order_service_package_products as $order_service_package_product){
// 获取服务包内某一药品的总数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($order_service_package_product['product_id']);
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($this->order_service_package['order_service_id'],$order_service_package_product['product_id'],$total_quantity);
if ($remaining_quantity > 0){
break;
}
}
}

View File

@ -4,6 +4,7 @@ namespace App\Services;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\OrderInquiry;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund;
use App\Model\OrderPrescription;
@ -12,6 +13,8 @@ use App\Model\OrderProduct;
use App\Model\OrderProductCoupon;
use App\Model\OrderProductItem;
use App\Model\OrderProductRefund;
use App\Model\OrderServicePackageInquiry;
use App\Model\OrderServicePackageProduct;
use App\Model\Product;
use App\Model\ProductPlatformAmount;
use App\Model\UserPatient;
@ -100,6 +103,16 @@ class OrderProductService extends BaseService
return $result;
}
// 获取问诊订单数据
$params = array();
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
$order_inquiry = OrderInquiry::getOne($params);
if (empty($order_inquiry)){
$result['status'] = 0;
$result['message'] = "未查询到对应订单数据";
return $result;
}
// 取消药品订单
$data = array();
$data['order_product_status'] = 5;
@ -151,6 +164,15 @@ class OrderProductService extends BaseService
$params = array();
$params['amount_id'] = $product_platform_amount['amount_id'];
ProductPlatformAmount::inc($params, 'stock', (float)$item['amount']);
// 回退服务包已使用药品数量
if ($order_inquiry['inquiry_type'] == 1 && $order_inquiry['inquiry_mode'] == 8){
$params = array();
$params['order_product_id'] = $item['order_product_id'];
$params['product_item_id'] = $item['product_item_id'];
$params['product_id'] = $item['product_id'];
OrderServicePackageProduct::deleteOrderServicePackageProduct($params);
}
}
// 获取处方数据

View File

@ -1271,24 +1271,62 @@ class OrderServicePackageService extends BaseService
}
/**
* 获取服务包内某一药品的剩余数量
* @param string|int $order_service_id
* 获取服务包内某一药品的总数量
* @param string|int $product_id
* @return int
*/
public function getOrderServiceProductRemainingQuantity(string|int $order_service_id,string|int $product_id): int
public function getOrderServiceProductTotalQuantity(string|int $product_id): int
{
$total_quantity = 0;
// 获取健康包内容
$params = array();
$health_package = HealthPackage::getOne($params);
if (empty($health_package)){
return $total_quantity;
}
$params = array();
$params['package_id'] = $health_package['package_id'];
$params['product_id'] = $product_id;
$health_package_product = HealthPackage::getOne($params);
if (empty($health_package_product)){
return $total_quantity;
}
return $health_package_product['quantity'];
}
/**
* 获取服务包内某一药品的可使用数量
* @param string|int $order_service_id
* @param string|int $product_id
* @param string|int $total_quantity
* @return int
*/
public function getOrderServiceProductCanUseQuantity(string|int $order_service_id,string|int $product_id,string|int $total_quantity): int
{
$remaining_quantity = 0;
$params = array();
$params['order_service_id'] = $order_service_id;
$params['product_id'] = $product_id;
$order_service_package_product = OrderServicePackageProduct::getOne($params);
if (!empty($order_service_package_product)){
$remaining_quantity = $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
if ($remaining_quantity < 0){
$remaining_quantity = 0;
}
$order_service_package_products = OrderServicePackageProduct::getList($params);
if (empty($order_service_package_products)){
return $remaining_quantity;
}
// 订单使用数量
$used_quantity = 0;
foreach ($order_service_package_products as $order_service_package_product){
$used_quantity = $used_quantity + $order_service_package_product['used_quantity'];
}
// 剩余数量 = 总数量-使用数量
$remaining_quantity = $total_quantity - $used_quantity;
if ($remaining_quantity < 0){
$remaining_quantity = 0;
}
return $remaining_quantity;
@ -1339,6 +1377,13 @@ class OrderServicePackageService extends BaseService
*/
public function checkOrderServiceRemainingProduct(string|int $order_no): bool
{
// 获取健康包内容
$params = array();
$health_package = HealthPackage::getOne($params);
if (empty($health_package)){
return false;
}
// 获取服务包关联商品
$params = array();
$params['order_service_no'] = $order_no;
@ -1348,10 +1393,26 @@ class OrderServicePackageService extends BaseService
}
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
foreach ($order_service_package_products as $order_service_package_product){
$remaining_quantity = $order_service_package_product['quantity'] - $order_service_package_product['used_quantity'];
$params['package_id'] = $health_package['package_id'];
$health_package_products = HealthPackage::getList($params);
if (empty($health_package_products)){
return false;
}
foreach ($health_package_products as $health_package_product){
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
$params['product_id'] = $health_package_product['product_id'];
$order_service_package_products = OrderServicePackageProduct::getList($params);
$used_quantity = 0;
foreach ($order_service_package_products as $order_service_package_product){
$used_quantity = $used_quantity + $order_service_package_product['used_quantity'];
}
$remaining_quantity = $health_package_product['quantity'] - $used_quantity;
// 存在一个可使用商品数量即返回
if ($remaining_quantity > 0){
return true;
}

View File

@ -1487,7 +1487,7 @@ class PatientOrderService extends BaseService
return fail();
}
// 处理健康包赠送商品
// 处理健康包赠送商品-金额、数量
if ($order_inquiry['inquiry_type'] == 1 && $order_inquiry['inquiry_mode'] == 8){
// 获取问诊订单关联服务包id
$params = array();
@ -1503,12 +1503,22 @@ class PatientOrderService extends BaseService
$amount_total = 0;
foreach ($product_datas as &$product_data){
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductRemainingQuantity($order_service_package_inquiry['order_service_id'],$product_data['product_id']);
// 获取服务包内某一药品的数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($product_data['product_id']);
// 可用数量大于处方商品数量此情况把商品价格置为0
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($order_service_package_inquiry['order_service_id'],$product_data['product_id'],$total_quantity);
// 可用数量大于处方商品数量
if ($remaining_quantity >= $product_data['product_num']){
// 把商品价格置为0。金额按照0计算
$product_data['product_price'] = 0;
// 已使用数量 = 原数量+此次使用数量
$product_data['used_quantity'] = $product_data['product_num'];
}else{
// 已使用数量 = 最大可用数量;表示此服务包商品已使用完毕
$product_data['used_quantity'] = $remaining_quantity;
}
// 此处重新计算药品总金额
@ -1674,6 +1684,28 @@ class PatientOrderService extends BaseService
$params = array();
$params['amount_id'] = $product_platform_amount['amount_id'];
ProductPlatformAmount::dec($params, 'stock', $product_data['product_num']);
// 处理健康包数据,增加使用数量
if ($order_inquiry['inquiry_type'] == 1 && $order_inquiry['inquiry_mode'] == 8){
// 获取问诊订单关联服务包id
$params = array();
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
$order_service_package_inquiry = OrderServicePackageInquiry::getOne($params);
if (empty($order_service_package_inquiry)){
Db::rollBack();
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
}
$data = array();
$data['order_service_id'] = $order_service_package_inquiry['order_service_id'];
$data['order_product_id'] = $order_product['order_product_id'];
$data['order_product_no'] = $order_product['order_product_no'];
$data['product_item_id'] = $order_product_item['product_item_id'];
$data['product_id'] = $product_data['product_id'];
$data['used_quantity'] = $product_data['used_quantity'];
OrderServicePackageProduct::addOrderServicePackageProduct($data);
}
}
// 修改处方为已使用
@ -2009,16 +2041,20 @@ class PatientOrderService extends BaseService
}
// 此处不检测未支付的商品订单,在创建订单时会进行数量的扣减
// 处理商品数量
// 处理商品数量、金额
$OrderServicePackageService = new OrderServicePackageService();
$amount_total = 0;
foreach ($product_datas as &$product_data){
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductRemainingQuantity($order_service_package_inquiry['order_service_id'],$product_data['product_id']);
// 获取服务包内某一药品的数量
$total_quantity = $OrderServicePackageService->getOrderServiceProductTotalQuantity($product_data['product_id']);
// 可用数量大于处方商品数量此情况把商品价格置为0
// 获取服务包内某一药品的剩余数量
$remaining_quantity = $OrderServicePackageService->getOrderServiceProductCanUseQuantity($order_service_package_inquiry['order_service_id'],$product_data['product_id'],$total_quantity);
// 可用数量大于处方商品数量
if ($remaining_quantity >= $product_data['product_num']){
// 把商品价格置为0。金额按照0计算
$product_data['product_price'] = 0;
}

View File

@ -897,7 +897,7 @@ Router::addGroup('/test', function () {
//
// Router::get('/uninquiry', [TestController::class, 'uninquiry']);
// 模拟退款
// Router::get('/refund', [TestController::class, 'refund']);
Router::get('/refund', [TestController::class, 'refund']);
});