| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * @Author: kai
- * @Date: 2021-03-02 09:50:20
- * @LastEditTime: 2021-04-23 13:39:19
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \admin_php\modules\client\models\v1\order\OrderSubmitPreviewForm.php
- */
- namespace app\modules\client\models\v1\order;
- use app\constants\OptionSetting;
- use app\models\AboutArticle;
- use app\models\District;
- use app\models\FoodFlag;
- use app\models\FoodTableNumber;
- use app\models\Goods;
- use app\models\Md;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\User;
- use app\modules\admin\models\SettingForm;
- use app\plugins\adopt\models\AdoptSetting;
- use app\utils\Delivery\Delivery;
- use app\utils\Notice\NoticeAction;
- use app\utils\OrderNo;
- use app\utils\Tools;
- use yii\helpers\Json;
- use app\utils\RandDiscount;
- use app\models\Mch;
- class OrderSubmitPreviewForm extends OrderForm
- {
- public function rules()
- {
- return parent::rules();
- }
- public function filter_mch_list()
- {
- $mch_list = $this->_filter_mch_list();
- if($this->hasErrors()){
- return [
- 'code' => 1,
- 'msg' => array_shift($this->getFirstErrors()),
- ];
- }
- return [
- 'code' => 0,
- 'data' => $mch_list,
- ];
- }
- public function search()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- // HKG 新增判断商城打烊状态 开始
- if(Option::get(OptionSetting::MALL_STATUS,get_store_id(),'store',0)['value'] == 1)
- return [
- 'code' => 1,
- 'msg' => '商城已打烊~'
- ];
- // HKG 新增判断商城打烊状态 结束
- try {
- // 预生成订单号
- //$order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
- // cache()->set('delivery_order_no_' . get_user_id(), $order_no);
- $mchList = $this->getMchListData();
- if (isset($mchList['code']) && $mchList['code'] === 1) {
- return $mchList;
- }
- $agreement_id = 0;
- $agreement_title = '';
- $adopt_order = false;
- foreach ($mchList as &$mch) {
- $order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
- $goods_id = array_column($mch['goods_list'], 'goods_id');
- if ((!empty(input_params('flag_id')) || !empty(input_params('table_num')))) {
- $order_no = OrderNo::getOrderNo(OrderNo::ORDER_ALIPAY_FOOD);
- foreach($mch['goods_list'] as $uitem){
- $goods_id = array_unique(array_merge($goods_id, array_column($uitem['goods_list'], 'goods_id')));
- }
- }
- if ($mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_ADOPT) {
- $adopt_order = true;
- }
- sort($goods_id);
- $goods_id = implode(',', $goods_id);
- $goods_id = md5($goods_id);
- cache()->set('delivery_order_no_' . $goods_id . '_' . get_user_id(), $order_no);
- }
- // var_dump(json_encode($mchList));die;
- if(isset($mchList['code']) && $mchList['code'] == 1){
- return $mchList;
- }
- if ($adopt_order) {
- $adoptSetting = AdoptSetting::findOne(['store_id' => $this->store_id]);
- $agreement_id = $adoptSetting->agreement_id;
- $agreement_title = AboutArticle::find()->where([
- 'is_delete' => 0,
- 'id' => $adoptSetting->agreement_id,
- ])->select('name')->scalar() ?: '认养协议';
- }
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- ];
- }
-
- $food_pay_type = [];
- $md_info = [];
- $flag_id = 0;
- $tableInfo = [];
- $store = Store::findOne(get_store_id());
- $mchModel = Mch::findOne(get_mch_id());
- $md = Md::findOne(get_md_id());
- $food_pay_list = [];
- if (get_md_id() && (!empty(input_params('flag_id')) || !empty(input_params('table_num')))) {
- if (!empty(input_params('flag_id'))) {
- $flagInfo = FoodFlag::findOne(['id' => input_params('flag_id')]);
- } else {
- $flagInfo = FoodFlag::findOne(['store_id' => get_store_id(), 'md_id' => get_md_id(), 'status' => 0, 'table_num' => input_params('table_num')]);
- }
- $flag_id = $flagInfo->id;
- $flagType = $flagInfo->type;
- // 根据桌号 查询
- $tableInfo = FoodTableNumber::find()->where(['num' => $flagInfo->table_num,'store_id' => get_store_id(), 'md_id' => get_md_id(),'mch_id' => get_mch_id()])->one();
- if (get_md_id() > 0 && $md->is_single) {
- $food_payment = $md->food_payment;
- $food_paytype = $md->food_pay_type;
- } else if($mchModel){
- $food_payment = $mchModel->food_payment;
- $food_paytype = $mchModel->food_pay_type;
- } else {
- $food_payment = $store->food_payment;
- $food_paytype = $store->food_pay_type;
- }
- if (!empty($food_payment)) {
- $food_pay_type = Json::decode($food_payment);
- $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store')['value']);
- $is_food_ok = false;
- if (!empty($pay_type_list_json)) {
- $pay_type_list = Json::decode($pay_type_list_json['value']);
- $user_pay_list_json = Option::get('payment_' . get_user_id(), get_store_id(), 'user')['value'];
- if ($user_pay_list_json) {
- $user_pay_list_arr = Json::decode($user_pay_list_json);
- foreach ($pay_type_list as $key => &$value) {
- if (isset($user_pay_list_arr[$key]) && $user_pay_list_arr[$key]['value'] == 0 && $value['value'] == 1) {
- $value['value'] = 0;
- }
- }
- }
- foreach ($pay_type_list as $index => $pay_type) {
- if ($index == 'friend' && $pay_type['value'] == 1) {
- $is_food_ok = true;
- break;
- }
- }
- }
- foreach ($food_pay_type as &$value) {
- if ($value['key'] == 'friend') {
- if ($is_food_ok && $value['value'] == 1) {
- $value['value'] = 1;
- } else {
- $value['value'] = 0;
- }
- }
- if ($value['key'] == 'friend') {
- $value['payment'] = 7;
- }
- if ($value['key'] == 'alipay') {
- $value['payment'] = 4;
- }
- if ($value['key'] == 'wechat') {
- $value['payment'] = 1;
- }
- if ($value['key'] == 'huodao') {
- $value['payment'] = 2;
- }
- if ($value['key'] == 'bytedance') {
- $value['payment'] = 5;
- }
- }
- $food_pay_list = $food_pay_type;
- }
- foreach ($food_pay_list as $k => $val) {
- if ($val['payment'] == 4 && is_wechat_platform() || ($val['payment'] == 4 && intval($val['value']) === 0)) {
- unset($food_pay_list[$k]);
- }
- if ($val['payment'] == 1 && is_alipay_platform() || ($val['payment'] == 1 && intval($val['value']) === 0)) {
- unset($food_pay_list[$k]);
- }
- if ($val['payment'] == 7 && intval($val['value']) === 0) {
- unset($food_pay_list[$k]);
- }
- if ($val['payment'] == 2 && intval($val['value']) === 0) {
- unset($food_pay_list[$k]);
- }
- if ($val['payment'] == 5 && intval($val['value']) === 0) {
- unset($food_pay_list[$k]);
- }
- }
- $food_pay_list = array_values($food_pay_list);
- // 点餐支付方式
- $default_food_delivery = (int)Option::get('default_food_delivery', get_store_id(), 'store', 0)['value']; //到店自提
- if($mchModel){
- $default_food_delivery = (int)Option::get('default_food_delivery', get_mch_id(), 'mch', 0)['value'];
- }
- $food_pay_type = [
- 'food_pay_type' => $food_paytype,
- 'food_payment' => $food_pay_list,
- 'default_food_delivery' => $default_food_delivery
- ];
- // $food_pay_type = $food_pay_list;
- }
- if (get_md_id() > 0) {
- $province_name = District::findOne($md->province)->name;
- $city_name = District::findOne($md->city)->name;
- $district_name = District::findOne($md->district)->name;
- $md_info = [
- 'is_md' => 1,
- 'name' => $md->name,
- 'detail_address' => $province_name . $city_name . $district_name . $md->address,
- 'distance' => Tools::getDistance($this->latitude, $this->longitude, $md->latitude, $md->longitude),
- 'longitude' => $md->longitude,
- 'latitude' => $md->latitude,
- 'logo' => $md->cover_url
- ];
- } else {
- $province_name = District::findOne($store->province_id)->name;
- $city_name = District::findOne($store->city_id)->name;
- $district_name = District::findOne($store->district_id)->name;
- $lat = '';
- $long = '';
- $coordinate = explode(',', $store->coordinate);
- if ($coordinate) {
- $lat = $coordinate[0];
- $long = $coordinate[1];
- }
- $md_info = [
- 'is_md' => 0,
- 'name' => $store->name,
- 'detail_address' => $province_name . $city_name . $district_name . $store->address,
- 'distance' => Tools::getDistance($this->latitude, $this->longitude, $lat, $long),
- 'longitude' => $long,
- 'latitude' => $lat,
- 'logo' => $store->logo
- ];
- }
- if ($mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_ADOPT) {
- $template_id = NoticeAction::getSendTamplateId(['order_pay', 'adopt_mature', 'adopt_order_confirm_mature']);
- } else {
- $noticeType = ['order_pay', 'order_submit'];
- if($this->send == 'shop'){
- $noticeType[] = 'shop_arrive';
- }else{
- $noticeType[] = 'order_send';
- }
- $template_id = NoticeAction::getSendTamplateId($noticeType, is_h5() ? 'wxaapi' : 'miapp');
- }
- if ($this->is_worker) {
- goto is_worker;
- }
- //计算联盟券
- $take_price = 0;
- $send_price = 0;
- // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例
- // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => get_store_id()]);
- // if($this_store_send_profit_model->value > 0){
- // $basic_send_profit = $this_store_send_profit_model->value;
- // }else{
- // $basic_send_profit = $send_profit_model->value;
- // }
- foreach ($mchList[0]['goods_list'] as $arr) {
- //TODO 新的
- //计算返利
- $send_price += $arr['send_price'] ?? 0;
- $take_price += $arr['take_price'] ?? 0;
- }
- $user = User::findOne(get_user_id());
- $user_league_price = 0;
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- if($saas_user){
- $user_league_price = $saas_user->league_price;
- }
- $show_saas_alliance_coupon = true;
- if (\Yii::$app->prod_is_dandianpu()) {
- $show_saas_alliance_coupon = false;
- }
- // // 随机立减
- // $mchList[0]['rand_discount_price'] = 0;
- // // 此处做缓存,用于解决前端短时间内请求多次订单预览接口
- // if (
- // $mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_NORMAL ||
- // $mchList[0]['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_VIRTUAL
- // ) {
- // $randDiscountCache = cache()->get('rand_discount_cache_' . get_user_id());
- // if ($randDiscountCache) {
- // $randDiscountOption = $randDiscountCache;
- // } else {
- // $randDiscountOption = RandDiscount::getOption(\get_store_id(), $mchList[0]['total_price']);
- // cache()->set('rand_discount_cache_' . get_user_id(), $randDiscountOption, 3);
- // }
- // cache()->set('rand_discount_cache2_' . get_user_id(), $randDiscountOption);
- // if ($randDiscountOption['is_open'] && (float)$randDiscountOption['rand'] > 0) {
- // $mchList[0]['rand_discount_price'] = $randDiscountOption['rand'];
- // }
- // }
- is_worker:
- $invoiceConf = [];
- $invoice_is_open = \app\modules\admin\models\InvoiceForm::is_open($store->id, $invoiceConf);
- return [
- 'code' => 0,
- 'msg' => 'OOKK',
- 'order_no' => $order_no,
- 'data' => [
- 'user' => get_user(),
- 'user_integralPrice' => get_user()->integralPrice,
- 'invoice_is_open' => $invoice_is_open,
- 'invoiceConf' => $invoiceConf,
- 'goods_send_profit' => 0,
- 'store_send_profit' => 0,
- 'flag_id' => intval($flag_id),
- 'flag_type' => intval($flagType),
- 'table_info' => $tableInfo,
- 'take_price' => sprintf('%.2f',$take_price ?? 0),
- 'send_price' => sprintf('%.2f', $send_price ?? 0),
- 'user_league_price' => !$this->is_worker ? $user_league_price ?? 0 : 0,
- 'md_info' => !$this->is_worker ? $md_info : null,
- 'food_pay' => $food_pay_type,
- 'pay_type_list' => $food_pay_list ?: $this->getPayTypeList(),
- 'address' => $this->address,
- 'worker' => $this->worker,
- 'level' => !$this->is_worker ? $this->getLevelData() : null,
- 'mch_list' => $mchList,
- 'integral'=> $this->integral,
- 'goods_card_list' => !$this->is_worker ? $this->goodsCardList(): null,
- 'template_id' => $template_id,
- 'show_saas_alliance_coupon' => !$this->is_worker ? $show_saas_alliance_coupon : false,
- 'travel' => $this->is_worker ? $this->travel : null,
- 'agreement_id' => $agreement_id,
- 'agreement_title' => $agreement_title,
- ],
- ];
- }
- }
|