OrderService-backups.php 69 KB

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