OrderService.php 27 KB

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