Commodity.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Commodity extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_commodity','commodity');
  7. $this->load->_model('Model_commodityread','commodityread');
  8. $this->load->_model('Model_shop','shop');
  9. $this->load->_model('Model_apismt','apismt');
  10. $this->load->_model('Model_typeclass','typeclass');
  11. $this->load->_model('Model_commodityread_del','commodityreaddel');
  12. $this->load->_model('Model_classid','classid');
  13. $this->load->_model("Model_logic_order","logic_order");
  14. $this->load->_model("Model_logic_ding","logic_ding");
  15. }
  16. //定义方法的调用规则 获取URI第二段值
  17. public function _remap($arg,$arg_array)
  18. {
  19. if($arg == 'aeorder')//同步
  20. {
  21. $this->_aeorder();
  22. }
  23. else if($arg == 'edit')//修改
  24. {
  25. $this->_edit($arg_array);
  26. }
  27. else if($arg == 'batchedit')//批量修改
  28. {
  29. $this->_batchedit();
  30. }
  31. else if($arg == 'skuedit')//修改
  32. {
  33. $this->_skuedit($arg_array);
  34. }
  35. else if($arg == 'excel_export'){
  36. $this->_excel_export();
  37. }
  38. else
  39. {
  40. $this->_index();
  41. }
  42. }
  43. //管理
  44. public function _index()
  45. {
  46. $dt = 0;
  47. if(isset($_SESSION['api']))
  48. {
  49. $user = $this->user->get_api($_SESSION['api']);
  50. $usp = $user;
  51. $fgshop = "";$sid = "";
  52. $usersp = explode('|',trim($user['shop'],'|'));
  53. foreach ($usersp as $value)
  54. {
  55. $fgshop .= " shop = ".$value." or";
  56. $sid .= " id = ".$value." or";
  57. }
  58. }
  59. $post = $this->input->post(NULL, TRUE);
  60. if(isset($post['page']))
  61. {
  62. $page = $this->input->post('page',true);
  63. $perpage = $this->input->post('perpage',true);
  64. $shop = $this->input->post('shop',true);
  65. $productid = $this->input->post('productid',true);
  66. $title = $this->input->post('title',true);
  67. $type = $this->input->post('type',true);
  68. $code = $this->input->post('code',true);
  69. $skuid = $this->input->post('skuid',true);
  70. $category = $this->input->post('category',true);
  71. $categorytypeclass = $this->typeclass->get_titleclassid($category,16);
  72. $where = "1=1 and (".rtrim($fgshop,'or').")";
  73. if($shop)
  74. {
  75. $where .= " and shop = '$shop'";
  76. }
  77. if($productid)
  78. {
  79. $where .= " and productid = '$productid'";
  80. }
  81. if($title)
  82. {
  83. $where .= " and title like '%$title%'";
  84. }
  85. if($type)
  86. {
  87. $where .= " and type = '$type'";
  88. }
  89. if($category)
  90. {
  91. $where .= " and category like '%".$categorytypeclass['id']."%'";
  92. }
  93. if($code)
  94. {
  95. $u = $this->commodityread->get_sku($code);
  96. $u = $u['productid'];
  97. $where .= " and productid = '$u'";
  98. }
  99. if($skuid)
  100. {
  101. $u = $this->commodityread->get_skuid($skuid);
  102. $u = $u['productid'];
  103. $where .= " and productid = '$u'";
  104. }
  105. //数据排序
  106. $order_str = "time desc";
  107. if(empty($page))
  108. {
  109. $start = 0;
  110. $perpage = 1;
  111. }
  112. else
  113. {
  114. $start = ($page - 1)*$perpage;
  115. }
  116. //取得信息列表
  117. $info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str,$start,$perpage);
  118. //格式化数据
  119. foreach ($info_list as $key=>$v)
  120. {
  121. $shop = $this->shop->read($v['shop']);
  122. $info_list[$key]['shop'] = $shop['shopname'];
  123. $info_list[$key]['img'] = "<img src='".$v['img']."' style='height:55px' />";
  124. $category = '';
  125. if($v['category'])
  126. {
  127. $v['category'] = explode(',',trim($v['category'],','));
  128. foreach ($v['category'] as $vv)
  129. {
  130. $typeclass = $this->typeclass->read($vv);
  131. $category .= "<p>".$typeclass['title']."</p>";
  132. }
  133. $info_list[$key]['category'] = $category;
  134. }
  135. else
  136. {
  137. $info_list[$key]['category'] = '';
  138. }
  139. $info_list[$key]['time'] = "<p><a href='http://posting.aliexpress.com/wsproduct/edit_wholesale_product.htm?product_id=".$v['productid']."' target='_blank'>编辑</a></p><p><a href='http://www.aliexpress.com/item/".$v['productid'].".html' target='_blank'>浏览</a></p>";
  140. }
  141. $total = $this->commodity->find_count($where);
  142. $pagenum = ceil($total/$perpage);
  143. $over = $total-($start+$perpage);
  144. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  145. echo json_encode($rows);exit;
  146. }
  147. $wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
  148. $this->data['wlshop'] = $wlshop;
  149. $this->_Template('commodity',$this->data);
  150. }
  151. //修改
  152. public function _edit($arg_array)
  153. {
  154. $post = $this->input->post(NULL, TRUE);
  155. if(isset($post['id']))
  156. {
  157. $id = $this->input->post('id',true);
  158. $category = $this->input->post('category',true);
  159. $data = $this->input->post('data');
  160. if($data)
  161. {
  162. $data = explode('|',rtrim($data,'|'));
  163. foreach ($data as $v)
  164. {
  165. $d = explode('@',$v);
  166. $this->commodityread->save(array('sku'=>$d[1]),$d[0]);
  167. }
  168. }
  169. if($this->commodity->save(array('category'=>','.$category),$id))
  170. {
  171. echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
  172. }
  173. else
  174. {
  175. echo json_encode(array('msg'=>'类目未变动,更新失败','success'=>false));exit;
  176. }
  177. }
  178. $arg_array = $arg_array[0];
  179. $commodity = $this->commodity->read($arg_array);
  180. $this->data['commodity'] = $commodity;
  181. $sku = $this->commodityread->find_all('productid = '.$commodity['productid']);
  182. $this->data['sku'] = $sku;
  183. $this->_Template('commodity_edit',$this->data);
  184. }
  185. public function _batchedit_b()//老系统形式,保留,不用
  186. {
  187. $dictionaries = $this->typeclass->find_all('ae!= "" and (classid=13 or classid=22 or classid=8 or classid=15 or classid=27 or classid=25 or classid=26 or classid=18 or classid=14 or classid=9 or classid=12 or classid=10 or classid=6)'); //产品人发类型颜色
  188. $dtc = array();$ctd = array();
  189. foreach ($dictionaries as $v)
  190. {
  191. if(stripos($v['spare'],'|') !== false)//如果有多个值
  192. {
  193. $v['spare'] = explode('|',$v['spare']);
  194. foreach ($v['spare'] as $vs)
  195. {
  196. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
  197. }
  198. }
  199. else
  200. {
  201. $dtc[strtolower($v['spare'])] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
  202. }
  203. $ctd[$v['id']] = $v['title'];
  204. }
  205. $ctd[126] = 'Hair Weaving';
  206. $ctd[127] = 'Closure';
  207. $ctd[128] = 'Wigs';
  208. $ctd[130] = 'Clip-in Full Head';
  209. $post = $this->input->post(NULL, TRUE);
  210. if(isset($post['id']))
  211. {
  212. $id = $this->input->post('id',true);
  213. $ndata = $this->input->post('data',true);
  214. $id = explode(',',rtrim($id,','));
  215. $data = explode('-',rtrim($ndata,'-'));
  216. $rows = array();
  217. foreach ($id as $val)
  218. {
  219. //a:颜色 b:密度 c:头路设计
  220. $u = $this->commodityread->read($val);
  221. $u['codeid'] = strtolower($u['codeid']);
  222. $u['codeid'] = str_replace(array('stretched length','inches','& '),array('length','',''),$u['codeid']);
  223. $sku = explode(';',rtrim($u['codeid'],';'));
  224. $codeid = array();$whlabel = ''; $fpdata = '';
  225. foreach ($sku as $v)
  226. {
  227. $va = explode(':',$v);
  228. $codeid[$va[0]] = $va[1];
  229. }
  230. $num = '';$ti = '';$tit = '';$error = '';$i = 1;
  231. foreach ($data as $k=>$v)
  232. {
  233. if($v == 'a')
  234. {
  235. if(isset($codeid['color']) && isset($dtc[$codeid['color']]))
  236. {
  237. $num .= $dtc[$codeid['color']]['id'].'-';
  238. $ti .= $dtc[$codeid['color']]['title'].' ';
  239. }
  240. else
  241. {
  242. $num .= '57-';
  243. $ti .= 'Natural Black ';
  244. }
  245. }
  246. else if($v == 'b')
  247. {
  248. if(isset($codeid['density']) && isset($dtc[$codeid['density']]))
  249. {
  250. $num .= $dtc[$codeid['density']]['id'].'-';
  251. $ti .= $dtc[$codeid['density']]['title'].' ';
  252. }
  253. else
  254. {
  255. $num .= '71-';
  256. $ti .= '150% ';
  257. }
  258. }
  259. else if($v == 'c')
  260. {
  261. if(isset($codeid['part design']) && isset($dtc[$codeid['part design']]))
  262. {
  263. $num .= $dtc[$codeid['part design']]['id'].'-';
  264. $ti .= $dtc[$codeid['part design']]['title'].' ';
  265. }
  266. else
  267. {
  268. $num .= '75-';
  269. $ti .= 'Free Part ';
  270. }
  271. }
  272. else
  273. {
  274. if($i == 2)
  275. {
  276. $num .= $v.'-id-';
  277. }
  278. else
  279. {
  280. $num .= $v.'-';
  281. }
  282. $ti .= $ctd[$v].' ';
  283. }
  284. $i++;
  285. }
  286. if(stripos($codeid['length'],'closure') !== false && stripos($ndata,'127') !== false)
  287. {
  288. $v = preg_replace(array('/([\s\S]*)closure/','/([\s\S]*)closure\s/'),array('',''),$codeid['length']);
  289. $v = preg_replace('/([\s]*)/','',$v);
  290. $fpdata = $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
  291. $tit = '<p>'.$ti.$v.'inch</p>';
  292. $whlabel = '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
  293. }
  294. else
  295. {
  296. $codeid['length'] = explode(' ',$codeid['length']);
  297. foreach ($codeid['length'] as $v)
  298. {
  299. if(is_numeric($v))
  300. {
  301. $fpdata .= $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
  302. $tit .= '<p>'.$ti.$v.'inch</p>';
  303. $whlabel .= '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
  304. }
  305. else
  306. {
  307. break;
  308. }
  309. }
  310. }
  311. $fa = '';$wl = '';
  312. $u['fpdata'] = explode(';',rtrim($u['fpdata'],';'));
  313. $u['whlabel'] = explode('|',trim($u['whlabel'],'|'));
  314. for($i=0;$i<count($u['fpdata']);$i++)
  315. {
  316. if(stripos($u['fpdata'][$i],$data[0]) === false)
  317. {
  318. if($u['fpdata'][$i] != '')
  319. {
  320. $fa .= $u['fpdata'][$i].';';
  321. $te = explode('|',$u['fpdata'][$i]);
  322. $tit .= '<p>'.$te[1].'</p>';
  323. }
  324. }
  325. if(stripos($u['whlabel'][$i],$data[0]) === false)
  326. {
  327. if($u['whlabel'][$i] != '')
  328. {
  329. $wl .= '|'.$u['whlabel'][$i];
  330. }
  331. }
  332. }
  333. $this->commodityread->save(array('fpdata'=>$fa.$fpdata,'whlabel'=>$wl.$whlabel.'|'),$val);
  334. $rows[] = array('id'=>$val,'title'=>$tit);
  335. }
  336. echo json_encode(array('rows'=>$rows,'success'=>true));exit;
  337. }
  338. }
  339. public function _skuedit($arg_array)
  340. {
  341. $post = $this->input->post(NULL, TRUE);
  342. if(isset($post['id']))
  343. {
  344. $id = $this->input->post('id',true);
  345. $post['fpdata'] = $this->input->post('fpdata',true);
  346. $post['whlabel'] = $this->input->post('whlabel',true);
  347. if($this->commodityread->save($post,$id))
  348. {
  349. echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
  350. }
  351. else
  352. {
  353. echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
  354. }
  355. }
  356. $arg_array = $arg_array[0];$fpdata = array();
  357. $commodityread = $this->commodityread->read($arg_array);
  358. if(stripos($commodityread['fpdata'],';') !== false)
  359. {
  360. $fpdata = explode(';',rtrim($commodityread['fpdata'],';'));
  361. foreach ($fpdata as $k=>$v)
  362. {
  363. $fpdata[$k] = explode('|',$v);
  364. }
  365. }
  366. $this->data['fpdata'] = $fpdata;
  367. $this->data['commodityread'] = $commodityread;
  368. $this->_Template('commodityread_edit',$this->data);
  369. }
  370. //获取
  371. public function _aeorder()
  372. {
  373. $sid = '';
  374. if(isset($_SESSION['api']))
  375. {
  376. $user = $this->user->get_api($_SESSION['api']);
  377. $user = explode('|',trim($user['shop'],'|'));
  378. foreach ($user as $value)
  379. {
  380. $sid .= " id = '$value' or";
  381. }
  382. $sid = " and (".rtrim($sid,'or').")";
  383. }
  384. $sid = " and id = 11";
  385. $post = $this->input->post(NULL, TRUE);
  386. if($sid)
  387. {
  388. $shop = $this->shop->find_all("code != '' and type = '270'".$sid);//获取店铺信息
  389. foreach ($shop as $value)
  390. {
  391. $res = array();
  392. for($x=1;$x<5;$x++)
  393. {
  394. $resi = $this->apismt->get_commoditylist($x,100,$value['code']);//获取商品列表
  395. if($resi)
  396. {
  397. $res += $resi;
  398. }
  399. }
  400. if($res)
  401. {
  402. $yzpid = $this->commodity->find_all('shop = '.$value['id']);
  403. foreach ($yzpid as $v)
  404. {
  405. if($v['productid'] == '' || $v['productid'] == '0')
  406. {
  407. $this->commodity->remove($v['id']);
  408. continue;
  409. }
  410. if(!isset($res[$v['productid']]))
  411. {
  412. $this->commodity->save(array('type'=>'offline'),$v['id']);
  413. }
  414. }
  415. foreach ($res as $v)
  416. {
  417. $productid = $this->commodity->get_productid($v);
  418. //if(!$productid)
  419. //{
  420. @$read = $this->apismt->get_commodityread($v,$value['code']);
  421. //$this->logic_ding->sendToDing("速卖通获取商品详情"."【".json_encode($read)."】");
  422. if(is_array($read))
  423. {
  424. @$cid = $this->apismt->get_commoditysku($read['cid'],$value['code']);
  425. }
  426. else
  427. {
  428. continue;
  429. }
  430. //处理结束
  431. if(isset($cid[0]))
  432. {
  433. // 处理 $cid
  434. $cc = array();
  435. foreach ($cid as $c)
  436. {
  437. $names = json_decode($c['names'],true);
  438. $cc[$c['id']] = $names['en'];
  439. if(!isset($c['values']))
  440. {
  441. continue;
  442. }
  443. if(isset($c['values']['aeop_attr_value_dto'][0]))
  444. {
  445. foreach ($c['values']['aeop_attr_value_dto'] as $cv)
  446. {
  447. if(isset($read['gg'][$cv['id']]))//如果有自定义
  448. {
  449. $cc[$cv['id']] = $read['gg'][$cv['id']];
  450. }
  451. else
  452. {
  453. $cnames = json_decode($cv['names'],true);
  454. $cc[$cv['id']] = $cnames['en'];
  455. }
  456. }
  457. }
  458. else
  459. {
  460. $cnames = json_decode($c['values']['aeop_attr_value_dto']['names'],true);
  461. $cc[$c['values']['aeop_attr_value_dto']['id']] = $cnames['en'];
  462. }
  463. }
  464. $read['title'] = ($read['title'])?$read['title']:'';
  465. if(!$productid)
  466. {
  467. //处理结束
  468. $this->commodity->insert(array('shop'=>$value['id'],'productid'=>$v,'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type'],'time'=>time()));
  469. }
  470. else
  471. {
  472. $this->commodity->save(array('shop'=>$value['id'],'productid'=>$read['productid'],'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type']),$productid['id']);
  473. }
  474. $this->_aeordersku($read['sku'],$value['id'],$v,$cc);
  475. }
  476. //}
  477. }
  478. }
  479. else
  480. {
  481. continue;
  482. }
  483. }
  484. echo json_encode(array('msg'=>'同步完成!','success'=>true));exit;
  485. }
  486. }
  487. public function _aeordersku($sku,$shop,$productid,$cc)
  488. {
  489. $yzsku = $this->commodityread->find_all("productid = '$productid'");
  490. foreach ($yzsku as $v)
  491. {
  492. $s = 0;
  493. if(!isset($sku[$v['skuid']]))
  494. {
  495. $skuid = explode(';',$v['skuid']);
  496. if(count($skuid) < 2)
  497. {
  498. $this->commodityreaddel->insert($v);
  499. $this->commodityread->remove($v['id']);//asd
  500. continue;
  501. }
  502. else
  503. {
  504. $skuidarray = $this->_qpl($skuid);
  505. foreach ($skuidarray as $skuv)
  506. {
  507. if(isset($sku[$skuv]))
  508. {
  509. $this->commodityread->save(array('skuid'=>$skuv),$v['id']);
  510. $s++;
  511. break 1;
  512. }
  513. }
  514. }
  515. if($s < 1)
  516. {
  517. $this->commodityreaddel->insert($v);
  518. $this->commodityread->remove($v['id']);//asd
  519. }
  520. }
  521. }
  522. foreach ($sku as $v)
  523. {
  524. $read = $this->commodityread->get_productid($productid,$v['skuid']);
  525. if(isset($read['productid']))
  526. {
  527. $cd = explode(';',$v['skuid']);$cod = '';
  528. foreach ($cd as $vv)
  529. {
  530. $cv = explode(':',$vv);
  531. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  532. }
  533. $this->commodityread->save(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()),$read['id']);
  534. }
  535. else
  536. {
  537. $cd = explode(';',$v['skuid']);$cod = '';
  538. foreach ($cd as $vv)
  539. {
  540. $cv = explode(':',$vv);
  541. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  542. }
  543. $this->commodityread->insert(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()));
  544. }
  545. }
  546. }
  547. public function _qpl($source)//全排列
  548. {
  549. $qpl = array();
  550. sort($source); //保证初始数组是有序的
  551. $last = count($source) - 1; //$source尾部元素下标
  552. $x = $last;
  553. $count = 1; //组合个数统计
  554. $qpl[implode(';', $source)] = implode(';', $source); //输出第一种组合
  555. while (true)
  556. {
  557. $y = $x--; //相邻的两个元素
  558. if ($source[$x] < $source[$y])//如果前一个元素的值小于后一个元素的值
  559. {
  560. $z = $last;
  561. while ($source[$x] > $source[$z]) //从尾部开始,找到第一个大于 $x 元素的值
  562. {
  563. $z--;
  564. }
  565. /* 交换 $x 和 $z 元素的值 */
  566. list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
  567. /* 将 $y 之后的元素全部逆向排列 */
  568. for ($i = $last; $i > $y; $i--, $y++)
  569. {
  570. list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
  571. }
  572. $qpl[implode(';', $source)] = implode(';', $source); //输出组合
  573. $x = $last;
  574. $count++;
  575. }
  576. if ($x == 0)//全部组合完毕
  577. {
  578. break;
  579. }
  580. }
  581. return $qpl;
  582. }
  583. public function _skudelhf($a)//commodityread 同步删除的恢复
  584. {
  585. $this->db->trans_begin();
  586. $qbpid = array();
  587. $a = $this->commodityread->find_all("time > '1653613200'");
  588. foreach ($a as $v)
  589. {
  590. $qbpid[$v['productid']] = $v['productid'];
  591. }
  592. foreach ($qbpid as $valer)
  593. {
  594. $del = $this->commodityreaddel->find_all("productid = '".$valer."' and sku != ''",'*','id asc');
  595. if($del)
  596. {
  597. foreach ($del as $val)
  598. {
  599. $skuid = explode(';',$val['skuid']);
  600. if(count($skuid) < 2)
  601. {
  602. continue;
  603. }
  604. $skuidarray = $this->_qpl($skuid);
  605. $red = $this->commodityread->find_all("productid = '".$val['productid']."' and sku = ''");
  606. foreach ($red as $v)
  607. {
  608. if(isset($skuidarray[$v['skuid']]))
  609. {
  610. $this->commodityread->save(array('sku'=>$val['sku']),$v['id']);
  611. }
  612. }
  613. }
  614. }
  615. }
  616. if ($this->db->trans_status() === TRUE)
  617. {
  618. $this->db->trans_commit();
  619. echo 1;
  620. }
  621. else
  622. {
  623. $this->db->trans_rollback();
  624. echo 2;
  625. }
  626. }
  627. public function _excel_export(){
  628. if(isset($_SESSION['api']))
  629. {
  630. $user = $this->user->get_api($_SESSION['api']);
  631. $usp = $user;
  632. $fgshop = "";$sid = "";
  633. $usersp = explode('|',trim($user['shop'],'|'));
  634. foreach ($usersp as $value)
  635. {
  636. $fgshop .= " shop = ".$value." or";
  637. $sid .= " id = ".$value." or";
  638. }
  639. }
  640. $params = $this->input->get(NULL, TRUE);
  641. if(isset($params['excel']) && $params['excel'] == 1){
  642. $shop = $this->input->get('shop',true);
  643. $productid = $this->input->get('productid',true);
  644. $title = $this->input->get('title',true);
  645. $type = $this->input->get('type',true);
  646. $code = $this->input->get('code',true);
  647. $skuid = $this->input->get('skuid',true);
  648. $category = $this->input->get('category',true);
  649. $categorytypeclass = $this->typeclass->get_titleclassid($category,16);
  650. $where = "1=1 and (".rtrim($fgshop,'or').")";
  651. if($shop)
  652. {
  653. $where .= " and shop = '$shop'";
  654. }
  655. if($productid)
  656. {
  657. $where .= " and productid = '$productid'";
  658. }
  659. if($title)
  660. {
  661. $where .= " and title like '%$title%'";
  662. }
  663. if($type)
  664. {
  665. $where .= " and type = '$type'";
  666. }
  667. if($category)
  668. {
  669. $where .= " and category like '%".$categorytypeclass['id']."%'";
  670. }
  671. if($code)
  672. {
  673. $u = $this->commodityread->get_sku($code);
  674. $u = $u['productid'];
  675. $where .= " and productid = '$u'";
  676. }
  677. if($skuid)
  678. {
  679. $u = $this->commodityread->get_skuid($skuid);
  680. $u = $u['productid'];
  681. $where .= " and productid = '$u'";
  682. }
  683. //数据排序
  684. $order_str = "time desc";
  685. //取得信息列表
  686. $info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str);
  687. //格式化数据
  688. $final_list = [];
  689. $shop_where = trim($sid,'or');
  690. $shop_list = $this->shop->find_all($shop_where,'id,shopname');
  691. $shop_list = array_column($shop_list,'shopname','id');
  692. $typeclass = [];
  693. $classid = $this->classid->sku();
  694. $tcall = $this->typeclass->find_all();
  695. foreach ($tcall as $v)
  696. {
  697. //$tcjm[$v['id']] = array($v['jm'],$v['classid']);
  698. $typeclass[$v['id']] = array('zh'=>$v['zh'],'classid'=>$v['classid'],'bm'=>$v['bm'],'title'=>$v['title'],'jm'=>$v['jm']);
  699. }
  700. $dtctitle = [];
  701. foreach($tcall as $v){
  702. $dtctitle[$v['id']] = $v['title'];
  703. }
  704. $dtc = [];
  705. foreach ($tcall as $v) {
  706. if (stripos($v['spare'], '|') !== false) //如果有多个值
  707. {
  708. $v['spare'] = explode('|', $v['spare']);
  709. foreach ($v['spare'] as $k => $vs) {
  710. if (stripos($v['zh'], '|') !== false) {
  711. $vzh = explode('|', $v['zh']);
  712. $dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $vzh[$k], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
  713. } else {
  714. $dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
  715. }
  716. }
  717. } else {
  718. $dtc[strtolower($v['spare'])] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $v['spare']);
  719. }
  720. }
  721. foreach ($info_list as $key=>$v)
  722. {
  723. $shopname = isset($shop_list[$v['shop']])?$shop_list[$v['shop']]:'';
  724. $skulist = $this->commodityread->find_all("productid = ".$v['productid']);
  725. $tmp_list = [];
  726. foreach($skulist as $skv){
  727. $tmp_sku = str_replace('--','-',$skv['sku']);
  728. $u9_info = $this->logic_order->skuTransferFeatures($tmp_sku,$typeclass,$dtc,$dtctitle,$classid);
  729. $tmp_list[] = [
  730. 'codeid' => $skv['codeid'],
  731. 'erp_sku' => $skv['sku'],
  732. 'jm'=>$u9_info['jm'],
  733. 'zh'=>$u9_info['zh'],
  734. ];
  735. }
  736. $tmp_codeid = "";
  737. $tmp_erp_sku = "";
  738. $tmp_jm = "";
  739. $tmp_zh = "";
  740. foreach($tmp_list as $t){
  741. $tmp_codeid .= $t['codeid']."<br>";
  742. $tmp_erp_sku .= $t['erp_sku']."<br>";
  743. $tmp_jm .= $t['jm']."<br>";
  744. $tmp_zh .= $t['zh']."<br>";
  745. }
  746. $final_list[] = [
  747. 'shopname' => $shopname,
  748. 'productid' => "'".$v['productid'],
  749. 'codeid' => $tmp_codeid,
  750. 'erp_sku' => $tmp_erp_sku,
  751. 'jm' => $tmp_jm,
  752. 'zh' => $tmp_zh,
  753. 'title' => $v['title'],
  754. ];
  755. }
  756. //生成excel
  757. $filename = '速卖通-'.date('Ymd',time()).".xlsx";
  758. $str = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head>
  759. <!--[if gte mso 9]><xml>
  760. <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>EXCEL</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml>
  761. <![endif]-->
  762. </head><body>";
  763. $str .= "<table border=1 style='font-family: Microsoft Yahei;font-size: 13px;'>";
  764. $str .= "<tr><td>店铺名称</td><td>速卖通产品编码</td><td>速卖通标题</td><td>速卖通SKU名称</td><td>erpSKU</td><td>用友简码</td><td>仓库品名</td></tr>";
  765. foreach ($final_list as $key=>$value)
  766. {
  767. $str .= "<tr>";
  768. $str .= "<td>". $value['shopname']. "</td>";
  769. $str .= "<td>". $value['productid']. "</td>";
  770. $str .= "<td>". $value['title']. "</td>";
  771. $str .= "<td>". $value['codeid']. "</td>";
  772. $str .= "<td>". $value['erp_sku']. "</td>";
  773. $str .= "<td>". $value['jm']. "</td>";
  774. $str .= "<td>". $value['zh']. "</td>";
  775. $str .= "</tr>";
  776. }
  777. $str .= "</table></body></html>";
  778. header( "Content-Type: application/vnd.ms-excel; name='excel'" );
  779. header( "Content-type: application/octet-stream" );
  780. header( "Content-Disposition: attachment; filename=".$filename );
  781. header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  782. header( "Pragma: no-cache" );
  783. header( "Expires: 0" );
  784. exit($str);
  785. }
  786. }
  787. }