CashierOrderCalculate.php 33 KB

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