OrderSubmitForm.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\pt\order;
  8. use app\models\Address;
  9. use app\models\Cart;
  10. use app\models\common\CommonOrder;
  11. use app\models\FoodCart;
  12. use app\models\FoodFlag;
  13. use app\models\Goods;
  14. use app\models\GoodsBook;
  15. use app\models\IntegralAppreciationGoods;
  16. use app\models\IntegralAppreciationUser;
  17. use app\models\IntegralAppreciationUserIntegralLog;
  18. use app\models\Level;
  19. use app\models\LevelOrder;
  20. use app\models\Md;
  21. use app\models\MdProfit;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\PtActivity;
  25. use app\models\PtActivityOrder;
  26. use app\models\PtActivityOrderDetail;
  27. use app\models\SeckillActivity;
  28. use app\models\SeckillActivityGoods;
  29. use app\models\SeckillActivityOrderLog;
  30. use app\models\Store;
  31. use app\models\User;
  32. use app\models\UserCoupon;
  33. use app\models\Option;
  34. use app\models\AccountLog;
  35. use app\models\VerifyCardSale;
  36. use app\models\VideoGoodsList;
  37. use app\models\VideoGoodsSetting;
  38. use app\models\VideoGoodsShare;
  39. use app\plugins\adopt\models\AdoptGoods;
  40. use app\plugins\adopt\models\AdoptOrderInfo;
  41. use app\utils\CloudPrint;
  42. use app\utils\Delivery\Delivery;
  43. use app\utils\Delivery\Alipay\ADelivery;
  44. use app\utils\Notice\NoticeSend;
  45. use app\utils\OrderNo;
  46. use app\utils\PrintOrder;
  47. use app\utils\Tools;
  48. use yii\helpers\Json;
  49. class OrderSubmitForm extends OrderForm
  50. {
  51. public $user;
  52. public $payment;
  53. public $use_integral;
  54. public $buy_level_id;
  55. public $adopt_name;
  56. public $adopt_mobile;
  57. public function rules()
  58. {
  59. $rules = [
  60. [['payment', 'use_integral', 'buy_level_id'], 'integer'],
  61. [['adopt_name', 'adopt_mobile'], 'string']
  62. ];
  63. return array_merge(parent::rules(), $rules);
  64. }
  65. public function preOrder() {
  66. if (!$this->validate())
  67. return [
  68. 'code' => 1,
  69. 'msg' => $this->getErrorSummary(false)[0],
  70. ];
  71. try {
  72. $mch_list = $this->getMchListData(true);
  73. $goods_list = [];
  74. foreach ($mch_list as $mch) {
  75. foreach ($mch['goods_list'] as $goods) {
  76. $goods_list[] = [
  77. 'name' => $goods['goods_name'],
  78. 'price' => $goods['price'],
  79. 'pic' => $goods['goods_pic'],
  80. 'num' => $goods['num'],
  81. ];
  82. }
  83. }
  84. $order_no = cache()->get('delivery_order_no_' . get_user_id());
  85. if(is_alipay_platform()){
  86. $this->address_id || $this->address_id = 220;
  87. $res = ADelivery::createOrder(1, $this->store_id, get_mini_id(), get_saas_user()->ali_user_id, $goods_list, $order_no, $this->address_id);;
  88. return $res;
  89. }
  90. return Delivery::preAddOrder($goods_list, $order_no);
  91. } catch (\Exception $e) {
  92. return [
  93. 'code' => 1,
  94. 'msg' => $e->getMessage()
  95. ];
  96. }
  97. }
  98. public function save()
  99. {
  100. if (!$this->validate())
  101. return [
  102. 'code' => 1,
  103. 'msg' => $this->getErrorSummary(false)[0],
  104. ];
  105. try {
  106. $mchListData = $this->getMchListData(true);
  107. } catch (\Exception $e) {
  108. return [
  109. 'code' => 1,
  110. 'msg' => $e->getMessage()
  111. ];
  112. }
  113. $order_id_list = [];
  114. $md_select = 0;
  115. /**
  116. * @var $address Address
  117. */
  118. $address = (object)($this->address);
  119. $t = \Yii::$app->db->beginTransaction();
  120. foreach ($mchListData as &$mch) {
  121. if (isset($mch['use_integral']) && !$mch['use_integral']) {
  122. $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0];
  123. $mch['give'] = 0;
  124. }
  125. $mch_goods = $mch['goods_list'];
  126. if ($mch_goods) {
  127. $checkMchData = $this->checkMchData($mch);
  128. if ($checkMchData['code'] == 1) {
  129. $t->rollBack();
  130. return $checkMchData;
  131. }
  132. $level_price = 0;
  133. $total_price = 0;
  134. $is_have_express = false;
  135. $is_have_shop = false;
  136. $is_have_delivery = 0;
  137. if ($mch['send'] == 'express') {
  138. $is_have_express = true;
  139. }
  140. if ($mch['send'] == 'shop') {// || get_md_id()
  141. $is_have_shop = true;
  142. // if (!empty($mch['shop']) && !empty($mch['shop']['id'])) {
  143. // $md_select = $mch['shop']['id'];
  144. // }
  145. $md = Md::findOne(['id' => get_md_id(), 'is_delete' => 0, 'store_id' => $this->store_id]);
  146. if (empty($md)) {
  147. return [
  148. 'code' => 1,
  149. 'msg' => '到店自提请选择对应门店'
  150. ];
  151. }
  152. }
  153. if (!in_array($mch['send'], ['express', 'shop'])) {
  154. return [
  155. 'code' => 1,
  156. 'msg' => '请选择快递或到店自提'
  157. ];
  158. }
  159. foreach ($mch_goods as $v) {
  160. $level_price += $v['level_price'];
  161. $total_price += isset($v['total_price']) ? $v['total_price'] : $v['price'];
  162. }
  163. $mch['level_price'] = $level_price;
  164. $mch['level_diff_price'] = sprintf('%.2f',$mch['next_level']['diff_price']);
  165. $mch['total_price'] = $total_price;
  166. // $mch['total_integral'] = $total_integral; //积分兑换
  167. $payPrice = $this->getPayPrice($mch);
  168. $order = new PtActivityOrder();
  169. // 订单来源
  170. if (is_h5()) {
  171. $order->order_origin = Order::ORDER_SOURCE_WEB;
  172. }
  173. $order->store_id = $this->store_id;
  174. $order->user_id = $this->user_id;
  175. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_PT);
  176. $order->pay_price = $payPrice;
  177. $order->pt_number = $mch['pt_number'];
  178. if ($this->head_integral > 0) {
  179. if ($this->head_integral > get_user()->integral) {
  180. return [
  181. 'code' => 1,
  182. 'msg' => '开团积分不足'
  183. ];
  184. }
  185. $order->head_integral = $this->head_integral;
  186. }
  187. // 订单类型
  188. // 是否是平台小程序订单
  189. if (get_md_id()) {
  190. $order->md_id = get_md_id();
  191. }
  192. if($md_select > 0){
  193. $order->md_id = $md_select;
  194. }
  195. $order->created_at = time();
  196. $order->content = $mch['content'] ?? '';
  197. $order->is_offline = $is_have_shop ? 1 : 0;
  198. $order->remark = $mch['remark'];
  199. if ($this->payment == 2) {
  200. $order->pay_type = 2;
  201. $order->is_pay = 0;
  202. }
  203. if ($this->payment == 3) {
  204. $order->pay_type = 3;
  205. $order->is_pay = 0;
  206. }
  207. // 线下支付
  208. if ($this->payment == 6) {
  209. $order->pay_type = 6;
  210. $order->is_pay = 0;
  211. }
  212. if ($is_have_express || get_md_id()) {
  213. $order->address = $address->province . $address->city . $address->district . $address->detail;
  214. $order->mobile = $address->mobile;
  215. $order->name = $address->name;
  216. $order->province_id = $address->province_id;
  217. $order->city_id = $address->city_id;
  218. $order->district_id = $address->district_id;
  219. $order->address_data = json_encode([
  220. 'province' => trim($address->province),
  221. 'city' => trim($address->city),
  222. 'district' => trim($address->district),
  223. 'detail' => trim($address->detail),
  224. 'latitude' => $address->latitude,
  225. 'longitude' => $address->longitude,
  226. ], JSON_UNESCAPED_UNICODE);
  227. $order->express_price = $mch['express_price'];
  228. $order->total_price = $mch['total_price'] + $mch['express_price'];
  229. }
  230. if ($is_have_shop) {
  231. if (get_md_id()) {
  232. $md_info = Md::findOne(get_md_id());
  233. $order->province_id = $md_info->province;
  234. $order->city_id = $md_info->city;
  235. $order->district_id = $md_info->district;
  236. }
  237. $order->express_price = 0;
  238. $order->total_price = $mch['total_price'];
  239. }
  240. //获取代理配置信息
  241. $store = Store::findOne($this->store_id);
  242. //支付金额 * 店铺让利比例 * 代理商分润比例
  243. // $admin_profit = Option::get('admin_profit', 0, 'saas', 0)['value'];
  244. if (!empty($store->transfer_profit)) {
  245. $order->ag_rebate = $payPrice * ($store->transfer_profit / 100);
  246. }
  247. $order->saas_id = 0;
  248. if ($order->save()) {
  249. // 绑定上下级
  250. $user = User::findOne($this->user_id);
  251. if ($user && $user->parent_user_id) {
  252. CommonOrder::saveParentId($user->parent_user_id);
  253. }
  254. // 处理订单生成之后其他相关数据
  255. $orderRes = $this->insertData($mch, $order);
  256. if ($orderRes['code'] == 1) {
  257. $t->rollBack();
  258. return $orderRes;
  259. }
  260. $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0, 0, $order['mch_id']);
  261. $printer_order->print_order();
  262. // 订单提交完成发送消息
  263. NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name'], 0, $order->store_id);
  264. $order_id_list[] = $order->id;
  265. } else {
  266. $t->rollBack();
  267. return [
  268. 'code' => 0,
  269. 'msg' => implode(';', array_values($order->firstErrors))
  270. ];
  271. }
  272. }
  273. }
  274. if (count($order_id_list) > 0) {
  275. $t->commit();
  276. $store = Store::findOne($this->store_id);
  277. $order_id = $order_id_list[0];
  278. CloudPrint::doPrint($order_id, 0, $this->store_id);
  279. return [
  280. 'code' => 0,
  281. 'msg' => '订单提交成功',
  282. 'data' => (object)[
  283. 'order_id' => $order_id_list[0],
  284. 'add_time' => $order->created_at,
  285. 'order_price'=> $order->pay_price,
  286. 'douyin_url' => $store->douyin_url,
  287. ],
  288. ];
  289. } else {
  290. $t->rollBack();
  291. return [
  292. 'order_list' => $order_id_list,
  293. 'order_price'=> $order->pay_price
  294. ];
  295. }
  296. }
  297. private function reduceIntegral($order) {
  298. $integral = Json::decode($order->integral);
  299. // 减去当前用户账户积分
  300. if ($integral['forehead_integral'] > 0) {
  301. $user = User::findOne($order->user_id);
  302. $before = $user->integral;
  303. $user->integral -= $integral['forehead_integral'];
  304. if ($user->save()) {
  305. $log = new AccountLog();
  306. $log->store_id = $order->store_id;
  307. $log->user_id = $user->id;
  308. $log->type = AccountLog::TYPE_INTEGRAL;
  309. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  310. $log->amount = $integral['forehead_integral'];
  311. $log->desc = "订单积分抵扣支付,订单号为:{$order->order_no}。";
  312. $log->before = $before;
  313. $log->after = $user->integral;
  314. $log->operator = '';
  315. $log->operator_id = 0;
  316. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  317. $log->created_at = time();
  318. $log->order_id = $order->id;
  319. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  320. $log->save();
  321. }
  322. }
  323. }
  324. public function getNo($attrStr,$searchAttrIds){
  325. $attr = json_decode($attrStr,true);
  326. foreach ($attr as $item){
  327. $attrIds = array_column($item['attr_list'],'attr_id');
  328. if ($attrIds == $searchAttrIds){
  329. return $item['no'];
  330. }
  331. }
  332. }
  333. // 获得实际支付金额
  334. private function getPayPrice($mch)
  335. {
  336. $goods_list = $mch['goods_list'];
  337. $payPrice = $mch['level_price'];
  338. if (isset($mch['use_integral']) && $mch['use_integral']) {
  339. $payPrice -= $mch['integral']['forehead'];
  340. }
  341. if (isset($mch['picker_coupon'])) {
  342. $pickerCoupon = $mch['picker_coupon'];
  343. if ($pickerCoupon['sub_price'] > 0) {
  344. $coupon_price = 0;
  345. if ($pickerCoupon['appoint_type'] == 1 && $pickerCoupon['cat_id_list'] != null) {
  346. foreach ($goods_list as $goods) {
  347. foreach ($goods['cat_id'] as $v1) {
  348. if (in_array($v1, $pickerCoupon['cat_id_list'])) {
  349. $coupon_price += floatval($goods['level_price']);
  350. break;
  351. };
  352. };
  353. }
  354. } else if ($pickerCoupon['appoint_type'] == 2 && $pickerCoupon['goods_id_list'] != null) {
  355. foreach ($goods_list as $goods) {
  356. if (in_array($goods['goods_id'], $pickerCoupon['goods_id_list'])) {
  357. $coupon_price += floatval($goods['level_price']);
  358. };
  359. }
  360. };
  361. if ($pickerCoupon['sub_price'] > $coupon_price && $coupon_price > 0) {
  362. $payPrice -= $coupon_price;
  363. } else {
  364. $payPrice -= $pickerCoupon['sub_price'];
  365. }
  366. }
  367. }
  368. // Todo 此处有待确认算法是否正确 by Syan
  369. // $payPrice = $payPrice > 0 ? $payPrice : 0.01;
  370. if ($mch['express_price'] > 0) {
  371. if (isset($mch['send']) && $mch['send'] == 'express') {
  372. $payPrice += $mch['express_price'];
  373. } else {
  374. foreach ($goods_list as $goods) {
  375. if (isset($goods['send']) && $goods['send'] == 'express') {
  376. $payPrice += $mch['express_price'];
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. if ($this->buy_level_id) {
  383. $level = Level::findOne($this->buy_level_id);
  384. $payPrice += ($level->price * 1);
  385. }
  386. return $payPrice > 0 ? $payPrice : 0;
  387. }
  388. // 检查数据
  389. private function checkMchData($mch)
  390. {
  391. if (empty($mch['goods_list'])) {
  392. return [
  393. 'code' => 1,
  394. 'msg' => '商品不存在或已删除'
  395. ];
  396. }
  397. $checkFormData = $this->checkFormData($mch);
  398. if ($checkFormData['code'] == 1) {
  399. return $checkFormData;
  400. }
  401. $checkSendType = $this->checkSendType($mch);
  402. if ($checkSendType['code'] == 1) {
  403. return $checkSendType;
  404. }
  405. // $checkCoupon = $this->checkCoupon($mch);
  406. // if ($checkCoupon['code'] == 1) {
  407. // return $checkCoupon;
  408. // }
  409. // $checkGoods = $this->checkGoods($mch);
  410. // if ($checkGoods['code'] == 1) {
  411. // return $checkGoods;
  412. // }
  413. return ['code' => 0, 'msg' => 'success'];
  414. }
  415. // 检测优惠券是否可使用
  416. private function checkCoupon($mch)
  417. {
  418. if (empty($mch['picker_coupon'])) {
  419. return [
  420. 'code' => 0,
  421. 'msg' => ''
  422. ];
  423. }
  424. $ok = false;
  425. foreach ($mch['coupon_list'] as $item) {
  426. if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) {
  427. $ok = true;
  428. }
  429. }
  430. if (!$ok) {
  431. return [
  432. 'code' => 1,
  433. 'msg' => '该优惠券已过期'
  434. ];
  435. } else {
  436. return [
  437. 'code' => 0,
  438. 'msg' => ''
  439. ];
  440. }
  441. }
  442. // 检测发货方式
  443. private function checkSendType($mch)
  444. {
  445. if ($mch['mch_id'] == 0) {
  446. if (!$this->address) {
  447. return [
  448. 'coe' => 1,
  449. 'msg' => '收货地址不存在'
  450. ];
  451. }
  452. if ($mch['offer_rule'] && $mch['offer_rule']['is_allowed'] == 1) {
  453. return [
  454. 'code' => 1,
  455. 'msg' => $mch['offer_rule']['msg']
  456. ];
  457. }
  458. if ($mch['is_area'] == 1 && $mch['send'] == 'express') {
  459. return [
  460. 'code' => 1,
  461. 'msg' => '所选地区自营商品暂时无货'
  462. ];
  463. }
  464. }
  465. return ['code' => 0, 'msg' => ''];
  466. }
  467. // 检测自定义表单
  468. private function checkFormData($mch)
  469. {
  470. foreach ($mch['goods_list'] as $goods) {
  471. if (isset($goods['form']['code'])) {
  472. return $goods['form'];
  473. }
  474. if (!isset($goods['form'])) {
  475. continue;
  476. }
  477. $form = $goods['form'];
  478. if ($form['is_form'] == 1) {
  479. $form_list = $form['list'];
  480. foreach ($form_list as $index => $value) {
  481. if ($value['required'] == 1) {
  482. if (in_array($value['type'], ['radio', 'checkbox'])) {
  483. $is_true = false;
  484. foreach ($value['default_list'] as $k => $v) {
  485. if ($v['checked'] == true) {
  486. $is_true = true;
  487. }
  488. }
  489. if (!$is_true) {
  490. return [
  491. 'code' => 1,
  492. 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
  493. 'name' => $value['name']
  494. ];
  495. }
  496. } else {
  497. if (empty($value['default']) && strlen($value['default']) == 0) {
  498. return [
  499. 'code' => 1,
  500. 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
  501. 'name' => $value['name']
  502. ];
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. return [
  510. 'code' => 0,
  511. 'msg' => ''
  512. ];
  513. }
  514. // 检测商品相关
  515. private function checkGoods($mch)
  516. {
  517. \Yii::warning($mch);
  518. foreach ($mch['goods_list'] as $goods) {
  519. $attr_id_list = [];
  520. foreach ($goods['attr_list'] as $item) {
  521. array_push($attr_id_list, $item['attr_id']);
  522. }
  523. $_goods = Goods::find()->where('id = ' . $goods['goods_id'] . ' for update')->one();
  524. $is_install = $this->goodsIsInstall($goods['goods_id']);
  525. if ($is_install !== 2) {
  526. $store = Store::findOne(get_store_id());
  527. $store_address = [
  528. 'province_id' => $store->province_id,
  529. 'city_id' => $store->city_id,
  530. 'district_id' => $store->district_id,
  531. ];
  532. $address = $is_install === 1 ? $this->address : $store_address;
  533. $result = $this->goodsIsGoodsAgent($address, $goods['goods_id']);
  534. if ($result['code'] !== 0) {
  535. return $result;
  536. }
  537. }
  538. }
  539. return ['code' => 0, 'msg' => ''];
  540. }
  541. public function getOrderNo()
  542. {
  543. $order_no = null;
  544. while (true) {
  545. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  546. $exist_order_no = Order::find()->where(['order_no' => $order_no])->exists();
  547. if (!$exist_order_no) {
  548. break;
  549. }
  550. }
  551. return $order_no;
  552. }
  553. // 优惠券可优惠的商品总额计算
  554. private function pickerGoods(&$mch)
  555. {
  556. $totalPrice = 0;
  557. $pickerCoupon = $mch['picker_coupon'];
  558. if (empty($pickerCoupon)) {
  559. return;
  560. }
  561. foreach ($mch['goods_list'] as $item) {
  562. if ($pickerCoupon['appoint_type'] == 1) {
  563. if ($pickerCoupon['cat_id_list'] !== null) {
  564. $catIdList = $pickerCoupon['cat_id_list'];
  565. if (array_intersect($item['cat_id'], $catIdList)) {
  566. $totalPrice += $item['price'];
  567. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  568. }
  569. } else {
  570. $totalPrice += $item['price'];
  571. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  572. }
  573. } else if ($pickerCoupon['appoint_type'] == 2) {
  574. if ($pickerCoupon['goods_id_list'] !== null) {
  575. $goodsIdList = $pickerCoupon['goods_id_list'];
  576. if (in_array($item['goods_id'], $goodsIdList)) {
  577. $totalPrice += $item['price'];
  578. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  579. }
  580. } else {
  581. $totalPrice += $item['price'];
  582. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  583. }
  584. } else {
  585. $totalPrice += $item['price'];
  586. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  587. }
  588. }
  589. $mch['picker_coupon']['total_price'] = $totalPrice;
  590. }
  591. /**
  592. * @param $mch
  593. * @param $order PtActivityOrder
  594. * @return array
  595. */
  596. private function insertData($mch, $order)
  597. {
  598. // 计算商品相关
  599. $goods_list = $mch['goods_list'];
  600. $goodsPrice = 0;
  601. // 计算商品分润
  602. $goods_profit = 0;
  603. foreach ($goods_list as $goods) {
  604. // if ($goods['product_type'] == Goods::GOODS_TYPE_ADOPT) {
  605. // $adopt_order_info = new AdoptOrderInfo();
  606. // $adopt_order_info->name = $this->adopt_name;
  607. // $adopt_order_info->mobile = $this->adopt_mobile;
  608. // $adopt_order_info->order_id = $order->id;
  609. // $adopt_order_info->store_id = $this->store_id;
  610. // $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods['id']])->one();
  611. // $adopt_order_info->mature_time = time() + $adopt_goods->growth_cycle * 86400;
  612. //
  613. // if (!$adopt_order_info->save()) {
  614. // return [
  615. // 'code' => 1,
  616. // 'msg' => '订单提交失败,请稍后再重试',
  617. // 'error' => $adopt_order_info->errors
  618. // ];
  619. // }
  620. // }
  621. // 存入下单表单
  622. if ($goods['form'] && $goods['form']['is_form'] == 1) {
  623. foreach ($goods['form']['list'] as $index => $value) {
  624. $order_form = new \app\models\OrderForm();
  625. $order_form->store_id = $this->store_id;
  626. $order_form->order_id = $order->id;
  627. $order_form->goods_id = $goods['id'];
  628. $order_form->key = $value['name'];
  629. $order_form->value = $value['default'];
  630. $order_form->type = $value['type'];
  631. $order_form->is_delete = 0;
  632. $order_form->save();
  633. }
  634. }
  635. // 删除购物车
  636. if (isset($goods['cart_id'])) {
  637. Cart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]);
  638. }
  639. $order_detail = new PtActivityOrderDetail();
  640. $order_detail->order_id = $order->id;
  641. $order_detail->goods_id = $goods['goods_id'];
  642. $order_detail->num = $goods['num'];
  643. $order_detail->is_level = 0;
  644. $order_detail->activity_id = $this->pt_activity_id;
  645. $payPrice = $goods['level_price'];
  646. $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0;
  647. $goodsPrice += $payPrice;
  648. $orderPrice = floatval($order->pay_price) - floatval($order->express_price);
  649. if ($goodsPrice > $orderPrice) {
  650. $payPrice = $payPrice - ($goodsPrice - $orderPrice);
  651. $goodsPrice = $orderPrice;
  652. }
  653. $order_detail->total_price = $payPrice;
  654. // 计算单个商品可分红金额
  655. $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : floatval($goods['rate']) * $goods['num'];
  656. $goods_profit += $profit;
  657. $order_detail->profit = $profit;
  658. $order_detail->is_delete = 0;
  659. $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE);
  660. $goods_info = Goods::findOne($goods['goods_id']);
  661. $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic;
  662. $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name'];
  663. $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]);
  664. if ($goods['give'] > 0) {
  665. $order_detail->integral = $goods['give'];
  666. } else {
  667. $order_detail->integral = 0;
  668. }
  669. $order_detail->delivery_type = $mch['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : OrderDetail::GOODS_DELIVERY_SHOP;
  670. $attr_id_list = [];
  671. foreach ($goods['attr_list'] as $item) {
  672. array_push($attr_id_list, $item['attr_id']);
  673. }
  674. //增值积分下单扣减用户积分
  675. /*$integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goods['goods_id'], 'is_delete' => 0]);
  676. if ($integralAppreciationGoods && $integralAppreciationGoods->integral_amount > 0) {
  677. $integral_appreciation_setting = Option::get('integral_appreciation_setting', $order->store_id, 'integral_appreciation')['value'];
  678. $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
  679. $integral_custom_name = $integral_appreciation_setting['integral_custom_name'] ?: '增值积分';
  680. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $this->user_id]);
  681. if (!$integralAppreciationUser) {
  682. return [
  683. 'code' => 1,
  684. 'msg' => "用户{$integral_custom_name}数量不足"
  685. ];
  686. }
  687. $integral_amount = bcmul($integralAppreciationGoods->integral_amount, $goods['num'], 2);
  688. if ($integral_amount > $integralAppreciationUser->integral) {
  689. return [
  690. 'code' => 1,
  691. 'msg' => "用户{$integral_custom_name}数量不足"
  692. ];
  693. }
  694. $integralAppreciationUser->integral = bcsub($integralAppreciationUser->integral, $integral_amount, 2);
  695. $integralAppreciationUser->save();
  696. $result = IntegralAppreciationUserIntegralLog::saveIntegralLog(
  697. $integralAppreciationUser->id,
  698. $integral_amount,
  699. IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
  700. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_BUY_GOODS, "购买商品 订单号{$order->order_no}", $order->id,
  701. IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT);
  702. if ($result['code']) {
  703. return $result;
  704. }
  705. }*/
  706. if (!$order_detail->save()) {
  707. return [
  708. 'code' => 1,
  709. 'msg' => '订单提交失败,请稍后再重试',
  710. 'error' => $order_detail->errors[0]
  711. ];
  712. }
  713. }
  714. // goods_profit 计算之后存入订单表中
  715. $order->profit = $goods_profit;
  716. $order->save();
  717. // 计算门店收益
  718. if ($order->is_offline) {
  719. // MdProfit::handleProfit($order->id, $order->total_price, $order->total_price, $order->md_id);
  720. }
  721. return [
  722. 'code' => 0,
  723. 'msg' => ''
  724. ];
  725. }
  726. public function getBookPrice($payPrice, $mch_goods) {
  727. $price = $payPrice;
  728. foreach ($mch_goods as $goods) {
  729. if (0 && $goods['product_type'] == 1) {
  730. // $goods_book = GoodsBook::findOne(['goods_id' => $goods['id']]);
  731. // $date_book = Json::decode($goods_book->date_book);
  732. // $book_config_data = array_column($date_book, NULL, 'date');
  733. // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'price'));
  734. // $num_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num'));
  735. $price = 0;
  736. if (empty($goods['book'])) {
  737. return [
  738. 'code' => 1,
  739. 'msg' => '预约日期数据为空'
  740. ];
  741. }
  742. foreach ($goods['book'] as $value) {
  743. // if ($num_config[$value['date']] < $goods['num']) {
  744. // return [
  745. // 'code' => 1,
  746. // 'msg' => '选中日期' . $value['date'] . '内暂无房源'
  747. // ];
  748. // }
  749. // $price += $data_config[$value['date']];
  750. $price += $goodsInfo['price'];
  751. }
  752. $price = $price * $goods['num'];
  753. }
  754. if ($goods['product_type'] == 2) {
  755. $price = 0;
  756. $goods_book = GoodsBook::findOne(['goods_id' => $goods['id']]);
  757. $service_book = Json::decode($goods_book->service_book);
  758. //$service_book = $service_book['data'];
  759. $date_data = $this->getDateByInterval(7);
  760. $new_arr = [
  761. 'data' => []
  762. ];
  763. foreach ($date_data as $index => $datum) {
  764. $new_arr['data'][$index]['date'] = $datum;
  765. $new_arr['data'][$index]['time'] = $service_book['data'][0]['time'];
  766. }
  767. if (!$new_arr) {
  768. return [
  769. 'code' => 1,
  770. 'msg' => '数据异常'
  771. ];
  772. }
  773. $price = 0;
  774. $service_book = $new_arr['data'];
  775. $service_book_date = array_column($service_book, NULL, 'date');
  776. $service_book_time = $service_book_date[$goods['service']['date']]['time'];
  777. foreach ($service_book_time as $m) {
  778. foreach ($m['times'] as $n) {
  779. if ($n['time'] == $goods['service']['time']) {
  780. $price = $goods['service']['price'] * $goods['num'];
  781. break;
  782. }
  783. }
  784. if ($price > 0) {
  785. break;
  786. }
  787. }
  788. }
  789. }
  790. \Yii::error($price);
  791. return $price;
  792. }
  793. /**
  794. * 检查当前时间是否为指定时间范围内
  795. * @param $begin 20:00:00
  796. * @param $end 08:00:00
  797. * @param $date
  798. * @return bool true 时间范围内
  799. */
  800. private function checkTimeBeginEnd ($begin, $end, $date = null)
  801. {
  802. if (empty($begin) && empty($end)) {
  803. return true;//
  804. }
  805. $date = is_null ($date) ? date ('H:i:s') : $date;
  806. $curTime = strtotime ($date); // 当前时分
  807. $assignTime1 = strtotime ($begin); // 获得指定分钟时间戳,00:00
  808. $assignTime2 = strtotime ($end); // 获得指定分钟时间戳,01:00
  809. $result = false;
  810. if ($assignTime1 > $assignTime2) {
  811. //跨24点
  812. if ($curTime > $assignTime1 || $curTime < $assignTime2) {
  813. $result = true;
  814. }
  815. }
  816. if ($curTime > $assignTime1 && $curTime < $assignTime2) {
  817. $result = true;
  818. }
  819. if ($assignTime1 == $assignTime2) {
  820. $result = true;
  821. }
  822. return $result;
  823. }
  824. }