TimestampBehavior::class, 'value' => time() ] ]; } /** * @inheritdoc */ public function rules() { return [ [['store_id', 'mch_id', 'name', 'detail', 'type'], 'required'], [['store_id', 'mch_id', 'express_id', 'created_at', 'is_enable', 'is_delete', 'type', 'updated_at', 'is_new'], 'integer'], [['detail'], 'string'], ['mch_id', 'default', 'value' => 0], [['name', 'express'], 'string', 'max' => 255], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'mch_id' => '商户id', 'name' => '名称', 'express_id' => '物流公司', 'detail' => '规则详细', 'created_at' => '创建时间', 'is_enable' => '是否启用:0=否,1=是', 'is_delete' => 'Is Delete', 'express' => '快递公司', 'type' => '计费方式', 'updated_at' => '更新时间', 'is_new' => '是否是新版本' ]; } /** * 购物车结算 多种运费规则组合计算运费 * @param $store_id * @param $province_id * @param $goods * @return float|int */ public static function getExpressPriceMore($store_id, $city_id, $goodsList, $province_id, &$error_data = []) { $newGoodsList = []; foreach ($goodsList as $row) { if (isset($newGoodsList[$row['freight']])) { $newGoodsList[$row['freight']]['num'] += $row['num']; $newGoodsList[$row['freight']]['weight'] += $row['weight']; } else { $newGoodsList[$row['freight']] = $row; } } $price = 0; foreach ($newGoodsList as $key => $goods) { $open = true; if ($goods['freight'] != '0') { $postage_rules = PostageRules::find()->andWhere([ 'is_delete' => 0, 'id' => $goods['freight'], ])->asArray()->one(); } if (!$postage_rules) { $goodsInfo = Goods::findOne($goods['goods_id']); if (isset($goodsInfo['mch_id']) && $goodsInfo['mch_id']) { $postage_rules = PostageRules::find()->andWhere([ 'is_delete' => 0, 'is_enable' => 1, 'mch_id' => $goodsInfo['mch_id'], ])->asArray()->one(); } else { $postage_rules = PostageRules::find()->andWhere([ 'store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1, 'mch_id' => 0, ])->asArray()->one(); } } if ($postage_rules) { $list = json_decode($postage_rules['detail'], true); foreach ($list as $i => $item) { if (intval($postage_rules['type']) === 1) { if (!empty($item['rules'])) { //按照重量 foreach ($item['rules'] as $rules_item) { if ($rules_item['min_weight'] <= $goods['weight'] && $goods['weight'] < $rules_item['max_weight']) { foreach ($item['province_list'] as $j => $province) { if ($province['id'] == $province_id || $province['id'] == $city_id) { $price += (float)$rules_item['frist_price']; if ($goods['weight'] > $rules_item['frist']) { $diff = $goods['weight'] - $rules_item['frist']; if ($rules_item['second'] > 0 && $rules_item['second_price'] > 0) { $diff = ceil($diff / $rules_item['second']); $price += $diff * (float)$rules_item['second_price']; } } $open = false; goto postage_rules_end; } } } } } } else { foreach ($item['province_list'] as $j => $province) { if ($province['id'] == $province_id || $province['id'] == $city_id) { if (!empty($item['rules'])) { //按照数量 foreach ($item['rules'] as $rules_item) { $price += (float)$rules_item['frist_price']; if ($goods['num'] > intval($rules_item['frist'])) { $diff = $goods['num'] - intval($rules_item['frist']); if (intval($rules_item['second']) > 0 && floatval($rules_item['second_price']) > 0) { $diff = ceil($diff / $rules_item['second']); $price += $diff * (float)$rules_item['second_price']; } } } $open = false; goto postage_rules_end; } } } } } postage_rules_end: if ($open) { $error_data[$goods['goods_id']] = [ 'a' => $postage_rules, '$item' => $item, 'msg' => '该订单重量超过配送范围,请联系客服调整运费规则', 'status' => false ]; return 0; } } } return $price; } public static function getExpressPrice($store_id, $city_id, $goods, $num, $province_id) { if ($goods->freight != '0') { $postage_rules = PostageRules::findOne([ // 'store_id' => $store_id, 'is_delete' => 0, 'id' => $goods->freight, ]); } else { $postage_rules = PostageRules::findOne([ 'store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1, ]); } if (!$postage_rules) { $postage_rules = PostageRules::findOne([ 'store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1 ]); } $price = 0.00; $list = Json::decode($postage_rules->detail); $matching = null; if (empty($list)) { return 0; } foreach ($list as $i => $item) { $in_array = false; foreach ($item['province_list'] as $j => $province) { if (isset($province->id) && ($province->id == $province_id || $province->id == $city_id)) { $in_array = true; break; } } if ($in_array) { $matching = $list[$i]; } } if ($matching == null) { return 0; } if ($postage_rules->type == 1) { // 按重计费 $totalWeight = $goods->weight * $num; $totalWeight -= $matching->frist; $price += $matching->frist_price; $leave = $matching->second ? (ceil($totalWeight / $matching->second) > 0 ? ceil($totalWeight / $matching->second) : 0) : 0; $price += $leave * $matching->second_price; } else { // 按件计费 $num -= $matching->frist; $price += $matching->frist_price; $leave = $matching->second ? (ceil($num / $matching->second) > 0 ? ceil($num / $matching->second) : 0) : 0; $price += $leave * $matching->second_price; } return $price; } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub $postageRules = self::findOne($this->id); (new DiyCommon)->JobBehaviors($postageRules->store_id, StoreSyncExtLog::TYPE_POSTAGE_RULES, [$this->id]); } }