Goods.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Store\Lib;
  3. use Dever;
  4. class Goods
  5. {
  6. # 处理库存操作 1是增加,2是减少 1是总库存,2是销量
  7. public function oper($store_id, $type, $col, $data)
  8. {
  9. if (!$data) {
  10. return;
  11. }
  12. if ($type == 1) {
  13. $method = 'inc';
  14. $otherMethod = 'dec';
  15. } else {
  16. $method = 'dec';
  17. $otherMethod = 'inc';
  18. }
  19. if ($col == 1) {
  20. $method .= 'Total';
  21. $otherMethod .= 'Total';
  22. $num = 'total_num';
  23. } else {
  24. $method .= 'Sell';
  25. $otherMethod .= 'Sell';
  26. $num = 'sell_num';
  27. }
  28. foreach ($data as $k => $v) {
  29. if (!$v['sku_id']) {
  30. $v['sku_id'] = -1;
  31. }
  32. $up = array();
  33. $up['where_store_id'] = $store_id;
  34. $up['where_goods_id'] = $v['goods_id'];
  35. $up[$num] = $v['num'];
  36. $state = Dever::db('store/goods')->$method($up);
  37. if ($state) {
  38. $upSku = $up;
  39. $upSku['where_sku_id'] = $v['sku_id'];
  40. $state = Dever::db('store/goods_sku')->$method($upSku);
  41. if (!$state) {
  42. Dever::db('store/goods')->$otherMethod($up);
  43. }
  44. }
  45. }
  46. return $state;
  47. }
  48. # 获取店铺的商品列表
  49. public function getGoodsSku($store)
  50. {
  51. $table = 'store/goods_sku';
  52. $where['store_id'] = isset($store['id']) ? $store['id'] : $store;
  53. $method = 'getDataPage';
  54. $name = Dever::input('name');
  55. if ($name) {
  56. $where['name'] = $name;
  57. }
  58. $total = Dever::input('total');
  59. if ($total) {
  60. $where['total'] = $total;
  61. }
  62. $where['status'] = 1;
  63. $where['state'] = 1;
  64. $where['state_1'] = 1;
  65. $data = Dever::db($table)->$method($where);
  66. $result = array();
  67. if ($data) {
  68. foreach ($data as $k => $v) {
  69. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id'], 2);
  70. $data[$k]['total'] = $v['total'];
  71. }
  72. }
  73. return $data;
  74. }
  75. # 获取店铺的商品SKU列表
  76. public function getSkuList($store)
  77. {
  78. $table = 'store/goods_sku';
  79. $where['store_id'] = isset($store['id']) ? $store['id'] : $store;
  80. $method = 'getDataPage';
  81. $name = Dever::input('name');
  82. if ($name) {
  83. $where['name'] = $name;
  84. }
  85. $where['status'] = 1;
  86. $where['state'] = 1;
  87. $where['state_1'] = 1;
  88. $data = Dever::db($table)->$method($where);
  89. $result = array();
  90. if ($data) {
  91. foreach ($data as $k => $v) {
  92. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id'], 2);
  93. if ($data[$k]['price_type'] == 4) {
  94. continue;
  95. }
  96. $data[$k]['total'] = $v['total'] ? $v['total'] : 0;
  97. if ($data[$k]['total'] <= 0) {
  98. continue;
  99. }
  100. if (!isset($result[$v['id']])) {
  101. $result[$v['id']]['id'] = $v['id'];
  102. $result[$v['id']]['name'] = $v['name'];
  103. $result[$v['id']]['price'] = $data[$k]['price'];
  104. $result[$v['id']]['total'] = $data[$k]['total'];
  105. $result[$v['id']]['children'] = array();
  106. }
  107. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  108. $result[$v['id']]['children'][] = array
  109. (
  110. 'id' => $v['id'] . '-' . $data[$k]['sku_id'],
  111. 'name' => $v['name'] . '-' . $data[$k]['sku_name'] . '[剩余'.$data[$k]['total'].'个]',
  112. 'price' => $data[$k]['price'],
  113. 'total' => $data[$k]['total'],
  114. 'end' => true,
  115. );
  116. }
  117. if (!$result[$v['id']]['children']) {
  118. $result[$v['id']]['name'] = $v['name'] . '[剩余'.$data[$k]['total'].'个]';
  119. $result[$v['id']]['end'] = true;
  120. }
  121. }
  122. }
  123. return array_values($result);
  124. }
  125. # 获取库存
  126. public function getInfo($store_id, $info, $sku_id = false, $attr = true)
  127. {
  128. $where['store_id'] = $store_id;
  129. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  130. $other = Dever::db('store/goods_sku')->getData($where);
  131. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  132. if($data) {
  133. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  134. $data['total'] = 0;
  135. if ($data['price_type'] == 4) {
  136. if (isset($data['goods']) && is_array($data['goods'])) {
  137. foreach ($data['goods'] as $k => $v) {
  138. $data['goods'][$k]['total'] = $this->getTotal($other, -1);
  139. if ($data['total'] > $data['goods'][$k]['total']) {
  140. $data['total'] = $data['goods'][$k]['total'];
  141. }
  142. }
  143. }
  144. } else {
  145. $data['total'] = $this->getTotal($other, $sku_id);
  146. }
  147. }
  148. return $data;
  149. }
  150. # 验证库存
  151. public function checkTotal(&$num, $goods_id, $store_id, $sku_id, $state = 1)
  152. {
  153. $info = $this->getInfo($store_id, $goods_id, $sku_id, false);
  154. if (!$info) {
  155. Dever::alert('商品不存在');
  156. }
  157. $total = $info['total'];
  158. if ($num > $total) {
  159. if ($state == 2) {
  160. Dever::alert('库存不足');
  161. }
  162. $num = $total;
  163. }
  164. return $total;
  165. }
  166. # 获取库存
  167. public function getTotal($other, $sku_id)
  168. {
  169. if (isset($other[$sku_id])) {
  170. return $other[$sku_id]['total'];
  171. } else {
  172. return 0;
  173. }
  174. }
  175. }