Sku.php 26 KB

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