Voucher.php 17 KB

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