Sku.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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['single_attr'] = Dever::input('single_attr');
  32. $data['single_key'] = Dever::input('single_key');
  33. $data['input_attr'] = Dever::input('input_attr');
  34. $data['input_key'] = Dever::input('input_key');
  35. $data['hook'] = Dever::input('hook');
  36. $data['set'] = $this->getSet($data['other']);
  37. return $data;
  38. }
  39. private function getSet($other)
  40. {
  41. if ($other) {
  42. if (Dever::project($other)) {
  43. $set = Dever::load($other . '/lib/sku')->set;
  44. if (!isset($set['price_type'])) {
  45. $set['price_type'] = false;
  46. }
  47. } else {
  48. Dever::alert($other . '不存在');
  49. }
  50. } else {
  51. $set = array('col' => $this->col, 'price_type' => false);
  52. }
  53. return $set;
  54. }
  55. public function show()
  56. {
  57. $config = Dever::load('goods/lib/sku')->config();
  58. return $this->table($config['goods_id'], $config['other'], $config['other_id'], $config['set'], true);
  59. }
  60. public function update_api()
  61. {
  62. $config = Dever::load('goods/lib/sku')->config();
  63. $where = array();
  64. if ($config['other']) {
  65. $where[$config['other_col']] = $config['other_id'];
  66. $where['goods_id'] = $config['goods_id'];
  67. $key = 'sku_id';
  68. $table = $config['other'] . '/goods';
  69. } else {
  70. $where['info_id'] = $config['goods_id'];
  71. $key = 'key';
  72. $table = 'goods/info';
  73. }
  74. $sku_table = $table . '_sku';
  75. if ($config['key'] == -1) {
  76. if ($config['other']) {
  77. $info = Dever::db($table)->one($where);
  78. } else {
  79. $info = Dever::db($table)->one($config['goods_id']);
  80. }
  81. $update = $where;
  82. foreach ($config['set']['col'] as $k => $v) {
  83. $update[$k] = Dever::input($k);
  84. }
  85. if ($info && $update) {
  86. $update['where_id'] = $info['id'];
  87. Dever::db($table)->update($update);
  88. unset($update['where_id']);
  89. }
  90. $where[$key] = -1;
  91. $info = Dever::db($sku_table)->one($where);
  92. $update[$key] = $where[$key];
  93. Dever::config('base')->hook = false;
  94. if (!$info) {
  95. $state = Dever::db($sku_table)->insert($update);
  96. } else {
  97. $update['where_id'] = $info['id'];
  98. $state = Dever::db($sku_table)->update($update);
  99. }
  100. } elseif ($config['key']) {
  101. if (!is_array($config['key'])) {
  102. $config['key'] = explode(',', $config['key']);
  103. }
  104. $min = array();
  105. $cur_sku = false;
  106. $sku_id = false;
  107. foreach ($config['key'] as $k => $v) {
  108. $type = 1;
  109. if (strstr($v, '||')) {
  110. $temp = explode('||', $v);
  111. $v = $temp[0];
  112. $type = $temp[1];
  113. }
  114. if (isset($config['attr'][$k])) {
  115. $data = $where;
  116. if ($config['other']) {
  117. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  118. if (!$sku) {
  119. continue;
  120. }
  121. $data[$key] = $sku['id'];
  122. $info = Dever::db($sku_table)->one($data);
  123. } else {
  124. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  125. $data['attr'] = $config['attr'][$k];
  126. $data[$key] = $v;
  127. }
  128. $data['type'] = $type;
  129. foreach ($config['set']['col'] as $k1 => $v1) {
  130. $value = Dever::input($k1);
  131. $cur_sku = false;
  132. if (isset($value[$k])) {
  133. $data[$k1] = $value[$k];
  134. if ($k1 == $this->main) {
  135. if (!isset($min[$k1])) {
  136. $min = $data;
  137. $cur_sku = true;
  138. } elseif ($min[$k1] > $data[$k1]) {
  139. $min = $data;
  140. $cur_sku = true;
  141. }
  142. }
  143. }
  144. }
  145. Dever::config('base')->hook = false;
  146. if (!$info) {
  147. $temp = Dever::db($sku_table)->insert($data);
  148. if ($cur_sku) {
  149. $sku_id = $temp;
  150. }
  151. } else {
  152. $data['where_id'] = $info['id'];
  153. if ($cur_sku) {
  154. $sku_id = $data['where_id'];
  155. }
  156. Dever::db($sku_table)->update($data);
  157. }
  158. }
  159. }
  160. if (!$config['other'] && $min) {
  161. $update = array();
  162. if ($sku_id) {
  163. $update['sku_id'] = $sku_id;
  164. }
  165. foreach ($config['set']['col'] as $k => $v) {
  166. if (isset($min[$k])) {
  167. $update[$k] = $min[$k];
  168. }
  169. }
  170. $update['where_id'] = $config['goods_id'];
  171. Dever::db($table)->update($update);
  172. }
  173. }
  174. if ($config['single_key']) {
  175. if (!is_array($config['single_key'])) {
  176. $config['single_key'] = explode(',', $config['single_key']);
  177. }
  178. foreach ($config['single_key'] as $k => $v) {
  179. $type = 2;
  180. if (strstr($v, '||')) {
  181. $temp = explode('||', $v);
  182. $v = $temp[0];
  183. $type = $temp[1];
  184. }
  185. if (isset($config['single_attr'][$k])) {
  186. $data = $where;
  187. if ($config['other']) {
  188. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  189. if (!$sku) {
  190. continue;
  191. }
  192. $data[$key] = $sku['id'];
  193. $info = Dever::db($sku_table)->one($data);
  194. } else {
  195. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  196. $data['attr'] = $config['single_attr'][$k];
  197. $data[$key] = $v;
  198. }
  199. $data['type'] = $type;
  200. foreach ($config['set']['col'] as $k1 => $v1) {
  201. $value = Dever::input('single_' . $k1);
  202. if (isset($value[$k])) {
  203. $data[$k1] = $value[$k];
  204. }
  205. }
  206. Dever::config('base')->hook = false;
  207. if (!$info) {
  208. $temp = Dever::db($sku_table)->insert($data);
  209. } else {
  210. $data['where_id'] = $info['id'];
  211. Dever::db($sku_table)->update($data);
  212. }
  213. }
  214. }
  215. }
  216. if ($config['input_key']) {
  217. if (!is_array($config['input_key'])) {
  218. $config['input_key'] = explode(',', $config['input_key']);
  219. }
  220. foreach ($config['input_key'] as $k => $v) {
  221. $type = 3;
  222. if (strstr($v, '||')) {
  223. $temp = explode('||', $v);
  224. $v = $temp[0];
  225. $type = $temp[1];
  226. }
  227. if (isset($config['input_key'][$k])) {
  228. $data = $where;
  229. if ($config['other']) {
  230. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  231. if (!$sku) {
  232. continue;
  233. }
  234. $data[$key] = $sku['id'];
  235. $info = Dever::db($sku_table)->one($data);
  236. } else {
  237. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  238. $data['attr'] = $config['input_key'][$k];
  239. $data[$key] = $v;
  240. }
  241. $data['type'] = $type;
  242. foreach ($config['set']['col'] as $k1 => $v1) {
  243. $value = Dever::input('input_' . $k1);
  244. if (isset($value[$k])) {
  245. $data[$k1] = $value[$k];
  246. }
  247. }
  248. Dever::config('base')->hook = false;
  249. if (!$info) {
  250. $temp = Dever::db($sku_table)->insert($data);
  251. } else {
  252. $data['where_id'] = $info['id'];
  253. Dever::db($sku_table)->update($data);
  254. }
  255. }
  256. }
  257. }
  258. Dever::config('base')->hook = true;
  259. Dever::out('yes');
  260. }
  261. public function table($goods, $other = false, $other_id = false, $set = false, $update = true, $parent_goods = false)
  262. {
  263. if (!$set) {
  264. $set = $this->getSet($other);
  265. }
  266. $info = is_array($goods) ? $goods : Dever::db('goods/info')->one($goods);
  267. $info = Info::init(-1)->info($info, 'view_reorder');
  268. if ($info && $other && isset($set['price_type']) && $set['price_type']) {
  269. $price_type = explode(',', $set['price_type']);
  270. if (in_array($info['price_type'], $price_type)) {
  271. if ($update) {
  272. return '组合商品无法设置库存';
  273. }
  274. if ($info['goods'] && is_array($info['goods'])) {
  275. $html = '';
  276. foreach ($info['goods'] as $k => $v) {
  277. $html .= $this->table($v['id'], $other, $other_id, $set, $update, $v) . '<br />';
  278. }
  279. $html .= '*组合商品的库存为以上商品中的最低库存为准';
  280. return $html;
  281. } else {
  282. return '';
  283. }
  284. }
  285. }
  286. $input = '';
  287. if ($other) {
  288. $other_col = $other . '_id';
  289. $sku_value = Dever::db($other . '/goods_sku')->getData(array('goods_id' => $info['id'], $other_col => $other_id));
  290. $input = '<input type="hidden" name="goods_id" value="'.$info['id'].'"/><input type="hidden" name="'.$other_col.'" value="'.$other_id.'"/>';
  291. }
  292. $sku = Dever::db('goods/info_sku')->getDataByKeys(array('info_id' => $info['id']));
  293. $group_attr = $single_attr = $input_attr = array();
  294. $head = $single_head = $input_head = '<thead><tr>';
  295. $html = '请先选择属性';
  296. if ($parent_goods) {
  297. $head .= '<th>商品名称</th>';
  298. $single_head .= '<th>商品名称</th>';
  299. $input_head .= '<th>商品名称</th>';
  300. }
  301. if (isset($info['group_attr']) && $info['group_attr']) {
  302. foreach ($info['group_attr'] as $k => $v) {
  303. if (isset($info['attr'][$v]['option'])) {
  304. $group_attr[$k] = $info['attr'][$v]['option'];
  305. $head .= '<th>'.$info['attr'][$v]['name'].'</th>';
  306. }
  307. }
  308. }
  309. if (isset($info['single_attr']) && $info['single_attr']) {
  310. $single_head .= '<th>属性名称</th>';
  311. foreach ($info['single_attr'] as $k => $v) {
  312. if (isset($info['attr'][$v]['option'])) {
  313. $single_attr = array_merge($single_attr, $info['attr'][$v]['option']);
  314. }
  315. }
  316. }
  317. if (isset($info['input_attr']) && $info['input_attr']) {
  318. $input_head .= '<th>属性名称</th>';
  319. $input_head .= '<th>起算</th>';
  320. foreach ($info['input_attr'] as $k => $v) {
  321. if (isset($info['attr'][$v]['option'])) {
  322. $input_attr[$k] = $info['attr'][$v];
  323. }
  324. }
  325. }
  326. $col_num = 0;
  327. foreach ($set['col'] as $k => $v) {
  328. if (!$update && strstr($v, '^')) {
  329. continue;
  330. }
  331. $col_num++;
  332. if (strstr($v, '^')) {
  333. $v = str_replace('^', '', $v);
  334. }
  335. if (strstr($v, '|')) {
  336. $v = str_replace('|', '', $v);
  337. }
  338. $head .= '<th>'.$v.'</th>';
  339. $single_head .= '<th>'.$v.'</th>';
  340. $input_head .= '<th>'.$v.'</th>';
  341. }
  342. $head .= '</tr></thead>';
  343. $single_head .= '</tr></thead>';
  344. $input_head .= '</tr></thead>';
  345. $body = '<tbody>';
  346. $single_body = '<tbody>';
  347. $input_body = '<tbody>';
  348. if (!$group_attr) {
  349. $prefix = '[总价设置]:';
  350. if (!$update && $col_num <= 1) {
  351. $head = '';
  352. }
  353. if ($update) {
  354. $input .= '<input type="hidden" name="key" value="-1"/>';
  355. }
  356. $body .= '<tr>';
  357. if ($parent_goods) {
  358. $body .= '<td width="10">'.$parent_goods['name'].'</td>';
  359. }
  360. foreach ($set['col'] as $k1 => $v1) {
  361. if (!$update && strstr($v1, '^')) {
  362. continue;
  363. }
  364. $value = '';
  365. if (isset($sku_value[-1]) && isset($sku_value[-1][$k1])) {
  366. $value = $sku_value[-1][$k1];
  367. } elseif (isset($sku[-1]) && isset($sku[-1][$k1])) {
  368. $value = $sku[-1][$k1];
  369. }
  370. if ($update) {
  371. if (strstr($v1, '|')) {
  372. $v1 = str_replace('|', '', $v1);
  373. $body .= '<td width="30">'.$value.'</td>';
  374. } else {
  375. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'" value="'.$value.'"/></td>';
  376. }
  377. } elseif ($col_num > 1) {
  378. $body .= '<td width="30">'.$value.'</td>';
  379. } else {
  380. $body .= $value;
  381. }
  382. }
  383. $body .= '</tr>';
  384. } else {
  385. $prefix = '[组合价格设置]:';
  386. $group_attr = Dever::cartesian($group_attr);
  387. $show = false;
  388. foreach ($group_attr as $k => $v) {
  389. $body .= '<tr data-row="' .($k+1). '">';
  390. if ($parent_goods) {
  391. $body .= '<td>'.$parent_goods['name'].'</td>';
  392. }
  393. $key = $id = array();
  394. foreach ($v['name'] as $k1 => $v1) {
  395. $rows = 1;
  396. $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
  397. $id[] = array
  398. (
  399. 'id' => $v['id'][$k1],
  400. 'attr_id' => $v['info_id'][$k1],
  401. );
  402. $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
  403. }
  404. $key = implode('_', $key);
  405. foreach ($set['col'] as $k1 => $v1) {
  406. if (!$update && strstr($v1, '^')) {
  407. continue;
  408. }
  409. $value = '';
  410. if (isset($sku[$key])) {
  411. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  412. $value = $sku_value[$sku[$key]['id']][$k1];
  413. } elseif (isset($sku[$key][$k1])) {
  414. $value = $sku[$key][$k1];
  415. }
  416. }
  417. if ($update) {
  418. if (strstr($v1, '|')) {
  419. $v1 = str_replace('|', '', $v1);
  420. $body .= '<td width="30">'.$value.'</td>';
  421. } else {
  422. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  423. }
  424. } else {
  425. $body .= '<td width="30">'.$value.'</td>';
  426. }
  427. }
  428. if ($update) {
  429. $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
  430. }
  431. $body .= '</tr>';
  432. }
  433. }
  434. $body .= $input;
  435. $html = $head . $body;
  436. if ($update) {
  437. $html = '<table class="layui-table">' . $html . '</table>';
  438. } else {
  439. $html = '<table class="layui-table">' . $html . '</table>';
  440. }
  441. if ($single_attr) {
  442. $html = $prefix . $html;
  443. foreach ($single_attr as $k => $v) {
  444. $id = array();
  445. $single_body .= '<tr data-row="' .($k+1). '">';
  446. if ($parent_goods) {
  447. $single_body .= '<td>'.$parent_goods['name'].'</td>';
  448. }
  449. $rows = 1;
  450. $single_body .= '<td width="100" rowspan="'.$rows.'">'. $v['parent_name'].'->'. $v['name'].'</td>';
  451. $id[] = array
  452. (
  453. 'id' => $v['id'],
  454. 'attr_id' => $v['info_id'],
  455. );
  456. $key = $v['info_id'] . '-' . $v['id'];
  457. foreach ($set['col'] as $k1 => $v1) {
  458. if (!$update && strstr($v1, '^')) {
  459. continue;
  460. }
  461. $value = '';
  462. if (isset($sku[$key])) {
  463. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  464. $value = $sku_value[$sku[$key]['id']][$k1];
  465. } elseif (isset($sku[$key][$k1])) {
  466. $value = $sku[$key][$k1];
  467. }
  468. }
  469. if ($update) {
  470. if (strstr($v1, '|')) {
  471. $v1 = str_replace('|', '', $v1);
  472. $single_body .= '<td width="30">'.$value.'</td>';
  473. } else {
  474. $single_body .= '<td width="30"><input type="text" class="layui-input" name="single_'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  475. }
  476. } else {
  477. $single_body .= '<td width="30">'.$value.'</td>';
  478. }
  479. }
  480. if ($update) {
  481. $single_body .= '<input type="hidden" name="single_key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="single_attr['.$k.']">'.json_encode($id).'</textarea>';
  482. }
  483. $single_body .= '</tr>';
  484. }
  485. $html = $html . '<br />[单选价格设置]:<table class="layui-table">' . $single_head . $single_body . '</table>';;
  486. }
  487. if ($input_attr) {
  488. foreach ($input_attr as $k => $v) {
  489. if ($v['sell_compute'] == 2) {
  490. $v['sell_compute_type'] = '+';
  491. } elseif ($v['sell_compute'] == 3) {
  492. $v['sell_compute_type'] = '-';
  493. } else {
  494. $v['sell_compute_type'] = '*';
  495. }
  496. $id = array();
  497. $input_body .= '<tr data-row="' .($k+1). '">';
  498. if ($parent_goods) {
  499. $input_body .= '<td>'.$parent_goods['name'].'</td>';
  500. }
  501. $rows = 1;
  502. $key = $id = $name = array();
  503. foreach ($v['option'] as $k1 => $v1) {
  504. $id[] = array
  505. (
  506. 'id' => $v1['id'],
  507. 'attr_id' => $v1['info_id'],
  508. );
  509. $key[] = $v1['price_key'];
  510. $name[] = $v1['name'];
  511. }
  512. $key = implode('_', $key);
  513. $input_body .= '<td width="100" rowspan="'.$rows.'">'. $v['name'].'->'. implode($v['sell_compute_type'], $name).'</td>';
  514. $input_body .= '<td width="5"> >='.$v['sell_value'].'</td>';
  515. foreach ($set['col'] as $k1 => $v1) {
  516. if (!$update && strstr($v1, '^')) {
  517. continue;
  518. }
  519. $value = '';
  520. if (isset($sku[$key])) {
  521. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  522. $value = $sku_value[$sku[$key]['id']][$k1];
  523. } elseif (isset($sku[$key][$k1])) {
  524. $value = $sku[$key][$k1];
  525. }
  526. }
  527. if ($update) {
  528. if (strstr($v1, '|')) {
  529. $v1 = str_replace('|', '', $v1);
  530. $input_body .= '<td width="30">'.$value.'</td>';
  531. } else {
  532. $input_body .= '<td width="30"><input type="text" class="layui-input" name="input_'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  533. }
  534. } else {
  535. $input_body .= '<td width="30">'.$value.'</td>';
  536. }
  537. }
  538. if ($update) {
  539. $input_body .= '<input type="hidden" name="input_key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="input_attr['.$k.']">'.json_encode($id).'</textarea>';
  540. }
  541. $input_body .= '</tr>';
  542. }
  543. $html = $html . '<br />[计算价设置]:<table class="layui-table">' . $input_head . $input_body . '</table>';;
  544. }
  545. return $html;
  546. }
  547. private function create_table($head, $body)
  548. {
  549. $html = '<table class="layui-table">';
  550. }
  551. }