Sku.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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['hook'] = Dever::input('hook');
  32. $data['set'] = $this->getSet($data['other']);
  33. return $data;
  34. }
  35. private function getSet($other)
  36. {
  37. if ($other) {
  38. if (Dever::project($other)) {
  39. $set = Dever::load($other . '/lib/sku')->set;
  40. if (!isset($set['price_type'])) {
  41. $set['price_type'] = false;
  42. }
  43. } else {
  44. Dever::alert($other . '不存在');
  45. }
  46. } else {
  47. $set = array('col' => $this->col, 'price_type' => false);
  48. }
  49. return $set;
  50. }
  51. public function show()
  52. {
  53. $config = Dever::load('goods/lib/sku')->config();
  54. return $this->table($config['goods_id'], $config['other'], $config['other_id'], $config['set'], true);
  55. }
  56. public function update_api()
  57. {
  58. $config = Dever::load('goods/lib/sku')->config();
  59. $where = array();
  60. if ($config['other']) {
  61. $where[$config['other_col']] = $config['other_id'];
  62. $where['goods_id'] = $config['goods_id'];
  63. $key = 'sku_id';
  64. $table = $config['other'] . '/goods';
  65. } else {
  66. $where['info_id'] = $config['goods_id'];
  67. $key = 'key';
  68. $table = 'goods/info';
  69. }
  70. $sku_table = $table . '_sku';
  71. if ($config['key'] == -1) {
  72. if ($config['other']) {
  73. $info = Dever::db($table)->one($where);
  74. } else {
  75. $info = Dever::db($table)->one($config['goods_id']);
  76. }
  77. $update = $where;
  78. foreach ($config['set']['col'] as $k => $v) {
  79. $update[$k] = Dever::input($k);
  80. }
  81. if ($info && $update) {
  82. $update['where_id'] = $info['id'];
  83. Dever::db($table)->update($update);
  84. unset($update['where_id']);
  85. }
  86. $where[$key] = -1;
  87. $info = Dever::db($sku_table)->one($where);
  88. $update[$key] = $where[$key];
  89. Dever::config('base')->hook = false;
  90. if (!$info) {
  91. $state = Dever::db($sku_table)->insert($update);
  92. } else {
  93. $update['where_id'] = $info['id'];
  94. $state = Dever::db($sku_table)->update($update);
  95. }
  96. } elseif ($config['key']) {
  97. if (!is_array($config['key'])) {
  98. $config['key'] = explode(',', $config['key']);
  99. }
  100. $min = array();
  101. $cur_sku = false;
  102. $sku_id = false;
  103. foreach ($config['key'] as $k => $v) {
  104. if (isset($config['attr'][$k])) {
  105. $data = $where;
  106. if ($config['other']) {
  107. $sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  108. if (!$sku) {
  109. continue;
  110. }
  111. $data[$key] = $sku['id'];
  112. $info = Dever::db($sku_table)->one($data);
  113. } else {
  114. $info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
  115. $data['attr'] = $config['attr'][$k];
  116. $data[$key] = $v;
  117. }
  118. foreach ($config['set']['col'] as $k1 => $v1) {
  119. $value = Dever::input($k1);
  120. $cur_sku = false;
  121. if (isset($value[$k])) {
  122. $data[$k1] = $value[$k];
  123. if ($k1 == $this->main) {
  124. if (!isset($min[$k1])) {
  125. $min = $data;
  126. $cur_sku = true;
  127. } elseif ($min[$k1] > $data[$k1]) {
  128. $min = $data;
  129. $cur_sku = true;
  130. }
  131. }
  132. }
  133. }
  134. Dever::config('base')->hook = false;
  135. if (!$info) {
  136. $temp = Dever::db($sku_table)->insert($data);
  137. if ($cur_sku) {
  138. $sku_id = $temp;
  139. }
  140. } else {
  141. $data['where_id'] = $info['id'];
  142. if ($cur_sku) {
  143. $sku_id = $data['where_id'];
  144. }
  145. Dever::db($sku_table)->update($data);
  146. }
  147. }
  148. }
  149. if (!$config['other'] && $min) {
  150. $update = array();
  151. if ($sku_id) {
  152. $update['sku_id'] = $sku_id;
  153. }
  154. foreach ($config['set']['col'] as $k => $v) {
  155. if (isset($min[$k])) {
  156. $update[$k] = $min[$k];
  157. }
  158. }
  159. $update['where_id'] = $config['goods_id'];
  160. Dever::db($table)->update($update);
  161. }
  162. }
  163. Dever::config('base')->hook = true;
  164. Dever::out('yes');
  165. }
  166. public function table($goods, $other = false, $other_id = false, $set = false, $update = true, $parent_goods = false)
  167. {
  168. if (!$set) {
  169. $set = $this->getSet($other);
  170. }
  171. $info = is_array($goods) ? $goods : Dever::db('goods/info')->one($goods);
  172. $info = Info::init(-1)->info($info, 'view_reorder');
  173. if ($info && $other && isset($set['price_type']) && $set['price_type']) {
  174. $price_type = explode(',', $set['price_type']);
  175. if (in_array($info['price_type'], $price_type)) {
  176. if ($update) {
  177. return '组合商品无法设置库存';
  178. }
  179. if ($info['goods'] && is_array($info['goods'])) {
  180. $html = '';
  181. foreach ($info['goods'] as $k => $v) {
  182. $html .= $this->table($v['id'], $other, $other_id, $set, $update, $v) . '<br />';
  183. }
  184. $html .= '*组合商品的库存为以上商品中的最低库存为准';
  185. return $html;
  186. } else {
  187. return '';
  188. }
  189. }
  190. }
  191. $input = '';
  192. if ($other) {
  193. $other_col = $other . '_id';
  194. $sku_value = Dever::db($other . '/goods_sku')->getData(array('goods_id' => $info['id'], $other_col => $other_id));
  195. $input = '<input type="hidden" name="goods_id" value="'.$info['id'].'"/><input type="hidden" name="'.$other_col.'" value="'.$other_id.'"/>';
  196. }
  197. $sku = Dever::db('goods/info_sku')->getDataByKeys(array('info_id' => $info['id']));
  198. $sell_attr = $option_attr = array();
  199. $head = $option_head = '<thead><tr>';
  200. $html = '请先选择属性';
  201. if ($parent_goods) {
  202. $head .= '<th>商品名称</th>';
  203. $option_head .= '<th>商品名称</th>';
  204. }
  205. if (isset($info['sell_attr']) && $info['sell_attr']) {
  206. foreach ($info['sell_attr'] as $k => $v) {
  207. $head .= '<th>'.$v['name'].'</th>';
  208. if (isset($v['option_sku'])) {
  209. $sell_attr[$k] = $v['option_sku'];
  210. }
  211. }
  212. }
  213. if (isset($info['option_attr']) && $info['option_attr']) {
  214. $option_head .= '<th>属性名称</th>';
  215. foreach ($info['option_attr'] as $k => $v) {
  216. if (isset($v['option'])) {
  217. $option_attr = array_merge($option_attr, $v['option']);
  218. }
  219. }
  220. }
  221. $col_num = 0;
  222. foreach ($set['col'] as $k => $v) {
  223. if (!$update && strstr($v, '^')) {
  224. continue;
  225. }
  226. $col_num++;
  227. if (strstr($v, '^')) {
  228. $v = str_replace('^', '', $v);
  229. }
  230. if (strstr($v, '|')) {
  231. $v = str_replace('|', '', $v);
  232. }
  233. $head .= '<th>'.$v.'</th>';
  234. $option_head .= '<th>'.$v.'</th>';
  235. }
  236. $head .= '</tr></thead>';
  237. $option_head .= '</tr></thead>';
  238. $body = '<tbody>';
  239. $option_body = '<tbody>';
  240. if (!$sell_attr) {
  241. $prefix = '[单价设置]:';
  242. if (!$update && $col_num <= 1) {
  243. $head = '';
  244. }
  245. if ($update) {
  246. $input .= '<input type="hidden" name="key" value="-1"/>';
  247. }
  248. $body .= '<tr>';
  249. if ($parent_goods) {
  250. $body .= '<td width="10">'.$parent_goods['name'].'</td>';
  251. }
  252. foreach ($set['col'] as $k1 => $v1) {
  253. if (!$update && strstr($v1, '^')) {
  254. continue;
  255. }
  256. $value = '';
  257. if (isset($sku_value[-1]) && isset($sku_value[-1][$k1])) {
  258. $value = $sku_value[-1][$k1];
  259. } elseif (isset($sku[-1]) && isset($sku[-1][$k1])) {
  260. $value = $sku[-1][$k1];
  261. }
  262. if ($update) {
  263. if (strstr($v1, '|')) {
  264. $v1 = str_replace('|', '', $v1);
  265. $body .= '<td width="30">'.$value.'</td>';
  266. } else {
  267. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'" value="'.$value.'"/></td>';
  268. }
  269. } elseif ($col_num > 1) {
  270. $body .= '<td width="30">'.$value.'</td>';
  271. } else {
  272. $body .= $value;
  273. }
  274. }
  275. $body .= '</tr>';
  276. } else {
  277. $prefix = '[多价设置]:';
  278. $sell_attr = Dever::cartesian($sell_attr);
  279. $show = false;
  280. foreach ($sell_attr as $k => $v) {
  281. $body .= '<tr data-row="' .($k+1). '">';
  282. if ($parent_goods) {
  283. $body .= '<td>'.$parent_goods['name'].'</td>';
  284. }
  285. $key = $id = array();
  286. foreach ($v['name'] as $k1 => $v1) {
  287. $rows = 1;
  288. $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
  289. $id[] = array
  290. (
  291. 'id' => $v['id'][$k1],
  292. 'attr_id' => $v['info_id'][$k1],
  293. );
  294. $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
  295. }
  296. $key = implode('_', $key);
  297. foreach ($set['col'] as $k1 => $v1) {
  298. if (!$update && strstr($v1, '^')) {
  299. continue;
  300. }
  301. $value = '';
  302. if (isset($sku[$key])) {
  303. if (isset($sku_value[$sku[$key]['id']][$k1])) {
  304. $value = $sku_value[$sku[$key]['id']][$k1];
  305. } elseif (isset($sku[$key][$k1])) {
  306. $value = $sku[$key][$k1];
  307. }
  308. }
  309. if ($update) {
  310. if (strstr($v1, '|')) {
  311. $v1 = str_replace('|', '', $v1);
  312. $body .= '<td width="30">'.$value.'</td>';
  313. } else {
  314. $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  315. }
  316. } else {
  317. $body .= '<td width="30">'.$value.'</td>';
  318. }
  319. }
  320. if ($update) {
  321. $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
  322. }
  323. $body .= '</tr>';
  324. }
  325. }
  326. $body .= $input;
  327. $html = $head . $body;
  328. if ($update) {
  329. $html = $prefix . '<table class="layui-table">' . $html . '</table>';
  330. } else {
  331. $html = '<table class="layui-table">' . $html . '</table>';
  332. }
  333. if ($option_attr) {
  334. print_r($option_attr);die;
  335. foreach ($option_attr as $k => $v) {
  336. $option_body .= '<tr data-row="' .($k+1). '">';
  337. if ($parent_goods) {
  338. $option_body .= '<td>'.$parent_goods['name'].'</td>';
  339. }
  340. $rows = 1;
  341. $option_body .= '<td width="50" rowspan="'.$rows.'">'.$v['parent_name'] . '-' . $v['name'].'</td>';
  342. $id = array
  343. (
  344. 'id' => $v['id'],
  345. 'attr_id' => $v['info_id'],
  346. );
  347. $key = $v['info_id'] . '-' . $v['id'];
  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. $option_body .= '<td width="30">'.$value.'</td>';
  364. } else {
  365. $option_body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']" value="'.$value.'"/></td>';
  366. }
  367. } else {
  368. $option_body .= '<td width="30">'.$value.'</td>';
  369. }
  370. }
  371. if ($update) {
  372. $option_body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
  373. }
  374. $option_body .= '</tr>';
  375. }
  376. $html = $html . '<br />[加价设置]:<table class="layui-table">' . $option_head . $option_body . '</table>';;
  377. }
  378. return $html;
  379. }
  380. }