Price.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Scm_product\Lib;
  3. use Dever;
  4. class Price
  5. {
  6. public function __construct()
  7. {
  8. Dever::load('manage/auth.init');
  9. }
  10. # 获取价格模板信息
  11. public function get($price_id, $goods_id, $sku_id, $price, $buy_price)
  12. {
  13. # 暂时未增加临期价格
  14. if ($price_id <= 0) {
  15. return array($price, $buy_price);
  16. }
  17. $time = time();
  18. $template = Dever::db('scm_product/price')->find($price_id);
  19. if ($template) {
  20. $rule = Dever::db('scm_product/price_rule')->select(array('price_id' => $template['id'], 'status' => 1));
  21. if ($rule) {
  22. foreach ($rule as $k => $v) {
  23. $get = false;
  24. if ($v['time'] == 1) {
  25. $get = true;
  26. } elseif ($v['time'] == 2 && $time >= $v['time_start'] && $time <= $v['time_end']) {
  27. $get = true;
  28. }
  29. if ($get) {
  30. $info = Dever::db('scm_product/price_rule_sku')->find(array('price_rule_id' => $v['id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
  31. if ($info) {
  32. $price = Dever::per($price, $info['price']);
  33. $buy_price = Dever::per($buy_price, $info['buy_price']);
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. return array($price, $buy_price);
  41. }
  42. # 获取商品列表
  43. public function goods_api()
  44. {
  45. $price_rule_id = Dever::input('price_rule_id', 1);
  46. $goods_category = Dever::input('goods_category');
  47. $where['price_rule_id'] = $price_rule_id;
  48. return Dever::outDiy(Dever::load('scm_product/lib/info')->getSetList('scm_product/price_rule_sku', $where, $goods_category));
  49. }
  50. # 配置商品
  51. public function setGoods_api()
  52. {
  53. $data = array();
  54. $data['price_rule_id'] = Dever::input('id', 1);
  55. $price = Dever::db('scm_product/price_rule')->find($data['price_rule_id']);
  56. $goods_category = '';
  57. $data['type'] = 1;
  58. $data['sell_num'] = $data['sell_num'] = 0;
  59. if ($price) {
  60. $goods_category = $price['category'];
  61. $data['type'] = $price['type'];
  62. $data['sell_num'] = $price['sell_num'];
  63. $data['buy_num'] = $price['buy_num'];
  64. }
  65. $data['host'] = Dever::url('lib/price.setGoods', 'scm_product');
  66. $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');
  67. $data['submit'] = Dever::url('lib/price.setGoods_action_commit?json=1', 'scm_product');
  68. return Dever::render('set_price_goods', $data);
  69. }
  70. # 配置商品
  71. public function setGoods_action_commit_api()
  72. {
  73. $goods = Dever::input('goods');
  74. if (!$goods) {
  75. Dever::alert('请传入商品');
  76. }
  77. $goods = Dever::json_decode($goods);
  78. $price_rule_id = Dever::input('price_rule_id');
  79. $shop = Dever::db('scm_product/price')->one($price_rule_id);
  80. $where['option_price_rule_id'] = $price_rule_id;
  81. $where['set_state'] = 2;
  82. Dever::db('scm_product/price_rule_sku')->updates($where);
  83. foreach ($goods as $k => $v) {
  84. if ($v['del'] == 1) {
  85. $temp = explode('-', $k);
  86. $goods_id = $temp[0];
  87. $sku_id = -1;
  88. if (isset($temp[1])) {
  89. $sku_id = $temp[1];
  90. }
  91. $goods_info = Dever::db('scm_product/info')->one($goods_id);
  92. $w = array();
  93. $w['goods_id'] = $goods_id;
  94. $w['sku_id'] = $sku_id;
  95. $w['price_rule_id'] = $price_rule_id;
  96. $sku_info = Dever::db('scm_product/price_rule_sku')->one($w);
  97. $w['status'] = $goods_info['status'];
  98. if (isset($v['price']) && $v['price']) {
  99. $w['price'] = $v['price'];
  100. }
  101. if (isset($v['buy_price']) && $v['buy_price']) {
  102. $w['buy_price'] = $v['buy_price'];
  103. }
  104. if (!$sku_info) {
  105. Dever::db('scm_product/price_rule_sku')->insert($w);
  106. } else {
  107. $w['where_id'] = $sku_info['id'];
  108. $w['state'] = 1;
  109. Dever::db('scm_product/price_rule_sku')->update($w);
  110. }
  111. }
  112. }
  113. return 'refer';
  114. }
  115. }