| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\order;
- use app\constants\OptionSetting;
- use app\jobs\SetCloudGoodsStatus;
- use app\models\Address;
- use app\models\Admin;
- use app\models\AgentGoodsBindGoods;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cart;
- use app\models\Cat;
- use app\models\common\CommonGoods;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\DeliveryRules;
- use app\models\DistrictArr;
- use app\models\FoodCart;
- use app\models\FoodFlag;
- use app\models\Form;
- use app\models\FreeDeliveryRules;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\IntegralAppreciationGoods;
- use app\models\IntegralAppreciationPool;
- use app\models\IntegralAppreciationUser;
- use app\models\Level;
- use app\models\Mch;
- use app\models\MchGoodsCat;
- use app\models\MchOption;
- use app\models\MchPlugin;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\MdGroupActivities;
- use app\models\MdGroupActivitiesGoods;
- use app\models\OfferPrice;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PostageRules;
- use app\models\SaasUser;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use app\models\Shop;
- use app\models\Store;
- use app\models\TerritorialLimitation;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\CartOffline;
- use app\models\District;
- use app\models\WechatIntracityService;
- use app\models\Worker;
- use app\models\WorkerGoods;
- use app\models\WorkerGoodsExt;
- use app\models\WorkerOrderExt;
- use app\models\WorkerSetting;
- use app\plugins\adopt\models\AdoptGoods;
- use app\plugins\adopt\models\AdoptSetting;
- use app\utils\Delivery\Delivery;
- use app\utils\Delivery\Alipay\ADelivery;
- use app\utils\OrderNo;
- use app\models\VerifyCard;
- use app\models\VerifyCardSale;
- use app\models\ActivityWechatRoom;
- use app\models\ActivityWechatRoomGoods;
- use app\models\ActivityNewUser;
- use app\models\ActivityNewUserGoods;
- use app\models\ActivityCutPrice;
- use app\models\ActivityCutPriceGoods;
- use app\models\ActivityCutPriceOrder;
- use app\utils\Tools;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\modules\admin\models\maiyatian\MaiyatianForm;
- use app\modules\admin\models\keloop\KeloopForm;
- use app\models\FugouCart;
- class OrderForm extends Model
- {
- public $mch_list;
- public $address_id;
- public $worker_id;
- public $_from;
- public $longitude;
- public $latitude;
- public $send_price;
- public $balance;
- public $take_price;
- public $store_id;
- public $store;
- public $user_id;
- public $verify_card_id;
- /** @var User $user */
- protected $user;
- protected $address;
- protected $worker;
- protected $level;
- protected $travel;
- protected $integral;
- protected $keyword;
- public $order_type; //1认养商品
- public $goods_count_activity_new_user = [];
- public $goods_count_activity_cut_price = [];
- public $activity_cut_price_order_id = 0;
- public $activity_wechat_room_id = 0;
- public $order_no = '';
- public $invoiceConf = [];
- public $is_seckill;
- public $is_wastore;
- public $traceId;
- public $is_giving_gifts;
- public $giving_gifts_data;
- public $is_worker;
- public $send;
- public $pre_order_data;
- public function reInitMchList()
- {
- if ($this->activity_cut_price_order_id) {
- $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id);
- $this->mch_list = json_decode($activityOrder['order_mch_list'], true);
- }
- }
- public function rules()
- {
- $rules = [
- ['mch_list', 'required'],
- ['address_id', 'integer'],
- ['verify_card_id', 'integer'],
- ['order_type', 'integer'],
- [['order_type',], 'default', 'value' => 0],
- ['mch_list', function ($attr, $params) {
- if (!is_array($this->mch_list)) {
- $data = Json::decode($this->mch_list);
- if (!$data) {
- $this->addError($attr, "{$attr}数据格式错误。");
- }
- $this->mch_list = $data;
- }
- }],
- ['mch_list', function ($attr, $params) {
- $this->_filter_mch_list();
- foreach ($this->mch_list as $i => &$mch) {
- if (!is_array($mch['goods_list'])) {
- $this->addError($attr, "{$attr}[{$i}]['goods_list']必须是一个数组。");
- return;
- }
- }
- }],
- ['invoiceConf', function ($attr, $params) {
- $this->invoiceConf = is_string($this->invoiceConf) ? json_decode($this->invoiceConf, true) : $this->invoiceConf;
- }],
- [['longitude', 'latitude', '_from'], 'trim'],
- [['_from'], 'in', 'range' => ['app', 'mini', 'h5', 'official']],
- [['activity_cut_price_order_id', 'is_seckill', 'is_giving_gifts', 'giving_gifts_data', 'is_worker', 'worker_id', 'activity_wechat_room_id', 'order_no', 'traceId', 'is_wastore', 'pre_order_data'], 'safe'],
- ];
- return $rules;
- }
- public function _filter_mch_list()
- {
- try {
- if (!is_array($this->mch_list)) {
- $data = Json::decode($this->mch_list);
- if (!$data) {
- $this->addError('mch_list', "attr参数错误1");
- }
- $this->mch_list = $data;
- }
- if (isset($this->mch_list['_filter_func'])) {
- $_filter_func = $this->mch_list['_filter_func'];
- $this->mch_list = $this->$_filter_func($this->mch_list);
- }
- } catch (\Exception $ex) {
- \Yii::error($ex);
- $this->addError('mch_list', "attr参数错误2。" . $ex->getMessage());
- return;
- }
- return $this->mch_list;
- }
- public function _filter_func_ali_goodsDetailPlugin($param)
- {
- $mch_item = [];
- $data = $param['data'];
- $goods_id = Goods::goodsIdOriginal($data['outItemId']);
- $goods = Goods::findOne($goods_id);
- $attrs = json_decode($goods['attr'], true);
- $mch_item['product_type'] = $goods['product_type'];
- $mch_item['mch_id'] = $goods['mch_id'];
- $goods_item = [
- 'goods_id' => $goods['id'],
- 'num' => $data['count'],
- 'attr' => [],
- ];
- $attr = $attrs[0];
- $sku = Goods::skuAttrOriginal($goods, $data['outSkuId']);
- if ($sku) {
- $attr = $sku;
- }
- $attr_id_list = array_column($attr['attr_list'], 'attr_id');
- sort($attr_id_list);
- $attr_list = Attr::find()->alias('a')
- ->select('attr_group_id, a.id AS attr_id, a.attr_name')
- ->where(['a.id' => $attr_id_list])
- ->asArray()->all();
- $goods_item['attr'] = $attr_list;
- $mch_item['goods_list'] = [$goods_item];
- $mch_list = [$mch_item];
- return $mch_list;
- }
- public function afterValidate()
- {
- $this->user = User::findOne($this->user_id);
- $this->level = $this->getLevelData();
- $this->address = $this->getAddressData();
- $this->worker = $this->getWorkerData();
- $this->travel = $this->getWorkerTravelPrice();
- $this->integral = [
- 'forehead' => 0,
- 'forehead_integral' => 0,
- 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRATION, get_store_id(), 'store')['value'])['value']
- ];
- parent::afterValidate();
- }
- /**
- * @throws \Exception
- */
- protected function getMchListData($submit = false)
- {
- $submit || $this->reInitMchList();
- if($submit){
- $submitMchList = $this->mch_list;
- }
- $delivery_type = [
- 'express',//快递
- 'shop',//门店自提
- 'delivery',//同城配送
- 'online'//在线支付(虚拟产品或其他非普通)
- ];
- // 先处理订单商品 为拆单作准备
- foreach($this->mch_list as $i => &$mch){
- $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit, $mch['list']);
- if (empty($mch['goods_list'])) {
- throw new \Exception($mch['goods_list']['msg'] ?? '参数错误', 1);
- }
- if ($mch['goods_list']['code'] === 1) {
- throw new \Exception($mch['goods_list']['msg'], 1);
- }
- //获取商品配送方式 获取所有商品配送方式的交集 如果没有交集就报错
- $goods_delivery_type_arr = array_column($mch['goods_list'], 'delivery_type');
- $goods_delivery_type_init = $delivery_type;
- foreach ($goods_delivery_type_arr as $item) {
- $goods_delivery_type_init = array_values(array_intersect($goods_delivery_type_init, $item));
- }
- if (empty($goods_delivery_type_init)) {
- throw new \Exception('多种商品未设置相同的配送方式', 1);
- }
- $delivery_type = array_values(array_intersect($delivery_type, $goods_delivery_type_init));
- if (!empty($delivery_type)) {
- foreach ($mch['goods_list'] as &$goods_item) {
- $goods_item['delivery_type'] = $delivery_type;
- }
- }
- }
- if (empty($delivery_type)) {
- throw new \Exception('下单商品未设置相同的配送方式', 1);
- }
- $checkMchList = \app\modules\admin\models\localPublicRanking\LocalPublicRankingForm::checkMchList($this->store_id, $this->mch_list);
- if($checkMchList['code']){
- return $checkMchList;
- }
- // 根据不同的到货时间进行拆单
- foreach($this->mch_list as &$value){
- // 只有一个商品
- if(count($value['goods_list']) == 1){
- continue;
- }
- // 根据商品的到货时间 进行拆单
- $goodsListTemp = [];
- foreach($value['goods_list'] as $goodsValue){
- // 当商品没有设置到货规则的时候 统一默认按0处理
- $goodsListTemp[$goodsValue['delivery_rules'] ? $goodsValue['delivery_rules']['arrival_time'] : 0][] = $goodsValue;
- }
- $goodsListTemp = array_values($goodsListTemp);
- foreach($goodsListTemp as $temKey=>$temValue){
- if($temKey == 0) {
- $value['goods_list'] = $temValue;
- }else{
- $this->mch_list[] = [
- 'mch_id' => $value['mch_id'],
- 'goods_list' => $temValue
- ];
- }
- }
- }
- unset($value);
- // 再根据不同活动拆单
- foreach($this->mch_list as &$value){
- // 只有一个商品
- if(count($value['goods_list']) == 1){
- continue;
- }
- $goodsListTemp = [];
- foreach($value['goods_list'] as $goodsValue){
- $goodsListTemp[$goodsValue['md_group_activities_id'] ?? 0][] = $goodsValue;
- }
- $goodsListTemp = array_values($goodsListTemp);
- foreach($goodsListTemp as $temKey=>$temValue){
- if($temKey == 0) {
- $value['goods_list'] = $temValue;
- }else{
- $this->mch_list[] = [
- 'mch_id' => $value['mch_id'],
- 'goods_list' => $temValue
- ];
- }
- }
- }
- unset($value);
- //20240729加根据云仓商品(为仓库配送与非仓库配送进行拆单)与非云仓商品进行拆单
- foreach($this->mch_list as &$value){
- // 只有一个商品
- if(count($value['goods_list']) == 1){
- continue;
- }
- $goodsListTemp = [];
- foreach($value['goods_list'] as $goodsValue){
- $goods_index = 0;
- //如果是云仓商品
- if (intval($goodsValue['cloud_goods_id']) && !intval($goodsValue['is_wholesale'])) {
- $goods_index = 1;
- //是仓库配送产品
- if (intval($goodsValue['is_front_delivery'])) {
- $goods_index = 2;
- }
- //是仓库配送产品
- if (intval($goodsValue['is_front_centralize'])) {
- $goods_index = 3;
- }
- }
- $goodsListTemp[$goods_index][] = $goodsValue;
- }
- $goodsListTemp = array_values($goodsListTemp);
- foreach($goodsListTemp as $temKey=>$temValue){
- if($temKey == 0) {
- $value['goods_list'] = $temValue;
- }else{
- $this->mch_list[] = [
- 'mch_id' => $value['mch_id'],
- 'goods_list' => $temValue
- ];
- }
- }
- }
- unset($value);
- //20240729加根据是否是增值积分产品下单
- foreach($this->mch_list as &$value){
- // 只有一个商品
- if(count($value['goods_list']) == 1){
- continue;
- }
- $goodsListTemp = [];
- foreach($value['goods_list'] as $goodsValue){
- $goods_index = 0;
- //如果是增值积分商品
- $integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goodsValue['id'], 'is_delete' => 0]);
- if ($integralAppreciationGoods && $integralAppreciationGoods->integral_amount > 0) {
- $goods_index = 1;
- }
- $goodsListTemp[$goods_index][] = $goodsValue;
- }
- $goodsListTemp = array_values($goodsListTemp);
- foreach($goodsListTemp as $temKey=>$temValue){
- if($temKey == 0) {
- $value['goods_list'] = $temValue;
- }else{
- $this->mch_list[] = [
- 'mch_id' => $value['mch_id'],
- 'goods_list' => $temValue
- ];
- }
- }
- }
- unset($value);
- unset($mch);
- foreach ($this->mch_list as $i => &$mch) {
- $mch['mch_id'] = $mch['goods_list'][0]['mch_id'];
- // 将获取订单商品的方法提到上面单独执行
- // $mch['goods_list'] = $this->getGoodsList($mch['goods_list'], $submit);
- // if (empty($mch['goods_list'])) {
- // throw new \Exception($mch['goods_list']['msg'] ?? '参数错误', 1);
- // }
- // if ($mch['goods_list']['code'] === 1) {
- // throw new \Exception($mch['goods_list']['msg'], 1);
- // }
- $shop_count_result = $this->checkCatShopCount($mch['goods_list']);
- if ($shop_count_result['code'] !== 0) {
- return $shop_count_result;
- }
- $is_integral = false;
- // if ($mch['mch_id']) {
- if( isset($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_INTEGRAL){
- $integral_name = Option::get(OptionSetting::STORE_INTEGRAL_NAME, $this->store_id, 'gift', Option::get(OptionSetting::STORE_INTEGRAL_NAME, $this->store_id, 'store', '积分')['value'])['value'];
- $mch['name'] = $integral_name.'商品';
- $is_integral = true;
- }else{
- $mch['name'] = '平台自营';
- }
- // if ($submit == false) {
- // $mch['form'] = $this->getFormData();
- // } else {
- // $mch['form'] = $this->getForm($mch['form']);
- // }
- $send_type_arr = [];
- if (!$this->is_worker) {
- $send_type_arr = Order::getSendTypeArr(get_store_id(), $submit, $mch['goods_list'], ['mini_id' => get_mini_id(), 'address' => $this->address, 'delivery_time' => $mch['delivery_time']]);
- }
- // } else {
- if ($mch['mch_id']) {
- $_mch = Mch::findOne([
- 'store_id' => $this->store_id,
- 'id' => $mch['mch_id'],
- ]);
- if (!$_mch) {
- unset($this->mch_list[$i]);
- continue;
- }
- // $send_type_arr = [];
- // if (!$this->is_worker) {
- //
- // if ($_mch['express_delivery']) {
- // $send_type_arr[] = 'express';
- // }
- // if ($_mch['self_delivery']) {
- // $send_type_arr[] = 'shop';
- // }
- // }
- $mch['name'] = $_mch->name;
- // $mch['form'] = null;
- // $mch['shop_list'] = [(object)[
- // 'address' => $_mch->address,
- // 'distance' => '',
- // 'id' => $_mch->id,
- // 'is_default' => 1,
- // 'latitude' => $_mch->latitude,
- // 'longitude' => $_mch->longitude,
- // 'mobile' => $_mch->tel,
- // 'name' => $_mch->name,
- // ]];
- }
- if (!$this->is_worker) {
- unset($goods_item);
- foreach ($mch['goods_list'] as $goods_index => $goods_item) {
- if ($goods_item['product_type'] == 3) {
- array_push($send_type_arr, 'online');
- // array_push($mch['goods_list'][$goods_index]['delivery_type'], 'express');
- // array_push($goods_item['delivery_type'], 'express');
- }
- $goods_delivery_type = $goods_item['delivery_type'];
- if (empty($goods_delivery_type)) {
- $goods_delivery_type = ['express'];
- }
- if (empty(array_intersect($send_type_arr, $goods_delivery_type))) {
- unset($mch['goods_list'][$goods_index]);
- }
- }
- $mch['goods_list'] = array_values($mch['goods_list']);
- if (empty($mch['goods_list'])) {
- return [
- 'code' => 1,
- 'msg' => '当前商品不支持当前配送方式'
- ];
- }
- $delivery_type_arr = array_column($mch['goods_list'], 'delivery_type');
- $delivery_type_ = [];
- foreach ($delivery_type_arr as $delivery_type_time) {
- $delivery_type_ = array_merge($delivery_type_, $delivery_type_time) ;
- }
- // if (in_array('online', $delivery_type_) && !in_array('shop', $delivery_type_)) {
- // array_push($delivery_type_, 'shop');
- // }
- // if (in_array('online', $send_type_arr) && !in_array('shop', $send_type_arr)) {
- // array_push($send_type_arr, 'shop');
- // }
- $delivery_type_ = array_unique($delivery_type_);
- $delivery_type_ = array_values($delivery_type_);//商品中的
- $delivery_type_ = array_values(array_intersect($send_type_arr, $delivery_type_));
- if (!empty($delivery_type_)) {
- $send_type_arr = $delivery_type_;
- }
- $send_type_arr = array_unique($send_type_arr);
- $mch['send_type'] = $send_type_arr;
- $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 (\Yii::$app->prod_hide_city_delivery() && $local_type['value'] == 'aggregate') { // 同城配送
- $mch['send_type'] = array_diff($mch['send_type'], ['delivery']);
- }
- if (in_array('shop', $send_type_arr)) {
- $shopArr = $this->getShopList();
- $mch['is_shop'] = $shopArr['shop'];
- $mch['shop_list'] = $shopArr['list'];
- } else {
- $mch['shop_list'] = [];
- $mch['is_shop'] = '';
- }
- } else {
- $mch['send_type'] = ['express'];
- }
- $total_price = 0;
- $level_price = 0;
- $final_price = 0;
- $pre_price = 0;
- $payment_type = 0;
- $next_level_price = 0;
- $integral = [
- 'forehead' => 0,
- 'forehead_integral' => 0
- ];
- $mch['plugin_type'] = isset($mch['plugin_type']) ? $mch['plugin_type'] : 0;
- $open = false;
- if ($this->is_seckill) {
- foreach ($mch['goods_list'] as &$mch_goods) {
- $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $mch_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('sg.is_use_coupon')->asArray()->one();
- if ($seckill_activity_goods && (int)$seckill_activity_goods['is_use_coupon'] === 0) {
- $open = true;
- }
- }
- }
- //计算每个等级下单金额
- $level_price_arr = [];
- $userLevelIntegralEnlarge = 1;
- if($this->level != -1){
- $level = Level::findOne(['level'=>$this->level, 'store_id' => $this->store_id,'is_delete'=>Level::NOT_DELETE,'status' => Level::STATUS_TRUE]);
- if($level){
- $userLevelIntegralEnlarge = $level->integral_enlarge ?? 1;
- }
- }
- $integral_appreciation_integral = 0;
- $integral_appreciation_price = 0;
- foreach ($mch['goods_list'] as &$_goods) {
- //计算每个等级下单金额
- if (!empty($_goods['level_price_arr'])) {
- foreach ($_goods['level_price_arr'] as &$level_price_item) {
- $level_price_item *= $_goods['num'];
- }
- if (empty($level_price_arr)) {
- $level_price_arr = $_goods['level_price_arr'];
- } else {
- foreach ($level_price_arr as $level_index => $level_) {
- $level_price_arr[$level_index] += $_goods['level_price_arr'][$level_index];
- $level_price_arr[$level_index] = sprintf('%.2f', $level_price_arr[$level_index]);
- }
- }
- }
- if (in_array($_goods['product_type'], [1, 2])) {
- $mch['send_type'] = ['shop'];
- }
- if (in_array($_goods['product_type'], [1, 2]) && $submit == true) {
- $res = $this->bookCheckGoodsNum($_goods, $_goods['product_type']);
- if ($res['code'] != 0) {
- throw new \Exception($res['msg'], 1);
- }
- }
- if ($submit == false) {
- $_goods = array_merge($_goods, [
- 'form' => []
- ]);
- $_goods['form'] = $this->getNewFormData($_goods);
- } else {
- $_goods['form'] = $this->getForm($_goods['form']);
- }
- if ((int)$_goods['product_type'] === 2) {
- $_goods['level_price'] = sprintf('%.2f', $mch['list']['price']);
- $_goods['price'] = $mch['list']['price'];
- }
- if (!$this->is_worker) {
- $total_price += doubleval($_goods['price']);
- $level_price += doubleval($_goods['level_price']) > 0 ? doubleval($_goods['level_price']) : doubleval($_goods['price']);
- $next_level_price += doubleval($_goods['next_level_price']) > 0 ? doubleval($_goods['next_level_price']) : doubleval($_goods['price']);
- //计算每个产品最大可抵扣多少金额
- $integral_appreciation_integral = bcadd($integral_appreciation_integral, $_goods['integral_appreciation_amount'], 2);
- $integral_appreciation_price = bcadd($integral_appreciation_price, $_goods['integral_appreciation_price'], 2);
- } else {
- if ($_goods['payment_type']) {
- $payment_type = 1;
- $level_price += doubleval($_goods['pre_price']);
- $total_price += doubleval($_goods['pre_price']);
- $final_price += doubleval($_goods['final_price']);
- $pre_price += doubleval($_goods['pre_price']);
- } else {
- $level_price += doubleval($_goods['price']);
- $total_price += doubleval($_goods['price']);
- $pre_price += doubleval($_goods['price']);
- }
- }
- $_goods['integral'] = [
- 'forehead' => doubleval($_goods['resIntegral']['forehead']),
- 'forehead_integral' => doubleval($_goods['resIntegral']['forehead_integral'])
- ];
- $integral['forehead'] += doubleval($_goods['resIntegral']['forehead']);
- $integral['forehead_integral'] += doubleval($_goods['resIntegral']['forehead_integral']);
- unset($_goods['is_form']);
- unset($_goods['form_name']);
- // 处理下单用户会员等级中的积分放大倍数
- $_goods['give'] = $_goods['give'] > 0 ? floor($_goods['give'] * $userLevelIntegralEnlarge) : 0;
- }
- $integral['forehead'] = round($integral['forehead'], 2);
- $mch['total_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $total_price);
- $mch['level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', $level_price);
- //获取当前mch中增值积分最大可抵扣商品金额
- $mch['integral_appreciation_integral'] = $integral_appreciation_integral ?: 0;
- $mch['integral_appreciation_price'] = $integral_appreciation_price ?: 0;
- $mch['level_price_arr'] = $level_price_arr;
- $mch['payment_type'] = $payment_type;
- $mch['diy_send_type'] = [
- [
- 'key' => 'express',
- 'name' => Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送')['value'] ?: '快递配送'
- ],
- [
- 'key' => 'shop',
- 'name' => Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送')['value'] ?: '自提配送'
- ],
- [
- 'key' => 'delivery',
- 'name' => Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送')['value'] ?: '同城配送'
- ]
- ];
- if ($this->is_worker) {
- $mch['final_price'] = floatval(sprintf('%.2f', $final_price));
- $mch['pre_price'] = floatval(sprintf('%.2f', $pre_price));
- $mch['integral'] = [];
- $mch['coupon_list'] = [];
- } else {
- //计算下级等级金额
- $next_level = null;
- //获取下一会员等级
- $next_level = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => Level::NOT_DELETE
- ])->andWhere(['AND', ['>', 'level', get_user()->level], ['>', 'price', 0]])->select('id, level, name, discount, price')->orderBy('level ASC')->asArray()->one();
- if (!empty($next_level)) {
- $next_level['diff_price'] = 0;
- if ($mch['level_price'] > $next_level_price) {
- $next_level['diff_price'] = sprintf('%.2f',($mch['level_price'] - $next_level_price));
- }
- $mch['next_level_price'] = !empty($this->verify_card_id) ? 0.00 : sprintf('%.2f', ($next_level_price + $next_level['price']));
- }
- if ($next_level['diff_price'] <= 0) {
- $next_level = null;
- }
- $mch['next_level'] = $next_level;
- $mch['integral'] = !empty($this->verify_card_id) ? [] : $integral;
- if($is_integral || $open){
- //积分兑换商品不能使用优惠券 或 秒杀活动中不允许使用优惠券则所有商品不允许使用优惠券
- $mch['coupon_list'] = [];
- }else{
- $this->getCouponList($mch);
- }
- }
- if (!$this->is_worker) {
- $mch['express_price'] = $this->getExpressPrice($mch);
- } else {
- $result = $this->travel;
- $mch['travel_price'] = floatval(sprintf('%.2f', $result['travelPrice']));
- $mch['express_price'] = 0;
- }
- if (!$this->is_worker) {
- $mch['offer_rule'] = $this->getOfferRule($mch);
- // if(empty(input_params('flag_id'))){
- $mch['is_area'] = $this->getTerritorialLimitation($mch);
- // }
- }
- // 如果是点餐的商品,结构需要变一下
- if ((!empty(input_params('flag_id')) || !empty(input_params('table_num'))) && !$submit) {//get_md_id() &&
- $user_list = [];
- $user_ids = array_unique(array_column($mch['goods_list'], 'user_id'));
- foreach ($user_ids as $user_id) {
- $goods_list = [];
- foreach ($mch['goods_list'] as $item) {
- if ($item['user_id'] == $user_id) {
- $goods_list[] = $item;
- }
- }
- $saas_user = SaasUser::findOne(['mobile' => User::findOne($user_id)->binding]);
- $user_list[] = [
- 'name' => $saas_user->name,
- 'avatar' => $saas_user->avatar,
- 'user_id' => $user_id,
- 'goods_list' => $goods_list
- ];
- }
- $mch['goods_list'] = $user_list;
- }
- $tax_price = \app\modules\admin\models\InvoiceForm::tax_price($this->store_id, $mch['total_price']);
- $mch['tax_price'] = $tax_price;
- }
- // unset($mch_item);
- // $coupon_id_arr = [];
- // foreach ($this->mch_list as &$mch_item) {
- //
- // if ($mch_item['coupon_list']) {
- // foreach ($mch_item['coupon_list'] as $coupon_index => $coupon_item) {
- // foreach ($coupon_id_arr as $coupon_id_item) {
- // if ($coupon_item['id'] == $coupon_id_item) {
- // unset($mch_item['coupon_list'][$coupon_index]);
- // }
- // }
- // array_push($coupon_id_arr, $coupon_item['id']);
- // }
- // $mch_item['coupon_list'] = array_values($mch_item['coupon_list']);
- //
- // }
- // }
- // 随机立减
- $mchList = &$this->mch_list;
- unset($_mchItem);
- foreach($mchList as &$_mchItem){
- if(!$submit){
- $_mchItem['rand_discount_price'] = 0;
- $_mchItem['max_rand_discount_price'] = 0;
- $inTypes = [Goods::GOODS_TYPE_NORMAL, Goods::GOODS_TYPE_VIRTUAL];
- $gTypes = array_column($_mchItem['goods_list'], 'product_type');
- if(array_diff($gTypes, $inTypes)){
- continue;
- }
- $randDiscountOption = \app\utils\RandDiscount::getOption(\get_store_id(), $_mchItem['total_price']);
- if ($randDiscountOption['is_open'] && (float)$randDiscountOption['rand'] > 0) {
- $_mchItem['max_rand_discount_price'] = $randDiscountOption['rand'];
- }
- }else{
- if ($_mchItem['rand_discount_price'] && $_mchItem['max_rand_discount_price'] < $_mchItem['rand_discount_price']) {
- return [
- 'code' => 1,
- 'msg' => '非法请求!',
- ];
- }
- }
- }
-
-
- if($submit){
- $store_integral = Option::get(\app\constants\OptionSetting::STORE_INTEGRAL, $this->store_id, 'gift', Option::get(\app\constants\OptionSetting::STORE_INTEGRAL, $this->store_id, 'store')['value'])['value'];
- foreach($this->mch_list as $i => &$_mchItem){
- $_mchItem['rand_discount_price'] = $submitMchList[$i]['rand_discount_price'];
- $_mchItem['use_integral'] = $submitMchList[$i]['use_integral'];
- $_mchItem['integral'] = $submitMchList[$i]['integral'];
- $_mchItem['use_balance'] = $submitMchList[$i]['use_balance'];
- $_mchItem['integral']['forehead_integral'] = sprintf("%.2f", $_mchItem['integral']['forehead'] * $store_integral);
- $_mchItem['resIntegral'] = $_mchItem['integral'];
- foreach($_mchItem['goods_list'] as $g => &$_mchGoodsItem){
- $_mchGoodsItem['rand_discount_price'] = $submitMchList[$i]['goods_list'][$g]['rand_discount_price'];
- $_mchGoodsItem['take_price'] = $submitMchList[$i]['goods_list'][$g]['take_price'];
- $_mchGoodsItem['balance'] = $submitMchList[$i]['goods_list'][$g]['balance'];
- $_mchGoodsItem['integral'] = $submitMchList[$i]['goods_list'][$g]['integral'];
- $_mchGoodsItem['integral']['forehead_integral'] = sprintf("%.2f", $_mchGoodsItem['integral']['forehead'] * $store_integral);
- $_mchGoodsItem['resIntegral'] = $_mchGoodsItem['integral'];
- }
- }
- }
- return $this->mch_list;
- }
- //分类起购数量 修正为当前分类下所有商品数量未达到数量后警告
- private function checkCatShopCount($goodsList)
- {
- $new_goods_list = [];
- foreach ($goodsList as $item) {
- $cat_id_arr = GoodsCat::find()->where(['goods_id' => $item['goods_id'], 'is_delete' => 0])->select('cat_id')->column();
- foreach ($cat_id_arr as $goods_cat_id) {
- $open = true;
- foreach ($new_goods_list as $new_goods_item) {
- if ($new_goods_item['cat_id'] === $goods_cat_id) {
- $new_goods_list[$goods_cat_id]['goods_list'][] = [
- 'goods_id' => $item['goods_id'],
- 'num' => $item['num']
- ];
- $open = false;
- }
- }
- if ($open) {
- $new_goods_list[$goods_cat_id]['cat_id'] = $goods_cat_id;
- $cat = Cat::find()->where(['id' => $goods_cat_id])
- ->select('shop_count, name')->asArray()->one();
- $new_goods_list[$goods_cat_id]['shop_count'] = $cat['shop_count'] ?: 0;
- $new_goods_list[$goods_cat_id]['name'] = $cat['name'] ?: '';
- $new_goods_list[$goods_cat_id]['goods_list'][] = [
- 'goods_id' => $item['goods_id'],
- 'num' => $item['num']
- ];
- }
- }
- }
- foreach ($new_goods_list as $index => $goods_item) {
- if ($goods_item['shop_count'] > 0) {
- $goods_num_arr = array_column($goods_item['goods_list'], 'num');
- if (array_sum($goods_num_arr) < $goods_item['shop_count']) {
- return [
- 'code' => 1,
- 'msg' => '分类' . $goods_item['name'] . "起购数量为" . $goods_item['shop_count'] . ',当前购买数量为' . array_sum($goods_num_arr),
- ];
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '检测起购数量完成',
- ];
- }
- //$list预约商品前端会传这个
- protected function getGoodsList($goods_list, $submit, $list = [])
- {
- // 获取卡券绑定的运费模板id
- $VerifyCardSale = VerifyCardSale::findOne(['id' => $this->verify_card_id]);
- $VerifyCard = null;
- if ($VerifyCardSale) {
- $VerifyCard = VerifyCard::findOne(['id' => $VerifyCardSale['verify_card_id']]);
- }
- $goodsIds = [];
- $cards = [];
- $send_price = 0;
- $take_price = 0;
- $goods_integral_amount = 0;
- foreach ($goods_list as $i => &$item) {
- $food_ext_goods = [];
- if (isset($item['fugou_cart_id'])) {
- $cart = FugouCart::findOne([
- 'store_id' => get_store_id(),
- 'id' => $item['fugou_cart_id'],
- ]);
- if (!$cart) {
- unset($goods_list[$i]);
- continue;
- }
- $item['num'] = $cart->num;
- $attr_id_list = (array)Json::decode($cart->attr);
- $goods = Goods::findOne($cart->goods_id);
- $goods->price = $cart->price;
- } elseif (isset($item['cart_id'])) {
- $cart = Cart::findOne([
- 'store_id' => get_store_id(),
- 'id' => $item['cart_id'],
- 'is_delete' => 0
- ]);
- if (!$cart) {
- unset($goods_list[$i]);
- continue;
- }
- $item['num'] = $cart->num;
- $attr_id_list = (array)Json::decode($cart->attr);
- $goods = Goods::findOne($cart->goods_id);
- } elseif (isset($item['food_cart_id'])) {
- $cart = FoodCart::findOne([
- 'id' => $item['food_cart_id'],
- 'status' => 1,
- 'is_delete' => 0
- ]);
- if (!$cart) {
- unset($goods_list[$i]);
- continue;
- }
- $food_ext_goods = $cart->food_ext_goods;
- $item['food_ext_goods'] = (array)json_decode($food_ext_goods, true);
- $item['num'] = $cart->num;
- $attr_id_list = (array)Json::decode($cart->attr);
- $goods = Goods::findOne($cart->goods_id);
- $user_id = $cart->user_id;
- } elseif ($item['goods_id']) {
- $attr_id_list = [];
- if ($item['attr']) {
- foreach ($item['attr'] as $_a) {
- array_push($attr_id_list, $_a['attr_id']);
- }
- }
- $goods = Goods::findOne([
- 'store_id' => get_store_id(),
- 'id' => $item['goods_id'],
- ]);
- if ($goods->product_type === Goods::GOODS_TYPE_WORKER) {
- $attr_id_list = [];
- // $attr = json_decode($goods->attr, true)[0];
- foreach ($item['attr'] as $_a) {
- array_push($attr_id_list, $_a['attr_id']);
- }
- $worker_goods_ext = WorkerGoodsExt::findOne(['goods_id' => $item['goods_id']]);
- if ($worker_goods_ext && intval($worker_goods_ext->bind_worker) === 1 && intval($this->worker['id']) === 0) {
- throw new \Exception('请选择技师');
- }
- }
- } else {
- unset($goods_list[$i]);
- continue;
- }
- if (!$goods) {
- unset($goods_list[$i]);
- continue;
- }
- if ((int)$goods->status === 0) {
- unset($goods_list[$i]);
- continue;
- }
- if (!isset($item['fugou_cart_id'])) { // 复购商品不走此逻辑,会污染价格
- \app\modules\admin\models\jushuitan\JuShuiTanForm::syncStoreJstGoodsQty(get_store_id(), $goods);
- }
- //增值积分产品 获取积分价格
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', $this->store_id, 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
- $pool = IntegralAppreciationPool::findOne(['store_id' => $this->store_id]);
- $integral_price = $pool->integral_price ?: ($integral_appreciation_setting['integral_init_price'] ?: 0);
- $item['integral_appreciation_amount'] = 0;
- //增值积分活动商品检测
- $integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goods->id, 'is_delete' => 0]);
- if ($integralAppreciationGoods && $integralAppreciationGoods->integral_amount > 0) {
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', get_store_id(), 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
- $integral_custom_name = $integral_appreciation_setting['integral_custom_name'] ?: '增值积分';
- $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $this->user_id]);
- if (!$integralAppreciationUser) {
- return [
- 'code' => 1,
- 'msg' => "用户{$integral_custom_name}数量不足"
- ];
- }
- $goods_integral_amount = bcadd(bcmul($integralAppreciationGoods->integral_amount, $item['num'], 2), $goods_integral_amount, 2);
- if ($goods_integral_amount > $integralAppreciationUser->integral) {
- return [
- 'code' => 1,
- 'msg' => "用户{$integral_custom_name}数量不足"
- ];
- }
- $item['integral_appreciation_amount'] = bcmul($integralAppreciationGoods->integral_amount, $item['num'], 2);
- $item['integral_appreciation_price'] = bcmul($integral_price, $item['integral_appreciation_amount'], 2);
- }
- //购买方式检测
- // //判断会员是否可以购买
- // if (!empty($goods->buy_type_info)) {
- // $buy_type_info = json_decode($goods->buy_type_info, true);
- // if ((int)$buy_type_info['type'] === 1) { //会员购买
- // if ((int)$buy_type_info['member_level'] !== 0) {
- // $user = User::findOne($this->user_id);
- // if ((int)$user->level !== (int)$buy_type_info['member_level']) {
- // $level = Level::findOne(['level' => $buy_type_info['member_level']])->name ?? "普通会员";
- // return [
- // 'code' => 1,
- // 'msg' => '当前商品仅支持' . $level . '购买',
- // ];
- // }
- // }
- // }
- // if ((int)$buy_type_info['type'] === 2) { //新用户购买
- // $order = Order::findOne(['user_id' => $this->user_id]);
- // if (!empty($order)) {
- // return [
- // 'code' => 1,
- // 'msg' => "当前商品仅支持新用户购买",
- // ];
- // }
- // }
- // }
- // $cat_id_arr = GoodsCat::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('cat_id')->column();
- // $shop_count = Cat::find()->where(['id' => $cat_id_arr])->andWhere(['>', 'shop_count', 0])->select('shop_count')->min('shop_count') ?: 0;
- // if ($shop_count > 0 && $shop_count > $item['num']) {
- // return [
- // 'code' => 1,
- // 'msg' => $goods->name . "起购数量为" . $shop_count,
- // ];
- // }
- foreach ($attr_id_list as $attr_index => $attr_item) {
- if ($attr_item < 0) {
- unset($attr_id_list[$attr_index]);
- }
- }
- $item['delivery_rules'] = null;
- $item['send_verify_card_num'] = $goods->send_verify_card_num;
- if (!$this->is_worker) {
- $delivery_rules = DeliveryRules::find()->where([
- 'id' => $goods->delivery_rules_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'store_id' => get_store_id()
- ])->select('type, times, days, name')->asArray()->one();
- if ($delivery_rules) {
- $days = "下单" . $delivery_rules['days'] . "天后";
- $primitive = $delivery_rules['times'];
- $delivery_rules['times'] =
- (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
- $delivery_rules['type'] .= '';
- // 计算到货时间
- $delivery_rules['arrival_time'] = (int)$delivery_rules['type'] === 1 ? strtotime("+".$delivery_rules['days']." day",strtotime(date('Y-m-d',time()))) : strtotime(date('Y-m-d',$primitive));
- }
- $item['delivery_rules'] = $delivery_rules;
- }
- if (($goods->confine_count && $goods->confine_count > 0)) {
- $goodsNum = Goods::getBuyNum($this->user, $goods->id);
- if ($goodsNum) {
- } else {
- $goodsNum = 0;
- }
- $goodsTotalNum = intval($goodsNum + $item['num']);
- if ($goodsTotalNum > $goods->confine_count) {
- throw new \Exception('商品:' . $goods->name . ' 只允许购买' . $goods->confine_count . $goods->unit, 1);
- }
- }
- //起订数量
- if (($goods['order_min_count'] && $goods['order_min_count'] > 0)) {
- if ($item['num'] < $goods['order_min_count']) {
- if($submit){
- throw new \Exception('商品:' . $goods->name . ' 起订数量' . $goods['order_min_count'], 1);
- }else{
- $item['_errors']['order_min_count'] = $goods['order_min_count'];
- }
- }
- }
- $attr_info = $goods->getAttrInfo($attr_id_list);
- // 排除当面付检测库存
- if ((in_array($goods->type, [2, 6]) == false && !in_array($goods->product_type, [1, 2, 4, 6]) && $item['num'] > $attr_info['num']) || $item['num'] <= 0) { //库存不足
- return [
- 'code' => 1,
- 'msg' => $goods->name . "库存数量不足,仅剩余" . $attr_info['num'],
- ];
- // unset($goods_list[$i]);
- // continue;
- }
- if (!$this->is_worker) {
- $cards = [];
- if (!empty($goods->verify_card_id)) {
- $ids = explode(',', $goods->verify_card_id);
- $cards = Tools::getVerifyList($ids);
- } else {
- $cards = [];
- }
- }
- $store = Store::findOne(get_store_id());
- $attr_list = Attr::find()->alias('a')
- ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
- ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
- ->where(['a.id' => $attr_id_list, 'ag.store_id' => get_store_id(),])
- ->asArray()->all();
- $item['cost_price'] = $goods->use_attr ? $attr_info['cost_price'] : $goods->cost_price;
- $item['user_id'] = !empty($user_id) ? $user_id : 0;
- $item['attr_list'] = $attr_list;
- $item['cloud_goods_id'] = $goods->cloud_goods_id;
- $item['is_wholesale'] = $goods->is_wholesale;
- $item['cloud_supplier_id'] = $goods->cloud_supplier_id;
- $item['is_front_delivery'] = $goods->is_front_delivery;
- $item['is_front_centralize'] = $goods->is_front_centralize;
- $item['goods_no'] = $attr_info['no'];
- $item['goods_id'] = $goods->id;
- $item['mch_id'] = $goods->mch_id;
- $item['rate'] = $goods->rate;
- $item['rate_type'] = $goods->rate_type;
- $item['chain_rate_type'] = $goods->chain_rate_type;
- $item['chain_rate'] = $goods->chain_rate;
- $item['goods_name'] = $goods->name;
- $item['goods_pic'] = $attr_info['pic'] ? $attr_info['pic'] : $goods->cover_pic;
- $item['balance'] = 0;
- $item['max_balance'] = ($store->balance == 1) ? round($goods->balance, 2) : 0;
- //如果没有使用规格,则使用商品售价 否则使用规格售价
- $item['price'] = sprintf('%.2f', (($goods->use_attr ? $attr_info['price'] : $goods->price) * $item['num']));
- $item['single_price'] = !empty($this->verify_card_id) ? 0 : sprintf('%.2f', $attr_info['price']);
- $item['weight'] = $goods->weight;
- if (!empty($this->verify_card_id)) {
- $item['integral'] = 0;
- } else {
- $item['integral'] = $goods->integral ? $goods->integral : 0;
- }
- $item['goods_integral'] = json_decode($goods->integral, true);
- $item['integral_price'] = $goods->integral_price;
- $item['freight'] = !empty($VerifyCard['freight_id']) ? $VerifyCard['freight_id'] : $goods->freight;
- $item['full_cut'] = $goods->full_cut;
- $item['goods_cat_id'] = $goods->cat_id;
- $item['id'] = $goods->id;
- $item['verify_card_id'] = $cards;
- $item['type'] = $goods->type;
- $item['delivery_type'] = Json::decode($goods->delivery_type);
- if (get_md_id() && empty(input_params('flag_id'))) {
- $md = Md::findOne(get_md_id());
- if ($md->is_single) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $md_delivery_type = Json::decode(Md::findOne(get_md_id())->delivery_type);
- $md_goods_delivery_type = Json::decode($md_goods->delivery_type);
- $delivery_type = [];
- foreach ($md_goods_delivery_type as $delivery_type_item) {
- if (in_array($delivery_type_item, $md_delivery_type)) {
- $delivery_type[] = $delivery_type_item;
- }
- }
- $item['delivery_type'] = $delivery_type;
- }
- }
- }
- // 当前选择的规格
- $attrIdArr = [];
- foreach ($item['attr_list'] as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- $GoodsFullMinus = [];
- if (!$this->is_worker) {
- $GoodsFullMinus = GoodsFullMinus::find()
- ->select('*')
- ->where(['store_id' => get_store_id(), 'goods_id' => $goods->id, 'is_delete' => 0])
- ->orderBy('full_minus_num ASC')->asArray()->all();
- }
- // 门店逻辑
- $attr = $goods->attr;
- $price = $goods->price;
- // 独立运营门店不参与活动 团购活动需要写在判断门店之前
- $activityModel = MdGroupActivitiesGoods::find()->alias('mgag')->leftJoin(['mga'=>MdGroupActivities::tableName()],'mga.id=mgag.activities_id')->where(['goods_id'=>$goods->id,'mgag.is_delete'=>0])->andWhere(['>=','mga.end_time',time()])->andWhere(['mga.is_delete'=>0])->asArray()->select('mgag.*,mga.start_time,mga.end_time')->one();
- if($activityModel){
- $attr = $activityModel['attr'];
- $price = $activityModel['price'];
- }
- $item['md_group_activities_id'] = $activityModel ? $activityModel['activities_id'] : 0;
- if (get_md_id() && empty(input_params('flag_id'))) {
- $md = Md::findOne(get_md_id());
- if ($md->is_single) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $attr = $md_goods->attr;
- $price = $md_goods->price;
- }
- }
- }
- if ($goods->product_type == Goods::GOODS_TYPE_DATE) {
- $price = 0;
- $service_book_desc = json_decode($goods->service_book_desc ?: '', true);
- if (!empty($service_book_desc)) {
- if (empty($list)) {
- return [
- 'code' => 1,
- 'msg' => '预约日期数据为空'
- ];
- }
- array_pop($list);
- foreach ($list 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'];
- }
- }
- }
- // $price = $price * $item['num'];
- //判断是否有库存
- $num = Goods::getGoodsNum($goods)['data'];
- foreach ($list as $iattr) {
- $idate = strtotime($iattr['date']);
- foreach ($num as $inum) {
- $indate = strtotime($inum['date']);
- if(($idate == $indate) && ($inum['num'] < $item['num'])){
- return ['code' => 1, 'msg' => $iattr['date'].'库存不足'];
- }
- }
- }
- }
- }
- $res = \app\modules\client\models\v1\common\CommonGoods::currentGoodsAttr([
- 'attr' => $attr,
- 'price' => $price,
- 'is_level' => $goods->is_level,
- 'mch_id' => $goods->mch_id,
- 'full_minus' => $GoodsFullMinus,
- 'unit' => $goods->unit,
- 'use_attr' => $goods->use_attr,
- 'food_ext_goods' => $food_ext_goods,
- ], $attrIdArr, [], $item['num']);
- $item['batch_price_tips'] = $res['batch_price_tips'];
- $item['current_batch_price_tips'] = $res['current_batch_price_tips'];
- $item['price'] = sprintf('%.2f', ($res['price'] * $item['num']));
- $item['level_price'] = sprintf('%.2f', ($res['level_price'] * $item['num']));
- $item['next_level_price'] = sprintf('%.2f', ($res['next_level_price'] * $item['num']));
- $item['is_level'] = $res['is_level'];
- if ($res['level_price_arr']) {
- $item['level_price_arr'] = $res['level_price_arr'];
- }
- $send_integral_profit = Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'gift', Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'store')['value'])['value'];
- $integralArr = $this->getIntegral((object)$item, Option::get(OptionSetting::STORE_INTEGRAL,get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRAL,get_store_id(), 'store')['value'])['value'], $goodsIds, $send_integral_profit);
- $item['give'] = $integralArr['give'];
- $item['resIntegral'] = $integralArr['resIntegral'];
- $item['maxIntegral'] = $integralArr['maxIntegral'];
- $goodsIds[] = $goods->id;
- $item['goods_card_list'] = $this->is_worker ? [] : Goods::getGoodsCard($goods->id);
- $item['is_form'] = $goods->is_form;
- $item['form_name'] = $goods->form_name;
- $item['product_type'] = $goods->product_type;
- $item['pre_price'] = 0; //定金金额
- $item['final_price'] = 0; //尾款金额
- $item['payment_type'] = 0; //支付模式
- if (!$this->is_worker) {
- $res_acnu = $this->getActivityNewUserGoodsPrice($res, $goods, $attr_id_list, $submit, $item['num']);
- if ($res_acnu['code'] === 0) {
- if ($res_acnu['price'] > 0) {
- $item['is_level'] = 0;
- $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_acnu['price']));
- $item['price_str'] = $res_acnu['price_str'];
- }
- } else {
- return $res_acnu;
- }
- if($this->activity_wechat_room_id){
- $res_acwr = $this->getActivityWorkRoomGoodsPrice($res, $goods, $attr_id_list, $submit, $item['num']);
- if ($res_acwr['code'] === 0) {
- $item['is_level'] = 0;
- if ($res_acwr['price'] > 0) {
- $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_acwr['price']));
- $item['price_str'] = $res_acwr['price_str'];
- }
- } else {
- return $res_acwr;
- }
- }
- $item['send_price'] = bcmul($goods->goods_send_profit * 0.01, $item['level_price'], 2);
- $item['take_price'] = bcmul($goods->goods_take_price * 0.01, $item['level_price'], 2);
- $item['max_take_price'] = bcmul($goods->goods_take_price * 0.01, $item['level_price'], 2);
- $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;
- }
- if(bcadd($take_price, $item['take_price'], 2) > $user_league_price){
- $item['take_price'] = $user_league_price;
- }
- if (bcadd($send_price, $item['send_price'], 2) > $store->league_price) {
- $item['send_price'] = 0;
- }
- $send_price += $item['send_price'];
- $take_price += $item['take_price'];
- } else {
- //全款 / 订金
- if ($goods->product_type === Goods::GOODS_TYPE_WORKER) {
- $goods_ext = WorkerGoodsExt::findOne(['goods_id' => $goods->id]);
- if ($goods_ext->payment_type === 1) {
- $item['payment_type'] = 1;
- $final_profit = $goods_ext->final_profit;
- $item['final_price'] = floatval(sprintf('%.2f', $item['price'] * ($final_profit / 100)));
- $item['pre_price'] = floatval(sprintf('%.2f', ($item['price'] - $item['final_price'])));
- }
- }
- }
- if($this->activity_cut_price_order_id){
- $res_accp = $this->getActivityCutPriceGoodsPrice($res, $goods, $attr_id_list, $submit, $item['num']);
- // var_dump($res_accp);die;
- if ($res_accp['code'] === 0) {
- $item['is_level'] = 0;
- if ($res_accp['price'] > 0) {
- $item['price'] = $item['level_price'] = sprintf('%.2f', ($res_accp['price']));
- $item['price_str'] = $res_accp['price_str'];
- }
- } else {
- return $res_accp;
- }
- }
- if ($this->is_seckill) {
- $res = $this->getSceKillGoodsPrice($goods, $attr_id_list, $submit, $item['num']);
- if ($res['code'] === 0) {
- $item['is_level'] = 0;
- if ($res['data'] > 0) {
- $item['price'] = $item['level_price'] = sprintf('%.2f', ($res['data'] * $item['num']));
- }
- } else {
- return $res;
- }
- }
- if ($goods->product_type == Goods::GOODS_TYPE_ADOPT) {
- $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods->id,'is_delete' => 0])->one();
- $item['desc'] = $adopt_goods->desc;
- }
- }
- // $user = User::findOne(get_user_id());
- // $totalGoodsBalance = array_sum(array_column($goods_list, 'balance'));
- // $userMoney = $user->money;
- // if ($userMoney < $totalGoodsBalance) {
- // $remainingBalance = $totalGoodsBalance - $userMoney;
- // $numGoods = count($goods_list);
- // $adjustmentPerGood = round($remainingBalance / $numGoods, 2);
- // for ($i = 0; $i < $numGoods; $i++) {
- // $goods_list[$i]['balance'] = floatval(number_format(
- // $goods_list[$i]['balance'] - $adjustmentPerGood,
- // 2,
- // '.',
- // ','
- // ));
- // }
- // }
- // 和空数组合并重建索引,避免出现因索引key间断导致客户端显示问题
- return array_merge($goods_list, []);
- }
- //砍价商品处理
- public function getActivityCutPriceGoodsPrice($res, $goods, $attr, $submit, $num)
- {
- try {
- $price_str = [];
- $activityOrder = ActivityCutPriceOrder::findOne($this->activity_cut_price_order_id);
- if(!$activityOrder){
- throw new \Exception('砍价活动订单信息不存在');
- }
- $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $activityOrder->activity_id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- if(!$goods_ext){
- throw new \Exception('砍价商品信息不存在');
- }
- $ext_attrs = json_decode($goods_ext['attr'], true);
- foreach ($ext_attrs as $ext_attr) {
- $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
- if (empty(array_diff($ext_attr_id, $attr))) {
- if($ext_attr['num'] < $num){
- throw new \Exception('砍价商品库存不足');
- }
- }
- }
- //全部享受优惠
- $price = $activityOrder->pay_price;
- $price_str[] = '砍价优惠价¥' . sprintf('%.2f', $price);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'price' => $price,
- 'price_str' => implode('+', $price_str),
- 'id' => $activityOrder->id,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //新人专享商品处理
- public function getActivityNewUserGoodsPrice($res, $goods, $attr, $submit, $num)
- {
- try {
- $price = 0;
- $price_str = [];
- $activity = ActivityNewUser::activityAt($goods->store_id);
- $open = false;
- if($activity){
- $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- if($goods_ext){
- $ext_attrs = json_decode($goods_ext['attr'], true);
- foreach ($ext_attrs as $ext_attr) {
- $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
- //与购物车添加的规格id数组比较
- if (empty(array_diff($ext_attr_id, $attr))) {
- $ext_price = $ext_attr['price'];
- $id = $goods_ext['id'];
- $open = true;
- }
- }
- if ($open) {
- $buy_limit = $activity->buy_limit;
- if (($buy_limit && $buy_limit > 0)) {
- // $goodsNum = 0;
- //部分享受优惠
- $goodsNum = intval(Goods::getBuyNum(get_user(), $goods->id));
- $diff_num = $buy_limit - $goodsNum;
- if ($diff_num > 0) {
- $buy_num = $num - $diff_num;
- if ($buy_num > 0) {
- $price += ($diff_num * $ext_price) + ($buy_num * $res['level_price']);
- $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $diff_num;
- } else {
- $price += $num * $ext_price;
- $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num;
- }
- }
- // for($i=$goodsNum+$this->goods_count_activity_new_user[$goods->id]; $i<$num; $i++){
- // if($i < $buy_limit){
- // $price += $ext_price;
- // $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price);
- // $this->goods_count_activity_new_user[$goods->id]++;
- // }else{
- // $price += sprintf('%.2f', $res['level_price']);
- // $price_str[] = sprintf('%.2f', $res['level_price']);
- // }
- // }
- }else{
- //全部享受优惠
- $price = $ext_price * $num;
- $price_str[] = '新人专享¥' . sprintf('%.2f', $ext_price) . '×' . $num;
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'price' => $price,
- 'price_str' => implode('+', $price_str),
- 'id' => $id
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //群活动专享商品处理
- public function getActivityWorkRoomGoodsPrice($res, $goods, $attr, $submit, $num)
- {
- try {
- $price = 0;
- $price_str = [];
- $activity = ActivityWechatRoom::activityAt($this->activity_wechat_room_id);
- if($activity){
- $wechatRoomIds = explode(',', $activity['wechat_room_id']);
- foreach($wechatRoomIds as $roomId){
- $inRoom = \app\modules\admin\models\mochat\MochatForm::userInRoom($this->store_id, $roomId, $this->user_id);
- if($inRoom){
- break;
- }
- }
- if(!$inRoom){
- throw new \Exception('操作失败,用户不在群里面');
- }
- $goods_ext = ActivityWechatRoomGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- if($goods_ext){
- $ext_attrs = json_decode($goods_ext['attr'], true);
- foreach ($ext_attrs as $ext_attr) {
- $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id');
- //与购物车添加的规格id数组比较
- if (empty(array_diff($ext_attr_id, $attr))) {
- $ext_price = $ext_attr['price'];
- $id = $goods_ext['id'];
- }
- }
- //全部享受优惠
- $price = $ext_price * $num;
- $price_str[] = '群友专享¥' . sprintf('%.2f', $ext_price) . '×' . $num;
- }
- }else{
- throw new \Exception('操作失败,群活动信息错误');
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'price' => $price,
- 'price_str' => implode('+', $price_str),
- 'id' => $id
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //秒杀商品处理
- public function getSceKillGoodsPrice($goods, $attr, $submit, $num)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- //秒杀订单
- $sql = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $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')->createCommand()->getRawSql();
- $sql = $sql . ' FOR UPDATE';
- $seckill_activity_goods = Yii::$app->db->createCommand($sql)->queryOne();
- $price = 0;
- $id = 0;
- if (!empty($seckill_activity_goods)) {
- // 这里未统计已经取消的订单
- $sum = SeckillActivityOrderLog::find()->alias('saol')
- ->leftJoin(['o' => Order::tableName()],'saol.order_id = o.id')
- ->where(['saol.activity_goods_id' => $seckill_activity_goods['sag_id'], 'saol.store_id' => get_store_id(), 'saol.user_id' => get_user_id(), 'saol.is_delete' => 0,'o.is_delete' => 0])
- ->andWhere(['!=', 'o.trade_status' ,1 ])
- ->count('saol.num');
- if ($seckill_activity_goods['self_limit_num'] > 0 && ($sum * 1 + $num) > $seckill_activity_goods['self_limit_num']) {
- throw new \Exception("购买数量超出限购数量");
- }
- if ($seckill_activity_goods['order_limit_num'] > 0 && $num > $seckill_activity_goods['order_limit_num']) {
- throw new \Exception("购买数量超出订单限购数量");
- }
- //如果商品使用规格 且 已经查找到活动
- // if ((int)$goods->use_attr === 1 && !empty($seckill_activity_goods['attr'])) {
- //解码
- $seckill_activity_goods['attr'] = $sag_attrs = json_decode($seckill_activity_goods['attr'], true);
- //获取设置的商品秒杀规格id数组与购物车添加的规格id数组比较
- // $sag_attrs = array_column($sag_attrs, 'attr_list');
- //假设没有对应的规格信息
- $open = false;
- foreach ($sag_attrs as $sag_attr) {
- $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
- // $sag_attr['seckill_num'] -= $num;
- //与购物车添加的规格id数组比较
- if (empty(array_diff($sag_attr_id, $attr))) {
- if ((int)$sag_attr['seckill_num'] >= $num) {
- $price = $sag_attr['seckill_price'];
- $id = $seckill_activity_goods['sag_id'];
- } else {
- throw new \Exception("当前秒杀商品库存不足");
- }
- }
- }
- // } else {
- // if ($seckill_activity_goods['seckill_num'] >= ($seckill_activity_goods['sale_num'] + $num)) {
- // $price = $seckill_activity_goods['seckill_price'];
- // $id = $seckill_activity_goods['sag_id'];
- // } else {
- // throw new \Exception("当前秒杀商品库存不足");
- // }
- // }
- if ($submit && $id > 0) {
- $activity_goods = SeckillActivityGoods::findOne($id);
- $activity_goods_attr = json_decode($activity_goods->attr, true);
- foreach ($activity_goods_attr as &$sag_attr) {
- $sag_attr_id = array_column($sag_attr['attr_list'], 'attr_id');
- // $sag_attr['seckill_num'] -= $num;
- //与购物车添加的规格id数组比较
- if (empty(array_diff($sag_attr_id, $attr))) {
- $sag_attr['seckill_num'] = $sag_attr['seckill_num'] - $num;
- }
- }
- $activity_goods->attr = json_encode($activity_goods_attr);
- $activity_goods->sale_num = ($activity_goods->sale_num * 1 + $num);
- $activity_goods->seckill_num = ($activity_goods->seckill_num - $num);
- if (!$activity_goods->save()) {
- throw new \Exception(json_encode($activity_goods->errors));
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $price,
- 'id' => $id
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //自定义表单
- protected function getFormData()
- {
- $new_list = [];
- $new_list['is_form'] = Option::get('is_form', $this->store_id, 'store', 0)['value'];
- $form_list = [];
- if ($new_list['is_form'] == 1) {
- $new_list['name'] = Option::get('form_name', $this->store_id, 'store', '表单信息')['value'];
- $form_list = Form::find()->where([
- 'store_id' => $this->store_id, 'is_delete' => 0,
- ])->orderBy(['sort' => SORT_ASC])->asArray()->all();
- foreach ($form_list as $index => $value) {
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- $default = str_replace(",", ",", $value['default']);
- $list = explode(',', $default);
- $default_list = [];
- foreach ($list as $k => $v) {
- $default_list[$k]['name'] = $v;
- if ($k == 0) {
- if ($value['type'] == 'radio') {
- $form_list[$index]['default'] = $v;
- } else {
- $form_list[$index]['default'] = [$v];
- }
- $default_list[$k]['checked'] = true;
- } else {
- $default_list[$k]['checked'] = false;
- }
- }
- $form_list[$index]['default_list'] = $default_list;
- }
- }
- }
- $new_list['list'] = $form_list;
- return $new_list;
- }
- //自定义表单
- public function getNewFormData($goods)
- {
- $new_list = [];
- $new_list['is_form'] = $goods['is_form'];
- $form_list = [];
- if ($new_list['is_form'] == 1) {
- $new_list['name'] = $goods['form_name'];
- $form_list = Form::find()->where([
- 'store_id' => $this->store_id, 'is_delete' => 0, 'goods_id' => $goods['id']
- ])->orderBy(['sort' => SORT_ASC])->asArray()->all();
- foreach ($form_list as $index => $value) {
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- $default = str_replace(",", ",", $value['default']);
- $list = explode(',', $default);
- $default_list = [];
- foreach ($list as $k => $v) {
- $default_list[$k]['name'] = $v;
- if ($k == 0) {
- if ($value['type'] == 'radio') {
- $form_list[$index]['default'] = $v;
- } else {
- $form_list[$index]['default'] = [$v];
- }
- $default_list[$k]['checked'] = true;
- } else {
- $default_list[$k]['checked'] = false;
- }
- }
- $form_list[$index]['default_list'] = $default_list;
- }
- }
- }
- $new_list['list'] = $form_list;
- return $new_list;
- }
- protected function getAddress()
- {
- if (!$this->address) {
- if ($this->address_id) {
- $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]);
- } else {
- $this->address = Address::find()->where([
- 'user_id' => get_saas_user_id(),
- 'is_default' => 1,
- 'is_delete' => 0,
- ])->limit(1)->one();
- }
- }
- return (object)$this->address;
- }
- //获取收货地址,有address_id优先获取,没有则获取默认地址
- protected function getAddressData()
- {
- $address = $this->getAddress();
- if (isset($address->id)) {
- return [
- 'id' => $address->id,
- 'name' => $address->name,
- 'mobile' => $address->mobile,
- 'province_id' => $address->province_id,
- 'province' => $address->province,
- 'city_id' => $address->city_id,
- 'city' => $address->city,
- 'district_id' => $address->district_id,
- 'district' => $address->district,
- 'town_id' => $address->town_id,
- 'town' => $address->town,
- 'village_id' => $address->village_id,
- 'village' => $address->village,
- 'detail' => $address->detail,
- 'is_default' => $address->is_default,
- 'latitude' => $address->latitude,
- 'longitude' => $address->longitude,
- ];
- } else {
- return [
- 'id' => '',
- 'name' => '',
- 'mobile' => '',
- 'province_id' => '',
- 'province' => '',
- 'city_id' => '',
- 'city' => '',
- 'district_id' => '',
- 'district' => '',
- 'detail' => '',
- 'is_default' => 1,
- 'latitude' => '',
- 'longitude' => '',
- ];
- }
- }
- //如果为服务订单则获取服务人员
- protected function getWorkerData()
- {
- $worker = (object)$this->worker;
- if (!$this->is_worker) {
- return null;
- }
- if (!$worker->id) {
- if ($this->worker_id) {
- $worker = Worker::findOne(['id' => $this->worker_id, 'store_id' => get_store_id(), 'status' => 1, 'open_status' => 1]);
- } else {
- // $worker_list = Worker::find()->where([
- // 'store_id' => get_store_id(),
- // 'status' => 1,
- // 'open_status' => 1,
- // ])->select('id')->column();
- // if ($worker_list) {
- // $rand_index = array_rand($worker_list);
- // $worker_id = $worker_list[$rand_index];
- // $worker = Worker::findOne($worker_id);
- // }
- $worker = (object)[
- 'id' => 0,
- 'name' => '',
- 'logo' => '',
- 'lat' => 0,
- 'lng' => 0,
- ];
- }
- }
- $arr = [
- 'id' => $worker->id,
- 'name' => $worker->name,
- 'logo' => $worker->logo,
- 'lat' => $worker->lat,
- 'lng' => $worker->lng,
- 'time' => null,
- 'default_date' => ''
- ];
- //配置服务人员默认可预定时间
- // $worker
- $result = $this->workerTime($worker->id);
- if ($result['code'] === 0) {
- $arr['time'] = $result['data'];
- if ($arr['time']) {
- $times = $result['data'][0]['time'][0]['times'];
- foreach ($times as $item) {
- if ($item['status'] === 0) {
- $arr['default_date'] = date('Y-m-d H:i', $item['time_int']);
- break;
- }
- }
- }
- }
- return $arr;
- }
- public function getWorkerTime() {}
- //获取支付方式 $order_type 0为系统其他订单 5为认养订单 该字段主要为了区分认养订单
- public function getPayTypeList($order_type = 0, $ignore = [])
- {
- if ($order_type == 5) {
- $adopt_setting = AdoptSetting::findOne(['store_id' => get_store_id()]);
- $adopt_payment = $adopt_setting->adopt_payment;
- $adopt_pay_list = [];
- if (!empty($adopt_payment)) {
- $adopt_pay_type = Json::decode($adopt_payment);
- foreach ($adopt_pay_type as &$value) {
- if ($value['key'] == 'alipay') {
- $value['payment'] = 4;
- }
- if ($value['key'] == 'wechat') {
- $value['payment'] = 1;
- }
- }
- $adopt_pay_list = $adopt_pay_type;
- }
- foreach ($adopt_pay_list as $k => $val) {
- if ($val['payment'] == 4 && is_wechat_platform()) {
- unset($adopt_pay_list[$k]);
- }
- if ($val['payment'] == 1 && is_alipay_platform()) {
- unset($adopt_pay_list[$k]);
- }
- }
- $new_list = array_values($adopt_pay_list);
- } else {
- $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']);
- $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;
- }
- }
- }
- $new_list = [];
- $ok = true;
- $is_virtual = false;
- $is_integral = false; //积分兑换商品
- foreach ($this->mch_list as $mch) {
- if (!empty($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == 3) {
- $is_virtual = true;
- }
- if (!empty($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_INTEGRAL) {
- $is_integral = true;
- }
- if (!empty($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == Goods::GOODS_TYPE_TIME) {
- $is_integral = true;
- }
- if ($mch['mch_id'] == 0) {
- continue;
- } else {
- $ok = false;
- break;
- }
- }
- foreach ($pay_type_list as $index => $pay_type_value) {
- if (in_array($index, $ignore)) {
- continue;
- }
- if ((is_wechat_platform() || is_h5() || is_app_platform()) && $index == 'wechat' && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_WX_B2B && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_WX_B2B,
- 'payment' => Order::PAY_TYPE_WX_B2B,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_alipay_platform() || is_h5() || is_app_platform()) && $index == 'alipay' && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => '支付宝支付',
- 'payment' => 4,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
- ];
- }
- //如果是微信且商城已配置通联支付进件数据
- if (is_wechat_platform() && $index == Order::PAY_TYPE_KEY_YUNST_WECHAT_PAY && $pay_type_value['value'] == 1) {
- $store = Store::findOne(get_store_id());
- if (!empty($store->cusid)) {
- $new_list[] = [
- 'name' => '通联支付',
- 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- if ((is_h5() || is_app_platform() || is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_ALLINPAY_WX && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ALLINPAY_WX,
- 'payment' => Order::PAY_TYPE_ALLINPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_app_platform() || is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_WX && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_WX,
- 'payment' => Order::PAY_TYPE_ADAPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_app_platform() || is_alipay_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_ALIPAY && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_ALIPAY ,
- 'payment' => Order::PAY_TYPE_ADAPAY_ALIPAY ,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
- ];
- }
- if ($index == Order::PAY_TYPE_KEY_ADAPAY_QUICKPAY_FRONTPAY && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_QUICKPAY_FRONTPAY,
- 'payment' => Order::PAY_TYPE_ADAPAY_QUICKPAY_FRONTPAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if (is_wechat_platform() && $index == Order::PAY_TYPE_KEY_HUIFU_V2_JSPAY_WX && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_HUIFU_V2_JSPAY_WX,
- 'payment' => Order::PAY_TYPE_HUIFU_V2_JSPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ($index == Order::PAY_TYPE_KEY_OFFLINE && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_OFFLINE,
- 'payment' => Order::PAY_TYPE_OFFLINE,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ($index == Order::PAY_TYPE_KEY_MONTH && $pay_type_value['value'] == 1) {
- $new_list[] = [
- 'name' => Order::PAY_TYPE_NAME_MONTH,
- 'payment' => Order::PAY_TYPE_MONTH,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ($index == 'huodao' && $pay_type_value['value'] == 1 && $ok && !$is_virtual && !$is_integral) {
- $new_list[] = [
- 'name' => '货到付款',
- 'payment' => 2,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
- ];
- }
- if ($index == 'friend' && $pay_type_value['value'] == 1 && !$is_integral) {
- $new_list[] = [
- 'name' => '朋友代付',
- 'payment' => 7,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
- ];
- }
- }
- if (is_toutiao_platform()) {
- $new_list[] = [
- 'name' => '线上支付',
- 'payment' => 5,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- return $new_list;
- }
- /**
- * 支付方式
- * @param array $is_payment //支付方式
- * @param array $ignore //忽略的支付方式
- * @return array
- */
- public static function getPayType($is_payment = array(), $ignore = array())
- {
- if (!$is_payment || empty($is_payment)) {
- $default = '{"wechat":{"value":1}}';
- if (is_alipay_platform()) {
- $default = '{"alipay":{"value":1}}';
- }
- if (is_toutiao_platform()) {
- $default = '{"toutiao":{"value":1}}';
- }
-
- $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['value']);
- $is_payment = Json::decode($pay_str['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 ($is_payment as $key => &$value) {
- if (isset($user_pay_list_arr[$key]) && $user_pay_list_arr[$key]['value'] == 0 && $value['value'] == 1) {
- $value['value'] = 0;
- }
- }
- }
- }
- $pay_type_list = [];
- foreach ($is_payment as $index => $value) {
- if (in_array($index, $ignore)) {
- continue;
- }
- if ($index == 'wechat' && $value['value'] == 1 && !is_alipay_platform()) {
- $pay_type_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png'
- ];
- }
- if ((is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_WX_B2B && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_WX_B2B,
- 'payment' => Order::PAY_TYPE_WX_B2B,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_h5() || is_app_platform() || is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_ALLINPAY_WX && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ALLINPAY_WX,
- 'payment' => Order::PAY_TYPE_ALLINPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_app_platform() || is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_WX && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_WX,
- 'payment' => Order::PAY_TYPE_ADAPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ((is_app_platform() || is_alipay_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_ALIPAY && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_ALIPAY,
- 'payment' => Order::PAY_TYPE_ADAPAY_ALIPAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
- ];
- }
- if ($index == Order::PAY_TYPE_KEY_ADAPAY_QUICKPAY_FRONTPAY && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_ADAPAY_QUICKPAY_FRONTPAY,
- 'payment' => Order::PAY_TYPE_ADAPAY_QUICKPAY_FRONTPAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if (is_wechat_platform() && $index == Order::PAY_TYPE_KEY_HUIFU_V2_JSPAY_WX && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_HUIFU_V2_JSPAY_WX,
- 'payment' => Order::PAY_TYPE_HUIFU_V2_JSPAY_WX,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ($index == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) {
- $pay_type_list[] = [
- 'name' => '线上支付',
- 'payment' => 5,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png'
- ];
- }
- if ($index == 'huodao' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '货到付款',
- 'payment' => 2,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png'
- ];
- }
- if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP || is_alipay_platform() || is_h5()) {
- if ($index == 'alipay' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '支付宝支付',
- 'payment' => 4,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png'
- ];
- }
- }
- if ($index == 'balance' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '账户余额支付',
- 'payment' => 3,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png'
- ];
- }
- if ($index == 'friend' && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => '朋友代付',
- 'payment' => 7,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
- ];
- }
- //如果是微信且商城已配置通联支付进件数据
- if (is_wechat_platform() && $index == Order::PAY_TYPE_KEY_YUNST_WECHAT_PAY && $value['value'] == 1) {
- $store = Store::findOne(get_store_id());
- if (!empty($store->cusid)) {
- $new_list[] = [
- 'name' => '通联支付',
- 'payment' => Order::PAY_TYPE_YUNST_WECHAT_PAY,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- if ($index == Order::PAY_TYPE_KEY_OFFLINE && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_OFFLINE,
- 'payment' => Order::PAY_TYPE_OFFLINE,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if ($index == Order::PAY_TYPE_KEY_MONTH && $value['value'] == 1) {
- $pay_type_list[] = [
- 'name' => Order::PAY_TYPE_NAME_MONTH,
- 'payment' => Order::PAY_TYPE_MONTH,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- $is_open_combine = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge')['value'];
- $use = get_user();
- if ($is_open_combine && $use['money']) {
- array_unshift($pay_type_list, [
- 'name' => '余额支付',
- 'payment' => 3,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/clientImg/images/account_pay_icon.png'
- ]);
- }
- if (!$pay_type_list) {
- if (is_wechat_platform()) {
- $pay_type_list[] = [
- 'name' => '微信支付',
- 'payment' => 1,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
- ];
- }
- if (is_toutiao_platform()) {
- $pay_type_list[] = [
- 'name' => '线上支付',
- 'payment' => 5,
- 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png'
- ];
- }
- }
- return $pay_type_list;
- }
- protected function getCouponList(&$mch)
- {
- // if (get_md_id()) {
- // $mch['coupon_list'] = [];
- // return;
- // }
- if (!isset($mch['mch_id'])) {
- $mch['mch_id'] = 0;
- }
- $goods_total_price = $mch['total_price'];
- $cat_ids = $this->getCatIdList($mch['goods_list'],$mch['mch_id']);
- $coupon_goods_id = $this->getGoodsIdList($mch['goods_list']);
- $list = UserCoupon::find()->alias('uc')
- ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
- ->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id')
- ->where([
- 'AND',
- ['uc.is_delete' => 0],
- ['uc.is_use' => 0],
- ['c.mch_id' => $mch['mch_id']],
- ['uc.is_expire' => 0],
- ['uc.user_id' => $this->user_id],
- ['<=', 'c.min_price', $goods_total_price],
- ])
- ->andWhere($mch['plugin_type'] == 6 ? ['c.appoint_type' => 3] : [
- 'OR',
- ['<>', 'c.appoint_type', 3],
- 'c.appoint_type IS NULL'
- ])
- ->select('c.name, c.expire_day, c.id, c.expire_type, c.discount_type, c.discount, c.discount_max_price, uc.id user_coupon_id,c.mch_id,c.rule,c.sub_price,c.min_price,cas.event,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list')
- ->indexBy('user_coupon_id')->asArray()->all();
- $events = [
- 0 => '平台发放',
- 1 => '分享红包',
- 2 => '购物返券',
- 3 => '领券中心',
- ];
- $max_price = 0;
- if ($mch['plugin_type'] == 6) {
- $max_price = $mch['total_price'];
- } else {
- foreach ($mch['goods_list'] as $v) {
- $max_price += $v['price'];
- }
- }
- $new_list = [];
- foreach ($list as $i => $item) {
- if ($item['expire_type'] == 1) {
- if ($item['end_time'] > 0) {
- if (time() > $item['end_time']) {
- $list[$i]['_reason'] = 'time() > $item[\'end_time\']';
- continue;
- }
- }
- }
- if ($item['expire_type'] == 2) {
- if (time() > $item['end_time'] || time() < $item['begin_time']) {
- $list[$i]['_reason'] = '(time() > $item[\'end_time\'] || time() < $item[\'begin_time\'])';
- continue;
- }
- }
- $list[$i]['status'] = 0;
- if (isset($item['is_use']) && $item['is_use']) {
- $list[$i]['status'] = 1;
- }
- if (isset($item['is_expire']) && $item['is_expire']) {
- $list[$i]['status'] = 2;
- }
- $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
- $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']);
- $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']);
- $list[$i]['discount_type'] = $item['discount_type'];
- $list[$i]['discount'] = $item['discount'];
- if (!$item['event']) {
- if ($item['type'] == 2) {
- $list[$i]['event'] = $item['event'] = 3;
- } else {
- $list[$i]['event'] = $item['event'] = 0;
- }
- }
- $list[$i]['event_desc'] = $events[$item['event']] ?: '领券中心';
- $list[$i]['min_price'] = doubleval($item['min_price']);
- if ($list[$i]['discount_type'] == 1) {
- $list[$i]['sub_price'] = round($mch['total_price'] - doubleval($mch['total_price'] * $list[$i]['discount'] / 10), 2);
- if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
- $list[$i]['sub_price'] = $item['discount_max_price'];
- }
- } else {
- $list[$i]['sub_price'] = doubleval($item['sub_price']);
- }
- $goodsAdd = array_column($mch['goods_list'], 'goods_id');
- if ($list[$i]['appoint_type'] == 1) {
- $list[$i]['cat_id_list'] = json_decode($list[$i]['cat_id_list']);
- if ($list[$i]['cat_id_list'] != null) {
- $current = array_intersect($list[$i]['cat_id_list'], $cat_ids);
- if ($current) {
- $goodsAdd = [];
- $price = 0;
- foreach ($current as $v) {
- foreach ($mch['goods_list'] as $v2) {
- if (in_array($v, $v2['cat_id']) && !in_array($v2['goods_id'], $goodsAdd)) {
- $price += $v2['price'];
- array_push($goodsAdd, $v2['goods_id']);
- }
- };
- }
- if ($list[$i]['discount_type'] == 1) {
- $list[$i]['sub_price'] = round($price - doubleval($price * $list[$i]['discount'] / 10), 2);
- if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
- $list[$i]['sub_price'] = $item['discount_max_price'];
- }
- } else {
- $list[$i]['sub_price'] = doubleval($item['sub_price']);
- }
- if ($price < $list[$i]['min_price']) {
- $list[$i]['_reason'] = '$price < $list[$i][\'min_price\']';
- // unset($list[$i]);
- continue;
- }
- } else {
- $list[$i]['_reason'] = '$current else';
- // unset($list[$i]);
- continue;
- }
- }
- } elseif ($list[$i]['appoint_type'] == 2) {
- $list[$i]['goods_id_list'] = json_decode($list[$i]['goods_id_list']);
- if ($list[$i]['goods_id_list'] != null) {
- $current = array_intersect($list[$i]['goods_id_list'], $coupon_goods_id);
- if ($current) {
- $goodsAdd = [];
- $price = 0;
- foreach ($current as $v) {
- foreach ($mch['goods_list'] as $v2) {
- if ($v == $v2['goods_id'] && !in_array($v2['goods_id'], $goodsAdd)) {
- $price += $v2['price'];
- array_push($goodsAdd, $v2['goods_id']);
- }
- }
- }
- if ($list[$i]['discount_type'] == 1) {
- $list[$i]['sub_price'] = round($price - doubleval($price * $list[$i]['discount'] / 10), 2);
- if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){
- $list[$i]['sub_price'] = $item['discount_max_price'];
- }
- } else {
- $list[$i]['sub_price'] = doubleval($item['sub_price']);
- }
- if ($price < $list[$i]['min_price']) {
- $list[$i]['_reason'] = '$price < $list[$i][\'min_price\']';
- // unset($list[$i]);
- continue;
- }
- } else {
- $list[$i]['_reason'] = '$current else';
- // unset($list[$i]);
- continue;
- }
- }
- } else {
- if ($max_price < $list[$i]['min_price']) {
- $list[$i]['_reason'] = '$max_price < $list[$i][\'min_price\']';
- // unset($list[$i]);
- continue;
- }
- }
- $list[$i]['goods_use'] = $goodsAdd;
- $new_list[] = $list[$i];
- }
- $mch['__coupon_list'] = $list;
- if (!empty($this->verify_card_id)) {
- $mch['coupon_list'] = [];
- } else {
- if (!$this->is_worker) {
- $mch['coupon_list'] = $new_list;
- }
- }
- }
- protected function getCatIdList(&$goods_list,$mch_id = 0)
- {
- $cat_id_list = [];
- foreach ($goods_list as &$goods) {
- if ($goods['goods_cat_id'] == 0) {
- if ($mch_id > 0) {
- $goods_cat_list = MchGoodsCat::find()
- ->select('cat_id')->where([
- 'goods_id' => $goods['goods_id'],
- ])->all();
- }else {
- $goods_cat_list = GoodsCat::find()
- ->select('cat_id')->where([
- 'goods_id' => $goods['goods_id'],
- 'is_delete' => 0,
- ])->all();
- }
- foreach ($goods_cat_list as $goods_cat) {
- $cat_id_list[] = $goods_cat->cat_id;
- $goods['cat_id'][] = $goods_cat->cat_id;
- }
- } else {
- $cat_id_list[] = $goods['goods_cat_id'];
- $goods['cat_id'][] = $goods['goods_cat_id'];
- }
- $cat_parent_list = Cat::find()->select('parent_id')
- ->andWhere(['id' => $goods['cat_id'], 'store_id' => $this->store_id, 'is_delete' => 0])->andWhere(['>', 'parent_id', 0])
- ->column();
- $cat_id_list = array_merge($cat_parent_list, $cat_id_list);
- $goods['cat_id'] = array_merge($cat_parent_list, is_array($goods['cat_id']) ? $goods['cat_id'] : []);
- }
- unset($goods);
- return array_unique($cat_id_list);
- }
- protected function getGoodsIdList($goods_list)
- {
- $goods_id_list = [];
- foreach ($goods_list as $goods) {
- $goods_id_list[] = $goods['goods_id'];
- }
- return $goods_id_list;
- }
- protected function getLevelData()
- {
- $level = Level::find()->select([
- 'name', 'level', 'discount',
- ])->where(['level' => $this->user->level, 'store_id' => $this->store_id, 'is_delete' => 0])
- ->asArray()->one();
- return $level;
- }
- //积分计算
- /**
- * @param $goods_item object 重新编写的goods_item
- * @param $store_integral int 商城设置的积分规则
- * @param $goods_id array 已设置积分的商品id数组
- * @return array
- */
- protected function getIntegral($goods_item, $store_integral, $goods_id = array(), $send_integral_profit = 0)
- {
- $integral = json_decode($goods_item->integral, true);
- $resIntegral = [
- 'forehead' => 0,
- 'forehead_integral' => 0,
- ];
- $maxIntegral = [
- 'forehead' => 0,
- 'forehead_integral' => 0,
- ];
- // 赠送积分计算
- // if ($send_integral_profit > 0) {
- // // 百分比
- // $goods_item->give = (int)($goods_item->price * ($send_integral_profit / 100));
- // }
- // else {
- // 固定积分
- // $goods_item->give = (int)($give * $goods_item->num);
- // }
- if ($integral) {
- // // 赠送积分计算
- $give = (int)$integral['give'];
- if (strpos($give, '%') !== false) {
- // 百分比
- $give = trim($give, '%');
- $goods_item->give = (int)(intval($goods_item->price) * ($give / 100));
- } else {
- // 固定积分
- $goods_item->give = (intval($give) * intval($goods_item->num));
- }
- if ($give <= 0 && $send_integral_profit > 0) {
- $goods_item->give = (int)(intval($goods_item->price) * ($send_integral_profit / 100));
- }
- // 抵扣积分计算
- $forehead = $integral['forehead'];
- if ($forehead) {
- if (strpos($forehead, '%') !== false) {//百分比积分抵扣计算
- if ($forehead >= 100) {
- $forehead = 100;
- }
- if ($integral['more'] == '1') {//多件累计计算
- $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->price * $store_integral);
- } else {
- if (!in_array($goods_item->id, $goods_id)) { //不允许多件累计 同id商品值计算一次积分抵扣
- $resIntegral['forehead_integral'] = (int)(($forehead / 100) * $goods_item->single_price * $store_integral);
- }
- }
- } else {
- // if ($integral['more'] == '1') {
- // $resIntegral['forehead_integral'] = (int)($store_integral * $goods_item->price);
- // if ($goods_item->price > ($forehead * $goods_item->num)) {
- // $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
- // }
- // } else {
- // if (!in_array($goods_item->id, $goods_id)) {
- // $goodsPrice = $goods_item->single_price;
- // $resIntegral['forehead_integral'] = (int)($store_integral * $goodsPrice);
- // if ($goodsPrice > $forehead) {
- // if ($forehead > 0) {
- // $resIntegral['forehead_integral'] = (int)($forehead * $store_integral);
- // } else {
- // $resIntegral['forehead_integral'] = 0;
- // }
- // }
- // }
- // }
- // 累计
- $resIntegral['forehead_integral'] = bcmul($store_integral, $goods_item->price);
- if ($goods_item->price > ($forehead * $goods_item->num)) {
- $resIntegral['forehead_integral'] = (int)($forehead * $goods_item->num * $store_integral);
- }
- }
- $maxIntegral['forehead_integral'] = $resIntegral['forehead_integral'];
- $maxIntegral['forehead'] = $store_integral ? sprintf("%.2f", ($maxIntegral['forehead_integral'] / $store_integral)) : 0;
- }
- $user_integral = $this->user->integral;
- $user_integral = floor_num($user_integral, 0);
- if ($this->integral['forehead_integral'] < $user_integral) {
- $resetIntegral = $user_integral - $this->integral['forehead_integral'];
- $resIntegral['forehead_integral'] = $resIntegral['forehead_integral'] >= $resetIntegral ? $resetIntegral : $resIntegral['forehead_integral'];
- $resIntegral['forehead'] = $store_integral ? sprintf("%.2f", ($resIntegral['forehead_integral'] / $store_integral)) : 0;
- $this->integral['forehead_integral'] += $resIntegral['forehead_integral'];
- $this->integral['forehead'] += $resIntegral['forehead'];
- } else {
- $resIntegral['forehead_integral'] = 0;
- $resIntegral['forehead'] = 0;
- }
- }
- return [
- 'maxIntegral' => $maxIntegral,
- 'resIntegral' => $resIntegral,
- 'give' => $goods_item->give ?? ''
- ];
- }
- public function getExpressPrice($mch)
- {
- if($this->is_giving_gifts){
- return 0;
- }
- $address = $this->address ?: $this->address = Address::findOne([
- 'user_id' => get_saas_user_id(),
- 'is_default' => 1,
- 'is_delete' => 0,
- ]);
- $expressPrice = 0;
- if ($address) {
- //先计算单品满件包邮和满额包邮
- $resGoodsList = Goods::cutFull($mch['goods_list']);
- //再通过运费规则计算运费
- $expressPrice = PostageRules::getExpressPriceMore($this->store_id ?? get_store_id(), $address['city_id'], $resGoodsList, $address['province_id']);
- }
- $mchTotalPrice = array_sum(array_column($mch['goods_list'], 'price'));
- $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice, $mchTotalPrice);
- return $expressPrice['expressPrice'] >= 0 ? $expressPrice['expressPrice'] : 0;
- }
- public function getWorkerTravelPrice()
- {
- $travelPrice = 0;
- $store_id = get_store_id();
- $lat = 0;
- $lng = 0;
- if ($this->address) {
- $address = $this->address;
- $lat = $address['latitude'];
- $lng = $address['longitude'];
- }
- if (empty($this->worker['id'])) {
- return [
- 'distance' => 0,
- 'starting_price' => 0,
- 'additional_charge' => 0,
- 'travelPrice' => 0
- ];
- }
- $from = [
- $this->worker['lat'] > 0 ? $this->worker['lat'] : '1.11',
- $this->worker['lng'] > 0 ? $this->worker['lng'] : '1.11'
- ];
- $to = [
- $lat ?: ($from[0]),
- $lng ?: ($from[1])
- ];
- sort($from);
- sort($to);
- // $from = implode(",", $from);
- // $to = implode(",", $to);
- $distance = $this->get_distance($from, $to);
- $distance *= 2;
- // $place_url = 'https://apis.map.qq.com/ws/distance/v1/?mode=driving&from='.$from.'&to='.$to .'&key=7XCBZ-2BHRO-FK4WS-SM73D-4DQ43-WUBUT';
- // $json_place = file_get_contents($place_url);
- // $result = json_decode($json_place, true);
- // $distance = 0;
- // if ($result['status'] === 0) {
- // $distance = $result['result']['elements'][0]['distance'];
- // }
- //
- //
- // $place_url = 'https://apis.map.qq.com/ws/distance/v1/?mode=driving&from='.$to.'&to='.$from .'&key=OMQBZ-ACWE2-VWZU5-C6W7M-Y6EIF-VNBYM';
- // $json_place = file_get_contents($place_url);
- // $result_ = json_decode($json_place, true);
- // if ($result_['status'] === 0) {
- // $distance += $result_['result']['elements'][0]['distance'];
- // }
- $setting = WorkerSetting::findOne(['store_id' => $store_id, 'is_delete' => 0]);
- $starting_price = $setting->starting_price ?? 0;
- $additional_charge = $setting->additional_charge ?? 0;
- if ($distance > 0 && $setting) {
- $distance = sprintf("%.2f", $distance / 1000); //千米
- //计算起步数
- $travelPrice += floatval($starting_price);
- if ($distance > 1) {
- //大于一公里按照每公里加收算
- $diff = ceil($distance - 1);
- $travelPrice += floatval(sprintf("%.2f",$diff * $additional_charge));
- }
- }
- return [
- 'distance' => $distance,
- 'starting_price' => $starting_price,
- 'additional_charge' => $additional_charge,
- 'travelPrice' => $travelPrice
- ];
- }
- public function get_distance($from, $to, $km = true, $decimal = 2)
- {
- sort($from);
- sort($to);
- $EARTH_RADIUS = 6370996.81; // 地球半径系数
- $distance = $EARTH_RADIUS*2*asin(sqrt(pow(sin( ($from[0]*pi()/180-$to[0]*pi()/180)/2),2)+cos($from[0]*pi()/180)*cos($to[0]*pi()/180)* pow(sin( ($from[1]*pi()/180-$to[1]*pi()/180)/2),2)))*1000;
- if($km){
- $distance = $distance / 1000;
- }
- return round($distance, $decimal);
- }
- //获取服务人员时间
- public function workerTime($id)
- {
- try {
- $worker = Worker::findOne($id);
- // if (!$worker) {
- // return [
- // 'code' => 0,
- // 'msg' => 'success',
- // 'data' => null
- // ];
- // }
- $time = null;
- $book_start_time_ = 0000;
- $book_end_time_ = 2400;
- $times_arr = [];
- if ($worker) {
- $book_start_time_ = $worker->book_start_time;
- $book_end_time_ = $worker->book_end_time;
- if ($book_start_time_ > $book_end_time_) {
- $middle = $book_start_time_;
- $book_start_time_ = $book_end_time_;
- $book_end_time_ = $middle;
- }
- }
- $book_end_time_ = $book_end_time_ > 0 ? $book_end_time_ : 2400;
- //判断是否存在服务开始结束时间
- if ($book_start_time_ >= 0 && $book_end_time_ >= 0) {
- $book_start_time_ = intval($book_start_time_);
- $book_end_time_ = intval($book_end_time_);
- if ($id > 0) {
- $order_ids = WorkerOrderExt::find()->where(['worker_id' => $id, 'user_revoke' => 0, 'status_ext' => [WorkerOrderExt::STATUS_EXT_HAS_BIND, WorkerOrderExt::STATUS_EXT_ON_ROAD, WorkerOrderExt::STATUS_EXT_ARRIVE, WorkerOrderExt::STATUS_EXT_START]])
- ->select('order_id')->column();
- $times_arr = Order::find()->where(['id' => $order_ids])->select('book_info')->column();
- foreach ($times_arr as &$time_item_) {
- $book_info = json_decode($time_item_, true);
- $time_item_ = strtotime($book_info['worker_time']);
- }
- }
- $time = [];
- //计算7天日期
- $book_date_arr = [];//range(date('Ymd'), date('Ymd')+6);
- for($i = 0;$i<=6;$i++) {
- array_push($book_date_arr, date('Ymd', strtotime('+'.$i.' day')));
- }
- //每天开始结束时间
- foreach ($book_date_arr as $date_index => $date_item) {
- //更改日期格式
- $book_start_time = $book_start_time_;
- $book_end_time = $book_end_time_;
- //周几
- $week = date('N', strtotime($date_item));
- switch ($week) {
- case 1:
- $week = "周一";
- break;
- case 2:
- $week = "周二";
- break;
- case 3:
- $week = "周三";
- break;
- case 4:
- $week = "周四";
- break;
- case 5:
- $week = "周五";
- break;
- case 6:
- $week = "周六";
- break;
- default:
- $week = "周日";
- break;
- }
- //是否今天
- $day = date('Ymd', strtotime($date_item));
- $today = date('Ymd');
- if ($day === $today) {
- $week = "今天";
- }
- $time[$date_index]['date'] = $week . date('m.d', strtotime($date_item));
- $book_start_time = sprintf("%04d", $book_start_time);
- $book_end_time = sprintf("%04d", $book_end_time);
- $book_start_time = substr(str_pad($book_start_time, 4, 0, STR_PAD_LEFT), -4, 2) . ':' . substr($book_start_time, -2);
- $book_end_time = substr(str_pad($book_end_time, 4, 0, STR_PAD_LEFT), -4, 2) . ':' . substr($book_end_time, -2);
- //生成时间
- $book_start_time = strtotime($date_item . ' ' . $book_start_time);
- $book_end_time = strtotime($date_item . ' ' . $book_end_time);
- //不可预约已过去时间计算逻辑
- if ($book_start_time <= time()) {
- $i = '00';
- if (date('i') >= 30) {
- $i = '30';
- }
- //前半小时预约两小时后的整点,后半小时预约两小时后的半点
- $book_start_time = strtotime(date('Ymd H:' . $i, time() + 7200));
- }
- if ($book_end_time <= time()) {
- unset($time[$date_index]);
- continue;
- }
- //每半个小时为一个可预约时间段
- $book_time_arr = range($book_start_time, $book_end_time, 1800);
- if (count($book_time_arr) <= 1) {
- unset($time[$date_index]);
- continue;
- }
- //记录开始结束时间
- $time[$date_index]['time'][0]['start_time'] = date('H:i', $book_time_arr[0]);
- $time[$date_index]['time'][0]['end_time'] = date('H:i', $book_time_arr[count($book_time_arr) - 1]);
- $time[$date_index]['time'][0]['times'] = [];
- foreach ($book_time_arr as $time_index_ => $time_item) {
- if ($time_index_ !== count($book_time_arr) - 1) {
- $status = 0;
- $end_time = $time_item + 1800;
- //显示时间段不可预约状态
- if (!empty($times_arr) && in_array($time_item, $times_arr)) {
- $status = 1;
- }
- $time[$date_index]['time'][0]['times'][$time_index_] = [
- 'status' => $status,
- 'time' => date('H:i', $time_item),
- 'time_int' => $time_item
- ];
- }
- }
- }
- }
- $time = array_values($time);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $time
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 计算打包费
- * @param $mch
- * @return int
- */
- protected function getDabaoPrice($mch)
- {
- $goods_list = $mch['goods_list'];
- $dabao_price = 0;
- foreach ($goods_list as $value) {
- $dabao_price += $value['dabao_price'];
- }
- return $dabao_price >= 0 ? $dabao_price : 0;
- }
- // 获取门店列表
- protected function getShopList()
- {
- $start = 0;
- $shop_table_name = Shop::tableName();
- $latitude = 0;
- $longitude = 0;
- if ($this->address && isset($this->address['id'])) {
- $address = Address::findOne($this->address['id']);
- if ($address) {
- $latitude = $address->latitude ? $address->latitude : 0;
- $longitude = $address->longitude ? $address->longitude : 0;
- }
- }
- $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as
- distance FROM {$shop_table_name} WHERE ((`store_id`={$this->store_id}) AND (`is_delete`=0)) ";
- if ($this->keyword) {
- $sql .= " AND (`name` like '%{$this->keyword}%') ";
- }
- $sql .= "ORDER BY `distance` LIMIT {$start},30";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $shop = null;
- foreach ($list as $index => $item) {
- if ($item['is_default'] == 1) {
- $shop = $item;
- }
- $list[$index]['distance'] = round($item['distance']/1000, 2).'km';
- }
- return [
- 'list' => $list,
- 'shop' => $shop
- ];
- }
- // 获取起送规则
- protected function getOfferRule($mch)
- {
- $res = [
- 'is_allowed' => 0,
- 'total_price' => 0,
- 'msg' => ''
- ];
- if ($mch['mch_id'] > 0) {
- $res['msg'] = '商户不支持起送规则';
- return $res;
- }
- if (!$this->address) {
- $res['msg'] = '请选择收货地址';
- return $res;
- }
- $offerRule = OfferPrice::findOne(['store_id' => get_store_id(),'is_delete' => 0]);
- if (!$offerRule) {
- $res['msg'] = '起送规则不存在';
- return $res;
- }
- if ($offerRule->is_enable == 0) {
- $res['msg'] = '起送规则未开启';
- return $res;
- }
- $ruleList = Json::decode($offerRule->detail);
- $res['total_price'] = $offerRule->price;
- if (is_array($ruleList)) {
- foreach ($ruleList as $value) {
- foreach ($value['province_list'] as $item) {
- if (isset($item['districtAll'])) {
- if ($item['id'] == $this->address['city_id']) {
- $res['total_price'] = $value['offer_price'];
- }
- } else if (isset($item['cityAll'])){
- if ($item['id'] == $this->address['province_id']) {
- $res['total_price'] = $value['offer_price'];
- }
- } else {
- if ($item['id'] == $this->address['district_id']) {
- $res['total_price'] = $value['offer_price'];
- }
- }
- }
- }
- }
- if ($mch['total_price'] >= $res['total_price']) {
- $res['is_allowed'] = 0;
- } else {
- $res['is_allowed'] = 1;
- }
- $value = round($res['total_price'] - $mch['total_price'], 2);
- $res['msg'] = "自营商品,还差{$value}元起送";
- return $res;
- }
- protected function getTerritorialLimitation($mch)
- {
- $isArea = 0;
- if ($mch['mch_id'] > 0) {
- return $isArea;
- }
- if ($this->address) {
- $area = TerritorialLimitation::findOne([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'is_enable' => 1,
- ]);
- if ($area) {
- $city_id = []; //限制的地区ID
- $detail = json_decode($area->detail);
- if (!is_array($detail)) {
- $detail = [];
- }
- foreach ($detail as $key => $value) {
- foreach ($value->province_list as $key2 => $value2) {
- $city_id[] = $value2->id;
- }
- }
- $addressArr = [
- $this->address['province_id'],
- $this->address['city_id'],
- $this->address['district_id']
- ];
- $addressArray = array_intersect($addressArr, $city_id);
- if (empty($addressArray)) {
- $isArea = 1;
- }
- }
- }
- return $isArea;
- }
- // 包邮规则
- protected function getFreeDeliveryRules($mch, $expressPrice, $mchTotalPrice)
- {
- if ($expressPrice == 0) {
- // return $expressPrice;
- return ['expressPrice' => $expressPrice, 'diff' => 0];
- }
- $freeDeliveryPrice = 0;
- if ($mch['mch_id'] == 0) {
- $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id ?? get_store_id(), 'is_delete' => 0, 'mch_id' => 0])->asArray()->all();
- foreach ($free as $k => $v) {
- $city = json_decode($v['city'], true);
- foreach ($city as $v1) {
- if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) {
- $expressPrice = 0;
- break;
- }
- if ($this->address['district_id'] == $v1['id']) {
- $freeDeliveryPrice = max($freeDeliveryPrice, $v['price']);
- }
- }
- }
- } else {
- $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id ?? get_store_id(), 'mch_id' => $mch['mch_id'], 'is_delete' => 0])->asArray()->all();
- foreach ($free as $k => $v) {
- $city = json_decode($v['city'], true);
- foreach ($city as $v1) {
- if ($this->address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) {
- $expressPrice = 0;
- break;
- }
- }
- }
- }
- $diff = 0;
- // echo $mchTotalPrice;die;
- if ($freeDeliveryPrice > 0) {
- $diff = $freeDeliveryPrice - $mchTotalPrice;
- }
- if ($expressPrice > 0) {
- return ['expressPrice' => $expressPrice, 'diff' => $diff];
- } else {
- return ['expressPrice' => $expressPrice, 'diff' => 0];
- }
- }
- // 获取用户填写的自定义表单
- protected function getForm(&$form)
- {
- if ($form['is_form'] == 1) {
- $formList = &$form['list'];
- foreach ($formList 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']
- ];
- }
- }
- }
- if ($value['type'] == 'checkbox') {
- $formList[$index]['default'] = is_array($value['default']) ? implode(',', $value['default']) : $value['default'];
- }
- }
- }
- return $form;
- }
- protected function goodsCardList()
- {
- $list = [];
- foreach ($this->mch_list as $mch) {
- if($mch['mch_id'] == 0) {
- foreach ($mch['goods_list'] as $goods) {
- if (!empty($goods['goods_list'])) {
- foreach ($goods['goods_list'] as $val) {
- if(!$val['goods_card_list']) {
- $val['goods_card_list'] = [];
- }
- $list = array_merge($list, $val['goods_card_list']);
- }
- }
- }
- }
- }
- return $list;
- }
- /**
- * @param $goods
- * @param $type
- */
- public function bookCheckGoodsNum($goods, $type)
- {
- // 酒店预约
- if ($type == 1) {
- // $date_book = GoodsBook::findOne(['goods_id' => $goods['id']])->date_book;
- // $date_book = Json::decode($date_book);
- // if (!$date_book) {
- // return [
- // 'code' => 1,
- // 'msg' => '数据异常'
- // ];
- // }
- // $data_config = array_combine(array_column($date_book, 'date'), array_column($date_book, 'num'));
- //
- // foreach ($goods['book'] as $value) {
- // if ($data_config[$value['date']] < $goods['num']) {
- // return [
- // 'code' => 1,
- // 'msg' => '选中日期' . $value['date'] . '内暂无房源'
- // ];
- // }
- // }
- }
- // 服务预约
- if ($type == 2) {
- $service_book = GoodsBook::findOne(['goods_id' => $goods['id']])->service_book;
- $service_book = Json::decode($service_book);
- if (!$service_book) {
- return [
- 'code' => 1,
- 'msg' => '数据异常'
- ];
- }
- $date_data = $this->getDateByInterval(7);
- $count = 0;
- $new_arr = [
- 'data' => []
- ];
- $order = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id')
- ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']])
- ->andWhere(['>', 'o.created_at', strtotime(date("Y-m-d"))])
- ->select('od.attr, od.num')->asArray()->all();
- 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' => '数据异常'
- ];
- }
- $service_book = $new_arr['data'];
- $service_book_date = array_column($service_book, NULL, 'date');
- $service_book_time = $service_book_date[$goods['service']['date']]['time'];
- $num = 0;
- $price = 0;
- foreach ($order as $item) {
- $item['attr'] = json_decode($item['attr'], true);
- if ($item['attr']['date'] === $goods['service']['date']) {
- if ($goods['service']['time'] === $item['attr']['time']) {
- $num += $item['num'];
- }
- }
- }
- foreach ($service_book_time as $m) {
- foreach ($m['times'] as $n) {
- if ($n['time'] == $goods['service']['time']) {
- if (($goods['num'] + $num) > $n['num']) {
- return [
- 'code' => 1,
- 'msg' => $n['time'] . '时间段内暂不可预约'
- ];
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'price' => $price
- ]
- ];
- }
- /**
- * 计算初始价格
- * @param $order_id
- * @return array
- */
- public static function findPrice($order_id)
- {
- $order = Order::findOne($order_id);
- $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->asArray()->all();
- $original_price = 0;
- $md_price = 0;
- foreach ($order_detail as $detail) {
- $goods = Goods::findOne($detail['goods_id']);
- if ($goods->use_attr > 0) {
- $attrs = Json::decode($goods->attr);
- // 当前选择的规格
- $attrIdArr = [];
- foreach (Json::decode($detail['attr']) as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- foreach ($attrs as $attr) {
- $attrIds = [];
- foreach ($attr['attr_list'] as $item) {
- $attrIds[] = $item['attr_id'];
- }
- sort($attrIds);
- sort($attrIdArr);
- // 找出当前规格信息
- if (implode($attrIds) === implode($attrIdArr)) {
- $attr_price = $attr['price'];
- if (intval($detail['is_level']) && $order->discount) {
- $attr_price = bcdiv(bcmul($attr_price, $order->discount, 2), 10, 2);
- }
- $original_price += $attr_price;
- }
- }
- } else {
- $goods_price = $goods->price;
- if (intval($detail['is_level']) && $order->discount) {
- $goods_price = bcdiv(bcmul($goods_price, $order->discount, 2), 10, 2);
- }
- $original_price += $goods_price;
- }
- }
- foreach ($order_detail as $detail) {
- $md_goods = MdGoods::findOne(['goods_id' => $detail['goods_id'], 'md_id' => get_md_id()]);
- $goods = Goods::findOne($detail['goods_id']);
- $goods_attr = $goods->attr;
- if ($md_goods) {
- $goods_attr = $md_goods->attr;
- }
- if ($goods->use_attr == 1) {
- $attrs = Json::decode($goods_attr);
- // 当前选择的规格
- $attrIdArr = [];
- foreach (Json::decode($detail['attr']) as $attrListItem) {
- $attrIdArr[] = $attrListItem['attr_id'];
- }
- foreach ($attrs as $attr) {
- $attrIds = [];
- foreach ($attr['attr_list'] as $item) {
- $attrIds[] = $item['attr_id'];
- }
- sort($attrIds);
- sort($attrIdArr);
- // 找出当前规格信息
- if (implode($attrIds) === implode($attrIdArr)) {
- $attr_price = $attr['price'];
- if (intval($detail['is_level']) && $order->discount) {
- $attr_price = bcdiv(bcmul($attr_price, $order->discount, 2), 10, 2);
- }
- $md_price += $attr_price;
- }
- }
- } else {
- if ($md_goods) {
- $md_goods_price = $md_goods->price;
- } else {
- $md_goods_price = $goods->price;
- }
- if (intval($detail['is_level']) && $order->discount) {
- $md_goods_price = bcdiv(bcmul($md_goods_price, $order->discount, 2), 10, 2);
- }
- $md_price += $md_goods_price;
- }
- }
- return [
- 'original_price' => $original_price > 0 ? $original_price : $md_price,
- 'md_price' => $md_price
- ];
- }
- public function getLeaguePrice($mchList)
- {
- //计算联盟券
- $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 as &$arr) {
- $goods = Goods::findOne($arr['goods_id']);
- //TODO 新的
- //计算返利
- $send_price += floatval($goods->goods_send_profit * $arr['price'] * 0.01 * $arr['num']);
- $take_price += floatval($goods->goods_take_price * $arr['price'] * 0.01 * $arr['num']);
- }
- $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;
- }
- if($take_price > $user_league_price){
- $take_price = $user_league_price;
- }
- $show_saas_alliance_coupon = true;
- if (\Yii::$app->prod_is_dandianpu()) {
- $send_price = 0;
- $take_price = 0;
- $show_saas_alliance_coupon = false;
- }
- return [$send_price, $take_price, $show_saas_alliance_coupon, $user_league_price];
- }
- public function getDateByInterval(int $num) :array
- {
- //var_dump($st, $et);die;
- $returnData = [];
- $i = 0;
- do {
- $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d'))));
- $returnData[] = $temp;
- $i++;
- } while ($i < $num);
- return $returnData;
- }
- //判断是否需为代理配送且要上门安装
- public function goodsIsInstall($goods_id)
- {
- try {
- $goods = Goods::findOne($goods_id);
- if ($goods->cloud_goods_id <= 0) {
- return 2;
- }
- $goods_url = "/goods/getAttrParams";
- $param = [
- 'id' => $goods->cloud_goods_id
- ];
- //请求接口
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsInfo = cloud_post($domain . $goods_url, $param);
- $goodsInfo = json_decode($goodsInfo, true);
- if ((int)$goodsInfo['code'] === 0) {
- $goodsInfo = $goodsInfo['data'];
- //需要安装
- if ((int)$goodsInfo['send_type'] === 3 && (int)$goodsInfo['is_need_install'] === 1) {
- return 1;
- }
- //不需要安装
- if ((int)$goodsInfo['send_type'] === 3) {
- return 3;
- }
- }
- return 2;
- } catch (\Exception $e) {
- return 2;
- }
- }
- //判断是否存在代理
- public function goodsIsGoodsAgent($address, $goods_id, $cloud_goods_id = 0)
- {
- try {
- if (empty($address) || !is_array($address)) {
- throw new \Exception('缺少地址信息');
- }
- if (empty($cloud_goods_id)) {
- $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0]);
- if ((!empty($goods) && empty($goods->cloud_goods_id) || empty($goods) )) {
- return [
- 'code' => 0,
- 'msg' => '非云仓商品'
- ];
- }
- $cloud_goods_id = $goods->cloud_goods_id;
- }
- $goods_agent_admin = AgentGoodsBindGoods::find()->where(['cloud_goods_id' => $cloud_goods_id, 'status' => 1, 'is_delete' => 0])
- ->select('goods_agent_admin_id')->column();
- $where = [
- 'province_id' => $address['province_id'],
- 'type' => 'goods_agent',
- 'is_delete' => 0,
- 'id' => $goods_agent_admin
- ];
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 1,
- 'city_id' => $address['city_id'],
- 'district_id' => $address['district_id'],
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 2,
- 'city_id' => $address['city_id'],
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- $admin = Admin::findOne(array_merge(
- [
- 'area_level' => 3,
- ], $where));
- if (!empty($admin)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $admin
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '该区域未存在产品代理,无法下单'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|