OrderSubmitPreviewForm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: kai
  9. * @Date: 2021-03-02 09:50:20
  10. * @LastEditTime: 2021-04-23 13:39:19
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\order\OrderSubmitPreviewForm.php
  14. */
  15. namespace app\modules\client\models\v1\order;
  16. use app\constants\OptionSetting;
  17. use app\models\AboutArticle;
  18. use app\models\District;
  19. use app\models\FoodFlag;
  20. use app\models\FoodTableNumber;
  21. use app\models\Goods;
  22. use app\models\Md;
  23. use app\models\Option;
  24. use app\models\SaasUser;
  25. use app\models\Store;
  26. use app\models\User;
  27. use app\modules\admin\models\SettingForm;
  28. use app\plugins\adopt\models\AdoptSetting;
  29. use app\utils\Delivery\Delivery;
  30. use app\utils\Notice\NoticeAction;
  31. use app\utils\OrderNo;
  32. use app\utils\Tools;
  33. use yii\helpers\Json;
  34. use app\utils\RandDiscount;
  35. use app\models\Mch;
  36. class OrderSubmitPreviewForm extends OrderForm
  37. {
  38. public function rules()
  39. {
  40. return parent::rules();
  41. }
  42. public function filter_mch_list()
  43. {
  44. $mch_list = $this->_filter_mch_list();
  45. if($this->hasErrors()){
  46. return [
  47. 'code' => 1,
  48. 'msg' => array_shift($this->getFirstErrors()),
  49. ];
  50. }
  51. return [
  52. 'code' => 0,
  53. 'data' => $mch_list,
  54. ];
  55. }
  56. public function search()
  57. {
  58. if (!$this->validate())
  59. return [
  60. 'code' => 1,
  61. 'msg' => $this->getErrorSummary(false)[0],
  62. ];
  63. // HKG 新增判断商城打烊状态 开始
  64. if(Option::get(OptionSetting::MALL_STATUS,get_store_id(),'store',0)['value'] == 1)
  65. return [
  66. 'code' => 1,
  67. 'msg' => '商城已打烊~'
  68. ];
  69. // HKG 新增判断商城打烊状态 结束
  70. try {
  71. // 预生成订单号
  72. //$order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  73. // cache()->set('delivery_order_no_' . get_user_id(), $order_no);
  74. $mchList = $this->getMchListData();
  75. if (isset($mchList['code']) && $mchList['code'] === 1) {
  76. return $mchList;
  77. }
  78. $agreement_id = 0;
  79. $agreement_title = '';
  80. $adopt_order = false;
  81. foreach ($mchList as &$mch) {
  82. $order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  83. $goods_id = array_column($mch['goods_list'], 'goods_id');
  84. if ((!empty(input_params('flag_id')) || !empty(input_params('table_num')))) {
  85. $order_no = OrderNo::getOrderNo(OrderNo::ORDER_ALIPAY_FOOD);
  86. foreach($mch['goods_list'] as $uitem){
  87. $goods_id = array_unique(array_merge($goods_id, array_column($uitem['goods_list'], 'goods_id')));
  88. }
  89. }
  90. if ($mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_ADOPT) {
  91. $adopt_order = true;
  92. }
  93. sort($goods_id);
  94. $goods_id = implode(',', $goods_id);
  95. $goods_id = md5($goods_id);
  96. cache()->set('delivery_order_no_' . $goods_id . '_' . get_user_id(), $order_no);
  97. }
  98. // var_dump(json_encode($mchList));die;
  99. if(isset($mchList['code']) && $mchList['code'] == 1){
  100. return $mchList;
  101. }
  102. if ($adopt_order) {
  103. $adoptSetting = AdoptSetting::findOne(['store_id' => $this->store_id]);
  104. $agreement_id = $adoptSetting->agreement_id;
  105. $agreement_title = AboutArticle::find()->where([
  106. 'is_delete' => 0,
  107. 'id' => $adoptSetting->agreement_id,
  108. ])->select('name')->scalar() ?: '认养协议';
  109. }
  110. } catch(\Exception $e) {
  111. return [
  112. 'code' => 1,
  113. 'line' => $e->getLine(),
  114. 'msg' => $e->getMessage(),
  115. 'file' => $e->getFile(),
  116. ];
  117. }
  118. $food_pay_type = [];
  119. $md_info = [];
  120. $flag_id = 0;
  121. $tableInfo = [];
  122. $store = Store::findOne(get_store_id());
  123. $mchModel = Mch::findOne(get_mch_id());
  124. $md = Md::findOne(get_md_id());
  125. $food_pay_list = [];
  126. if (get_md_id() && (!empty(input_params('flag_id')) || !empty(input_params('table_num')))) {
  127. if (!empty(input_params('flag_id'))) {
  128. $flagInfo = FoodFlag::findOne(['id' => input_params('flag_id')]);
  129. } else {
  130. $flagInfo = FoodFlag::findOne(['store_id' => get_store_id(), 'md_id' => get_md_id(), 'status' => 0, 'table_num' => input_params('table_num')]);
  131. }
  132. $flag_id = $flagInfo->id;
  133. $flagType = $flagInfo->type;
  134. // 根据桌号 查询
  135. $tableInfo = FoodTableNumber::find()->where(['num' => $flagInfo->table_num,'store_id' => get_store_id(), 'md_id' => get_md_id(),'mch_id' => get_mch_id()])->one();
  136. if (get_md_id() > 0 && $md->is_single) {
  137. $food_payment = $md->food_payment;
  138. $food_paytype = $md->food_pay_type;
  139. } else if($mchModel){
  140. $food_payment = $mchModel->food_payment;
  141. $food_paytype = $mchModel->food_pay_type;
  142. } else {
  143. $food_payment = $store->food_payment;
  144. $food_paytype = $store->food_pay_type;
  145. }
  146. if (!empty($food_payment)) {
  147. $food_pay_type = Json::decode($food_payment);
  148. $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store')['value']);
  149. $is_food_ok = false;
  150. if (!empty($pay_type_list_json)) {
  151. $pay_type_list = Json::decode($pay_type_list_json['value']);
  152. $user_pay_list_json = Option::get('payment_' . get_user_id(), get_store_id(), 'user')['value'];
  153. if ($user_pay_list_json) {
  154. $user_pay_list_arr = Json::decode($user_pay_list_json);
  155. foreach ($pay_type_list as $key => &$value) {
  156. if (isset($user_pay_list_arr[$key]) && $user_pay_list_arr[$key]['value'] == 0 && $value['value'] == 1) {
  157. $value['value'] = 0;
  158. }
  159. }
  160. }
  161. foreach ($pay_type_list as $index => $pay_type) {
  162. if ($index == 'friend' && $pay_type['value'] == 1) {
  163. $is_food_ok = true;
  164. break;
  165. }
  166. }
  167. }
  168. foreach ($food_pay_type as &$value) {
  169. if ($value['key'] == 'friend') {
  170. if ($is_food_ok && $value['value'] == 1) {
  171. $value['value'] = 1;
  172. } else {
  173. $value['value'] = 0;
  174. }
  175. }
  176. if ($value['key'] == 'friend') {
  177. $value['payment'] = 7;
  178. }
  179. if ($value['key'] == 'alipay') {
  180. $value['payment'] = 4;
  181. }
  182. if ($value['key'] == 'wechat') {
  183. $value['payment'] = 1;
  184. }
  185. if ($value['key'] == 'huodao') {
  186. $value['payment'] = 2;
  187. }
  188. if ($value['key'] == 'bytedance') {
  189. $value['payment'] = 5;
  190. }
  191. }
  192. $food_pay_list = $food_pay_type;
  193. }
  194. foreach ($food_pay_list as $k => $val) {
  195. if ($val['payment'] == 4 && is_wechat_platform() || ($val['payment'] == 4 && intval($val['value']) === 0)) {
  196. unset($food_pay_list[$k]);
  197. }
  198. if ($val['payment'] == 1 && is_alipay_platform() || ($val['payment'] == 1 && intval($val['value']) === 0)) {
  199. unset($food_pay_list[$k]);
  200. }
  201. if ($val['payment'] == 7 && intval($val['value']) === 0) {
  202. unset($food_pay_list[$k]);
  203. }
  204. if ($val['payment'] == 2 && intval($val['value']) === 0) {
  205. unset($food_pay_list[$k]);
  206. }
  207. if ($val['payment'] == 5 && intval($val['value']) === 0) {
  208. unset($food_pay_list[$k]);
  209. }
  210. }
  211. $food_pay_list = array_values($food_pay_list);
  212. // 点餐支付方式
  213. $default_food_delivery = (int)Option::get('default_food_delivery', get_store_id(), 'store', 0)['value']; //到店自提
  214. if($mchModel){
  215. $default_food_delivery = (int)Option::get('default_food_delivery', get_mch_id(), 'mch', 0)['value'];
  216. }
  217. $food_pay_type = [
  218. 'food_pay_type' => $food_paytype,
  219. 'food_payment' => $food_pay_list,
  220. 'default_food_delivery' => $default_food_delivery
  221. ];
  222. // $food_pay_type = $food_pay_list;
  223. }
  224. if (get_md_id() > 0) {
  225. $province_name = District::findOne($md->province)->name;
  226. $city_name = District::findOne($md->city)->name;
  227. $district_name = District::findOne($md->district)->name;
  228. $md_info = [
  229. 'is_md' => 1,
  230. 'name' => $md->name,
  231. 'detail_address' => $province_name . $city_name . $district_name . $md->address,
  232. 'distance' => Tools::getDistance($this->latitude, $this->longitude, $md->latitude, $md->longitude),
  233. 'longitude' => $md->longitude,
  234. 'latitude' => $md->latitude,
  235. 'logo' => $md->cover_url
  236. ];
  237. } else {
  238. $province_name = District::findOne($store->province_id)->name;
  239. $city_name = District::findOne($store->city_id)->name;
  240. $district_name = District::findOne($store->district_id)->name;
  241. $lat = '';
  242. $long = '';
  243. $coordinate = explode(',', $store->coordinate);
  244. if ($coordinate) {
  245. $lat = $coordinate[0];
  246. $long = $coordinate[1];
  247. }
  248. $md_info = [
  249. 'is_md' => 0,
  250. 'name' => $store->name,
  251. 'detail_address' => $province_name . $city_name . $district_name . $store->address,
  252. 'distance' => Tools::getDistance($this->latitude, $this->longitude, $lat, $long),
  253. 'longitude' => $long,
  254. 'latitude' => $lat,
  255. 'logo' => $store->logo
  256. ];
  257. }
  258. if ($mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_ADOPT) {
  259. $template_id = NoticeAction::getSendTamplateId(['order_pay', 'adopt_mature', 'adopt_order_confirm_mature']);
  260. } else {
  261. $noticeType = ['order_pay', 'order_submit'];
  262. if($this->send == 'shop'){
  263. $noticeType[] = 'shop_arrive';
  264. }else{
  265. $noticeType[] = 'order_send';
  266. }
  267. $template_id = NoticeAction::getSendTamplateId($noticeType, is_h5() ? 'wxaapi' : 'miapp');
  268. }
  269. if ($this->is_worker) {
  270. goto is_worker;
  271. }
  272. //计算联盟券
  273. $take_price = 0;
  274. $send_price = 0;
  275. // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例
  276. // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => get_store_id()]);
  277. // if($this_store_send_profit_model->value > 0){
  278. // $basic_send_profit = $this_store_send_profit_model->value;
  279. // }else{
  280. // $basic_send_profit = $send_profit_model->value;
  281. // }
  282. foreach ($mchList[0]['goods_list'] as $arr) {
  283. //TODO 新的
  284. //计算返利
  285. $send_price += $arr['send_price'] ?? 0;
  286. $take_price += $arr['take_price'] ?? 0;
  287. }
  288. $user = User::findOne(get_user_id());
  289. $user_league_price = 0;
  290. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  291. if($saas_user){
  292. $user_league_price = $saas_user->league_price;
  293. }
  294. $show_saas_alliance_coupon = true;
  295. if (\Yii::$app->prod_is_dandianpu()) {
  296. $show_saas_alliance_coupon = false;
  297. }
  298. // // 随机立减
  299. // $mchList[0]['rand_discount_price'] = 0;
  300. // // 此处做缓存,用于解决前端短时间内请求多次订单预览接口
  301. // if (
  302. // $mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_NORMAL ||
  303. // $mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_VIRTUAL
  304. // ) {
  305. // $randDiscountCache = cache()->get('rand_discount_cache_' . get_user_id());
  306. // if ($randDiscountCache) {
  307. // $randDiscountOption = $randDiscountCache;
  308. // } else {
  309. // $randDiscountOption = RandDiscount::getOption(\get_store_id(), $mchList[0]['total_price']);
  310. // cache()->set('rand_discount_cache_' . get_user_id(), $randDiscountOption, 3);
  311. // }
  312. // cache()->set('rand_discount_cache2_' . get_user_id(), $randDiscountOption);
  313. // if ($randDiscountOption['is_open'] && (float)$randDiscountOption['rand'] > 0) {
  314. // $mchList[0]['rand_discount_price'] = $randDiscountOption['rand'];
  315. // }
  316. // }
  317. is_worker:
  318. $invoiceConf = [];
  319. $invoice_is_open = \app\modules\admin\models\InvoiceForm::is_open($store->id, $invoiceConf);
  320. return [
  321. 'code' => 0,
  322. 'msg' => 'OOKK',
  323. 'order_no' => $order_no,
  324. 'data' => [
  325. 'user' => get_user(),
  326. 'user_integralPrice' => get_user()->integralPrice,
  327. 'invoice_is_open' => $invoice_is_open,
  328. 'invoiceConf' => $invoiceConf,
  329. 'goods_send_profit' => 0,
  330. 'store_send_profit' => 0,
  331. 'flag_id' => intval($flag_id),
  332. 'flag_type' => intval($flagType),
  333. 'table_info' => $tableInfo,
  334. 'take_price' => sprintf('%.2f',$take_price ?? 0),
  335. 'send_price' => sprintf('%.2f', $send_price ?? 0),
  336. 'user_league_price' => !$this->is_worker ? $user_league_price ?? 0 : 0,
  337. 'md_info' => !$this->is_worker ? $md_info : null,
  338. 'food_pay' => $food_pay_type,
  339. 'pay_type_list' => $food_pay_list ?: $this->getPayTypeList(),
  340. 'address' => $this->address,
  341. 'worker' => $this->worker,
  342. 'level' => !$this->is_worker ? $this->getLevelData() : null,
  343. 'mch_list' => $mchList,
  344. 'integral'=> $this->integral,
  345. 'goods_card_list' => !$this->is_worker ? $this->goodsCardList(): null,
  346. 'template_id' => $template_id,
  347. 'show_saas_alliance_coupon' => !$this->is_worker ? $show_saas_alliance_coupon : false,
  348. 'travel' => $this->is_worker ? $this->travel : null,
  349. 'agreement_id' => $agreement_id,
  350. 'agreement_title' => $agreement_title,
  351. ],
  352. ];
  353. }
  354. }