Sellerpromotionmgdiscount.php 20 KB

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