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); } }