Goods.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $data = Dever::db($table)->$method($where);
  63. $result = array();
  64. if ($data) {
  65. foreach ($data as $k => $v) {
  66. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  67. }
  68. }
  69. return $data;
  70. }
  71. # 获取店铺的商品SKU列表
  72. public function getSkuList($store)
  73. {
  74. $table = 'store/goods_sku';
  75. $where['store_id'] = isset($store['id']) ? $store['id'] : $store;
  76. $method = 'getDataPage';
  77. $name = Dever::input('name');
  78. if ($name) {
  79. $where['name'] = $name;
  80. }
  81. $data = Dever::db($table)->$method($where);
  82. $result = array();
  83. if ($data) {
  84. foreach ($data as $k => $v) {
  85. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  86. if ($data[$k]['price_type'] == 4) {
  87. continue;
  88. }
  89. $data[$k]['total'] = $data[$k]['total'] ? $data[$k]['total'] : 0;
  90. if ($data[$k]['total'] <= 0) {
  91. continue;
  92. }
  93. if (!isset($result[$v['id']])) {
  94. $result[$v['id']]['id'] = $v['id'];
  95. $result[$v['id']]['name'] = $v['name'];
  96. $result[$v['id']]['total'] = $data[$k]['total'];
  97. $result[$v['id']]['children'] = array();
  98. }
  99. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  100. $result[$v['id']]['children'][] = array
  101. (
  102. 'id' => $v['id'] . '-' . $data[$k]['sku_id'],
  103. 'name' => $v['name'] . '-' . $data[$k]['sku_name'] . '[剩余'.$data[$k]['total'].'个]',
  104. 'total' => $data[$k]['total'],
  105. 'end' => true,
  106. );
  107. }
  108. if (!$result[$v['id']]['children']) {
  109. $result[$v['id']]['name'] = $v['name'] . '[剩余'.$data[$k]['total'].'个]';
  110. $result[$v['id']]['end'] = true;
  111. }
  112. }
  113. }
  114. return array_values($result);
  115. }
  116. # 获取库存
  117. public function getInfo($store_id, $info, $sku_id = false, $attr = true)
  118. {
  119. $where['store_id'] = $store_id;
  120. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  121. $other = Dever::db('store/goods_sku')->getData($where);
  122. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  123. if($data) {
  124. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  125. $data['total'] = 0;
  126. if ($data['price_type'] == 4) {
  127. if (isset($data['goods']) && is_array($data['goods'])) {
  128. foreach ($data['goods'] as $k => $v) {
  129. $data['goods'][$k]['total'] = $this->getTotal($other, -1);
  130. if ($data['total'] > $data['goods'][$k]['total']) {
  131. $data['total'] = $data['goods'][$k]['total'];
  132. }
  133. }
  134. }
  135. } else {
  136. $data['total'] = $this->getTotal($other, $sku_id);
  137. }
  138. }
  139. return $data;
  140. }
  141. # 验证库存
  142. public function checkTotal(&$num, $goods_id, $store_id, $sku_id, $state = 1)
  143. {
  144. $info = $this->getInfo($store_id, $goods_id, $sku_id, false);
  145. if (!$info) {
  146. Dever::alert('商品不存在');
  147. }
  148. $total = $info['total'];
  149. if ($num > $total) {
  150. if ($state == 2) {
  151. Dever::alert('库存不足');
  152. }
  153. $num = $total;
  154. }
  155. return $total;
  156. }
  157. # 获取库存
  158. public function getTotal($other, $sku_id)
  159. {
  160. if (isset($other[$sku_id])) {
  161. return $other[$sku_id]['total'];
  162. } else {
  163. return 0;
  164. }
  165. }
  166. }