Sellerpromotionbundling.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Sellerpromotionbundling extends BaseSeller
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionbundling.lang.php');
  21. if (intval(config('ds_config.promotion_allow')) !== 1) {
  22. $this->error(lang('promotion_unavailable'), (string) url('Seller/index'));
  23. }
  24. }
  25. public function index()
  26. {
  27. $pbundling_model = model('pbundling');
  28. // 更新套装状态
  29. $where = array();
  30. $where[] = array('store_id', '=', session('store_id'));
  31. $where[] = array('blquota_endtime', '<', TIMESTAMP);
  32. $pbundling_model->editBundlingQuotaClose($where);
  33. $isPlatformStore = check_platform_store() ? true : false;
  34. View::assign('isPlatformStore', $isPlatformStore);
  35. $hasList = $isPlatformStore;
  36. $bundling_published = '';
  37. if (!$isPlatformStore) {
  38. // 检查是否已购买套餐
  39. $where = array();
  40. $where[] = array('store_id', '=', session('store_id'));
  41. $bundling_quota = $pbundling_model->getBundlingQuotaInfo($where);
  42. View::assign('bundling_quota', $bundling_quota);
  43. if (!empty($bundling_quota)) {
  44. // 计算已经发布活动、剩余活动数量
  45. $bundling_published = $pbundling_model->getBundlingCount(array('store_id' => session('store_id')));
  46. $bundling_surplus = intval(config('ds_config.promotion_bundling_sum')) - intval($bundling_published);
  47. View::assign('bundling_published', $bundling_published);
  48. View::assign('bundling_surplus', $bundling_surplus);
  49. $hasList = true;
  50. } elseif (intval(config('ds_config.promotion_bundling_price')) == 0) { //如果没有购买过套餐,则将之前添加的优惠组合关闭
  51. $hasList = true;
  52. } else {
  53. $update = array('bl_state' => $pbundling_model::STATE0);
  54. $pbundling_model->editBundling($update, $where);
  55. }
  56. }
  57. if ($hasList) {
  58. // 查询活动
  59. $where = array();
  60. $where[] = array('store_id', '=', session('store_id'));
  61. if (input('param.bundling_name') != '') {
  62. $where[] = array('bl_name', 'like', '%' . trim(input('param.bundling_name')) . '%');
  63. }
  64. if (is_numeric(input('param.state'))) {
  65. $where[] = array('bl_state', '=', input('param.state'));
  66. }
  67. $bundling_list = $pbundling_model->getBundlingList($where, '*', 'bl_id desc', 10, 0, $bundling_published);
  68. $bundling_list = array_under_reset($bundling_list, 'bl_id');
  69. View::assign('show_page', $pbundling_model->page_info->render());
  70. if (!empty($bundling_list)) {
  71. $blid_array = array_keys($bundling_list);
  72. $bgoods_array = $pbundling_model->getBundlingGoodsList(array(array('bl_id', 'in', $blid_array), array('blgoods_appoint', '=', 1)), 'bl_id,goods_id,count(*) as count', 'blgoods_appoint desc', 'bl_id');
  73. $bgoods_array1 = array_under_reset($bgoods_array, 'goods_id');
  74. if (!empty($bgoods_array1)) {
  75. $goodsid_array = array_keys($bgoods_array1);
  76. $goods_array = model('goods')->getGoodsList(array(
  77. array('goods_id', 'in', $goodsid_array)
  78. ), 'goods_id,goods_image');
  79. $goods_array = array_under_reset($goods_array, 'goods_id');
  80. }
  81. $bgoods_array = array_under_reset($bgoods_array, 'bl_id');
  82. foreach ($bundling_list as $key => $val) {
  83. $bundling_list[$key]['goods_id'] = '';
  84. $bundling_list[$key]['count'] = 0;
  85. $bundling_list[$key]['img'] = goods_thumb(array(), 60);
  86. if (isset($bgoods_array[$val['bl_id']]) && !empty($bgoods_array[$val['bl_id']])) {
  87. $bundling_list[$key]['goods_id'] = $bgoods_array[$val['bl_id']]['goods_id'];
  88. $bundling_list[$key]['count'] = $bgoods_array[$val['bl_id']]['count'];
  89. $bundling_list[$key]['img'] = goods_thumb($goods_array[$bgoods_array[$val['bl_id']]['goods_id']], 60);
  90. }
  91. }
  92. }
  93. View::assign('bundling_list', $bundling_list);
  94. // 状态数组
  95. $state_array = array(0 => lang('bundling_status_0'), 1 => lang('bundling_status_1'));
  96. View::assign('state_array', $state_array);
  97. }
  98. $this->setSellerCurMenu('Sellerpromotionbundling');
  99. $this->setSellerCurItem('index');
  100. return View::fetch($this->template_dir . 'index');
  101. }
  102. /**
  103. * 套餐购买
  104. */
  105. public function bundling_quota_add()
  106. {
  107. if (request()->isPost()) {
  108. if (intval(config('ds_config.promotion_bundling_price')) == 0) {
  109. ds_json_encode(10001, lang('param_error'));
  110. }
  111. $quantity = intval(input('post.bundling_quota_quantity')); // 购买数量(月)
  112. $price_quantity = $quantity * intval(config('ds_config.promotion_bundling_price')); // 扣款数
  113. if ($quantity <= 0 || $quantity > 12) {
  114. ds_json_encode(10001, lang('bundling_quota_price_fail'));
  115. }
  116. // 实例化模型
  117. $pbundling_model = model('pbundling');
  118. $data = array();
  119. $data['store_id'] = session('store_id');
  120. $data['store_name'] = session('store_name');
  121. $data['member_id'] = session('member_id');
  122. $data['member_name'] = session('member_name');
  123. $data['blquota_month'] = $quantity;
  124. $data['blquota_starttime'] = TIMESTAMP;
  125. $data['blquota_endtime'] = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  126. $data['blquota_state'] = 1;
  127. $return = $pbundling_model->addBundlingQuota($data);
  128. if ($return) {
  129. // 添加店铺费用记录
  130. $this->recordStorecost($price_quantity, '购买优惠套装');
  131. // 添加任务队列
  132. $end_time = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  133. $this->addcron(array('cron_exetime' => $end_time, 'cron_value' => serialize(intval(session('store_id'))), 'cron_type' => 'editBundlingQuotaClose'), true);
  134. $this->recordSellerlog('购买' . $quantity . '套优惠套装,单位元');
  135. ds_json_encode(10000, lang('bundling_quota_price_succ'));
  136. } else {
  137. ds_json_encode(10001, lang('bundling_quota_price_fail'));
  138. }
  139. }
  140. // 输出导航
  141. $this->setSellerCurMenu('Sellerpromotionbundling');
  142. $this->setSellerCurItem('bundling_quota_add');
  143. return View::fetch($this->template_dir . 'quota_add');
  144. }
  145. /**
  146. * 套餐续费
  147. */
  148. public function bundling_renew()
  149. {
  150. if (request()->isPost()) {
  151. if (intval(config('ds_config.promotion_bundling_price')) == 0) {
  152. ds_json_encode(10001, lang('param_error'));
  153. }
  154. $pbundling_model = model('pbundling');
  155. $quantity = intval(input('post.bundling_quota_quantity')); // 购买数量(月)
  156. $price_quantity = $quantity * intval(config('ds_config.promotion_bundling_price')); // 扣款数
  157. if ($quantity <= 0 || $quantity > 12) {
  158. ds_json_encode(10001, lang('bundling_quota_price_fail'));
  159. }
  160. $where = array();
  161. $where[] = array('store_id', '=', session('store_id'));
  162. $bundling_quota = $pbundling_model->getBundlingQuotaInfo($where);
  163. if ($bundling_quota['blquota_endtime'] > TIMESTAMP) {
  164. // 套餐未超时(结束时间+购买时间)
  165. $update['blquota_endtime'] = intval($bundling_quota['blquota_endtime']) + 60 * 60 * 24 * 30 * $quantity;
  166. } else {
  167. // 套餐已超时(当前时间+购买时间)
  168. $update['blquota_endtime'] = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  169. }
  170. $return = $pbundling_model->editBundlingQuotaOpen($update, $where);
  171. if ($return) {
  172. // 添加店铺费用记录
  173. $this->recordStorecost($price_quantity, '购买优惠套装');
  174. // 添加任务队列
  175. $this->addcron(array(
  176. 'cron_exetime' => $update['blquota_endtime'], 'cron_value' => serialize(session('store_id')), 'cron_type' => 'editBundlingQuotaClose'
  177. ), true);
  178. $this->recordSellerlog('续费' . $quantity . '套优惠套装,单位元');
  179. ds_json_encode(10000, lang('bundling_quota_price_succ'));
  180. } else {
  181. ds_json_encode(10001, lang('bundling_quota_price_fail'));
  182. }
  183. }
  184. $this->setSellerCurMenu('Sellerpromotionbundling');
  185. $this->setSellerCurItem('bundling_quota_add');
  186. return View::fetch($this->template_dir . 'quota_add');
  187. }
  188. /**
  189. * 套餐活动添加
  190. */
  191. public function bundling_add()
  192. {
  193. /**
  194. * 实例化模型
  195. */
  196. $pbundling_model = model('pbundling');
  197. // 验证套餐数量
  198. if (intval(config('ds_config.promotion_bundling_sum')) != 0 && !isset($_REQUEST['bundling_id'])) {
  199. $count = $pbundling_model->getBundlingCount(array('store_id' => session('store_id')));
  200. if (intval(config('ds_config.promotion_bundling_sum')) <= intval($count)) {
  201. $this->error(lang('bundling_add_fail_quantity_beyond'));
  202. ds_json_encode(10001, lang('goods_index_consult_fail'));
  203. }
  204. }
  205. if (request()->isPost()) {
  206. // 插入套餐
  207. $data = array();
  208. $bl_id = intval(input('post.bundling_id'));
  209. if ($bl_id <= 0) {
  210. $data['bl_name'] = input('post.bundling_name');
  211. $data['store_id'] = session('store_id');
  212. $data['store_name'] = session('store_name');
  213. $data['bl_discount_price'] = input('post.discount_price');
  214. $data['bl_freight_choose'] = input('post.bundling_freight_choose');
  215. $data['bl_freight'] = input('post.bundling_freight');
  216. $data['bl_state'] = intval(input('post.state'));
  217. $bl_id = $pbundling_model->addBundling($data);
  218. if (!$bl_id) {
  219. ds_json_encode(10001, lang('ds_common_op_fail'));
  220. }
  221. } else {
  222. $condition = array();
  223. $condition[] = array('bl_id', '=', $bl_id);
  224. $condition[] = array('store_id', '=', session('store_id'));
  225. $data['bl_name'] = input('post.bundling_name');
  226. $data['bl_discount_price'] = input('post.discount_price');
  227. $data['bl_freight_choose'] = input('post.bundling_freight_choose');
  228. $data['bl_freight'] = input('post.bundling_freight');
  229. $data['bl_state'] = intval(input('post.state'));
  230. $pbundling_model->editBundling($data, $condition);
  231. }
  232. // 插入套餐商品
  233. $goods_model = model('goods');
  234. $data_goods = array();
  235. $appoint_goodsid = false;
  236. if (input('post.bundling_id')) {
  237. $pbundling_model->delBundlingGoods(array('bl_id' => $bl_id));
  238. }
  239. $goods_array = input('post.goods/a'); #获取数组
  240. if (!empty($goods_array) && is_array($goods_array)) {
  241. foreach ($goods_array as $key => $val) {
  242. // 验证是否为本店铺商品
  243. $goods_info = $goods_model->getGoodsInfoByID($val['gid']);
  244. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  245. continue;
  246. }
  247. $data = array();
  248. $data['bl_id'] = $bl_id;
  249. $data['goods_id'] = $goods_info['goods_id'];
  250. $data['goods_commonid'] = $goods_info['goods_commonid'];
  251. $data['goods_name'] = $goods_info['goods_name'];
  252. $data['goods_image'] = $goods_info['goods_image'];
  253. $data['blgoods_price'] = ds_price_format($val['price']);
  254. $data['blgoods_appoint'] = intval($val['appoint']);
  255. if (!$appoint_goodsid && intval($val['appoint']) == 1) {
  256. $appoint_goodsid = intval($val['gid']);
  257. }
  258. $data_goods[] = $data;
  259. }
  260. }
  261. // 插入数据
  262. $return = $pbundling_model->addBundlingGoodsAll($data_goods);
  263. if (!input('post.bundling_id') && !$appoint_goodsid) {
  264. // 自动发布动态
  265. // bl_id,bl_name,image_path,bl_discount_price,bl_freight_choose,bl_freight,store_id
  266. $data_array = array();
  267. $data_array['bl_id'] = $return;
  268. $data_array['goods_id'] = $appoint_goodsid;
  269. $data_array['bl_name'] = input('post.bundling_name');
  270. $data_array['bl_img'] = '';
  271. $data_array['bl_discount_price'] = $data['bl_discount_price'];
  272. $data_array['bl_freight_choose'] = $data['bl_freight_choose'];
  273. $data_array['bl_freight'] = $data['bl_freight'];
  274. $data_array['store_id'] = session('store_id');
  275. $this->storeAutoShare($data_array, 'bundling');
  276. }
  277. $this->recordSellerlog('添加优惠套装,名称:' . input('post.bundling_name') . ' id:' . $return);
  278. ds_json_encode(10000, lang('ds_common_op_succ'));
  279. } else {
  280. if (intval(input('param.bundling_id')) > 0) {
  281. $bundling_info = $pbundling_model->getBundlingInfo(array('bl_id' => intval(input('param.bundling_id')), 'store_id' => session('store_id')));
  282. // halt($bundling_info);
  283. View::assign('bundling_info', $bundling_info);
  284. // 验证是否属于自己的组合套餐
  285. if (empty($bundling_info['store_id'])) {
  286. $this->error(lang('param_error'), (string) url('Sellerpromotionbundling/index'));
  287. }
  288. $b_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => intval(input('param.bundling_id'))));
  289. if (!empty($b_goods_list)) {
  290. $goodsid_array = array();
  291. foreach ($b_goods_list as $val) {
  292. $goodsid_array[] = $val['goods_id'];
  293. }
  294. $goods_list = model('goods')->getGoodsList(array(array('goods_id', 'in', $goodsid_array)), 'goods_id,goods_price,goods_image,goods_name');
  295. View::assign('goods_list', array_under_reset($goods_list, 'goods_id'));
  296. }
  297. View::assign('b_goods_list', $b_goods_list);
  298. // 输出导航
  299. $this->setSellerCurMenu('Sellerpromotionbundling');
  300. $this->setSellerCurItem('bundling_edit');
  301. } else {
  302. // 输出导航
  303. $this->setSellerCurMenu('Sellerpromotionbundling');
  304. $this->setSellerCurItem('bundling_add');
  305. }
  306. return View::fetch($this->template_dir . 'bundling_add');
  307. }
  308. }
  309. /**
  310. * 套餐活动添加商品
  311. */
  312. public function bundling_add_goods()
  313. {
  314. /**
  315. * 实例化模型
  316. */
  317. $goods_model = model('goods');
  318. // where条件
  319. $where = array();
  320. $where[] = array('goods.store_id', '=', session('store_id'));
  321. if (intval(input('param.storegc_id')) > 0) {
  322. $where[] = array('goods.goods_stcids', 'like', '%,' . intval(input('param.storegc_id')) . ',%');
  323. }
  324. if (trim(input('param.keyword')) != '') {
  325. $where[] = array('goods.goods_name', 'like', '%' . trim(input('param.keyword')) . '%');
  326. }
  327. $goods_list = $goods_model->getGoodsListForPromotion($where, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price,goods.goods_serial,goods.goods_storage', 8, 'bundling');
  328. View::assign('show_page', $goods_model->page_info->render());
  329. View::assign('goods_list', $goods_list);
  330. /**
  331. * 商品分类
  332. */
  333. $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
  334. View::assign('store_goods_class', $store_goods_class);
  335. echo View::fetch($this->template_dir . 'bundling_add_goods');
  336. exit;
  337. }
  338. /**
  339. * 删除优惠套装活动
  340. */
  341. public function drop_bundling()
  342. {
  343. /**
  344. * 参数验证
  345. */
  346. $blids = trim(input('param.bundling_id'));
  347. if (empty($blids)) {
  348. ds_json_encode(10001, lang('param_error'));
  349. }
  350. $return = model('pbundling')->delBundling($blids, session('store_id'));
  351. if ($return) {
  352. $this->recordSellerlog('删除优惠套装,套餐id:' . $blids);
  353. ds_json_encode(10000, lang('bundling_delete_success'));
  354. } else {
  355. ds_json_encode(10001, lang('bundling_delete_fail'));
  356. }
  357. }
  358. /**
  359. * 用户中心右边,小导航
  360. *
  361. * @param string $menu_type 导航类型
  362. * @param string $name 当前导航的name
  363. * @return
  364. */
  365. protected function getSellerItemList()
  366. {
  367. $menu_array = array();
  368. switch (request()->action()) {
  369. case 'index':
  370. case 'bundling_quota_list':
  371. $menu_array = array(
  372. array(
  373. 'name' => 'index', 'text' => lang('bundling_list'),
  374. 'url' => (string) url('Sellerpromotionbundling/index')
  375. )
  376. );
  377. break;
  378. case 'bundling_quota_add':
  379. $menu_array = array(
  380. array(
  381. 'name' => 'index', 'text' => lang('bundling_list'),
  382. 'url' => (string) url('Sellerpromotionbundling/index')
  383. ), array(
  384. 'name' => 'bundling_quota_add', 'text' => lang('bundling_quota_add'),
  385. 'url' => (string) url('Sellerpromotionbundling/bundling_quota_add')
  386. )
  387. );
  388. break;
  389. case 'bundling_renew':
  390. $menu_array = array(
  391. array(
  392. 'name' => 'index', 'text' => lang('bundling_list'),
  393. 'url' => (string) url('Sellerpromotionbundling/index')
  394. ), array(
  395. 'name' => 'bundling_renew', 'text' => lang('set_renewal'),
  396. 'url' => (string) url('Sellerpromotionbundling/bundling_renew')
  397. )
  398. );
  399. break;
  400. case 'bundling_add':
  401. $menu_array = array(
  402. array(
  403. 'name' => 'index', 'text' => lang('bundling_list'),
  404. 'url' => (string) url('Sellerpromotionbundling/index')
  405. ), array(
  406. 'name' => 'bundling_add', 'text' => lang('bundling_add'),
  407. 'url' => (string) url('Sellerpromotionbundling/bundling_add')
  408. )
  409. );
  410. break;
  411. case 'bundling_edit':
  412. $menu_array = array(
  413. array(
  414. 'name' => 'index', 'text' => lang('bundling_list'),
  415. 'url' => (string) url('Sellerpromotionbundling/index')
  416. ), array(
  417. 'name' => 'bundling_edit', 'text' => lang('bundling_edit'),
  418. 'url' => (string) url('Sellerpromotionbundling/bundling_edit')
  419. )
  420. );
  421. break;
  422. }
  423. return $menu_array;
  424. }
  425. }