### 查詢訂單商品滿足的最優滿減滿折活動信息(結算用)
**位置:**
Common\Lib\EventLib.class.php
**參數:**
* @param array $order_goods 訂單商品列表數組
* @param int $use 用途,2表示門店結算, 3表示小程序結算
* @param int $customer_id 客戶ID
**調用:**
~~~
$event = new EventLib();
$order_goods = array(
array(
"sku_id"=>20467, // 購買的SKUID
"ac_price"=>3000, // SKU限時折扣活動折后單價
"num"=>1 // 購買數量
),
array(
"sku_id"=>20468,
"ac_price"=>1000,
"num"=>1
)
);
$total_event = $event->orderTotalEvent($order_goods, 3, 14983); // 14983董先生的顧客ID
~~~
**返回:**
返回最優滿減滿折活動信息$active數組 ,數組各項含義如下 :
$active['event_id'] // 活動ID
$active['coupon'] // 滿減滿折優惠金額
$active['after_coupon'] // 活動優惠后的訂單總金額
$active['event_name'] // 活動名稱
$active['url'] // POS中活動詳情鏈接
**完整代碼:**
~~~
/**
* 查詢訂單商品滿足的最優滿減滿折活動信息
* QJM 2018-07-18
* @param array $order_goods 訂單商品列表數組
* $order_goods = array(
* array(
* "sku_id"=>20467, // 購買的SKUID
* "ac_price"=>3000, // SKU限時折扣活動折后單價
* "num"=>1 // 購買數量
* ),
* array(
* "sku_id"=>20468,
* "ac_price"=>1000,
* "num"=>1
* )
* )
* @param int $use 用途,2表示門店結算, 3表示商城結算
* @param int $customer_id 客戶ID
* @return array 活動優惠信息
*/
public function orderTotalEvent($order_goods, $use, $customer_id) {
// 1、根據customer_id獲取會員級別ID
if (empty($customer_id) || $customer_id == 0) {
$vip_type = 0; // 0表示普通顧客
} else {
$important = $this->getVipType($customer_id);
if ($important == -1) {
return -1; // '異常2001:未找到該顧客信息,請新增顧客信息'
}
$vip_type = $important['ber_id']; // 會員級別ID
}
// 2、根據用途和會員類型,獲取符合的滿減、滿折活動基本信息
$event_reduce = $this->getEvent(2, $use, $vip_type); // 調用封裝的函數獲取滿減活動
$event_discount = $this->getEvent(3, $use, $vip_type); // 調用封裝的函數獲取滿折活動
$event_result = array_merge($event_reduce, $event_discount); // 合并滿減滿折活動列表
// 3、判斷訂單商品列表中的SKU是否在營銷活動的范圍內
$event_goods = array(); // 記錄符合營銷活動的商品和活動ID
foreach ($event_result as $key_a => $value_a) {
$this_goods = 0; // 記錄每個活動符合范圍的商品信息 0代表沒有商品符合活動范圍 1代表有商品符合活動范圍
foreach ($order_goods as $key_b => $value_b) {
// 根據傳入的商品ID獲取商品的SPUID和SKUID及商品品類信息
$sku_id = $value_b['sku_id']; // 獲取sku_id
$spu_id = M('goods_sku')->where('id=' . $sku_id)->getField('spu_id'); // 獲取spu_id
$goods = M('goods_sku as sku')
->field('sku.original_price as price, sku.goods_sku, skc.goods_skc, spu.brand_id as brand, spu.spu_no')
->join('coscia_goods_skc as skc on skc.id = sku.skc_id')
->join('coscia_goods_spu as spu on spu.id = sku.spu_id')
->where('sku.id ='.$sku_id)
->find();
$goods['sex'] = M('goods_spu_category')->where('level=1 and spu_id=' . $spu_id)->getField('cat_id'); // 獲取性別ID
$goods['big_class'] = M('goods_spu_category')->where('level=2 and spu_id=' . $spu_id)->getField('cat_id'); // 獲取大類ID
// 滿減滿折范圍 1為品類,2為SPU,3為SKU
if ($value_a['order_reduce'] == 1 || $value_a['order_discount'] == 1) {
$where_a['event_id'] = array('eq', $value_a['id']);
$where_a['is_delete'] = array('eq', 0);
$event_class = M('event_total_goods_class')->where($where_a)->select();
foreach ($event_class as $key_c => $value_c) {
// 判斷品牌ID是否相等
if (!empty($value_c['brand_id'])) {
if ($value_c['brand_id'] != $goods['brand'] && $value_c['cat_big_id'] != 1) {
unset($event_class[$key_c]);
continue;
}
}
// 判斷性別ID是否相等
if (!empty($value_c['cat_sex_id'])) {
if ($value_c['cat_sex_id'] != $goods['sex'] && $value_c['cat_big_id'] != 1) {
unset($event_class[$key_c]);
continue;
}
}
// 判斷大類ID是否相等
if (!empty($value_c['cat_big_id'])) {
if ($value_c['cat_big_id'] != $goods['big_class'] && $value_c['cat_big_id'] != 1) {
unset($event_class[$key_c]);
continue;
}
}
}
// 只要滿足其中一個活動范圍,說明這商品滿足活動范圍
if (!empty($event_class)) {
$this_goods = 1;
$event_goods[] = array('goods' => $value_b ,'event_id' => $value_a['id']);
}
} elseif ($value_a['order_reduce'] == 2 || $value_a['order_discount'] == 2) {
$where_b['event_id'] = array('eq', $value_a['id']);
$where_b['mark'] = array('eq', 1); // 1是款號;2是SKU
$where_b['is_delete'] = array('eq', 0);
$where_b['menu_id'] = array('eq', $spu_id);
$event_no = M('event_total_goods_no')->where($where_b)->select();
// 待測試商品SKU對應款號
$spu_no = $goods['spu_no'];
foreach ($event_no as $key_d => $value_d) {
if ($value_d['system_style_no'] == $spu_no) {
$this_goods = 1;
$event_goods[] = array('goods' => $value_b ,'event_id' => $value_a['id']);
break;
}
}
} elseif ($value_a['order_reduce'] == 3 || $value_a['order_discount'] == 3) {
$where_c['event_id'] = array('eq', $value_a['id']);
$where_c['is_delete'] = array('eq', 0);
$event_no = M('event_total_goods_no')->where($where_c)->select();
foreach ($event_no as $key_e => $value_e) {
if ($value_e['system_style_no'] == $goods['goods_sku']) {
$this_goods = 1;
$event_goods[] = array('goods' => $value_b, 'event_id' => $value_a['id']);
break;
}
}
} elseif ($value_a['order_reduce'] == 4 || $value_a['order_discount'] == 4) {
$where_d['event_id'] = array('eq', $value_a['id']);
$where_d['is_delete'] = array('eq', 0);
$event_no = M('event_total_goods_no')->where($where_d)->select();
foreach ($event_no as $key_f => $value_f) {
if ($value_f['system_style_no'] == $goods['goods_skc']) {
$this_goods = 1;
$event_goods[] = array('goods' => $value_b, 'event_id' => $value_a['id']);
break;
}
}
}
}
// 判斷沒有活動商品符合條件,篩選掉這個活動信息
if ($this_goods == 0) {
unset($event_result[$key_a]);
}
}
if (empty($event_result)) {
return -2; // 無符合的活動
}
// 4、判斷每個活動范圍的商品符合的滿減滿折規則,找出每個活動的最優規則
foreach ($event_result as $key_a => $value_a) {
// 滿減規則的邏輯判斷
if (!empty($value_a['order_reduce'])) {
$where_d['event_id'] = array('eq', $value_a['id']);
$where_d['type'] = array('eq', 2);
$rules = M('event_rules')->where($where_d)->select();
$price_sum = 0;
foreach ($event_goods as $key_b => $value_b) {
if ($value_b['event_id'] == $value_a['id']) {
// 得出活動商品的消費總金額
$price_sum += $value_b['goods']['ac_price'] * $value_b['goods']['num'];
}
}
foreach ($rules as $key_c => $value_c) {
if ($value_c['full'] > $price_sum) {
unset($rules[$key_c]);
}
}
// 如果本活動的活動規則都不符合訂單總價,刪除掉活動
if (empty($rules)) {
unset($event_result[$key_a]);
} else {
// 最優滿減規則判斷
$rules_full = 0;
$k = 0;
foreach ($rules as $key_d => $value_d) {
if (empty($rules_full)) {
$rules_full = $value_d['full'];
$k = $key_d;
continue;
}
if ($value_d['full'] < $rules_full) {
unset($rules[$key_d]);
} else {
unset($rules[$k]);
$k = $key_d;
$rules_full = $value_d['full'];
}
}
}
}
// 滿折規則的邏輯判斷
else {
// 查詢滿折規則的數組,需要符合最優原則
$where_e['event_id'] = array('eq',$value_a['id']);
$where_e['type'] = array('eq',3);
$rules = M('event_rules')->where($where_e)->select();
$count = 0;
foreach ($event_goods as $key_h => $value_h) {
if ($value_h['event_id'] == $value_a['id']) {
// 得出活動商品的總數量
$count += $value_h['goods']['num'];
}
}
foreach ($rules as $key_c => $value_c) {
if ($value_c['full'] > $count) {
unset($rules[$key_c]);
}
}
if (empty($rules)) {
unset($event_result[$key_a]);
} else {
// 最優滿折規則判斷
$rules_full = 0;
$k = 0;
foreach ($rules as $key_d => $value_d) {
if (empty($rules_full)) {
$rules_full = $value_d['full'];
$k = $key_d;
continue;
}
if ($value_d['full'] < $rules_full) {
unset($rules[$key_d]);
} else {
unset($rules[$k]);
$k = $key_d;
$rules_full = $value_d['full'];
}
}
}
}
// 將每個活動中訂單滿足的最優的規則作為活動規則項保存到活動數組
if (!empty($rules)) {
$r_a = array();
foreach ($rules as $k => $v) {
$r_a = $v;
}
$event_result[$key_a]['rules'] = $r_a;
}
}
if (empty($event_result)) {
return -2; // 無符合的活動
}
// 5、最優活動判斷,分別計算整單的優惠金多少,哪個優惠金額多,就是最優營銷活動
$all_price = 0; // 初始化當前整單總金額為0
foreach($order_goods as $key_z => $value_z) {
$all_price += $value_z['ac_price'] * $value_z['num'];
}
$coupon = 0; // 設置優惠金初始值
$k_a = 0;
foreach ($event_result as $key_a => $value_a) {
// 初始參數設定
if (empty($coupon)) {
// 滿減規則
if ($value_a['rules']['type'] == 2) {
$coupon = $value_a['rules']['event_reduce'];
$k_a = $key_a;
continue;
}
// 滿折規則
elseif ($value_a['rules']['type'] == 3) {
$rules_goods = []; // 符合當前滿折活動的商品 $event_goods是所有活動商品
foreach ($event_goods as $key_c => $value) {
if($value['event_id'] == $event_result[$key_a]['id']) {
$rules_goods[] = $event_goods[$key_c];
}
}
// 這里不能整單進行打折,需要只對活動商品打折
$ac_goods = array_column($rules_goods,'goods'); // 參與該滿折活動的商品
// 初始化參與本活動的商品的總價格為0
$total_price = 0;
foreach ($ac_goods as $key_f => $value_f) {
$total_price += ($value_f['ac_price'] * $value_f['num']);
}
$coupon = $total_price * (1-$value_a['rules']['event_reduce']); // 減多少元或者打幾折
$k_a = $key_a;
continue;
}
}
// 滿減規則
if ($value_a['rules']['type'] == 2) {
if ($value_a['rules']['reduce'] < $coupon) {
unset($event_result[$key_a]);
} else {
unset($event_result[$k_a]);
$k_a = $key_a;
$coupon = $value_a['rules']['reduce'];
}
}
// 滿折規則
elseif ($value_a['rules']['type'] == 3) {
$rules_goods = []; // 符合當前滿折活動的商品 $event_goods是所有活動商品
foreach ($event_goods as $key_c => $value) {
if($value['event_id'] == $event_result[$key_a]['id']) {
$rules_goods[] = $event_goods[$key_c];
}
}
// 這里不能整單進行打折,需要只對活動商品打折
$ac_goods = array_column($rules_goods,'goods'); // 參與該滿折活動的商品
// 初始化參與本活動的商品的總價格為0
$total_price = 0;
foreach ($ac_goods as $key_f => $value_f) {
$total_price += ($value_f['ac_price'] * $value_f['num']);
}
$coupon_a = $total_price * (1-$value_a['rules']['event_reduce']);
if ($coupon_a < $coupon) {
unset($event_result[$key_a]);
} else {
unset($event_result[$k_a]);
$k_a = $key_a;
$coupon = $coupon_a;
}
}
}
if (empty($event_result)) {
return -2; // 無符合的活動
}
$event_a = array();
foreach ($event_result as $key => $value) {
$event_a = $value;
}
$coupon = round($coupon,2); // 滿減滿折優惠總金額 小數點后2位四舍五入
$after_coupon = $all_price - $coupon; // 最終活動優惠后的金額
$url = U("/Wpos/Member/Event/index",array('id' => $event_a['id'])); // 活動詳情鏈接
$active = array(
'event_id' => $event_a['id'],
'coupon' => $coupon,
'after_coupon' => $after_coupon,
'event_name' => $event_a['event_name'] ,
'rules' => $event_a['rules'],
'url' => $url
);
return $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
- 問題反饋