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