Sellerpromotionbundling.php 21 KB

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