Sellerpromotionbargain.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * 卖家砍价管理
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. use think\facade\Db;
  9. /**
  10. * ============================================================================
  11. * DSMall多用户商城
  12. * ============================================================================
  13. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  14. * 网站地址: http://www.csdeshang.com
  15. * ----------------------------------------------------------------------------
  16. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  17. * 不允许对程序代码以任何形式任何目的的再发布。
  18. * ============================================================================
  19. * 控制器
  20. */
  21. class Sellerpromotionbargain extends BaseSeller {
  22. public function initialize() {
  23. parent::initialize();
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionbargain.lang.php');
  25. if (intval(config('ds_config.promotion_allow')) !== 1) {
  26. $this->error(lang('promotion_unavailable'), 'seller/index');
  27. }
  28. }
  29. public function index() {
  30. $bargainquota_model = model('pbargainquota');
  31. $pbargain_model = model('pbargain');
  32. if (check_platform_store()) {
  33. View::assign('isPlatformStore', true);
  34. } else {
  35. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  36. View::assign('current_bargain_quota', $current_bargain_quota);
  37. }
  38. $condition = array();
  39. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  40. if ((input('param.bargain_name'))) {
  41. $condition[] = array('bargain_name', 'like', '%' . input('param.bargain_name') . '%');
  42. }
  43. if (input('param.state') != '' && in_array(input('param.state'), array(0, 1, 2, 3))) {
  44. $condition[] = array('bargain_state', '=', intval(input('param.state')));
  45. }
  46. $bargain_list = $pbargain_model->getBargainList($condition, 10, 'bargain_id desc');
  47. foreach ($bargain_list as $key => $val) {
  48. $bargain_list[$key]['bargain_state_text'] = $pbargain_model->getBargainStateText($val);
  49. $bargain_list[$key] = array_merge($bargain_list[$key], $pbargain_model->getBargainBtn($val));
  50. }
  51. View::assign('bargain_list', $bargain_list);
  52. View::assign('show_page', $pbargain_model->page_info->render());
  53. View::assign('bargain_state_array', $pbargain_model->getBargainStateArray());
  54. $this->setSellerCurMenu('Sellerpromotionbargain');
  55. $this->setSellerCurItem('bargain_list');
  56. return View::fetch($this->template_dir . 'index');
  57. }
  58. /**
  59. * 添加砍价活动
  60. * */
  61. public function bargain_add() {
  62. if (!request()->isPost()) {
  63. if (check_platform_store()) {
  64. View::assign('isPlatformStore', true);
  65. } else {
  66. View::assign('isPlatformStore', false);
  67. $bargainquota_model = model('pbargainquota');
  68. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  69. if (empty($current_bargain_quota)) {
  70. if (intval(config('ds_config.promotion_bargain_price')) != 0) {
  71. $this->error(lang('bargain_quota_current_error1'));
  72. } else {
  73. $current_bargain_quota = array('bargainquota_starttime' => TIMESTAMP, 'bargainquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  74. }
  75. }
  76. View::assign('current_bargain_quota', $current_bargain_quota);
  77. }
  78. //输出导航
  79. $this->setSellerCurMenu('Sellerpromotionbargain');
  80. $this->setSellerCurItem('bargain_add');
  81. return View::fetch($this->template_dir . 'bargain_add');
  82. } else {
  83. $data = $this->post_data();
  84. if (!$this->_check_allow_bargain($data['bargain_goods_id'])) {
  85. ds_json_encode(10001, lang('sellerpromotionbargain_goods_not_allow'));
  86. }
  87. $data['member_id'] = $this->store_info['member_id'];
  88. $data['member_name'] = $this->store_info['member_name'];
  89. $data['store_id'] = $this->store_info['store_id'];
  90. $data['store_name'] = $this->store_info['store_name'];
  91. if (!check_platform_store()) {
  92. //获取当前套餐
  93. $bargainquota_model = model('pbargainquota');
  94. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  95. if (empty($current_bargain_quota)) {
  96. if (intval(config('ds_config.promotion_bargain_price')) != 0) {
  97. ds_json_encode(10001, lang('please_buy_package_first'));
  98. } else {
  99. $current_bargain_quota = array('bargainquota_starttime' => TIMESTAMP, 'bargainquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  100. }
  101. }
  102. $quota_start_time = intval($current_bargain_quota['bargainquota_starttime']);
  103. $quota_end_time = intval($current_bargain_quota['bargainquota_endtime']);
  104. if ($data['bargain_begintime'] < $quota_start_time) {
  105. ds_json_encode(10001, sprintf(lang('bargain_add_start_time_explain'), date('Y-m-d', $current_bargain_quota['bargainquota_starttime'])));
  106. }
  107. if ($data['bargain_endtime'] > $quota_end_time) {
  108. ds_json_encode(10001, sprintf(lang('bargain_add_end_time_explain'), date('Y-m-d', $current_bargain_quota['bargainquota_endtime'])));
  109. }
  110. }
  111. //生成活动
  112. $pbargain_model = model('pbargain');
  113. $result = $pbargain_model->addBargain($data);
  114. if ($result) {
  115. $this->recordSellerlog(lang('add_group_activities') . $data['bargain_name'] . lang('activity_number') . $result);
  116. $pbargain_model->_dGoodsBargainCache($data['bargain_goods_id']); #清除缓存
  117. ds_json_encode(10000, lang('bargain_add_success'));
  118. } else {
  119. ds_json_encode(10001, lang('bargain_add_fail'));
  120. }
  121. }
  122. }
  123. public function post_data() {
  124. $data = array(
  125. 'bargain_name' => input('post.bargain_name'),
  126. 'bargain_limit' => input('post.bargain_limit'),
  127. 'bargain_time' => input('post.bargain_time'),
  128. 'bargain_floorprice' => input('post.bargain_floorprice'),
  129. 'bargain_total' => input('post.bargain_total'),
  130. 'bargain_max' => input('post.bargain_max'),
  131. 'bargain_remark' => input('post.bargain_remark'),
  132. 'bargain_begintime' => input('post.start_time'),
  133. 'bargain_endtime' => input('post.end_time'),
  134. );
  135. $pbargain_validate = ds_validate('pbargain');
  136. if (!$pbargain_validate->scene('pbargin_save')->check($data)) {
  137. ds_json_encode(10001, $pbargain_validate->getError());
  138. }
  139. //获取提交的数据
  140. $goods_id = intval(input('post.bargain_goods_id'));
  141. if (empty($goods_id)) {
  142. ds_json_encode(10001, lang('param_error'));
  143. }
  144. $goods_model = model('goods');
  145. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  146. if (empty($goods_info) || $goods_info['store_id'] != $this->store_info['store_id']) {
  147. ds_json_encode(10001, lang('param_error'));
  148. }
  149. $data['bargain_begintime'] = strtotime($data['bargain_begintime']);
  150. $data['bargain_endtime'] = strtotime($data['bargain_endtime']);
  151. if ($data['bargain_endtime'] <= TIMESTAMP) {
  152. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_endtime_error'));
  153. }
  154. if ($data['bargain_begintime'] <= TIMESTAMP) {
  155. $data['bargain_state'] = 2;
  156. } else {
  157. $data['bargain_state'] = 1;
  158. }
  159. $data['bargain_goods_id'] = $goods_info['goods_id'];
  160. $data['bargain_goods_name'] = $goods_info['goods_name'];
  161. $data['bargain_goods_price'] = $goods_info['goods_price'];
  162. $data['bargain_goods_image'] = $goods_info['goods_image'];
  163. $data['bargain_goods_commonid'] = $goods_info['goods_commonid'];
  164. //砍价差价*100必须大于底价刀数(因为1刀做少0.01元)
  165. if (($data['bargain_goods_price'] - $data['bargain_floorprice']) * 100 < $data['bargain_total']) {
  166. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_total_error'));
  167. }
  168. if (($data['bargain_goods_price'] - $data['bargain_floorprice']) > ($data['bargain_total'] * $data['bargain_max'])) {
  169. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_limit_error'));
  170. }
  171. return $data;
  172. }
  173. /**
  174. * 编辑砍价活动
  175. * */
  176. public function bargain_edit() {
  177. $bargain_id = input('param.bargain_id');
  178. $pbargain_model = model('pbargain');
  179. $bargain_info = $pbargain_model->getBargainInfoByID($bargain_id, $this->store_info['store_id']);
  180. $btn = $pbargain_model->getBargainBtn($bargain_info);
  181. if ($btn) {
  182. $bargain_info = array_merge($bargain_info, $btn);
  183. }
  184. if (!request()->isPost()) {
  185. if (check_platform_store()) {
  186. View::assign('isPlatformStore', true);
  187. } else {
  188. View::assign('isPlatformStore', false);
  189. }
  190. if (empty($bargain_info) || !$bargain_info['editable']) {
  191. $this->error(lang('param_error'));
  192. }
  193. View::assign('bargain_info', $bargain_info);
  194. //输出导航
  195. $this->setSellerCurMenu('Sellerpromotionbargain');
  196. $this->setSellerCurItem('bargain_edit');
  197. return View::fetch($this->template_dir . 'bargain_add');
  198. } else {
  199. if (empty($bargain_info) || !$bargain_info['editable']) {
  200. ds_json_encode(10001, lang('param_error'));
  201. }
  202. $data = $this->post_data();
  203. if (!$this->_check_allow_bargain($data['bargain_goods_id'], $bargain_info['bargain_id'])) {
  204. ds_json_encode(10001, lang('sellerpromotionbargain_goods_not_allow'));
  205. }
  206. $result = $pbargain_model->editBargain($data, array('bargain_id' => $bargain_id));
  207. if ($result) {
  208. $this->recordSellerlog(lang('edit_group_activities') . $data['bargain_name'] . lang('activity_number') . $bargain_id);
  209. $pbargain_model->_dGoodsBargainCache($data['bargain_goods_id']); #清除缓存
  210. ds_json_encode(10000, lang('ds_common_op_succ'));
  211. } else {
  212. ds_json_encode(10001, lang('ds_common_op_fail'));
  213. }
  214. }
  215. }
  216. /**
  217. * 砍价活动 取消
  218. */
  219. public function bargain_end() {
  220. $bargain_id = intval(input('post.bargain_id'));
  221. $pbargain_model = model('pbargain');
  222. $bargain_info = $pbargain_model->getBargainInfoByID($bargain_id, $this->store_info['store_id']);
  223. if (!$bargain_info) {
  224. ds_json_encode(10001, lang('param_error'));
  225. }
  226. $btn = $pbargain_model->getBargainBtn($bargain_info);
  227. if (!$btn['editable']) {
  228. ds_json_encode(10001, lang('param_error'));
  229. }
  230. /**
  231. * 指定砍价活动结束
  232. */
  233. $condition = array();
  234. $condition[] = array('bargain_id','=',$bargain_id);
  235. $condition[] = array('bargain_state','=',1);
  236. $result = $pbargain_model->cancelBargain($condition);
  237. if ($result) {
  238. $this->recordSellerlog(lang('group_activities_end_early') . $bargain_info['bargain_name'] . lang('activity_number') . $bargain_id);
  239. $pbargain_model->_dGoodsBargainCache($bargain_info['bargain_goods_id']); #清除缓存
  240. ds_json_encode(10000, lang('ds_common_op_succ'));
  241. } else {
  242. ds_json_encode(10001, lang('ds_common_op_fail'));
  243. }
  244. }
  245. /**
  246. * 商品砍价订单列表
  247. */
  248. public function bargain_order() {
  249. $pbargain_model = model('pbargain');
  250. $pbargainorder_model = model('pbargainorder');
  251. $bargain_id = intval(input('param.bargain_id'));
  252. //判断此砍价是否属于店铺
  253. $pbargain = $pbargain_model->getBargainInfo(array('store_id' => $this->store_info['store_id'], 'bargain_id' => $bargain_id));
  254. if (empty($pbargain)) {
  255. $this->error(lang('param_error'));
  256. }
  257. $condition = array();
  258. $condition[] = array('bargain_id','=',$bargain_id);
  259. if (input('param.bargainorder_state') != '') {
  260. $condition[] = array('bargainorder_state','=',intval(input('param.bargainorder_state')));
  261. }
  262. $pbargainorder_list = $pbargainorder_model->getPbargainorderList($condition, 10);
  263. View::assign('show_page', $pbargainorder_model->page_info->render());
  264. View::assign('pbargainorder_list', $pbargainorder_list);
  265. View::assign('bargainorder_state_array', $pbargainorder_model->getBargainorderStateArray());
  266. $this->setSellerCurMenu('Sellerpromotionbargain');
  267. $this->setSellerCurItem('bargain_order');
  268. return View::fetch($this->template_dir . 'bargain_order');
  269. }
  270. /**
  271. * 商品砍价记录列表
  272. */
  273. public function bargain_log() {
  274. $pbargainlog_model = model('pbargainlog');
  275. $bargainorder_id = intval(input('param.bargainorder_id'));
  276. $condition = array();
  277. $condition[] = array('bargainorder_id','=',$bargainorder_id);
  278. $pbargainlog_list = $pbargainlog_model->getPbargainlogList($condition, 10); #获取砍价记录信息
  279. View::assign('show_page', $pbargainlog_model->page_info->render());
  280. View::assign('pbargainlog_list', $pbargainlog_list);
  281. $this->setSellerCurMenu('Sellerpromotionbargain');
  282. $this->setSellerCurItem('bargain_log');
  283. return View::fetch($this->template_dir . 'bargain_log');
  284. }
  285. /**
  286. * 砍价套餐购买
  287. * */
  288. public function bargain_quota_add() {
  289. //输出导航
  290. $this->setSellerCurMenu('Sellerpromotionbargain');
  291. $this->setSellerCurItem('bargain_quota_add');
  292. return View::fetch($this->template_dir . 'bargain_quota_add');
  293. }
  294. /**
  295. * 砍价套餐购买保存
  296. * */
  297. public function bargain_quota_add_save() {
  298. if (intval(config('ds_config.promotion_bargain_price')) == 0) {
  299. ds_json_encode(10001, lang('param_error'));
  300. }
  301. $bargain_quota_quantity = intval(input('post.bargain_quota_quantity'));
  302. if ($bargain_quota_quantity <= 0 || $bargain_quota_quantity > 12) {
  303. ds_json_encode(10001, lang('bargain_quota_quantity_error'));
  304. }
  305. //获取当前价格
  306. $current_price = intval(config('ds_config.promotion_bargain_price'));
  307. //获取该用户已有套餐
  308. $bargainquota_model = model('pbargainquota');
  309. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  310. $bargain_add_time = 86400 * 30 * $bargain_quota_quantity;
  311. if (empty($current_bargain_quota)) {
  312. //生成套餐
  313. $param = array();
  314. $param['member_id'] = session('member_id');
  315. $param['member_name'] = session('member_name');
  316. $param['store_id'] = $this->store_info['store_id'];
  317. $param['store_name'] = session('store_name');
  318. $param['bargainquota_starttime'] = TIMESTAMP;
  319. $param['bargainquota_endtime'] = TIMESTAMP + $bargain_add_time;
  320. $bargainquota_model->addBargainquota($param);
  321. } else {
  322. $param = array();
  323. $param['bargainquota_endtime'] = Db::raw('bargainquota_endtime+' . $bargain_add_time);
  324. $bargainquota_model->editBargainquota($param, array('bargainquota_id' => $current_bargain_quota['bargainquota_id']));
  325. }
  326. //记录店铺费用
  327. $this->recordStorecost($current_price * $bargain_quota_quantity, lang('buy_spell_group'));
  328. $this->recordSellerlog(lang('buy') . $bargain_quota_quantity . lang('combo_pack') . $current_price . lang('ds_yuan'));
  329. ds_json_encode(10000, lang('bargain_quota_add_success'));
  330. }
  331. /**
  332. * 选择活动商品
  333. * */
  334. public function search_goods() {
  335. $goods_model = model('goods');
  336. $condition = array();
  337. $condition[] = array('goods.store_id', '=', $this->store_info['store_id']);
  338. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  339. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 8, 'bargain');
  340. View::assign('goods_list', $goods_list);
  341. View::assign('show_page', $goods_model->page_info->render());
  342. echo View::fetch($this->template_dir . 'search_goods');
  343. exit;
  344. }
  345. public function bargain_goods_info() {
  346. $goods_id = intval(input('param.goods_id'));
  347. $data = array();
  348. $data['result'] = true;
  349. //获取商品具体信息用于显示
  350. $goods_model = model('goods');
  351. $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);
  352. if (empty($goods_info)) {
  353. $data['result'] = false;
  354. $data['message'] = lang('param_error');
  355. echo json_encode($data);
  356. die;
  357. }
  358. $data['goods_id'] = $goods_info['goods_id'];
  359. $data['goods_name'] = $goods_info['goods_name'];
  360. $data['goods_price'] = $goods_info['goods_price'];
  361. $data['goods_image'] = goods_thumb($goods_info, 240);
  362. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  363. echo json_encode($data);
  364. die;
  365. }
  366. /*
  367. * 判断此商品是否已经参加砍价
  368. */
  369. private function _check_allow_bargain($goods_id, $bargain_id = 0) {
  370. $condition = array();
  371. $condition[] = array('bargain_goods_id','=',$goods_id);
  372. $condition[] = array('bargain_state','=',1);
  373. if ($bargain_id) {
  374. $condition[] = array('bargain_id', '<>', $bargain_id);
  375. }
  376. $bargain = model('pbargain')->getBargainInfo($condition);
  377. if ($bargain) {
  378. return false;
  379. } else {
  380. return true;
  381. }
  382. }
  383. protected function getSellerItemList() {
  384. $menu_array = array(
  385. array(
  386. 'name' => 'bargain_list', 'text' => lang('bargain_active_list'),
  387. 'url' => (string) url('Sellerpromotionbargain/index')
  388. ),
  389. );
  390. switch (request()->action()) {
  391. case 'bargain_add':
  392. $menu_array[] = array(
  393. 'name' => 'bargain_add', 'text' => lang('bargain_add'),
  394. 'url' => (string) url('Sellerpromotionbargain/bargain_add')
  395. );
  396. break;
  397. case 'bargain_edit':
  398. $menu_array[] = array(
  399. 'name' => 'bargain_edit', 'text' => lang('bargain_edit'), 'url' => 'javascript:;'
  400. );
  401. break;
  402. case 'bargain_quota_add':
  403. $menu_array[] = array(
  404. 'name' => 'bargain_quota_add', 'text' => lang('bargain_quota_add'),
  405. 'url' => (string) url('Sellerpromotionbargain/bargain_quota_add')
  406. );
  407. break;
  408. case 'bargain_order':
  409. $menu_array[] = array(
  410. 'name' => 'bargain_order', 'text' => lang('bargainorder'),
  411. 'url' => (string) url('Sellerpromotionbargain/bargain_order', ['bargain_id' => input('param.bargain_id')])
  412. );
  413. break;
  414. case 'bargain_log':
  415. $menu_array[] = array(
  416. 'name' => 'bargain_log', 'text' => lang('pbargainlog'),
  417. 'url' => (string) url('Sellerpromotionbargain/bargain_log', ['bargainorder_id' => input('param.bargainorder_id')])
  418. );
  419. break;
  420. }
  421. return $menu_array;
  422. }
  423. }