Sku.php 26 KB

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