Sku.php 32 KB

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