OrderService.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. <?php
  2. namespace app\api\service;
  3. use app\api\controller\Product;
  4. use app\common\model\ActivityProductModel;
  5. use app\common\model\AdminModel;
  6. use app\common\model\ConfigModel;
  7. use app\common\model\CreditCardConfigModel;
  8. use app\common\model\CustomerModel;
  9. use app\common\model\CustomerZueCoinModel;
  10. use app\common\model\OrderAnnualFeeModel;
  11. use app\common\model\OrderModel;
  12. use app\common\model\OrderPaymentModel;
  13. use app\common\model\OrderProceedsModel;
  14. use app\common\model\OrderProductModel;
  15. use app\common\model\PaymentChannelModel;
  16. use app\common\model\ProductModel;
  17. use app\common\model\StoreModel;
  18. use app\common\model\StoreProductModel;
  19. use app\common\model\ZueCoinRecordModel;
  20. use app\exception\BaseException;
  21. use think\facade\Db;
  22. class OrderService extends \app\BaseService
  23. {
  24. private $storeModel;
  25. private $orderModel;
  26. private $productModel;
  27. private $orderAnnualFeeModel;
  28. private $orderPaymentModel;
  29. private $orderProductModel;
  30. private $orderProceedsModel;
  31. private $activityProductModel;
  32. private $storeProductModel;
  33. private $configModel;
  34. private $adminModel;
  35. private $customerModel;
  36. private $paymentChannelModel;
  37. private $creditCardModel;
  38. private $zueCoinModel;
  39. private $zueCoinRecordModel;
  40. public function __construct()
  41. {
  42. $this->storeModel = new StoreModel();
  43. $this->orderModel = new OrderModel();
  44. $this->productModel = new ProductModel();
  45. $this->configModel = new ConfigModel();
  46. $this->storeProductModel = new StoreProductModel();
  47. $this->orderAnnualFeeModel = new OrderAnnualFeeModel();
  48. $this->orderPaymentModel = new OrderPaymentModel();
  49. $this->orderProductModel = new OrderProductModel();
  50. $this->orderProceedsModel = new OrderProceedsModel();
  51. $this->activityProductModel = new ActivityProductModel();
  52. $this->adminModel = new AdminModel();
  53. $this->customerModel = new CustomerModel();
  54. $this->paymentChannelModel = new PaymentChannelModel();
  55. $this->creditCardModel = new CreditCardConfigModel();
  56. $this->zueCoinModel = new CustomerZueCoinModel();
  57. $this->zueCoinRecordModel = new ZueCoinRecordModel();
  58. }
  59. // 计算单个商品消费税
  60. private function calculate_sales_tax($product_real_price, $product_sales_tax_rate = 0)
  61. {
  62. $config = $this->configModel->findConfig();
  63. $sales_tax_rate = $config ? $config->sales_tax_rate : 0;
  64. $sales_tax_rate = $product_sales_tax_rate > 0 ? $product_sales_tax_rate : $sales_tax_rate;
  65. if ($product_sales_tax_rate == 0 || $product_real_price == 0) {
  66. return ['sales_tax_rate' => 0, 'sales_tax' => 0];
  67. }
  68. return [
  69. 'sales_tax_rate' => $sales_tax_rate,
  70. 'sales_tax' => fixed2Float($product_real_price * ($sales_tax_rate / 100))
  71. ];
  72. }
  73. // 计算单个商品总费用
  74. private function calculate_product_amount()
  75. {
  76. }
  77. // 生成单个订单商品
  78. private function genOrderProduct($product, $order_product_id = null, $order = null, $order_no = null)
  79. {
  80. }
  81. /**
  82. * @param $params
  83. * @param array $store_products_params
  84. * @return \SResult
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function create($params, array $store_products_params)
  90. {
  91. // 客户
  92. $customer = $this->customerModel->findById($params['customer_id']);
  93. if (!$customer)
  94. return $this->fail(lang("The Customer does not exist"));
  95. // 门店
  96. $store = $this->storeModel->findById($params['store_id']);
  97. if (!$store)
  98. return $this->fail(lang("The store does not exist"));
  99. // 顾问 or 老师 ids
  100. $user_ids = [];
  101. // 门店商品ids
  102. $store_product_ids = [];
  103. for ($i = 0; $i < count($store_products_params); $i++) {
  104. $store_products_param = $store_products_params[$i];
  105. $ids = [];
  106. if (isset($store_products_param->adviser_1_id)) array_push($ids, $store_products_param->adviser_1_id);
  107. if (isset($store_products_param->adviser_2_id)) array_push($ids, $store_products_param->adviser_2_id);
  108. if (isset($store_products_param->teacher_1_id)) array_push($ids, $store_products_param->teacher_1_id);
  109. if (isset($store_products_param->teacher_2_id)) array_push($ids, $store_products_param->teacher_2_id);
  110. $user_ids = array_unique(array_merge($user_ids, $ids));
  111. array_push($store_product_ids, $store_products_param->store_product_id);
  112. }
  113. // Adviser|Teacher
  114. $users = $this->adminModel->findByIds($user_ids)->toArray();
  115. if (count($users) != count($user_ids))
  116. return $this->fail(lang("The Adviser|Teacher does not exist"));
  117. $fmt_users = array_reduce($users, function ($result, $item) {
  118. $result[$item['id']] = $item;
  119. return $result;
  120. }, []);
  121. // 门店商品
  122. $store_products = $this->storeProductModel->fetchRelationsByOrder($store_product_ids)->toArray();
  123. if (count($store_products) != count($store_product_ids))
  124. return $this->fail(lang("The Product does not exist"));
  125. // 商品信息
  126. $products = array_map(function ($p) {
  127. return $p['product'] ? $p['product'] : null;
  128. }, $store_products);
  129. if (in_array(null, $products))
  130. return $this->fail(lang("The Product does not exist"));
  131. $activityProducts = $this->activityProductModel->fetchByProductIds(array_map(function ($data) {
  132. return $data['id'];
  133. }, $products))->toArray();
  134. $fmt_store_products = [];
  135. foreach ($store_products as &$item) {
  136. $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
  137. return $aProduct['product_id'] == $item['product_id'];
  138. });
  139. $res = compare($item['product']['real_price'] ?? 0, $aProducts);
  140. $item['activity'] = $res['item'];
  141. $item['product']['origin_price'] = $item['product']['real_price'];
  142. $item['product']['real_price'] = $res['min_num'];
  143. $tax = $this->calculate_sales_tax($item['product']['real_price'], $item['product']['sales_tax_rate']);
  144. $item['product']['sales_tax_rate'] = $tax['sales_tax_rate'];
  145. $item['product']['sales_tax'] = $tax['sales_tax'];
  146. $fmt_store_products[$item['id']] = $item;
  147. }
  148. $order_products = [];
  149. //项目总额
  150. $rental_amount = 0;
  151. $product_amount = 0;
  152. // 总消费税
  153. $total_sales_tax = 0;
  154. $advisor_ids = [];
  155. $product_names = [];
  156. $total_annual_fee = 0;
  157. $order_no = $this->orderModel->genOrderNo($store->id, $store->abbr);
  158. for ($i = 0; $i < count($store_products_params); $i++) {
  159. $store_products_param = $store_products_params[$i];
  160. $store_product = $fmt_store_products[$store_products_param->store_product_id];
  161. if ($store_product['now_stock'] < $store_products_param->quantity)
  162. return $this->fail(lang("inventory shortage"));
  163. array_push($advisor_ids, $store_products_param->adviser_1_id);
  164. if (isset($store_products_param->adviser_2_id)) {
  165. array_push($advisor_ids, $store_products_param->adviser_2_id);
  166. }
  167. for ($j = 0; $j < $store_products_param->quantity; $j++) {
  168. array_push($product_names, $store_product['product']['name']);
  169. array_push($order_products, [
  170. 'order_id' => 0,
  171. 'order_no' => $order_no,
  172. 'customer_id' => $customer->id,
  173. 'product_id' => $store_product['product']['id'],
  174. 'product_category_id' => $store_product['product']['category_id'],
  175. 'is_serve' => $store_product['product']['is_serve'],
  176. 'store_product_id' => $store_product['id'],
  177. 'product_name' => $store_product['product']['name'],
  178. 'adviser_1_id' => isset($store_products_param->adviser_1_id) ? $store_products_param->adviser_1_id : null,
  179. 'adviser_1_name' => isset($store_products_param->adviser_1_id) ? $fmt_users[$store_products_param->adviser_1_id]['nickname'] : null,
  180. 'adviser_2_id' => isset($store_products_param->adviser_2_id) ? $store_products_param->adviser_2_id : null,
  181. 'adviser_2_name' => isset($store_products_param->adviser_2_id) ? $fmt_users[$store_products_param->adviser_2_id]['nickname'] : null,
  182. 'teacher_1_id' => isset($store_products_param->teacher_1_id) ? $store_products_param->teacher_1_id : null,
  183. 'teacher_1_name' => isset($store_products_param->teacher_1_id) ? $fmt_users[$store_products_param->teacher_1_id]['nickname'] : null,
  184. 'teacher_2_id' => isset($store_products_param->teacher_2_id) ? $store_products_param->teacher_2_id : null,
  185. 'teacher_2_name' => isset($store_products_param->teacher_2_id) ? $fmt_users[$store_products_param->teacher_2_id]['nickname'] : null,
  186. 'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
  187. 'is_upload' => 0,
  188. 'report' => null,
  189. 'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
  190. 'annuity' => $store_product['product']['annuity'],
  191. 'real_price' => $store_product['product']['real_price'],
  192. 'service_charge' => 0,
  193. 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
  194. 'reduce_type' => $store_product['activity'] ? $store_product['activity']['type'] : 0,
  195. 'sales_tax_rate' => $store_product['product']['sales_tax_rate'],
  196. 'sales_tax' => $store_product['product']['sales_tax'],
  197. 'transaction_price' => fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']),
  198. 'zue_coin' => 0,
  199. 'create_time' => time(),
  200. 'update_time' => time(),
  201. ]);
  202. $product_amount += $store_product['product']['real_price'];
  203. $total_sales_tax += $store_product['product']['sales_tax'];
  204. if ($store_product['product']['is_gather_annuity'] == 1) {
  205. $total_annual_fee += $store_product['product']['annuity'];
  206. $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']);
  207. } else {
  208. $rental_amount += ($store_product['product']['real_price'] + $store_product['product']['sales_tax']);
  209. }
  210. }
  211. }
  212. $order = $this->orderModel->create([
  213. 'no' => $order_no,
  214. 'customer_id' => $customer->id,
  215. 'customer_name' => "{$customer->name_en}|{$customer->name_zh}",
  216. 'obj_names' => join(',', array_unique($product_names)),
  217. 'rental_amount' => $rental_amount,
  218. 'receivable_amount' => $rental_amount,
  219. 'receive_amount' => 0,
  220. 'imposed_amount' => $rental_amount,
  221. 'product_amount' => $product_amount,
  222. 'total_sales_tax' => $total_sales_tax,
  223. 'service_charge_amount' => 0,
  224. 'total_annual_fee' => $total_annual_fee,
  225. 'type' => 2,
  226. 'store_id' => $store->id,
  227. 'advisor_ids' => join(',', array_unique($advisor_ids)),
  228. 'create_time' => time(),
  229. 'update_time' => time()
  230. ]);
  231. foreach ($order_products as &$order_product) $order_product['order_id'] = $order->id;
  232. Db::startTrans();
  233. try {
  234. Db::table('erp_order_product')->insertAll($order_products);
  235. Db::commit();
  236. } catch (\Exception $e) {
  237. Db::rollback();
  238. Db::table('erp_order')->delete($order->id);
  239. return $this->fail(lang($e->getMessage()));
  240. }
  241. return $this->ok($order);
  242. }
  243. private function fetch_fmt_pay_channels()
  244. {
  245. $pay_channels = $this->paymentChannelModel->where('is_delete', 0)->select()->toArray();
  246. return array_reduce($pay_channels, function ($result, $item) {
  247. $result[$item['id']] = $item;
  248. return $result;
  249. }, []);
  250. }
  251. private function fetch_fmt_credit_card_configs()
  252. {
  253. $credit_card_configs = $this->creditCardModel->findAll()->toArray();
  254. return array_reduce($credit_card_configs, function ($result, $item) {
  255. $result[$item['id']] = $item;
  256. return $result;
  257. }, []);
  258. }
  259. private function findCustomerZueCoin($customer_id)
  260. {
  261. $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer_id);
  262. return $zue_coin_model ? $zue_coin_model->zue_coin : 0;
  263. }
  264. /**
  265. * @param $params
  266. * @param $channels
  267. * @return \SResult
  268. * @throws \app\exception\BaseException
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\DbException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. */
  273. public function payment($params, $channels)
  274. {
  275. $order = $this->orderModel->findById($params['order_id']);
  276. if (!$order)
  277. return $this->fail(lang("Order does not exist"));
  278. if ($order->imposed_amount <= 0)
  279. return $this->fail('订单已支付,请勿重复付款!');
  280. // 客户
  281. $customer = $this->customerModel->findById($order->customer_id);
  282. if (!$customer)
  283. return $this->fail(lang("The Customer does not exist"));
  284. $fmt_pay_channels = $this->fetch_fmt_pay_channels();
  285. $fmt_credit_card_configs = $this->fetch_fmt_credit_card_configs();
  286. // 本次总计支付费用
  287. $total_fee = 0;
  288. // 本次信用卡支付手续费
  289. $service_charge = 0;
  290. // 本次御龙币
  291. $zue_coin = 0;
  292. $order_payments = [];
  293. $record_channel_names = [];
  294. $channel_ids = array_map(function ($data) {
  295. return $data['channel_id'];
  296. }, $this->orderPaymentModel->findByOrderId($order->id)->toArray());
  297. for ($i = 0; $i < count($channels); $i++) {
  298. $channel = $channels[$i];
  299. $fmt_pay_channel = $fmt_pay_channels[$channel->channel_id];
  300. if (!$fmt_pay_channel)
  301. return $this->fail("支付渠道错误!");
  302. array_push($record_channel_names, '[' . $fmt_pay_channel['name'] . ']');
  303. array_push($channel_ids, $channel->channel_id);
  304. $item = [
  305. 'order_id' => $order->id,
  306. 'channel_id' => $channel->channel_id,
  307. 'channel_name' => $fmt_pay_channel['name'],
  308. 'create_time' => time(),
  309. 'update_time' => time(),
  310. ];
  311. if ($fmt_pay_channel['type'] == 1) { // 正常支付
  312. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  313. $item['fee'] = $channel->fee;
  314. $total_fee += $channel->fee;
  315. if ($fmt_pay_channel['is_upload_code']) {
  316. predicate(isset($channel->code), '必须上传付款编号');
  317. $item['code'] = $channel->code;
  318. }
  319. array_push($order_payments, $item);
  320. } else if ($fmt_pay_channel['type'] == 2) { // 御龙币支付
  321. predicate(isset($channel->fee) && $channel->fee > 0, '支付费用未填写');
  322. $item['fee'] = $channel->fee;
  323. // TODO: 御龙币需求未明确
  324. array_push($order_payments, $item);
  325. $zue_coin += $channel->fee;
  326. } else {
  327. for ($j = 0; $j < count($channel->credit_card); $j++) {
  328. $credit_card = $channel->credit_card[$j];
  329. $fmt_credit_card_config = $fmt_credit_card_configs[$credit_card->credit_card_id];
  330. if (!$fmt_credit_card_config)
  331. return $this->fail("信用卡支付渠道错误!");
  332. predicate(isset($credit_card->fee) && $credit_card->fee > 0, '支付费用未填写');
  333. $total_fee += $credit_card->fee;
  334. $el = [
  335. 'order_id' => $order->id,
  336. 'channel_id' => $channel->channel_id,
  337. 'channel_name' => $fmt_pay_channel['name'],
  338. 'fee' => $credit_card->fee,
  339. 'credit_card_id' => $credit_card->credit_card_id,
  340. 'credit_card_name' => $fmt_credit_card_config['bank'],
  341. 'create_time' => time(),
  342. 'update_time' => time(),
  343. ];
  344. if ($fmt_pay_channel['is_upload_code']) {
  345. predicate(isset($credit_card->code), '必须填写付款编号');
  346. $el['code'] = $credit_card->code;
  347. }
  348. if ($fmt_credit_card_config['is_stage'] == 1) {
  349. predicate(isset($credit_card->stage_num), '必须选择分期数');
  350. $el['stage_num'] = $credit_card->stage_num;
  351. $config = [
  352. '6' => $fmt_credit_card_config['stage_6'],
  353. '9' => $fmt_credit_card_config['stage_9'],
  354. '12' => $fmt_credit_card_config['stage_12'],
  355. '24' => $fmt_credit_card_config['stage_24'],
  356. '36' => $fmt_credit_card_config['stage_36'],
  357. ];
  358. $now_config = $config[$credit_card->stage_num];
  359. predicate($now_config[0] == 1, '该分期未启用!');
  360. if ($now_config[1] > 0) { // 收取手续费
  361. $el['service_charge_rate'] = $now_config[1];
  362. $now_service_charge = $el['fee'] * ($now_config[1] / 100);
  363. $el['service_charge'] = $now_service_charge;
  364. $service_charge += $now_service_charge;
  365. }
  366. }
  367. array_push($order_payments, $el);
  368. }
  369. }
  370. }
  371. // 总共需要收取这么多费用; (商品的价格 + 商品税费 + 年费) - 御龙币抵扣的费用
  372. $receivable_amount = fixed2Float($order->product_amount + $order->total_sales_tax + $order->total_annual_fee);
  373. $order_use_zue_coin = $order->zue_coin;
  374. $order_use_zue_coin_amount = $order->zue_coin_amount;
  375. $change_zue_coin_record = null;
  376. $zue_coin_model = $this->zueCoinModel->findByCustomerId($customer->id);
  377. $zue_coin_exchange_rate = 0;
  378. if ($zue_coin > 0) {
  379. if (!$zue_coin_model)
  380. return $this->fail("当前客户御龙币账户余额: 0");
  381. if ($zue_coin > $zue_coin_model->zue_coin)
  382. return $this->fail("当前客户御龙币账户余额:{$zue_coin_model->zue_coin}");
  383. $zue_coin_config = $fmt_pay_channels['11'];
  384. if (!$zue_coin_config)
  385. return $this->fail("御龙币设置不存在!");
  386. $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
  387. $zue_coin_consume_rate = $zue_coin_config['zue_coin_consume_rate'] ?? 0; // 报销比例
  388. if ($zue_coin_exchange_rate == 0 || $zue_coin_consume_rate == 0) {
  389. return $this->fail("该笔订单不支持御龙币支付");
  390. }
  391. // 1.计算出该订单能使用多少御龙币
  392. // 2.结合之前已使用的御龙币 算出还能使用多少御龙币
  393. // 3.判断当前御龙币是否符合兑换数额
  394. // 4.进行扣除并记录
  395. // 根据御龙币的设置 总共能报销这么多钱
  396. $max_consume_amount = fixed2Float($receivable_amount * ($zue_coin_consume_rate / 100));
  397. // 根据御龙币的设置 最大的报销的御龙币
  398. $max_consume_zue_coin = fixed2Float($max_consume_amount / $zue_coin_exchange_rate);
  399. if ($max_consume_zue_coin - ($order->zue_coin + $zue_coin) < 0) {
  400. return $this->fail("最多能使用御龙币: {$max_consume_zue_coin}, 当前已使用{$order->zue_coin}");
  401. }
  402. // 根据当前订单支付情况 得出现在最大的报销金额;
  403. $now_max_consume_amount = $order->imposed_amount < $max_consume_amount ? $order->imposed_amount : $max_consume_amount;
  404. // 当前最多还能使用多少御龙币
  405. $now_can_consume_zue_coin = fixed2Float($now_max_consume_amount / $zue_coin_exchange_rate);
  406. // 判断当前还能使用
  407. if ($now_can_consume_zue_coin - $zue_coin < 0)
  408. return $this->fail("当前剩余可使用的: {$now_can_consume_zue_coin}");
  409. // 总共使用了这么多御龙币
  410. $order_use_zue_coin += $zue_coin;
  411. // 总共兑换了这么多钱
  412. $order_use_zue_coin_amount = fixed2Float($order_use_zue_coin * $zue_coin_exchange_rate);
  413. $change_zue_coin_record = [
  414. 'zue_coin_id' => $zue_coin_model->id,
  415. 'order_id' => $order->id,
  416. 'origin' => $zue_coin_model->zue_coin,
  417. 'change' => -$zue_coin,
  418. 'after' => $zue_coin_model->zue_coin - $zue_coin,
  419. 'reason' => 1,
  420. 'create_time' => time(),
  421. 'update_time' => time(),
  422. ];
  423. }
  424. // 总计费用 - 御龙币抵消费用;
  425. $receivable_amount -= $order_use_zue_coin_amount;
  426. // 总计实收金额
  427. $receive_amount = fixed2Float($order->receive_amount + $total_fee);
  428. if (round($receive_amount) > round($receivable_amount)) return $this->fail("实收金额大于待收金额! 当前已付款: {$order->receive_amount} / {$order->receivable_amount} (商品总价:{$order->product_amount} + 商品总消费税:{$order->total_sales_tax} + 第一年年费:{$order->total_annual_fee} + 信用卡总手续费:{$order->service_charge_amount})");
  429. $order_annual_fees = [];
  430. $update_store_products = [];
  431. $update_order_products = [];
  432. // 是否全部支付
  433. $is_all_in_pay = round($receive_amount) == round($receivable_amount);
  434. if ($is_all_in_pay) {
  435. // 减库存
  436. $order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
  437. $store_products = $this->storeProductModel->findByIds(array_map(function ($r) {
  438. return $r['store_product_id'];
  439. }, $order_products))->toArray();
  440. $fmt_store_products = array_reduce($store_products, function ($result, $item) {
  441. $result[$item['id']] = $item;
  442. return $result;
  443. }, []);
  444. // 商品本身的价格 | 年费 | 消费税
  445. $total_ = $order->product_amount + $order->total_annual_fee + $order->total_sales_tax;
  446. // 信用卡手续费 每一块钱所占的比例
  447. $item_service_charge_rate = ($order->service_charge_amount + $service_charge) / $total_;
  448. // 每一个御龙币所占的比例
  449. $item_zue_coin_rate = $order_use_zue_coin / $total_;
  450. foreach ($order_products as $order_product) {
  451. predicate($fmt_store_products[$order_product['store_product_id']]['now_stock'] > 0, lang('inventory shortage'));
  452. $fmt_store_products[$order_product['store_product_id']]['now_stock'] -= 1;
  453. if (isset($update_store_products[$order_product['store_product_id']])) {
  454. $update_store_products[$order_product['store_product_id']]['reduce_num'] += 1;
  455. } else {
  456. $update_store_products[$order_product['store_product_id']] = ['id' => $order_product['store_product_id'], 'reduce_num' => 1];
  457. }
  458. if ($order_product['is_gather_annuity'] && $order_product['annuity'] > 0) {
  459. $adviser_ids = [];
  460. if ($order_product['adviser_1_id']) {
  461. array_push($adviser_ids, $order_product['adviser_1_id']);
  462. }
  463. if ($order_product['adviser_2_id']) {
  464. array_push($adviser_ids, $order_product['adviser_2_id']);
  465. }
  466. array_push($order_annual_fees, [
  467. 'order_id' => $order->id,
  468. 'customer_id' => $customer->id,
  469. 'customer_name' => "{$customer->name_en}|{$customer->name_zh}",
  470. 'customer_mobile' => $customer->mobile,
  471. 'customer_create_username' => $customer->create_username,
  472. 'index' => 1,
  473. 'order_product_id' => $order_product['id'],
  474. 'order_product_name' => $order_product['product_name'],
  475. 'fee' => $order_product['annuity'],
  476. 'is_pay' => 1,
  477. 'start_time' => time(),
  478. 'end_time' => time() + (365 * 24 * 60 * 60),
  479. 'order_create_time' => strtotime($order->create_time),
  480. 'adviser_ids' => join(',', $adviser_ids),
  481. 'store_id' => $order->store_id,
  482. 'create_time' => time(),
  483. 'update_time' => time()
  484. ]);
  485. }
  486. $now = $order_product['real_price'] + $order_product['annuity'] + $order_product['sales_tax'];
  487. $item_service_charge = fixed2Float($now * $item_service_charge_rate);
  488. $item_zue_coin = fixed2Float($now * $item_zue_coin_rate);
  489. $item_zue_coin_amount = fixed2Float($item_zue_coin * $zue_coin_exchange_rate);
  490. $transaction_price = fixed2Float($now - $item_zue_coin_amount);
  491. array_push($update_order_products, [
  492. 'id' => $order_product['id'],
  493. 'service_charge' => $item_service_charge,
  494. 'zue_coin' => $item_zue_coin,
  495. 'zue_coin_amount' => $item_zue_coin_amount,
  496. 'transaction_price' => $transaction_price,
  497. 'channel_ids' => join(',', array_unique($channel_ids)),
  498. 'is_pay' => 1
  499. ]);
  500. }
  501. }
  502. Db::startTrans();
  503. try {
  504. $update_order = [
  505. 'receivable_amount' => $receivable_amount,
  506. 'receive_amount' => $receive_amount,
  507. 'imposed_amount' => $receivable_amount - $receive_amount,
  508. 'service_charge_amount' => $order->service_charge_amount + $service_charge,
  509. 'zue_coin' => $order_use_zue_coin,
  510. 'zue_coin_amount' => $order_use_zue_coin_amount,
  511. 'remarks' => $params['remarks'] ?? null
  512. ];
  513. if ($is_all_in_pay) { // 支付满了
  514. if (count($update_order_products) > 0) {
  515. foreach ($update_order_products as $item) {
  516. Db::table('erp_order_product')->where('id', $item['id'])->update($item);
  517. }
  518. }
  519. if (count($order_annual_fees) > 0) {
  520. Db::table('erp_order_annual_fee')->insertAll($order_annual_fees);
  521. }
  522. if (count($update_store_products) > 0) {
  523. foreach ($update_store_products as $update) {
  524. Db::table('erp_store_product')->where('id', $update['id'])->dec('now_stock', 1)->update();
  525. Db::table('erp_store_product')->where('id', $update['id'])->inc('sale_stock', 1)->update();
  526. }
  527. }
  528. }
  529. Db::table('erp_order_proceeds')->save([
  530. 'order_id' => $order->id,
  531. 'channels' => join(' ', $record_channel_names),
  532. 'fee' => $total_fee,
  533. 'admin_id' => $params['admin_id'],
  534. 'admin_name' => $params['admin_name'],
  535. 'create_time' => time(),
  536. 'update_time' => time(),
  537. ]);
  538. if ($zue_coin > 0 && $change_zue_coin_record != null) {
  539. Db::table('erp_zue_coin_record')->save($change_zue_coin_record);
  540. Db::table('erp_customer_zue_coin')->where('id', $change_zue_coin_record['zue_coin_id'])->dec('zue_coin', $zue_coin)->update();
  541. }
  542. if (count($order_payments) > 0) {
  543. Db::table('erp_order_payment')->insertAll(array_map(function ($data) {
  544. return [
  545. "order_id" => isset($data['order_id']) ? $data['order_id'] : null,
  546. "channel_id" => isset($data['channel_id']) ? $data['channel_id'] : null,
  547. "channel_name" => isset($data['channel_name']) ? $data['channel_name'] : null,
  548. "fee" => isset($data['fee']) ? $data['fee'] : null,
  549. "credit_card_id" => isset($data['credit_card_id']) ? $data['credit_card_id'] : null,
  550. "credit_card_name" => isset($data['credit_card_name']) ? $data['credit_card_name'] : null,
  551. "create_time" => isset($data['create_time']) ? $data['create_time'] : null,
  552. "update_time" => isset($data['update_time']) ? $data['update_time'] : null,
  553. "code" => isset($data['code']) ? $data['code'] : null,
  554. "stage_num" => isset($data['stage_num']) ? $data['stage_num'] : null,
  555. "service_charge_rate" => isset($data['service_charge_rate']) ? $data['service_charge_rate'] : null,
  556. "service_charge" => isset($data['service_charge']) ? $data['service_charge'] : null
  557. ];
  558. }, $order_payments));
  559. }
  560. Db::table('erp_order')->where('id', $order->id)->update($update_order);
  561. Db::commit();
  562. } catch (\Exception $e) {
  563. Db::rollback();
  564. return $this->fail(lang($e->getMessage()));
  565. }
  566. return $this->ok(true);
  567. }
  568. /**
  569. * @param $admin_id
  570. * @param $order_id
  571. * @param array $order_product_ids
  572. * @return \SResult
  573. * @throws \think\db\exception\DataNotFoundException
  574. * @throws \think\db\exception\DbException
  575. * @throws \think\db\exception\ModelNotFoundException
  576. */
  577. public function returnOrderProduct($admin_id, $order_id, array $order_product_ids)
  578. {
  579. // $verify = AuthService::verify($admin_id, 10005);
  580. $order = $this->orderModel->findById($order_id);
  581. if (!$order) return $this->fail(lang("Order does not exist"));
  582. if ($order->type == 2) return $this->fail(lang("The status of the order does not support return or exchange"));
  583. $return_order_products = $this->orderProductModel->findByIds($order_product_ids)->toArray();
  584. if (count($order_product_ids) != count($return_order_products)) return $this->fail(lang("The order item was incorrectly selected"));
  585. $all_order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
  586. $merge_order_products = array_reduce($all_order_products,
  587. function ($result, $item) use ($order_product_ids) {
  588. if (!in_array($item['id'], $order_product_ids))
  589. array_push($result, $item);
  590. return $result;
  591. }, []);
  592. $obj_names = array_map(function ($data) {
  593. return $data['product_name'];
  594. }, $merge_order_products);
  595. $advisor_ids = array_reduce($merge_order_products, function ($result, $item) {
  596. if ($item['adviser_1_id'])
  597. array_push($result, $item['adviser_1_id']);
  598. if ($item['adviser_2_id'])
  599. array_push($result, $item['adviser_2_id']);
  600. return $result;
  601. }, []);
  602. $zue_coin_config = $this->paymentChannelModel->findById(11);
  603. if (!$zue_coin_config)
  604. return $this->fail("御龙币设置不存在!");
  605. $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
  606. //商品本金
  607. $total_product_amount = $order->product_amount;
  608. // 整体税费
  609. $total_sales_tax = $order->total_sales_tax;
  610. // 总体信用卡服务费
  611. $total_service_amount = $order->service_charge_amount;
  612. // 总体年费
  613. $total_annual_fee = $order->total_annual_fee;
  614. // 总体御龙币
  615. $total_zue_coin = $order->zue_coin;
  616. // 御龙币兑换的金额
  617. $total_zue_coin_amount = $order->zue_coin_amount;
  618. // 御龙币当时的兑换比例
  619. $zue_coin_exchange_rate = $order->zue_coin_amount > 0 && $order->zue_coin > 0 ? fixed2Float($order->zue_coin_amount / $order->zue_coin) : $zue_coin_exchange_rate;
  620. // 总计退还多少钱
  621. $total_return_amount = 0;
  622. // 商品库存
  623. $return_now_stocks = [];
  624. foreach ($return_order_products as $return_order_product) {
  625. // 减少御龙币
  626. $total_zue_coin -= $return_order_product['zue_coin'];
  627. $total_zue_coin_amount -= ($return_order_product['zue_coin'] * $zue_coin_exchange_rate);
  628. // 减少年费
  629. $total_annual_fee -= $return_order_product['annuity'];
  630. // 减少信用卡服务费
  631. $total_service_amount -= $return_order_product['service_charge'];
  632. // 减少税费
  633. $total_sales_tax -= $return_order_product['sales_tax'];
  634. // 减少本金
  635. $total_product_amount -= $return_order_product['real_price'];
  636. $total_return_amount -= $return_order_product['transaction_price'];
  637. if (isset($return_now_stocks[$return_order_product['store_product_id']])) {
  638. $return_now_stocks[$return_order_product['store_product_id']] += 1;
  639. } else {
  640. $return_now_stocks[$return_order_product['store_product_id']] = 1;
  641. }
  642. }
  643. $total_order_rental_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_annual_fee);
  644. $total_receivable_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_annual_fee - ($total_zue_coin * $zue_coin_exchange_rate));
  645. $total_receive_amount = $total_receivable_amount;
  646. $annual_fees = $this->orderAnnualFeeModel->fetchByOrderProductId($order_product_ids);
  647. Db::startTrans();
  648. try {
  649. // 退还御龙币
  650. $return_zue_coin = $order->zue_coin - $total_zue_coin;
  651. if ($return_zue_coin > 0) {
  652. $customer_zue_coin = $this->zueCoinModel->findByCustomerId($order->customer_id);
  653. if (!$customer_zue_coin) {
  654. throw new BaseException('御龙币账户记录不存在', 1012);
  655. }
  656. Db::table('erp_customer_zue_coin')->where('id', '=', $customer_zue_coin->id)->inc('zue_coin', $return_zue_coin)->update();
  657. Db::table('erp_zue_coin_record')->save([
  658. 'zue_coin_id' => $customer_zue_coin->id,
  659. 'order_id' => $order->id,
  660. 'origin' => $customer_zue_coin->zue_coin,
  661. 'change' => $return_zue_coin,
  662. 'after' => $customer_zue_coin->zue_coin + $return_zue_coin,
  663. 'reason' => 3,
  664. 'create_time' => time(),
  665. 'update_time' => time(),
  666. ]);
  667. }
  668. // 退还年费
  669. if (count($annual_fees) > 0)
  670. Db::table('erp_order_annual_fee')->where('id', 'in', array_map(function ($data) {
  671. return $data['id'];
  672. }, $annual_fees))->update(['is_delete' => 1, 'delete_time' => time()]);
  673. // 更新订单
  674. if (count($return_order_products) == count($all_order_products)) {
  675. Db::table('erp_order')->where('id', $order->id)->update(['is_delete' => 1]);
  676. } else {
  677. Db::table('erp_order')->where('id', $order->id)->update([
  678. 'rental_amount' => $total_order_rental_amount,
  679. 'receivable_amount' => $total_receivable_amount,
  680. 'receive_amount' => $total_receive_amount,
  681. 'product_amount' => $total_product_amount,
  682. 'total_sales_tax' => $total_sales_tax,
  683. 'service_charge_amount' => $total_service_amount,
  684. 'total_annual_fee' => $total_annual_fee,
  685. 'zue_coin' => $total_zue_coin,
  686. 'zue_coin_amount' => $total_zue_coin_amount,
  687. 'obj_names' => join(',', array_unique($obj_names)),
  688. 'advisor_ids' => join(',', array_unique($advisor_ids)),
  689. ]);
  690. }
  691. // 插入退款记录
  692. Db::table('erp_order_payment')->save([
  693. 'order_id' => $order->id,
  694. 'channel_id' => 0,
  695. 'channel_name' => "Exchange a purchase",
  696. 'fee' => $total_return_amount,
  697. 'create_time' => time(),
  698. 'update_time' => time(),
  699. ]);
  700. // 退还商品库存
  701. $return_stock_keys = array_keys($return_now_stocks);
  702. foreach ($return_stock_keys as $key) {
  703. Db::table("erp_store_product")
  704. ->where('id', $key)
  705. ->inc('now_stock', $return_now_stocks[$key])
  706. ->dec('sale_stock', $return_now_stocks[$key])
  707. ->update();
  708. }
  709. // 删除订单商品
  710. Db::table('erp_order_product')->where('id', 'in', $order_product_ids)->update(['is_delete' => 1, 'delete_time' => time()]);
  711. Db::commit();
  712. } catch (\Exception $e) {
  713. Db::rollback();
  714. return $this->fail(lang($e->getMessage()));
  715. }
  716. return $this->ok(true);
  717. }
  718. /**
  719. * @param $admin_id
  720. * @param $admin_name
  721. * @param $order_id
  722. * @param $origin_order_product_ids
  723. * @param $append_store_product_ids
  724. * @return \SResult
  725. * @throws \think\db\exception\DataNotFoundException
  726. * @throws \think\db\exception\DbException
  727. * @throws \think\db\exception\ModelNotFoundException
  728. */
  729. public function exchange($admin_id, $admin_name, $order_id, $origin_order_product_ids, $append_store_product_ids)
  730. {
  731. // $verify = AuthService::verify($admin_id, 10005);
  732. $order = $this->orderModel->findById($order_id);
  733. if (!$order) return $this->fail(lang("Order does not exist"));
  734. if ($order->type == 2) return $this->fail(lang("The status of the order does not support return or exchange"));
  735. $return_order_products = $this->orderProductModel->findByIds($origin_order_product_ids)->toArray();
  736. if (count($origin_order_product_ids) != count($return_order_products)) return $this->fail(lang("The order item was incorrectly selected"));
  737. $all_order_products = $this->orderProductModel->findByOrderId($order->id)->toArray();
  738. $merge_order_products = array_reduce($all_order_products,
  739. function ($result, $item) use ($origin_order_product_ids) {
  740. if (!in_array($item['id'], $origin_order_product_ids))
  741. array_push($result, $item);
  742. return $result;
  743. }, []);
  744. $obj_names = array_map(function ($data) {
  745. return $data['product_name'];
  746. }, $merge_order_products);
  747. $advisor_ids = array_reduce($merge_order_products, function ($result, $item) {
  748. if ($item['adviser_1_id'])
  749. array_push($result, $item['adviser_1_id']);
  750. if ($item['adviser_2_id'])
  751. array_push($result, $item['adviser_2_id']);
  752. return $result;
  753. }, []);
  754. $zue_coin_config = $this->paymentChannelModel->findById(11);
  755. if (!$zue_coin_config)
  756. return $this->fail("御龙币设置不存在!");
  757. $zue_coin_exchange_rate = $zue_coin_config['zue_coin_exchange_rate'] ?? 0; // 兑换比例
  758. //商品本金
  759. $total_product_amount = $order->product_amount;
  760. // 整体税费
  761. $total_sales_tax = $order->total_sales_tax;
  762. // 总体信用卡服务费
  763. $total_service_amount = $order->service_charge_amount;
  764. // 总体年费
  765. $total_annual_fee = $order->total_annual_fee;
  766. // 总体御龙币
  767. $total_zue_coin = $order->zue_coin;
  768. // 御龙币兑换的金额
  769. $total_zue_coin_amount = $order->zue_coin_amount;
  770. // 御龙币当时的兑换比例
  771. $zue_coin_exchange_rate = $order->zue_coin_amount > 0 && $order->zue_coin > 0 ? fixed2Float($order->zue_coin_amount / $order->zue_coin) : $zue_coin_exchange_rate;
  772. // 差价
  773. $diff_amount = 0;
  774. // 商品库存
  775. $return_now_stocks = [];
  776. // 减操作
  777. foreach ($return_order_products as $return_order_product) {
  778. // 减少御龙币
  779. $total_zue_coin -= $return_order_product['zue_coin'];
  780. $total_zue_coin_amount -= ($return_order_product['zue_coin'] * $zue_coin_exchange_rate);
  781. // 减少年费
  782. $total_annual_fee -= $return_order_product['annuity'];
  783. // 减少信用卡服务费
  784. $total_service_amount -= $return_order_product['service_charge'];
  785. // 减少税费
  786. $total_sales_tax -= $return_order_product['sales_tax'];
  787. // 减少本金
  788. $total_product_amount -= $return_order_product['real_price'];
  789. $diff_amount -= $return_order_product['transaction_price'];
  790. if (isset($return_now_stocks[$return_order_product['store_product_id']])) {
  791. $return_now_stocks[$return_order_product['store_product_id']] += 1;
  792. } else {
  793. $return_now_stocks[$return_order_product['store_product_id']] = 1;
  794. }
  795. }
  796. /////////////// 加操作
  797. // 客户
  798. $customer = $this->customerModel->findById($order->customer_id);
  799. if (!$customer)
  800. return $this->fail(lang("The Customer does not exist"));
  801. // 门店商品
  802. $store_products = $this->storeProductModel->fetchRelationsByOrder($append_store_product_ids)->toArray();
  803. $store_products = array_reduce($store_products, function ($result, $item) {
  804. $result[$item['id']] = $item;
  805. return $result;
  806. }, []);
  807. $store_products = array_map(function ($id) use ($store_products) {
  808. return $store_products[$id];
  809. }, $append_store_product_ids);
  810. if (count($store_products) != count($append_store_product_ids))
  811. return $this->fail(lang("The Product does not exist"));
  812. // 商品信息
  813. $products = array_map(function ($p) {
  814. return $p['product'] ? $p['product'] : null;
  815. }, $store_products);
  816. if (in_array(null, $products))
  817. return $this->fail(lang("The Product does not exist"));
  818. $config = $this->configModel->findConfig();
  819. $activityProducts = $this->activityProductModel->fetchByProductIds(array_map(function ($data) {
  820. return $data['id'];
  821. }, $products))->toArray();
  822. $fmt_store_products = [];
  823. foreach ($store_products as &$item) {
  824. $aProducts = array_filter($activityProducts, function ($aProduct) use ($item) {
  825. return $aProduct['product_id'] == $item['product_id'];
  826. });
  827. $res = compare($item['product']['real_price'] ?? 0, $aProducts);
  828. $item['activity'] = $res['item'];
  829. $item['product']['origin_price'] = $item['product']['real_price'];
  830. $item['product']['real_price'] = fixed2Float($res['min_num'] > 0 ? $res['min_num'] : 0);
  831. $sales_tax_rate = $item['product']['sales_tax_rate'] > 0 ? $item['product']['sales_tax_rate'] : $config->sales_tax_rate;
  832. $sales_tax = fixed2Float($sales_tax_rate > 0 && $item['product']['real_price'] > 0 ? ($item['product']['real_price'] * ($sales_tax_rate / 100)) : 0);
  833. $item['product']['sales_tax_rate'] = $sales_tax_rate;
  834. $item['product']['sales_tax'] = $sales_tax;
  835. $fmt_store_products[$item['id']] = $item;
  836. }
  837. $order_products = [];
  838. $order_annual_fees = [];
  839. for ($j = 0; $j < count($store_products); $j++) {
  840. $store_product = $store_products[$j];
  841. array_push($obj_names, $store_product['product']['name']);
  842. array_push($advisor_ids, $admin_id);
  843. array_push($order_products, [
  844. 'order_id' => $order->id,
  845. 'order_no' => $order->no,
  846. 'customer_id' => $order->customer_id,
  847. 'product_id' => $store_product['product']['id'],
  848. 'product_category_id' => $store_product['product']['category_id'],
  849. 'is_serve' => $store_product['product']['is_serve'],
  850. 'store_product_id' => $store_product['id'],
  851. 'product_name' => $store_product['product']['name'],
  852. 'adviser_1_id' => $admin_id,
  853. 'adviser_1_name' => $admin_name,
  854. 'adviser_2_id' => null,
  855. 'adviser_2_name' => null,
  856. 'teacher_1_id' => null,
  857. 'teacher_1_name' => null,
  858. 'teacher_2_id' => null,
  859. 'teacher_2_name' => null,
  860. 'is_upload_numerology' => $store_product['product']['is_upload_numerology'],
  861. 'is_upload' => 0,
  862. 'report' => null,
  863. 'is_gather_annuity' => $store_product['product']['is_gather_annuity'],
  864. 'annuity' => $store_product['product']['annuity'],
  865. 'real_price' => $store_product['product']['real_price'],
  866. 'service_charge' => 0,
  867. 'reduce_price' => $store_product['activity'] ? $store_product['product']['origin_price'] - $store_product['product']['real_price'] : 0,
  868. 'reduce_type' => $store_product['activity'] ? $store_product['activity']['type'] : 0,
  869. 'sales_tax_rate' => $store_product['product']['sales_tax_rate'],
  870. 'sales_tax' => $store_product['product']['sales_tax'],
  871. 'transaction_price' => fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']),
  872. 'zue_coin' => 0,
  873. 'is_pay' => 0,
  874. 'create_time' => time(),
  875. 'update_time' => time(),
  876. ]);
  877. array_push($order_annual_fees, $store_product['product']['is_gather_annuity'] && $store_product['product']['annuity'] > 0 ? [
  878. 'order_id' => $order->id,
  879. 'customer_id' => $order->customer_id,
  880. 'customer_name' => $order->customer_name,
  881. 'customer_mobile' => $customer->mobile,
  882. 'customer_create_username' => $customer->create_username,
  883. 'index' => 1,
  884. 'order_product_id' => -1,
  885. 'order_product_name' => $store_product['product']['name'],
  886. 'fee' => $store_product['product']['annuity'],
  887. 'is_pay' => 0,
  888. 'start_time' => time(),
  889. 'end_time' => time() + (365 * 24 * 60 * 60),
  890. 'order_create_time' => strtotime($order->create_time),
  891. 'adviser_ids' => $admin_id,
  892. 'store_id' => $order->store_id,
  893. 'create_time' => time(),
  894. 'update_time' => time()
  895. ] : null);
  896. $total_product_amount += $store_product['product']['real_price'];
  897. $total_sales_tax += $store_product['product']['sales_tax'];
  898. if ($store_product['product']['is_gather_annuity'] == 1)
  899. $total_annual_fee += $store_product['product']['annuity'];
  900. $diff_amount += fixed2Float($store_product['product']['real_price'] + $store_product['product']['sales_tax'] + $store_product['product']['annuity']);
  901. }
  902. $total_order_rental_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_service_amount + $total_annual_fee);
  903. $total_receivable_amount = fixed2Float($total_product_amount + $total_sales_tax + $total_service_amount + $total_annual_fee - ($total_zue_coin * $zue_coin_exchange_rate));
  904. $total_receive_amount = $total_receivable_amount;
  905. $annual_fees = $this->orderAnnualFeeModel->fetchByOrderProductId($origin_order_product_ids);
  906. $return_zue_coin = $order->zue_coin - $total_zue_coin;
  907. Db::startTrans();
  908. try {
  909. // 退还御龙币
  910. if ($return_zue_coin > 0) {
  911. $customer_zue_coin = $this->zueCoinModel->findByCustomerId($order->customer_id);
  912. if (!$customer_zue_coin) {
  913. throw new BaseException('御龙币账户记录不存在', 1012);
  914. }
  915. Db::table('erp_customer_zue_coin')->where('id', '=', $customer_zue_coin->id)->inc('zue_coin', $return_zue_coin)->update();
  916. Db::table('erp_zue_coin_record')->save([
  917. 'zue_coin_id' => $customer_zue_coin->id,
  918. 'order_id' => $order->id,
  919. 'origin' => $customer_zue_coin->zue_coin,
  920. 'change' => $return_zue_coin,
  921. 'after' => $customer_zue_coin->zue_coin + $return_zue_coin,
  922. 'reason' => 3,
  923. 'create_time' => time(),
  924. 'update_time' => time(),
  925. ]);
  926. }
  927. // 退还年费
  928. if (count($annual_fees) > 0)
  929. Db::table('erp_order_annual_fee')->where('id', 'in', array_map(function ($data) {
  930. return $data['id'];
  931. }, $annual_fees))->update(['is_delete' => 1, 'delete_time' => time()]);
  932. // 退还商品库存
  933. $return_stock_keys = array_keys($return_now_stocks);
  934. foreach ($return_stock_keys as $key) {
  935. Db::table("erp_store_product")
  936. ->where('id', $key)
  937. ->inc('now_stock', $return_now_stocks[$key])
  938. ->dec('sale_stock', $return_now_stocks[$key])
  939. ->update();
  940. }
  941. // 删除订单商品
  942. Db::table('erp_order_product')->where('id', 'in', $origin_order_product_ids)->update(['is_delete' => 1, 'delete_time' => time()]);
  943. $imposed_amount = fixed2Float($total_receivable_amount - $order->receive_amount); // 应收金额 - 实收金额
  944. if ($imposed_amount == 0) {// 应收金额 == 代收金额;
  945. // 新增商品
  946. for ($i = 0; $i < count($order_products); $i++) {
  947. $order_product = $order_products[$i];
  948. $order_product['is_pay'] = 1;
  949. $order_annual_fee = $order_annual_fees[$i];
  950. $last_id = Db::table('erp_order_product')->insert($order_product, true);
  951. if ($order_annual_fee != null) {
  952. $order_annual_fee['order_product_id'] = $last_id;
  953. $order_annual_fee['is_pay'] = 1;
  954. Db::table('erp_order_annual_fee')->save($order_annual_fee);
  955. }
  956. Db::table("erp_store_product")
  957. ->where('id', $order_product['store_product_id'])
  958. ->dec('now_stock', 1)
  959. ->inc('sale_stock', 1)
  960. ->update();
  961. }
  962. // 更新订单
  963. Db::table('erp_order')->where('id', $order->id)->update([
  964. 'rental_amount' => $total_order_rental_amount,
  965. 'receivable_amount' => $total_receivable_amount,
  966. 'receive_amount' => $total_receive_amount,
  967. 'product_amount' => $total_product_amount,
  968. 'total_sales_tax' => $total_sales_tax,
  969. 'service_charge_amount' => $total_service_amount,
  970. 'total_annual_fee' => $total_annual_fee,
  971. 'zue_coin' => $total_zue_coin,
  972. 'zue_coin_amount' => $total_zue_coin_amount,
  973. 'obj_names' => join(',', array_unique($obj_names)),
  974. 'advisor_ids' => join(',', array_unique($advisor_ids)),
  975. ]);
  976. } else { // 补差价
  977. // 更新订单
  978. Db::table('erp_order')->where('id', $order->id)->update([
  979. 'rental_amount' => $total_order_rental_amount,
  980. 'receivable_amount' => $total_receivable_amount,
  981. "imposed_amount" => $imposed_amount,
  982. 'product_amount' => $total_product_amount,
  983. 'total_sales_tax' => $total_sales_tax,
  984. 'service_charge_amount' => $total_service_amount,
  985. 'total_annual_fee' => $total_annual_fee,
  986. 'zue_coin' => $total_zue_coin,
  987. 'zue_coin_amount' => $total_zue_coin_amount,
  988. 'type' => 2,
  989. 'obj_names' => join(',', array_unique($obj_names)),
  990. 'advisor_ids' => join(',', array_unique($advisor_ids)),
  991. ]);
  992. // 新增商品
  993. Db::table('erp_order_product')->insertAll($order_products);
  994. }
  995. Db::commit();
  996. } catch (\Exception $e) {
  997. Db::rollback();
  998. return $this->fail(lang($e->getMessage()));
  999. }
  1000. return $this->ok($diff_amount);
  1001. }
  1002. public function paymentDone($admin_id, $order_id, $remarks = null)
  1003. {
  1004. $order = $this->orderModel->findById($order_id);
  1005. if (!$order)
  1006. return $this->fail(lang("Order does not exist"));
  1007. if ($order->imposed_amount < 0)
  1008. return $this->fail('订单状态为历史订单!不支持付款');
  1009. // 客户
  1010. $customer = $this->customerModel->findById($order->customer_id);
  1011. if (!$customer)
  1012. return $this->fail(lang("The Customer does not exist"));
  1013. $no_pay_order_products = $this->orderProductModel->findByOrderIdAndPay($order->id, 0)->toArray();
  1014. Db::startTrans();
  1015. try {
  1016. $update_order = [
  1017. 'imposed_amount' => 0,
  1018. 'receive_amount' => $order->receivable_amount,
  1019. 'type' => 1,
  1020. 'remarks' => $remarks
  1021. ];
  1022. $order_annual_fees = [];
  1023. for ($i = 0; $i < count($no_pay_order_products); $i++) {
  1024. $order_product = $no_pay_order_products[$i];
  1025. Db::table('erp_order_product')->where('id', $order_product['id'])->update([
  1026. 'is_pay' => 1
  1027. ]);
  1028. if ($order_product['is_gather_annuity'] && $order_product['annuity'] > 0) {
  1029. array_push($order_annual_fees, [
  1030. 'order_id' => $order->id,
  1031. 'customer_id' => $order->customer_id,
  1032. 'customer_name' => $order->customer_name,
  1033. 'customer_mobile' => $customer->mobile,
  1034. 'customer_create_username' => $customer->create_username,
  1035. 'index' => 1,
  1036. 'order_product_id' => $order_product['id'],
  1037. 'order_product_name' => $order_product['product_name'],
  1038. 'fee' => $order_product['annuity'],
  1039. 'is_pay' => 1,
  1040. 'start_time' => time(),
  1041. 'end_time' => time() + (365 * 24 * 60 * 60),
  1042. 'order_create_time' => strtotime($order->create_time),
  1043. 'adviser_ids' => $admin_id,
  1044. 'store_id' => $order->store_id,
  1045. 'create_time' => time(),
  1046. 'update_time' => time()
  1047. ]);
  1048. }
  1049. Db::table("erp_store_product")
  1050. ->where('id', $order_product['store_product_id'])
  1051. ->dec('now_stock', 1)
  1052. ->inc('sale_stock', 1)
  1053. ->update();
  1054. }
  1055. Db::table('erp_order_annual_fee')->insertAll($order_annual_fees);
  1056. // 更新订单
  1057. Db::table('erp_order')->where('id', $order->id)->update($update_order);
  1058. // 插入退款记录
  1059. Db::table('erp_order_payment')->save([
  1060. 'order_id' => $order->id,
  1061. 'channel_id' => 0,
  1062. 'channel_name' => "Exchange a purchase",
  1063. 'fee' => $order->imposed_amount,
  1064. 'create_time' => time(),
  1065. 'update_time' => time(),
  1066. ]);
  1067. Db::table('erp_order')->where('id', $order->id)->update($update_order);
  1068. Db::commit();
  1069. } catch (\Exception $e) {
  1070. Db::rollback();
  1071. return $this->fail(lang($e->getMessage()));
  1072. }
  1073. return $this->ok(true);
  1074. }
  1075. /**
  1076. * @param $params
  1077. * @return array
  1078. * @throws \think\db\exception\DbException
  1079. */
  1080. public function fetch($params)
  1081. {
  1082. $verify = AuthService::verify($params['admin_id'], $params['type'] == 1 ? 10003 : 10004);
  1083. $res = $this->orderModel->search($params, $verify);
  1084. $items = $res['data'];
  1085. return [array_map(function ($item) {
  1086. return [
  1087. "id" => $item['id'],
  1088. "no" => $item['no'],
  1089. "customer_id" => $item['customer_id'],
  1090. "customer_name" => $item['customer_name'],
  1091. "obj_names" => $item['obj_names'],
  1092. "rental_amount" => $item['rental_amount'],
  1093. "receivable_amount" => $item['receivable_amount'],
  1094. "receive_amount" => $item['receive_amount'],
  1095. "imposed_amount" => $item['imposed_amount'],
  1096. "product_amount" => $item['product_amount'],
  1097. "total_sales_tax" => $item['total_sales_tax'],
  1098. "service_charge_amount" => $item['service_charge_amount'],
  1099. "total_annual_fee" => $item['total_annual_fee'],
  1100. "zue_coin" => $item['zue_coin'],
  1101. "zue_coin_amount" => $item['zue_coin_amount'],
  1102. "type" => $item['type'],
  1103. "store_id" => $item['store_id'],
  1104. "advisor_ids" => $item['advisor_ids'],
  1105. "remarks" => $item['remarks'],
  1106. "create_time" => $item['create_time'],
  1107. "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1108. array_push($result, $item['adviser_1_name']);
  1109. if ($item['adviser_2_id'] > 0)
  1110. array_push($result, $item['adviser_2_name']);
  1111. return $result;
  1112. }, []))),
  1113. "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1114. if ($item['teacher_1_id'] > 0)
  1115. array_push($result, $item['teacher_1_name']);
  1116. if ($item['teacher_2_id'] > 0)
  1117. array_push($result, $item['teacher_2_name']);
  1118. return $result;
  1119. }, []))),
  1120. "payments" => join(' ', array_unique(array_reduce($item['payments'], function ($result, $item) {
  1121. array_push($result, '[' . $item['channel_name'] . ']');
  1122. return $result;
  1123. }, []))),
  1124. "stage_num" => array_reduce($item['payments'], function ($result, $item) {
  1125. if ($item['stage_num'] && $item['stage_num'] > $result) {
  1126. $result = $item['stage_num'];
  1127. }
  1128. return $result;
  1129. }, 0),
  1130. 'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
  1131. return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
  1132. }, $item['products'])),
  1133. ];
  1134. }, $items), $res['total']];
  1135. }
  1136. /**
  1137. * @param $params
  1138. * @return array
  1139. * @throws \think\db\exception\DbException
  1140. */
  1141. public function fetchByCustomerId($params)
  1142. {
  1143. $res = $this->orderModel->fetchByCustomerId($params['customer_id'], $params['page'] ?? 1, $params['size'] ?? 20);
  1144. $items = $res['data'];
  1145. return [array_map(function ($item) {
  1146. return [
  1147. "id" => $item['id'],
  1148. "no" => $item['no'],
  1149. "customer_id" => $item['customer_id'],
  1150. "customer_name" => $item['customer_name'],
  1151. "obj_names" => $item['obj_names'],
  1152. "rental_amount" => $item['rental_amount'],
  1153. "receivable_amount" => $item['receivable_amount'],
  1154. "receive_amount" => $item['receive_amount'],
  1155. "imposed_amount" => $item['imposed_amount'],
  1156. "product_amount" => $item['product_amount'],
  1157. "total_sales_tax" => $item['total_sales_tax'],
  1158. "service_charge_amount" => $item['service_charge_amount'],
  1159. "total_annual_fee" => $item['total_annual_fee'],
  1160. "zue_coin" => $item['zue_coin'],
  1161. "zue_coin_amount" => $item['zue_coin_amount'],
  1162. "type" => $item['type'],
  1163. "store_id" => $item['store_id'],
  1164. "advisor_ids" => $item['advisor_ids'],
  1165. "remarks" => $item['remarks'],
  1166. "create_time" => $item['create_time'],
  1167. "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1168. array_push($result, $item['adviser_1_name']);
  1169. if ($item['adviser_2_id'] > 0)
  1170. array_push($result, $item['adviser_2_name']);
  1171. return $result;
  1172. }, []))),
  1173. "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1174. if ($item['teacher_1_id'] > 0)
  1175. array_push($result, $item['teacher_1_name']);
  1176. if ($item['teacher_2_id'] > 0)
  1177. array_push($result, $item['teacher_2_name']);
  1178. return $result;
  1179. }, []))),
  1180. "stage_num" => array_reduce($item['payments'], function ($result, $item) {
  1181. if ($item['stage_num'] && $item['stage_num'] > $result) {
  1182. $result = $item['stage_num'];
  1183. }
  1184. return $result;
  1185. }, 0),
  1186. 'is_need_upload' => $item['type'] == 1 && in_array(true, array_map(function ($product) {
  1187. return $product['is_upload_numerology'] == 1 && $product['is_upload'] == 0;
  1188. }, $item['products'])),
  1189. ];
  1190. }, $items), $res['total']];
  1191. }
  1192. public function fetchOrderProducts($order_id, $page = 1, $size = 10)
  1193. {
  1194. $res = $this->orderProductModel->fetchByOrderId($order_id, $page, $size);
  1195. return [$res->items(), $res->total()];
  1196. }
  1197. public function upload_numerology($order_product_id, $report)
  1198. {
  1199. $order_product = $this->orderProductModel->findById($order_product_id);
  1200. if (!$order_product) return $this->fail('订单商品不存在,无法上传命理报告!');
  1201. if (!$order_product->is_pay) return $this->fail('订单未支付,无法上传商品');
  1202. $order_product->is_upload = 1;
  1203. $order_product->report = $report;
  1204. $order_product->save();
  1205. return $this->ok($order_product);
  1206. }
  1207. public function fetchWaitGatherAnnualFee($admin_id, $text = null, $start_time = null, $end_time = null, $page = 1, $size = 10)
  1208. {
  1209. $res = $this->orderAnnualFeeModel->fetchByAdviser($admin_id, $text, $start_time, $end_time, $page, $size);
  1210. return [$res[0]->items(), $res[0]->total(), $res[1]];
  1211. }
  1212. public function paymentAnnualFee($annual_fee_id)
  1213. {
  1214. $annual_fee = $this->orderAnnualFeeModel->findById($annual_fee_id);
  1215. if (!$annual_fee_id) {
  1216. return $this->fail('年费记录不存在!');
  1217. }
  1218. if ($annual_fee->is_pay == 1)
  1219. return $this->fail('该条年费记录已经支付!');
  1220. $annual_fee->is_pay = 1;
  1221. $annual_fee->update_time = time();
  1222. $annual_fee->save();
  1223. return $this->ok($annual_fee);
  1224. }
  1225. public function fetchOrderByReturn($admin_id, $store_id, $no = null, $customer_name = null, $page = 1, $size = 10)
  1226. {
  1227. $verify = AuthService::verify($admin_id, 10005);
  1228. $res = $this->orderModel->fetchOrderByReturn($admin_id, $store_id, $no, $customer_name, $page, $size, $verify)->toArray();
  1229. $items = $res['data'];
  1230. return [array_map(function ($item) {
  1231. return [
  1232. "id" => $item['id'],
  1233. "no" => $item['no'],
  1234. "customer_id" => $item['customer_id'],
  1235. "customer_name" => $item['customer_name'],
  1236. "obj_names" => $item['obj_names'],
  1237. "rental_amount" => $item['rental_amount'],
  1238. "receivable_amount" => $item['receivable_amount'],
  1239. "receive_amount" => $item['receive_amount'],
  1240. "imposed_amount" => $item['imposed_amount'],
  1241. "product_amount" => $item['product_amount'],
  1242. "total_sales_tax" => $item['total_sales_tax'],
  1243. "service_charge_amount" => $item['service_charge_amount'],
  1244. "total_annual_fee" => $item['total_annual_fee'],
  1245. "zue_coin" => $item['zue_coin'],
  1246. "zue_coin_amount" => $item['zue_coin_amount'],
  1247. "type" => $item['type'],
  1248. "store_id" => $item['store_id'],
  1249. "advisor_ids" => $item['advisor_ids'],
  1250. "remarks" => $item['remarks'],
  1251. "create_time" => $item['create_time'],
  1252. "advisers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1253. array_push($result, $item['adviser_1_name']);
  1254. if ($item['adviser_2_id'] > 0)
  1255. array_push($result, $item['adviser_2_name']);
  1256. return $result;
  1257. }, []))),
  1258. "teachers" => join('/', array_unique(array_reduce($item['products'], function ($result, $item) {
  1259. if ($item['teacher_1_id'] > 0)
  1260. array_push($result, $item['teacher_1_name']);
  1261. if ($item['teacher_2_id'] > 0)
  1262. array_push($result, $item['teacher_2_name']);
  1263. return $result;
  1264. }, [])))
  1265. ];
  1266. }, $items), $res['total']];
  1267. }
  1268. public function findByOrderNo($admin_id, $order_no)
  1269. {
  1270. $order = $this->orderModel->findByOrderNo($admin_id, $order_no);
  1271. if (!$order)
  1272. return $this->fail(lang("Order does not exist"));
  1273. return $this->ok($order);
  1274. }
  1275. public function findOrderByOrderId($order_id)
  1276. {
  1277. return $this->orderModel->findById($order_id);
  1278. }
  1279. public function fetchOrderPayments($order_id)
  1280. {
  1281. return $this->orderPaymentModel->findByOrderId($order_id);
  1282. }
  1283. }