Sellerpromotionbundling.php 21 KB

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