| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com
- * =========================================================
- */
- namespace addon\cashier\model\order;
- use addon\cashier\model\Cashier;
- use addon\coupon\model\Coupon;
- use addon\pointcash\model\Config as PointConfig;
- use app\model\BaseModel;
- use app\model\member\Member;
- /**
- * 收银订单计算
- *
- * @author Administrator
- *
- */
- class CashierOrderCalculate extends BaseModel
- {
- /**
- * 计算
- * @param $params
- */
- public function calculate($params)
- {
- $site_id = $params[ 'site_id' ];
- $out_trade_no = $params[ 'out_trade_no' ];
- $member_id = $params[ 'member_id' ] ?? 0;
- if ($member_id == 0) {
- unset($params[ 'member_id' ]);
- }
- $condition = array (
- [ 'out_trade_no', '=', $out_trade_no ]
- );
- $cashier_order_info = model('order')->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);
- }
- }
|