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