getInfo($condition); if (empty($cashier_order_info)) return $this->error(); $cashier_order_info[ 'collectmoney_config' ] = ( new Cashier() )->getCashierCollectMoneyConfig($cashier_order_info[ 'site_id' ], $cashier_order_info[ 'store_id' ])[ 'data' ]; $is_calculate = isset($params[ 'promotion' ]) ? true : false; $promotion = $params[ 'promotion' ] ?? []; $order_money = $cashier_order_info[ 'order_money' ]; $original_money = $order_money; $cashier_order_info[ 'original_money' ] = $original_money; $cashier_order_info[ 'goods_num' ] = numberFormat($cashier_order_info[ 'goods_num' ]); $cashier_order_info = array_merge($cashier_order_info, $promotion, $params); $order_id = $cashier_order_info[ 'order_id' ]; $order_goods_condition = array ( [ 'order_id', '=', $order_id ] ); $cashier_order_goods_list = model('order_goods')->getList($order_goods_condition); $order_goods_id_map = []; foreach ($cashier_order_goods_list as $goods_k => $goods_info) { $item_order_goods_id = $goods_info[ 'order_goods_id' ]; $order_goods_id_map[ $goods_k ] = $item_order_goods_id; $cashier_order_goods_list[ $goods_k ][ 'num' ] = numberFormat($cashier_order_goods_list[ $goods_k ][ 'num' ]); } $cashier_order_info[ 'order_goods_id_map' ] = $order_goods_id_map; $cashier_order_info[ 'goods_list' ] = $cashier_order_goods_list; if ($member_id > 0) { $member_model = new Member(); // $member_account_info = $member_model->getMemberDetail($member_id, $site_id)[ 'data' ] ?? []; $member_account_info = $member_model->getMemberDetailstore($member_id, $site_id,$cashier_order_info[ 'store_id' ])[ 'data' ] ?? []; $cashier_order_info[ 'member_account' ] = $member_account_info; } $result = $this->promotionCalculate($cashier_order_info, $is_calculate); if ($result[ 'code' ] < 0) { return $result; } $result_data = $result[ 'data' ]; $result = $this->payCalculate($result_data); if ($result[ 'code' ] < 0) { return $result; } return $result; } /** * 支付计算 * @param $params * @return array */ public function payCalculate($cashier_order_info) { $member_id = $cashier_order_info[ 'member_id' ] ?? 0; $pay_money = $cashier_order_info[ 'pay_money' ]; $pay_type = $cashier_order_info[ 'pay_type' ] ?? ''; if ($pay_type == 'third') { $pay_type = 'ONLINE_PAY'; } $online_type = empty($cashier_order_info[ 'online_type' ]) ? $pay_type : $cashier_order_info[ 'online_type' ]; $paid_money = 0; $cash = $cashier_order_info[ 'cash' ] ?? 0; $balance = $cashier_order_info[ 'total_balance' ]; $online_money = $cashier_order_info[ 'online_money' ] ?? 0; switch ( $online_type ) { case 'cash': $cash = $cashier_order_info[ 'cash' ]; $paid_money += $cash; break; case 'online': $online_money = $pay_money; $paid_money += $online_money; break; case 'own_wechatpay': $own_wechatpay = $pay_money; $paid_money += $own_wechatpay; break; case 'own_alipay': $own_alipay = $pay_money; $paid_money += $own_alipay; break; case 'own_pos': $own_pos = $pay_money; $paid_money += $own_pos; break; } $surplus_money = $pay_money - $paid_money; if ($surplus_money < 0) { $cash_change = abs($surplus_money); } $data = array ( 'pay_money' => $pay_money, 'paid_money' => $paid_money, 'surplus_money' => $surplus_money, 'cash' => $cash, 'cash_change' => $cash_change ?? 0, 'total_balance' => $balance,//总余额 'online_money' => $online_money, 'online_type' => $online_type, 'own_wechatpay' => $own_wechatpay ?? 0, 'own_alipay' => $own_alipay ?? 0, 'own_pos' => $own_pos ?? 0, 'pay_type' => $pay_type, ); if ($member_id > 0) { $data[ 'member_id' ] = $member_id; } $data = array_merge($cashier_order_info, $data); return $this->success($data); } /** * 活动计算 * @param $cashier_order_info */ public function promotionCalculate($calculate, $is_calculate = true) { if ($is_calculate) { $member_id = $calculate[ 'member_id' ] ?? 0; $site_id = $calculate[ 'site_id' ]; if ($member_id > 0) { $coupon_calculate = $this->couponCalculate($calculate); if ($coupon_calculate[ 'code' ] < 0) return $coupon_calculate; $calculate = $coupon_calculate[ 'data' ]; } $reduction_calculate = $this->reductionCalculate($calculate); if ($reduction_calculate[ 'code' ] < 0) return $reduction_calculate; $calculate = $reduction_calculate[ 'data' ]; if ($member_id > 0 && $calculate[ 'collectmoney_config' ][ 'point' ] > 0) { $point_calculate = $this->pointCalculate($calculate); if ($point_calculate[ 'code' ] < 0) return $point_calculate; $calculate = $point_calculate[ 'data' ]; } if ($member_id > 0 && $calculate[ 'collectmoney_config' ][ 'balance' ] > 0) { $balance_calculate = $this->balanceCalculate($calculate); if ($balance_calculate[ 'code' ] < 0) return $balance_calculate; $calculate = $balance_calculate[ 'data' ]; } $offset = $calculate[ 'offset' ] ?? []; $calculate[ 'offset' ] = $offset; } return $this->success($calculate); } /** * 调价计算 * @param $cashier_order_info */ public function reductionCalculate($cashier_order_info) { $offset = $cashier_order_info[ 'offset' ] ?? []; $reduction = $cashier_order_info[ 'reduction' ] ?? 0;//调整金额 $order_money = $cashier_order_info[ 'order_money' ]; $promotion_money = $cashier_order_info[ 'promotion_money' ]; $pay_money = $cashier_order_info[ 'pay_money' ]; if ($reduction > 0) { $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0; if ($reduction > $order_money) { $reduction = $order_money; } $order_money -= $reduction; if ($reduction > $pay_money) { $reduction = $pay_money; } $pay_money -= $reduction; $offset[ 'reduction' ] = $reduction; $cashier_order_info[ 'reduction' ] = $reduction; $offset_money += $reduction; $promotion_money += $reduction; $calculate_data[ 'offset_money' ] = $offset_money; } $cashier_order_info[ 'pay_money' ] = $pay_money; $cashier_order_info[ 'offset' ] = $offset; $cashier_order_info[ 'promotion_money' ] = $promotion_money; $cashier_order_info[ 'order_money' ] = $order_money; return $this->success($cashier_order_info); } /** * 余额计算 * @param $data */ public function balanceCalculate($cashier_order_info) { $offset = $cashier_order_info[ 'offset' ] ?? []; $pay_money = $cashier_order_info[ 'pay_money' ]; $order_money = $cashier_order_info[ 'order_money' ]; // $promotion = $cashier_order_info['promotion'] ?? []; $is_use_balance = $cashier_order_info[ 'is_use_balance' ] ?? 0; $member_account = $cashier_order_info[ 'member_account' ] ?? []; if (!empty($member_account)) { $order_type = $cashier_order_info[ 'cashier_order_type' ]; if (in_array($order_type, [ 'goods', 'card' ])) { $member_balance_total = $member_account[ 'balance_total' ] ?? 0; $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0; $balance_money = 0; if ($member_balance_total > 0) { if ($member_balance_total > $pay_money) { $balance_money = $pay_money; } else { $balance_money = $member_balance_total; } } $balance_array = array ( 'balance' => $balance_money, 'balance_money' => $balance_money, 'balance_switch' => false ); $total_balance = 0; if ($pay_money > 0) { if ($balance_money > 0) { $balance_array[ 'balance_switch' ] = true; if ($is_use_balance > 0) { $total_balance = $balance_money; $pay_money -= $balance_money; } } else { $is_use_balance = 0; } } else { $is_use_balance = 0; } $offset[ 'balance' ] = $balance_array; $cashier_order_info[ 'is_use_balance' ] = $is_use_balance; $cashier_order_info[ 'order_money' ] = $order_money; $cashier_order_info[ 'pay_money' ] = $pay_money; $cashier_order_info[ 'offset' ] = $offset; $cashier_order_info[ 'total_balance' ] = $total_balance ?? 0; $offset_money += $total_balance; $cashier_order_info[ 'offset_money' ] = $offset_money; } } return $this->success($cashier_order_info); } /** * 优惠券计算 * @param $calculate_data */ public function couponCalculate($calculate_data) { $member_id = $calculate_data[ 'member_id' ] ?? 0; $site_id = $calculate_data[ 'site_id' ]; if ($member_id > 0) { $order_type = $calculate_data[ 'cashier_order_type' ]; //只有开单和卡项可以用优惠券 if (in_array($order_type, [ 'goods', 'card' ])) { $pay_money = $calculate_data[ 'pay_money' ]; $order_money = $calculate_data[ 'order_money' ]; $site_id = $calculate_data[ 'site_id' ]; $goods_list = $calculate_data[ 'goods_list' ] ?? []; $is_can_coupon = true; foreach ($goods_list as $k => $v) { $trade_type = $v[ 'goods_class' ]; if ($trade_type == 'money') { $is_can_coupon = false; } } $goods_ids = array_unique(array_column($goods_list, 'goods_id')); $goods_money = $calculate_data[ 'goods_money' ];//优惠券现在取用的是商品价用作门槛 // $real_goods_money = $calculate_data['real_goods_money'] ?? 0; $real_goods_money = $goods_money; $offset = $calculate_data[ 'offset' ] ?? []; if ($order_money > 0 && $pay_money > 0) { $coupon_list = []; //先查询全场通用的优惠券 $member_coupon_model = new Coupon(); $all_condition = array ( [ 'member_id', '=', $member_id ], [ 'state', '=', 1 ], [ 'site_id', '=', $site_id ], [ 'goods_type', '=', 1 ],//全场 // [ 'at_least', '<=', $data[ 'shop_goods_list' ][ 'goods_money' ] ] ); $all_coupon_list = $member_coupon_model->getCouponList($all_condition)[ 'data' ] ?? []; //在查询部分商品的优惠券 $item_condition = array ( [ 'member_id', '=', $member_id ], [ 'state', '=', 1 ], [ 'site_id', '=', $site_id ], [ 'goods_type', '=', 2 ], ); $item_like_array = []; foreach ($goods_ids as $k => $v) { $item_like_array[] = '%,' . $v . ',%'; } $item_condition[] = [ 'goods_ids', 'like', $item_like_array, 'OR' ]; $item_coupon_list = $member_coupon_model->getCouponList($item_condition)[ 'data' ] ?? []; $member_coupon_list = array_merge($all_coupon_list, $item_coupon_list); } $default_coupon_id = 0; $default_coupon_end_time = 0; $default_coupon_type = ''; $default_coupon_discount = 0; $default_coupon_offset_money = 0; $default_coupon_money = 0; $temp_member_coupon_list = []; $temp_cache_coupon = [];//用以减轻I/O压力 if (!empty($member_coupon_list)) { foreach ($member_coupon_list as $k => $v) { // $parent_id = $v['coupon_type_id']; $coupon_id = $v[ 'coupon_id' ]; $at_least = $v[ 'at_least' ];//最小条件 $item_type = $v[ 'type' ];//reward-满减 discount-折扣 random-随机 $goods_type = $v[ 'goods_type' ]; // $temp_cache_coupon_item = $temp_cache_coupon[$parent_id] ?? []; // if (empty($temp_cache_coupon_item)) { if ($goods_type == 1) {//全场 $intersect = $goods_ids; } else { $item_goods_ids = explode(',', $v[ 'goods_ids' ]); $intersect = array_intersect($item_goods_ids, $goods_ids); } //计算这几个商品的商品总价 $goods_sum = 0; $coupon_order_goods_ids = array (); $item_coupon_goods_list = []; foreach ($goods_list as $goods_k => $goods_v) { $item_id = $goods_v[ 'goods_id' ]; if (in_array($item_id, $intersect)) { $goods_sum += $goods_v[ 'real_goods_money' ] ?? 0;//这儿用 商品价还是商品真实价格 $coupon_order_goods_ids[] = $goods_k; $item_coupon_goods_list[] = $goods_v; } } // $temp_cache_coupon[$parent_id] = ['goods_sum' => $goods_sum, 'coupon_order_goods_ids' => $coupon_order_goods_ids, 'coupon_goods_list' => $item_coupon_goods_list]; // } else { // $goods_sum = $temp_cache_coupon_item['goods_sum']; // $coupon_order_goods_ids = $temp_cache_coupon_item['coupon_order_goods_ids']; // $item_coupon_goods_list = $temp_cache_coupon_item['coupon_goods_list']; // } //判断它支持的商品的商品金额够不够最低金额 if ($goods_sum < $at_least) { //移除会员优惠券 unset($member_coupon_list[ $k ]); continue; } switch ( $item_type ) { case 'reward'://满减 $item_coupon_money = $v[ 'money' ]; if ($item_coupon_money > $goods_sum) { $item_coupon_money = $goods_sum; } break; case 'discount'://折扣 $item_discount = $v[ 'discount' ];//折扣 $item_discount_limit = $v[ 'discount_limit' ];//最多抵扣 //计算折扣优惠金额 $item_coupon_money = $goods_sum * ( 10 - $item_discount ) / 10; $item_coupon_money = $item_coupon_money > $item_discount_limit && $item_discount_limit != 0 ? $item_discount_limit : $item_coupon_money; $item_coupon_money = $item_coupon_money > $goods_sum ? $goods_sum : $item_coupon_money; $item_coupon_money = moneyFormat($item_coupon_money); break; case 'divideticket'://随机 $item_coupon_money = $v[ 'money' ]; if ($item_coupon_money > $goods_sum) { $item_coupon_money = $goods_sum; } break; } $member_coupon_list[ $k ][ 'coupon_goods_money' ] = $goods_sum; $member_coupon_list[ $k ][ 'coupon_money' ] = $item_coupon_money; $member_coupon_list[ $k ][ 'coupon_order_goods_ids' ] = $coupon_order_goods_ids; $member_coupon_list[ $k ][ 'coupon_goods_list' ] = $item_coupon_goods_list; //一个准则,折扣券不优先用 if ($item_coupon_money > $default_coupon_money) { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; if ($item_type == 'discount') { $default_coupon_discount = $v[ 'discount' ]; } else { $default_coupon_offset_money = $v[ 'money' ]; } $default_coupon_money = $item_coupon_money; } else if ($item_coupon_money == $default_coupon_money) { if ($item_type == 'discount') { if ($default_coupon_type == $item_type) { if ($v[ 'discount_limit' ] < $default_coupon_discount) { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; $default_coupon_discount = $v[ 'discount' ]; } else if ($v[ 'discount_limit' ] == $default_coupon_discount) { if ($v[ 'end_time' ] < $default_coupon_end_time) { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; $default_coupon_discount = $v[ 'discount' ]; } } } } else { if ($default_coupon_type == $item_type) { if ($v[ 'money' ] < $default_coupon_offset_money) { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; $default_coupon_discount = $v[ 'money' ]; } else if ($v[ 'money' ] == $default_coupon_offset_money) { if ($v[ 'end_time' ] < $default_coupon_end_time) { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; $default_coupon_discount = $v[ 'money' ]; } } } else { $default_coupon_id = $coupon_id; $default_coupon_end_time = $v[ 'end_time' ]; $default_coupon_type = $item_type; $default_coupon_offset_money = $v[ 'money' ]; } } } } $temp_member_coupon_list = array_column($member_coupon_list, null, 'coupon_id'); } $coupon_id = $calculate_data[ 'coupon_id' ] ?? ''; $coupon_money = 0; $coupon_order_goods_ids = []; //计算优惠券优惠 if (!empty($coupon_id)) { $item_coupon_info = $temp_member_coupon_list[ $coupon_id ] ?? []; //剔除非法代金券 if (empty($item_coupon_info)) { $coupon_id = 0; } else { $item_coupon_money = $item_coupon_info[ 'coupon_money' ]; $real_goods_money -= $item_coupon_money; $coupon_money += $item_coupon_money; $coupon_order_goods_ids = $item_coupon_info[ 'coupon_order_goods_ids' ]; $coupon_goods_list = $item_coupon_info[ 'coupon_goods_list' ]; $coupon_goods_money = $item_coupon_info[ 'coupon_goods_money' ]; if ($item_coupon_money > $coupon_goods_money) { $item_coupon_money = $coupon_goods_money; } $coupon_goods_list = $this->goodsCouponCalculate($coupon_goods_list, $coupon_goods_money, $item_coupon_money); $coupon_goods_column = array_column($coupon_goods_list, 'null', 'order_goods_id'); foreach ($goods_list as $k => $v) { if (in_array($v[ 'order_goods_id' ], $coupon_order_goods_ids)) { $goods_list[ $k ] = $coupon_goods_column[ $v[ 'order_goods_id' ] ]; } } } } if ($is_can_coupon) { $coupon_switch = empty($member_coupon_list) ? false : true; } else { $coupon_switch = false; } $coupon_array = [ 'member_coupon_list' => $member_coupon_list ?? [], 'coupon_switch' => $coupon_switch ]; $offset[ 'coupon_array' ] = $coupon_array; $calculate_data[ 'offset' ] = $offset; $calculate_data[ 'real_goods_money' ] = $real_goods_money; $calculate_data[ 'coupon_money' ] = $coupon_money; $calculate_data[ 'goods_list' ] = $goods_list; $calculate_data[ 'coupon_id' ] = $coupon_id; $calculate_data[ 'coupon_order_goods_ids' ] = $coupon_order_goods_ids; $pay_money -= $coupon_money; $order_money -= $coupon_money; $calculate_data[ 'pay_money' ] = $pay_money; $calculate_data[ 'order_money' ] = $order_money; } } return $this->success($calculate_data); } /** * 按比例摊派优惠券优惠 */ public function goodsCouponCalculate($goods_list, $goods_money, $coupon_money) { $temp_coupon_money = $coupon_money; $last_key = count($goods_list) - 1; foreach ($goods_list as $k => $v) { if ($last_key != $k) { $item_coupon_money = moneyFormat($v[ 'real_goods_money' ] / $goods_money * $coupon_money); } else { $item_coupon_money = $temp_coupon_money; } $temp_coupon_money -= $item_coupon_money; $goods_list[ $k ][ 'coupon_money' ] = $item_coupon_money; $real_goods_money = $v[ 'real_goods_money' ] - $item_coupon_money; $real_goods_money = $real_goods_money < 0 ? 0 : $real_goods_money; $goods_list[ $k ][ 'real_goods_money' ] = $real_goods_money; //真实订单项金额 } return $goods_list; } public function roundCalculate($cashier_order_info) { $offset = $cashier_order_info[ 'offset' ] ?? []; $reduction = $cashier_order_info[ 'reduction' ] ?? 0;//调整金额 $order_money = $cashier_order_info[ 'order_money' ]; $promotion_money = $cashier_order_info[ 'promotion_money' ]; $round_type = $cashier_order_info[ 'round_type' ];//收银台 抹零方式 1抹分 2抹角 $pay_money = $cashier_order_info[ 'pay_money' ]; if (!empty($round_type)) { $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0; switch ( $round_type ) { case 1: $new_order_money = round(intval($order_money * 10) / 10, 2); break; case 2: $new_order_money = round(intval($order_money * 100) / 100, 2); break; } $round_money = $order_money - $new_order_money; // if ($round_money > $order_money) { // $round_money = $pay_money; // } $order_money -= $round_money; // if ($round_money > $pay_money) { // $round_money = $pay_money; // } $pay_money -= $round_money; $offset[ 'round' ] = $round_money; $cashier_order_info[ 'round_money' ] = $round_money; $offset_money += $round_money; $promotion_money += $round_money; $calculate_data[ 'offset_money' ] = $offset_money; } $cashier_order_info[ 'pay_money' ] = $pay_money; $cashier_order_info[ 'offset' ] = $offset; $cashier_order_info[ 'promotion_money' ] = $promotion_money; $cashier_order_info[ 'order_money' ] = $order_money; return $this->success($cashier_order_info); } /** * 积分计算 * @param $data */ public function pointCalculate($cashier_order_info) { $member_account = $cashier_order_info[ 'member_account' ] ?? []; if (addon_is_exit('pointcash')) { if (!empty($member_account)) { $order_type = $cashier_order_info[ 'cashier_order_type' ]; if (in_array($order_type, [ 'goods', 'card' ])) { $site_id = $cashier_order_info[ 'site_id' ]; $offset = $cashier_order_info[ 'offset' ] ?? []; $pay_money = $cashier_order_info[ 'pay_money' ]; $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0; $order_money = $cashier_order_info[ 'order_money' ]; $site_type = $cashier_order_info[ 'site_type' ] ?? ''; $point = 0; $point_money = 0; $use_point_money = 0; $use_point = 0; $is_use_point = $cashier_order_info[ 'is_use_point' ] ?? 0;//使用积分 //积分 $point_config_model = new PointConfig(); $point_value = $point_config_model->getPointCashConfig($site_id)[ 'data' ][ 'value' ] ?? []; $is_use = $point_value[ 'is_enable' ] ?? 0; if ($is_use > 0) { $is_limit = $point_value[ 'is_limit' ]; if ($is_limit == 1) { $limit = $point_value[ 'limit' ]; if ($order_money < $limit) { return $this->success($cashier_order_info); } } $max_point_money = 0; if ($point_value[ 'is_limit_use' ] == 1) { if ($point_value[ 'type' ] == 0) { $max_point_money = $point_value[ 'max_use' ]; } else { $ratio = $point_value[ 'max_use' ] / 100; $max_point_money = round(( $order_money * $ratio ), 2); } if ($max_point_money > $order_money) { $max_point_money = $order_money; } } $point_exchange_rate = $point_value[ 'cash_rate' ] ?? 0;//积分兑换比率 为0的话认为没有开启积分兑换 if ($point_exchange_rate > 0) { $member_account_point = $member_account[ 'point' ];//会员积分 if ($member_account_point > 0) {//拥有积分大于0 $point_money = round($member_account_point / $point_exchange_rate, 2);//积分抵扣金额 if ($point_money > $pay_money) { $point_money = $pay_money; } if ($max_point_money != 0 && $point_money > $max_point_money) { $point_money = $max_point_money; } $point = ceil($point_money * $point_exchange_rate); } } } $point_array = array ( 'point' => $point, 'point_money' => $point_money, 'point_switch' => false ); if ($pay_money > 0) { if ($point > 0) { $point_array[ 'point_switch' ] = true; //存在可用积分且选用了积分抵扣 if ($is_use_point) { $order_money -= $point_money; $pay_money -= $point_money; $use_point_money = $point_money; $use_point = $point; } } else { $is_use_point = 0; } } else { $is_use_point = 0; } $offset[ 'point_array' ] = $point_array; $cashier_order_info[ 'offset' ] = $offset; $cashier_order_info[ 'order_money' ] = $order_money; $cashier_order_info[ 'is_use_point' ] = $is_use_point; $offset_money += $use_point_money; $cashier_order_info[ 'offset_money' ] = $offset_money; $cashier_order_info[ 'point_money' ] = $use_point_money;//积分抵扣多少金额 $cashier_order_info[ 'point' ] = $use_point;//使用多少个积分 $cashier_order_info[ 'pay_money' ] = $pay_money; } } } return $this->success($cashier_order_info); } }