OrderService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. namespace app\api\service;
  3. use app\common\model\ActivityProductModel;
  4. use app\common\model\AdminModel;
  5. use app\common\model\ConfigModel;
  6. use app\common\model\CreditCardConfigModel;
  7. use app\common\model\CustomerModel;
  8. use app\common\model\CustomerZueCoinModel;
  9. use app\common\model\OrderAnnualFeeModel;
  10. use app\common\model\OrderModel;
  11. use app\common\model\OrderPaymentModel;
  12. use app\common\model\OrderProceedsModel;
  13. use app\common\model\OrderProductModel;
  14. use app\common\model\PaymentChannelModel;
  15. use app\common\model\ProductModel;
  16. use app\common\model\StoreModel;
  17. use app\common\model\StoreProductModel;
  18. use think\facade\Db;
  19. class OrderService extends \app\BaseService
  20. {
  21. private $storeModel;
  22. private $orderModel;
  23. private $productModel;
  24. private $orderAnnualFeeModel;
  25. private $orderPaymentModel;
  26. private $orderProductModel;
  27. private $orderProceedsModel;
  28. private $activityProductModel;
  29. private $storeProductModel;
  30. private $configModel;
  31. private $adminModel;
  32. private $customerModel;
  33. private $paymentChannelModel;
  34. private $creditCardModel;
  35. private $zueCoinModel;
  36. public function __construct()
  37. {
  38. $this->storeModel = new StoreModel();
  39. $this->orderModel = new OrderModel();
  40. $this->productModel = new ProductModel();
  41. $this->configModel = new ConfigModel();
  42. $this->storeProductModel = new StoreProductModel();
  43. $this->orderAnnualFeeModel = new OrderAnnualFeeModel();
  44. $this->orderPaymentModel = new OrderPaymentModel();
  45. $this->orderProductModel = new OrderProductModel();
  46. $this->orderProceedsModel = new OrderProceedsModel();
  47. $this->activityProductModel = new ActivityProductModel();
  48. $this->adminModel = new AdminModel();
  49. $this->customerModel = new CustomerModel();
  50. $this->paymentChannelModel = new PaymentChannelModel();
  51. $this->creditCardModel = new CreditCardConfigModel();
  52. $this->zueCoinModel = new CustomerZueCoinModel();
  53. }
  54. /**
  55. * @param $params
  56. * @param array $store_products_params
  57. * @return \SResult
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function create($params, array $store_products_params) {
  63. // 客户
  64. $customer = $this->customerModel->findById($params['customer_id']);
  65. if (!$customer)
  66. return $this->fail(lang("The Customer does not exist"));
  67. // 门店
  68. $store = $this->storeModel->findById($params['store_id']);
  69. if (!$store)
  70. return $this->fail(lang("The store does not exist"));
  71. // 顾问 or 老师 ids
  72. $user_ids = [];
  73. // 门店商品ids
  74. $store_product_ids = [];
  75. for ($i = 0; $i < count($store_products_params); $i++) {
  76. $store_products_param = $store_products_params[$i];
  77. $ids = [];
  78. if(isset($store_products_param->adviser_1_id)) array_push($ids, $store_products_param->adviser_1_id);
  79. if(isset($store_products_param->adviser_2_id)) array_push($ids, $store_products_param->adviser_2_id);
  80. if(isset($store_products_param->teacher_1_id)) array_push($ids, $store_products_param->teacher_1_id);
  81. if(isset($store_products_param->teacher_2_id)) array_push($ids, $store_products_param->teacher_2_id);
  82. $user_ids = array_unique(array_merge($user_ids, $ids));
  83. array_push($store_product_ids, $store_products_param->store_product_id);
  84. }
  85. // Adviser|Teacher
  86. $users = $this->adminModel->findByIds($user_ids)->toArray();
  87. if(count($users) != count($user_ids))
  88. return $this->fail(lang("The Adviser|Teacher does not exist"));
  89. $fmt_users = array_reduce($users, function ($result, $item) {
  90. $result[$item['id']] = $item;
  91. return $result;
  92. }, []);
  93. // 门店商品
  94. $store_products = $this->storeProductModel->fetchRelationsByOrder($store_product_ids)->toArray();
  95. if(count($store_products) != count($store_product_ids))
  96. return $this->fail(lang("The Product does not exist"));
  97. // 商品信息
  98. $products = array_map(function ($p) {
  99. return $p['product'] ? $p['product'] : null;
  100. }, $store_products);
  101. if(in_array(null, $products))
  102. return $this->fail(lang("The Product does not exist"));
  103. $config = $this->configModel->findConfig();
  104. $activityProducts = $this->activityProductModel->fetchByProductIds(array_map(function ($data) {
  105. return $data['id'];
  106. }, $products))->toArray();
  107. $fmt_store_products = [];
  108. foreach ($store_products as &$item) {
  109. $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
  110. return $aProduct['product_id'] == $item['product_id'];
  111. });
  112. $res = compare($item['product']['real_price'] ?? 0, $aProducts);
  113. $item['activity'] = $res['item'];
  114. $item['product']['origin_price'] = $item['product']['real_price'];
  115. $item['product']['real_price'] = fixed2Float($res['min_num'] > 0 ? $res['min_num'] : 0);
  116. $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
  117. $sales_tax = fixed2Float($sales_tax_rate > 0 && $item['product']['real_price'] > 0 ? ($item['product']['real_price'] * ($sales_tax_rate / 100)) : 0);
  118. $item['product']['sales_tax_rate'] = $sales_tax_rate;
  119. $item['product']['sales_tax'] = $sales_tax;
  120. $fmt_store_products[$item['id']] = $item;
  121. }
  122. $order_products = [];
  123. //项目总额
  124. $rental_amount = 0;
  125. $product_amount = 0;
  126. // 总消费税
  127. $total_sales_tax = 0;
  128. $advisor_ids = [];
  129. $product_names = [];
  130. $total_annual_fee = 0;
  131. $order_no = $this->orderModel->genOrderNo($store->id, $store->abbr);
  132. for ($i = 0; $i < count($store_products_params); $i++) {
  133. $store_products_param = $store_products_params[$i];
  134. $store_product = $fmt_store_products[$store_products_param->store_product_id];
  135. if($store_product['now_stock'] < $store_products_param->quantity)
  136. return $this->fail(lang("inventory shortage"));
  137. array_push($advisor_ids, $store_products_param->adviser_1_id);
  138. if(isset($store_products_param->adviser_2_id)) {
  139. array_push($advisor_ids, $store_products_param->adviser_2_id);
  140. }
  141. for ($j = 0; $j < $store_products_param->quantity; $j++) {
  142. array_push($product_names, $store_product['product']['name']);
  143. array_push($order_products, [
  144. 'order_id' => 0,
  145. 'order_no' => $order_no,
  146. 'customer_id' => $customer->id,
  147. 'product_id' => $store_product['product']['id'],
  148. 'product_category_id' => $store_product['product']['category_id'],
  149. 'is_serve' => $store_product['product']['is_serve'],
  150. 'store_product_id' => $store_product['id'],
  151. 'product_name' => $store_product['product']['name'],
  152. 'adviser_1_id' => isset($store_products_param->adviser_1_id) ? $store_products_param->adviser_1_id : null,
  153. 'adviser_1_name' => isset($store_products_param->adviser_1_id) ? $fmt_users[$store_products_param->adviser_1_id]['nickname'] : null,
  154. 'adviser_2_id' => isset($store_products_param->adviser_2_id) ? $store_products_param->adviser_2_id : null,
  155. 'adviser_2_name' => isset($store_products_param->adviser_2_id) ? $fmt_users[$store_products_param->adviser_2_id]['nickname'] : null,
  156. 'teacher_1_id' => isset($store_products_param->teacher_1_id) ? $store_products_param->teacher_1_id : null,
  157. 'teacher_1_name' => isset($store_products_param->teacher_1_id) ? $fmt_users[$store_products_param->teacher_1_id]['nickname'] : null,
  158. 'teacher_2_id' => isset($store_products_param->teacher_2_id) ? $store_products_param->teacher_2_id : null,
  159. 'teacher_2_name' => isset($store_products_param->teacher_2_id) ? $fmt_users[$store_products_param->teacher_2_id]['nickname'] : null,
  160. 'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
  161. 'is_upload' => 0,
  162. 'report' => null,
  163. 'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
  164. 'annuity' => $store_product['product']['annuity'],
  165. 'real_price' => $store_product['product']['real_price'],
  166. 'service_charge' => 0,
  167. 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
  168. 'reduce_type' => $store_product['activity'] ? $store_product['activity']['type'] : 0,
  169. 'sales_tax_rate' => $store_product['product']['sales_tax_rate'],
  170. 'sales_tax' => $store_product['product']['sales_tax'],
  171. 'transaction_price' => 0,
  172. 'zue_coin' => 0,
  173. 'create_time' => time(),
  174. 'update_time' => time(),
  175. ]);
  176. $product_amount += $store_product['product']['real_price'];
  177. $total_sales_tax += $store_product['product']['sales_tax'];
  178. if($store_product['product']['is_gather_annuity'] == 1) {
  179. $total_annual_fee += $store_product['product']['annuity'];
  180. $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']);
  181. } else {
  182. $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax']);
  183. }
  184. }
  185. }
  186. $order = $this->orderModel->create([
  187. 'no' => $order_no,
  188. 'customer_id' => $customer->id,
  189. 'customer_name' => "{$customer->name_en}|{$customer->name_zh}",
  190. 'obj_names' => join(',',array_unique($product_names)),
  191. 'rental_amount' => $rental_amount,
  192. 'receivable_amount' => $rental_amount,
  193. 'receive_amount' => 0,
  194. 'imposed_amount' => $rental_amount,
  195. 'product_amount' => $product_amount,
  196. 'total_sales_tax' => $total_sales_tax,
  197. 'service_charge_amount' => 0,
  198. 'total_annual_fee' => $total_annual_fee,
  199. 'type' => 2,
  200. 'store_id' => $store->id,
  201. 'advisor_ids' => join(',', $advisor_ids),
  202. 'create_time' => time(),
  203. 'update_time' => time()
  204. ]);
  205. foreach ($order_products as &$order_product) $order_product['order_id'] = $order->id;
  206. Db::startTrans();
  207. try {
  208. Db::table('erp_order_product')->insertAll($order_products);
  209. Db::commit();
  210. } catch (\Exception $e) {
  211. Db::rollback();
  212. Db::table('erp_order')->delete($order->id);
  213. return $this->fail(lang($e->getMessage()));
  214. }
  215. return $this->ok($order);
  216. }
  217. /**
  218. * @param $params
  219. * @param $channels
  220. * @return \SResult
  221. * @throws \app\exception\BaseException
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function payment($params, $channels) {
  227. $order = $this->orderModel->findById($params);
  228. if(!$order)
  229. return $this->fail('订单不存在!');
  230. if($order->type == 1)
  231. return $this->fail('订单状态为历史订单!不支持付款');
  232. // 客户
  233. $customer = $this->customerModel->findById($order->customer_id);
  234. if (!$customer)
  235. return $this->fail(lang("The Customer does not exist"));
  236. $pay_channels = $this->paymentChannelModel->where('is_delete', 0)->select()->toArray();
  237. $fmt_pay_channels = array_reduce($pay_channels, function ($result, $item) {
  238. $result[$item['id']] = $item;
  239. return $result;
  240. },[]);
  241. $credit_card_configs = $this->creditCardModel->findAll()->toArray();
  242. $fmt_credit_card_configs = array_reduce($credit_card_configs, function ($result, $item) {
  243. $result[$item['id']] = $item;
  244. return $result;
  245. },[]);
  246. // 本次总计支付费用
  247. $total_fee = 0;
  248. // 本次信用卡支付手续费
  249. $service_charge = 0;
  250. // 本次御龙币
  251. $zue_coin = 0;
  252. $order_payments = [];
  253. $record_channel_names = [];
  254. for ($i = 0; $i < count($channels); $i++) {
  255. $channel = $channels[$i];
  256. $fmt_pay_channel = $fmt_pay_channels[$channel->channel_id];
  257. if (!$fmt_pay_channel)
  258. return $this->fail("支付渠道错误!");
  259. array_push($record_channel_names, '['.$fmt_pay_channel['name'].']');
  260. $item = [
  261. 'order_id' => $order->id,
  262. 'channel_id' => $channel->channel_id,
  263. 'create_time' => time(),
  264. 'update_time' => time(),
  265. ];
  266. if ($fmt_pay_channel['type'] == 1) { // 正常支付
  267. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  268. $item['fee'] = $channel->fee;
  269. $total_fee += $channel->fee;
  270. if ($fmt_pay_channel['is_upload_code']) {
  271. predicate(isset($channel->code),'必须上传付款编号');
  272. $item['code'] = $channel->code;
  273. }
  274. array_push($order_payments, $item);
  275. } else if($fmt_pay_channel['type'] == 2) { // 御龙币支付
  276. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  277. $item['fee'] = $channel->fee;
  278. // TODO: 御龙币需求未明确
  279. array_push($order_payments, $item);
  280. $zue_coin += $channel->fee;
  281. } else {
  282. for ($j = 0; $j < count($channel->credit_card); $j++) {
  283. $credit_card = $channel->credit_card[$j];
  284. $fmt_credit_card_config = $fmt_credit_card_configs[$credit_card->credit_card_id];
  285. if (!$fmt_credit_card_config)
  286. return $this->fail("信用卡支付渠道错误!");
  287. predicate(isset($credit_card->fee) && $credit_card->fee > 0, '支付费用未填写');
  288. $total_fee += $credit_card->fee;
  289. $el = [
  290. 'order_id' => $order->id,
  291. 'channel_id' => $channel->channel_id,
  292. 'fee' => $credit_card->fee,
  293. 'credit_card_id' => $credit_card->credit_card_id,
  294. 'credit_card_name' => $fmt_credit_card_config['bank'],
  295. 'create_time' => time(),
  296. 'update_time' => time(),
  297. ];
  298. if ($fmt_pay_channel['is_upload_code']) {
  299. predicate(isset($credit_card->code),'必须填写付款编号');
  300. $el['code'] = $credit_card->code;
  301. }
  302. if ($fmt_credit_card_config['is_stage'] == 1) {
  303. predicate(isset($credit_card->stage_num),'必须选择分期数');
  304. $el['stage_num'] = $credit_card->stage_num;
  305. $config = [
  306. '6' => $fmt_credit_card_config['stage_6'],
  307. '9' => $fmt_credit_card_config['stage_9'],
  308. '12' => $fmt_credit_card_config['stage_12'],
  309. '24' => $fmt_credit_card_config['stage_24'],
  310. '36' => $fmt_credit_card_config['stage_36'],
  311. ];
  312. $now_config = $config[$credit_card->stage_num];
  313. predicate($now_config[0] == 1,'该分期未启用!');
  314. if ($now_config[1] > 0) { // 收取手续费
  315. $el['service_charge_rate'] = $now_config[1];
  316. $now_service_charge = $el['fee'] * ($now_config[1] / 100);
  317. $el['service_charge'] = $now_service_charge;
  318. $service_charge += $now_service_charge;
  319. }
  320. }
  321. array_push($order_payments, $el);
  322. }
  323. }
  324. }
  325. // 信用卡手续费
  326. $total_service_charge_amount = ($order->service_charge_amount + $service_charge);
  327. // 总共需要收取这么多费用; (商品的价格 + 商品税费 + 年费 + 信用卡手续费) - 御龙币抵扣的费用
  328. $imposed_amount = fixed2Float($order->product_amount + $order->total_sales_tax + $order->total_annual_fee + $total_service_charge_amount);
  329. $order_use_zue_coin = $order->zue_coin;
  330. $order_use_zue_coin_amount = $order->zue_coin_amount;
  331. $change_zue_coin_record = null;
  332. if($zue_coin > 0) {
  333. $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer->id);
  334. if(!$zue_coin_model)
  335. return $this->fail("当前客户御龙币账户余额: 0");
  336. if ($zue_coin > $zue_coin_model->zue_coin)
  337. return $this->fail("当前客户御龙币账户余额:{$zue_coin_model->zue_coin}");
  338. $zue_coin_config = $fmt_pay_channels['11'];
  339. if(!$zue_coin_config)
  340. return $this->fail("御龙币设置不存在!");
  341. $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
  342. $zue_coin_consume_rate = $zue_coin_config['zue_coin_consume_rate'] ?? 0; // 报销比例
  343. if($zue_coin_exchange_rate == 0 || $zue_coin_consume_rate == 0) {
  344. return $this->fail("该笔订单不支持御龙币支付");
  345. }
  346. // 1.计算出该订单能使用多少御龙币
  347. // 2.结合之前已使用的御龙币 算出还能使用多少御龙币
  348. // 3.判断当前御龙币是否符合兑换数额
  349. // 4.进行扣除并记录
  350. // 总共能报销这么多钱
  351. $total_consume_amount = fixed2Float($imposed_amount * ($zue_coin_consume_rate / 100));
  352. // 总共能消耗这么多御龙币
  353. $total_can_zue_coin = fixed2Float($total_consume_amount / $zue_coin_exchange_rate);
  354. // 还能使用多少御龙币
  355. $now_can_consume_zue_coin = $total_can_zue_coin - $order_use_zue_coin;
  356. // 判断当前还能使用
  357. if ($now_can_consume_zue_coin - $zue_coin < 0)
  358. return $this->fail("当前已使用御龙币数额: {$order_use_zue_coin}/{$total_can_zue_coin},当前已兑换金额: {$order_use_zue_coin_amount}/{$total_consume_amount},御龙币兑换比例: {$zue_coin_exchange_rate},御龙币报销比例:{$zue_coin_consume_rate}");
  359. // 总共使用了这么多御龙币
  360. $order_use_zue_coin += $zue_coin;
  361. // 总共兑换了这么多钱
  362. $order_use_zue_coin_amount = fixed2Float($order_use_zue_coin * $zue_coin_exchange_rate);
  363. $change_zue_coin_record = [
  364. 'zue_coin_id' => $zue_coin_model->id,
  365. 'origin' => $zue_coin_model->zue_coin,
  366. 'change' => $zue_coin,
  367. 'after' => $zue_coin_model->zue_coin - $zue_coin,
  368. 'reason' => 1,
  369. 'create_time' => time(),
  370. 'update_time' => time(),
  371. ];
  372. }
  373. // 总计费用 - 御龙币抵消费用;
  374. $imposed_amount -= $order_use_zue_coin_amount;
  375. // 总计实收金额
  376. $receive_amount = fixed2Float($order->receive_amount + $total_fee);
  377. if (round($receive_amount) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount} (商品总价:{$order->product_amount} + 商品总消费税:{$order->total_sales_tax} + 第一年年费:{$order->total_annual_fee} + 信用卡总手续费:{$total_service_charge_amount})");
  378. $order_annual_fees = [];
  379. $update_store_products = [];
  380. $update_order_products = [];
  381. if (round($receive_amount) == round($imposed_amount)) { // 减库存
  382. $order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
  383. $store_products = $this->storeProductModel->findByIds(array_map(function ($r) {return $r['store_product_id'];}, $order_products))->toArray();
  384. $fmt_store_products = array_reduce($store_products, function ($result, $item) {
  385. $result[$item['id']] = $item;
  386. return $result;
  387. },[]);
  388. // 商品本身的价格 | 年费 | 消费税 | 信用卡支付的服务费 | 御龙币使用的御龙币退还
  389. $total_ = $imposed_amount - ($order->service_charge_amount + $service_charge);
  390. // 每一块钱所占的比例
  391. $item_service_charge = ($order->service_charge_amount + $service_charge) / $total_;
  392. // 每一个御龙币所占的比例
  393. $item_zue_coin = $order_use_zue_coin / $total_;
  394. foreach ($order_products as $order_product) {
  395. predicate($fmt_store_products[$order_product['store_product_id']]['now_stock'] > 0, lang('inventory shortage'));
  396. $fmt_store_products[$order_product['store_product_id']]['now_stock'] -= 1;
  397. if (isset($update_store_products[$order_product['store_product_id']])) {
  398. $update_store_products[$order_product['store_product_id']]['reduce_num'] += 1;
  399. } else {
  400. $update_store_products[$order_product['store_product_id']] = ['id' => $order_product['store_product_id'], 'reduce_num' => 1];
  401. }
  402. if ($order_product['is_gather_annuity']) {
  403. array_push($order_annual_fees, [
  404. 'order_id' => $order->id,
  405. 'customer_id' => $customer->id,
  406. 'customer_name' => "{$customer->name_en}|{$customer->name_zh}",
  407. 'customer_mobile' => $customer->mobile,
  408. 'customer_create_username' => $customer->create_username,
  409. 'index' => 1,
  410. 'order_product_id' => $order_product['id'],
  411. 'order_product_name' => $order_product['product_name'],
  412. 'fee' => $order_product['annuity'],
  413. 'is_pay' => 1,
  414. 'start_time' => time(),
  415. 'end_time' => time() + (365 * 24 * 60 * 60),
  416. 'order_create_time' => strtotime($order->create_time),
  417. 'adviser_1_id' => $order_product['adviser_1_id'],
  418. 'store_id' => $order->store_id,
  419. 'create_time' => time(),
  420. 'update_time' => time()
  421. ]);
  422. }
  423. $now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
  424. array_push($update_order_products, [
  425. 'id' => $order_product['id'],
  426. 'service_charge' => $now * $item_service_charge,
  427. 'zue_coin' => $now * $item_zue_coin,
  428. 'transaction_price' => $now + ($now * $item_service_charge),
  429. 'is_pay' => 1
  430. ]);
  431. }
  432. }
  433. Db::startTrans();
  434. try {
  435. $update_order = [
  436. 'rental_amount' => $imposed_amount + $order_use_zue_coin_amount,
  437. 'receivable_amount' => $imposed_amount,
  438. 'receive_amount' => $receive_amount,
  439. 'imposed_amount' => $imposed_amount - $receive_amount,
  440. 'service_charge_amount' => $order->service_charge_amount + $service_charge,
  441. 'zue_coin' => $order_use_zue_coin,
  442. 'zue_coin_amount' => $order_use_zue_coin_amount,
  443. 'remarks' => $params['remarks']
  444. ];
  445. if (round($receive_amount) == round($imposed_amount)) { // 支付满了
  446. $update_order['type'] = 1;
  447. if(count($update_order_products) > 0) {
  448. foreach ($update_order_products as $item) {
  449. Db::table('erp_order_product')->where('order_id', $item['id'])->update($item);
  450. }
  451. }
  452. if(count($order_annual_fees) > 0) {
  453. Db::table('erp_order_annual_fee')->insertAll($order_annual_fees);
  454. }
  455. if(count($update_store_products) > 0) {
  456. foreach ($update_store_products as $update) {
  457. Db::table('erp_store_product')->where('id', $update['id'])->dec('now_stock',1)->update();
  458. Db::table('erp_store_product')->where('id', $update['id'])->inc('sale_stock',1)->update();
  459. }
  460. }
  461. }
  462. Db::table('erp_order_proceeds')->save([
  463. 'order_id' => $order->id,
  464. 'channels' => join(' ',$record_channel_names),
  465. 'fee' => $total_fee,
  466. 'admin_id' => $params['admin_id'],
  467. 'admin_name' => $params['admin_name'],
  468. 'create_time' => time(),
  469. 'update_time' => time(),
  470. ]);
  471. if ($zue_coin > 0 && $change_zue_coin_record != null) {
  472. Db::table('erp_zue_coin_record')->save($change_zue_coin_record);
  473. Db::table('erp_customer_zue_coin')->where('id', $change_zue_coin_record['zue_coin_id'])->dec('zue_coin', $zue_coin)->update();
  474. }
  475. if(count($order_payments) > 0) {
  476. Db::table('erp_order_payment')->insertAll(array_map(function ($data) {
  477. return [
  478. "order_id" => isset($data['order_id']) ? $data['order_id'] : null,
  479. "channel_id" => isset($data['channel_id']) ? $data['channel_id'] : null,
  480. "fee" => isset($data['fee']) ? $data['fee'] : null,
  481. "credit_card_id" => isset($data['credit_card_id']) ? $data['credit_card_id'] : null,
  482. "credit_card_name" => isset($data['credit_card_name']) ? $data['credit_card_name'] : null,
  483. "create_time" => isset($data['create_time']) ? $data['create_time'] : null,
  484. "update_time" => isset($data['update_time']) ? $data['update_time'] : null,
  485. "code" => isset($data['code']) ? $data['code'] : null,
  486. "stage_num" => isset($data['stage_num']) ? $data['stage_num'] : null,
  487. "service_charge_rate" => isset($data['service_charge_rate']) ? $data['service_charge_rate'] : null,
  488. "service_charge" => isset($data['service_charge']) ? $data['service_charge'] : null
  489. ];
  490. },$order_payments));
  491. }
  492. Db::table('erp_order')->where('id', $order->id)->update($update_order);
  493. Db::commit();
  494. } catch (\Exception $e) {
  495. Db::rollback();
  496. return $this->fail(lang($e->getMessage()));
  497. }
  498. return $this->ok(true);
  499. }
  500. public function fetch($params) {
  501. $is_look_all = AuthService::is_look_all($params['admin_id'],$params['type'] == 1 ? 10003 : 10004);
  502. $res = $this->orderModel->search($params, $is_look_all);
  503. $items = $res['data'];
  504. foreach ($items as &$item) {
  505. $item['is_need_upload'] = $item['type'] == 1 && in_array(true, array_map(function ($product) {
  506. return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
  507. }, $item['products']));
  508. }
  509. return [$items, $res['total']];
  510. }
  511. /**
  512. * @param $params
  513. * @return array
  514. * @throws \think\db\exception\DbException
  515. */
  516. public function fetchByCustomerId($params) {
  517. $res = $this->orderModel->fetchByCustomerId($params['customer_id'], $params['page'] ?? 1, $params['size'] ?? 20);
  518. $items = $res['data'];
  519. foreach ($items as &$item) {
  520. $item['is_need_upload'] = $item['type'] == 1 && in_array(true, array_map(function ($product) {
  521. return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
  522. }, $item['products']));
  523. }
  524. return [$items, $res['total']];
  525. }
  526. public function upload_numerology($order_product_id, $report) {
  527. $order_product = $this->orderProductModel->findById($order_product_id);
  528. if (!$order_product) return $this->fail('订单商品不存在,无法上传命理报告!');
  529. if (!$order_product->is_pay) return $this->fail('订单未支付,无法上传商品');
  530. $order_product->is_upload = 1;
  531. $order_product->report = $report;
  532. $order_product->save();
  533. return $this->ok($order_product);
  534. }
  535. public function fetchWaitGatherAnnualFee($admin_id, $text = null, $start_time = null, $end_time = null, $page = 1, $size = 10) {
  536. $res = $this->orderAnnualFeeModel->fetchByAdviser($admin_id, $text, $start_time, $end_time, $page, $size);
  537. return [$res->items(),$res->total()];
  538. }
  539. public function paymentAnnualFee($annual_fee_id) {
  540. $annual_fee = $this->orderAnnualFeeModel->findById($annual_fee_id);
  541. if(!$annual_fee_id) {
  542. return $this->fail('年费记录不存在!');
  543. }
  544. if($annual_fee->is_pay == 1)
  545. return $this->fail('该条年费记录已经支付!');
  546. $annual_fee->is_pay = 1;
  547. $annual_fee->update_time = time();
  548. $annual_fee->save();
  549. return $this->ok($annual_fee);
  550. }
  551. }