OrderService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\OrderAnnualFeeModel;
  9. use app\common\model\OrderModel;
  10. use app\common\model\OrderPaymentModel;
  11. use app\common\model\OrderProceedsModel;
  12. use app\common\model\OrderProductModel;
  13. use app\common\model\PaymentChannelModel;
  14. use app\common\model\ProductModel;
  15. use app\common\model\StoreModel;
  16. use app\common\model\StoreProductModel;
  17. use think\facade\Db;
  18. class OrderService extends \app\BaseService
  19. {
  20. private $storeModel;
  21. private $orderModel;
  22. private $productModel;
  23. private $orderAnnualFeeModel;
  24. private $orderPaymentModel;
  25. private $orderProductModel;
  26. private $orderProceedsModel;
  27. private $activityProductModel;
  28. private $storeProductModel;
  29. private $configModel;
  30. private $adminModel;
  31. private $customerModel;
  32. private $paymentChannelModel;
  33. private $creditCardModel;
  34. public function __construct()
  35. {
  36. $this->storeModel = new StoreModel();
  37. $this->orderModel = new OrderModel();
  38. $this->productModel = new ProductModel();
  39. $this->configModel = new ConfigModel();
  40. $this->storeProductModel = new StoreProductModel();
  41. $this->orderAnnualFeeModel = new OrderAnnualFeeModel();
  42. $this->orderPaymentModel = new OrderPaymentModel();
  43. $this->orderProductModel = new OrderProductModel();
  44. $this->orderProceedsModel = new OrderProceedsModel();
  45. $this->activityProductModel = new ActivityProductModel();
  46. $this->adminModel = new AdminModel();
  47. $this->customerModel = new CustomerModel();
  48. $this->paymentChannelModel = new PaymentChannelModel();
  49. $this->creditCardModel = new CreditCardConfigModel();
  50. }
  51. /**
  52. * @param $params
  53. * @param array $store_products_params
  54. * @return \SResult
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function create($params, array $store_products_params) {
  60. // 客户
  61. $customer = $this->customerModel->findById($params['customer_id']);
  62. if (!$customer)
  63. return $this->fail(lang("The Customer does not exist"));
  64. // 门店
  65. $store = $this->storeModel->findById($params['store_id']);
  66. if (!$store)
  67. return $this->fail(lang("The store does not exist"));
  68. // 顾问 or 老师 ids
  69. $user_ids = [];
  70. // 门店商品ids
  71. $store_product_ids = [];
  72. for ($i = 0; $i < count($store_products_params); $i++) {
  73. $store_products_param = $store_products_params[$i];
  74. $ids = [];
  75. if(isset($store_products_param->adviser_1_id)) array_push($ids, $store_products_param->adviser_1_id);
  76. if(isset($store_products_param->adviser_2_id)) array_push($ids, $store_products_param->adviser_2_id);
  77. if(isset($store_products_param->teacher_id)) array_push($ids, $store_products_param->teacher_id);
  78. $user_ids = array_unique(array_merge($user_ids, $ids));
  79. array_push($store_product_ids, $store_products_param->store_product_id);
  80. }
  81. // Adviser|Teacher
  82. $users = $this->adminModel->findByIds($user_ids)->toArray();
  83. if(count($users) != count($user_ids))
  84. return $this->fail(lang("The Adviser|Teacher does not exist"));
  85. $fmt_users = array_reduce($users, function ($result, $item) {
  86. $result[$item['id']] = $item;
  87. return $result;
  88. }, []);
  89. // 门店商品
  90. $store_products = $this->storeProductModel->fetchRelationsByOrder($store_product_ids)->toArray();
  91. if(count($store_products) != count($store_product_ids))
  92. return $this->fail(lang("The Product does not exist"));
  93. // 商品信息
  94. $products = array_map(function ($p) {
  95. return $p['product'] ? $p['product'] : null;
  96. }, $store_products);
  97. if(in_array(null, $products))
  98. return $this->fail(lang("The Product does not exist"));
  99. $config = $this->configModel->findConfig();
  100. $activityProducts = $this->activityProductModel->fetchByProductIds(array_map(function ($data) {
  101. return $data['id'];
  102. }, $products))->toArray();
  103. $fmt_store_products = [];
  104. foreach ($store_products as &$item) {
  105. $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
  106. return $aProduct['product_id'] == $item['product_id'];
  107. });
  108. $res = compare($item['product']['real_price'] ?? 0, $aProducts);
  109. $item['activity'] = $res['item'];
  110. $item['product']['origin_price'] = $item['product']['real_price'];
  111. $item['product']['real_price'] = fixed2Float($res['min_num'] > 0 ? $res['min_num'] : 0);
  112. $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
  113. $sales_tax = fixed2Float($sales_tax_rate > 0 && $item['product']['real_price'] > 0 ? ($item['product']['real_price'] * ($sales_tax_rate / 100)) : 0);
  114. $item['product']['sales_tax_rate'] = $sales_tax_rate;
  115. $item['product']['sales_tax'] = $sales_tax;
  116. $fmt_store_products[$item['id']] = $item;
  117. }
  118. $order_products = [];
  119. //项目总额
  120. $rental_amount = 0;
  121. // 总消费税
  122. $total_sales_tax = 0;
  123. $advisor_1_ids = [];
  124. for ($i = 0; $i < count($store_products_params); $i++) {
  125. $store_products_param = $store_products_params[$i];
  126. $store_product = $fmt_store_products[$store_products_param->store_product_id];
  127. array_push($advisor_1_ids, $store_products_param->adviser_1_id);
  128. for ($j = 0; $j < $store_products_param->quantity; $j++) {
  129. array_push($order_products, [
  130. 'order_id' => 0,
  131. 'product_id' => $store_product['product']['id'],
  132. 'product_name' => $store_product['product']['name'],
  133. 'adviser_1_id' => isset($store_products_param->adviser_1_id) ?? null,
  134. 'adviser_1_name' => isset($store_products_param->adviser_1_id) ? $fmt_users[$store_products_param->adviser_1_id]['nickname'] : null,
  135. 'adviser_2_id' => isset($store_products_param->adviser_2_id) ?? null,
  136. 'adviser_2_name' => isset($store_products_param->adviser_2_id) ? $fmt_users[$store_products_param->adviser_2_id]['nickname'] : null,
  137. 'teacher_id' => isset($store_products_param->teacher_id) ?? null,
  138. 'teacher_name' => isset($store_products_param->teacher_id) ? $fmt_users[$store_products_param->teacher_id]['nickname'] : null,
  139. 'report' => null,
  140. 'real_price' => $store_product['product']['real_price'],
  141. 'service_charge' => 0,
  142. 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
  143. 'reduce_type' => $store_product['activity'] ? $store_product['activity']['type'] : 0,
  144. 'sales_tax_rate' => $store_product['product']['sales_tax_rate'],
  145. 'sales_tax' => $store_product['product']['sales_tax'],
  146. 'create_time' => time(),
  147. 'update_time' => time(),
  148. ]);
  149. $rental_amount += $store_product['product']['real_price'];
  150. $total_sales_tax += $store_product['product']['sales_tax'];
  151. }
  152. }
  153. $order = $this->orderModel->create([
  154. 'no' => $this->orderModel->genOrderNo($store->id, $store->abbr),
  155. 'customer_id' => $customer->id,
  156. 'customer_name' => $customer->name_en,
  157. 'rental_amount' => $rental_amount,
  158. 'receivable_amount' => $rental_amount,
  159. 'receive_amount' => 0,
  160. 'imposed_amount' => $rental_amount,
  161. 'total_sales_tax' => $total_sales_tax,
  162. 'type' => 2,
  163. 'store_id' => $store->id,
  164. 'advisor_ids' => join(',', $advisor_1_ids),
  165. 'create_time' => time(),
  166. 'update_time' => time()
  167. ]);
  168. foreach ($order_products as &$order_product) $order_product['order_id'] = $order->id;
  169. Db::startTrans();
  170. try {
  171. Db::table('erp_order_product')->insertAll($order_products);
  172. Db::commit();
  173. } catch (\Exception $e) {
  174. Db::rollback();
  175. Db::table('erp_order')->delete($order->id);
  176. return $this->fail(lang($e->getMessage()));
  177. }
  178. return $this->ok($order);
  179. }
  180. /**
  181. * @param $params
  182. * @param $channels
  183. * @return \SResult
  184. * @throws \app\exception\BaseException
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function payment($params, $channels) {
  190. $order = $this->orderModel->findById($params);
  191. if(!$order)
  192. return $this->fail('订单不存在!');
  193. if($order->type == 1)
  194. return $this->fail('订单状态为历史订单!不支持付款');
  195. $pay_channels = $this->paymentChannelModel->where('is_delete', 0)->select()->toArray();
  196. $fmt_pay_channels = array_reduce($pay_channels, function ($result, $item) {
  197. $result[$item['id']] = $item;
  198. return $result;
  199. },[]);
  200. $credit_card_configs = $this->creditCardModel->findAll()->toArray();
  201. $fmt_credit_card_configs = array_reduce($credit_card_configs, function ($result, $item) {
  202. $result[$item['id']] = $item;
  203. return $result;
  204. },[]);
  205. // 总计支付费用
  206. $total_fee = 0;
  207. // 信用卡支付手续费
  208. $service_charge = 0;
  209. // 御龙币
  210. $zue_coin = 0;
  211. $order_payments = [];
  212. $record_channel_names = [];
  213. for ($i = 0; $i < count($channels); $i++) {
  214. $channel = $channels[$i];
  215. $fmt_pay_channel = $fmt_pay_channels[$channel->channel_id];
  216. if (!$fmt_pay_channel)
  217. return $this->fail("支付渠道错误!");
  218. array_push($record_channel_names, '['.$fmt_pay_channel['name'].']');
  219. $item = [
  220. 'order_id' => $order->id,
  221. 'channel_id' => $channel->channel_id,
  222. 'create_time' => time(),
  223. 'update_time' => time(),
  224. ];
  225. if ($fmt_pay_channel['type'] == 1) { // 正常支付
  226. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  227. $item['fee'] = $channel->fee;
  228. $total_fee += $channel->fee;
  229. if ($fmt_pay_channel['is_upload_code']) {
  230. predicate(isset($channel->code),'必须上传付款编号');
  231. $item['code'] = $channel->code;
  232. }
  233. array_push($order_payments, $item);
  234. } else if($fmt_pay_channel['type'] == 2) { // 御龙币支付
  235. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  236. $item['fee'] = $channel->fee;
  237. // TODO: 御龙币需求未明确
  238. array_push($order_payments, $item);
  239. } else {
  240. for ($j = 0; $j < count($channel->credit_card); $j++) {
  241. $credit_card = $channel->credit_card[$j];
  242. $fmt_credit_card_config = $fmt_credit_card_configs[$credit_card->credit_card_id];
  243. if (!$fmt_credit_card_config)
  244. return $this->fail("信用卡支付渠道错误!");
  245. predicate(isset($credit_card->fee) && $credit_card->fee > 0, '支付费用未填写');
  246. $total_fee += $credit_card->fee;
  247. $el = [
  248. 'order_id' => $order->id,
  249. 'channel_id' => $channel->channel_id,
  250. 'fee' => $credit_card->fee,
  251. 'credit_card_id' => $credit_card->credit_card_id,
  252. 'credit_card_name' => $fmt_credit_card_config['bank'],
  253. 'create_time' => time(),
  254. 'update_time' => time(),
  255. ];
  256. if ($fmt_pay_channel['is_upload_code']) {
  257. predicate(isset($credit_card->code),'必须填写付款编号');
  258. $el['code'] = $credit_card->code;
  259. }
  260. if ($fmt_credit_card_config['is_stage'] == 1) {
  261. predicate(isset($credit_card->stage_num),'必须选择分期数');
  262. $el['stage_num'] = $credit_card->stage_num;
  263. $config = [
  264. '6' => $fmt_credit_card_config['stage_6'],
  265. '9' => $fmt_credit_card_config['stage_9'],
  266. '12' => $fmt_credit_card_config['stage_12'],
  267. '24' => $fmt_credit_card_config['stage_24'],
  268. '36' => $fmt_credit_card_config['stage_36'],
  269. ];
  270. $now_config = $config[$credit_card->stage_num];
  271. predicate($now_config[0] == 1,'该分期未启用!');
  272. if ($now_config[1] > 0) { // 收取手续费
  273. $el['service_charge_rate'] = $now_config[1];
  274. $now_service_charge = $el['fee'] * ($now_config[1] / 100);
  275. $el['service_charge'] = $now_service_charge;
  276. $service_charge += $now_service_charge;
  277. }
  278. }
  279. array_push($order_payments, $el);
  280. }
  281. }
  282. }
  283. // 总共需要收取这么多费用;
  284. $imposed_amount = fixed2Float($order->imposed_amount + $service_charge + $order->total_sales_tax);
  285. $total_fee = fixed2Float($total_fee);
  286. if (round($total_fee) > round($imposed_amount)) return $this->fail("实收金额大于待收金额! 应该支付 {$imposed_amount}");
  287. return $this->ok($total_fee);
  288. Db::startTrans();
  289. try {
  290. Db::table('erp_order_payment')->insertAll($order_payments);
  291. Db::table('erp_order_proceeds')->save([
  292. 'order_id' => $order->id,
  293. 'channels' => join(' ',$record_channel_names),
  294. 'fee' => $total_fee,
  295. 'admin_id' => $params['admin_id'],
  296. 'admin_name' => $params['admin_name'],
  297. 'create_time' => time(),
  298. 'update_time' => time(),
  299. ]);
  300. $update_order = [
  301. 'service_charge_amount' => $service_charge,
  302. 'receive_amount' => $order->receive_amount + $total_fee,
  303. 'imposed_amount' => $order->imposed_amount - $total_fee,
  304. ];
  305. if (round($total_fee) < round($imposed_amount)) { // 未支付满
  306. // DOTO:
  307. } else {
  308. $update_order['type'] = 2;
  309. }
  310. Db::table('erp_order')->where('id',$order->id)->update($update_order);
  311. Db::commit();
  312. } catch (\Exception $e) {
  313. Db::rollback();
  314. return $this->fail(lang($e->getMessage()));
  315. }
  316. return $this->ok(true);
  317. }
  318. }