Info.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace Scm_product\Lib;
  3. use Dever;
  4. class Info
  5. {
  6. # 获取商品列表
  7. public function getList()
  8. {
  9. }
  10. # 获取商品详情
  11. public function getInfo($id, $type = 1)
  12. {
  13. # type == 1 是销售商品 2是采购商品
  14. $info = is_array($id) ? $id : Dever::db('scm_product/info')->getOne($id);
  15. if (!$info) {
  16. Dever::alert('商品信息不存在');
  17. }
  18. $info = $this->get($info, $type);
  19. return $info;
  20. }
  21. private function get($info, $type = 1)
  22. {
  23. # 基本信息
  24. $info['udate'] = date('Y-m-d H:i', $info['udate']);
  25. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  26. # 获取分类
  27. $info['category_info'] = Dever::load('scm_product/lib/category')->getInfo($info['category']);
  28. # 获取属性
  29. $attr = Dever::db('scm_product/info_attr')->select(array('info_id' => $info['id']));
  30. if ($attr) {
  31. foreach ($attr as $k => $v) {
  32. $attr_info = Dever::load('scm_product/lib/attr')->getInfo($v['attr_id'], $v['attr_value']);
  33. if ($attr_info) {
  34. $info['attr'][] = $attr_info;
  35. }
  36. }
  37. }
  38. # 获取规格
  39. $info['spec'] = Dever::load('scm_product/lib/spec')->getData($info['id'], 'getData');
  40. # 获取价格
  41. $info['sku'] = Dever::load('scm_product/lib/sku')->getData($info['id'], false, $type);
  42. # 获取组合商品
  43. if ($info['type'] > 10) {
  44. $info['goods'] = Dever::json_decode($info['goods']);
  45. } else {
  46. unset($info['goods']);
  47. }
  48. # 获取单位信息
  49. if ($info['unit_id'] > 0) {
  50. $info['unit'] = Dever::db('scm/unit')->find($info['unit_id']);
  51. }
  52. return $info;
  53. }
  54. # 获取商品及其属性列表
  55. public function getSetList($table, $other_where, $cate_id = false, $col = 'goods_id', $set = array(), $price_id = -1, $source = '')
  56. {
  57. $price = Dever::input('price', 0);
  58. $buy_price = Dever::input('buy_price', 0);
  59. $total = Dever::input('total', 1);
  60. if ($cate_id) {
  61. $where['top_category_id'] = $cate_id;
  62. }
  63. $where['status'] = 1;
  64. if (!$source) {
  65. $source = 'scm_product/info';
  66. }
  67. $data = Dever::db($source)->getData($where);
  68. $result = array();
  69. if ($data) {
  70. $i = 0;
  71. foreach ($data as $k => $v) {
  72. $sku = Dever::db('scm_product/info_sku')->select(array('info_id' => $v['id']));
  73. if ($sku) {
  74. $other = array();
  75. if ($table) {
  76. $other_where[$col] = $v['id'];
  77. $other_where['sku_id'] = $sku[0]['id'];
  78. $other = Dever::db($table)->find($other_where);
  79. }
  80. if ($set) {
  81. $result[$i]['set'] = $set;
  82. }
  83. $result[$i]['id'] = $v['id'];
  84. $result[$i]['name'] = $v['name'];
  85. $result[$i]['type'] = $v['type'];
  86. $result[$i]['stock'] = $v['stock'];
  87. $result[$i]['commission'] = $v['commission'];
  88. $result[$i]['sell_type'] = $v['sell_type'];
  89. $result[$i]['buy_type'] = $v['buy_type'];
  90. $result[$i]['old_price'] = $result[$i]['price'] = $price ? $price : $sku[0]['price'];
  91. $result[$i]['old_buy_price'] = $result[$i]['buy_price'] = $buy_price ? $buy_price : $sku[0]['buy_price'];
  92. $result[$i]['old_cost_price'] = $result[$i]['cost_price'] = $sku[0]['cost_price'];
  93. $result[$i]['select'] = 2;
  94. $result[$i]['del'] = 1;
  95. $result[$i]['total'] = isset($v['total']) ? $v['total'] : 0;
  96. if ($price_id > 0 && $sku[0]['key'] == -1) {
  97. list($result[$i]['price'], $result[$i]['buy_price']) = Dever::load('scm_product/lib/price')->get($price_id, $v['id'], $sku[0]['id'], $result[$i]['price'], $result[$i]['buy_price']);
  98. }
  99. if ($other) {
  100. if (isset($other['commission']) && $other['commission']) {
  101. $result[$i]['commission'] = $other['commission'];
  102. }
  103. if (isset($other['price']) && $other['price']) {
  104. $result[$i]['price'] = $other['price'];
  105. }
  106. if (isset($other['buy_price']) && $other['buy_price']) {
  107. $result[$i]['buy_price'] = $other['buy_price'];
  108. }
  109. if (isset($other['cost_price']) && $other['cost_price']) {
  110. $result[$i]['cost_price'] = $other['cost_price'];
  111. }
  112. if (isset($other['sell_min']) && $other['sell_min']) {
  113. $result[$i]['sell_min'] = $other['sell_min'];
  114. }
  115. if (isset($other['buy_min']) && $other['buy_min']) {
  116. $result[$i]['buy_min'] = $other['buy_min'];
  117. }
  118. if (isset($other['source_id']) && $other['source_id']) {
  119. $result[$i]['source_id'] = $other['source_id'];
  120. }
  121. if (isset($other['in_num'])) {
  122. if (isset($other['out_num'])) {
  123. $result[$i]['total'] = $other['in_num'] - $other['out_num'];
  124. } else {
  125. $result[$i]['total'] = $other['in_num'];
  126. }
  127. } elseif (isset($other['num'])) {
  128. $result[$i]['total'] = $other['num'];
  129. }
  130. $result[$i]['select'] = $other['state'];
  131. }
  132. $result[$i]['total'] = $result[$i]['total'] * $total;
  133. $result[$i]['children'] = array();
  134. $num = count($sku);
  135. if ($sku[0]['key'] == -1) {
  136. $result[$i]['id'] .= '-' . $sku[0]['id'];
  137. $result[$i]['old_price'] = $sku[0]['price'];
  138. $result[$i]['old_buy_price'] = $sku[0]['buy_price'];
  139. if (!$price && $result[$i]['price'] <= 0) {
  140. $result[$i]['price'] = $sku[0]['price'];
  141. }
  142. if (!$buy_price && $result[$i]['buy_price'] <= 0) {
  143. $result[$i]['buy_price'] = $sku[0]['buy_price'];
  144. }
  145. //$result[$i]['id'] .= '-' . $sku[0]['id'];
  146. $result[$i]['end'] = true;
  147. } else {
  148. foreach ($sku as $k1 => $v1) {
  149. $v1['sku_name'] = '';
  150. if ($v1['key']) {
  151. $key = str_replace('-', ',', $v1['key']);
  152. $spec = Dever::db('scm_product/info_spec_value')->getGroupData(array('ids' => $key));
  153. $v1['sku_name'] = $spec['name'];
  154. }
  155. $name = $v['name'];
  156. if ($v1['sku_name']) {
  157. $name .= '-' . $v1['sku_name'];
  158. }
  159. $children = array
  160. (
  161. 'id' => $v['id'] . '-' . $v1['id'],
  162. 'name' => $name,
  163. 'stock' => $v['stock'],
  164. 'commission' => $v['commission'],
  165. 'sell_type' => $v['sell_type'],
  166. 'buy_type' => $v['buy_type'],
  167. 'old_price' => $v1['price'],
  168. 'old_buy_price' => $v1['buy_price'],
  169. 'old_cost_price' => $v1['cost_price'],
  170. 'price' => $price ? $price : $v1['price'],
  171. 'buy_price' => $buy_price ? $buy_price : $v1['buy_price'],
  172. 'cost_price' => $v1['cost_price'],
  173. 'type' => $v['type'],
  174. 'del' => 1,
  175. 'end' => true,
  176. 'total' => 0,
  177. );
  178. $other_sku = array();
  179. if ($table) {
  180. $other_where['sku_id'] = $v1['id'];
  181. $other_sku = Dever::db($table)->find($other_where);
  182. }
  183. $children['select'] = 2;
  184. $children['total'] = 0;
  185. if ($set) {
  186. $children['set'] = $set;
  187. }
  188. if ($price_id > 0) {
  189. list($children['price'], $children['buy_price']) = Dever::load('scm_product/lib/price')->get($price_id, $v['id'], $v1['id'], $children['price'], $children['buy_price']);
  190. }
  191. if ($other_sku) {
  192. if (isset($other_sku['commission']) && $other_sku['commission']) {
  193. $children['commission'] = $other_sku['commission'];
  194. }
  195. if (isset($other_sku['price']) && $other_sku['price']) {
  196. $children['price'] = $other_sku['price'];
  197. }
  198. if (isset($other_sku['buy_price']) && $other_sku['buy_price']) {
  199. $children['buy_price'] = $other_sku['buy_price'];
  200. }
  201. if (isset($other_sku['cost_price']) && $other_sku['cost_price']) {
  202. $children['cost_price'] = $other_sku['cost_price'];
  203. }
  204. if (isset($other_sku['sell_min']) && $other_sku['sell_min']) {
  205. $children['sell_min'] = $other_sku['sell_min'];
  206. }
  207. if (isset($other_sku['buy_min']) && $other_sku['buy_min']) {
  208. $children['buy_min'] = $other_sku['buy_min'];
  209. }
  210. if (isset($other_sku['source_id']) && $other_sku['source_id']) {
  211. $children['source_id'] = $other_sku['source_id'];
  212. }
  213. if (isset($other_sku['in_num'])) {
  214. if (isset($other_sku['out_num'])) {
  215. $children['total'] = $other_sku['in_num'] - $other_sku['out_num'];
  216. } else {
  217. $children['total'] = $other_sku['in_num'];
  218. }
  219. } elseif (isset($other_sku['num'])) {
  220. $children['total'] = $other_sku['num'];
  221. }
  222. $children['select'] = $other_sku['state'];
  223. }
  224. $children['total'] = $children['total']*$total;
  225. $result[$i]['children'][] = $children;
  226. }
  227. }
  228. $i++;
  229. }
  230. }
  231. }
  232. return $result;
  233. }
  234. # 获取支付所需要的信息
  235. public function getPayInfo($id, $sku = 0, $seller_id = false)
  236. {
  237. $info = is_array($id) ? $id : Dever::db('scm_product/info')->one($id);
  238. if ($info) {
  239. # 最小购买数1
  240. $info['min'] = 1;
  241. $sku = $sku > 0 ? $sku : -1;
  242. $info['sku_id'] = $sku;
  243. if ($sku > 0) {
  244. $where['info_id'] = $info['id'];
  245. $where['id'] = $sku;
  246. $sku = Dever::db('scm_product/info_sku')->find($where);
  247. if ($sku) {
  248. if ($seller_id) {
  249. $seller = Dever::db('scm_role/seller')->find($seller_id);
  250. if ($seller) {
  251. list($sku['price'], $sku['buy_price']) = Dever::load('scm_product/lib/price')->get($seller['price_id'], $info['id'], $sku['id'], $sku['price'], $sku['buy_price']);
  252. }
  253. }
  254. $info['price'] = $sku['price'];
  255. $info['buy_price'] = $sku['buy_price'];
  256. }
  257. }
  258. /*
  259. $info['freight_id'] = 0;
  260. $info['freight_price'] = 0;
  261. $freight = $this->freight($info, $info['price'], $user, $num);
  262. if ($freight) {
  263. $info['freight_id'] = $freight['id'];
  264. $info['freight_price'] = $freight['price'];
  265. }
  266. */
  267. # 佣金计算
  268. //$info['reward'] = $this->reward($info, $info['price']*$num);
  269. }
  270. return $info;
  271. }
  272. # 获取商品的基本信息,只包括名称-价格等信息
  273. public function getBaseInfo($info, $sku = false, $unit_id = false)
  274. {
  275. $info = is_array($info) ? $info : Dever::db('scm_product/info')->one($info);
  276. $where = array('info_id' => $info['id']);
  277. if ($sku && $sku > 0) {
  278. $where['id'] = $sku;
  279. }
  280. $info['base_unit_id'] = $info['unit_id'];
  281. if ($unit_id) {
  282. $info['unit_id'] = $unit_id;
  283. }
  284. $info['unit'] = '';
  285. if ($info['unit_id']) {
  286. $unit = Dever::db('scm/unit')->find($info['unit_id']);
  287. if ($unit) {
  288. $info['unit'] = $unit['name'];
  289. }
  290. }
  291. if (!$info['unit']) {
  292. $info['unit'] = '个';
  293. }
  294. $info['aname'] = $info['name'];
  295. $info['spec_name'] = array();
  296. $sku = Dever::db('scm_product/info_sku')->select($where);
  297. if ($sku) {
  298. foreach ($sku as $k => $v) {
  299. $key = str_replace('-', ',', $v['key']);
  300. $spec = Dever::db('scm_product/info_spec_value')->getGroupData(array('ids' => $key));
  301. if ($spec) {
  302. $info['aname'] .= '-' . $spec['name'];
  303. $info['spec_name'][] = $spec['name'];
  304. }
  305. if ($sku && $sku > 0) {
  306. $info['code'] = $v['code'];
  307. $info['price'] = $v['price'];
  308. $info['buy_price'] = $v['buy_price'];
  309. $info['cost_price'] = $v['cost_price'];
  310. }
  311. }
  312. }
  313. if (!$info['spec_name']) {
  314. $info['spec_name'] = '无';
  315. } else {
  316. $info['spec_name'] = implode(',', $info['spec_name']);
  317. }
  318. $pic = explode(',', $info['pic']);
  319. $info['cover'] = $pic[0];
  320. $this->getGroup($info);
  321. return $info;
  322. }
  323. # 获取组合商品
  324. public function getGroup(&$info)
  325. {
  326. $info['goods'] = array();
  327. if ($info['type'] == 11 || $info['type'] == 12) {
  328. $goods = Dever::db('scm_product/info_goods')->select(array('info_id' => $info['id']));
  329. $info['goods'] = array();
  330. foreach ($goods as $k => $v) {
  331. $goods_info = $this->getBaseInfo($v['goods_id'], $v['sku_id']);
  332. if ($goods_info) {
  333. $info['goods'][] = array
  334. (
  335. 'id' => $goods_info['id'],
  336. 'code' => $goods_info['code'],
  337. 'name' => $goods_info['aname'],
  338. 'price' => $goods_info['price'],
  339. 'buy_price' => $goods_info['buy_price'],
  340. 'cover' => $goods_info['cover'],
  341. 'unit_id' => $goods_info['unit_id'],
  342. 'num' => $v['num'],
  343. );
  344. }
  345. }
  346. }
  347. }
  348. }