voucher.model.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * 代金券模型
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class voucherModel extends Model {
  7. const VOUCHER_STATE_UNUSED = 1;
  8. const VOUCHER_STATE_USED = 2;
  9. const VOUCHER_STATE_EXPIRE = 3;
  10. private $voucher_state_array = array(
  11. self::VOUCHER_STATE_UNUSED => '未使用',
  12. self::VOUCHER_STATE_USED => '已使用',
  13. self::VOUCHER_STATE_EXPIRE => '已过期',
  14. );
  15. private $applystate_arr;
  16. private $quotastate_arr;
  17. private $templatestate_arr;
  18. public function __construct(){
  19. parent::__construct();
  20. //申请记录状态
  21. $this->applystate_arr = array('new'=>array(1,Language::get('voucher_applystate_new')),'verify'=>array(2,Language::get('voucher_applystate_verify')),'cancel'=>array(3,Language::get('voucher_applystate_cancel')));
  22. //套餐状态
  23. $this->quotastate_arr = array('activity'=>array(1,Language::get('voucher_quotastate_activity')),'cancel'=>array(2,Language::get('voucher_quotastate_cancel')),'expire'=>array(3,Language::get('voucher_quotastate_expire')));
  24. //代金券模板状态
  25. $this->templatestate_arr = array('usable'=>array(1,'有效'),'disabled'=>array(2,'失效'));
  26. }
  27. /**
  28. * 获取代金券模板状态
  29. */
  30. public function getTemplateState(){
  31. return $this->templatestate_arr;
  32. }
  33. /**
  34. * 领取的代金券状态
  35. */
  36. public function getVoucherState(){
  37. return array('unused'=>array(1,Language::get('voucher_voucher_state_unused')),'used'=>array(2,Language::get('voucher_voucher_state_used')),'expire'=>array(3,Language::get('voucher_voucher_state_expire')));
  38. }
  39. /**
  40. * 返回当前可用的代金券列表,每种类型(模板)的代金券里取出一个代金券码(同一个模板所有码面额和到期时间都一样)
  41. * @param array $condition 条件
  42. * @param array $goods_total 商品总金额
  43. * @return string
  44. */
  45. public function getCurrentAvailableVoucher($condition = array(), $goods_total = 0) {
  46. $condition['voucher_end_date'] = array('gt',time());
  47. $condition['voucher_state'] = 1;
  48. $voucher_list = $this->table('voucher')->where($condition)->key('voucher_t_id')->select();
  49. foreach ($voucher_list as $key => $voucher) {
  50. if ($goods_total < $voucher['voucher_limit']) {
  51. unset($voucher_list[$key]);
  52. } else {
  53. $voucher_list[$key]['desc'] = sprintf('面额%s元 有效期至 %s',$voucher['voucher_price'],date('Y-m-d',$voucher['voucher_end_date']));
  54. }
  55. }
  56. return $voucher_list;
  57. }
  58. /**
  59. * 取得当前有效代金券数量
  60. * @param int $member_id
  61. */
  62. public function getCurrentAvailableVoucherCount($member_id) {
  63. $info = rcache($member_id, 'm_voucher', 'voucher_count');
  64. if (empty($info['voucher_count']) && $info['voucher_count'] !== 0) {
  65. $condition['voucher_owner_id'] = $member_id;
  66. $condition['voucher_end_date'] = array('gt',time());
  67. $condition['voucher_state'] = 1;
  68. $voucher_count = $this->table('voucher')->where($condition)->count();
  69. $voucher_count = intval($voucher_count);
  70. wcache($member_id, array('voucher_count' => $voucher_count), 'm_voucher');
  71. } else {
  72. $voucher_count = intval($info['voucher_count']);
  73. }
  74. return $voucher_count;
  75. }
  76. /*
  77. * 查询当前可用的套餐
  78. */
  79. public function getCurrentQuota($store_id){
  80. $store_id = intval($store_id);
  81. if($store_id <= 0){
  82. return false;
  83. }
  84. $param = array();
  85. $param['quota_storeid'] = $store_id;
  86. $param['quota_endtime'] = array('gt', time());
  87. $info = $this->table('voucher_quota')->where($param)->find();
  88. return $info;
  89. }
  90. /*
  91. * 查询新申请的套餐
  92. */
  93. public function getNewApply($store_id){
  94. $store_id = intval($store_id);
  95. if($store_id <= 0){
  96. return false;
  97. }
  98. $new_apply = $this->table('voucher_apply')->where(array('apply_storeid'=>$store_id,'apply_state'=>$this->applystate_arr['new'][0]))->find();
  99. $newapply_flag = false;
  100. if(!empty($new_apply)){
  101. $newapply_flag = true;
  102. }
  103. return $newapply_flag;
  104. }
  105. /**
  106. * 获得代金券列表
  107. */
  108. public function getVoucherList($where, $field = '*', $limit = 0, $page = 0, $order = '', $group = ''){
  109. $voucher_list = array();
  110. if (is_array($page)){
  111. if ($page[1] > 0){
  112. $voucher_list = $this->table('voucher')->field($field)->where($where)->page($page[0],$page[1])->order($order)->group($group)->select();
  113. } else {
  114. $voucher_list = $this->table('voucher')->field($field)->where($where)->page($page[0])->order($order)->group($group)->select();
  115. }
  116. } else {
  117. $voucher_list = $this->table('voucher')->field($field)->where($where)->page($page)->order($order)->group($group)->select();
  118. }
  119. return $voucher_list;
  120. }
  121. public function getVoucherUnusedList($where, $field = '*') {
  122. $where['voucher_state'] = 1;
  123. return $this->getVoucherList($where, $field);
  124. }
  125. /**
  126. * 查询可兑换代金券模板详细信息,包括店铺信息
  127. */
  128. public function getCanChangeTemplateInfo($vid,$member_id,$store_id = 0){
  129. if ($vid <= 0 || $member_id <= 0 ){
  130. return array('state'=>false,'msg'=>'参数错误');
  131. }
  132. //查询可用代金券模板
  133. $where = array();
  134. $where['voucher_t_id'] = $vid;
  135. $where['voucher_t_state'] = $this->templatestate_arr['usable'][0];
  136. $where['voucher_t_end_date'] = array('gt',time());
  137. $template_info = $this->table('voucher_template')->field('*')->where($where)->find();
  138. if (empty($template_info) || $template_info['voucher_t_total']<=$template_info['voucher_t_giveout']){//代金券不存在或者已兑换完
  139. return array('state'=>false,'msg'=>'代金券信息错误');
  140. }
  141. //验证是否为店铺自己
  142. if ($store_id > 0 && $store_id == $template_info['voucher_t_store_id']){
  143. return array('state'=>false,'msg'=>'不可以兑换自己店铺的代金券');
  144. }
  145. //查询代金券对应的店铺信息
  146. $store_info = Model('store')->getStoreInfoByID($template_info['voucher_t_store_id']);
  147. if (empty($store_info)){
  148. return array('state'=>false,'msg'=>'代金券信息错误');
  149. }
  150. //整理代金券信息
  151. if (!empty($template_info['voucher_t_customimg'])){
  152. $template_info['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.ATTACH_VOUCHER.DS.$template_info['voucher_t_store_id'].DS.$template_info['voucher_t_customimg'];
  153. }else{
  154. $template_info['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.defaultGoodsImage(240);
  155. }
  156. $template_info = array_merge($template_info,$store_info);
  157. //查询代金券列表
  158. $where = array();
  159. $where['voucher_owner_id'] = $member_id;
  160. $where['voucher_store_id'] = $template_info['voucher_t_store_id'];
  161. $voucher_list= $this->getVoucherList($where);
  162. if (!empty($voucher_list)){
  163. $voucher_count = 0;//在该店铺兑换的代金券数量
  164. $voucherone_count = 0;//该张代金券兑换的数量
  165. foreach ($voucher_list as $k=>$v){
  166. //如果代金券未用且未过期
  167. if ($v['voucher_state'] == 1 && $v['voucher_end_date'] > time()){
  168. $voucher_count += 1;
  169. }
  170. if ($v['voucher_t_id'] == $template_info['voucher_t_id']){
  171. $voucherone_count += 1;
  172. }
  173. }
  174. //买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量的验证
  175. if ($voucher_count >= intval(C('promotion_voucher_buyertimes_limit'))){
  176. $message = sprintf('您的可用代金券已有%s张,不可再兑换了',C('promotion_voucher_buyertimes_limit'));
  177. return array('state'=>false,'msg'=>$message);
  178. }
  179. //同一张代金券最多能兑换的次数
  180. if (!empty($template_info['voucher_t_eachlimit']) && $voucherone_count >= $template_info['voucher_t_eachlimit']){
  181. $message = sprintf('该代金券您已兑换%s次,不可再兑换了',$template_info['voucher_t_eachlimit']);
  182. return array('state'=>false,'msg'=>$message);
  183. }
  184. }
  185. //验证会员积分是否足够
  186. $member_info = Model('member')->getMemberInfoByID($member_id);
  187. if (empty($member_info)){
  188. return array('state'=>false,'msg'=>'参数错误');
  189. }
  190. if (intval($member_info['member_points']) < intval($template_info['voucher_t_points'])){
  191. return array('state'=>false,'msg'=>'您的积分不足,暂时不能兑换该代金券');
  192. }
  193. return array('state'=>true,'info'=>$template_info);
  194. }
  195. /*
  196. * 获取代金券编码
  197. */
  198. public function get_voucher_code() {
  199. return mt_rand(10,99)
  200. . sprintf('%010d',time() - 946656000)
  201. . sprintf('%03d', (float) microtime() * 1000)
  202. . sprintf('%03d', (int) $_SESSION['member_id'] % 1000);
  203. }
  204. /**
  205. * 更新代金券信息
  206. * @param array $data
  207. * @param array $condition
  208. */
  209. public function editVoucher($data,$condition,$member_id = 0 ) {
  210. $result = $this->table('voucher')->where($condition)->update($data);
  211. if ($result && $member_id > 0){
  212. wcache($member_id, array('voucher_count' => null), 'm_voucher');
  213. }
  214. return $result;
  215. }
  216. /**
  217. * 返回代金券状态数组
  218. * @return array
  219. */
  220. public function getVoucherStateArray() {
  221. return $this->voucher_state_array;
  222. }
  223. /**
  224. * 获取买家代金券列表
  225. *
  226. * @param int $member_id 用户编号
  227. * @param int $voucher_state 代金券状态
  228. * @param int $page 分页数
  229. */
  230. public function getMemberVoucherList($member_id, $voucher_state, $page = null) {
  231. if(empty($member_id)) {
  232. return false;
  233. }
  234. //更新过期代金券状态
  235. $this->_checkVoucherExpire($member_id);
  236. $field = 'voucher_id,voucher_code,voucher_title,voucher_desc,voucher_start_date,voucher_end_date,voucher_price,voucher_limit,voucher_state,voucher_order_id,voucher_store_id,store_name,store_id,store_domain,voucher_t_customimg';
  237. $on = 'voucher.voucher_store_id = store.store_id,voucher.voucher_t_id=voucher_template.voucher_t_id';
  238. $where = array('voucher_owner_id'=>$member_id);
  239. $voucher_state = intval($voucher_state);
  240. if (intval($voucher_state) > 0 && array_key_exists($voucher_state, $this->voucher_state_array)){
  241. $where['voucher_state'] = $voucher_state;
  242. }
  243. $list = $this->table('voucher,store,voucher_template')->field($field)->join('inner,inner')->on($on)->where($where)->order('voucher_id desc')->page($page)->select();
  244. if(!empty($list) && is_array($list)){
  245. foreach ($list as $key=>$val){
  246. //代金券图片
  247. if (empty($val['voucher_t_customimg']) || !file_exists(BASE_UPLOAD_PATH.DS.ATTACH_VOUCHER.DS.$val['store_id'].DS.$val['voucher_t_customimg'])){
  248. $list[$key]['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.defaultGoodsImage(60);
  249. }else{
  250. $list[$key]['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.ATTACH_VOUCHER.DS.$val['store_id'].DS.str_ireplace('.', '_small.', $val['voucher_t_customimg']);
  251. }
  252. //代金券状态文字
  253. $list[$key]['voucher_state_text'] = $this->voucher_state_array[$val['voucher_state']];
  254. }
  255. }
  256. return $list;
  257. }
  258. /**
  259. * 更新过期代金券状态
  260. */
  261. private function _checkVoucherExpire($member_id) {
  262. $condition = array();
  263. $condition['voucher_owner_id'] = $member_id;
  264. $condition['voucher_state'] = self::VOUCHER_STATE_UNUSED;
  265. $condition['voucher_end_date'] = array('lt', time());
  266. $this->table('voucher')->where($condition)->update(array('voucher_state' => self::VOUCHER_STATE_EXPIRE));
  267. }
  268. /**
  269. * 查询代金券模板列表
  270. */
  271. public function getVoucherTemplateList($where, $field = '*', $limit = 0, $page = 0, $order = '', $group = '') {
  272. $voucher_list = array();
  273. if (is_array($page)){
  274. if ($page[1] > 0){
  275. $voucher_list = $this->table('voucher_template')->field($field)->where($where)->page($page[0],$page[1])->order($order)->group($group)->select();
  276. } else {
  277. $voucher_list = $this->table('voucher_template')->field($field)->where($where)->page($page[0])->order($order)->group($group)->select();
  278. }
  279. } else {
  280. $voucher_list = $this->table('voucher_template')->field($field)->where($where)->page($page)->order($order)->group($group)->select();
  281. }
  282. //查询店铺分类
  283. $store_class = rkcache('store_class', true);
  284. if (!empty($voucher_list) && is_array($voucher_list)){
  285. foreach ($voucher_list as $k=>$v){
  286. if (!empty($v['voucher_t_customimg'])){
  287. $v['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.ATTACH_VOUCHER.DS.$v['voucher_t_store_id'].DS.$v['voucher_t_customimg'];
  288. }else{
  289. $v['voucher_t_customimg'] = UPLOAD_SITE_URL.DS.defaultGoodsImage(240);
  290. }
  291. $v['voucher_t_limit'] = intval($v['voucher_t_limit']);
  292. $v['voucher_t_sc_name'] = $store_class[$v['voucher_t_sc_id']]['sc_name'];
  293. $voucher_list[$k] = $v;
  294. }
  295. }
  296. return $voucher_list;
  297. }
  298. /**
  299. * 更新代金券模板信息
  300. * @param array $data
  301. * @param array $condition
  302. */
  303. public function editVoucherTemplate($where,$data) {
  304. return $this->table('voucher_template')->where($where)->update($data);
  305. }
  306. /**
  307. * 获得代金券面额列表
  308. */
  309. public function getVoucherPriceList(){
  310. return $this->table('voucher_price')->order('voucher_price asc')->select();
  311. }
  312. /**
  313. * 获得推荐的热门代金券列表
  314. * @param int $num 查询条数
  315. */
  316. public function getRecommendTemplate($num){
  317. //查询店铺分类
  318. $store_class = rkcache('store_class', true);
  319. //查询推荐的热门代金券列表
  320. $where = array();
  321. $where['voucher_t_recommend'] = '1';
  322. $where['voucher_t_state'] = $this->templatestate_arr['usable'][0];
  323. $where['voucher_t_end_date'] = array('gt',time());
  324. $recommend_voucher = $this->getVoucherTemplateList($where, $field = '*', $num, 0, 'voucher_t_id desc');
  325. if ($recommend_voucher){
  326. foreach ($recommend_voucher as $k=>$v){
  327. $v['voucher_t_sc_name'] = $store_class[$v['voucher_t_sc_id']]['sc_name'];
  328. $recommend_voucher[$k] = $v;
  329. }
  330. }
  331. return $recommend_voucher;
  332. }
  333. /**
  334. * 积分兑换代金券
  335. */
  336. public function exchangeVoucher($template_info, $member_id, $member_name = ''){
  337. if (intval($member_id) <= 0 || empty($template_info)){
  338. return array('state'=>false,'msg'=>'参数错误');
  339. }
  340. //查询会员信息
  341. if (!$member_name){
  342. $member_info = Model('member')->getMemberInfoByID($member_id);
  343. $member_name = $member_info['member_name'];
  344. }
  345. //添加代金券信息
  346. $insert_arr = array();
  347. $insert_arr['voucher_code'] = $this->get_voucher_code();
  348. $insert_arr['voucher_t_id'] = $template_info['voucher_t_id'];
  349. $insert_arr['voucher_title'] = $template_info['voucher_t_title'];
  350. $insert_arr['voucher_desc'] = $template_info['voucher_t_desc'];
  351. $insert_arr['voucher_start_date'] = time();
  352. $insert_arr['voucher_end_date'] = $template_info['voucher_t_end_date'];
  353. $insert_arr['voucher_price'] = $template_info['voucher_t_price'];
  354. $insert_arr['voucher_limit'] = $template_info['voucher_t_limit'];
  355. $insert_arr['voucher_store_id'] = $template_info['voucher_t_store_id'];
  356. $insert_arr['voucher_state'] = 1;
  357. $insert_arr['voucher_active_date'] = time();
  358. $insert_arr['voucher_owner_id'] = $member_id;
  359. $insert_arr['voucher_owner_name'] = $member_name;
  360. //扣除会员积分
  361. $points_arr['pl_memberid'] = $member_id;
  362. $points_arr['pl_membername'] = $member_name;
  363. $points_arr['pl_points'] = -$template_info['voucher_t_points'];
  364. $points_arr['point_ordersn'] = $insert_arr['voucher_code'];
  365. $points_arr['pl_desc'] = L('home_voucher').$insert_arr['voucher_code'].L('points_pointorderdesc');
  366. Model('points')->savePointsLog('app',$points_arr,true);
  367. $result = $this->table('voucher')->insert($insert_arr);
  368. if ($result){
  369. //代金券模板的兑换数增加
  370. $this->editVoucherTemplate(array('voucher_t_id'=>$template_info['voucher_t_id']), array('voucher_t_giveout'=>array('exp','voucher_t_giveout+1')));
  371. wcache($member_id, array('voucher_count' => array('exp','voucher_count+1')), 'm_voucher');
  372. return array('state'=>true,'msg'=>'兑换成功');
  373. } else {
  374. return array('state'=>false,'msg'=>'兑换失败');
  375. }
  376. }
  377. }