CashierOrderCalculate.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\cashier\model\order;
  11. use addon\cashier\model\Cashier;
  12. use addon\coupon\model\Coupon;
  13. use addon\pointcash\model\Config as PointConfig;
  14. use app\model\BaseModel;
  15. use app\model\member\Member;
  16. /**
  17. * 收银订单计算
  18. *
  19. * @author Administrator
  20. *
  21. */
  22. class CashierOrderCalculate extends BaseModel
  23. {
  24. /**
  25. * 计算
  26. * @param $params
  27. */
  28. public function calculate($params)
  29. {
  30. $site_id = $params[ 'site_id' ];
  31. $out_trade_no = $params[ 'out_trade_no' ];
  32. $member_id = $params[ 'member_id' ] ?? 0;
  33. if ($member_id == 0) {
  34. unset($params[ 'member_id' ]);
  35. }
  36. $condition = array (
  37. [ 'out_trade_no', '=', $out_trade_no ]
  38. );
  39. $cashier_order_info = model('order')->getInfo($condition);
  40. if (empty($cashier_order_info))
  41. return $this->error();
  42. $cashier_order_info[ 'collectmoney_config' ] = ( new Cashier() )->getCashierCollectMoneyConfig($cashier_order_info[ 'site_id' ], $cashier_order_info[ 'store_id' ])[ 'data' ];
  43. $is_calculate = isset($params[ 'promotion' ]) ? true : false;
  44. $promotion = $params[ 'promotion' ] ?? [];
  45. $order_money = $cashier_order_info[ 'order_money' ];
  46. $original_money = $order_money;
  47. $cashier_order_info[ 'original_money' ] = $original_money;
  48. $cashier_order_info[ 'goods_num' ] = numberFormat($cashier_order_info[ 'goods_num' ]);
  49. $cashier_order_info = array_merge($cashier_order_info, $promotion, $params);
  50. $order_id = $cashier_order_info[ 'order_id' ];
  51. $order_goods_condition = array (
  52. [ 'order_id', '=', $order_id ]
  53. );
  54. $cashier_order_goods_list = model('order_goods')->getList($order_goods_condition);
  55. $order_goods_id_map = [];
  56. foreach ($cashier_order_goods_list as $goods_k => $goods_info) {
  57. $item_order_goods_id = $goods_info[ 'order_goods_id' ];
  58. $order_goods_id_map[ $goods_k ] = $item_order_goods_id;
  59. $cashier_order_goods_list[ $goods_k ][ 'num' ] = numberFormat($cashier_order_goods_list[ $goods_k ][ 'num' ]);
  60. }
  61. $cashier_order_info[ 'order_goods_id_map' ] = $order_goods_id_map;
  62. $cashier_order_info[ 'goods_list' ] = $cashier_order_goods_list;
  63. if ($member_id > 0) {
  64. $member_model = new Member();
  65. // $member_account_info = $member_model->getMemberDetail($member_id, $site_id)[ 'data' ] ?? [];
  66. $member_account_info = $member_model->getMemberDetailstore($member_id, $site_id,$cashier_order_info[ 'store_id' ])[ 'data' ] ?? [];
  67. $cashier_order_info[ 'member_account' ] = $member_account_info;
  68. }
  69. $result = $this->promotionCalculate($cashier_order_info, $is_calculate);
  70. if ($result[ 'code' ] < 0) {
  71. return $result;
  72. }
  73. $result_data = $result[ 'data' ];
  74. $result = $this->payCalculate($result_data);
  75. if ($result[ 'code' ] < 0) {
  76. return $result;
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 支付计算
  82. * @param $params
  83. * @return array
  84. */
  85. public function payCalculate($cashier_order_info)
  86. {
  87. $member_id = $cashier_order_info[ 'member_id' ] ?? 0;
  88. $pay_money = $cashier_order_info[ 'pay_money' ];
  89. $pay_type = $cashier_order_info[ 'pay_type' ] ?? '';
  90. if ($pay_type == 'third') {
  91. $pay_type = 'ONLINE_PAY';
  92. }
  93. $online_type = empty($cashier_order_info[ 'online_type' ]) ? $pay_type : $cashier_order_info[ 'online_type' ];
  94. $paid_money = 0;
  95. $cash = $cashier_order_info[ 'cash' ] ?? 0;
  96. $balance = $cashier_order_info[ 'total_balance' ];
  97. $online_money = $cashier_order_info[ 'online_money' ] ?? 0;
  98. switch ( $online_type ) {
  99. case 'cash':
  100. $cash = $cashier_order_info[ 'cash' ];
  101. $paid_money += $cash;
  102. break;
  103. case 'online':
  104. $online_money = $pay_money;
  105. $paid_money += $online_money;
  106. break;
  107. case 'own_wechatpay':
  108. $own_wechatpay = $pay_money;
  109. $paid_money += $own_wechatpay;
  110. break;
  111. case 'own_alipay':
  112. $own_alipay = $pay_money;
  113. $paid_money += $own_alipay;
  114. break;
  115. case 'own_pos':
  116. $own_pos = $pay_money;
  117. $paid_money += $own_pos;
  118. break;
  119. }
  120. $surplus_money = $pay_money - $paid_money;
  121. if ($surplus_money < 0) {
  122. $cash_change = abs($surplus_money);
  123. }
  124. $data = array (
  125. 'pay_money' => $pay_money,
  126. 'paid_money' => $paid_money,
  127. 'surplus_money' => $surplus_money,
  128. 'cash' => $cash,
  129. 'cash_change' => $cash_change ?? 0,
  130. 'total_balance' => $balance,//总余额
  131. 'online_money' => $online_money,
  132. 'online_type' => $online_type,
  133. 'own_wechatpay' => $own_wechatpay ?? 0,
  134. 'own_alipay' => $own_alipay ?? 0,
  135. 'own_pos' => $own_pos ?? 0,
  136. 'pay_type' => $pay_type,
  137. );
  138. if ($member_id > 0) {
  139. $data[ 'member_id' ] = $member_id;
  140. }
  141. $data = array_merge($cashier_order_info, $data);
  142. return $this->success($data);
  143. }
  144. /**
  145. * 活动计算
  146. * @param $cashier_order_info
  147. */
  148. public function promotionCalculate($calculate, $is_calculate = true)
  149. {
  150. if ($is_calculate) {
  151. $member_id = $calculate[ 'member_id' ] ?? 0;
  152. $site_id = $calculate[ 'site_id' ];
  153. if ($member_id > 0) {
  154. $coupon_calculate = $this->couponCalculate($calculate);
  155. if ($coupon_calculate[ 'code' ] < 0)
  156. return $coupon_calculate;
  157. $calculate = $coupon_calculate[ 'data' ];
  158. }
  159. $reduction_calculate = $this->reductionCalculate($calculate);
  160. if ($reduction_calculate[ 'code' ] < 0)
  161. return $reduction_calculate;
  162. $calculate = $reduction_calculate[ 'data' ];
  163. if ($member_id > 0 && $calculate[ 'collectmoney_config' ][ 'point' ] > 0) {
  164. $point_calculate = $this->pointCalculate($calculate);
  165. if ($point_calculate[ 'code' ] < 0)
  166. return $point_calculate;
  167. $calculate = $point_calculate[ 'data' ];
  168. }
  169. if ($member_id > 0 && $calculate[ 'collectmoney_config' ][ 'balance' ] > 0) {
  170. $balance_calculate = $this->balanceCalculate($calculate);
  171. if ($balance_calculate[ 'code' ] < 0)
  172. return $balance_calculate;
  173. $calculate = $balance_calculate[ 'data' ];
  174. }
  175. $offset = $calculate[ 'offset' ] ?? [];
  176. $calculate[ 'offset' ] = $offset;
  177. }
  178. return $this->success($calculate);
  179. }
  180. /**
  181. * 调价计算
  182. * @param $cashier_order_info
  183. */
  184. public function reductionCalculate($cashier_order_info)
  185. {
  186. $offset = $cashier_order_info[ 'offset' ] ?? [];
  187. $reduction = $cashier_order_info[ 'reduction' ] ?? 0;//调整金额
  188. $order_money = $cashier_order_info[ 'order_money' ];
  189. $promotion_money = $cashier_order_info[ 'promotion_money' ];
  190. $pay_money = $cashier_order_info[ 'pay_money' ];
  191. if ($reduction > 0) {
  192. $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0;
  193. if ($reduction > $order_money) {
  194. $reduction = $order_money;
  195. }
  196. $order_money -= $reduction;
  197. if ($reduction > $pay_money) {
  198. $reduction = $pay_money;
  199. }
  200. $pay_money -= $reduction;
  201. $offset[ 'reduction' ] = $reduction;
  202. $cashier_order_info[ 'reduction' ] = $reduction;
  203. $offset_money += $reduction;
  204. $promotion_money += $reduction;
  205. $calculate_data[ 'offset_money' ] = $offset_money;
  206. }
  207. $cashier_order_info[ 'pay_money' ] = $pay_money;
  208. $cashier_order_info[ 'offset' ] = $offset;
  209. $cashier_order_info[ 'promotion_money' ] = $promotion_money;
  210. $cashier_order_info[ 'order_money' ] = $order_money;
  211. return $this->success($cashier_order_info);
  212. }
  213. /**
  214. * 余额计算
  215. * @param $data
  216. */
  217. public function balanceCalculate($cashier_order_info)
  218. {
  219. $offset = $cashier_order_info[ 'offset' ] ?? [];
  220. $pay_money = $cashier_order_info[ 'pay_money' ];
  221. $order_money = $cashier_order_info[ 'order_money' ];
  222. // $promotion = $cashier_order_info['promotion'] ?? [];
  223. $is_use_balance = $cashier_order_info[ 'is_use_balance' ] ?? 0;
  224. $member_account = $cashier_order_info[ 'member_account' ] ?? [];
  225. if (!empty($member_account)) {
  226. $order_type = $cashier_order_info[ 'cashier_order_type' ];
  227. if (in_array($order_type, [ 'goods', 'card' ])) {
  228. $member_balance_total = $member_account[ 'balance_total' ] ?? 0;
  229. $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0;
  230. $balance_money = 0;
  231. if ($member_balance_total > 0) {
  232. if ($member_balance_total > $pay_money) {
  233. $balance_money = $pay_money;
  234. } else {
  235. $balance_money = $member_balance_total;
  236. }
  237. }
  238. $balance_array = array (
  239. 'balance' => $balance_money,
  240. 'balance_money' => $balance_money,
  241. 'balance_switch' => false
  242. );
  243. $total_balance = 0;
  244. if ($pay_money > 0) {
  245. if ($balance_money > 0) {
  246. $balance_array[ 'balance_switch' ] = true;
  247. if ($is_use_balance > 0) {
  248. $total_balance = $balance_money;
  249. $pay_money -= $balance_money;
  250. }
  251. } else {
  252. $is_use_balance = 0;
  253. }
  254. } else {
  255. $is_use_balance = 0;
  256. }
  257. $offset[ 'balance' ] = $balance_array;
  258. $cashier_order_info[ 'is_use_balance' ] = $is_use_balance;
  259. $cashier_order_info[ 'order_money' ] = $order_money;
  260. $cashier_order_info[ 'pay_money' ] = $pay_money;
  261. $cashier_order_info[ 'offset' ] = $offset;
  262. $cashier_order_info[ 'total_balance' ] = $total_balance ?? 0;
  263. $offset_money += $total_balance;
  264. $cashier_order_info[ 'offset_money' ] = $offset_money;
  265. }
  266. }
  267. return $this->success($cashier_order_info);
  268. }
  269. /**
  270. * 优惠券计算
  271. * @param $calculate_data
  272. */
  273. public function couponCalculate($calculate_data)
  274. {
  275. $member_id = $calculate_data[ 'member_id' ] ?? 0;
  276. $site_id = $calculate_data[ 'site_id' ];
  277. if ($member_id > 0) {
  278. $order_type = $calculate_data[ 'cashier_order_type' ];
  279. //只有开单和卡项可以用优惠券
  280. if (in_array($order_type, [ 'goods', 'card' ])) {
  281. $pay_money = $calculate_data[ 'pay_money' ];
  282. $order_money = $calculate_data[ 'order_money' ];
  283. $site_id = $calculate_data[ 'site_id' ];
  284. $goods_list = $calculate_data[ 'goods_list' ] ?? [];
  285. $is_can_coupon = true;
  286. foreach ($goods_list as $k => $v) {
  287. $trade_type = $v[ 'goods_class' ];
  288. if ($trade_type == 'money') {
  289. $is_can_coupon = false;
  290. }
  291. }
  292. $goods_ids = array_unique(array_column($goods_list, 'goods_id'));
  293. $goods_money = $calculate_data[ 'goods_money' ];//优惠券现在取用的是商品价用作门槛
  294. // $real_goods_money = $calculate_data['real_goods_money'] ?? 0;
  295. $real_goods_money = $goods_money;
  296. $offset = $calculate_data[ 'offset' ] ?? [];
  297. if ($order_money > 0 && $pay_money > 0) {
  298. $coupon_list = [];
  299. //先查询全场通用的优惠券
  300. $member_coupon_model = new Coupon();
  301. $all_condition = array (
  302. [ 'member_id', '=', $member_id ],
  303. [ 'state', '=', 1 ],
  304. [ 'site_id', '=', $site_id ],
  305. [ 'goods_type', '=', 1 ],//全场
  306. // [ 'at_least', '<=', $data[ 'shop_goods_list' ][ 'goods_money' ] ]
  307. );
  308. $all_coupon_list = $member_coupon_model->getCouponList($all_condition)[ 'data' ] ?? [];
  309. //在查询部分商品的优惠券
  310. $item_condition = array (
  311. [ 'member_id', '=', $member_id ],
  312. [ 'state', '=', 1 ],
  313. [ 'site_id', '=', $site_id ],
  314. [ 'goods_type', '=', 2 ],
  315. );
  316. $item_like_array = [];
  317. foreach ($goods_ids as $k => $v) {
  318. $item_like_array[] = '%,' . $v . ',%';
  319. }
  320. $item_condition[] = [ 'goods_ids', 'like', $item_like_array, 'OR' ];
  321. $item_coupon_list = $member_coupon_model->getCouponList($item_condition)[ 'data' ] ?? [];
  322. $member_coupon_list = array_merge($all_coupon_list, $item_coupon_list);
  323. }
  324. $default_coupon_id = 0;
  325. $default_coupon_end_time = 0;
  326. $default_coupon_type = '';
  327. $default_coupon_discount = 0;
  328. $default_coupon_offset_money = 0;
  329. $default_coupon_money = 0;
  330. $temp_member_coupon_list = [];
  331. $temp_cache_coupon = [];//用以减轻I/O压力
  332. if (!empty($member_coupon_list)) {
  333. foreach ($member_coupon_list as $k => $v) {
  334. // $parent_id = $v['coupon_type_id'];
  335. $coupon_id = $v[ 'coupon_id' ];
  336. $at_least = $v[ 'at_least' ];//最小条件
  337. $item_type = $v[ 'type' ];//reward-满减 discount-折扣 random-随机
  338. $goods_type = $v[ 'goods_type' ];
  339. // $temp_cache_coupon_item = $temp_cache_coupon[$parent_id] ?? [];
  340. // if (empty($temp_cache_coupon_item)) {
  341. if ($goods_type == 1) {//全场
  342. $intersect = $goods_ids;
  343. } else {
  344. $item_goods_ids = explode(',', $v[ 'goods_ids' ]);
  345. $intersect = array_intersect($item_goods_ids, $goods_ids);
  346. }
  347. //计算这几个商品的商品总价
  348. $goods_sum = 0;
  349. $coupon_order_goods_ids = array ();
  350. $item_coupon_goods_list = [];
  351. foreach ($goods_list as $goods_k => $goods_v) {
  352. $item_id = $goods_v[ 'goods_id' ];
  353. if (in_array($item_id, $intersect)) {
  354. $goods_sum += $goods_v[ 'real_goods_money' ] ?? 0;//这儿用 商品价还是商品真实价格
  355. $coupon_order_goods_ids[] = $goods_k;
  356. $item_coupon_goods_list[] = $goods_v;
  357. }
  358. }
  359. // $temp_cache_coupon[$parent_id] = ['goods_sum' => $goods_sum, 'coupon_order_goods_ids' => $coupon_order_goods_ids, 'coupon_goods_list' => $item_coupon_goods_list];
  360. // } else {
  361. // $goods_sum = $temp_cache_coupon_item['goods_sum'];
  362. // $coupon_order_goods_ids = $temp_cache_coupon_item['coupon_order_goods_ids'];
  363. // $item_coupon_goods_list = $temp_cache_coupon_item['coupon_goods_list'];
  364. // }
  365. //判断它支持的商品的商品金额够不够最低金额
  366. if ($goods_sum < $at_least) {
  367. //移除会员优惠券
  368. unset($member_coupon_list[ $k ]);
  369. continue;
  370. }
  371. switch ( $item_type ) {
  372. case 'reward'://满减
  373. $item_coupon_money = $v[ 'money' ];
  374. if ($item_coupon_money > $goods_sum) {
  375. $item_coupon_money = $goods_sum;
  376. }
  377. break;
  378. case 'discount'://折扣
  379. $item_discount = $v[ 'discount' ];//折扣
  380. $item_discount_limit = $v[ 'discount_limit' ];//最多抵扣
  381. //计算折扣优惠金额
  382. $item_coupon_money = $goods_sum * ( 10 - $item_discount ) / 10;
  383. $item_coupon_money = $item_coupon_money > $item_discount_limit && $item_discount_limit != 0 ? $item_discount_limit : $item_coupon_money;
  384. $item_coupon_money = $item_coupon_money > $goods_sum ? $goods_sum : $item_coupon_money;
  385. $item_coupon_money = moneyFormat($item_coupon_money);
  386. break;
  387. case 'divideticket'://随机
  388. $item_coupon_money = $v[ 'money' ];
  389. if ($item_coupon_money > $goods_sum) {
  390. $item_coupon_money = $goods_sum;
  391. }
  392. break;
  393. }
  394. $member_coupon_list[ $k ][ 'coupon_goods_money' ] = $goods_sum;
  395. $member_coupon_list[ $k ][ 'coupon_money' ] = $item_coupon_money;
  396. $member_coupon_list[ $k ][ 'coupon_order_goods_ids' ] = $coupon_order_goods_ids;
  397. $member_coupon_list[ $k ][ 'coupon_goods_list' ] = $item_coupon_goods_list;
  398. //一个准则,折扣券不优先用
  399. if ($item_coupon_money > $default_coupon_money) {
  400. $default_coupon_id = $coupon_id;
  401. $default_coupon_end_time = $v[ 'end_time' ];
  402. $default_coupon_type = $item_type;
  403. if ($item_type == 'discount') {
  404. $default_coupon_discount = $v[ 'discount' ];
  405. } else {
  406. $default_coupon_offset_money = $v[ 'money' ];
  407. }
  408. $default_coupon_money = $item_coupon_money;
  409. } else if ($item_coupon_money == $default_coupon_money) {
  410. if ($item_type == 'discount') {
  411. if ($default_coupon_type == $item_type) {
  412. if ($v[ 'discount_limit' ] < $default_coupon_discount) {
  413. $default_coupon_id = $coupon_id;
  414. $default_coupon_end_time = $v[ 'end_time' ];
  415. $default_coupon_type = $item_type;
  416. $default_coupon_discount = $v[ 'discount' ];
  417. } else if ($v[ 'discount_limit' ] == $default_coupon_discount) {
  418. if ($v[ 'end_time' ] < $default_coupon_end_time) {
  419. $default_coupon_id = $coupon_id;
  420. $default_coupon_end_time = $v[ 'end_time' ];
  421. $default_coupon_type = $item_type;
  422. $default_coupon_discount = $v[ 'discount' ];
  423. }
  424. }
  425. }
  426. } else {
  427. if ($default_coupon_type == $item_type) {
  428. if ($v[ 'money' ] < $default_coupon_offset_money) {
  429. $default_coupon_id = $coupon_id;
  430. $default_coupon_end_time = $v[ 'end_time' ];
  431. $default_coupon_type = $item_type;
  432. $default_coupon_discount = $v[ 'money' ];
  433. } else if ($v[ 'money' ] == $default_coupon_offset_money) {
  434. if ($v[ 'end_time' ] < $default_coupon_end_time) {
  435. $default_coupon_id = $coupon_id;
  436. $default_coupon_end_time = $v[ 'end_time' ];
  437. $default_coupon_type = $item_type;
  438. $default_coupon_discount = $v[ 'money' ];
  439. }
  440. }
  441. } else {
  442. $default_coupon_id = $coupon_id;
  443. $default_coupon_end_time = $v[ 'end_time' ];
  444. $default_coupon_type = $item_type;
  445. $default_coupon_offset_money = $v[ 'money' ];
  446. }
  447. }
  448. }
  449. }
  450. $temp_member_coupon_list = array_column($member_coupon_list, null, 'coupon_id');
  451. }
  452. $coupon_id = $calculate_data[ 'coupon_id' ] ?? '';
  453. $coupon_money = 0;
  454. $coupon_order_goods_ids = [];
  455. //计算优惠券优惠
  456. if (!empty($coupon_id)) {
  457. $item_coupon_info = $temp_member_coupon_list[ $coupon_id ] ?? [];
  458. //剔除非法代金券
  459. if (empty($item_coupon_info)) {
  460. $coupon_id = 0;
  461. } else {
  462. $item_coupon_money = $item_coupon_info[ 'coupon_money' ];
  463. $real_goods_money -= $item_coupon_money;
  464. $coupon_money += $item_coupon_money;
  465. $coupon_order_goods_ids = $item_coupon_info[ 'coupon_order_goods_ids' ];
  466. $coupon_goods_list = $item_coupon_info[ 'coupon_goods_list' ];
  467. $coupon_goods_money = $item_coupon_info[ 'coupon_goods_money' ];
  468. if ($item_coupon_money > $coupon_goods_money) {
  469. $item_coupon_money = $coupon_goods_money;
  470. }
  471. $coupon_goods_list = $this->goodsCouponCalculate($coupon_goods_list, $coupon_goods_money, $item_coupon_money);
  472. $coupon_goods_column = array_column($coupon_goods_list, 'null', 'order_goods_id');
  473. foreach ($goods_list as $k => $v) {
  474. if (in_array($v[ 'order_goods_id' ], $coupon_order_goods_ids)) {
  475. $goods_list[ $k ] = $coupon_goods_column[ $v[ 'order_goods_id' ] ];
  476. }
  477. }
  478. }
  479. }
  480. if ($is_can_coupon) {
  481. $coupon_switch = empty($member_coupon_list) ? false : true;
  482. } else {
  483. $coupon_switch = false;
  484. }
  485. $coupon_array = [
  486. 'member_coupon_list' => $member_coupon_list ?? [],
  487. 'coupon_switch' => $coupon_switch
  488. ];
  489. $offset[ 'coupon_array' ] = $coupon_array;
  490. $calculate_data[ 'offset' ] = $offset;
  491. $calculate_data[ 'real_goods_money' ] = $real_goods_money;
  492. $calculate_data[ 'coupon_money' ] = $coupon_money;
  493. $calculate_data[ 'goods_list' ] = $goods_list;
  494. $calculate_data[ 'coupon_id' ] = $coupon_id;
  495. $calculate_data[ 'coupon_order_goods_ids' ] = $coupon_order_goods_ids;
  496. $pay_money -= $coupon_money;
  497. $order_money -= $coupon_money;
  498. $calculate_data[ 'pay_money' ] = $pay_money;
  499. $calculate_data[ 'order_money' ] = $order_money;
  500. }
  501. }
  502. return $this->success($calculate_data);
  503. }
  504. /**
  505. * 按比例摊派优惠券优惠
  506. */
  507. public function goodsCouponCalculate($goods_list, $goods_money, $coupon_money)
  508. {
  509. $temp_coupon_money = $coupon_money;
  510. $last_key = count($goods_list) - 1;
  511. foreach ($goods_list as $k => $v) {
  512. if ($last_key != $k) {
  513. $item_coupon_money = moneyFormat($v[ 'real_goods_money' ] / $goods_money * $coupon_money);
  514. } else {
  515. $item_coupon_money = $temp_coupon_money;
  516. }
  517. $temp_coupon_money -= $item_coupon_money;
  518. $goods_list[ $k ][ 'coupon_money' ] = $item_coupon_money;
  519. $real_goods_money = $v[ 'real_goods_money' ] - $item_coupon_money;
  520. $real_goods_money = $real_goods_money < 0 ? 0 : $real_goods_money;
  521. $goods_list[ $k ][ 'real_goods_money' ] = $real_goods_money; //真实订单项金额
  522. }
  523. return $goods_list;
  524. }
  525. public function roundCalculate($cashier_order_info)
  526. {
  527. $offset = $cashier_order_info[ 'offset' ] ?? [];
  528. $reduction = $cashier_order_info[ 'reduction' ] ?? 0;//调整金额
  529. $order_money = $cashier_order_info[ 'order_money' ];
  530. $promotion_money = $cashier_order_info[ 'promotion_money' ];
  531. $round_type = $cashier_order_info[ 'round_type' ];//收银台 抹零方式 1抹分 2抹角
  532. $pay_money = $cashier_order_info[ 'pay_money' ];
  533. if (!empty($round_type)) {
  534. $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0;
  535. switch ( $round_type ) {
  536. case 1:
  537. $new_order_money = round(intval($order_money * 10) / 10, 2);
  538. break;
  539. case 2:
  540. $new_order_money = round(intval($order_money * 100) / 100, 2);
  541. break;
  542. }
  543. $round_money = $order_money - $new_order_money;
  544. // if ($round_money > $order_money) {
  545. // $round_money = $pay_money;
  546. // }
  547. $order_money -= $round_money;
  548. // if ($round_money > $pay_money) {
  549. // $round_money = $pay_money;
  550. // }
  551. $pay_money -= $round_money;
  552. $offset[ 'round' ] = $round_money;
  553. $cashier_order_info[ 'round_money' ] = $round_money;
  554. $offset_money += $round_money;
  555. $promotion_money += $round_money;
  556. $calculate_data[ 'offset_money' ] = $offset_money;
  557. }
  558. $cashier_order_info[ 'pay_money' ] = $pay_money;
  559. $cashier_order_info[ 'offset' ] = $offset;
  560. $cashier_order_info[ 'promotion_money' ] = $promotion_money;
  561. $cashier_order_info[ 'order_money' ] = $order_money;
  562. return $this->success($cashier_order_info);
  563. }
  564. /**
  565. * 积分计算
  566. * @param $data
  567. */
  568. public function pointCalculate($cashier_order_info)
  569. {
  570. $member_account = $cashier_order_info[ 'member_account' ] ?? [];
  571. if (addon_is_exit('pointcash')) {
  572. if (!empty($member_account)) {
  573. $order_type = $cashier_order_info[ 'cashier_order_type' ];
  574. if (in_array($order_type, [ 'goods', 'card' ])) {
  575. $site_id = $cashier_order_info[ 'site_id' ];
  576. $offset = $cashier_order_info[ 'offset' ] ?? [];
  577. $pay_money = $cashier_order_info[ 'pay_money' ];
  578. $offset_money = $cashier_order_info[ 'offset_money' ] ?? 0;
  579. $order_money = $cashier_order_info[ 'order_money' ];
  580. $site_type = $cashier_order_info[ 'site_type' ] ?? '';
  581. $point = 0;
  582. $point_money = 0;
  583. $use_point_money = 0;
  584. $use_point = 0;
  585. $is_use_point = $cashier_order_info[ 'is_use_point' ] ?? 0;//使用积分
  586. //积分
  587. $point_config_model = new PointConfig();
  588. $point_value = $point_config_model->getPointCashConfig($site_id)[ 'data' ][ 'value' ] ?? [];
  589. $is_use = $point_value[ 'is_enable' ] ?? 0;
  590. if ($is_use > 0) {
  591. $is_limit = $point_value[ 'is_limit' ];
  592. if ($is_limit == 1) {
  593. $limit = $point_value[ 'limit' ];
  594. if ($order_money < $limit) {
  595. return $this->success($cashier_order_info);
  596. }
  597. }
  598. $max_point_money = 0;
  599. if ($point_value[ 'is_limit_use' ] == 1) {
  600. if ($point_value[ 'type' ] == 0) {
  601. $max_point_money = $point_value[ 'max_use' ];
  602. } else {
  603. $ratio = $point_value[ 'max_use' ] / 100;
  604. $max_point_money = round(( $order_money * $ratio ), 2);
  605. }
  606. if ($max_point_money > $order_money) {
  607. $max_point_money = $order_money;
  608. }
  609. }
  610. $point_exchange_rate = $point_value[ 'cash_rate' ] ?? 0;//积分兑换比率 为0的话认为没有开启积分兑换
  611. if ($point_exchange_rate > 0) {
  612. $member_account_point = $member_account[ 'point' ];//会员积分
  613. if ($member_account_point > 0) {//拥有积分大于0
  614. $point_money = round($member_account_point / $point_exchange_rate, 2);//积分抵扣金额
  615. if ($point_money > $pay_money) {
  616. $point_money = $pay_money;
  617. }
  618. if ($max_point_money != 0 && $point_money > $max_point_money) {
  619. $point_money = $max_point_money;
  620. }
  621. $point = ceil($point_money * $point_exchange_rate);
  622. }
  623. }
  624. }
  625. $point_array = array (
  626. 'point' => $point,
  627. 'point_money' => $point_money,
  628. 'point_switch' => false
  629. );
  630. if ($pay_money > 0) {
  631. if ($point > 0) {
  632. $point_array[ 'point_switch' ] = true;
  633. //存在可用积分且选用了积分抵扣
  634. if ($is_use_point) {
  635. $order_money -= $point_money;
  636. $pay_money -= $point_money;
  637. $use_point_money = $point_money;
  638. $use_point = $point;
  639. }
  640. } else {
  641. $is_use_point = 0;
  642. }
  643. } else {
  644. $is_use_point = 0;
  645. }
  646. $offset[ 'point_array' ] = $point_array;
  647. $cashier_order_info[ 'offset' ] = $offset;
  648. $cashier_order_info[ 'order_money' ] = $order_money;
  649. $cashier_order_info[ 'is_use_point' ] = $is_use_point;
  650. $offset_money += $use_point_money;
  651. $cashier_order_info[ 'offset_money' ] = $offset_money;
  652. $cashier_order_info[ 'point_money' ] = $use_point_money;//积分抵扣多少金额
  653. $cashier_order_info[ 'point' ] = $use_point;//使用多少个积分
  654. $cashier_order_info[ 'pay_money' ] = $pay_money;
  655. }
  656. }
  657. }
  658. return $this->success($cashier_order_info);
  659. }
  660. }