Voucher.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Voucher extends AdminControl {
  18. private $quotastate_arr;
  19. private $templatestate_arr;
  20. public function initialize() {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/voucher.lang.php');
  23. if (config('ds_config.voucher_allow') != 1 || config('ds_config.points_isuse') != 1) {
  24. $this->error(lang('admin_voucher_unavailable'), 'operation/setting');
  25. }
  26. $this->quotastate_arr = array(
  27. 'activity' => array(1, lang('admin_voucher_quotastate_activity')),
  28. 'cancel' => array(2, lang('admin_voucher_quotastate_cancel')),
  29. 'expire' => array(3, lang('admin_voucher_quotastate_expire'))
  30. );
  31. //代金券模板状态
  32. $this->templatestate_arr = array(
  33. 'usable' => array(1, lang('admin_voucher_templatestate_usable')),
  34. 'disabled' => array(2, lang('admin_voucher_templatestate_disabled'))
  35. );
  36. View::assign('quotastate_arr', $this->quotastate_arr);
  37. View::assign('templatestate_arr', $this->templatestate_arr);
  38. }
  39. /**
  40. * 代金券设置
  41. */
  42. public function setting() {
  43. $setting_model = model('config');
  44. if (request()->isPost()) {
  45. $data = [
  46. 'promotion_voucher_price' => input('post.promotion_voucher_price'),
  47. 'promotion_voucher_storetimes_limit' => input('post.promotion_voucher_storetimes_limit'),
  48. 'promotion_voucher_buyertimes_limit' => input('post.promotion_voucher_buyertimes_limit')
  49. ];
  50. $voucher_validate = ds_validate('voucher');
  51. if (!$voucher_validate->scene('setting')->check($data)){
  52. $this->error($voucher_validate->getError());
  53. }
  54. //每月代金劵软件服务单价
  55. $promotion_voucher_price = intval(input('post.promotion_voucher_price'));
  56. if ($promotion_voucher_price < 0) {
  57. $this->error(lang('param_error'));
  58. }
  59. //每月店铺可以发布的代金劵数量
  60. $promotion_voucher_storetimes_limit = intval(input('post.promotion_voucher_storetimes_limit'));
  61. if ($promotion_voucher_storetimes_limit <= 0) {
  62. $promotion_voucher_storetimes_limit = 20;
  63. }
  64. //买家可以领取的代金劵总数
  65. $promotion_voucher_buyertimes_limit = intval(input('post.promotion_voucher_buyertimes_limit'));
  66. if ($promotion_voucher_buyertimes_limit <= 0) {
  67. $promotion_voucher_buyertimes_limit = 5;
  68. }
  69. $update_array = array();
  70. $update_array['promotion_voucher_price'] = $promotion_voucher_price;
  71. $update_array['voucher_storetimes_limit'] = $promotion_voucher_storetimes_limit;
  72. $update_array['voucher_buyertimes_limit'] = $promotion_voucher_buyertimes_limit;
  73. $result = $setting_model->editConfig($update_array);
  74. if ($result) {
  75. $this->log(lang('admin_voucher_setting') . lang('ds_voucher_price_manage'));
  76. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  77. } else {
  78. $this->error(lang('ds_common_save_fail'));
  79. }
  80. } else {
  81. $setting = rkcache('config', true);
  82. View::assign('setting', $setting);
  83. return View::fetch();
  84. }
  85. }
  86. /*
  87. * 代金券面额列表
  88. */
  89. public function pricelist() {
  90. //获得代金券金额列表
  91. $voucher_model = model('voucher');
  92. $voucherprice_list = $voucher_model->getVoucherpriceList(10,'voucherprice asc');
  93. View::assign('voucherprice_list', $voucherprice_list);
  94. View::assign('show_page', $voucher_model->page_info->render());
  95. $this->setAdminCurItem('pricelist');
  96. return View::fetch();
  97. }
  98. /*
  99. * 添加代金券面额页面
  100. */
  101. public function priceadd() {
  102. if (request()->isPost()) {
  103. $voucher_model = model('voucher');
  104. $data = [
  105. 'voucher_price' => input('post.voucher_price'),
  106. 'voucher_price_describe' => input('post.voucher_price_describe'),
  107. 'voucher_points' => input('post.voucher_points')
  108. ];
  109. $voucher_validate = ds_validate('voucher');
  110. if (!$voucher_validate->scene('priceadd')->check($data)){
  111. $this->error($voucher_validate->getError());
  112. }
  113. //验证面额是否存在
  114. $voucher_price = intval(input('post.voucher_price'));
  115. $voucher_points = intval(input('post.voucher_points'));
  116. $voucherprice_info = $voucher_model->getOneVoucherprice(array('voucherprice' => $voucher_price));
  117. if (!empty($voucherprice_info)) {
  118. $this->error(lang('admin_voucher_price_exist'));
  119. }
  120. //保存
  121. $insert_arr = array(
  122. 'voucherprice_describe' => trim(input('post.voucher_price_describe')),
  123. 'voucherprice' => $voucher_price, 'voucherprice_defaultpoints' => $voucher_points,
  124. );
  125. $rs = $voucher_model->addVoucherprice($insert_arr);
  126. if ($rs) {
  127. $this->log(lang('ds_add') . lang('admin_voucher_priceadd') . '[' . input('post.voucher_price') . ']');
  128. dsLayerOpenSuccess(lang('ds_common_save_succ'),(string)url('voucher/pricelist'));
  129. } else {
  130. $this->error(lang('ds_common_save_fail'), 'voucher/priceadd');
  131. }
  132. }
  133. else {
  134. return View::fetch();
  135. }
  136. }
  137. /*
  138. * 编辑代金券面额
  139. */
  140. public function priceedit() {
  141. $id = intval(input('param.priceid'));
  142. if ($id <= 0) {
  143. $this->error(lang('param_error'), 'voucher/pricelist');
  144. }
  145. if (request()->isPost()) {
  146. $data = [
  147. 'voucher_price' => input('post.voucher_price'),
  148. 'voucher_price_describe' => input('post.voucher_price_describe'),
  149. 'voucher_points' => input('post.voucher_points')
  150. ];
  151. $voucher_validate = ds_validate('voucher');
  152. if (!$voucher_validate->scene('priceedit')->check($data)){
  153. $this->error($voucher_validate->getError());
  154. }
  155. //验证面额是否存在
  156. $voucher_price = intval(input('post.voucher_price'));
  157. $voucher_points = intval(input('post.voucher_points'));
  158. $voucher_model = model('voucher');
  159. $where = array();
  160. $where[]=array('voucherprice','=',$voucher_price);
  161. $where[]=array('voucherprice_id','<>', $id);
  162. $voucherprice_info = $voucher_model->getOneVoucherprice($where);
  163. if (!empty($voucherprice_info)) {
  164. $this->error(lang('admin_voucher_price_exist'));
  165. }
  166. $update_arr = array();
  167. $update_arr['voucherprice_describe'] = trim(input('post.voucher_price_describe'));
  168. $update_arr['voucherprice'] = $voucher_price;
  169. $update_arr['voucherprice_defaultpoints'] = $voucher_points;
  170. $rs = $voucher_model->editVoucherprice(array('voucherprice_id' => $id),$update_arr);
  171. if ($rs>=0) {
  172. $this->log(lang('ds_edit') . lang('admin_voucher_priceadd') . '[' . input('post.voucher_price') . ']');
  173. dsLayerOpenSuccess(lang('ds_common_save_succ'),(string)url('voucher/pricelist'));
  174. } else {
  175. $this->error(lang('ds_common_save_fail'), 'voucher/pricelist');
  176. }
  177. }
  178. else {
  179. $voucher_model = model('voucher');
  180. $voucherprice_info = $voucher_model->getOneVoucherprice(array('voucherprice_id' => $id));
  181. if (empty($voucherprice_info)) {
  182. $this->error(lang('param_error'), 'voucher/pricelist');
  183. }
  184. View::assign('info', $voucherprice_info);
  185. return View::fetch('priceadd');
  186. }
  187. }
  188. /*
  189. * 删除代金券面额
  190. */
  191. public function pricedrop() {
  192. $voucher_price_id = trim(input('param.voucher_price_id'));
  193. if (empty($voucher_price_id)) {
  194. $this->error(lang('param_error'), 'voucher/pricelist');
  195. }
  196. $voucher_model = model('voucher');
  197. $condition = array();
  198. $condition[] = array('voucherprice_id','in', $voucher_price_id);
  199. $rs = $voucher_model->delVoucherprice($condition);
  200. if ($rs) {
  201. $this->log(lang('ds_del') . lang('admin_voucher_priceadd') . '[ID:' . $voucher_price_id . ']');
  202. ds_json_encode(10000, lang('ds_common_del_succ'));
  203. } else {
  204. ds_json_encode(10001, lang('ds_common_del_fail'));
  205. }
  206. }
  207. /**
  208. * 套餐管理
  209. * */
  210. public function quotalist() {
  211. //更新过期套餐的状态
  212. $time = TIMESTAMP;
  213. $voucher_model = model('voucher');
  214. $condition = array();
  215. $condition[]=array('voucherquota_endtime','<', $time);
  216. $condition[]=array('voucherquota_state','=',$this->quotastate_arr['activity'][0]);
  217. $update = array();
  218. $update['voucherquota_state'] = $this->quotastate_arr['expire'][0];
  219. $voucher_model->editVoucherquota($condition,$update);
  220. $param = array();
  221. if (trim(input('param.store_name'))) {
  222. $param[] = array('voucherquota_storename','like', "%{input('param.store_name')}%");
  223. }
  224. $state = intval(input('param.state'));
  225. if ($state) {
  226. $param[]=array('voucherquota_state','=',$state);
  227. }
  228. $voucherquota_list = $voucher_model->getVoucherquotaList($param,10,'voucherquota_id desc');
  229. View::assign('show_page', $voucher_model->page_info->render());
  230. View::assign('voucherquota_list', $voucherquota_list);
  231. $this->setAdminCurItem('quotalist');
  232. return View::fetch();
  233. }
  234. /**
  235. * 代金券列表
  236. */
  237. public function index() {
  238. $param = array();
  239. if (trim(input('param.store_name'))) {
  240. $param[] = array('vouchertemplate_storename','like', "%{input('param.store_name')}%");
  241. }
  242. if (trim(input('param.sdate'))) {
  243. $sdate = strtotime(input('param.sdate'));
  244. $param[] = array('vouchertemplate_adddate','>=', $sdate);
  245. }
  246. if (trim(input('param.edate'))) {
  247. $edate = strtotime(input('param.edate'))+86399;
  248. $param[] = array('vouchertemplate_adddate','<=', $edate);
  249. }
  250. $state = intval(input('param.state'));
  251. if ($state) {
  252. $param[]=array('vouchertemplate_state','=',$state);
  253. }
  254. if (input('param.recommend') === '1') {
  255. $param[]=array('vouchertemplate_recommend','=',1);
  256. } elseif (input('param.recommend') === '0') {
  257. $param[]=array('vouchertemplate_recommend','=',0);
  258. }
  259. $voucher_model = model('voucher');
  260. $vouchertemplate_list = $voucher_model->getVouchertemplateList($param,'','',10,'vouchertemplate_state asc,vouchertemplate_id desc');
  261. View::assign('show_page', $voucher_model->page_info->render());
  262. View::assign('vouchertemplate_list', $vouchertemplate_list);
  263. // 输出自营店铺IDS
  264. View::assign('flippedOwnShopIds', array_flip(model('store')->getOwnShopIds()));
  265. $this->setAdminCurItem('index');
  266. return View::fetch();
  267. }
  268. /*
  269. * 代金券模版编辑
  270. */
  271. public function templateedit() {
  272. $t_id = intval(input('param.tid'));
  273. if ($t_id <= 0) {
  274. $t_id = intval(input('post.tid'));
  275. }
  276. if ($t_id <= 0) {
  277. $this->error(lang('param_error'), 'voucher/index');
  278. }
  279. //查询模板信息
  280. $param = array();
  281. $param['vouchertemplate_id'] = $t_id;
  282. $voucher_model = model('voucher');
  283. $t_info = $voucher_model->getVouchertemplateInfo($param);
  284. if (empty($t_info)) {
  285. $this->error(lang('param_error'), 'voucher/index');
  286. }
  287. if (request()->isPost()) {
  288. $points = intval(input('post.points'));
  289. if ($points < 0) {
  290. $this->error(lang('admin_voucher_template_points_error'));
  291. }
  292. $update_arr = array();
  293. $update_arr['vouchertemplate_points'] = $points;
  294. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  295. $update_arr['vouchertemplate_recommend'] = intval(input('post.recommend')) == 1 ? 1 : 0;
  296. $condition = array();
  297. $condition[] = array('vouchertemplate_id','=',$t_info['vouchertemplate_id']);
  298. $rs = $voucher_model->editVouchertemplate($condition,$update_arr);
  299. if ($rs) {
  300. $this->log(lang('ds_edit') . lang('ds_voucher_price_manage') . lang('admin_voucher_styletemplate') . '[ID:' . $t_id . ']');
  301. $this->success(lang('ds_common_save_succ'), 'voucher/index');
  302. } else {
  303. $this->error(lang('ds_common_save_fail'), 'voucher/index');
  304. }
  305. } else {
  306. //查询店铺分类
  307. $store_class = rkcache('storeclass', true);
  308. View::assign('store_class', $store_class);
  309. View::assign('t_info', $t_info);
  310. $this->setAdminCurItem('templateedit');
  311. return View::fetch();
  312. }
  313. }
  314. /**
  315. * ajax操作
  316. */
  317. public function ajax() {
  318. $voucher_model = model('voucher');
  319. switch (input('param.branch')) {
  320. case 'vouchertemplate_recommend':
  321. $voucher_model->editVouchertemplate(array('vouchertemplate_id' => intval(input('param.id'))), array(input('param.column') => intval(input('param.value'))));
  322. $logtext = '';
  323. if (intval(input('param.value')) == 1) {//推荐代金券
  324. $logtext = '推荐代金券';
  325. } else {
  326. $logtext = '取消推荐代金券';
  327. }
  328. $this->log($logtext . '[ID:' . intval(input('param.id')) . ']', 1);
  329. echo 'true';
  330. exit;
  331. break;
  332. }
  333. }
  334. /**
  335. * 页面内导航菜单
  336. * @param string $menu_key 当前导航的menu_key
  337. * @param array $array 附加菜单
  338. * @return
  339. */
  340. protected function getAdminItemList() {
  341. $menu_array = array(
  342. array(
  343. 'name' => 'index',
  344. 'text' => lang('admin_voucher_template_manage'),
  345. 'url' => (string)url('Voucher/index')
  346. ), array(
  347. 'name' => 'quotalist',
  348. 'text' => lang('admin_voucher_quota_manage'),
  349. 'url' => (string)url('Voucher/quotalist')
  350. ), array(
  351. 'name' => 'pricelist',
  352. 'text' => lang('admin_voucher_pricemanage'),
  353. 'url' => (string)url('Voucher/pricelist')
  354. ), array(
  355. 'name' => 'priceadd',
  356. 'text' => lang('admin_voucher_priceadd'),
  357. 'url' => "javascript:dsLayerOpen('".(string)url('Voucher/priceadd')."','".lang('admin_voucher_priceadd')."')"
  358. ), array(
  359. 'name' => 'setting',
  360. 'text' => lang('admin_voucher_setting'),
  361. 'url' => "javascript:dsLayerOpen('".(string)url('Voucher/setting')."','".lang('admin_voucher_setting')."')"
  362. ),
  363. );
  364. if (request()->action() == 'templateedit') {
  365. $menu_array = array(
  366. array(
  367. 'name' => 'index',
  368. 'text' => lang('admin_voucher_template_manage'),
  369. 'url' => (string)url('Voucher/index')
  370. ), array(
  371. 'name' => 'templateedit',
  372. 'text' => lang('admin_voucher_template_edit'),
  373. 'url' => ''
  374. )
  375. );
  376. }
  377. return $menu_array;
  378. }
  379. }