VoucherModel.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Admin\Model;
  15. class VoucherModel{
  16. /**
  17. *显示重量单位分页
  18. */
  19. public function show_voucher_class_page($store_id){
  20. $count=M('Voucher')->where( array('store_id' => $store_id) )->count();
  21. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  22. $show = $Page->show();// 分页显示输出
  23. $list = M('Voucher')->where( array('store_id' => $store_id) )->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
  24. return array(
  25. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  26. 'list'=>$list,
  27. 'page'=>$show
  28. );
  29. }
  30. public function show_voucher_list_page($voucher_id)
  31. {
  32. $count=M('voucher_list')->where( array('voucher_id' => $voucher_id) )->count();
  33. $Page = new \Think\Page($count, 20);
  34. $show = $Page->show();// 分页显示输出
  35. $list = M('voucher_list')->where( array('voucher_id' => $voucher_id) )->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
  36. return array(
  37. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  38. 'list'=>$list,
  39. 'page'=>$show
  40. );
  41. }
  42. public function validate($data,$status='update'){
  43. $error=array();
  44. if(empty($data['title'])){
  45. $error='重量名称必填';
  46. }elseif(empty($data['unit'])){
  47. $error='重量单位必填';
  48. }elseif(empty($data['value'])){
  49. $error='重量值 必填';
  50. }
  51. if($status=='add'){
  52. if(M('WeightClass')->getByTitle($data['title'])){
  53. $error='该重量名称已经存在';
  54. }
  55. }else{
  56. if(M('WeightClass')->where('weight_class_id!='.$data['weight_class_id']." AND title='".$data['title']."'")->find()){
  57. $error='该重量名称已经存在';
  58. }
  59. }
  60. if($error){
  61. return array(
  62. 'status'=>'back',
  63. 'message'=>$error
  64. );
  65. }
  66. }
  67. public function add_voucher($data){
  68. $data['begin_time'] = strtotime($data['begin_time']);
  69. $data['end_time'] = strtotime($data['end_time']);
  70. if(empty($data['type'])) {
  71. $data['type'] = 0;
  72. }
  73. $data['is_limit_goods_buy'] = $data['limit_goods'];
  74. $voucher_id = M('Voucher')->add($data);
  75. //limit_goods 0 全场商品,1 部分商品 is_limit_goods_buy
  76. if( $data['limit_goods'] == 1)
  77. {
  78. $goods_ids = $data['goods_ids'];
  79. $goods_ids_arr = explode(',', $goods_ids);
  80. if( !empty($goods_ids_arr) )
  81. {
  82. foreach($goods_ids_arr as $goods_id)
  83. {
  84. $voucher_goods_data = array();
  85. $voucher_goods_data['voucher_id'] = $voucher_id;
  86. $voucher_goods_data['goods_id'] = $goods_id;
  87. M('voucher_goods')->add($voucher_goods_data);
  88. }
  89. }
  90. }
  91. if($voucher_id){
  92. $need = $data['total_count'];
  93. while(true) {
  94. $voucher = array(
  95. 'voucher_id' => $voucher_id,
  96. 'voucher_title' => $data['voucher_title'],
  97. 'store_id' => $data['store_id'],
  98. 'type' => $data['type'],
  99. 'credit' => $data['credit'],
  100. 'limit_money' => $data['limit_money'],
  101. 'is_limit_goods_buy' => $data['limit_goods'],
  102. 'consume' => 'N',
  103. 'begin_time' => $data['begin_time'],
  104. 'end_time' => $data['end_time'],
  105. 'add_time'=>time(),
  106. );
  107. $need -= ( M('voucher_list')->add($voucher)) ? 1 : 0;
  108. if ( $need <= 0 ) break;
  109. }
  110. return array(
  111. 'status'=>'success',
  112. 'message'=>'新增成功',
  113. 'jump'=>U('Voucher/index')
  114. );
  115. }else{
  116. return array(
  117. 'status'=>'fail',
  118. 'message'=>'新增失败',
  119. 'jump'=>U('Voucher/index')
  120. );
  121. }
  122. }
  123. public function edit_weight_class($data){
  124. $error=$this->validate($data);
  125. if($error){
  126. return $error;
  127. }
  128. $r=M('WeightClass')->save($data);
  129. if($r){
  130. return array(
  131. 'status'=>'success',
  132. 'message'=>'修改成功',
  133. 'jump'=>U('WeightClass/index')
  134. );
  135. }else{
  136. return array(
  137. 'status'=>'fail',
  138. 'message'=>'修改失败',
  139. 'jump'=>U('WeightClass/index')
  140. );
  141. }
  142. }
  143. public function del_voucher($id){
  144. M('voucher_list')->where( array('voucher_id' => $id,'user_id' => 0) )->delete();
  145. $r=M('voucher')->where(array('id'=>$id ))->delete();
  146. if($r){
  147. return array(
  148. 'status'=>'success',
  149. 'message'=>'删除成功',
  150. 'jump'=>U('Voucher/index')
  151. );
  152. }else{
  153. return array(
  154. 'status'=>'fail',
  155. 'message'=>'删除失败',
  156. 'jump'=>U('Voucher/index')
  157. );
  158. }
  159. }
  160. }
  161. ?>