### 通过订单的唯一码数组参数和顾客身份判断是否符合满减满折营销活动规则
**位置:**
Wpos\Controller\IndexController.class.php
**参数:**
* 判断是否符合满减满折营销活动规则
* @param $number_goods array 这条订单的唯一码数组
* @param $customer int 顾客会员身份
* @return array
**调用:**
* $result = $this->check_numbers_active($number_goods,$customer);
**完整代码:**
~~~
/**
* 整个订单的营销活动判断封装
* 包括活动最优判断,优惠最多的营销活动
* Lanson 2017-08-31
* @param $number_goods array 这条订单的唯一码数组 例如:$number_goods = array(array( 'S02CB130C01400416',1786)
,array('F02CCMAAZ96M0001',2992),array('F02CAJQ3Z95L0401',6000))
* @param $customer int 顾客会员身份
* @return array
*/
// 满减满折的判断逻辑很复杂 Lanson 2017-08-31
public function check_numbers_active($number_goods,$customer) {
// 1.调用营销活动公共方法
$event_result = $this->check_public_active();
if (empty($event_result)) {
return array('success' => false, 'msg' => '暂时不符合活动条件');
}
// 2.判断是否满减满折活动
foreach ($event_result as $key => $value) {
// 判断活动是否包含满减满折
if (empty($value['order_discount']) && empty($value['order_reduce'])) {
unset($event_result[$key]);
}
}
if (empty($event_result)) {
return array('success' => false, 'msg' => '暂时不符合活动条件');
}
// 3.判断唯一码是否符合营销活动的范围内
$event_goods = array(); // 记录符合营销活动的商品和活动ID
foreach ($event_result as $keyA => $valueA) {
$this_goods = 0; // 记录每个活动符合范围的商品信息 0代表没有商品符合活动范围 1代表有商品符合活动范围
foreach ($number_goods as $keyB => $valueB) {
// 查询每个的唯一码的基本信息
$goods = M('goods_no no')
->field("no.no,no.system_style_no,menu.brand,menu.large_class,menu.middle_class,menu.small_class,menu.sex,
no.company_retail_price as price,no.discount,no.color_name,no.size")
->join("coscia_goods_menu as menu on no.menu_id = menu.id", "left")
->where("no.no = '{$valueB[0]}'")
->find();
// 将商品的品牌、大类、中类、小类、性别全部转化为ID值
$brand_id = M('goods_base_data_value_list')
->where(" `key` = 'name_en' and `value` = '{$goods['brand']}'")
->getField('base_data_id');
$large_class_id = M('goods_base_data_value_list')
->where(" `key` = 'name' and `value` = '{$goods['large_class']}'")
->getField('base_data_id');
$middle_class_id = M('goods_base_data_value_list as list')
->join("coscia_goods_base_data_mark as mark on mark.id = list.base_data_id")
->join("coscia_goods_base_data_list as b on mark.data_list_id = b.id")
->where("list.key = 'no' and list.value = '{$goods['middle_class']}' and b.name = '中类'")
->getField('base_data_id');
$small_class_id = M('goods_base_data_value_list as list')
->join("coscia_goods_base_data_mark as mark on mark.id = list.base_data_id")
->join("coscia_goods_base_data_list as b on mark.data_list_id = b.id")
->where("list.key = 'name' and list.value = '{$goods['small_class']}' and b.name = '小类'")
->getField('base_data_id');
$sex_id = M('goods_base_data_value_list as list')
->join("coscia_goods_base_data_mark as mark on mark.id = list.base_data_id")
->join("coscia_goods_base_data_list as b on mark.data_list_id = b.id")
->where("list.key = 'name' and list.value = '{$goods['sex']}' and b.name = '性别'")
->getField('base_data_id');
$goods['brand'] = $brand_id;
$goods['large_class'] = $large_class_id;
$goods['middle_class'] = $middle_class_id;
$goods['small_class'] = $small_class_id;
$goods['sex'] = $sex_id;
// 对营销活动的类型判断,是满减还是满折,商品范围是按品牌还是款号或SKU (逻辑判断非常复杂)
// 判断是满减满折的商品活动范围
// 1.满减满折品类
if ($valueA['order_reduce'] == 1 || $valueA['order_discount'] == 1) {
$whereA['event_id'] = array('eq',$valueA['id']);
$whereA['is_delete'] = array('eq',0);
$event_class = M('event_total_goods_class')->where($whereA)->select();
foreach ($event_class as $keyC => $valueC) {
// 判断大类ID是否相等
if (!empty($valueC['large_class'])) {
if ($valueC['large_class'] == 1) {
continue;
} elseif ($valueC['large_class'] != $goods['large_class']) {
unset($event_class[$keyC]);
continue;
}
}
// 判断品牌ID是否相等
if (!empty($valueC['brand'])) {
if ($valueC['brand'] != $goods['brand']) {
unset($event_class[$keyC]);
continue;
}
}
// 判断中类ID是否相等
if (!empty($valueC['middle_class'])) {
if ($valueC['middle_class'] != $goods['middle_class']) {
unset($event_class[$keyC]);
continue;
}
}
// 判断小类ID是否相等
if (!empty($valueC['small_class'])) {
if ($valueC['small_class'] != $goods['small_class']) {
unset($event_class[$keyC]);
continue;
}
}
// 判断性别ID是否相等
if (!empty($valueC['sex'])) {
if ($valueC['sex'] != $goods['sex']) {
unset($event_class[$keyC]);
continue;
}
}
}
// 只要满足其中一个活动范围,说明这商品满足活动范围
if (!empty($event_class)) {
$this_goods = 1;
$event_goods[] = array('goods' => $valueB ,'event_id' => $valueA['id']);
}
}
// 2.满减满折款号
elseif ($valueA['order_reduce'] == 2 || $valueA['order_discount'] == 2) {
$whereD['event_id'] = array('eq',$valueA['id']);
$whereD['is_delete'] = array('eq',0);
$event_no = M('event_total_goods_no')->where($whereD)->select();
$system_no = $goods['system_style_no'];
foreach ($event_no as $keyD => $valueD) {
if ($valueD['system_style_no'] == $system_no) {
$this_goods = 1;
$event_goods[] = array('goods' => $valueB ,'event_id' => $valueA['id']);
break;
}
}
}
// 3.满减满折SKU
elseif ($valueA['order_reduce'] == 3 || $valueA['order_discount'] == 3) {
$whereE['event_id'] = array('eq', $valueA['id']);
$whereE['is_delete'] = array('eq', 0);
$event_no = M('event_total_goods_no')->where($whereE)->select();
$sku = $this->noToSku($goods['no']); // 通过函数从唯一码截取SKU码
foreach ($event_no as $keyE => $valueE) {
if ($valueE['system_style_no'] == $sku) {
$this_goods = 1;
$event_goods[] = array('goods' => $valueB, 'event_id' => $valueA['id']);
break;
}
}
}
}
// 判断没有活动商品符合条件,筛选掉这个活动信息
if ($this_goods == 0) {
unset($event_result[$keyA]);
}
}
if (empty($event_result)) {
return array('success' => false, 'msg' => '暂时不符合活动条件');
}
// 5.根据满减满折的规则来判断在活动范围的商品是否符合规则
foreach ($event_result as $keyA => $valueA) {
// 满减规则的逻辑判断
if (!empty($valueA['order_reduce'])) {
// 查询满减规则的数组,需要符合最优原则
$whereF['event_id'] = array('eq',$valueA['id']);
$whereF['type'] = array('eq',2);
$rules = M('event_rules')->where($whereF)->select();
$price_sum = 0;
foreach ($event_goods as $keyB => $valueB) {
if ($valueB['event_id'] == $valueA['id']) {
// 得出活动商品的消费总金额
$price_sum += $valueB['goods']['price'];
}
}
foreach ($rules as $keyC => $valueC) {
if ($valueC['full'] > $price_sum) {
unset($rules[$keyC]);
}
}
if (empty($rules)) {
unset($event_result[$keyA]);
} else {
// 最优满减规则判断
$rules_full = 0;
$k = 0;
foreach ($rules as $keyD => $valueD) {
if (empty($rules_full)) {
$rules_full = $valueD['full'];
$k = $keyD;
continue;
}
if ($valueD['full'] < $rules_full) {
unset($rules[$keyD]);
} else {
unset($rules[$k]);
$k = $keyD;
$rules_full = $valueD['full'];
}
}
}
}
// 满折规则的逻辑判断
else {
// 查询满折规则的数组,需要符合最优原则
$whereG['event_id'] = array('eq',$valueA['id']);
$whereG['type'] = array('eq',3);
$rules = M('event_rules')->where($whereG)->select();
$count = 0;
foreach ($event_goods as $keyH => $valueH) {
if ($valueH['event_id'] == $valueA['id']) {
// 得出活动商品的总数量
$count += 1;
}
}
foreach ($rules as $keyC => $valueC) {
if ($valueC['full'] > $count) {
unset($rules[$keyC]);
}
}
if (empty($rules)) {
unset($event_result[$keyA]);
} else {
// 最优满折规则判断
$rules_full = 0;
$k = 0;
foreach ($rules as $keyD => $valueD) {
if (empty($rules_full)) {
$rules_full = $valueD['full'];
$k = $keyD;
continue;
}
if ($valueD['full'] < $rules_full) {
unset($rules[$keyD]);
} else {
unset($rules[$k]);
$k = $keyD;
$rules_full = $valueD['full'];
}
}
}
}
if (!empty($rules)) {
$r_a = array();
foreach ($rules as $k => $v) {
$r_a = $v;
}
$event_result[$keyA]['rule'] = $r_a;
}
}
if (empty($event_result)) {
return array('success' => false, 'msg' => '暂时不符合活动条件');
}
// 6.最优活动判断,分别计算整单的优惠金多少,哪个优惠金额多,就是最优营销活动
$all_price = array_sum(array_column($number_goods,1)); // 当前的整单总金额
// dump($all_price);
$coupon = 0; // 设置优惠金初始值
$kA = 0;
foreach ($event_result as $keyA => $valueA) {
// 初始参数设定
if (empty($coupon)) {
// 满减规则
if ($valueA['rule']['type'] == 2) {
$coupon = $valueA['rule']['reduce'];
$kA = $keyA;
continue;
}
// 满折规则
elseif ($valueA['rule']['type'] == 3) {
$coupon = $all_price * ((10 - $valueA['rule']['reduce'])/10);
$kA = $keyA;
continue;
}
}
// 满减规则
if ($valueA['rule']['type'] == 2) {
if ($valueA['rule']['reduce'] < $coupon) {
unset($event_result[$keyA]);
} else {
unset($event_result[$kA]);
$kA = $keyA;
$coupon = $valueA['rule']['reduce'];
}
}
// 满折规则
elseif ($valueA['rule']['type'] == 3) {
$couponA = $all_price * ((10 - $valueA['rule']['reduce'])/10);
if ($couponA < $coupon) {
unset($event_result[$keyA]);
} else {
unset($event_result[$kA]);
$kA = $keyA;
$coupon = $couponA;
}
}
}
if (empty($event_result)) {
return array('success' => false, 'msg' => '暂时不符合活动条件');
}
$event_a = array();
foreach ($event_result as $key => $value) {
$event_a = $value;
}
$coupon = round($coupon,0); // 满减满折优惠总金额
$price_coupon = $all_price - $coupon; // 最终活动优惠后的金额
$active = array('event_id' => $event_a['id'],'coupon' => $coupon,'price' => $price_coupon,
'event_name' => $event_a['event_name']);
return array('success' => true,'active' => $active);
}
~~~
- 模版
- 前言
- 项目架构
- 项目规范
- HTML
- CSS
- Javascript
- PHP
- MySQL
- 注意规范
- 开发版本管理
- 开发流程
- 系统配置
- 阿里云服务器配置
- 计划任务配置说明
- 开发示例
- Page分页
- Search_param搜索结果赋值
- Add新增
- Edit编辑
- Ajax表单验证
- Ajax二级联动
- Excel 导出数据首位不去0的方法
- POS总部控制
- 下载CSV格式的模板
- 订单唯一码表和订单SKU表实收金额生成
- 快捷日期选择
- JS函数
- ajax_send
- ajax_result
- createQrCodes
- createBarCodes
- printTpl
- JS插件
- BootstrapValidator表单验证插件
- Address省市区插件
- Bootstrap-datepicker日期插件
- Bootstrap-select多选框插件
- Toastr消息提示插件
- PalyAudit扫描声音提示插件
- WebUploader多图片上传插件
- Ueditor富文本编辑器插件
- Function
- alert
- object_to_array
- array_to_object
- get_address
- set_param_url
- get_shops_name
- get_user_name
- get_warehouse
- get_cheapest_sku
- print_attr(新)
- print_img(新)
- get_spu_no(新)
- get_type_name(新)
- get_brand_en(新)
- get_cat_name(新)
- get_attr_name(新)
- spu_cat_info(新)
- get_time_event_price
- get_vendors
- check_total_reduce
- check_total_discount
- get_inventory
- get_delivery
- get_sale_inventory
- get_customer_name
- phone_protection
- get_order_no
- get_event_name
- get_order_status
- get_item_status
- get_ditch_name
- get_card_no
- get_shop_sales
- get_pay_name
- get_season
- amt_format
- get_cat_parent
- print_attr_id
- round_bcadd
- round_bcsub
- round_bcmul
- round_bcdiv
- get_account_name
- Controller
- Common_BaseController
- check_membership_card
- get_menu_list
- importErrorMassage
- Wpos_IndexController
- get_customer_vip_card
- get_shops_id
- calculate_active_integral
- check_numbers_active
- check_goods_active
- Woms_IndexController
- Model
- View
- category
- cycle_date.html
- shop_select门店多选搜索框
- 品牌A-Z排序多选brand_mc.html
- 供应商代码A-Z排序vendor_no_mc.html
- Lib
- BuyerLib
- WarehouseLib
- EventLib
- getTimeEventPrice
- getVipType
- getEvent
- orderTotalEvent
- orderTimeEvent
- getTotalReduce
- getTotalDiscount
- SaleLib
- CustomerLib
- addCustomerService
- GiftcardLib
- WechatLib
- wxRefund
- OrdersLib
- orderLog
- calculatePayinAmount
- calculateSubtotal
- correctPayinAmount
- saveOrderAddress
- getOrderAddress
- setDeliveryNo
- SyncLib
- updateOuterStock
- UserLib
- createCommission
- FlowLib
- orderList
- addOrder
- addLog
- orderInfo
- checkSku
- orderSave
- orderStop
- orderExecute
- skuEdit
- orderPrinta
- scanGoods
- boxClose
- orderOut
- take
- bview
- check
- deliveryStatus
- checkGoods
- GoodsLib
- createGoodsNo
- createNewGoodsNo
- getSystemStyleNo
- getDim
- MallLib
- smsLog
- GoodsBaseLib
- getBrandInfo
- getBrandsInfo
- getAttrIdArray
- getPrintAttr
- getMustAttr
- getCatIdInfo
- valTypeId
- valsTypeId
- getCatNoInfo
- getCatInfo
- getAttrArr
- getAttrInfo
- getValInfo
- getAttrId
- getValId
- getAttrSeaon
- getValueId
- PointsLog
- pointsIn
- pointsUp
- EcGoodsLib
- getSkuInventory
- Tools
- CsvTools
- csvImport
- csvExport
- ExcelTools
- importExcel
- exportExcel
- exportHeadExcel
- MailTools
- SmsTools
- sendMessage
- UploadTools
- ExportTools
- exportData
- TaobaoTools
- getOnsaleItems
- getSkusItems
- PicturesTools
- uploadPicture
- Plugins
- WxBase
- Taobao
- 问题反馈