Sellerpromotionpresell.php 19 KB

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