Goods.php 5.9 KB

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