123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace Scm_product\Lib;
- use Dever;
- class Price
- {
- public function __construct()
- {
- Dever::load('manage/auth.init');
- }
- # 获取价格模板信息
- public function get($price_id, $goods_id, $sku_id, $price, $buy_price)
- {
- # 暂时未增加临期价格
- if ($price_id <= 0) {
- return array($price, $buy_price);
- }
- $time = time();
- $template = Dever::db('scm_product/price')->find($price_id);
- if ($template) {
- $rule = Dever::db('scm_product/price_rule')->select(array('price_id' => $template['id'], 'status' => 1));
- if ($rule) {
- foreach ($rule as $k => $v) {
- $get = false;
- if ($v['time'] == 1) {
- $get = true;
- } elseif ($v['time'] == 2 && $time >= $v['time_start'] && $time <= $v['time_end']) {
- $get = true;
- }
- if ($get) {
- $info = Dever::db('scm_product/price_rule_sku')->find(array('price_rule_id' => $v['id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
- if ($info) {
- $price = Dever::per($price, $info['price']);
- $buy_price = Dever::per($buy_price, $info['buy_price']);
- break;
- }
- }
- }
- }
- }
- return array($price, $buy_price);
- }
- # 获取商品列表
- public function goods_api()
- {
- $price_rule_id = Dever::input('price_rule_id', 1);
- $goods_category = Dever::input('goods_category');
- $where['price_rule_id'] = $price_rule_id;
- return Dever::outDiy(Dever::load('scm_product/lib/info')->getSetList('scm_product/price_rule_sku', $where, $goods_category));
- }
- # 配置商品
- public function setGoods_api()
- {
- $data = array();
- $data['price_rule_id'] = Dever::input('id', 1);
- $price = Dever::db('scm_product/price_rule')->find($data['price_rule_id']);
- $goods_category = '';
- $data['type'] = 1;
- $data['sell_num'] = $data['sell_num'] = 0;
- if ($price) {
- $goods_category = $price['category'];
- $data['type'] = $price['type'];
- $data['sell_num'] = $price['sell_num'];
- $data['buy_num'] = $price['buy_num'];
- }
-
- $data['host'] = Dever::url('lib/price.setGoods', 'scm_product');
- $data['url'] = Dever::url('lib/price.goods?price_rule_id=' . $data['price_rule_id'] . '&goods_category=' . $goods_category . '&price=' . $data['sell_num'] . '&buy_price=' . $data['buy_num'], 'scm_product');
- $data['submit'] = Dever::url('lib/price.setGoods_action_commit?json=1', 'scm_product');
- return Dever::render('set_price_goods', $data);
- }
- # 配置商品
- public function setGoods_action_commit_api()
- {
- $goods = Dever::input('goods');
- if (!$goods) {
- Dever::alert('请传入商品');
- }
- $goods = Dever::json_decode($goods);
- $price_rule_id = Dever::input('price_rule_id');
- $shop = Dever::db('scm_product/price')->one($price_rule_id);
- $where['option_price_rule_id'] = $price_rule_id;
- $where['set_state'] = 2;
- Dever::db('scm_product/price_rule_sku')->updates($where);
- foreach ($goods as $k => $v) {
- if ($v['del'] == 1) {
- $temp = explode('-', $k);
- $goods_id = $temp[0];
- $sku_id = -1;
- if (isset($temp[1])) {
- $sku_id = $temp[1];
- }
- $goods_info = Dever::db('scm_product/info')->one($goods_id);
- $w = array();
- $w['goods_id'] = $goods_id;
- $w['sku_id'] = $sku_id;
- $w['price_rule_id'] = $price_rule_id;
- $sku_info = Dever::db('scm_product/price_rule_sku')->one($w);
- $w['status'] = $goods_info['status'];
- if (isset($v['price']) && $v['price']) {
- $w['price'] = $v['price'];
- }
- if (isset($v['buy_price']) && $v['buy_price']) {
- $w['buy_price'] = $v['buy_price'];
- }
- if (!$sku_info) {
- Dever::db('scm_product/price_rule_sku')->insert($w);
- } else {
- $w['where_id'] = $sku_info['id'];
- $w['state'] = 1;
- Dever::db('scm_product/price_rule_sku')->update($w);
- }
- }
- }
- return 'refer';
- }
- }
|