Sku.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace Goods\Lib;
  3. use Dever;
  4. class Sku
  5. {
  6. # 设置sku特殊字段
  7. private $col = array
  8. (
  9. 's_price' => '市场价',
  10. 'f_price' => '采购价',
  11. 'price' => '销售价',
  12. );
  13. # 主要价格字段
  14. private $main = 'price';
  15. public function __construct()
  16. {
  17. # 设置权限
  18. Dever::load('manage/auth.init');
  19. }
  20. public function config()
  21. {
  22. $data['action'] = Dever::url('goods/lib/sku.update');
  23. $data['goods_id'] = Dever::input('goods_id');
  24. $data['other'] = Dever::input('other');
  25. $data['other_id'] = Dever::input('other_id');
  26. if ($data['other']) {
  27. $data['other_col'] = $data['other'] . '_id';
  28. }
  29. $data['attr'] = Dever::input('attr');
  30. $data['key'] = Dever::input('key');
  31. $data['option_attr'] = Dever::input('option_attr');
  32. $data['option_key'] = Dever::input('option_key');
  33. $data['hook'] = Dever::input('hook');
  34. $data['set'] = $this->getSet($data['other']);
  35. return $data;
  36. }
  37. private function getSet($other)
  38. {
  39. if ($other) {
  40. if (Dever::project($other)) {
  41. $set = Dever::load($other . '/lib/sku')->set;
  42. if (!isset($set['price_type'])) {
  43. $set['price_type'] = false;
  44. }
  45. } else {
  46. Dever::alert($other . '不存在');
  47. }
  48. } else {
  49. $set = array('col' => $this->col, 'price_type' => false);
  50. }
  51. return $set;
  52. }
  53. public function show()
  54. {
  55. $config = Dever::load('goods/lib/sku')->config();
  56. return $this->table($config['goods_id'], $config['other'], $config['other_id'], $config['set'], true);
  57. }
  58. public function update_api()
  59. {
  60. $config = Dever::load('goods/lib/sku')->config();
  61. $where = array();
  62. if ($config['other']) {
  63. $where[$config['other_col']] = $config['other_id'];
  64. $where['goods_id'] = $config['goods_id'];
  65. $key = 'sku_id';
  66. $table = $config['other'] . '/goods';
  67. } else {
  68. $where['info_id'] = $config['goods_id'];
  69. $key = 'key';
  70. $table = 'goods/info';
  71. }
  72. $sku_table = $table . '_sku';
  73. if ($config['key'] == -1) {
  74. if ($config['other']) {
  75. $info = Dever::db($table)->one($where);
  76. } else {
  77. $info = Dever::db($table)->one($config['goods_id']);
  78. }
  79. $update = $where;
  80. foreach ($config['set']['col'] as $k => $v) {
  81. $update[$k] = Dever::input($k);
  82. }
  83. if ($info && $update) {
  84. $update['where_id'] = $info['id'];
  85. Dever::db($table)->update($update);
  86. unset($update['where_id']);
  87. }
  88. $where[$key] = -1;
  89. $info = Dever::db($sku_table)->one($where);
  90. $update[$key] = $where[$key];
  91. Dever::config('base')->hook = false;
  92. if (!$info) {
  93. $state = Dever::db($sku_table)->insert($update);
  94. } else {
  95. $update['where_id'] = $info['id'];
  96. $state = Dever::db($sku_table)->update($update);
  97. }
  98. } elseif ($config['key']) {
  99. if (!is_array($config['key'])) {
  100. $config['key'] = explode(',', $config['key']);
  101. }
  102. $min = array();
  103. $cur_sku = false;
  104. $sku_id = false;
  105. foreach ($config['key'] as $k => $v) {
  106. $type = 1;
  107. if (strstr($v, '||')) {
  108. $temp = explode('||', $v);
  109. $v = $temp[0];
  110. $type = $temp[1];
  111. }
  112. if (isset($config['attr'][$k])) {
  113. $data = $where;
  114. if ($config['other']) {
  115. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  116. if (!$sku) {
  117. continue;
  118. }
  119. $data[$key] = $sku['id'];
  120. $info = Dever::db($sku_table)->one($data);
  121. } else {
  122. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  123. $data['attr'] = $config['attr'][$k];
  124. $data[$key] = $v;
  125. }
  126. $data['type'] = $type;
  127. foreach ($config['set']['col'] as $k1 => $v1) {
  128. $value = Dever::input($k1);
  129. $cur_sku = false;
  130. if (isset($value[$k])) {
  131. $data[$k1] = $value[$k];
  132. if ($k1 == $this->main) {
  133. if (!isset($min[$k1])) {
  134. $min = $data;
  135. $cur_sku = true;
  136. } elseif ($min[$k1] > $data[$k1]) {
  137. $min = $data;
  138. $cur_sku = true;
  139. }
  140. }
  141. }
  142. }
  143. Dever::config('base')->hook = false;
  144. if (!$info) {
  145. $temp = Dever::db($sku_table)->insert($data);
  146. if ($cur_sku) {
  147. $sku_id = $temp;
  148. }
  149. } else {
  150. $data['where_id'] = $info['id'];
  151. if ($cur_sku) {
  152. $sku_id = $data['where_id'];
  153. }
  154. Dever::db($sku_table)->update($data);
  155. }
  156. }
  157. }
  158. if (!$config['other'] && $min) {
  159. $update = array();
  160. if ($sku_id) {
  161. $update['sku_id'] = $sku_id;
  162. }
  163. foreach ($config['set']['col'] as $k => $v) {
  164. if (isset($min[$k])) {
  165. $update[$k] = $min[$k];
  166. }
  167. }
  168. $update['where_id'] = $config['goods_id'];
  169. Dever::db($table)->update($update);
  170. }
  171. }
  172. if ($config['option_key']) {
  173. if (!is_array($config['option_key'])) {
  174. $config['option_key'] = explode(',', $config['option_key']);
  175. }
  176. foreach ($config['option_key'] as $k => $v) {
  177. $type = 3;
  178. if (strstr($v, '||')) {
  179. $temp = explode('||', $v);
  180. $v = $temp[0];
  181. $type = $temp[1];
  182. }
  183. if (isset($config['option_attr'][$k])) {
  184. $data = $where;
  185. if ($config['other']) {
  186. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  187. if (!$sku) {
  188. continue;
  189. }
  190. $data[$key] = $sku['id'];
  191. $info = Dever::db($sku_table)->one($data);
  192. } else {
  193. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  194. $data['attr'] = $config['option_attr'][$k];
  195. $data[$key] = $v;
  196. }
  197. $data['type'] = $type;
  198. foreach ($config['set']['col'] as $k1 => $v1) {
  199. $value = Dever::input('option_' . $k1);
  200. if (isset($value[$k])) {
  201. $data[$k1] = $value[$k];
  202. }
  203. }
  204. Dever::config('base')->hook = false;
  205. if (!$info) {
  206. $temp = Dever::db($sku_table)->insert($data);
  207. } else {
  208. $data['where_id'] = $info['id'];
  209. Dever::db($sku_table)->update($data);
  210. }
  211. }
  212. }
  213. }
  214. Dever::config('base')->hook = true;
  215. Dever::out('yes');
  216. }
  217. public function table($goods, $other = false, $other_id = false, $set = false, $update = true, $parent_goods = false)
  218. {
  219. if (!$set) {
  220. $set = $this->getSet($other);
  221. }
  222. $info = is_array($goods) ? $goods : Dever::db('goods/info')->one($goods);
  223. $info = Info::init(-1)->info($info, 'view_reorder');
  224. if ($info && $other && isset($set['price_type']) && $set['price_type']) {
  225. $price_type = explode(',', $set['price_type']);
  226. if (in_array($info['price_type'], $price_type)) {
  227. if ($update) {
  228. return '组合商品无法设置库存';
  229. }
  230. if ($info['goods'] && is_array($info['goods'])) {
  231. $html = '';
  232. foreach ($info['goods'] as $k => $v) {
  233. $html .= $this->table($v['id'], $other, $other_id, $set, $update, $v) . '<br />';
  234. }
  235. $html .= '*组合商品的库存为以上商品中的最低库存为准';
  236. return $html;
  237. } else {
  238. return '';
  239. }
  240. }
  241. }
  242. $input = '';
  243. if ($other) {
  244. $other_col = $other . '_id';
  245. $sku_value = Dever::db($other . '/goods_sku')->getData(array('goods_id' => $info['id'], $other_col => $other_id));
  246. $input = '<input type="hidden" name="goods_id" value="'.$info['id'].'"/><input type="hidden" name="'.$other_col.'" value="'.$other_id.'"/>';
  247. }
  248. $sku = Dever::db('goods/info_sku')->getDataByKeys(array('info_id' => $info['id']));
  249. $sell_attr = $option_attr = array();
  250. $head = $option_head = '<thead><tr>';
  251. $html = '请先选择属性';
  252. if ($parent_goods) {
  253. $head .= '<th>商品名称</th>';
  254. $option_head .= '<th>商品名称</th>';
  255. }
  256. if (isset($info['sell_attr']) && $info['sell_attr']) {
  257. foreach ($info['sell_attr'] as $k => $v) {
  258. $head .= '<th>'.$v['name'].'</th>';
  259. if (isset($v['option_sku'])) {
  260. $sell_attr[$k] = $v['option_sku'];
  261. }
  262. }
  263. }
  264. if (isset($info['option_attr']) && $info['option_attr']) {
  265. $option_head .= '<th>属性名称</th>';
  266. foreach ($info['option_attr'] as $k => $v) {
  267. if (isset($v['option_sku'])) {
  268. $option_attr = array_merge($option_attr, $v['option_sku']);
  269. }
  270. }
  271. }
  272. $col_num = 0;
  273. foreach ($set['col'] as $k => $v) {
  274. if (!$update && strstr($v, '^')) {
  275. continue;
  276. }
  277. $col_num++;
  278. if (strstr($v, '^')) {
  279. $v = str_replace('^', '', $v);
  280. }
  281. if (strstr($v, '|')) {
  282. $v = str_replace('|', '', $v);
  283. }
  284. $head .= '<th>'.$v.'</th>';
  285. $option_head .= '<th>'.$v.'</th>';
  286. }
  287. $head .= '</tr></thead>';
  288. $option_head .= '</tr></thead>';
  289. $body = '<tbody>';
  290. $option_body = '<tbody>';
  291. if (!$sell_attr) {
  292. $prefix = '[单价设置]:';
  293. if (!$update && $col_num <= 1) {
  294. $head = '';
  295. }
  296. if ($update) {
  297. $input .= '<input type="hidden" name="key" value="-1"/>';
  298. }
  299. $body .= '<tr>';
  300. if ($parent_goods) {
  301. $body .= '<td width="10">'.$parent_goods['name'].'</td>';
  302. }
  303. foreach ($set['col'] as $k1 => $v1) {
  304. if (!$update && strstr($v1, '^')) {
  305. continue;
  306. }
  307. $value = '';
  308. if (isset($sku_value[-1]) && isset($sku_value[-1][$k1])) {
  309. $value = $sku_value[-1][$k1];
  310. } elseif (isset($sku[-1]) && isset($sku[-1][$k1])) {
  311. $value = $sku[-1][$k1];
  312. }
  313. if ($update) {
  314. if (strstr($v1, '|')) {
  315. $v1 = str_replace('|', '', $v1);
  316. $body .= '<td width="30">'.$value.'</td>';
  317. } else {
  318. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'" value="'.$value.'"/></td>';
  319. }
  320. } elseif ($col_num > 1) {
  321. $body .= '<td width="30">'.$value.'</td>';
  322. } else {
  323. $body .= $value;
  324. }
  325. }
  326. $body .= '</tr>';
  327. } else {
  328. $prefix = '[多价设置]:';
  329. $sell_attr = Dever::cartesian($sell_attr);
  330. $show = false;
  331. foreach ($sell_attr as $k => $v) {
  332. $body .= '<tr data-row="' .($k+1). '">';
  333. if ($parent_goods) {
  334. $body .= '<td>'.$parent_goods['name'].'</td>';
  335. }
  336. $key = $id = array();
  337. foreach ($v['name'] as $k1 => $v1) {
  338. $rows = 1;
  339. $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
  340. $id[] = array
  341. (
  342. 'id' => $v['id'][$k1],
  343. 'attr_id' => $v['info_id'][$k1],
  344. );
  345. $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
  346. }
  347. $key = implode('_', $key);
  348. foreach ($set['col'] as $k1 => $v1) {
  349. if (!$update && strstr($v1, '^')) {
  350. continue;
  351. }
  352. $value = '';
  353. if (isset($sku[$key])) {
  354. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  355. $value = $sku_value[$sku[$key]['id']][$k1];
  356. } elseif (isset($sku[$key][$k1])) {
  357. $value = $sku[$key][$k1];
  358. }
  359. }
  360. if ($update) {
  361. if (strstr($v1, '|')) {
  362. $v1 = str_replace('|', '', $v1);
  363. $body .= '<td width="30">'.$value.'</td>';
  364. } else {
  365. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  366. }
  367. } else {
  368. $body .= '<td width="30">'.$value.'</td>';
  369. }
  370. }
  371. if ($update) {
  372. $key .= '||2';
  373. $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
  374. }
  375. $body .= '</tr>';
  376. }
  377. }
  378. $body .= $input;
  379. $html = $head . $body;
  380. if ($update) {
  381. $html = '<table class="layui-table">' . $html . '</table>';
  382. } else {
  383. $html = '<table class="layui-table">' . $html . '</table>';
  384. }
  385. if ($option_attr) {
  386. $html = $prefix . $html;
  387. foreach ($option_attr as $k => $v) {
  388. $id = array();
  389. $option_body .= '<tr data-row="' .($k+1). '">';
  390. if ($parent_goods) {
  391. $option_body .= '<td>'.$parent_goods['name'].'</td>';
  392. }
  393. $rows = 1;
  394. $option_body .= '<td width="50" rowspan="'.$rows.'">'. $v['parent_name'].'->'. $v['name'].'</td>';
  395. $id[] = array
  396. (
  397. 'id' => $v['id'],
  398. 'attr_id' => $v['info_id'],
  399. );
  400. $key = $v['info_id'] . '-' . $v['id'];
  401. foreach ($set['col'] as $k1 => $v1) {
  402. if (!$update && strstr($v1, '^')) {
  403. continue;
  404. }
  405. $value = '';
  406. if (isset($sku[$key])) {
  407. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  408. $value = $sku_value[$sku[$key]['id']][$k1];
  409. } elseif (isset($sku[$key][$k1])) {
  410. $value = $sku[$key][$k1];
  411. }
  412. }
  413. if ($update) {
  414. if (strstr($v1, '|')) {
  415. $v1 = str_replace('|', '', $v1);
  416. $option_body .= '<td width="30">'.$value.'</td>';
  417. } else {
  418. $option_body .= '<td width="30"><input type="text" class="layui-input" name="option_'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  419. }
  420. } else {
  421. $option_body .= '<td width="30">'.$value.'</td>';
  422. }
  423. }
  424. if ($update) {
  425. $key .= '||3';
  426. $option_body .= '<input type="hidden" name="option_key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="option_attr['.$k.']">'.json_encode($id).'</textarea>';
  427. }
  428. $option_body .= '</tr>';
  429. }
  430. $html = $html . '<br />[加价设置]:<table class="layui-table">' . $option_head . $option_body . '</table>';;
  431. }
  432. return $html;
  433. }
  434. }