Voucher.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Voucher extends AdminControl
  13. {
  14. private $quotastate_arr;
  15. private $templatestate_arr;
  16. public function initialize()
  17. {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/voucher.lang.php');
  20. if (config('ds_config.voucher_allow') != 1 || config('ds_config.points_isuse') != 1) {
  21. $this->error(lang('admin_voucher_unavailable'), 'operation/setting');
  22. }
  23. $this->quotastate_arr = array(
  24. 'activity' => array(1, lang('admin_voucher_quotastate_activity')),
  25. 'cancel' => array(2, lang('admin_voucher_quotastate_cancel')),
  26. 'expire' => array(3, lang('admin_voucher_quotastate_expire'))
  27. );
  28. //代金券模板状态
  29. $this->templatestate_arr = array(
  30. 'usable' => array(1, lang('admin_voucher_templatestate_usable')),
  31. 'disabled' => array(2, lang('admin_voucher_templatestate_disabled'))
  32. );
  33. View::assign('quotastate_arr', $this->quotastate_arr);
  34. View::assign('templatestate_arr', $this->templatestate_arr);
  35. }
  36. /**
  37. * 代金券设置
  38. */
  39. public function setting()
  40. {
  41. $setting_model = model('config');
  42. if (request()->isPost()) {
  43. $data = [
  44. 'promotion_voucher_price' => input('post.promotion_voucher_price'),
  45. 'promotion_voucher_storetimes_limit' => input('post.promotion_voucher_storetimes_limit'),
  46. 'promotion_voucher_buyertimes_limit' => input('post.promotion_voucher_buyertimes_limit')
  47. ];
  48. $voucher_validate = ds_validate('voucher');
  49. if (!$voucher_validate->scene('setting')->check($data)) {
  50. $this->error($voucher_validate->getError());
  51. }
  52. //每月代金劵软件服务单价
  53. $promotion_voucher_price = intval(input('post.promotion_voucher_price'));
  54. if ($promotion_voucher_price < 0) {
  55. $this->error(lang('param_error'));
  56. }
  57. //每月店铺可以发布的代金劵数量
  58. $promotion_voucher_storetimes_limit = intval(input('post.promotion_voucher_storetimes_limit'));
  59. if ($promotion_voucher_storetimes_limit <= 0) {
  60. $promotion_voucher_storetimes_limit = 20;
  61. }
  62. //买家可以领取的代金劵总数
  63. $promotion_voucher_buyertimes_limit = intval(input('post.promotion_voucher_buyertimes_limit'));
  64. if ($promotion_voucher_buyertimes_limit <= 0) {
  65. $promotion_voucher_buyertimes_limit = 5;
  66. }
  67. $update_array = array();
  68. $update_array['promotion_voucher_price'] = $promotion_voucher_price;
  69. $update_array['voucher_storetimes_limit'] = $promotion_voucher_storetimes_limit;
  70. $update_array['voucher_buyertimes_limit'] = $promotion_voucher_buyertimes_limit;
  71. $result = $setting_model->editConfig($update_array);
  72. if ($result) {
  73. $this->log(lang('admin_voucher_setting') . lang('ds_voucher_price_manage'));
  74. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  75. } else {
  76. $this->error(lang('ds_common_save_fail'));
  77. }
  78. } else {
  79. $setting = rkcache('config', true);
  80. View::assign('setting', $setting);
  81. return View::fetch();
  82. }
  83. }
  84. /*
  85. * 代金券面额列表
  86. */
  87. public function pricelist()
  88. {
  89. //获得代金券金额列表
  90. $voucher_model = model('voucher');
  91. $voucherprice_list = $voucher_model->getVoucherpriceList(10, 'voucherprice asc');
  92. View::assign('voucherprice_list', $voucherprice_list);
  93. View::assign('show_page', $voucher_model->page_info->render());
  94. $this->setAdminCurItem('pricelist');
  95. return View::fetch();
  96. }
  97. /*
  98. * 添加代金券面额页面
  99. */
  100. public function priceadd()
  101. {
  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. } else {
  133. return View::fetch();
  134. }
  135. }
  136. /*
  137. * 编辑代金券面额
  138. */
  139. public function priceedit()
  140. {
  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. } else {
  178. $voucher_model = model('voucher');
  179. $voucherprice_info = $voucher_model->getOneVoucherprice(array('voucherprice_id' => $id));
  180. if (empty($voucherprice_info)) {
  181. $this->error(lang('param_error'), 'voucher/pricelist');
  182. }
  183. View::assign('info', $voucherprice_info);
  184. return View::fetch('priceadd');
  185. }
  186. }
  187. /*
  188. * 删除代金券面额
  189. */
  190. public function pricedrop()
  191. {
  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. //更新过期套餐的状态
  213. $time = TIMESTAMP;
  214. $voucher_model = model('voucher');
  215. $condition = array();
  216. $condition[] = array('voucherquota_endtime', '<', $time);
  217. $condition[] = array('voucherquota_state', '=', $this->quotastate_arr['activity'][0]);
  218. $update = array();
  219. $update['voucherquota_state'] = $this->quotastate_arr['expire'][0];
  220. $voucher_model->editVoucherquota($condition, $update);
  221. $param = array();
  222. if (trim(input('param.store_name'))) {
  223. $param[] = array('voucherquota_storename', 'like', "%{input('param.store_name')}%");
  224. }
  225. $state = intval(input('param.state'));
  226. if ($state) {
  227. $param[] = array('voucherquota_state', '=', $state);
  228. }
  229. $voucherquota_list = $voucher_model->getVoucherquotaList($param, 10, 'voucherquota_id desc');
  230. View::assign('show_page', $voucher_model->page_info->render());
  231. View::assign('voucherquota_list', $voucherquota_list);
  232. $this->setAdminCurItem('quotalist');
  233. return View::fetch();
  234. }
  235. /**
  236. * 代金券列表
  237. */
  238. public function index()
  239. {
  240. $param = array();
  241. if (trim(input('param.store_name'))) {
  242. $param[] = array('vouchertemplate_storename', 'like', "%{input('param.store_name')}%");
  243. }
  244. if (trim(input('param.sdate'))) {
  245. $sdate = strtotime(input('param.sdate'));
  246. $param[] = array('vouchertemplate_adddate', '>=', $sdate);
  247. }
  248. if (trim(input('param.edate'))) {
  249. $edate = strtotime(input('param.edate')) + 86399;
  250. $param[] = array('vouchertemplate_adddate', '<=', $edate);
  251. }
  252. $state = intval(input('param.state'));
  253. if ($state) {
  254. $param[] = array('vouchertemplate_state', '=', $state);
  255. }
  256. if (input('param.recommend') === '1') {
  257. $param[] = array('vouchertemplate_recommend', '=', 1);
  258. } elseif (input('param.recommend') === '0') {
  259. $param[] = array('vouchertemplate_recommend', '=', 0);
  260. }
  261. $voucher_model = model('voucher');
  262. $vouchertemplate_list = $voucher_model->getVouchertemplateList($param, '', '', 10, 'vouchertemplate_state asc,vouchertemplate_id desc');
  263. View::assign('show_page', $voucher_model->page_info->render());
  264. View::assign('vouchertemplate_list', $vouchertemplate_list);
  265. // 输出自营店铺IDS
  266. View::assign('flippedOwnShopIds', array_flip(model('store')->getOwnShopIds()));
  267. $this->setAdminCurItem('index');
  268. return View::fetch();
  269. }
  270. /*
  271. * 代金券模版编辑
  272. */
  273. public function templateedit()
  274. {
  275. $t_id = intval(input('param.tid'));
  276. if ($t_id <= 0) {
  277. $t_id = intval(input('post.tid'));
  278. }
  279. if ($t_id <= 0) {
  280. $this->error(lang('param_error'), 'voucher/index');
  281. }
  282. //查询模板信息
  283. $param = array();
  284. $param['vouchertemplate_id'] = $t_id;
  285. $voucher_model = model('voucher');
  286. $t_info = $voucher_model->getVouchertemplateInfo($param);
  287. if (empty($t_info)) {
  288. $this->error(lang('param_error'), 'voucher/index');
  289. }
  290. if (request()->isPost()) {
  291. $points = intval(input('post.points'));
  292. if ($points < 0) {
  293. $this->error(lang('admin_voucher_template_points_error'));
  294. }
  295. $update_arr = array();
  296. $update_arr['vouchertemplate_points'] = $points;
  297. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  298. $update_arr['vouchertemplate_recommend'] = intval(input('post.recommend')) == 1 ? 1 : 0;
  299. $condition = array();
  300. $condition[] = array('vouchertemplate_id', '=', $t_info['vouchertemplate_id']);
  301. $rs = $voucher_model->editVouchertemplate($condition, $update_arr);
  302. if ($rs) {
  303. $this->log(lang('ds_edit') . lang('ds_voucher_price_manage') . lang('admin_voucher_styletemplate') . '[ID:' . $t_id . ']');
  304. $this->success(lang('ds_common_save_succ'), 'voucher/index');
  305. } else {
  306. $this->error(lang('ds_common_save_fail'), 'voucher/index');
  307. }
  308. } else {
  309. //查询店铺分类
  310. $store_class = rkcache('storeclass', true);
  311. View::assign('store_class', $store_class);
  312. View::assign('t_info', $t_info);
  313. $this->setAdminCurItem('templateedit');
  314. return View::fetch();
  315. }
  316. }
  317. /**
  318. * ajax操作
  319. */
  320. public function ajax()
  321. {
  322. $voucher_model = model('voucher');
  323. switch (input('param.branch')) {
  324. case 'vouchertemplate_recommend':
  325. $voucher_model->editVouchertemplate(array('vouchertemplate_id' => intval(input('param.id'))), array(input('param.column') => intval(input('param.value'))));
  326. $logtext = '';
  327. if (intval(input('param.value')) == 1) { //推荐代金券
  328. $logtext = '推荐代金券';
  329. } else {
  330. $logtext = '取消推荐代金券';
  331. }
  332. $this->log($logtext . '[ID:' . intval(input('param.id')) . ']', 1);
  333. echo 'true';
  334. exit;
  335. break;
  336. }
  337. }
  338. /**
  339. * 页面内导航菜单
  340. * @param string $menu_key 当前导航的menu_key
  341. * @param array $array 附加菜单
  342. * @return
  343. */
  344. protected function getAdminItemList()
  345. {
  346. $menu_array = array(
  347. array(
  348. 'name' => 'index',
  349. 'text' => lang('admin_voucher_template_manage'),
  350. 'url' => (string)url('Voucher/index')
  351. ), array(
  352. 'name' => 'quotalist',
  353. 'text' => lang('admin_voucher_quota_manage'),
  354. 'url' => (string)url('Voucher/quotalist')
  355. ), array(
  356. 'name' => 'pricelist',
  357. 'text' => lang('admin_voucher_pricemanage'),
  358. 'url' => (string)url('Voucher/pricelist')
  359. ), array(
  360. 'name' => 'priceadd',
  361. 'text' => lang('admin_voucher_priceadd'),
  362. 'url' => "javascript:dsLayerOpen('" . (string)url('Voucher/priceadd') . "','" . lang('admin_voucher_priceadd') . "')"
  363. ), array(
  364. 'name' => 'setting',
  365. 'text' => lang('admin_voucher_setting'),
  366. 'url' => "javascript:dsLayerOpen('" . (string)url('Voucher/setting') . "','" . lang('admin_voucher_setting') . "')"
  367. ),
  368. );
  369. if (request()->action() == 'templateedit') {
  370. $menu_array = array(
  371. array(
  372. 'name' => 'index',
  373. 'text' => lang('admin_voucher_template_manage'),
  374. 'url' => (string)url('Voucher/index')
  375. ), array(
  376. 'name' => 'templateedit',
  377. 'text' => lang('admin_voucher_template_edit'),
  378. 'url' => ''
  379. )
  380. );
  381. }
  382. return $menu_array;
  383. }
  384. }