| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\jobs\CreatedMdGoods;
- use app\models\AgentFrontErpInventory;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Coupon;
- use app\models\DeliveryRules;
- use app\models\Form;
- use app\models\Goods;
- use app\models\GoodsAreaRules;
- use app\models\GoodsBook;
- use app\models\GoodsBrand;
- use app\models\GoodsBrowse;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\GoodsPic;
- use app\models\Level;
- use app\models\Mch;
- use app\models\Option;
- use app\models\PostageRules;
- use app\models\ShareLevel;
- use app\models\Store;
- use app\models\StoreCloud;
- use app\models\UserCoupon;
- use app\models\VerifyCard;
- use app\models\Warehouse;
- use app\models\WarehouseZone;
- use app\models\WorkerGoods;
- use app\models\WorkerGoodsExt;
- use app\modules\admin\models\booking\BookingForm;
- use app\modules\mch\models\LevelListForm;
- use app\utils\Delivery\Delivery;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\jobs\SyncMdGoodsJob;
- use app\utils\Tools;
- use app\models\GoodsChainLevel;
- use app\models\ShareHolderLevel;
- use app\models\MchGoodsCat;
- use app\modules\admin\models\MchForm;
- class GoodsForm extends Model
- {
- //从银豹同步时设置为1
- public $yinbaoSync = 0;
- public $store_id;
- public $status;
- public $keyword;
- public $cat_id;
- public $type;
- public $id;
- public $name;
- public $key_word;
- public $unit;
- public $price;
- public $can_set_md_price = 1;
- public $cost_price;
- public $original_price;
- public $integral_price;
- public $service;
- public $weight;
- public $freight;
- public $pieces;
- public $forehead;
- public $quick_purchase;
- public $content;
- public $goods_num;
- public $goods_no;
- public $integral;
- public $use_attr;
- public $is_negotiable;
- public $cover_pic;
- public $individual_share;
- public $share_type;
- public $share_commission_first;
- public $share_commission_second;
- public $share_commission_third;
- public $video_url;
- public $attr;
- public $attr_member_price_List;
- public $attr_setting_type;
- public $virtual_sales;
- public $confine_count;
- public $order_min_count;
- public $balance;
- public $is_level;
- public $goods_pic_list;
- public $goods_verify_card_list;
- public $goods_coupon_card_list;
- public $delivery_type;
- public $product_type;
- public $date_book;
- public $service_book;
- public $form;
- public $rate;
- public $rate_type;
- public $chain_rate;
- public $chain_rate_type;
- public $queue_rate;
- public $md_rate;
- public $md_rate_type;
- public $md_hx_rate;
- public $md_hx_rate_type;
- public $dl_p_rate;
- public $dl_c_rate;
- public $dl_d_rate;
- public $dl_t_rate;
- public $dl_p_rate_type;
- public $dl_c_rate_type;
- public $dl_d_rate_type;
- public $dl_t_rate_type;
- public $pic_url;
- public $supplier;
- public $is_change_attr;
- public $sort;
- public $delivery_rules_id;
- public $goods_area_rules_id;
- public $order_rules_buyer_location_id;
- public $food_ext_goods_id;
- public $is_wholesale;
- public $wholesale_num;
- public $wholesale_attr;
- public $pay_price;
- public $accessories_image;
- public $goods_send_profit;
- public $goods_take_price;
- public $service_book_desc;
- public $parameter_list;
- public $buy_type_info;
- public $browse_type_info;
- public $is_open;
- public $chain_level_value;
- public $chain_type;
- public $jst_goods_id;
- public $jst_supplier_id;
- public $desc;
- public $warn;
- public $worker_goods_cat_id;
- public $payment_type;
- public $final_profit;
- public $bind_worker;
- public $worker_ids;
-
- public $booking_goods_cat_id;
- public $share_commission_new_first;
- public $share_commission_new_second;
- public $share_commission_new_third;
- public $send_verify_card_num;
- public $send_coupon_card_num;
- public $brand_id;
- public $risk_blind;//无视风险
- public $share_holder_profit_switch;
- public $share_holder_high_profit;
-
-
- public $time_made_day;
- public $time_shelf_life;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $mch;
- public $mch_id;
- public $mch_cat_id;
-
-
- public $updated_at;
- public $sub_name;
- /**
- * 初始化
- */
- // public function init()
- // {
- // if (!$this->store_id) {
- // $this->store_id = get_store_id();
- // }
- // }
- /**
- * 采购员关联store_id
- */
- public function set_saas_purchase_store_id()
- {
- $this->store_id = get_saas_purchase_store_id();
- }
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['status', 'quick_purchase','store_id', 'id',
- 'freight', 'pieces', 'goods_num', 'use_attr', 'individual_share', 'attr_setting_type',
- 'virtual_sales', 'confine_count', 'order_min_count', 'is_level', 'product_type' , 'is_change_attr', 'chain_rate_type', 'rate_type', 'md_rate_type', 'md_hx_rate_type', 'dl_p_rate_type', 'dl_c_rate_type', 'dl_d_rate_type', 'dl_t_rate_type',
- 'integral_price','sort','can_set_md_price', 'delivery_rules_id', 'goods_area_rules_id', 'is_wholesale',
- 'wholesale_num', 'order_rules_buyer_location_id', 'chain_type', 'brand_id', 'risk_blind', 'share_holder_profit_switch', 'warehouse_id', 'warehouse_zone_id'], 'integer'],
- [['keyword', 'key_word'], 'string', 'max' => 60],
- [['name','service', 'video_url', 'goods_no', 'cover_pic', 'desc', 'sub_name'], 'string', 'max' => 255],
- [['content','pic_url', 'wholesale_attr','accessories_image', 'warn', 'worker_ids', 'share_commission_new_first', 'share_commission_new_second', 'share_commission_new_third'], 'string'],
- [['attr', 'attr_member_price_List', 'goods_verify_card_list', 'service_book', 'date_book', 'form', 'supplier', 'parameter_list', 'goods_coupon_card_list'], 'safe',],
- [['unit'], 'string', 'max' => 10],
- [['status', 'quick_purchase', 'chain_type'], 'default', 'value' => 1],
- [['cat_id', 'integral', 'goods_pic_list', 'delivery_type'], 'default', 'value' => []],
- [['use_attr', 'is_negotiable', 'individual_share',
- 'share_type', 'forehead', 'freight', 'attr_setting_type',
- 'virtual_sales', 'confine_count', 'order_min_count', 'is_level', 'is_open', 'share_holder_profit_switch'], 'default', 'value' => 0],
- [['sort'], 'default', 'value' => 100],
- [['unit'], 'default', 'value' => '件'],
- [['price', 'cost_price', 'original_price', 'forehead', 'share_commission_first', 'share_commission_second', 'share_commission_third', 'weight', 'chain_rate', 'rate', 'md_rate', 'md_hx_rate', 'dl_p_rate', 'dl_c_rate', 'dl_d_rate', 'dl_t_rate', 'pay_price', 'send_verify_card_num', 'send_coupon_card_num', 'share_holder_high_profit', 'balance','queue_rate'], 'number', 'max' => 9999999999],
- [['food_ext_goods_id', 'goods_send_profit', 'goods_take_price', 'buy_type_info', 'service_book_desc', 'chain_level_value', 'worker_goods_cat_id', 'payment_type', 'final_profit', 'bind_worker','browse_type_info'], 'safe'],
- [['booking_goods_cat_id'], 'safe'],
- [['jst_goods_id', 'jst_supplier_id'], 'safe'],
- [['time_made_day', 'time_shelf_life'], 'safe'],
- [['mch', 'mch_id', 'mch_cat_id'], 'safe'],
- [['updated_at'], 'safe'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '商品名称',
- 'sub_name' => '商品副标题名称',
- 'keyword' => '关键字',
- 'cat_id' => '分类',
- 'unit' => '单位',
- 'price' => '售价',
- 'cost_price' => '成本价',
- 'original_price' => '原价',
- 'service' => '服务',
- 'forehead' => '满额包邮',
- 'quick_purchase' => '是否添加快速购买',
- 'content' => '图文详情',
- 'use_attr' => '是否使用规格',
- 'is_negotiable' => '是否开启面议',
- 'goods_pic_list' => '商品图片',
- 'is_change_attr' => "是否更改规格值",
- 'is_wholesale' => '是否是批发来源',
- 'wholesale_num' => '批发数',
- 'wholesale_attr' => '批发规格',
- 'parameter_list' => '商品参数',
- 'service_book_desc' => '酒店预约产品其他信息',
- 'desc' => '服务商品特色',
- 'warn' => '预定须知'
- ];
- }
- //导入云仓商品
- public function saveCloudGoods($num_type = 0){
- if(!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- //获取议价比
- // $content = Option::get('cloud', 0, 'saas');
- // $content = json_decode($content['value'], true);
- $rate = 0;
- $content = Store::find()->where(['id' => $this->store_id])->select('rate')->asArray()->one();
- if ($content) {
- $rate = $content['rate'];
- }
- $store_cloud = StoreCloud::findOne(['store_id' => $this->store_id, 'is_delete' => 0]);
- //是否拥有选品权限
- if (!intval($store_cloud->can_distribution)) {
- return [
- 'code' => 1,
- 'msg' => '未开启选品权限',
- ];
- }
- //选品更新自动更新商城销售价格
- $cloud_is_update = Option::get('cloud_is_update', $this->store_id, 'store')['value'];
- if (intval($cloud_is_update)) {
- $repeat_update_goods_price = Option::get('repeat_update_goods_price', $this->store_id, 'store')['value'];
- if (!intval($repeat_update_goods_price)) {
- $num_type = 1;
- }
- }
- //请求接口
- $goods_url = "/goods/getAttrParams";
- $param = [
- 'id' => $this->id,
- 'mch_id' => $store_cloud->cloud_store_id
- ];
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsInfo = cloud_post($domain.$goods_url,$param);
- $goodsInfo = json_decode($goodsInfo, true);
- if($goodsInfo['code'] != 0){
- return [
- 'code' => 1,
- 'msg' => $goodsInfo['msg']
- ];
- }
- $goodsInfo = $goodsInfo['data'];
- if (empty($goodsInfo)) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在',
- ];
- }
- $channel_rate = 10;
- if ($goodsInfo['data']['channel_rate']) {
- $channel_rate = $goodsInfo['data']['channel_rate'];
- }
- $channel_rate = bcdiv($channel_rate, 10, 2);
- $where = [
- 'AND', [
- 'cloud_goods_id' => $this->id,
- 'store_id' => $this->store_id,
- 'is_wholesale' => !empty($this->is_wholesale) ? $this->is_wholesale : 0,//批发与铺货不同步
- ], [
- 'OR', [
- 'is_delete' => 0
- ],[
- 'is_delete' => 1,
- 'is_forbidden' => 1
- ]
- ]
- ];
- $goods = Goods::find()->where($where)->one();
- if (empty($goods)) {
- $goods = new Goods();
- $num_type = 0;
- } else {
- if (empty($goodsInfo['id'])) {
- $goods->is_delete = 1;
- $goods->save();
- goto goods_delete;
- }
- }
- $is_front_delivery = 0;
- if (intval($goodsInfo['send_type']) === 5) {//仓库同城
- $is_front_delivery = 1;
- }
- $is_front_express = 0;
- if (intval($goodsInfo['send_type']) === 6) {//仓库快递
- $is_front_express = 1;
- }
- $is_front_centralize = 0;
- if (intval($goodsInfo['send_type']) === 1) {//集采
- $is_front_centralize = 1;
- }
- $agent_goods_status = 0;
- if (intval($goodsInfo['send_type']) === 3) {//代理配送
- $agent_goods_status = 1;
- if (intval($goodsInfo['is_need_install']) === 1) {
- $agent_goods_status = 2;
- }
- }
- $is_cloud_express = 0;
- if (in_array($goodsInfo['send_type'], [0, 4]) && !intval($goodsInfo['stbz_goods_id'])) {//快递 / 库存
- $is_cloud_express = 1;
- }
- $goods->is_cloud_express = $is_cloud_express;
- $goods->is_front_delivery = $is_front_delivery;
- $goods->is_front_centralize = $is_front_centralize;
- $goods->agent_goods_status = $agent_goods_status;
- $goods->is_front_express = $is_front_express;
- $goods->status = (int)$goodsInfo['status'];
- $goods->store_id = $this->store_id;
- $goods->name = $goodsInfo['name'] ?: '';
- $goods->unit = $goodsInfo['unit'] ?: '';//单位
- $goods->cover_pic = $goodsInfo['pic_url'] ?: "";
- $goods->weight = $goodsInfo['weight'] ?: "0.00";
- $sale_price_type = Option::get('sale_price_type', $this->store_id, 'store')['value'];
- $this->pay_price = 0;
- // if ($goodsInfo['original_price']) {
- // $this->price = $goods->price = $goodsInfo['original_price'];
- // $goods->original_price = ($this->price + ($goodsInfo['original_price'] * ($rate / 100))) ?: 0.00;
- // } else {
- if (!$num_type) {
- $this->price = $this->pay_price = intval($sale_price_type) === 0 ? ($goodsInfo['platform_negotiated_price'] + ($goodsInfo['platform_negotiated_price']) * ($rate / 100)) : $goodsInfo['original_price'];
- $goods->price = $this->pay_price ?: '0.00';
- $goods->original_price = doubleval($goodsInfo['market_price']) ?: ($goodsInfo['underlined_price'] ?? $goodsInfo['price']);
- }
- // }
- $this->cost_price = $goods->cost_price = bcmul($goodsInfo['platform_negotiated_price'], $channel_rate, 2);
- $goods->cloud_supplier_id = $goodsInfo['supplier_id'] ?: 0;
- $goods->cloud_goods_id = $this->id;
- $goods->detail = $goodsInfo['desc'] ?: "";
- $goods->stbz_goods_id = $goodsInfo['stbz_goods_id'] ?: 0;
- $goods->video_url = $goodsInfo['video_url'] ?: '';
- if (!empty($this->is_wholesale)) {
- $this->goods_num = $goods->goods_num = ($this->wholesale_num ?: 0);
- } else {
- $this->goods_num = $goods->goods_num = $goodsInfo['num'] ?: 0;
- }
- $goods->is_wholesale = $this->is_wholesale ?: 0;
- $this->use_attr = $goods->use_attr = (int)$goodsInfo['use_attr'] ?? 1;
- $this->goods_no = $goods->goods_no = $goodsInfo['goods_no'] ?: 0;
- if (!$goods->save()) {
- return [
- 'code' => 1,
- 'msg' => "导入失败" . json_encode($goods->errors, JSON_UNESCAPED_UNICODE)
- ];
- }
- $cloudBindInfo = [];
- $this->attr = json_decode($goodsInfo['attrs'], true);
- if (!$num_type) {
- if (!empty($this->wholesale_attr) && !empty($this->attr)) {
- $this->wholesale_attr = json_decode($this->wholesale_attr, true);
- $wholesale_attr_id = array_column($this->wholesale_attr, 'attr_id');
- sort($wholesale_attr_id);
- foreach ($this->attr as $index => $item) {
- $attr_id = array_column($item['attr_list'], 'attr_id');
- sort($attr_id);
- if (array_diff($wholesale_attr_id, $attr_id)) {
- $this->attr[$index]['num'] = 0;
- } else {
- $this->attr[$index]['num'] = $this->wholesale_num;
- $this->attr[$index]['price'] = $this->pay_price;
- }
- }
- }
- }
- $this->setAttr($goods, $rate ?: 10, $channel_rate, $num_type, $goodsInfo['original_price']);
-
-
- $pic_list = $goodsInfo['pic_list'];
- $pic_list = json_decode($pic_list, true);
- GoodsPic::deleteAll(['goods_id' => $goods->id]);
- $pic = new GoodsPic();
- $pic->goods_id = $goods->id;
- $pic->pic_url = $goodsInfo['pic_url'];
- $pic->save();
- if ($pic_list) {
- foreach ($pic_list as $item) {
- if (!empty($item)) {
- $pic = new GoodsPic();
- $pic->goods_id = $goods->id;
- $pic->pic_url = $item['pic_url'] ?? $item;
- if (!$pic->save()) {
- continue;
- };
- }
- }
- }
- if (is_string($this->cat_id)) {
- $this->cat_id = explode(',', $this->cat_id);
- }
- if ($this->cat_id && count($this->cat_id)) {
- if ($goods->id > 0) {
- GoodsCat::deleteAll(['goods_id' => $goods->id]);
- }
- foreach ($this->cat_id as $cat_id) {
- $goodsCat = new GoodsCat();
- $goodsCat->goods_id = $goods->id;
- $goodsCat->cat_id = $cat_id;
- $goodsCat->store_id = $goods->store_id;
- if (!$goodsCat->save()) {
- continue;
- }
- }
- }
- goods_delete:
- $cloudBindInfo[0]['goods_id'] = $goods->id;
- $cloudBindInfo[0]['cloud_supplier_id'] = $goods->cloud_supplier_id;
- $cloudBindInfo[0]['cloud_goods_id'] = $goods->cloud_goods_id;
- $data['goods_id'] = $goods->cloud_goods_id;
- $data['cloudBindInfo'] = $cloudBindInfo;
- $data['_goods'] = $goods;
- // 同步门店商品
- queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]), 0, 1);
- return $data;
- }
- public function saveLeague() {
- $goods = Goods::findOne($this->id);
- //TODO id790 获取总设置
- $store_profit_max_model = Option::findOne(['name' => 'store_profit_max']);
- $store_profit_min_model = Option::findOne(['name' => 'store_profit_min']);
- $store_take_max_model = Option::findOne(['name' => 'store_send_min']);
- if ($this->goods_send_profit > 0 && $this->goods_send_profit < $store_profit_min_model->value) {
- return [
- 'code' => 1,
- 'msg' => '总平台设置最低让利比例为' . $store_profit_min_model->value . '未能达到'
- ];
- }
- if ($store_profit_max_model->value && $this->goods_send_profit > $store_profit_max_model->value) {
- return [
- 'code' => 1,
- 'msg' => '总平台设置最高让利比例为' . $store_profit_max_model->value . ',现已超出'
- ];
- }
- if($store_take_max_model->value && $this->goods_take_price > $store_take_max_model->value){
- return [
- 'code' => 1,
- 'msg' => '总平台设置最高扣除比例为' . $store_take_max_model->value . ',现已超出'
- ];
- }
- if(!$this->goods_send_profit){
- $this_goods_send_profit = 0;
- }else{
- $this_goods_send_profit = $this->goods_send_profit;
- }
- if(!$this->goods_take_price){
- $this_goods_take_price = 0;
- }else{
- $this_goods_take_price = $this->goods_take_price;
- }
- $goods->goods_take_price = $this_goods_take_price;
- $goods->goods_send_profit = $this_goods_send_profit;
- if ($goods->save()) {
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '操作失败,' . array_shift($goods->getFirstErrors()),
- ];
- }
- public function save()
- {
- if(!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- /* begin 2024/03/15 09:49:35 商品风控检测 WPing丶 */
- $is_risk = 0;
- $is_show_risk = Option::get(OptionSetting::IS_SHOW_RISK, $this->store_id, 'store', '')['value'];
- if (intval($is_show_risk)) {//根据后台设置的开关 判断是否需要隐藏风险提醒
- //检测商品价格信息是否有赔本的风险,如果有则先不保存数据提示客户后再进行下一步操作
- foreach((array)$this->attr as $attr_item) {
- if($this->jst_goods_id){
- break;
- }
- $max_discount_price = $this->risk($attr_item);
- $price = $this->use_attr == 1 ? $attr_item['price'] : $this->price;
- if($this->is_level) {
- $level_list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])
- ->select('level')->column();
- foreach($level_list as $level) {
- if(isset($attr_item['member'.$level]) && $attr_item['member'.$level] < $price) {
- $price = $attr_item['member'.$level];
- }
- }
- }
- $cost_price = $this->use_attr == 1 ? $attr_item['cost_price'] : $this->cost_price;
- $profit = bcsub($price,$cost_price,2);
- if($profit < $max_discount_price) {
- if($this->risk_blind == 0) {
- $attr_name = '';
- foreach($attr_item['attr_list'] as $kkk => $vvv) {
- if($kkk + 1 == count($attr_item['attr_list'])) {
- $attr_name.=$vvv['attr_group_name'].':'.$vvv['attr_name'];
- } else {
- $attr_name.=$vvv['attr_group_name'].':'.$vvv['attr_name'].',';
- }
- }
- if (intval($this->product_type) == Goods::GOODS_TYPE_NORMAL) {
- return [
- 'code' => 1,
- 'msg' => '规格为:'.$attr_name.'的商品当前价格有风险,是否确认保存?',
- 'is_risk' => 1,
- ];
- }
- } else {
- $is_risk = 1;
- }
- }
- }
- }
- /* end */
- // 同城配送需要校验信息
- // if (in_array('delivery', $this->delivery_type)) {
- // $res = Delivery::getBindAccount();
- // if ($res['code'] > 0) {
- // if ($res['code'] == 48001) {
- // return [
- // 'code' => 1,
- // 'msg' => '请在微信小程序后台开通即时配送物流服务'
- // ];
- // }
- // return $res;
- // }
- // if (empty($res['data']['shop_list'])) {
- // return [
- // 'code' => 1,
- // 'msg' => '当前未绑定运力账号,商品暂不能选择同城配送方式'
- // ];
- // }
- // $shop_list = $res['data']['shop_list'];
- // $count = 0;
- // foreach ($shop_list as $shop) {
- // if ($shop['audit_result'] == 0) {
- // $count++;
- // }
- // }
- // if ($count < 1) {
- // return [
- // 'code' => 1,
- // 'msg' => '当前暂无可用运力配送,商品暂不能选择同城配送方式'
- // ];
- // }
- // }
- $full_cut = [
- 'pieces' => 0,
- 'forehead' => 0
- ];
- if($this->pieces){
- $full_cut['pieces'] = $this->pieces;
- }
- if($this->forehead){
- $full_cut['forehead'] = $this->forehead;
- }
- //去掉逻辑
- // if ($this->product_type == Goods::GOODS_TYPE_INTEGRAL && $this->integral['forehead'] <= 0) {
- // return [
- // 'code' => 1,
- // 'msg' => '积分商品必须填写积分可兑换金额'
- // ];
- // }
- $t = \Yii::$app->db->beginTransaction();
- $goods = $this->id ? Goods::findOne($this->id) ?: new Goods(['yinbaoSync' => $this->yinbaoSync]) : new Goods(['yinbaoSync' => $this->yinbaoSync]);
- if($this->id){
- if($this->updated_at && ($this->updated_at != $goods->updated_at)){
- return [
- 'code' => 1,
- 'msg' => '商品信息发生变化,请刷新后重新编辑',
- ];
- }
- }
- $statusInit = $goods->status;
- !empty($this->jst_goods_id) && $goods->jst_goods_id = $this->jst_goods_id;
- !empty($this->jst_supplier_id) && $goods->jst_supplier_id = $this->jst_supplier_id;
- !empty($this->status) && $goods->status = $this->status;
- $goods->store_id = $this->store_id;
- $goods->name = $this->name;
- $goods->sub_name = $this->sub_name;
- $goods->key_word = $this->key_word;
- $goods->unit = $this->unit;
- $goods->price = $this->price;
- $goods->can_set_md_price = $this->can_set_md_price;
- $goods->integral_price = (isset($this->integral_price) && $this->integral_price> 0) ? intval($this->integral_price) : 0;
- $goods->cost_price = $this->cost_price;
- $goods->original_price = $this->original_price ?: $this->price;
- $goods->service = $this->service;
- $goods->weight = $this->weight;
- $goods->freight = $this->freight;
- $goods->pieces = $this->pieces;
- $goods->full_cut = Json::encode($full_cut);
- $goods->forehead = $this->forehead;
- $goods->quick_purchase = $this->quick_purchase;
- $goods->video_url = $this->video_url;
- $goods->detail = $this->content;
- $goods->goods_num = $this->goods_num ?? 0;
- $goods->goods_no = $this->goods_no;
- $goods->integral = json_encode($this->integral);
- $goods->use_attr = $this->use_attr;
- $goods->is_negotiable = $this->is_negotiable;
- $goods->cover_pic = $this->cover_pic;
- $goods->individual_share = $this->individual_share;
- $goods->share_type = $this->share_type;
- $goods->share_commission_first = $this->share_commission_first ? $this->share_commission_first : 0;
- $goods->share_commission_second = $this->share_commission_second ? $this->share_commission_second : 0;
- $goods->share_commission_third = $this->share_commission_third ? $this->share_commission_third : 0;
- $goods->attr_setting_type = $this->attr_setting_type ? 1 : 0;
- $goods->virtual_sales = $this->virtual_sales;
- $goods->confine_count = $this->confine_count;
- isset($this->order_min_count) && $goods->order_min_count = $this->order_min_count;
- $goods->balance = $this->balance;
- $goods->rate = $this->rate ? $this->rate : 0;
- $goods->rate_type = $this->rate_type == 1 ? 1 : 0;
- $goods->chain_rate = $this->chain_rate ? $this->chain_rate : 0;
- $goods->chain_rate_type = $this->chain_rate_type == 1 ? 1 : 0;
- $goods->queue_rate = $this->queue_rate ? $this->queue_rate : 0;
- $goods->md_rate = $this->md_rate ?: 0;
- $goods->md_rate_type = $this->md_rate_type == 1 ? 1 : 0;
- $goods->md_hx_rate = $this->md_hx_rate ?: 0;
- $goods->md_hx_rate_type = $this->md_hx_rate_type == 1 ? 1 : 0;
- $goods->dl_p_rate = $this->dl_p_rate ?: 0;
- $goods->dl_c_rate = $this->dl_c_rate ?: 0;
- $goods->dl_d_rate = $this->dl_d_rate ?: 0;
- $goods->dl_t_rate = $this->dl_t_rate ?: 0;
- $goods->dl_p_rate_type = $this->dl_p_rate_type == 1 ? 1 : 0;
- $goods->dl_c_rate_type = $this->dl_c_rate_type == 1 ? 1 : 0;
- $goods->dl_d_rate_type = $this->dl_d_rate_type == 1 ? 1 : 0;
- $goods->dl_t_rate_type = $this->dl_t_rate_type == 1 ? 1 : 0;
- $goods->is_level = $this->is_level;
- $goods->delivery_type = $this->delivery_type ? json_encode($this->delivery_type) : '["express"]';
- $goods->product_type = $this->product_type;
- $goods->form_name = $this->form['form_name'];
- $goods->is_form = $this->form['is_form'] ?: 0;
- $goods->delivery_rules_id = $this->delivery_rules_id;
- $goods->goods_area_rules_id = $this->goods_area_rules_id;
- $goods->order_rules_buyer_location_id = $this->order_rules_buyer_location_id;
- $goods->accessories_image = $this->accessories_image ?? '';
- $goods->service_book_desc = $this->service_book_desc;
- if(isset($this->time_made_day)){
- if(empty($this->time_made_day)){
- $goods->time_made_day = 0;
- }else{
- $goods->time_made_day = strtotime($this->time_made_day);
- }
- }
- isset($this->time_shelf_life) && $goods->time_shelf_life = intval($this->time_shelf_life);
- $goods->is_risk = $is_risk;
- $goodsBrand = GoodsBrand::findOne($this->brand_id);
- $goods->brand_id = $goodsBrand->id ?? 0;
- if ($goods->product_type === Goods::GOODS_TYPE_VIEW) {
- $goods->goods_num = $this->goods_num = 10000000;
- }
- //新单独分销
- $goods->share_commission_new_first = $this->share_commission_new_first ?: '';
- $goods->share_commission_new_second = $this->share_commission_new_second ?: '';
- $goods->share_commission_new_third = $this->share_commission_new_third ?: '';
- $goods->share_holder_profit_switch = $this->share_holder_profit_switch ?: '';
- $goods->share_holder_high_profit = $this->share_holder_high_profit ?: '';
- if (!empty($this->buy_type_info) && is_array($this->buy_type_info)) {
- if (!in_array($this->buy_type_info['type'], [0, 1, 2])) {
- return [
- 'code'=>1,
- 'msg'=>'商品购买类型设置错误'
- ];
- }
- if ((int)$this->buy_type_info['type'] === 1) {
- $level_list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])
- ->select('level')->column();
- if (!empty($this->buy_type_info['member_level'])) {
- foreach ($this->buy_type_info['member_level'] as $member_level) {
- if (!in_array($member_level, array_merge($level_list, [0, -1]))) {
- return [
- 'code'=>1,
- 'msg'=>'商品购买会员类型错误'
- ];
- }
- }
- } else {
- $this->buy_type_info['member_level'] = [
- 0
- ];
- }
- }
- }
- $goods->buy_type_info = json_encode($this->buy_type_info);
- if ($this->parameter_list){
- foreach ($this->parameter_list as $key => $value ) {
- if (empty($value['name']) || empty($value['value'])) {
- unset($this->parameter_list[$key]);
- }
- }
- $goods->parameter_list = json_encode($this->parameter_list,JSON_UNESCAPED_UNICODE);
- }else{
- $goods->parameter_list = '';
- }
- // if(isset($this->sort) && $this->sort> 0 ){
- // $goods->sort = $this->sort;
- // }
- if (isset($this->goods_verify_card_list)) {
- $verify_card_id = '';
- foreach($this->goods_verify_card_list as $val) {
- $verify_card_id .= $verify_card_id ? ','.$val['id'] : $val['id'];
- }
- $goods->verify_card_id = $verify_card_id;
- }
- if (isset($this->goods_coupon_card_list)) {
- $coupon_card_id = '';
- foreach($this->goods_coupon_card_list as $val) {
- $coupon_card_id .= $coupon_card_id ? ','.$val['id'] : $val['id'];
- }
- $goods->coupon_card_id = $coupon_card_id;
- } else {
- $goods->coupon_card_id = '';
- }
- $goods->send_verify_card_num = $this->send_verify_card_num;
- $goods->send_coupon_card_num = $this->send_coupon_card_num;
- if ((int)$this->product_type === 2) {
- $service_book = $this->service_book;
- $num = 0;
- if (!is_array($service_book['data'][0]['time'][0]['times'])) {
- return [
- 'code'=>1,
- 'msg'=>'预约商品信息规格有误'
- ];
- }
- foreach ($service_book['data'][0]['time'][0]['times'] as $item) {
- $num += $item['num'];
- }
- $goods->service_book_num = $num;
- }
- if ($this->product_type === Goods::GOODS_TYPE_WORKER) {
- $goods->status = 1;
- }
- if($this->mch){
- $statusChange = !$this->id || ($statusInit != $goods->status);
- $mchSetting = (new MchForm(['store_id' => $this->store_id]))->mchSetting();
- if($mchSetting['mch_goods_audit'] && $statusChange){
- $goods->mch_audit = 1;
- $goods->mch_audit_time = time();
- $goods->status = Goods::STATUS_DISABLE;
- }
- $goods->mch_id = $this->mch_id;
- }
- if ($goods->save()) {
- if($this->mch){
- $saveCat = MchGoodsCat::saveCat($goods->id, $this->mch_cat_id);
- if ($saveCat['code']) {
- $t->rollBack();
- return $saveCat;
- }
- }
- $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods['id']]);
- if (!$goodsChainLevel) {
- $goodsChainLevel = new GoodsChainLevel();
- $goodsChainLevel->goods_id = $goods['id'];
- }
- $goodsChainLevel->type = $this->chain_type;
- $goodsChainLevel->is_open = $this->is_open;
- $goodsChainLevel->value = json_encode($this->chain_level_value);
- $goodsChainLevel->save();
- // 服务商品
- $bookingGoodsSave = $this->bookingGoodsSave($goods);
- if($bookingGoodsSave['code'] != 0){
- $t->rollBack();
- return $bookingGoodsSave;
- }
- // 服务商品
- if ($this->product_type === Goods::GOODS_TYPE_WORKER) {
- $payment_type = (int)$this->payment_type;
- $worker_ids = $this->worker_ids;
- $bind_worker = (int)$this->bind_worker;
- if (!in_array($payment_type, [0, 1])) {
- $t->rollBack();
- return [
- 'code'=>1,
- 'msg'=>'服务商品状态'
- ];
- }
- $goods_ext = WorkerGoodsExt::findOne(['goods_id' => $goods->id]) ?? new WorkerGoodsExt();
- $goods_ext->store_id = $goods->store_id;
- $goods_ext->goods_id = $goods->id;
- $goods_ext->payment_type = $payment_type;
- $goods_ext->desc = $this->desc;
- $goods_ext->warn = $this->warn;
- $goods_ext->cat_id = $this->worker_goods_cat_id;
- $goods_ext->final_profit = $this->final_profit;
- $goods_ext->bind_worker = $bind_worker;
- if (!$goods_ext->save()) {
- $t->rollBack();
- return [
- 'code'=>1,
- 'msg'=>'服务商品保存错误'
- ];
- };
- if ($worker_ids) {
- if ($bind_worker === 1) {
- $worker_ids = explode(',', $worker_ids);
- if (!empty($worker_ids)) {
- //将后台商品设置的服务人员删除
- WorkerGoods::updateAll(['status' => 0], ['goods_id' => $goods->id, 'public' => 1]);
- foreach ($worker_ids as $worker_id) {
- $worker = WorkerGoods::findOne(['goods_id' => $goods->id, 'worker_id' => $worker_id]);
- if (!$worker) {
- $worker = new WorkerGoods();
- $worker->public = 1;
- $worker->goods_id = $goods->id;
- $worker->worker_id = $worker_id;
- }
- $worker->status = 1;
- if (!$worker->save()) {
- $t->rollBack();
- return [
- 'code'=>1,
- 'msg'=>'服务人员保存错误'
- ];
- };
- }
- }
- }
- }
- }
- // 自定义表单
- Form::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'goods_id' => $goods['id']]);
- if ($this->form['is_form'] == 1) {
- if (!$this->form['form_list']) {
- $t->rollBack();
- return [
- 'code'=>1,
- 'msg'=>'请填写表单设置'
- ];
- }
- }
- if ($this->form['form_list']) {
- $this->form['form_list'] = array_values($this->form['form_list']);
- foreach ($this->form['form_list'] as $index => $value) {
- if (!$value['name']) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请输入字段名称'
- ];
- }
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- if (!$value['default']) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请输入单选或多选的默认值'
- ];
- }
- }
- if (isset($value['id'])) {
- $form = Form::findOne(['store_id' => $this->store_id, 'id' => $value['id']]);
- } else {
- $form = new Form();
- $form->goods_id = $goods['id'];
- }
- $form->is_delete = 0;
- $form->created_at = time();
- $form->type = $value['type'];
- $form->name = $value['name'];
- $form->default = isset($value['default']) ? $value['default'] : '';
- $form->required = $value['required'] ? $value['required'] : 0;
- $form->tip = $value['tip'];
- $form->sort = $index;
- $form->store_id = $this->store_id;
- if (!$form->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $form->errors[0]
- ];
- }
- }
- }
- //判断如果规格发生改变就删除满减规则
- if (!empty($this->is_change_attr)) {
- GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $goods->id]);
- }
- // 预约
- if ($this->product_type == Goods::GOODS_TYPE_DATE) {
- $goods->use_attr = 0;
- // if (empty($this->date_book)) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查预约日期相关配置是否正确'
- // ];
- // }
- //
- // foreach ($this->date_book as $k => $v) {
- // if (intval($v['num']) < 0 || $v['price'] <= 0) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查预约日期的价格和库存参数是否填写正确'
- // ];
- // }
- // }
- // $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]);
- // if (!$goods_book) {
- // $goods_book = new GoodsBook();
- // }
- // $goods_book->goods_id = $goods->id;
- // $goods_book->date_book = Json::encode($this->date_book);
- // $goods_book->save();
- }
- if ($this->product_type == Goods::GOODS_TYPE_TIME) {
- if (empty($this->service_book) || !is_array($this->service_book) || empty($this->service_book['data'])) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请检查服务商品预定参数'
- ];
- }
- // if (count($this->service_book['data']) != $this->service_book['date']) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间段和价格参数'
- // ];
- // }
- $service_book = $this->service_book;
- // foreach ($service_book[0]['time'] as $item) {
- // if (!$item['end_time'] || !$item['start_time']) {
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间开始结束日期'
- // ];
- // }
- // foreach ($item['times'] as $time) {
- // if (!$time['time'] || !$time['price'] || !$time['num']) {
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间段和价格参数'
- // ];
- // }
- // }
- // }
- //$time_arr = [];
- if (empty($service_book['data'][0]['time'])) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请检查是否少填写时间段'
- ];
- }
- $num = 0;
- foreach ($service_book['data'][0]['time'] as $v) {
- if (empty($v['start_time']) || empty($v['end_time'])) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请检查是否少填写时间段'
- ];
- }
- if (empty($v['times']) || !is_array($v['times'])) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请检查时间段下是否未选择详细时间'
- ];
- }
- foreach ($v['times'] as $m) {
- if (empty($m['time']) || empty($m['price'] || empty($m['num']))) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请检查是否少填写时间点, 价格以及库存'
- ];
- }
- $num += (int)$m['num'];
- }
- // $time_arr[] = [
- // 'start_time' => $v['start_time'],
- // 'end_time' => $v['end_time'],
- // ];
- }
- // foreach ($this->service_book['data'] as $val) {
- // if (empty($val['time'])) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间段'
- // ];
- // }
- // foreach ($val['time'] as $v) {
- // if (empty($v['start_time']) || empty($v['end_time'])) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间段'
- // ];
- // }
- // if (empty($v['times']) || !is_array($v['times'])) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查时间段下是否未选择详细时间'
- // ];
- // }
- // foreach ($v['times'] as $m) {
- // if (empty($m['time']) || empty($m['price'] || empty($m['num']))) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请检查是否少填写时间点, 价格以及库存'
- // ];
- // }
- // }
- // $time_arr[] = [
- // 'start_time' => $v['start_time'],
- // 'end_time' => $v['end_time'],
- // ];
- // }
- // }
- // if (!empty($time_arr)) {
- // if (!$this->compareDate($time_arr)) {
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => '请勿重复设置时间段'
- // ];
- // }
- // }
- $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]);
- if (!$goods_book) {
- $goods_book = new GoodsBook();
- $goods_book->goods_id = $goods->id;
- }
- $goods_book->service_book = Json::encode($this->service_book);
- $goods_book->save();
- }
- // 设置规格
- try {
- $this->setAttr($goods);
- } catch(\Exception $e){}
- if ($this->cat_id && count($this->cat_id)) {
- if ($goods->id > 0) {
- GoodsCat::deleteAll(['goods_id' => $goods->id]);
- }
- $goodsCatForm = new GoodsCat();
- foreach ($this->cat_id as $cat_id) {
- $goodsCat = clone $goodsCatForm;
- $goodsCat->goods_id = $goods->id;
- $goodsCat->cat_id = $cat_id;
- $goodsCat->store_id = $goods->store_id;
- if (!$goodsCat->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- }
- if ($this->goods_pic_list) {
- if ($goods->id > 0) {
- GoodsPic::deleteAll(['goods_id' => $goods->id]);
- }
- $goodsPicForm = new GoodsPic();
- foreach ($this->goods_pic_list as $pic) {
- $goodsPic = clone $goodsPicForm;
- $goodsPic->goods_id = $goods->id;
- $goodsPic->pic_url = $pic['pic_url'];
- if (!$goodsPic->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- }
- // 处理商品浏览权限数据
- // 删除关系表这个商品的所有数据
- GoodsBrowse::deleteAll(['goods_id' => $goods->id]);
- if (!empty($this->browse_type_info) && is_array($this->browse_type_info)) {
- if (!in_array($this->browse_type_info['type'], [0, 1])) {
- return [
- 'code'=>1,
- 'msg'=>'商品预览类型设置错误'
- ];
- }
- if ((int)$this->browse_type_info['type'] === 1 && !in_array(0,$this->browse_type_info['member_level'])) {
- $goodsBrowse = new GoodsBrowse();
- foreach($this->browse_type_info['member_level'] as $level){
- $goodsBrowseModel = clone $goodsBrowse;
- $goodsBrowseModel->goods_id = $goods->id;
- $goodsBrowseModel->level = $level;
- $goodsBrowseModel->save();
- }
- }
- }
- $t->commit();
- if ($goods->cloud_goods_id) {
- $cloud_is_update = Option::get('cloud_is_update', $goods->store_id, 'store')['value'];
- if ($cloud_is_update) {
- $this->id = $goods->cloud_goods_id;
- $this->store_id = $goods->store_id;
- $goodsInfo = $this->saveCloudGoods();
- if (!isset($goodsInfo['code'])) {
- $merchantForm = new MerchantForm();
- $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id']);
- }
- }
- }
- // 同步门店商品
- queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]), 0, 1);
- Goods::skuAttr($goods);
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- 'goods' => $goods,
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败' . json_encode($goods->errors, JSON_UNESCAPED_UNICODE)
- ];
- }
- }
-
- // 服务商品
- public function bookingGoodsSave($goods) {
- if ($this->product_type == Goods::GOODS_TYPE_TIME) {
- $bookingGoodsExtSave = BookingForm::bookingGoodsExtSave($goods->store_id, $goods->id, $this->booking_goods_cat_id);
- if($bookingGoodsExtSave['code'] != 0){
- return $bookingGoodsExtSave;
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- 'bookingGoodsExtSave' => $bookingGoodsExtSave,
- ];
- }
- }
- public function saveFood($params = [])
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $full_cut = [
- 'pieces' => 0,
- 'forehead' => 0
- ];
- if($this->pieces){
- $full_cut['pieces'] = $this->pieces;
- }
- if($this->forehead){
- $full_cut['forehead'] = $this->forehead;
- }
- $t = \Yii::$app->db->beginTransaction();
- $goods = $this->id ? Goods::findOne($this->id) ?: new Goods() : new Goods();
- $goods->store_id = $this->store_id;
- $params['mch'] && $goods->mch_id = $params['mch_id'];
- $goods->name = $this->name;
- $goods->key_word = $this->key_word;
- $goods->unit = $this->unit;
- $goods->price = $this->price;
- $goods->cost_price = $this->cost_price;
- $goods->original_price = $this->original_price;
- $goods->service = $this->service;
- $goods->weight = $this->weight;
- $goods->freight = $this->freight;
- $goods->pieces = $this->pieces;
- $goods->full_cut = Json::encode($full_cut);
- $goods->forehead = $this->forehead;
- $goods->quick_purchase = $this->quick_purchase;
- $goods->video_url = $this->video_url;
- $goods->detail = $this->content;
- $goods->goods_num = $this->goods_num;
- $goods->goods_no = $this->goods_no;
- $goods->integral = json_encode( $this->integral);
- $goods->use_attr = $this->use_attr;
- $goods->is_negotiable = $this->is_negotiable;
- $goods->cover_pic = $this->cover_pic;
- $goods->individual_share = $this->individual_share;
- $goods->share_commission_first = $this->share_commission_first ? $this->share_commission_first : 0;
- $goods->share_commission_second = $this->share_commission_second ? $this->share_commission_second : 0;
- $goods->share_commission_third = $this->share_commission_third ? $this->share_commission_third : 0;
- $goods->attr_setting_type = $this->attr_setting_type ? 1 : 0;
- $goods->virtual_sales = $this->virtual_sales;
- $goods->confine_count = $this->confine_count;
- isset($this->order_min_count) && $goods->order_min_count = $this->order_min_count;
- $goods->rate = $this->rate ? $this->rate : 0;
- $goods->rate_type = $this->rate_type == 1 ? 1 : 0;
- $goods->chain_rate = $this->chain_rate ? $this->chain_rate : 0;
- $goods->chain_rate_type = $this->chain_rate_type == 1 ? 1 : 0;
- $goods->is_level = $this->is_level;
- $goods->delivery_type = $this->delivery_type ? json_encode($this->delivery_type) : '["delivery","shop"]';
- $goods->product_type = 0;
- $goods->form_name = '';
- $goods->is_form = 0;
- $goods->food_ext_goods_id = $this->food_ext_goods_id ? : 0;
- $goods->cat_id = $this->cat_id;
- $goods->md_food_id = get_md_id();
- if ($this->goods_verify_card_list) {
- $verify_card_id = '';
- foreach($this->goods_verify_card_list as $val) {
- $verify_card_id .= $verify_card_id ? ','.$val['id'] : $val['id'];
- }
- $goods->verify_card_id = $verify_card_id;
- }
- if ($goods->save()) {
- // 设置规格
- try {
- $this->setAttr($goods);
- } catch(\Exception $e){}
- if ($this->goods_pic_list) {
- if ($goods->id > 0) {
- GoodsPic::deleteAll(['goods_id' => $goods->id]);
- }
- $goodsPicForm = new GoodsPic();
- foreach ($this->goods_pic_list as $pic) {
- $goodsPic = clone $goodsPicForm;
- $goodsPic->goods_id = $goods->id;
- $goodsPic->pic_url = $pic['pic_url'];
- if (!$goodsPic->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败'
- ];
- }
- }
- public function getEdit($id)
- {
- if ($id) {
- $goods = Goods::findOne($id);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '商品获取失败'
- ];
- }
- $erp_is_open = erp\InventoryForm::isOpen(get_store_id());
- Goods::skuAttr($goods);
- $getAttrGroupList = $goods->getAttrGroupList();
- $goods = $goods->toArray();
- $cat_id = GoodsCat::find()
- ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
- ->select(['cat_id'])->asArray()->all();
- $cat_id = array_column($cat_id, 'cat_id');
- $goods['cat_id'] = $cat_id;
- if ($goods['mch_id']) {
- $mchList = (new MchForm(['store_id' => get_store_id()]))->mchSelectList();
- $mchCommonCatSelectList = (new MchForm(['store_id' => get_store_id()]))->mchCommonCatSelectList();
- $goods['mch'] = $mchList['data'][$goods['mch_id']];
- $goods['mch_common_cat'] = $mchCommonCatSelectList['data'][$goods['mch']['mch_common_cat_id']];
- $mch_goods_cat = MchGoodsCat::find()
- ->where([ 'goods_id' => $goods['id'] ])
- ->select(['cat_id'])
- ->asArray()
- ->all();
- $mch_goods_cat_id = array_column($mch_goods_cat, 'cat_id');
- $goods['mch_cat_id'] = $mch_goods_cat_id;
- }
- $goods['integral'] = json_decode($goods['integral'],true);
- $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
- ->where(['goods_id' => $id])->asArray()->all();
- foreach ($goods as &$val) {
- $val = is_int($val) ? (string)$val : $val;
- }
- $goods['content'] = $goods['detail'];
- // 获取等级分润配置
- $gooodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $id]);
- $goods['is_open'] = '0';
- $goods['chain_type'] = '1';
- $goods['chain_level_value'] = [];
- if ($gooodsChainLevel) {
- $goods['is_open'] = (string)$gooodsChainLevel->is_open;
- $goods['chain_type'] = (string)$gooodsChainLevel->type;
- if ($chainLevelValue = json_decode($gooodsChainLevel->value)) {
- $goods['chain_level_value'] = $chainLevelValue;
- }
- }
- if (intval($goods['product_type']) === Goods::GOODS_TYPE_WORKER) {
- $worker_id = WorkerGoods::find()->where(['goods_id' => $id, 'status' => 1])->select('worker_id')->column();
- $goods['worker_ids'] = implode(',', $worker_id);
- $worker_goods_ext = WorkerGoodsExt::findOne(['goods_id' => $id]);
- $goods['desc'] = $worker_goods_ext->desc;
- $goods['warn'] = $worker_goods_ext->warn;
- $goods['worker_goods_cat_id'] = $worker_goods_ext->cat_id;
- $goods['bind_worker'] = $worker_goods_ext->bind_worker;
- $goods['final_profit'] = $worker_goods_ext->final_profit;
- $goods['payment_type'] = $worker_goods_ext->payment_type;
- }
- } else {
- $goods = null;
- $goods['is_open'] = '0';
- $goods['chain_level_value'] = [];
- }
- if (!empty($goods['buy_type_info'])) {
- $goods['buy_type_info'] = json_decode($goods['buy_type_info'],true);
- $goods['buy_type_info']['type'] = (int)$goods['buy_type_info']['type'];
- if (isset($goods['buy_type_info']['member_level']) && is_numeric($goods['buy_type_info']['member_level'])) {
- $goods['buy_type_info']['member_level'] = explode(',', $goods['buy_type_info']['member_level']);
- }
- } else {
- $goods['buy_type_info'] = [
- 'type' => 0,
- 'member_level' => ['-1']
- ];
- }
- // 获取商品浏览会员等级对应数据
- $goodsBrowse = GoodsBrowse::find()->where(['goods_id' => $id])->asArray()->select('level')->column();
- if(empty($goodsBrowse)){
- $goods['browse_type_info'] = ['type' => 0,'member_level' => [0]];
- }else{
- $goods['browse_type_info'] = ['type' => 1,'member_level' => $goodsBrowse];
- }
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
- $send_type = $send_type ? Json::decode($send_type['value']) : [];
- $send_type_arr = [];
- foreach ((array)$send_type as $key => $send) {
- if ($send['value'] == 1) {
- $send_type_arr[$key] = $send['text'];
- }
- }
- $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
- $goods['goods_delivery_type'] = !empty($goods['delivery_type']) ? Json::decode($goods['delivery_type']) : array_keys($arr);
- $goods['delivery_type'] = $arr;
- $goods['parameter_list'] = Json::decode($goods['parameter_list']);
- $goods['rate_type'] = (string)(int)$goods['rate_type'];
- $goods['chain_rate_type'] = (string)(int)$goods['chain_rate_type'];
- $goods['md_rate_type'] = (string)(int)$goods['md_rate_type'];
- $goods['md_hx_rate_type'] = (string)(int)$goods['md_hx_rate_type'];
- $goods['dl_p_rate_type'] = (string)(int)$goods['dl_p_rate_type'];
- $goods['dl_c_rate_type'] = (string)(int)$goods['dl_c_rate_type'];
- $goods['dl_d_rate_type'] = (string)(int)$goods['dl_d_rate_type'];
- $goods['dl_t_rate_type'] = (string)(int)$goods['dl_t_rate_type'];
- $goods['is_front_delivery'] = (int)$goods['is_front_delivery'];
- $goods['is_front_centralize'] = (int)$goods['is_front_centralize'];
- $goods['agent_goods_status'] = (int)$goods['agent_goods_status'];
- $goods['is_cloud_express'] = (int)$goods['is_cloud_express'];
- //获取配送规则
- $DeliveryRules = DeliveryRules::find()->where(['store_id' => get_store_id(), 'status' => 1, 'is_delete' => 0])->select('id, type, times, days')->asArray()->all();
- foreach ($DeliveryRules as &$deliveryRule) {
- if ((int)$deliveryRule['type'] === 0) {
- $deliveryRule['times'] = date("Y-m-d H:i:s", $deliveryRule['times']);
- }
- }
- // 获取运费规则
- $postage = PostageRules::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'mch_id' => 0
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- if ($goods['mch_id'] || $this->mch_id) {
- $postage = PostageRules::find()->where([
- 'store_id' => 0,
- 'is_delete' => 0,
- 'mch_id' => get_mch_id(),
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- }
- array_unshift($postage, ['id' => 0, 'name' => '--']);
- // 获取所有的会员等级
- $level = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'status' => 1
- ])->select(['level as id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
- //获取分销等级
- $share_level = ShareLevel::find()->where(['store_id' => get_store_id(), 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
- ->select('level id, name')->orderBy('level ASC')->asArray()->all();
- $share_level = array_merge([[
- 'id' => 0,
- 'name' => '默认等级'
- ]], $share_level);
- // 卡券
- $verify_list = VerifyCard::find()
- ->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0
- ])->orderBy(['sort' => SORT_ASC])
- ->select([
- '*'
- ])->asArray()
- ->all();
- $verify_list = Tools::getVerifyList($verify_list);
- $coupon_list = Coupon::find()
- ->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'is_business' => 0,
- ])->andWhere(['<>', 'total_count', 0])
- ->orderBy(['sort' => SORT_ASC])
- ->asArray()
- ->all();
- foreach ($coupon_list as $coupon_index => $coupon_item) {
- $coupon_send_count = UserCoupon::find()->where([
- 'coupon_id' => $coupon_item['id'],
- 'type' => [
- UserCoupon::TYPE_STORE,
- UserCoupon::TYPE_AUTO,
- UserCoupon::TYPE_GET
- ]
- ])->select('id')->count();
- if (intval($coupon_item['total_count']) !== -1 && $coupon_item['total_count'] <= $coupon_send_count) {
- unset($coupon_list[$coupon_index]);
- }
- }
- $coupon_list = array_values($coupon_list);
- $goods_verify_card_list = [];
- // 获取商品已添加核销卡
- if (!empty($goods) && !empty($goods['verify_card_id'])) {
- $verify_cards = explode(',', $goods['verify_card_id']);
- foreach ($verify_cards as $k => $v) {
- $res = VerifyCard::find()
- ->select(['id','name'])
- ->where(['id' => $v,'is_delete' => 0])
- ->asArray()->one();
- if ($res) {
- $goods_verify_card_list[] = $res;
- }
- }
- }
- $goods_coupon_card_list = [];
- // 获取商品已添加核销卡
- if (!empty($goods) && !empty($goods['coupon_card_id'])) {
- $verify_cards = explode(',', $goods['coupon_card_id']);
- foreach ($verify_cards as $k => $v) {
- $res = Coupon::find()
- ->select(['id','name'])
- ->where(['id' => $v,'is_delete' => 0])
- ->asArray()->one();
- if ($res) {
- $goods_coupon_card_list[] = $res;
- }
- }
- }
- // 服务商品预约
- $goods['service_book'] = [
- 'date' => 0,
- 'data' => [],
- 'flag' => 1
- ];
- $goods['date_book'] = [
- 'data' => [],
- 'date' => 30
- ];
- $goods['form'] = [
- 'form_list' => [],
- 'is_form' => 0,
- 'form_name' => ''
- ];
- if (isset($goods['id'])) {
- $goods_book_config = GoodsBook::findOne(['goods_id' => $goods['id']]);
- if ($goods_book_config) {
- $service_book = $goods_book_config->service_book;
- $date_book = $goods_book_config->date_book;
- if ($service_book) {
- $goods['service_book'] = Json::decode($service_book);
- }
- if ($date_book) {
- $goods['date_book']['data'] = Json::decode($date_book);
- $goods['date_book']['date'] = count($goods['date_book']['data']);
- }
- }
- $goods['form']['form_list'] = Form::find()->where(['is_delete' => 0, 'store_id' => $this->store_id, 'goods_id' => $goods['id']])->orderBy(['sort' => SORT_ASC])->asArray()->all();
- $goods['form']['is_form'] = intval($goods['is_form']);
- $goods['form']['form_name'] = $goods['form_name'];
- }
- $nums = [];
- $endtime = mktime(0, 0, 0, date('m') + 2, 1);
- for($i = time(); $i < $endtime; $i+=86400){
- $date = date('Y-m-d', $i);
- $nums[] = [
- 'date' => $date,
- 'num' => '0',
- 'price' => '0.00'
- ];
- }
- $goods['service_book_desc'] = [
- 'date_book' => $nums ,
- 'date' => count($nums)
- ];
- if ($goods['product_type'] == Goods::GOODS_TYPE_DATE) {
- $goods_info = Goods::findOne($goods['id']);
- $goods['num'] = Goods::getGoodsNum($goods_info)['data'];
- $goods['service_book_desc'] = [
- 'date_book' => $goods['num'] ,
- 'date' => count($goods['num'])
- ];
- }
- $goods['share_commission_new_first'] = $goods['share_commission_new_first'] ? json_decode($goods['share_commission_new_first'], true) : null;
- $goods['share_commission_new_second'] = $goods['share_commission_new_second'] ? json_decode($goods['share_commission_new_second'], true) : null;
- $goods['share_commission_new_third'] = $goods['share_commission_new_third'] ? json_decode($goods['share_commission_new_third'], true) : null;
- $order_rules_buyer_location = \app\models\OrderRulesBuyerLocation::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- // 获取等级
- $shareHolderLevel = ShareHolderLevel::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'status' => 1
- ])->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
-
- if($goods['product_type'] == 2){
- $bookingGoodsExt = \app\models\BookingGoodsExt::findOne(['goods_id' => $goods['id']]);
- }
- $goods['share_holder_profit_switch'] = (int)$goods['share_holder_profit_switch'];
- $goods['share_holder_high_profit'] = (float)$goods['share_holder_high_profit'];
- $warehouseList = Warehouse::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- ])->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
- $warehouse_zone_list = WarehouseZone::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- ])->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
- $is_show_risk = Option::get(OptionSetting::IS_SHOW_RISK, $this->store_id, 'store', '')['value'];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'erp_is_open' => $erp_is_open,
- 'warehouse_list' => $warehouseList,
- 'warehouse_zone_list' => $warehouse_zone_list,
- 'bookingGoodsExt' => $bookingGoodsExt,
- 'info' => $goods,
- 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [],
- 'postage' => $postage,
- 'order_rules_buyer_location' => $order_rules_buyer_location,
- 'level' => $level,
- 'verify_list' => $verify_list,
- 'coupon_list' => $coupon_list,
- 'goods_verify_card_list' => $goods_verify_card_list,
- 'goods_coupon_card_list' => $goods_coupon_card_list,
- 'delivery_rules' => $DeliveryRules,
- 'shareHolderLevel' => $shareHolderLevel,
- 'share_level' => $share_level,
- 'is_show_risk' => intval($is_show_risk)
- ]
- ];
- }
- public function getFoodEdit($id)
- {
- if ($id) {
- $goods = Goods::findOne($id);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '商品获取失败'
- ];
- }
- $getAttrGroupList = $goods->getAttrGroupList();
- $goods = $goods->toArray();
- $goods['integral'] = json_decode($goods['integral'],true);
- $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
- ->where(['goods_id' => $id])->asArray()->all();
- foreach ($goods as &$val) {
- $val = is_int($val) ? (string)$val : $val;
- }
- $goods['content'] = $goods['detail'];
- } else {
- $goods = null;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'info' => $goods,
- 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [],
- ]
- ];
- }
- /**
- * @param Goods $goods
- */
- public function setAttr($goods, $rate = 0, $channel_rate = 0, $num_type = 0, $original_price = 0)
- {
- if ($goods->product_type == Goods::GOODS_TYPE_TIME) {
- $defaultAttr = '[{"attr_list":[{"attr_id":1,"attr_name":"默认"}],"num":0,"price":0,"integral_price":0,"no":"","pic":"","share_commission_first":"","share_commission_second":"","share_commission_third":""}]';
- $goods->attr = $defaultAttr;
- $goods->save();
- return;
- }
- if (!$this->use_attr) {
- [$default_attr, $default_attr_group] = $this->getDefaultAttr();
- if (empty($this->attr)) {
- $this->attr = [
- [
- 'attr_list' => [
- [
- 'attr_group_name' => $default_attr_group->attr_group_name,
- 'attr_id' => $default_attr->id,
- 'attr_name' => $default_attr->attr_name,
- ],
- ],
- 'num' => intval($this->goods_num) ?: 0,
- 'price' => $this->price,
- 'integral_price' => $this->integral_price,
- 'cost_price' => $this->cost_price,
- 'no' => $this->goods_no,
- 'is_wholesale' => (int)$this->is_wholesale,
- 'wholesale_num' => intval($this->wholesale_num) ?: 0,
- 'sku_id' => '',
- 'original_price' => $original_price ?: $goods->original_price,
- ],
- ];
- } else {
- $attr_list = [
- [
- 'attr_group_name' => $default_attr_group->attr_group_name,
- 'attr_id' => $default_attr->id,
- 'attr_name' => $default_attr->attr_name,
- ],
- ];
- $this->attr[0]['attr_list'] = $attr_list;
- if (!empty($this->is_wholesale)) {
- $this->attr[0]['num'] = intval($this->goods_num) ?: ($this->attr[0]['num'] ?: 0);
- } else {
- $this->attr[0]['num'] = intval($this->goods_num);
- }
-
- if (!$num_type) {
-
- if (!empty($this->is_wholesale)) {
- $this->attr[0]['price'] = doubleval($this->price) ?: ($this->attr[0]['price'] ?: 0);
- } else {
- $this->attr[0]['price'] = doubleval($this->price);
- }
-
- $this->attr[0]['integral_price'] = intval($this->integral_price) ?: 0;
- $this->attr[0]['cost_price'] = doubleval($this->cost_price) ?: 0;
- }
- $this->attr[0]['no'] = $this->attr[0]['no'] ?: $this->goods_no;
- $this->attr[0]['is_wholesale'] = (int)$this->is_wholesale;
- $this->attr[0]['wholesale_num'] = (int)$this->wholesale_num ?: 0;
- $this->attr[0]['sku_id'] = '';
- $this->attr[0]['original_price'] = doubleval($original_price ?: $goods->original_price) ?: 0;
- $this->attr = [
- $this->attr[0]
- ];
- }
- }
- if (empty($this->attr) || !is_array($this->attr)) {
- return;
- }
- $new_attr = [];
- $goods_attr = json_decode($goods->attr, true);
- foreach ($this->attr as $i => $item) {
- // if ($item['original_price'] > 0) {
- // $attr_price = $item['original_price'];
- // } else {
- // }
- $cost_price = $item['cost_price'];
- $attr_price = $item['price'];
- if (!$num_type || !$goods_attr) {
- $attr_price = (int)$rate > 0 && ((int)$this->use_attr === 1) ? doubleval($item['price']) + (doubleval($item['price']) * ($rate / 100)) : doubleval($item['price']);
- $sale_price_type = Option::get('sale_price_type', $this->store_id, 'store')['value'];
- if ($sale_price_type <= 0) {
- if ((int)$rate > 0) {
- $price = !empty($item['platform_negotiated_price']) ? $item['platform_negotiated_price'] : $item['price'];
- $attr_price = $price + $price * ($rate / 100);
- } else {
- $attr_price = $item['price'];
- }
- } else {
- $attr_price = $item['original_price'] ?: $item['price'];
- }
- if (isset($item['platform_negotiated_price'])) {
- $cost_price = $item['platform_negotiated_price'];
- if ($channel_rate > 0) {
- $cost_price = bcmul($item['platform_negotiated_price'], $channel_rate, 2);
- }
- }
- } else {
- $currentAttrIds = array_column($item, 'attr_id');
- sort($currentAttrIds);
- foreach ($goods_attr as $goods_attr_item) {
- // $goodsAttrIds = array_column($goods_attr_item, 'attr_id');
- // sort($goodsAttrIds);
- if ($item['no'] == $goods_attr_item['no']) {
- $attr_price = $goods_attr_item['price'];
- }
- }
- }
- //云仓商品更新库存:仓库配送产品更新库存时候计算方式:仓库库存
- //如果是仓库配送商品
- if (intval($goods->is_front_delivery)) {
- $curr_attr_id = array_column($item['attr_list'], 'attr_id');
- sort($curr_attr_id);
- $inventory_array = AgentFrontErpInventory::find()->where(['goods_id' => $goods->cloud_goods_id])
- ->asArray()->all();
- if ($inventory_array) {
- foreach ($inventory_array as $inventory_item) {
- $inventory_attr_ids = explode(',', $inventory_item['attr_ids']);
- sort($inventory_attr_ids);
- if (!array_diff($inventory_attr_ids, $curr_attr_id)) {
- $item['num'] = $inventory_item['num'];
- }
- }
- }
- }
- $new_attr_item = [
- 'attr_list' => [],
- 'num' => intval($item['num']),
- 'price' => sprintf('%.2f', $attr_price),
- 'integral_price' => intval($item['integral_price']),
- 'no' => $item['no'] ?: '',
- 'pic' => $item['pic'] ?: '',
- // 'share_commission_first' => $item['share_commission_first'] ?: '',
- // 'share_commission_second' => $item['share_commission_second'] ?: '',
- // 'share_commission_third' => $item['share_commission_third'] ?: '',
- 'is_wholesale' => (int)$this->is_wholesale ?: 0,
- 'wholesale_num' => (int)$item['num'] ?: 0,
- 'cost_price' => sprintf('%.2f', $cost_price ?? $item['price']),
- 'sku_id' => $item['sku_id'],
- 'original_price' => $item['original_price'] ?? sprintf('%.2f', $attr_price)
- ];
- $individual_share = $goods->individual_share;
- $attr_setting_type = $goods->attr_setting_type;
- if (intval($individual_share) === 1 && intval($attr_setting_type) === 1) {
- $share_level = ShareLevel::find()->where(['store_id' => get_store_id(), 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
- ->select('level id, name')->orderBy('level ASC')->asArray()->all();
- $share_level = array_merge([[
- 'id' => 0,
- 'name' => '默认等级'
- ]], $share_level);
- $share_commission_level = [];
- // foreach ($share_level as $share_level_index => $share_level_) {
- //新一级分销
- // if (!isset($item['share_commission_new_first']) || ($item['share_commission_new_first'])) {
- foreach ($share_level as $share_level_index => $share_level_) {
- if (!isset($item['share_commission_new_first'][$share_level_index])) {
- $item['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_first'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_first'] as $share_commission_new_index => $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_first'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- if (!isset($item['share_commission_new_second'][$share_level_index])) {
- $item['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_second'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_second'] as $share_commission_new_index => $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_second'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- if (!isset($item['share_commission_new_third'][$share_level_index])) {
- $item['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_third'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_third'] as $share_commission_new_index => $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_third'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- }
- // }
- // }
- $share_commission_level['share_commission_new_first'] = $item['share_commission_new_first'];
- $share_commission_level['share_commission_new_second'] = $item['share_commission_new_second'];
- $share_commission_level['share_commission_new_third'] = $item['share_commission_new_third'];
- $new_attr_item = array_merge($new_attr_item, $share_commission_level);
- }
- foreach ($item as $k => $v) {
- if (strpos($k,'member') !== false) {
- $new_attr_item[$k] = $v;
- }
- }
- foreach ($item['attr_list'] as $a) {
- $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
- if (!$attr_group_model) {
- $attr_group_model = new AttrGroup();
- $attr_group_model->attr_group_name = $a['attr_group_name'];
- $attr_group_model->store_id = $this->store_id;
- $attr_group_model->is_delete = 0;
- $attr_group_model->save();
- }
- $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
- if (!$attr_model) {
- $attr_model = new Attr();
- $attr_model->attr_name = $a['attr_name'];
- $attr_model->attr_group_id = $attr_group_model->id;
- $attr_model->is_delete = 0;
- $attr_model->save();
- }
- $new_attr_item['attr_list'][] = [
- 'attr_id' => $attr_model->id,
- 'attr_name' => $attr_model->attr_name,
- ];
- }
- //如果未更改售价就使用原来的价格
- if ($goods->attr && $num_type) {
- $new_attr_item_id = array_column($new_attr_item['attr_list'], 'attr_id');
- sort($new_attr_item_id);
- $old_goods_attr = json_decode($goods->attr, true);
- foreach ($old_goods_attr as $old_goods_attr_item) {
- $old_goods_attr_id = array_column($old_goods_attr_item['attr_list'], 'attr_id');
- sort($old_goods_attr_id);
- if (!array_diff($new_attr_item_id, $old_goods_attr_id)) {
- $member_arr = [];
- foreach ($old_goods_attr_item as $old_goods_attr_item_index => $old_goods_attr_item_item) {
- if (strpos($old_goods_attr_item_index, 'member') !== false) {
- $member_arr[$old_goods_attr_item_index] = $old_goods_attr_item_item;
- }
- }
- $new_attr_item = array_merge($new_attr_item, $member_arr);
- }
- }
- }
- $new_attr[] = $new_attr_item;
- }
- $goods->attr = Json::encode($new_attr);
- $goods->save();
- return;
- }
- /**
- * @return array
- */
- private function getDefaultAttr()
- {
- $default_attr_name = '默认';
- $default_attr_group_name = '规格';
- $attr = Attr::findOne([
- 'attr_name' => $default_attr_name,
- 'is_delete' => 0,
- 'is_default' => 1,
- ]);
- $attr_group = null;
- if (!$attr) {
- $attr_group = AttrGroup::findOne([
- 'attr_group_name' => $default_attr_group_name,
- 'is_delete' => 0,
- ]);
- if (!$attr_group) {
- $attr_group = new AttrGroup();
- $attr_group->store_id = $this->store_id;
- $attr_group->attr_group_name = $default_attr_group_name;
- $attr_group->is_delete = 0;
- $attr_group->save(false);
- }
- $attr = new Attr();
- $attr->attr_group_id = $attr_group->id;
- $attr->attr_name = $default_attr_name;
- $attr->is_delete = 0;
- $attr->is_default = 1;
- $attr->save(false);
- } else {
- $attr_group = AttrGroup::findOne($attr->attr_group_id);
- if (!$attr_group) {
- $attr_group = AttrGroup::findOne([
- 'attr_group_name' => $default_attr_group_name,
- 'is_delete' => 0,
- ]);
- if (!$attr_group) {
- $attr_group = new AttrGroup();
- $attr_group->store_id = $this->store_id;
- $attr_group->attr_group_name = $default_attr_group_name;
- $attr_group->is_delete = 0;
- $attr_group->save(false);
- }
- $attr->attr_group_id = $attr_group->id;
- $attr->save();
- }
- }
- return [$attr, $attr_group];
- }
- public function setAttrNum($data = [])
- {
- $data = $data ? $data : post_params();
- $goods = Goods::findOne($data['id']);
- $arrt = Json::encode($data['attr']);
- if ($goods) {
- $goods->attr = $arrt;
- $goods_num = 0;
- foreach($data['attr'] as $item) {
- $goods_num += $item['num'];
- }
- $goods->goods_num = $goods_num;
- // if ($goods->mch_id > 0) {
- // $mch = Mch::findOne($goods->mch_id);
- // if ($mch->is_goods_ex == 1) {
- // $goods->status = 0;
- // $goods->is_verify = -1;
- // }
- // }
- $goods->is_risk = $data['is_risk'];
- $res = ['code' => 1, 'msg' => '保存失败'];
- if ($goods->save()) {
- // 同步门店商品
- queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]), 0, 1);
- $res = ['code' => 0, 'msg' => '保存成功'];
- }
- } else {
- $res = ['code' => 1, 'msg' => '参数错误'];
- }
- return $res;
- }
- public function updateAttrNum()
- {
- $data = post_params();
- $goods = Goods::findOne($data['id']);
- $arrt = Json::encode($data['attr']);
- if ($goods) {
- $goods->attr = $arrt;
- $goods_num = 0;
- foreach($data['attr'] as $item) {
- $goods_num += $item['num'];
- }
- $goods->goods_num = $goods_num;
- // if ($goods->mch_id > 0) {
- // $mch = Mch::findOne($goods->mch_id);
- // if ($mch->is_goods_ex == 1) {
- // $goods->status = 0;
- // $goods->is_verify = -1;
- // }
- // }
- $res = ['code' => 1, 'msg' => '保存失败'];
- if ($goods->save()) {
- // 同步门店商品
- queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]), 0, 1);
- $res = ['code' => 0, 'msg' => '保存成功'];
- }
- } else {
- $res = ['code' => 1, 'msg' => '参数错误'];
- }
- return $res;
- }
- // 修改商品属性
- public function setType($goods_id = 0, $type = '', $value = '')
- {
- $type = $type ? $type : get_params('type');
- $goods_id = $goods_id ? $goods_id : get_params('id');
- $value = $value ? $value : get_params('value', 0);
- if (!$type || !$goods_id) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $goods = Goods::findOne([
- 'id' => $goods_id,
- // 'mch_id' => get_mch_id(),
- // 'store_id' => get_store_id()
- ]);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '查询商品为空'
- ];
- }
- if ($type == 'price' && $goods->use_attr == 1) {
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- }
- $goods->$type = $value;
- if($type == 'price') {
- $goods->is_risk = get_params('is_risk');
- }
- if ($type == 'goods_num' && $goods->use_attr == 0) {
- $attr = Json::decode($goods->attr);
- $attr[0]['num'] = $value;
- $goods->attr = Json::encode($attr);
- }
- if ($type == 'price' && $goods->use_attr == 0) {
- $attr = Json::decode($goods->attr);
- $attr[0]['price'] = doubleval($value);
- $goods->attr = Json::encode($attr);
- }
- // if ($goods->mch_id > 0) {
- // $mch = Mch::findOne($goods->mch_id);
- // if ($mch->is_goods_ex == 1) {
- // $goods->status = 0;
- // $goods->is_verify = -1;
- // }
- // }
- if ($goods->save()) {
- if ($goods->cloud_goods_id) {
- $cloud_is_update = Option::get('cloud_is_update', $goods->store_id, 'store')['value'];
- if ($cloud_is_update) {
- $this->id = $goods->cloud_goods_id;
- $this->store_id = $goods->store_id;
- $goodsInfo = $this->saveCloudGoods();
- if (!isset($goodsInfo['code'])) {
- $merchantForm = new MerchantForm();
- $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id']);
- }
- }
- }
- // 同步门店商品
- queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]), 0, 1);
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- public function upPriceBatch($goods_id = [], $num = 0, $type) {
- foreach($goods_id as $gid){
- $goods = Goods::findOne($gid);
- if ($type == 'add') {
- $goods->price += $goods->price * ($num / 100);
- $goods->original_price += $goods->original_price * ($num / 100);
- } else {
- $goods->price -= $goods->price * ($num / 100);
- $goods->original_price -= $goods->original_price * ($num / 100);
- }
-
- $res = $this->setType($goods->id, 'price', $goods->price);
- if($res['code'] != 0){
- return $res;
- }
- $res = $this->setType($goods->id, 'original_price', $goods->original_price);
- if($res['code'] != 0){
- return $res;
- }
- $attr = json_decode($goods->attr, true);
- foreach($attr as &$iattr){
- if ($type == 'add') {
- $iattr['price'] += $iattr['price'] * ($num / 100);
- } else {
- $iattr['price'] -= $iattr['price'] * ($num / 100);
- }
- $iattr['price'] = number_format($iattr['price'], 2, '.', '');
- }
- $res = $this->setAttrNum([
- 'id' => $goods->id,
- 'attr' => $attr,
- ]);
- if($res['code'] != 0){
- return $res;
- }
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- }
- // 修改商品属性
- public function updateAttribute()
- {
- $type = post_params('type');
- $goods_id = post_params('id');
- $value = post_params('value', 0);
- if (!$type || !$goods_id) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $goods = Goods::findOne([
- 'id' => $goods_id,
- 'store_id' => get_store_id()
- ]);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '查询商品为空'
- ];
- }
- $goods->$type = $value;
- if ($type == 'goods_num' && $goods->use_attr == 0) {
- $attr = Json::decode($goods->attr);
- $attr[0]['num'] = $value;
- $goods->attr = Json::encode($attr);
- }
- if ($type == 'price' && $goods->use_attr == 0) {
- $attr = Json::decode($goods->attr);
- $attr[0]['price'] = doubleval($value);
- $goods->attr = Json::encode($attr);
- }
- // if ($goods->mch_id > 0) {
- // $mch = Mch::findOne($goods->mch_id);
- // if ($mch->is_goods_ex == 1) {
- // $goods->status = 0;
- // $goods->is_verify = -1;
- // }
- // }
- if ($goods->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-18
- * @desc: 根据id获取商品列表
- * @param array $id
- * @return void
- */
- public static function getGoodsListById ($id = [])
- {
- if (empty($id) && !is_array($id)) {
- return [];
- }
- return Goods::find()->where(['in', 'id', $id])->select('id, name, cover_pic')->asArray()->all();
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-03-02
- * @desc: 根据商品id获取规格属性列表
- * @param [type] $id
- * @return void
- */
- public static function getAttrByGoodsId ($id)
- {
- $goods = Goods::findOne(['id' => $id, 'store_id' => get_store_id(), 'mch_id' => get_mch_id()]);
- if (!$goods->attr) {
- return [];
- }
- $attr_data = json_decode($goods->attr, true);
- foreach ($attr_data as $i => $attr_data_item) {
- if (!isset($attr_data[$i]['no'])) {
- $attr_data[$i]['no'] = '';
- }
- if (!isset($attr_data[$i]['pic'])) {
- $attr_data[$i]['pic'] = '';
- }
- foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
- $attr_group = $goods->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
- $t = $attr_data[$i]['attr_list'][$j]['attr_name'];
- unset($attr_data[$i]['attr_list'][$j]['attr_name']);
- $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- $attr_data[$i]['attr_list'][$j]['attr_name'] = $t;
- }
- }
- return $attr_data;
- }
- private function compareDate($arr): bool
- {
- array_multisort(array_column($arr, 'start_time'), SORT_ASC, $arr);
- foreach ($arr as $k => $v){
- if ($v['start_time'] >= $v['end_time'])
- return false;
- if ($k > 0 && $arr[$k]['start_time'] < $arr[$k-1]['end_time'])
- return false;
- }
- return true;
- }
- 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;
- }
- /**
- * 模块名:checkRisk
- * 代码描述:商品风险检测
- * 作者:WPing丶
- * 请求方式:GET
- * 创建时间:2024/03/18 17:42:46
- * @param int id
- * @param string str
- * @param bool bool
- */
- public function checkRisk() {
- foreach($this->attr as $attr_item) {
- $risk_array = $this->risk($attr_item, true);
- $price = $this->use_attr == 1 ? $attr_item['price'] : $this->price;
- if($this->is_level) {
- $level_list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])
- ->select('level')->column();
- foreach($level_list as $level) {
- if($attr_item['member'.$level] > 0 && $attr_item['member'.$level] < $price) {
- $price = $attr_item['member'.$level];
- }
- }
- }
- $cost_price = $this->use_attr == 1 ? $attr_item['cost_price'] : $this->cost_price;
- $profit = bcsub($price,$cost_price,2);
- $end_str = '(售价'.$price.')-('.'成本价'.$cost_price;
- foreach($risk_array as $key => $item) {
- switch ($key) {
- case 'share_first_price': //一级分销
- $str = $item > 0 ? ')-(一级分销'.$item : '';
- break;
- case 'share_second_price'://二级分销
- $str = $item > 0 ? ')-(二级分销'.$item : '';
- break;
- case 'share_third_price'://三级分销
- $str = $item > 0 ? ')-(三级分销'.$item : '';
- break;
- case 'dl_p_price'://省级代理
- $str = $item > 0 ? ')-(省级代理'.$item : '';
- break;
- case 'dl_c_price'://市级代理
- $str = $item > 0 ? ')-(市级代理'.$item : '';
- break;
- case 'dl_d_price'://区级代理
- $str = $item > 0 ? ')-(区级代理'.$item : '';
- break;
- case 'md_price'://门店佣金
- $str = $item > 0 ? ')-(门店佣金'.$item : '';
- break;
- case 'direct'://直推佣金
- $str = $item > 0 ? ')-(直推佣金'.$item : '';
- break;
- case 'indirect'://间推佣金
- $str = $item > 0 ? ')-(间推佣金'.$item : '';
- break;
- case 'max_discount_price'://总佣金
- $max_discount_price = $item;
- break;
- }
- if($key != 'max_discount_price') {
- $end_str .= $str;
- }
- }
- $end_str.= ')='.bcsub($profit,$max_discount_price,2);
- $risk_array['price'] = $price;
- $risk_array['real_profit'] = $risk_array['is_show_risk'] ? bcsub($profit,$max_discount_price,2) : 0;
- $risk_array['cost_price'] = $cost_price;
- // $risk_array['is_open_share'] = false;
- // $shareOption = Option::get(
- // \app\constants\OptionSetting::SHARE_MONEY_SETTING,
- // $this->store_id,
- // \app\constants\OptionSetting::SHARE_GROUP_NAME,
- // json_encode(['commission_type' => []])
- // );
- // $shareOption = \json_decode($shareOption['value'], true);
- // if (in_array(1, ($shareOption['commission_type'] ?: []))) {
- $risk_array['is_open_share'] = true;
- // }
- if($profit < $max_discount_price) {
- return $risk_array;
- return $end_str;
- }
- }
- return $risk_array;
- return $end_str;
- }
- /**
- * 模块名: risk
- * 代码描述:风控检测
- * 作者:WPing丶
- * 创建时间:2024/03/15 11:03:54
- * @param int attr_item 规格
- * @param bool is_array 是否需要返回详细数组
- */
- public function risk($attr_item, $is_array = false) {
- //商品金额
- $price = $this->use_attr == 1 ? $attr_item['price'] : $this->price;;//默认商品金额为售价
- //判断用户有没有开启会员价
- if($this->is_level) {
- $level_list = Level::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1])
- ->select('level')->column();
- foreach($level_list as $level) {
- if($attr_item['member'.$level] > 0 && $attr_item['member'.$level] < $price) {
- $price = $attr_item['member'.$level];
- }
- }
- }
- //计算最大支出
- $max_discount_price = 0;//固定金额
- /* 股东分红 begin */
- if($this->is_open == 1) {
- //股东分红基础金额
- if($this->rate_type == 0) {//比例
- $share_holder_price = bcmul($this->price,bcdiv($this->rate,100,4),2);
- } else {//固定金额
- $share_holder_price = $this->rate;
- }
- $direct = 0;
- $indirect = 0;
- $k = 0;//自建一个foreach索引值,因为$this->chain_level_value不是索引数组,他的key值是会员等级
- foreach($this->chain_level_value as $level => $share) {
- $max_direct = $share[0] > $max_direct ? $share[0] : $max_direct;//判断最大的直推金额
- if($this->chain_type == 1) {//固定金额
- $max_discount_price += $share[1];//间推
- $indirect += $share[1];
- //判断是不是最后一个股东等级,如果是加上直推金额(直推只发一次,间推无限发放)
- if($k+1 == count($this->chain_level_value)) {
- $max_discount_price += $share[0];//直推
- $direct += $share[0];
- }
- } else {//比例
- $max_discount_price += bcmul($share_holder_price, bcdiv($share[1],100,4), 2);//间推
- $indirect += bcmul($share_holder_price, bcdiv($share[1],100,4), 2);
- //判断是不是最后一个股东等级,如果是加上直推金额(直推只发一次,间推无限发放)
- if($k+1 == count($this->chain_level_value)) {
- $max_discount_price += bcmul($share_holder_price, bcdiv($share[0],100,4), 2);//直推
- $direct += bcmul($share_holder_price, bcdiv($share[0],100,4), 2);
- }
- }
- $k++;
- }
- }
- /* 股东分红 end */
- /* 门店佣金 begin */
- if($this->md_hx_rate > 0 || $this->md_rate > 0) {
- if($this->md_hx_rate_type == 0) {
- $md_hx_rate = bcmul($price,bcdiv($this->md_hx_rate,100,4),2);
- } else {
- $md_hx_rate = $this->md_hx_rate;
- }
- if($this->md_rate_type == 0) {
- $md_rate = bcmul($price,bcdiv($this->md_rate,100,4),2);
- } else {
- $md_rate = $this->md_rate;
- }
- $md_price = $md_hx_rate >= $md_rate ? $md_hx_rate : $md_rate;
- $max_discount_price += $md_price;
- }
- /* 门店佣金 end */
- /* 区域代理佣金 begin */
- if ($this->dl_p_rate > 0) {//省代
- if($this->dl_p_rate_type == 0) {
- $dl_p_price = bcmul($price,bcdiv($this->dl_p_rate,100,4),2);
- } else {
- $dl_p_price = $this->dl_p_rate;
- }
- $max_discount_price += $dl_p_price;
- }
- if ($this->dl_c_rate > 0) {//市代
- if($this->dl_c_rate_type == 0) {
- $dl_c_price = bcmul($price,bcdiv($this->dl_c_rate,100,4),2);
- } else {
- $dl_c_price = $this->dl_c_rate;
- }
- $max_discount_price += $dl_c_price;
- }
- if ($this->dl_d_rate > 0) {//区代
- if($this->dl_d_rate_type == 0) {
- $dl_d_price = bcmul($price,bcdiv($this->dl_d_rate,100,4),2);
- } else {
- $dl_d_price = $this->dl_d_rate;
- }
- $max_discount_price += $dl_d_price;
- }
- /* 区域代理佣金 end */
- /* 分销佣金 begin */
- if($this->individual_share == 1) {//商品独立分销
- if($this->attr_setting_type == 0) {//普通设置
- $share_first = max(json_decode($this->share_commission_new_first, true));
- $share_second = max(json_decode($this->share_commission_new_second, true));
- $share_third = max(json_decode($this->share_commission_new_third, true));
- } else {//详细设置
- $share_first = $attr_item['share_commission_new_first'][0]['value'];
- $share_second = $attr_item['share_commission_new_second'][0]['value'];
- $share_third = $attr_item['share_commission_new_third'][0]['value'];
- }
- if($share_first > 0) {//一级分销
- if($this->share_type == 0) {
- $share_first_price = bcmul($price,bcdiv($share_first,100,4),2);
- } else {
- $share_first_price = $share_first;
- }
- $max_discount_price += $share_first_price;
- }
- if($share_second > 0) {//二级分销
- if($this->share_type == 0) {
- $share_second_price = bcmul($price,bcdiv($share_second,100,4),2);
- } else {
- $share_second_price = $share_second;
- }
- $max_discount_price += $share_second_price;
- }
- if($share_third > 0) {//三级分销
- if($this->share_type == 0) {
- $share_third_price = bcmul($price,bcdiv($share_third,100,4),2);
- } else {
- $share_third_price = $share_third;
- }
- $max_discount_price += $share_third_price;
- }
- } else {//默认分销
- $share_basic_setting = Option::get('share_money_setting', $this->store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
- $share_basic_setting = json_decode($share_basic_setting, true);
- $share_first = $share_basic_setting['level_one']['value'];
- if($share_first > 0) {//一级分销
- $share_first_price = bcmul($price,bcdiv($share_first,100,4),2);
- $max_discount_price += $share_first_price;
- }
- $share_second = $share_basic_setting['level_two']['value'];
- if($share_second > 0) {//二级分销
- $share_second_price = bcmul($price,bcdiv($share_second,100,4),2);
- $max_discount_price += $share_second_price;
- }
- $share_third = $share_basic_setting['level_three']['value'];
- if($share_third > 0) {//三级分销
- $share_third_price = bcmul($price,bcdiv($share_third,100,4),2);
- $max_discount_price += $share_third_price;
- }
- }
- /* 分销佣金 end */
- if($is_array) {
- $is_show_risk = Option::get(OptionSetting::IS_SHOW_RISK, $this->store_id, 'store', '')['value'];
- return [
- 'share_first_price' => round($share_first_price, 2),//一级分销
- 'share_second_price' => round($share_second_price, 2),//二级分销
- 'share_third_price' => round($share_third_price, 2),//三级分销
- 'dl_p_price' => round($dl_p_price, 2),//省级代理
- 'dl_c_price' => round($dl_c_price, 2),//市级代理
- 'dl_d_price' => round($dl_d_price, 2),//区级代理
- 'md_price' => round($md_price, 2),//门店佣金
- 'direct' => round($direct, 2),//直推佣金
- 'indirect' => round($indirect, 2),//间推佣金
- 'max_discount_price' => round($max_discount_price, 2),//总支出
- 'is_show_risk' => intval($is_show_risk)
- ];
- }
- return round($max_discount_price, 2);
- }
- }
|