| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\order;
- use app\models\Address;
- use app\models\Admin;
- use app\models\Cart;
- use app\models\BusinessCart;
- use app\models\common\CommonOrder;
- use app\models\DeliveryInfo;
- use app\models\FoodCart;
- use app\models\FoodFlag;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\Md;
- use app\models\MdProfit;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\Register;
- use app\models\SaasUser;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\VerifyCard;
- use app\models\VerifyCardSale;
- use app\models\VerifyCardLog;
- use app\modules\admin\models\ActivityCutPriceForm;
- use app\utils\CloudPrint;
- use app\utils\Delivery\Delivery;
- use app\utils\Delivery\Alipay\ADelivery;
- use app\utils\Notice\NoticeSend;
- use app\utils\OrderNo;
- use app\utils\PrintOrder;
- use app\utils\Tools;
- use yii\helpers\Json;
- use app\models\SaasCoupon;
- use app\modules\admin\models\maiyatian\MaiyatianForm;
- use app\modules\admin\models\keloop\KeloopForm;
- use app\models\LocalDeliveryFreight;
- use app\models\OfferPrice;
- use yii\helpers\VarDumper;
- class OrderSubmitForm extends OrderForm
- {
- public $user;
- public $payment;
- public $use_integral;
- public function rules()
- {
- $rules = [
- [['payment', 'use_integral'], 'integer']
- ];
- return array_merge(parent::rules(), $rules);
- }
- public function preOrder()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- try {
- if(!$this->address['id']){
- return [
- 'code' => 1,
- 'msg' => '请先选择收货地址',
- ];
- }
- $mch_list = $this->getStoreListData(true);
- $goods_list = [];
- foreach ($mch_list as $mch) {
- foreach ($mch['goods_list'] as $goods) {
- $goods_list[] = [
- 'name' => $goods['goods_name'],
- 'price' => $goods['price'],
- 'pic' => $goods['goods_pic'],
- 'num' => $goods['num'],
- ];
- }
- $delivery_time = $mch['delivery_time'];//送出时间
- }
- if($this->order_no){
- $existOrder = Order::findOne(['order_no' => $this->order_no]);
- if($existOrder){
- return [
- 'code' => 1,
- 'msg' => '单号错误0,请后退到商品页重新下单',
- ];
- }
- $order_no = $this->order_no;
- }else{
- $order_no = cache()->get('delivery_order_no_' . get_saas_user_id());
- }
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, $this->store_id, 'pay', $local_type);
- if($local_type['value'] == 'keloop') {
- if(KeloopForm::isSaasOpen()){
- $preAddOrder = KeloopForm::preAddOrder($this->store_id, $order_no, $this->address['longitude'], $this->address['latitude']);
- if ($preAddOrder['code']) {
- return $preAddOrder;
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => [
- "resultcode" => 0,
- 'fee' => (float)$preAddOrder['data']['fee'],
- 'fee_data' => $preAddOrder['data']['fee_data'],
- ],
- 'order_no' => $order_no,
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '快跑者配送没有配置',
- ];
- }
- if($local_type['value'] == 'maiyatian') {//麦芽田
- if(MaiyatianForm::isopen($this->store_id)){
- $getFreight = LocalDeliveryFreight::getFreight($this->store_id, $this->address, $delivery_time, LocalDeliveryFreight::TYPE_MAIYATIAN, $goods_list);
- if($getFreight['code'] != 0){
- return $getFreight;
- }
- $freight = $getFreight['data'];
- $preAddOrder = MaiyatianForm::preAddOrder($this->store_id, $order_no, $freight);
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => [
- "resultcode" => 0,
- // 'fee' => (float)MaiyatianForm::getConf($this->store_id, 'cyy_delivery_fee'),
- 'fee' => (float)$freight,
- ],
- 'order_no' => $order_no,
- ];
- return $preAddOrder;
- }
- return [
- 'code' => 1,
- 'msg' => '麦芽田没有配置',
- ];
- } elseif($local_type['value'] == 'aggregate') {//聚合配送
- if(is_alipay_platform()){
- $this->address_id || $this->address_id = 220;
- $res = ADelivery::createOrder(1, $this->store_id, get_mini_id(), get_saas_user()->ali_user_id, $goods_list, $order_no, $this->address_id);;
- return array_merge($res, ['order_no' => $order_no]);
- }
- return array_merge(Delivery::preAddOrder($goods_list, $order_no), ['order_no' => $order_no]);
- } elseif(in_array($local_type['value'], ['self', 'self_store'])) {//平台自配
- $type = LocalDeliveryFreight::TYPE_LOCAL;
- if ($local_type['value'] === 'self_store') {
- $type = LocalDeliveryFreight::TYPE_STORE_LOCAL;
- }
- $getFreight = LocalDeliveryFreight::getFreight($this->store_id, $this->address, $delivery_time, $type, $goods_list);
- if($getFreight['code'] != 0){
- return $getFreight;
- }
- $freight = $getFreight['data'];
- DeliveryInfo::deleteAll(['order_no' => $order_no, 'store_id' => get_store_id(), 'status' => 0]);
- // 存预下单表
- $delivery_info = new DeliveryInfo();
- $delivery_info->store_id = get_store_id();
- $delivery_info->order_no = $order_no;
- $delivery_info->fee = $freight;
- $delivery_info->delivery_type = 0;
- $delivery_info->created_at = time();
- $delivery_info->is_local = DeliveryInfo::IS_LOCAL_YSE;
- $delivery_info->is_store_delivery_type = intval($local_type['value'] === 'self_store');
- $serial_num = DeliveryInfo::find()->where(['>','created_at',strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num'];
- $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1;
- $delivery_info->save();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'order_no' => $order_no,
- 'data' => $getFreight['info'],
- ];
- /* end */
- // return [
- // 'code' => 0,
- // 'data' => [
- // 'goods_list' => $goods_list,
- // 'order_no' => $order_no,
- // 'test' => [
- // 'address' => $address,
- // 'store' => $store,
- // 'distance' => $distance,
- // 'delivery_time' => $delivery_time,
- // 'delivery_time_stamp' => $delivery_time_stamp,
- // 'LocalDeliveryFreight' => $data,
- // 'freight_detail' => $freight_detail,
- // 'freight' => $freight,
- // ]
- // ],
- // 'msg' => '平台自配'
- // ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function save()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- try {
- $mchListData = $this->getStoreListData(true);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $order_id_list = [];
- //$level = $this->level;
- /**
- * @var $address Address
- */
- $saas_user = get_saas_user();
- $this->user = User::findOne(['binding' => $saas_user->mobile,'store_id' => get_store_id(),'is_delete'=>0]);
- if (intval(get_store_id()) <= 0) {
- $this->user = $saas_user;
- }
- $address = (object)($this->address);
- $t = \Yii::$app->db->beginTransaction();
- foreach ($mchListData as &$mch) {
- if (isset($mch['use_integral']) && !$mch['use_integral']) {
- $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0];
- $mch['give'] = 0;
- }
- $mch_goods = $mch['goods_list'];
- if ($mch_goods) {
- $checkMchData = $this->checkMchData($mch);
- if ($checkMchData['code'] == 1) {
- $t->rollBack();
- return $checkMchData;
- }
- $level_price = 0;
- $total_price = 0;
- $is_have_express = false;
- $is_have_shop = false;
- $order_type = 0;
- $is_have_delivery = 0;
- $total_integral = 0;
- if ($mch['send'] == 'express') {
- $is_have_express = true;
- }
- if ($mch['send'] == 'shop') {
- $is_have_shop = true;
- }
- $is_delivery = OfferPrice::findOne([
- 'store_id' => get_store_id(),
- 'is_delete' => 0
- ]);
- $store_data = Store::findOne(get_store_id());
- $delivery_rules = [];
- if ($mch['send'] == 'delivery') {
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'store')['value'];
- $local_type = Option::get(OptionSetting::STORE_LOCAL_TYPE, get_store_id(), 'pay', $local_type);
- $is_have_express = true;
- $delivery_rules = json_decode($is_delivery->delivery_rules, true);
- if ($is_delivery->delivery_type == 1 && !empty($delivery_rules)) {
- $conf = [
- 'shop_tag' => $store_data->coordinate,
- 'deliveryRules' => $delivery_rules
- ];
- $deliveryRules = $this->canDeliver($address, $conf);
- if (!$deliveryRules['can_deliver']) {
- return [
- 'code' => 1,
- 'msg' => '当前订单超出配送距离'
- ];
- }
- }
- if ($local_type['value'] == 'keloop') { //快跑者配送
- if (!KeloopForm::isSaasOpen()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '商城未设置有效配送方式'
- ];
- }
- }
- if($local_type['value'] == 'maiyatian') {//麦芽田
- $maiyatian = MaiyatianForm::isopen($this->store_id);
- if (!$maiyatian) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '商城未设置有效配送方式'
- ];
- }
- } elseif($local_type['value'] == 'aggregate') {//聚合配送
- $store = Store::findOne($this->store_id);
- if ($store->delivery_type == 0) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '商城未设置有效配送方式'
- ];
- }
- $res = Delivery::getBindAccount();
- // 下单时判断是否可以同城配送
- if (empty($res['data']['shop_list'])) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '当前未绑定运力账号,暂不能保存选择同城配送方式'
- ];
- }
- $shop_list = $res['data']['shop_list'];
- $delivery_arr = array_column($shop_list, 'delivery_id');
- $delivery_arr[] = 'TEST';
- $delivery_arr_id = array_column($shop_list, null, 'delivery_id');
- if (!in_array(Delivery::$deliveryIdArr[$store->delivery_type], $delivery_arr) || $delivery_arr_id[Delivery::$deliveryIdArr[$store->delivery_type]]['audit_result'] != 0) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '当前账号未在微信后台绑定或者并未审核通过'
- ];
- }
- } elseif(in_array($local_type['value'], ['self', 'self_store'])) {//平台自配
- if(empty($mch['delivery_time'])){
- $store_id = $local_type['value'] === 'self_store' ? $this->store_id : 0;
- $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, $store_id, '', '{}')['value'], true);
- $mch['delivery_time'] = date('Y-m-d H:i:s', time() + 90 * 60);
- if(!empty($setting['default_time']) && !empty($setting['default_time']['value'])){
- $mch['delivery_time'] = date('Y-m-d H:i:s', time() + ($setting['default_time']['value'] * 60));
- }
- }
- }
- $is_have_delivery = true;
- }
- $take_price = 0;
- $send_price = 0;
- foreach ($mch_goods as $v) {
- if ($v['product_type'] == 1) {
- $order_type = 1;
- }
- if ($v['product_type'] == 2) {
- $order_type = 2;
- }
- if ($v['product_type'] == 3) {
- $order_type = 4;
- }
- if ($v['product_type'] == Goods::GOODS_TYPE_INTEGRAL) {
- $order_type = 7;
- $total_integral += isset($v['integral_price']) ? $v['integral_price'] : 0;
- }
- // if ($v['send'] == 'shop' && empty($v['shop_id']) && empty($v['shop'])) {
- // return [
- // 'code' => 1,
- // 'msg' => '自提商品请选择自提点'
- // ];
- // }
- $level_price += $v['level_price'];
- $total_price += isset($v['total_price']) ? $v['total_price'] : $v['price'];
- $send_price += $v['send_price'] ?? 0;
- $take_price += $v['take_price'] ?? 0;
- }
- $mch['level_price'] = $level_price;
- $mch['total_price'] = $total_price;
- $mch['total_integral'] = $total_integral;
- // 是否开启配送范围设置
- if ($is_have_delivery) {
- if (get_md_id() > 0) {
- $md_info = Md::findOne(get_md_id());
- $lat = $md_info->latitude;
- $long = $md_info->longitude;
- $is_set_distance = $md_info->is_set_distance;
- $distance = $md_info->distance;
- } else {
- $store_info = Store::findOne(get_store_id());
- $is_set_distance = $store_info->is_set_distance;
- $distance = $store_info->distance;
- $coordinate = explode(',', $store_info->coordinate);
- $lat = $coordinate[0];
- $long = $coordinate[1];
- }
- if ($is_set_distance) {
- $distance_str = Tools::getDistance($lat, $long, $address->latitude, $address->longitude);
- if ($distance_str != '未知距离') {
- if (false !== strpos($distance_str, 'km')) {
- if (floatval($distance_str) > $distance) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '当前订单超出配送距离'
- ];
- }
- } else {
- if (floatval($distance_str) > $distance * 1000) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '当前订单超出配送距离'
- ];
- }
- }
- }
- }
- }
- // 开启使用积分抵扣
- if (isset($mch['use_integral']) && $mch['use_integral'] == 1 && isset($this->user->integral)) {
- if ($this->user->integral < $mch['integral']['forehead'] || $this->user->integral < $mch['total_integral']) {
- return [
- 'code' => 1,
- 'msg' => '积分可抵扣支付数量不足'
- ];
- }
- }
- $resultPrice = $this->getPayPrice($mch);
- $payPrice = $resultPrice['pay_price'];
- $payPrice = $this->getBookPrice($payPrice, $mch_goods);
- if ($is_have_express && $is_delivery->delivery_type == 1 && !empty($delivery_rules)) {
- if ($resultPrice['goods_price'] < $deliveryRules['delivery_price']) {
- return [
- 'code' => 1,
- 'msg' => '同城配送订单满 ¥' . $deliveryRules['delivery_price'] . ' 起送'
- ];
- }
- }
- if ($order_type == 1) {
- // $bookCount = count($mch_goods[0]['book']);
- $mch['level_price'] = $payPrice;
- $mch['total_price'] = $payPrice;
- }
- $order = new Order();
- // $verifylog = new VerifyCardLog();
- // $verifycardsale = new VerifyCardSale();
- if($mch['delivery_time']){
- $order->delivery_time = strtotime($mch['delivery_time']);
- }
- $order->store_id = $this->store_id;
- $order->user_id = 0;
- //saas订单记录use用户信息
- if ($this->store_id > 0) {
- if(isset($this->user->id) && $this->user->id > 0){
- $order->user_id = $this->user->id;
- }
- } else {
- $order->store_id = 0;
- }
- if (is_app_platform()) {
- $order->order_origin = Order::ORDER_SOURCE_APP;
- }
- $order->saas_id = get_saas_user_id();
- if (!empty($this->verify_card_id)) {
- $order->type = 1;
- }
- if ($is_have_delivery) {
- $order->order_no = cache()->get('delivery_order_no_' . get_saas_user_id());
- } else {
- $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_ALLIANCE);
- }
- $order->pay_price = !empty($this->verify_card_id) ? 0 : $payPrice;
- if (isset($mch['picker_coupon']) && !empty($mch['picker_coupon'])) {
- $order->user_coupon_id = $mch['picker_coupon']['user_coupon_id'];
- $order->coupon_sub_price = $mch['picker_coupon']['sub_price'];
- // 查找优惠券优惠的商品
- $this->pickerGoods($mch);
- }
- // 订单类型
- $order->order_type = $order_type;
- // 积分兑换商品
- if (isset($mch['total_integral']) && $mch['total_integral'] > 0 ) {
- if ($this->user->integral < $mch['total_integral']) {
- return [
- 'code' => 1,
- 'msg' => '积分数量不足'
- ];
- }else{
- $order->integral_price = $mch['total_integral'] ?? 0;
- $order->integral_difference_price = 0;
- $mch['integral_type'] = 1; //全部使用积分
- $mch['integral_difference_price'] = 0; //补的差额
- }
- //当用户有积分时
- // if(get_user()->integral > 0){
- // $upIntegral = get_user()->integral - $order->integral_price;
- // //减用户积分
- // if(! User::updateAll(['integral'=>$upIntegral],['id'=> get_user_id()]) ){
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '积分抵扣失败,稍后重试!'
- // ];
- // }
- // }
- }
- // 是否是平台小程序订单
- //if (is_platform()) {
- $order->is_platform = 1;
- //}
- if (get_md_id()) {
- $order->md_id = get_md_id();
- }
- // if($this->send_price){
- $order->send_price = $send_price;
- // }
- // if($this->take_price){
- $order->take_price = $take_price;
- // }
- $order->created_at = time();
- $order->first_price = 0;
- $order->second_price = 0;
- $order->third_price = 0;
- $order->content = isset($mch['content']) ? $mch['content'] : '';
- $order->is_offline = $is_have_shop ? 1 : 0;
- $order->is_delivery = $is_have_delivery ? 1 : 0;
- if (isset($mch['use_integral']) && $mch['use_integral'] == 1) {
- $order->integral = json_encode($mch['integral'], JSON_UNESCAPED_UNICODE);
- } else {
- $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0], JSON_UNESCAPED_UNICODE);
- }
- $order->version = cyy_version();
- $order->mch_id = $mch['mch_id'] ?: 0;
- $order->discount = isset($level['discount']) ? $level['discount'] : 10;
- $order->remark = $mch['remark'] ?: '';
- if ($this->payment == 2) {
- $order->pay_type = 2;
- $order->is_pay = 0;
- }
- if ($this->payment == 3) {
- $order->pay_type = 3;
- $order->is_pay = 0;
- }
- // 线下支付
- if ($this->payment == 6) {
- $order->pay_type = 6;
- $order->is_pay = 0;
- }
- $flag_id = input_params('flag_id');
- $table_num = input_params('table_num');
- if(empty($flag_id) && !empty($mch_goods[0]['food_cart_id'])){
- $food_cart_id = $mch_goods[0]['food_cart_id'];
- $foodCart = FoodCart::findOne($food_cart_id);
- $flag_id = $foodCart->flag_id;
- }
- if ((!empty($flag_id) || !empty($table_num))) {
- if (get_md_id() < 0) {
- $store = Store::findOne(get_store_id());
- if ($store->open_status == 0) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '已打烊'
- ];
- }
- if ($store->is_time_forbid == 0 && !$this->checkTimeBeginEnd($store->s_time, $store->e_time) && $store->shop_time_type == 1) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '非营业时间禁止下单'
- ];
- }
- }
- if (!empty($flag_id)) {
- $order->food_flag_id = $flag_id;
- } else {
- $order->food_flag_id = FoodFlag::findOne(['store_id' => get_store_id(), 'md_id' => get_md_id(), 'status' => 0, 'table_num' => $table_num])->id;
- }
- $order->order_type = 3;
- $order->food_book_ext = $mch['food_book_ext'];
- }
- if ($is_have_express || get_md_id()) {
- $order->address = $address->province . $address->city . $address->district . $address->detail;
- $order->mobile = $address->mobile;
- $order->name = $address->name;
- $order->province_id = $address->province_id;
- $order->city_id = $address->city_id;
- $order->district_id = $address->district_id;
- $order->address_data = json_encode([
- 'province' => trim($address->province),
- 'city' => trim($address->city),
- 'district' => trim($address->district),
- 'detail' => trim($address->detail),
- 'latitude' => $address->latitude,
- 'longitude' => $address->longitude,
- ], JSON_UNESCAPED_UNICODE);
- if ($is_have_delivery) {
- $order->total_price = !empty($this->verify_card_id) ? 0 : $mch['total_price'];
- } else {
- $order->express_price = $mch['express_price'];
- $order->total_price = !empty($this->verify_card_id) ? 0 : $mch['total_price'] + $mch['express_price'];
- }
- }
- if ($is_have_shop) {
- $order->total_price = !empty($this->verify_card_id) ? 0 : $mch['total_price'];
- $order->express_price = 0;
- }
- if ($order->order_type == 1 || $order->order_type == 2) {
- $order->book_info = Json::encode($mch['book_info']);
- }
- if ($order->order_type == 2) {
- $order->total_price = $payPrice;
- }
- if ($order->order_type == 1) {
- $order->total_price = $mch['total_price'];
- }
- // // 礼品卡订单
- // if (!empty(post_params('verify_card_id'))) {
- // $order->verify_card_id = post_params('verify_card_id');
- // }
- //获取代理配置信息
- $store = Store::findOne($this->store_id);
- $option = Option::get('agency_price_config', 0, 'saas', [])['value'];
- if ($option) {
- $option = json_decode($option, true);
- }
- $province_percent = $option['province_percent'] ?? 0;
- $city_percent = $option['city_percent'] ?? 0;
- $district_percent = $option['district_percent'] ?? 0;
- $ag_rebate = $payPrice * ($store->transfer_profit / 100);
- $order->ag_rebate = $ag_rebate;
- // 配送订单预存数据
- // if ($is_have_delivery) {
- // $delivery_info = new DeliveryInfo();
- // $delivery_info->store_id = get_store_id();
- // $delivery_info->order_no = $order->order_no;
- // $delivery_info->status = 0;
- // $delivery_info->created_at = time();
- // $delivery_info->updated_at = time();
- // $delivery_info->save();
- // }
- $order->integral_difference_price = 0;
- if($this->activity_cut_price_order_id){
- $order->activity_cut_price_order_id = $this->activity_cut_price_order_id;
- }
- if ($is_have_delivery) {
- $cacheK = 'is_have_delivery_' . $order->order_no;
- $cacheV = cache()->get($cacheK);
- $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one();
- if(!$deliveryInfo){
- $t->rollBack();
- if($cacheV){
- return [
- 'code' => 1,
- 'msg' => '配送单不存在,请返回商品详情页重新下单。' . $cacheV . $cacheK
- ];
- }
- cache()->set($cacheK, 1, 180);
- $this->order_no = '';
- $preOrder = $this->preOrder();
- if($preOrder['code'] != 0){
- return $preOrder;
- }else{
- $this->order_no = '';
- return $this->save();
- }
- }
- $order->total_price += $deliveryInfo->fee;
- $order->pay_price += $deliveryInfo->fee;
- }
- if ($order->save()) {
- //砍价订单
- if($this->activity_cut_price_order_id){
- $orderUsed = ActivityCutPriceForm::orderUsed($this->activity_cut_price_order_id, $order);
- if ($orderUsed['code'] != 0) {
- $t->rollBack();
- return $orderUsed;
- }
- }
- // 绑定上下级
- // $user = User::findOne($this->user_id);
- // if ($user && $user->parent_user_id) {
- // CommonOrder::saveParentId($user->parent_user_id);
- // }
- // VerifyCard::updateAll(['send_times' => $send_times], 'id='.$VerifyCardSale['verify_card_id']);
- // VerifyCardSale::updateAll(['left_num' => $left_num, 'status' => 1],'id='.$this->verify_card_id);
- // $verifylog->save();
- // 更改点餐flag_id状态
- // if (get_md_id() && !empty(input_params('flag_id'))) {
- // FoodFlag::updateAll(['status' => 1], ['id' => input_params('flag_id')]);
- // }
- // 处理订单生成之后其他相关数据
- $orderRes = $this->insertData($mch, $order);
- if ($orderRes['code'] == 1) {
- $t->rollBack();
- return $orderRes;
- }
- $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0, 0, $order['mch_id']);
- $printer_order->print_order();
- // 订单提交完成发送消息
- //NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name']);
- $order_id_list[] = $order->id;
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => array_shift($order->getFirstErrors()),
- 'data' => $order,
- ];
- }
- }
- }
- if (count($order_id_list) > 0) {
- $t->commit();
- if (count($order_id_list) > 1) {//多个订单合并
- return [
- 'code' => 0,
- 'msg' => '订单提交成功',
- 'data' => (object)[
- 'order_id_list' => $order_id_list,
- 'add_time' => $order->created_at
- ],
- ];
- } else {//单个订单
- $order_id = $order_id_list[0];
- $order = Order::findOne($order_id);
- if($order->take_price > 0){
- if (($order->pay_price - $order->take_price) >= 0) {
- $order->pay_price -= $order->take_price;
- $order->total_price -= $order->take_price;
- } else {
- $order->take_price -= $order->pay_price;
- $order->total_price = $order->pay_price = 0;
- }
- $order->save();
- }
- CloudPrint::doPrint($order_id, 0, $this->store_id);
- return [
- 'code' => 0,
- 'msg' => '订单提交成功',
- 'data' => (object)[
- 'order_id' => $order_id_list[0],
- 'add_time' => $order->created_at,
- 'order_price' => $order->pay_price
- ],
- ];
- }
- } else {
- $t->rollBack();
- return [
- 'order_list' => $order_id_list
- ];
- }
- }
- /**
- * 判断是否可以配送并计算费用
- */
- private function canDeliver($address, $conf)
- {
- // 解析商家地址的经纬度
- list($shopLon, $shopLat) = explode(',', $conf['shop_tag']);
- // 计算距离
- $distance = Tools::getDistance((float)$shopLat, (float)$shopLon, $address->latitude ?: $address['latitude'], $address->longitude ?: $address['longitude']);
- if (strpos($distance, 'km') !== false) {
- $distance = floatval(explode('km', $distance)[0]);
- } elseif (strpos($distance, 'm') !== false) {
- $distance = floatval(explode('m', $distance)[0]) / 1000;
- }
- foreach ($conf['deliveryRules'] as $rule) {
- if ($rule['start'] <= $distance && $distance <= $rule['end']) {
- return [
- 'can_deliver' => true,
- 'distance' => $distance,
- 'delivery_price' => $rule['price'],
- 'delivery_range' => [$rule['start'], $rule['end']]
- ];
- }
- }
- return ['can_deliver' => false, 'distance' => $distance, 'delivery_price' => null, 'delivery_range' => null];
- }
- public function getNo($attrStr, $searchAttrIds)
- {
- $attr = json_decode($attrStr, true);
- foreach ($attr as $item) {
- $attrIds = array_column($item['attr_list'], 'attr_id');
- if ($attrIds == $searchAttrIds) {
- return $item['no'];
- }
- }
- }
- // 获得实际支付金额
- private function getPayPrice($mch)
- {
- $goods_list = $mch['goods_list'];
- $payPrice = $mch['level_price'];
- $goodsPrice = $mch['level_price'];
- if (isset($mch['use_integral']) && $mch['use_integral']) {
- $payPrice -= $mch['integral']['forehead'];
- }
- if (isset($mch['picker_coupon'])) {
- $pickerCoupon = $mch['picker_coupon'];
- if ($pickerCoupon['sub_price'] > 0) {
- $coupon_price = 0;
- if ($pickerCoupon['appoint_type'] == 1 && $pickerCoupon['cat_id_list'] != null) {
- foreach ($goods_list as $goods) {
- foreach ($goods['cat_id'] as $v1) {
- if (in_array($v1, $pickerCoupon['cat_id_list'])) {
- $coupon_price += floatval($goods['level_price']);
- break;
- };
- };
- }
- } else if ($pickerCoupon['appoint_type'] == 2 && $pickerCoupon['goods_id_list'] != null) {
- foreach ($goods_list as $goods) {
- if (in_array($goods['goods_id'], $pickerCoupon['goods_id_list'])) {
- $coupon_price += floatval($goods['level_price']);
- };
- }
- };
- if ($pickerCoupon['sub_price'] > $coupon_price && $coupon_price > 0) {
- $payPrice -= $coupon_price;
- } else {
- $payPrice -= $pickerCoupon['sub_price'];
- }
- }
- }
- // Todo 此处有待确认算法是否正确 by Syan
- // $payPrice = $payPrice > 0 ? $payPrice : 0.01;
- if ($mch['express_price'] > 0) {
- if (isset($mch['send']) && $mch['send'] == 'express') {
- $payPrice += $mch['express_price'];
- } else {
- foreach ($goods_list as $goods) {
- if (isset($goods['send']) && $goods['send'] == 'express') {
- $payPrice += $mch['express_price'];
- break;
- }
- }
- }
- }
- return ['pay_price'=>$payPrice > 0 ? $payPrice : 0,'goods_price' => $goodsPrice];
- }
- // 检查数据
- private function checkMchData($mch)
- {
- if (empty($mch['goods_list'])) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在或已删除'
- ];
- }
- $checkFormData = $this->checkFormData($mch);
- if ($checkFormData['code'] == 1) {
- return $checkFormData;
- }
- $checkSendType = $this->checkSendType($mch);
- if ($checkSendType['code'] == 1) {
- return $checkSendType;
- }
- $checkCoupon = $this->checkCoupon($mch);
- if ($checkCoupon['code'] == 1) {
- return $checkCoupon;
- }
- $checkGoods = $this->checkGoods($mch);
- if ($checkGoods['code'] == 1) {
- return $checkGoods;
- }
- return ['code' => 0, 'msg' => 'success'];
- }
- // 检测优惠券是否可使用
- private function checkCoupon($mch)
- {
- if (empty($mch['picker_coupon'])) {
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- $ok = false;
- foreach ($mch['coupon_list'] as $item) {
- if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) {
- $ok = true;
- }
- }
- if (!$ok) {
- return [
- 'code' => 1,
- 'msg' => '该优惠券已过期'
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- }
- // 检测发货方式
- private function checkSendType($mch)
- {
- if ($mch['mch_id'] == 0) {
- if (isset($mch['is_offline']) && $mch['is_offline'] == 1) {
- if (isset($mch['shop']) == false) {
- return [
- 'code' => 1,
- 'msg' => '请选择自提门店'
- ];
- }
- if (!(isset($mch['offline_name']) && $mch['offline_name']) || !(isset($mch['offline_mobile']) && $mch['offline_mobile'])) {
- return [
- 'code' => 1,
- 'msg' => '请输入自提信息'
- ];
- }
- if (!preg_match("/^1\d{10}$/", $mch['offline_mobile'])) {
- return [
- 'code' => 1,
- 'msg' => '请输入正确的手机号'
- ];
- }
- } else {
- if (!$this->address) {
- return [
- 'coe' => 1,
- 'msg' => '收货地址不存在'
- ];
- }
- if ($mch['offer_rule'] && $mch['offer_rule']['is_allowed'] == 1) {
- return [
- 'code' => 1,
- 'msg' => $mch['offer_rule']['msg']
- ];
- }
- if ($mch['is_area'] == 1 && $mch['send'] == 'express') {
- return [
- 'code' => 1,
- 'msg' => '所选地区自营商品暂时无货'
- ];
- }
- }
- } else {
- if (!$this->address) {
- return [
- 'coe' => 1,
- 'msg' => '收货地址不存在'
- ];
- }
- }
- return ['code' => 0, 'msg' => ''];
- }
- // 检测自定义表单
- private function checkFormData($mch)
- {
- foreach ($mch['goods_list'] as $goods) {
- if (isset($goods['form']['code'])) {
- return $goods['form'];
- }
- if (!isset($goods['form'])) {
- continue;
- }
- $form = $goods['form'];
- if ($form['is_form'] == 1) {
- $form_list = $form['list'];
- foreach ($form_list as $index => $value) {
- if ($value['required'] == 1) {
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- $is_true = false;
- foreach ($value['default_list'] as $k => $v) {
- if ($v['checked'] == true) {
- $is_true = true;
- }
- }
- if (!$is_true) {
- return [
- 'code' => 1,
- 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
- 'name' => $value['name']
- ];
- }
- } else {
- if (empty($value['default']) && strlen($value['default']) == 0) {
- return [
- 'code' => 1,
- 'msg' => '请填写' . $form['name'] . ',加“*”为必填项',
- 'name' => $value['name']
- ];
- }
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- // 检测商品相关
- private function checkGoods($mch)
- {
- \Yii::warning($mch);
- foreach ($mch['goods_list'] as $goods) {
- $attr_id_list = [];
- foreach ($goods['attr_list'] as $item) {
- array_push($attr_id_list, $item['attr_id']);
- }
- $_goods = Goods::findOne($goods['goods_id']);
- //2砍价商品6当面付
- if ($_goods->type != 2 && $_goods->type != 6 && $_goods->product_type == Goods::GOODS_TYPE_NORMAL) {
- if (!$_goods->numSub($attr_id_list, $goods['num'])) {
- return [
- 'code' => 1,
- 'msg' => '订单提交失败,商品“' . $_goods->name . '”库存不足',
- 'attr_id_list' => $attr_id_list,
- 'attr_list' => $goods['attr_list'],
- ];
- }
- }
- if($_goods->product_type == Goods::GOODS_TYPE_DATE){
- $num = Goods::getGoodsNum($_goods)['data'];
- $attr = $goods['book'];
- \Yii::error(['$num$num$num$num$num', $num]);
- \Yii::error(['$attr$attr$attr$attr', $attr]);
- foreach ($attr as $iattr) {
- $idate = $iattr['date'];
- foreach ($num as $inum) {
- $indate = $inum['date'];
- if(($idate == $indate) && ($inum['num'] < $goods['num'])){
- return ['code' => 1, 'msg' => $idate.'库存不足'];
- }
- }
- }
- }
- }
- return ['code' => 0, 'msg' => ''];
- }
- public function getOrderNo()
- {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Order::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- return $order_no;
- }
- // 优惠券可优惠的商品总额计算
- private function pickerGoods(&$mch)
- {
- $totalPrice = 0;
- $pickerCoupon = $mch['picker_coupon'];
- if (empty($pickerCoupon)) {
- return;
- }
- foreach ($mch['goods_list'] as $item) {
- if ($pickerCoupon['appoint_type'] == 1) {
- if ($pickerCoupon['cat_id_list'] !== null) {
- $catIdList = $pickerCoupon['cat_id_list'];
- if (array_intersect($item['cat_id'], $catIdList)) {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else if ($pickerCoupon['appoint_type'] == 2) {
- if ($pickerCoupon['goods_id_list'] !== null) {
- $goodsIdList = $pickerCoupon['goods_id_list'];
- if (in_array($item['goods_id'], $goodsIdList)) {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- } else {
- $totalPrice += $item['price'];
- $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
- }
- }
- $mch['picker_coupon']['total_price'] = $totalPrice;
- }
- /**
- * @param $mch
- * @param $order Order
- * @return array
- */
- private function insertData($mch, $order)
- {
- // 计算商品相关
- $goods_list = $mch['goods_list'];
- $goodsPrice = 0;
- // 计算商品分润
- $goods_profit = 0;
- $chain_goods_profit = 0;
- $chainGoodsList = [];
- foreach ($goods_list as $goods) {
- // 存入下单表单
- if ($goods['form'] && $goods['form']['is_form'] == 1) {
- foreach ($goods['form']['list'] as $index => $value) {
- $order_form = new \app\models\OrderForm();
- $order_form->store_id = $this->store_id;
- $order_form->order_id = $order->id;
- $order_form->goods_id = $goods['id'];
- $order_form->key = $value['name'];
- $order_form->value = $value['default'];
- $order_form->type = $value['type'];
- $order_form->is_delete = 0;
- $order_form->save();
- }
- }
- // 删除购物车
- if (isset($goods['cart_id'])) {
- BusinessCart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]);
- }
- $order_detail = new OrderDetail();
- $order_detail->order_id = $order->id;
- $order_detail->goods_id = $goods['goods_id'];
- $order_detail->num = $goods['num'];
- if (isset($goods['cost_price'])) {
- $order_detail->cost_price = $goods['cost_price'];
- }
- // if ($goods['is_level'] && $goods['is_level'] == 1) {
- // $order_detail->is_level = (int)$goods['is_level'];
- // } else {
- $order_detail->is_level = 1;
- //}
- $payPrice = $goods['level_price'];
- if (isset($mch['picker_coupon']) && $mch['picker_coupon'] && !empty($mch['picker_coupon'])) {
- if (!empty($mch['picker_coupon']['goods_id_list']) && in_array($goods['goods_id'], $mch['picker_coupon']['goods_id_list']) && $mch['picker_coupon']['total_price'] > 0) {
- $pickerCouponSubPrice = doubleval($mch['picker_coupon']['sub_price'] * doubleval($goods['price']) / $mch['picker_coupon']['total_price']);
- $payPrice -= $pickerCouponSubPrice;
- }
- // 删除优惠券
- SaasCoupon::updateAll(['is_use' => 1], ['id' => $mch['picker_coupon']['user_coupon_id']]);
- }
- // if (isset($mch['use_integral']) && $mch['use_integral']) {
- // if ($goods['resIntegral'] && $goods['resIntegral']['forehead'] > 0) {
- // $payPrice -= $goods['resIntegral']['forehead'];
- // }
- // }
- $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0;
- $goodsPrice += $payPrice;
- $orderPrice = floatval($order->pay_price) - floatval($order->express_price);
- if ($goodsPrice > $orderPrice) {
- $payPrice = $payPrice - ($goodsPrice - $orderPrice);
- $goodsPrice = $orderPrice;
- }
- $payPrice = $this->getBookPrice($payPrice, $goods_list);
- // $order_detail->supplier_price = $goods['supplier_price'];
- $order_detail->total_price = $payPrice;
- // 计算单个商品可分红金额
- $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : floatval($goods['rate'] * $goods['num']);
- $goods_profit += $profit;
- $order_detail->profit = $profit;
- // 链动
- $chain_profit = $goods['chain_rate_type'] == 0 ? floatval($payPrice * $goods['chain_rate'] / 100) : floatval($goods['chain_rate'] * $goods['num']);
- $chain_goods_profit += $chain_profit;
- $order_detail->chain_profit = $chain_profit;
- $order_detail->is_delete = 0;
- $chainGoodsList[] = [
- 'id' => $goods['id'],
- 'num' => $goods['num'],
- 'pay_price' => $payPrice,
- ];
- if ($goods['product_type'] == 1) {
- $order_detail->attr = json_encode($goods['book'], JSON_UNESCAPED_UNICODE);
- } elseif ($goods['product_type'] == 2) {
- //$order_detail->attr = json_encode($goods['service'], JSON_UNESCAPED_UNICODE);
- $service = $goods['service'];
- $time = explode('-', $service['time']);
- $order_detail->attr = json_encode([
- 'start_date' => $service['date'] . ' ' . $time[0],
- 'end_date' => $service['date'] . ' ' . $time[1],
- 'time' => $service['time'],
- 'date' => $service['date'],
- 'price' => $service['price'],
- ], JSON_UNESCAPED_UNICODE);
- // $order_detail->attr = json_encode($goods['service'], JSON_UNESCAPED_UNICODE);
- } else {
- $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE);
- }
- $goods_info = Goods::findOne($goods['goods_id']);
- $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic;
- $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name'];
- $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]);
- $order_detail->delivery_type = $mch['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : ($mch['send'] == 'delivery' ? OrderDetail::GOODS_DELIVERY_IM : OrderDetail::GOODS_DELIVERY_SHOP);
- if ($mch['send'] == 'shop' && empty(input_params('flag_id'))) {
- $order_detail->shop_id = $goods['shop_id'];
- }
- if (isset($goods['give']) && $goods['give'] > 0) {
- $order_detail->integral = $goods['give'];
- } else {
- $order_detail->integral = 0;
- }
- $order_detail->integral_price = $goods['integral_price'];
- $attr_id_list = [];
- foreach ($goods['attr_list'] as $item) {
- array_push($attr_id_list, $item['attr_id']);
- }
- //满减价格提示
- //$order_detail->batch_price_tips = $goods['current_batch_price_tips'];
- // 存入点餐的user_id
- if (!empty($goods['user_id'])) {
- $order_detail->user_id = $goods['user_id'];
- }
- if (!$order_detail->save()) {
- return [
- 'code' => 1,
- 'msg' => '订单提交失败,请稍后再重试',
- 'error' => $order_detail->errors[0]
- ];
- }
- if ($this->is_seckill) {
- $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods['goods_id']])
- ->leftJoin(['sg' => SeckillActivity::tableName()], 'sg.id = sag.activity_id')
- ->andWhere(['AND', ['>', 'sg.end_time', time()], ['<', 'sg.start_time', time()], ['sg.is_delete' => 0, 'sag.is_delete' => 0, 'sg.store_id' => get_store_id()]])
- ->select('sag.id sag_id, sg.id, sg.end_time, sag.attr, sag.use_attr, sag.seckill_num, sag.seckill_price, sag.sale_num, sg.order_limit_num, sg.self_limit_num')->asArray()->one();
- if ($seckill_activity_goods) {
- $activity_order_log = new SeckillActivityOrderLog();
- $activity_order_log->order_id = $order->id;
- $activity_order_log->order_detail_id = $order_detail->id;
- $activity_order_log->user_id = get_user_id();
- $activity_order_log->sale_price = $goods['level_price'];
- $activity_order_log->activity_id = $seckill_activity_goods['id'];
- $activity_order_log->activity_goods_id = $seckill_activity_goods['sag_id'];
- $activity_order_log->goods_id = $goods['goods_id'];
- $activity_order_log->num = $goods['num'];
- $activity_order_log->store_id = get_store_id();
- if (!$activity_order_log->save()) {
- debug_log("秒杀信息保存" . json_encode($activity_order_log->errors));
- }
- $order->seckill_order_id = $activity_order_log->id;
- }
- }
- }
- // goods_profit 计算之后存入订单表中
- $order->profit = $goods_profit;
- $order->chain_profit = $chain_goods_profit;
- $orderChainLevelProfit = \app\utils\Share\BonusPool::getOrderLevelProfit(get_store_id(), $chainGoodsList);
- $order->chain_level_value = json_encode($orderChainLevelProfit);
- $order->save();
- // // 计算门店收益
- // if (get_md_id() > 0) {
- $result = OrderForm::findPrice($order->id);
- MdProfit::handleProfit($order->id, $result['original_price'], $result['md_price']);
- if (!empty(input_params('flag_id'))) {
- FoodCart::updateAll(['is_delete' => 1], ['flag_id' => input_params('flag_id')]);
- FoodFlag::updateAll(['status' => 1], ['id' => input_params('flag_id')]);
- }
- // }
- return [
- 'code' => 0,
- 'msg' => ''
- ];
- }
- public function getBookPrice($payPrice, $mch_goods)
- {
- $price = $payPrice;
- foreach ($mch_goods as $goods) {
- if ($goods['product_type'] == 1) {
- $price = 0;
- $goods_id = $goods['goods_id'];
- $goodsInfoObj = Goods::findOne($goods_id);
- if ($goodsInfoObj) {
- $service_book_desc = json_decode($goodsInfoObj->service_book_desc ?: '', true);
- }
- if (!empty($service_book_desc)) {
- if (empty($goods['book'])) {
- return [
- 'code' => 1,
- 'msg' => '预约日期数据为空'
- ];
- }
- foreach ($goods['book'] as $value) {
- $book_date = strtotime($value['date']);
- foreach ($service_book_desc as $service_book_item) {
- $service_book_date = strtotime($service_book_item['date']);
- if ($book_date === $service_book_date) {
- $price += $service_book_item['price'];
- }
- }
- // if ($num_config[$value['date']] < $goods['num']) {
- // return [
- // 'code' => 1,
- // 'msg' => '选中日期' . $value['date'] . '内暂无房源'
- // ];
- // }
- // $price += $data_config[$value['date']];
- }
- $price = $price * $goods['num'];
- }
- }
- if ($goods['product_type'] == 2) {
- $price = 0;
- $goods_book = GoodsBook::findOne(['goods_id' => $goods['id']]);
- $service_book = Json::decode($goods_book->service_book);
- //$service_book = $service_book['data'];
- $date_data = $this->getDateByInterval(7);
- $new_arr = [
- 'data' => []
- ];
- foreach ($date_data as $index => $datum) {
- $new_arr['data'][$index]['date'] = $datum;
- $new_arr['data'][$index]['time'] = $service_book['data'][0]['time'];
- }
- if (!$new_arr) {
- return [
- 'code' => 1,
- 'msg' => '数据异常'
- ];
- }
- $price = 0;
- $service_book = $new_arr['data'];
- $service_book_date = array_column($service_book, NULL, 'date');
- $service_book_time = $service_book_date[$goods['service']['date']]['time'];
- foreach ($service_book_time as $m) {
- foreach ($m['times'] as $n) {
- if ($n['time'] == $goods['service']['time']) {
- $price = $goods['service']['price'] * $goods['num'];
- break;
- }
- }
- if ($price > 0) {
- break;
- }
- }
- }
- }
- \Yii::error($price);
- return $price;
- }
- /**
- * 检查当前时间是否为指定时间范围内
- * @param $begin 20:00:00
- * @param $end 08:00:00
- * @param $date
- * @return bool true 时间范围内
- */
- private function checkTimeBeginEnd ($begin, $end, $date = null)
- {
- if (empty($begin) && empty($end)) {
- return true;//
- }
- $date = is_null ($date) ? date ('H:i:s') : $date;
- $curTime = strtotime ($date); // 当前时分
- $assignTime1 = strtotime ($begin); // 获得指定分钟时间戳,00:00
- $assignTime2 = strtotime ($end); // 获得指定分钟时间戳,01:00
- $result = false;
- if ($assignTime1 > $assignTime2) {
- //跨24点
- if ($curTime > $assignTime1 || $curTime < $assignTime2) {
- $result = true;
- }
- }
- if ($curTime > $assignTime1 && $curTime < $assignTime2) {
- $result = true;
- }
- if ($assignTime1 == $assignTime2) {
- $result = true;
- }
- return $result;
- }
- }
|