Set.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Scm_role\Lib;
  3. use Dever;
  4. class Set
  5. {
  6. # 获取商品列表
  7. public function goods_api()
  8. {
  9. $id = Dever::input('id', 1);
  10. $role = Dever::input('role');
  11. $price_id = Dever::input('price_id', -1);
  12. $type = Dever::input('type', 1);
  13. $source = '';
  14. $table = 'scm_role/'.$role.'_goods_sku';
  15. if ($type == 2) {
  16. $source = $table;
  17. $table = '';
  18. }
  19. $goods_category = Dever::input('goods_category');
  20. $where[$role . '_id'] = $id;
  21. return Dever::outDiy(Dever::load('scm_product/lib/info')->getSetList($table, $where, $goods_category, 'goods_id', array(), $price_id, $source));
  22. }
  23. # 配置商品
  24. public function home_api()
  25. {
  26. $data = array();
  27. $data['id'] = Dever::input('id', 1);
  28. $data['role'] = Dever::input('role');
  29. $info = Dever::db('scm_role/' . $data['role'])->find($data['id']);
  30. $data['info'] = $info;
  31. $data['host'] = Dever::url('lib/set.home', 'scm_role');
  32. $uri = 'lib/set.goods?id=' . $data['id'] . '&role=' . $data['role'];
  33. if (isset($info['category'])) {
  34. $uri .= '&goods_category=' . $info['category'];
  35. }
  36. if (isset($info['price_id'])) {
  37. $uri .= '&price_id=' . $info['price_id'];
  38. }
  39. $data['url'] = Dever::url($uri, 'scm_role');
  40. $data['submit'] = Dever::url('lib/set.action_commit?json=1', 'scm_role');
  41. return Dever::render('set_' . $data['role'] . '_goods', $data);
  42. }
  43. # 配置商品
  44. public function action_commit_api()
  45. {
  46. $goods = Dever::input('goods');
  47. if (!$goods) {
  48. Dever::alert('请传入商品');
  49. }
  50. $role = Dever::input('role');
  51. $goods = Dever::json_decode($goods);
  52. $id = Dever::input('id');
  53. $col = $role . '_id';
  54. $table_goods = 'scm_role/'.$role.'_goods';
  55. $table_goods_sku = 'scm_role/'.$role.'_goods_sku';
  56. $info = Dever::db('scm_role/' . $role)->one($id);
  57. $where['option_' . $col] = $id;
  58. $where['set_state'] = 2;
  59. Dever::db($table_goods)->updates($where);
  60. Dever::db($table_goods_sku)->updates($where);
  61. foreach ($goods as $k => $v) {
  62. $temp = explode('-', $k);
  63. $goods_id = $temp[0];
  64. if (isset($temp[1])) {
  65. $sku_id = $temp[1];
  66. } else {
  67. $sku_id = -1;
  68. }
  69. if ($v['num'] < 0) {
  70. continue;
  71. }
  72. $w = array();
  73. $w['goods_id'] = $goods_id;
  74. $w[$col] = $id;
  75. $w['sku_id'] = $sku_id;
  76. if ($v['del'] == 2) {
  77. $sku_info = Dever::db($table_goods_sku)->one($w);
  78. if ($sku_info) {
  79. Dever::db($table_goods_sku)->update(array('where_id' => $sku_info['id'], 'state' => 2));
  80. }
  81. unset($w['sku_id']);
  82. $t = $w;
  83. $t['state'] = 1;
  84. $total = Dever::db($table_goods_sku)->total($t);
  85. if ($total <= 0) {
  86. $info = Dever::db($table_goods)->one($w);
  87. if ($info) {
  88. Dever::db($table_goods)->update(array('where_id' => $info['id'], 'state' => 2));
  89. }
  90. }
  91. } else {
  92. $goods_info = Dever::db('scm_product/info')->one($goods_id);
  93. $info = Dever::db($table_goods)->one($w);
  94. $u = $w;
  95. if (isset($v['min'])) {
  96. $u['min'] = $v['min'];
  97. }
  98. if (isset($v['cost_price'])) {
  99. $u['cost_price'] = $v['cost_price'];
  100. }
  101. if (!$info) {
  102. Dever::db($table_goods)->insert($u);
  103. } else {
  104. $u['where_id'] = $info['id'];
  105. $u['state'] = 1;
  106. Dever::db($table_goods)->update($u);
  107. }
  108. $u['sku_id'] = $w['sku_id'] = $sku_id;
  109. $info = Dever::db($table_goods_sku)->one($w);
  110. if (!$info) {
  111. Dever::db($table_goods_sku)->insert($u);
  112. } else {
  113. $u['where_id'] = $info['id'];
  114. $u['state'] = 1;
  115. Dever::db($table_goods_sku)->update($u);
  116. }
  117. }
  118. }
  119. return 'refer';
  120. }
  121. }