Commodity.php 25 KB

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