Sellerpromotionmgdiscount.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  11. * 网站地址: https://www.valimart.net/
  12. * ----------------------------------------------------------------------------
  13. *
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Sellerpromotionmgdiscount extends BaseSeller {
  18. public function initialize() {
  19. parent::initialize();
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionmgdiscount.lang.php');
  21. if (intval(config('ds_config.mgdiscount_allow')) !== 1) {
  22. $this->error(lang('mgdiscount_unavailable'), 'seller/index');
  23. }
  24. }
  25. /**
  26. * 店铺的基本设置
  27. */
  28. public function mgdiscount_store() {
  29. $mgdiscountquota_model = model('pmgdiscountquota');
  30. //当前的和系统设置的会员等级进行比对
  31. if (!request()->isPost()) {
  32. if (check_platform_store()) {
  33. View::assign('isPlatformStore', true);
  34. } else {
  35. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  36. View::assign('current_mgdiscount_quota', $current_mgdiscount_quota);
  37. }
  38. //当前店铺设置的会员等级对应的折扣
  39. $store = Db::name('store')->where('store_id', session('store_id'))->find();
  40. View::assign('mgdiscount_store_arr', $this->_get_mgdiscount_arr($store['store_mgdiscount']));
  41. View::assign('store', $store);
  42. $this->setSellerCurMenu('Sellermgdiscount');
  43. $this->setSellerCurItem('mgdiscount_store');
  44. return View::fetch($this->template_dir . 'mgdiscount_store');
  45. } else {
  46. $member_model = model('member');
  47. //系统等级设置
  48. $membergrade_arr = $member_model->getMemberGradeArr();
  49. $result_1 = array();
  50. $pre_level_discount = 0;
  51. foreach ($membergrade_arr as $key => $value) {
  52. $current_level_discount = floatval($_POST['mgdiscount_store'][$key]['level_discount'] * 10);
  53. //限制会员等级高的会员享受更低折扣
  54. if ($pre_level_discount == 0) {
  55. $pre_level_discount = $current_level_discount;
  56. }
  57. if ($current_level_discount > $pre_level_discount) {
  58. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  59. }
  60. $pre_level_discount = $current_level_discount;
  61. if ($current_level_discount < 1 || $current_level_discount > 100) {
  62. ds_json_encode(10001, lang('current_level_discount_range_error'));
  63. }
  64. $result_1[$key]['level_discount'] = $current_level_discount / 10;
  65. $result_1[$key]['level_name'] = $value['level_name'];
  66. }
  67. $data = array(
  68. 'store_mgdiscount' => serialize($result_1),
  69. 'store_mgdiscount_state' => intval(input('post.store_mgdiscount_state'))
  70. );
  71. $result = Db::name('store')->where('store_id', session('store_id'))->update($data);
  72. if ($result) {
  73. ds_json_encode(10000, lang('ds_common_op_succ'));
  74. } else {
  75. ds_json_encode(10001, lang('ds_common_op_fail'));
  76. }
  77. }
  78. }
  79. //显示不同商品享受的会员等级折扣
  80. public function mgdiscount_goods() {
  81. $goods_model = model('goods');
  82. $condition[] = array('goods_mgdiscount', '<>', '');
  83. $condition[] = array('store_id', '=', session('store_id'));
  84. $goods_list = $goods_model->getGoodsCommonOnlineList($condition);
  85. foreach ($goods_list as $key => $goods) {
  86. $goods_list[$key]['goods_mgdiscount_arr'] = $this->_get_mgdiscount_arr($goods['goods_mgdiscount']);
  87. }
  88. View::assign('show_page', $goods_model->page_info->render());
  89. View::assign('goods_list', $goods_list);
  90. /* 设置卖家当前菜单 */
  91. $this->setSellerCurMenu('Sellermgdiscount');
  92. $this->setSellerCurItem('mgdiscount_goods');
  93. return View::fetch($this->template_dir . 'mgdiscount_goods');
  94. }
  95. /**
  96. * 通过系统会员等级和现有数据比对得出数值
  97. * @param type $mgdiscount_arr_temp
  98. * @return type
  99. */
  100. private function _get_mgdiscount_arr($mgdiscount_arr_temp) {
  101. $mgdiscount_arr_temp = @unserialize($mgdiscount_arr_temp);
  102. $member_model = model('member');
  103. //系统等级设置
  104. $membergrade_arr = $member_model->getMemberGradeArr();
  105. $mgdiscount_arr = array();
  106. foreach ($membergrade_arr as $key => $value) {
  107. $mgdiscount_arr[$key] = $value;
  108. $mgdiscount_arr[$key]['level_discount'] = isset($mgdiscount_arr_temp[$key]['level_discount']) ? $mgdiscount_arr_temp[$key]['level_discount'] : 10;
  109. }
  110. return $mgdiscount_arr;
  111. }
  112. //新增现有商品的折扣
  113. public function mgdiscount_goods_add() {
  114. $member_model = model('member');
  115. //系统等级设置
  116. $membergrade_arr = $member_model->getMemberGradeArr();
  117. if (!request()->isPost()) {
  118. View::assign('mgdiscount_goods_arr', $membergrade_arr);
  119. $this->setSellerCurMenu('Sellermgdiscount');
  120. $this->setSellerCurItem('mgdiscount_goods_add');
  121. return View::fetch($this->template_dir . 'mgdiscount_goods_add');
  122. } else {
  123. if (!check_platform_store()) {
  124. //获取当前套餐
  125. $mgdiscountquota_model = model('pmgdiscountquota');
  126. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  127. if (empty($current_mgdiscount_quota)) {
  128. if (intval(config('ds_config.mgdiscount_price')) != 0) {
  129. ds_json_encode(10001, lang('please_buy_package_first'));
  130. } else {
  131. $current_mgdiscount_quota = array('mgdiscountquota_starttime' => TIMESTAMP, 'mgdiscountquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  132. }
  133. }
  134. $quota_start_time = intval($current_mgdiscount_quota['mgdiscountquota_starttime']);
  135. $quota_end_time = intval($current_mgdiscount_quota['mgdiscountquota_endtime']);
  136. if (TIMESTAMP < $quota_start_time) {
  137. ds_json_encode(10001, sprintf(lang('mgdiscount_add_start_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_starttime'])));
  138. }
  139. if (TIMESTAMP > $quota_end_time) {
  140. ds_json_encode(10001, sprintf(lang('mgdiscount_add_end_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_endtime'])));
  141. }
  142. }
  143. //获取提交的数据
  144. $goods_id = intval(input('post.mgdiscount_goods_id'));
  145. if (empty($goods_id)) {
  146. ds_json_encode(10001, lang('param_error'));
  147. }
  148. $goods_model = model('goods');
  149. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  150. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  151. ds_json_encode(10001, lang('param_error'));
  152. }
  153. $data = array();
  154. $pre_level_discount = 0;
  155. foreach ($membergrade_arr as $key => $value) {
  156. $current_level_discount = floatval($_POST['mgdiscount_goods'][$key]['level_discount'] * 10);
  157. //限制会员等级高的会员享受更低折扣
  158. if ($pre_level_discount == 0) {
  159. $pre_level_discount = $current_level_discount;
  160. }
  161. if ($current_level_discount > $pre_level_discount) {
  162. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  163. }
  164. $pre_level_discount = $current_level_discount;
  165. if ($current_level_discount < 1 || $current_level_discount > 100) {
  166. ds_json_encode(10001, lang('current_level_discount_range_error'));
  167. }
  168. $data[$key]['level_discount'] = $current_level_discount / 10;
  169. $data[$key]['level_name'] = $value['level_name'];
  170. }
  171. $condition = array();
  172. $condition[] = array('goods_commonid','=',$goods_info['goods_commonid']);
  173. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => serialize($data)), $condition);
  174. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => serialize($data)), $condition);
  175. if ($result && $result1) {
  176. $this->recordSellerlog('添加会员等级折扣,公共商品ID:' . $goods_info['goods_commonid']);
  177. ds_json_encode(10000, lang('mgdiscount_add_success'));
  178. } else {
  179. ds_json_encode(10001, lang('mgdiscount_add_fail'));
  180. }
  181. }
  182. }
  183. function mgdiscount_goods_edit() {
  184. //获取提交的数据
  185. $goods_commonid = intval(input('param.goods_commonid'));
  186. if (empty($goods_commonid)) {
  187. ds_json_encode(10001, lang('param_error'));
  188. }
  189. $goods_model = model('goods');
  190. $goodscommon_info = $goods_model->getGoodsCommonInfoByID($goods_commonid);
  191. if (empty($goodscommon_info) || $goodscommon_info['store_id'] != session('store_id')) {
  192. ds_json_encode(10001, lang('goods_not_exist'));
  193. }
  194. if (!request()->isPost()) {
  195. View::assign('goodscommon_info', $goodscommon_info);
  196. View::assign('mgdiscount_goods_arr', $this->_get_mgdiscount_arr($goodscommon_info['goods_mgdiscount']));
  197. $this->setSellerCurMenu('Sellermgdiscount');
  198. $this->setSellerCurItem('mgdiscount_goods_add');
  199. return View::fetch($this->template_dir . 'mgdiscount_goods_add');
  200. } else {
  201. if (!check_platform_store()) {
  202. //获取当前套餐
  203. $mgdiscountquota_model = model('pmgdiscountquota');
  204. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  205. if (empty($current_mgdiscount_quota)) {
  206. if (intval(config('ds_config.mgdiscount_price')) != 0) {
  207. ds_json_encode(10001, lang('please_buy_package_first'));
  208. } else {
  209. $current_mgdiscount_quota = array('mgdiscountquota_starttime' => TIMESTAMP, 'mgdiscountquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  210. }
  211. }
  212. $quota_start_time = intval($current_mgdiscount_quota['mgdiscountquota_starttime']);
  213. $quota_end_time = intval($current_mgdiscount_quota['mgdiscountquota_endtime']);
  214. if ($quota_start_time < $quota_start_time) {
  215. ds_json_encode(10001, sprintf(lang('mgdiscount_add_start_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_starttime'])));
  216. }
  217. if ($quota_start_time > $quota_end_time) {
  218. ds_json_encode(10001, sprintf(lang('mgdiscount_add_end_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_endtime'])));
  219. }
  220. }
  221. $member_model = model('member');
  222. //系统等级设置
  223. $membergrade_arr = $member_model->getMemberGradeArr();
  224. $data = array();
  225. $pre_level_discount = 0;
  226. foreach ($membergrade_arr as $key => $value) {
  227. $current_level_discount = intval($_POST['mgdiscount_goods'][$key]['level_discount'] * 10);
  228. //限制会员等级高的会员享受更低折扣
  229. if ($pre_level_discount == 0) {
  230. $pre_level_discount = $current_level_discount;
  231. }
  232. if ($current_level_discount > $pre_level_discount) {
  233. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  234. }
  235. $pre_level_discount = $current_level_discount;
  236. if ($current_level_discount < 1 || $current_level_discount > 100) {
  237. ds_json_encode(10001, lang('current_level_discount_range_error'));
  238. }
  239. $data[$key]['level_discount'] = $current_level_discount / 10;
  240. $data[$key]['level_name'] = $value['level_name'];
  241. }
  242. $condition = array();
  243. $condition[] = array('goods_commonid','=',$goods_commonid);
  244. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => serialize($data)), $condition);
  245. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => serialize($data)), $condition);
  246. if ($result && $result1) {
  247. $this->recordSellerlog('添加会员等级折扣,公共商品ID:' . $goods_commonid);
  248. ds_json_encode(10000, lang('mgdiscount_add_success'));
  249. } else {
  250. ds_json_encode(10001, lang('mgdiscount_add_fail'));
  251. }
  252. }
  253. }
  254. /**
  255. * 删除
  256. */
  257. public function mgdiscount_del() {
  258. $goods_commonid = intval(input('param.goods_commonid'));
  259. if (empty($goods_commonid)) {
  260. ds_json_encode(10001, lang('param_error'));
  261. }
  262. $condition = array();
  263. $condition[] = array('goods_commonid','=',$goods_commonid);
  264. $goods_model = model('goods');
  265. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => ''), $condition);
  266. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => ''), $condition);
  267. if ($result && $result1) {
  268. ds_json_encode(10000, lang('ds_common_op_succ'));
  269. } else {
  270. ds_json_encode(10001, lang('ds_common_op_fail'));
  271. }
  272. }
  273. /**
  274. * 会员等级折扣套餐购买
  275. * */
  276. public function mgdiscount_quota_add() {
  277. //输出导航
  278. $this->setSellerCurMenu('Sellermgdiscount');
  279. $this->setSellerCurItem('mgdiscount_quota_add');
  280. return View::fetch($this->template_dir . 'mgdiscount_quota_add');
  281. }
  282. /**
  283. * 会员等级折扣套餐购买保存
  284. * */
  285. public function mgdiscount_quota_add_save() {
  286. if (intval(config('ds_config.mgdiscount_price')) == 0) {
  287. ds_json_encode(10001, lang('param_error'));
  288. }
  289. $mgdiscount_quota_quantity = intval(input('post.mgdiscount_quota_quantity'));
  290. if ($mgdiscount_quota_quantity <= 0 || $mgdiscount_quota_quantity > 12) {
  291. ds_json_encode(10001, lang('mgdiscount_quota_quantity_error'));
  292. }
  293. //获取当前价格
  294. $current_price = intval(config('ds_config.mgdiscount_price'));
  295. //获取该用户已有套餐
  296. $mgdiscountquota_model = model('pmgdiscountquota');
  297. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  298. $mgdiscount_add_time = 86400 * 30 * $mgdiscount_quota_quantity;
  299. if (empty($current_mgdiscount_quota)) {
  300. //生成套餐
  301. $param = array();
  302. $param['member_id'] = session('member_id');
  303. $param['member_name'] = session('member_name');
  304. $param['store_id'] = session('store_id');
  305. $param['store_name'] = session('store_name');
  306. $param['mgdiscountquota_starttime'] = TIMESTAMP;
  307. $param['mgdiscountquota_endtime'] = TIMESTAMP + $mgdiscount_add_time;
  308. $mgdiscountquota_model->addMgdiscountquota($param);
  309. } else {
  310. $param = array();
  311. $param['mgdiscountquota_endtime'] = Db::raw('mgdiscountquota_endtime+' . $mgdiscount_add_time);
  312. $mgdiscountquota_model->editMgdiscountquota($param, array('mgdiscountquota_id' => $current_mgdiscount_quota['mgdiscountquota_id']));
  313. }
  314. //记录店铺费用
  315. $this->recordStorecost($current_price * $mgdiscount_quota_quantity, '购买会员等级折扣');
  316. $this->recordSellerlog('购买' . $mgdiscount_quota_quantity . '份会员等级折扣套餐,单价' . $current_price . lang('ds_yuan'));
  317. ds_json_encode(10000, lang('mgdiscount_quota_add_success'));
  318. }
  319. /**
  320. * 选择活动商品
  321. * */
  322. public function search_goods() {
  323. $goods_model = model('goods');
  324. $condition = array();
  325. $condition[] = array('store_id', '=', session('store_id'));
  326. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  327. $goods_list = $goods_model->getGoodsCommonListForPromotion($condition, '*', 8, 'groupbuy');
  328. View::assign('goods_list', $goods_list);
  329. View::assign('show_page', $goods_model->page_info->render());
  330. echo View::fetch($this->template_dir . 'search_goods');
  331. exit;
  332. }
  333. public function mgdiscount_goods_info() {
  334. $goods_commonid = intval(input('param.goods_commonid'));
  335. $data = array();
  336. $data['result'] = true;
  337. //判断此商品是否已经参加会员等级折扣,
  338. $result = $this->_check_allow_mgdiscount($goods_commonid);
  339. if ($result['result'] != TRUE) {
  340. echo json_encode($result);
  341. die;
  342. }
  343. //获取商品具体信息用于显示
  344. $goods_model = model('goods');
  345. $condition = array();
  346. $condition[]=array('goods_commonid','=',$goods_commonid);
  347. $goods_list = $goods_model->getGoodsOnlineList($condition);
  348. if (empty($goods_list)) {
  349. $data['result'] = false;
  350. $data['message'] = lang('param_error');
  351. echo json_encode($data);
  352. die;
  353. }
  354. $goods_info = $goods_list[0];
  355. $data['goods_id'] = $goods_info['goods_id'];
  356. $data['goods_name'] = $goods_info['goods_name'];
  357. $data['goods_price'] = $goods_info['goods_price'];
  358. $data['goods_image'] = goods_thumb($goods_info, 240);
  359. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  360. echo json_encode($data);
  361. die;
  362. }
  363. /*
  364. * 判断此商品是否已经参加拼团
  365. */
  366. private function _check_allow_mgdiscount($goods_commonid) {
  367. $condition = array();
  368. $goodscommon_info = model('goods')->getGoodsCommonInfoByID($goods_commonid);
  369. $result['result'] = TRUE;
  370. if ($goodscommon_info['goods_mgdiscount'] != '') {
  371. $result['result'] = FALSE;
  372. $result['message'] = lang('goods_discount_already_set');
  373. }
  374. return $result;
  375. }
  376. /**
  377. * 用户中心右边,小导航
  378. *
  379. * @param string $menu_type 导航类型
  380. * @param string $name 当前导航的name
  381. * @param array $array 附加菜单
  382. * @return
  383. */
  384. protected function getSellerItemList() {
  385. $menu_array = array(
  386. array(
  387. 'name' => 'mgdiscount_store',
  388. 'text' => lang('mgdiscount_store'),
  389. 'url' => (string) url('Sellerpromotionmgdiscount/mgdiscount_store')
  390. ),
  391. array(
  392. 'name' => 'mgdiscount_goods',
  393. 'text' => lang('mgdiscount_goods'),
  394. 'url' => (string) url('Sellerpromotionmgdiscount/mgdiscount_goods')
  395. ),
  396. );
  397. return $menu_array;
  398. }
  399. }