Sellerpromotionpresell.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Sellerpromotionpresell extends BaseSeller {
  21. public function initialize() {
  22. parent::initialize();
  23. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionpresell.lang.php');
  24. if (intval(config('ds_config.promotion_allow')) !== 1) {
  25. $this->error(lang('promotion_unavailable'), 'seller/index');
  26. }
  27. }
  28. public function index() {
  29. $presellquota_model = model('presellquota');
  30. $presell_model = model('presell');
  31. if (check_platform_store()) {
  32. View::assign('isPlatformStore', true);
  33. } else {
  34. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  35. View::assign('current_presell_quota', $current_presell_quota);
  36. }
  37. $condition = array();
  38. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  39. if ((input('param.goods_name'))) {
  40. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  41. }
  42. if (input('param.state') != '' && in_array(input('param.state'), array(0, 1, 2, 3))) {
  43. $condition[] = array('presell_state', '=', intval(input('param.state')));
  44. }
  45. $presell_list = $presell_model->getPresellList($condition, 10, 'presell_id desc');
  46. foreach ($presell_list as $key => $val) {
  47. $presell_list[$key]['presell_state_text'] = $presell_model->getPresellStateText($val);
  48. $presell_list[$key] = array_merge($presell_list[$key], $presell_model->getPresellBtn($val));
  49. }
  50. View::assign('presell_list', $presell_list);
  51. View::assign('show_page', $presell_model->page_info->render());
  52. View::assign('presell_state_array', $presell_model->getPresellStateArray());
  53. $this->setSellerCurMenu('Sellerpromotionpresell');
  54. $this->setSellerCurItem('presell_list');
  55. return View::fetch($this->template_dir . 'index');
  56. }
  57. /**
  58. * 添加预售活动
  59. * */
  60. public function presell_add() {
  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. $presell_id = input('param.presell_id');
  174. $presell_model = model('presell');
  175. $presell_info = $presell_model->getPresellInfoByID($presell_id, $this->store_info['store_id']);
  176. $btn = $presell_model->getPresellBtn($presell_info);
  177. if ($btn) {
  178. $presell_info = array_merge($presell_info, $btn);
  179. }
  180. if (!request()->isPost()) {
  181. if (check_platform_store()) {
  182. View::assign('isPlatformStore', true);
  183. } else {
  184. View::assign('isPlatformStore', false);
  185. }
  186. if (empty($presell_info) || !$presell_info['editable']) {
  187. $this->error(lang('param_error'));
  188. }
  189. View::assign('presell_info', $presell_info);
  190. //输出导航
  191. $this->setSellerCurMenu('Sellerpromotionpresell');
  192. $this->setSellerCurItem('presell_edit');
  193. return View::fetch($this->template_dir . 'presell_add');
  194. } else {
  195. if (empty($presell_info) || !$presell_info['editable']) {
  196. ds_json_encode(10001, lang('param_error'));
  197. }
  198. $data = array(
  199. 'presell_deposit_amount' => floatval(input('post.presell_deposit_amount')),
  200. 'presell_price' => floatval(input('post.presell_price')),
  201. 'presell_shipping_time' => input('post.presell_shipping_time'),
  202. );
  203. $presell_validate = ds_validate('presell');
  204. if (!$presell_validate->scene('presell_edit')->check($data)) {
  205. ds_json_encode(10001, $presell_validate->getError());
  206. }
  207. //获取提交的数据
  208. $goods_id = $presell_info['goods_id'];
  209. $goods_model = model('goods');
  210. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  211. if (empty($goods_info) || $goods_info['store_id'] != $this->store_info['store_id']) {
  212. ds_json_encode(10001, lang('param_error'));
  213. }
  214. if ($data['presell_price'] >= $goods_info['goods_price']) {
  215. ds_json_encode(10001, lang('presell_price_error'));
  216. }
  217. if ($presell_info['presell_type'] == 1) {//全款预售
  218. $data['presell_shipping_time'] = strtotime($data['presell_shipping_time']);
  219. if ($data['presell_shipping_time'] <= TIMESTAMP || $data['presell_shipping_time'] < $presell_info['presell_end_time']) {
  220. ds_json_encode(10001, lang('greater_than_end_time'));
  221. }
  222. }else{
  223. if($data['presell_deposit_amount']>0.2*$data['presell_price']){
  224. ds_json_encode(10001, lang('presell_deposit_amount_explain'));
  225. }
  226. }
  227. $result = $presell_model->editPresell($data, array('presell_id' => $presell_id));
  228. if ($result) {
  229. $this->recordSellerlog(lang('edit_group_activities') . $presell_info['goods_name'] . lang('activity_number') . $presell_id);
  230. $presell_model->_dGoodsPresellCache($goods_id); #清除缓存
  231. ds_json_encode(10000, lang('ds_common_op_succ'));
  232. } else {
  233. ds_json_encode(10001, lang('ds_common_op_fail'));
  234. }
  235. }
  236. }
  237. /**
  238. * 预售活动 取消
  239. */
  240. public function presell_end() {
  241. $presell_id = intval(input('post.presell_id'));
  242. $presell_model = model('presell');
  243. $presell_info = $presell_model->getPresellInfoByID($presell_id, $this->store_info['store_id']);
  244. if (!$presell_info) {
  245. ds_json_encode(10001, lang('param_error'));
  246. }
  247. $btn = $presell_model->getPresellBtn($presell_info);
  248. if (!$btn['editable']) {
  249. ds_json_encode(10001, lang('param_error'));
  250. }
  251. $condition = array();
  252. $condition[] = array('presell_id', '=', $presell_id);
  253. $condition[] = array('presell_state', '=', 1);
  254. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  255. $result = $presell_model->cancelPresell($condition);
  256. if ($result) {
  257. $this->recordSellerlog(lang('group_activities_end_early') . $presell_info['goods_name'] . lang('activity_number') . $presell_id);
  258. $presell_model->_dGoodsPresellCache($presell_info['goods_id']); #清除缓存
  259. ds_json_encode(10000, lang('ds_common_op_succ'));
  260. } else {
  261. ds_json_encode(10001, lang('ds_common_op_fail'));
  262. }
  263. }
  264. /**
  265. * 预售套餐购买
  266. * */
  267. public function presell_quota_add() {
  268. //输出导航
  269. $this->setSellerCurMenu('Sellerpromotionpresell');
  270. $this->setSellerCurItem('presell_quota_add');
  271. return View::fetch($this->template_dir . 'presell_quota_add');
  272. }
  273. /**
  274. * 预售套餐购买保存
  275. * */
  276. public function presell_quota_add_save() {
  277. if (intval(config('ds_config.promotion_presell_price')) == 0) {
  278. ds_json_encode(10001, lang('param_error'));
  279. }
  280. $presell_quota_quantity = intval(input('post.presell_quota_quantity'));
  281. if ($presell_quota_quantity <= 0 || $presell_quota_quantity > 12) {
  282. ds_json_encode(10001, lang('presell_quota_quantity_error'));
  283. }
  284. //获取当前价格
  285. $current_price = intval(config('ds_config.promotion_presell_price'));
  286. //获取该用户已有套餐
  287. $presellquota_model = model('presellquota');
  288. $current_presell_quota = $presellquota_model->getPresellquotaCurrent($this->store_info['store_id']);
  289. $presell_add_time = 86400 * 30 * $presell_quota_quantity;
  290. if (empty($current_presell_quota)) {
  291. //生成套餐
  292. $param = array();
  293. $param['member_id'] = session('member_id');
  294. $param['member_name'] = session('member_name');
  295. $param['store_id'] = $this->store_info['store_id'];
  296. $param['store_name'] = session('store_name');
  297. $param['presellquota_starttime'] = TIMESTAMP;
  298. $param['presellquota_endtime'] = TIMESTAMP + $presell_add_time;
  299. $presellquota_model->addPresellquota($param);
  300. } else {
  301. $param = array();
  302. $param['presellquota_endtime'] = Db::raw('presellquota_endtime+' . $presell_add_time);
  303. $presellquota_model->editPresellquota($param, array('presellquota_id' => $current_presell_quota['presellquota_id']));
  304. }
  305. //记录店铺费用
  306. $this->recordStorecost($current_price * $presell_quota_quantity, lang('buy_spell_group'));
  307. $this->recordSellerlog(lang('buy') . $presell_quota_quantity . lang('combo_pack') . $current_price . lang('ds_yuan'));
  308. ds_json_encode(10000, lang('presell_quota_add_success'));
  309. }
  310. /**
  311. * 选择活动商品
  312. * */
  313. public function search_goods() {
  314. $goods_model = model('goods');
  315. $condition = array();
  316. $condition[] = array('goods.store_id', '=', $this->store_info['store_id']);
  317. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  318. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 8, 'presell');
  319. View::assign('goods_list', $goods_list);
  320. View::assign('show_page', $goods_model->page_info->render());
  321. echo View::fetch($this->template_dir . 'search_goods');
  322. exit;
  323. }
  324. public function presell_goods_info() {
  325. $goods_id = intval(input('param.goods_id'));
  326. $data = array();
  327. $data['result'] = true;
  328. //获取商品具体信息用于显示
  329. $goods_model = model('goods');
  330. $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);
  331. if (empty($goods_info)) {
  332. $data['result'] = false;
  333. $data['message'] = lang('param_error');
  334. echo json_encode($data);
  335. die;
  336. }
  337. $data['goods_id'] = $goods_info['goods_id'];
  338. $data['goods_name'] = $goods_info['goods_name'];
  339. $data['goods_price'] = $goods_info['goods_price'];
  340. $data['goods_image'] = goods_thumb($goods_info, 240);
  341. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  342. echo json_encode($data);
  343. die;
  344. }
  345. /*
  346. * 判断此商品是否已经参加预售
  347. */
  348. private function _check_allow_presell($goods_id, $presell_id = 0) {
  349. $condition = array();
  350. $condition[] = array('goods_id', '=', $goods_id);
  351. $condition[] = array('presell_state', '=', 1);
  352. if ($presell_id) {
  353. $condition[] = array('presell_id', '<>', $presell_id);
  354. }
  355. $presell = model('presell')->getPresellInfo($condition);
  356. if ($presell) {
  357. return false;
  358. } else {
  359. return true;
  360. }
  361. }
  362. protected function getSellerItemList() {
  363. $menu_array = array(
  364. array(
  365. 'name' => 'presell_list', 'text' => lang('presell_active_list'),
  366. 'url' => (string) url('Sellerpromotionpresell/index')
  367. ),
  368. );
  369. switch (request()->action()) {
  370. case 'presell_add':
  371. $menu_array[] = array(
  372. 'name' => 'presell_add', 'text' => lang('presell_add'),
  373. 'url' => (string) url('Sellerpromotionpresell/presell_add')
  374. );
  375. break;
  376. case 'presell_edit':
  377. $menu_array[] = array(
  378. 'name' => 'presell_edit', 'text' => lang('presell_edit'), 'url' => 'javascript:;'
  379. );
  380. break;
  381. case 'presell_quota_add':
  382. $menu_array[] = array(
  383. 'name' => 'presell_quota_add', 'text' => lang('presell_quota_add'),
  384. 'url' => (string) url('Sellerpromotionpresell/presell_quota_add')
  385. );
  386. break;
  387. }
  388. return $menu_array;
  389. }
  390. }