Sellerpromotionpresell.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. *
  12. * ============================================================================
  13. *
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Sellerpromotionpresell extends BaseSeller
  20. {
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionpresell.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. {
  31. $presellquota_model = model('presellquota');
  32. $presell_model = model('presell');
  33. if (check_platform_store()) {
  34. View::assign('isPlatformStore', true);
  35. } else {
  36. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  37. View::assign('current_presell_quota', $current_presell_quota);
  38. }
  39. $condition = array();
  40. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  41. if ((input('param.goods_name'))) {
  42. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  43. }
  44. if (input('param.state') != '' && in_array(input('param.state'), array(0, 1, 2, 3))) {
  45. $condition[] = array('presell_state', '=', intval(input('param.state')));
  46. }
  47. $presell_list = $presell_model->getPresellList($condition, 10, 'presell_id desc');
  48. foreach ($presell_list as $key => $val) {
  49. $presell_list[$key]['presell_state_text'] = $presell_model->getPresellStateText($val);
  50. $presell_list[$key] = array_merge($presell_list[$key], $presell_model->getPresellBtn($val));
  51. }
  52. View::assign('presell_list', $presell_list);
  53. View::assign('show_page', $presell_model->page_info->render());
  54. View::assign('presell_state_array', $presell_model->getPresellStateArray());
  55. $this->setSellerCurMenu('Sellerpromotionpresell');
  56. $this->setSellerCurItem('presell_list');
  57. return View::fetch($this->template_dir . 'index');
  58. }
  59. /**
  60. * 添加预售活动
  61. * */
  62. public function presell_add()
  63. {
  64. if (!request()->isPost()) {
  65. if (check_platform_store()) {
  66. View::assign('isPlatformStore', true);
  67. } else {
  68. View::assign('isPlatformStore', false);
  69. $presellquota_model = model('presellquota');
  70. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  71. if (empty($current_presell_quota)) {
  72. if (intval(config('ds_config.promotion_presell_price')) != 0) {
  73. $this->error(lang('presell_quota_current_error1'));
  74. } else {
  75. $current_presell_quota = array('presellquota_starttime' => TIMESTAMP, 'presellquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  76. }
  77. }
  78. View::assign('current_presell_quota', $current_presell_quota);
  79. }
  80. //输出导航
  81. $this->setSellerCurMenu('Sellerpromotionpresell');
  82. $this->setSellerCurItem('presell_add');
  83. return View::fetch($this->template_dir . 'presell_add');
  84. } else {
  85. $data = array(
  86. 'goods_id' => intval(input('post.presell_goods_id')),
  87. 'presell_type' => intval(input('post.presell_type')),
  88. 'presell_deposit_amount' => floatval(input('post.presell_deposit_amount')),
  89. 'presell_price' => floatval(input('post.presell_price')),
  90. 'presell_start_time' => input('post.start_time'),
  91. 'presell_end_time' => input('post.end_time'),
  92. 'presell_shipping_time' => input('post.presell_shipping_time'),
  93. );
  94. $presell_validate = ds_validate('presell');
  95. if (!$presell_validate->scene('presell_add')->check($data)) {
  96. ds_json_encode(10001, $presell_validate->getError());
  97. }
  98. //获取提交的数据
  99. $goods_id = $data['goods_id'];
  100. $goods_model = model('goods');
  101. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  102. if (empty($goods_info) || $goods_info['store_id'] != $this->store_info['store_id']) {
  103. ds_json_encode(10001, lang('param_error'));
  104. }
  105. if ($data['presell_price'] >= $goods_info['goods_price']) {
  106. ds_json_encode(10001, lang('presell_price_error'));
  107. }
  108. $data['presell_start_time'] = strtotime($data['presell_start_time']);
  109. $data['presell_end_time'] = strtotime($data['presell_end_time']);
  110. if ($data['presell_end_time'] <= TIMESTAMP || $data['presell_end_time'] < $data['presell_start_time']) {
  111. ds_json_encode(10001, lang('greater_than_start_time'));
  112. }
  113. if ($data['presell_start_time'] <= TIMESTAMP) {
  114. $data['presell_state'] = 2;
  115. } else {
  116. $data['presell_state'] = 1;
  117. }
  118. $data['goods_id'] = $goods_info['goods_id'];
  119. $data['goods_name'] = $goods_info['goods_name'];
  120. $data['goods_commonid'] = $goods_info['goods_commonid'];
  121. if ($data['presell_type'] == 1) { //全款预售
  122. $data['presell_shipping_time'] = strtotime($data['presell_shipping_time']);
  123. if ($data['presell_shipping_time'] <= TIMESTAMP || $data['presell_shipping_time'] < $data['presell_end_time']) {
  124. ds_json_encode(10001, lang('greater_than_end_time'));
  125. }
  126. $data['presell_deposit_amount'] = 0;
  127. } else {
  128. $data['presell_shipping_time'] = 0;
  129. if ($data['presell_deposit_amount'] > 0.2 * $data['presell_price']) {
  130. ds_json_encode(10001, lang('presell_deposit_amount_explain'));
  131. }
  132. }
  133. if (!$this->_check_allow_presell($data['goods_id'])) {
  134. ds_json_encode(10001, lang('sellerpromotionpresell_goods_not_allow'));
  135. }
  136. $data['member_id'] = $this->store_info['member_id'];
  137. $data['member_name'] = $this->store_info['member_name'];
  138. $data['store_id'] = $this->store_info['store_id'];
  139. $data['store_name'] = $this->store_info['store_name'];
  140. if (!check_platform_store()) {
  141. //获取当前套餐
  142. $presellquota_model = model('presellquota');
  143. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  144. if (empty($current_presell_quota)) {
  145. if (intval(config('ds_config.promotion_presell_price')) != 0) {
  146. ds_json_encode(10001, lang('please_buy_package_first'));
  147. } else {
  148. $current_presell_quota = array('presellquota_starttime' => TIMESTAMP, 'presellquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  149. }
  150. }
  151. $quota_start_time = intval($current_presell_quota['presellquota_starttime']);
  152. $quota_end_time = intval($current_presell_quota['presellquota_endtime']);
  153. if ($data['presell_start_time'] < $quota_start_time) {
  154. ds_json_encode(10001, sprintf(lang('presell_add_start_time_explain'), date('Y-m-d', $current_presell_quota['presellquota_starttime'])));
  155. }
  156. if ($data['presell_end_time'] > $quota_end_time) {
  157. ds_json_encode(10001, sprintf(lang('presell_add_end_time_explain'), date('Y-m-d', $current_presell_quota['presellquota_endtime'])));
  158. }
  159. }
  160. //生成活动
  161. $presell_model = model('presell');
  162. $result = $presell_model->addPresell($data);
  163. if ($result) {
  164. $this->recordSellerlog(lang('add_group_activities') . $data['goods_name'] . lang('activity_number') . $result);
  165. $presell_model->_dGoodsPresellCache($data['goods_id']); #清除缓存
  166. ds_json_encode(10000, lang('presell_add_success'));
  167. } else {
  168. ds_json_encode(10001, lang('presell_add_fail'));
  169. }
  170. }
  171. }
  172. /**
  173. * 编辑预售活动
  174. * */
  175. public function presell_edit()
  176. {
  177. $presell_id = input('param.presell_id');
  178. $presell_model = model('presell');
  179. $presell_info = $presell_model->getPresellInfoByID($presell_id, $this->store_info['store_id']);
  180. $btn = $presell_model->getPresellBtn($presell_info);
  181. if ($btn) {
  182. $presell_info = array_merge($presell_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($presell_info) || !$presell_info['editable']) {
  191. $this->error(lang('param_error'));
  192. }
  193. View::assign('presell_info', $presell_info);
  194. //输出导航
  195. $this->setSellerCurMenu('Sellerpromotionpresell');
  196. $this->setSellerCurItem('presell_edit');
  197. return View::fetch($this->template_dir . 'presell_add');
  198. } else {
  199. if (empty($presell_info) || !$presell_info['editable']) {
  200. ds_json_encode(10001, lang('param_error'));
  201. }
  202. $data = array(
  203. 'presell_deposit_amount' => floatval(input('post.presell_deposit_amount')),
  204. 'presell_price' => floatval(input('post.presell_price')),
  205. 'presell_shipping_time' => input('post.presell_shipping_time'),
  206. );
  207. $presell_validate = ds_validate('presell');
  208. if (!$presell_validate->scene('presell_edit')->check($data)) {
  209. ds_json_encode(10001, $presell_validate->getError());
  210. }
  211. //获取提交的数据
  212. $goods_id = $presell_info['goods_id'];
  213. $goods_model = model('goods');
  214. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  215. if (empty($goods_info) || $goods_info['store_id'] != $this->store_info['store_id']) {
  216. ds_json_encode(10001, lang('param_error'));
  217. }
  218. if ($data['presell_price'] >= $goods_info['goods_price']) {
  219. ds_json_encode(10001, lang('presell_price_error'));
  220. }
  221. if ($presell_info['presell_type'] == 1) { //全款预售
  222. $data['presell_shipping_time'] = strtotime($data['presell_shipping_time']);
  223. if ($data['presell_shipping_time'] <= TIMESTAMP || $data['presell_shipping_time'] < $presell_info['presell_end_time']) {
  224. ds_json_encode(10001, lang('greater_than_end_time'));
  225. }
  226. } else {
  227. if ($data['presell_deposit_amount'] > 0.2 * $data['presell_price']) {
  228. ds_json_encode(10001, lang('presell_deposit_amount_explain'));
  229. }
  230. }
  231. $result = $presell_model->editPresell($data, array('presell_id' => $presell_id));
  232. if ($result) {
  233. $this->recordSellerlog(lang('edit_group_activities') . $presell_info['goods_name'] . lang('activity_number') . $presell_id);
  234. $presell_model->_dGoodsPresellCache($goods_id); #清除缓存
  235. ds_json_encode(10000, lang('ds_common_op_succ'));
  236. } else {
  237. ds_json_encode(10001, lang('ds_common_op_fail'));
  238. }
  239. }
  240. }
  241. /**
  242. * 预售活动 取消
  243. */
  244. public function presell_end()
  245. {
  246. $presell_id = intval(input('post.presell_id'));
  247. $presell_model = model('presell');
  248. $presell_info = $presell_model->getPresellInfoByID($presell_id, $this->store_info['store_id']);
  249. if (!$presell_info) {
  250. ds_json_encode(10001, lang('param_error'));
  251. }
  252. $btn = $presell_model->getPresellBtn($presell_info);
  253. if (!$btn['editable']) {
  254. ds_json_encode(10001, lang('param_error'));
  255. }
  256. $condition = array();
  257. $condition[] = array('presell_id', '=', $presell_id);
  258. $condition[] = array('presell_state', '=', 1);
  259. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  260. $result = $presell_model->cancelPresell($condition);
  261. if ($result) {
  262. $this->recordSellerlog(lang('group_activities_end_early') . $presell_info['goods_name'] . lang('activity_number') . $presell_id);
  263. $presell_model->_dGoodsPresellCache($presell_info['goods_id']); #清除缓存
  264. ds_json_encode(10000, lang('ds_common_op_succ'));
  265. } else {
  266. ds_json_encode(10001, lang('ds_common_op_fail'));
  267. }
  268. }
  269. /**
  270. * 预售套餐购买
  271. * */
  272. public function presell_quota_add()
  273. {
  274. //输出导航
  275. $this->setSellerCurMenu('Sellerpromotionpresell');
  276. $this->setSellerCurItem('presell_quota_add');
  277. return View::fetch($this->template_dir . 'presell_quota_add');
  278. }
  279. /**
  280. * 预售套餐购买保存
  281. * */
  282. public function presell_quota_add_save()
  283. {
  284. if (intval(config('ds_config.promotion_presell_price')) == 0) {
  285. ds_json_encode(10001, lang('param_error'));
  286. }
  287. $presell_quota_quantity = intval(input('post.presell_quota_quantity'));
  288. if ($presell_quota_quantity <= 0 || $presell_quota_quantity > 12) {
  289. ds_json_encode(10001, lang('presell_quota_quantity_error'));
  290. }
  291. //获取当前价格
  292. $current_price = intval(config('ds_config.promotion_presell_price'));
  293. //获取该用户已有套餐
  294. $presellquota_model = model('presellquota');
  295. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  296. $presell_add_time = 86400 * 30 * $presell_quota_quantity;
  297. if (empty($current_presell_quota)) {
  298. //生成套餐
  299. $param = array();
  300. $param['member_id'] = session('member_id');
  301. $param['member_name'] = session('member_name');
  302. $param['store_id'] = $this->store_info['store_id'];
  303. $param['store_name'] = session('store_name');
  304. $param['presellquota_starttime'] = TIMESTAMP;
  305. $param['presellquota_endtime'] = TIMESTAMP + $presell_add_time;
  306. $presellquota_model->addPresellquota($param);
  307. } else {
  308. $param = array();
  309. $param['presellquota_endtime'] = Db::raw('presellquota_endtime+' . $presell_add_time);
  310. $presellquota_model->editPresellquota($param, array('presellquota_id' => $current_presell_quota['presellquota_id']));
  311. }
  312. //记录店铺费用
  313. $this->recordStorecost($current_price * $presell_quota_quantity, lang('buy_spell_group'));
  314. $this->recordSellerlog(lang('buy') . $presell_quota_quantity . lang('combo_pack') . $current_price . lang('ds_yuan'));
  315. ds_json_encode(10000, lang('presell_quota_add_success'));
  316. }
  317. /**
  318. * 选择活动商品
  319. * */
  320. public function search_goods()
  321. {
  322. $goods_model = model('goods');
  323. $condition = array();
  324. $condition[] = array('goods.store_id', '=', $this->store_info['store_id']);
  325. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  326. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 8, 'presell');
  327. View::assign('goods_list', $goods_list);
  328. View::assign('show_page', $goods_model->page_info->render());
  329. echo View::fetch($this->template_dir . 'search_goods');
  330. exit;
  331. }
  332. public function presell_goods_info()
  333. {
  334. $goods_id = intval(input('param.goods_id'));
  335. $data = array();
  336. $data['result'] = true;
  337. //获取商品具体信息用于显示
  338. $goods_model = model('goods');
  339. $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);
  340. if (empty($goods_info)) {
  341. $data['result'] = false;
  342. $data['message'] = lang('param_error');
  343. echo json_encode($data);
  344. die;
  345. }
  346. $data['goods_id'] = $goods_info['goods_id'];
  347. $data['goods_name'] = $goods_info['goods_name'];
  348. $data['goods_price'] = $goods_info['goods_price'];
  349. $data['goods_image'] = goods_thumb($goods_info, 240);
  350. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  351. echo json_encode($data);
  352. die;
  353. }
  354. /*
  355. * 判断此商品是否已经参加预售
  356. */
  357. private function _check_allow_presell($goods_id, $presell_id = 0)
  358. {
  359. $condition = array();
  360. $condition[] = array('goods_id', '=', $goods_id);
  361. $condition[] = array('presell_state', '=', 1);
  362. if ($presell_id) {
  363. $condition[] = array('presell_id', '<>', $presell_id);
  364. }
  365. $presell = model('presell')->getPresellInfo($condition);
  366. if ($presell) {
  367. return false;
  368. } else {
  369. return true;
  370. }
  371. }
  372. protected function getSellerItemList()
  373. {
  374. $menu_array = array(
  375. array(
  376. 'name' => 'presell_list', 'text' => lang('presell_active_list'),
  377. 'url' => (string) url('Sellerpromotionpresell/index')
  378. ),
  379. );
  380. switch (request()->action()) {
  381. case 'presell_add':
  382. $menu_array[] = array(
  383. 'name' => 'presell_add', 'text' => lang('presell_add'),
  384. 'url' => (string) url('Sellerpromotionpresell/presell_add')
  385. );
  386. break;
  387. case 'presell_edit':
  388. $menu_array[] = array(
  389. 'name' => 'presell_edit', 'text' => lang('presell_edit'), 'url' => 'javascript:;'
  390. );
  391. break;
  392. case 'presell_quota_add':
  393. $menu_array[] = array(
  394. 'name' => 'presell_quota_add', 'text' => lang('presell_quota_add'),
  395. 'url' => (string) url('Sellerpromotionpresell/presell_quota_add')
  396. );
  397. break;
  398. }
  399. return $menu_array;
  400. }
  401. }