Commodity.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. $post = $this->input->post(NULL, TRUE);
  385. if($sid)
  386. {
  387. $shop = $this->shop->find_all("code != '' and type = '270'".$sid);//获取店铺信息
  388. foreach ($shop as $value)
  389. {
  390. $res = array();
  391. for($x=1;$x<5;$x++)
  392. {
  393. $resi = $this->apismt->get_commoditylist($x,100,$value['code']);//获取商品列表
  394. if($resi)
  395. {
  396. $res += $resi;
  397. }
  398. }
  399. if($res)
  400. {
  401. $yzpid = $this->commodity->find_all('shop = '.$value['id']);
  402. foreach ($yzpid as $v)
  403. {
  404. if($v['productid'] == '' || $v['productid'] == '0')
  405. {
  406. $this->commodity->remove($v['id']);
  407. continue;
  408. }
  409. if(!isset($res[$v['productid']]))
  410. {
  411. $this->commodity->save(array('type'=>'offline'),$v['id']);
  412. }
  413. }
  414. foreach ($res as $v)
  415. {
  416. $productid = $this->commodity->get_productid($v);
  417. //if(!$productid)
  418. //{
  419. @$read = $this->apismt->get_commodityread($v,$value['code']);
  420. //$this->logic_ding->sendToDing("速卖通获取商品详情"."【".json_encode($read)."】");
  421. if(is_array($read))
  422. {
  423. @$cid = $this->apismt->get_commoditysku($read['cid'],$value['code']);
  424. }
  425. else
  426. {
  427. continue;
  428. }
  429. //处理结束
  430. if(isset($cid[0]))
  431. {
  432. // 处理 $cid
  433. $cc = array();
  434. foreach ($cid as $c)
  435. {
  436. $names = json_decode($c['names'],true);
  437. $cc[$c['id']] = $names['en'];
  438. if(!isset($c['values']))
  439. {
  440. continue;
  441. }
  442. if(isset($c['values']['aeop_attr_value_dto'][0]))
  443. {
  444. foreach ($c['values']['aeop_attr_value_dto'] as $cv)
  445. {
  446. if(isset($read['gg'][$cv['id']]))//如果有自定义
  447. {
  448. $cc[$cv['id']] = $read['gg'][$cv['id']];
  449. }
  450. else
  451. {
  452. $cnames = json_decode($cv['names'],true);
  453. $cc[$cv['id']] = $cnames['en'];
  454. }
  455. }
  456. }
  457. else
  458. {
  459. $cnames = json_decode($c['values']['aeop_attr_value_dto']['names'],true);
  460. $cc[$c['values']['aeop_attr_value_dto']['id']] = $cnames['en'];
  461. }
  462. }
  463. $read['title'] = ($read['title'])?$read['title']:'';
  464. if(!$productid)
  465. {
  466. //处理结束
  467. $this->commodity->insert(array('shop'=>$value['id'],'productid'=>$v,'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type'],'time'=>time()));
  468. }
  469. else
  470. {
  471. $this->commodity->save(array('shop'=>$value['id'],'productid'=>$read['productid'],'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type']),$productid['id']);
  472. }
  473. $this->_aeordersku($read['sku'],$value['id'],$v,$cc);
  474. }
  475. //}
  476. }
  477. }
  478. else
  479. {
  480. continue;
  481. }
  482. }
  483. echo json_encode(array('msg'=>'同步完成!','success'=>true));exit;
  484. }
  485. }
  486. public function _aeordersku($sku,$shop,$productid,$cc)
  487. {
  488. $yzsku = $this->commodityread->find_all("productid = '$productid'");
  489. foreach ($yzsku as $v)
  490. {
  491. $s = 0;
  492. if(!isset($sku[$v['skuid']]))
  493. {
  494. $skuid = explode(';',$v['skuid']);
  495. if(count($skuid) < 2)
  496. {
  497. $this->commodityreaddel->insert($v);
  498. $this->commodityread->remove($v['id']);//asd
  499. continue;
  500. }
  501. else
  502. {
  503. $skuidarray = $this->_qpl($skuid);
  504. foreach ($skuidarray as $skuv)
  505. {
  506. if(isset($sku[$skuv]))
  507. {
  508. $this->commodityread->save(array('skuid'=>$skuv),$v['id']);
  509. $s++;
  510. break 1;
  511. }
  512. }
  513. }
  514. if($s < 1)
  515. {
  516. $this->commodityreaddel->insert($v);
  517. $this->commodityread->remove($v['id']);//asd
  518. }
  519. }
  520. }
  521. foreach ($sku as $v)
  522. {
  523. $read = $this->commodityread->get_productid($productid,$v['skuid']);
  524. if(isset($read['productid']))
  525. {
  526. $cd = explode(';',$v['skuid']);$cod = '';
  527. foreach ($cd as $vv)
  528. {
  529. $cv = explode(':',$vv);
  530. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  531. }
  532. $this->commodityread->save(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()),$read['id']);
  533. }
  534. else
  535. {
  536. $cd = explode(';',$v['skuid']);$cod = '';
  537. foreach ($cd as $vv)
  538. {
  539. $cv = explode(':',$vv);
  540. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  541. }
  542. $this->commodityread->insert(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()));
  543. }
  544. }
  545. }
  546. public function _qpl($source)//全排列
  547. {
  548. $qpl = array();
  549. sort($source); //保证初始数组是有序的
  550. $last = count($source) - 1; //$source尾部元素下标
  551. $x = $last;
  552. $count = 1; //组合个数统计
  553. $qpl[implode(';', $source)] = implode(';', $source); //输出第一种组合
  554. while (true)
  555. {
  556. $y = $x--; //相邻的两个元素
  557. if ($source[$x] < $source[$y])//如果前一个元素的值小于后一个元素的值
  558. {
  559. $z = $last;
  560. while ($source[$x] > $source[$z]) //从尾部开始,找到第一个大于 $x 元素的值
  561. {
  562. $z--;
  563. }
  564. /* 交换 $x 和 $z 元素的值 */
  565. list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
  566. /* 将 $y 之后的元素全部逆向排列 */
  567. for ($i = $last; $i > $y; $i--, $y++)
  568. {
  569. list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
  570. }
  571. $qpl[implode(';', $source)] = implode(';', $source); //输出组合
  572. $x = $last;
  573. $count++;
  574. }
  575. if ($x == 0)//全部组合完毕
  576. {
  577. break;
  578. }
  579. }
  580. return $qpl;
  581. }
  582. public function _skudelhf($a)//commodityread 同步删除的恢复
  583. {
  584. $this->db->trans_begin();
  585. $qbpid = array();
  586. $a = $this->commodityread->find_all("time > '1653613200'");
  587. foreach ($a as $v)
  588. {
  589. $qbpid[$v['productid']] = $v['productid'];
  590. }
  591. foreach ($qbpid as $valer)
  592. {
  593. $del = $this->commodityreaddel->find_all("productid = '".$valer."' and sku != ''",'*','id asc');
  594. if($del)
  595. {
  596. foreach ($del as $val)
  597. {
  598. $skuid = explode(';',$val['skuid']);
  599. if(count($skuid) < 2)
  600. {
  601. continue;
  602. }
  603. $skuidarray = $this->_qpl($skuid);
  604. $red = $this->commodityread->find_all("productid = '".$val['productid']."' and sku = ''");
  605. foreach ($red as $v)
  606. {
  607. if(isset($skuidarray[$v['skuid']]))
  608. {
  609. $this->commodityread->save(array('sku'=>$val['sku']),$v['id']);
  610. }
  611. }
  612. }
  613. }
  614. }
  615. if ($this->db->trans_status() === TRUE)
  616. {
  617. $this->db->trans_commit();
  618. echo 1;
  619. }
  620. else
  621. {
  622. $this->db->trans_rollback();
  623. echo 2;
  624. }
  625. }
  626. public function _excel_export(){
  627. if(isset($_SESSION['api']))
  628. {
  629. $user = $this->user->get_api($_SESSION['api']);
  630. $usp = $user;
  631. $fgshop = "";$sid = "";
  632. $usersp = explode('|',trim($user['shop'],'|'));
  633. foreach ($usersp as $value)
  634. {
  635. $fgshop .= " shop = ".$value." or";
  636. $sid .= " id = ".$value." or";
  637. }
  638. }
  639. $params = $this->input->get(NULL, TRUE);
  640. if(isset($params['excel']) && $params['excel'] == 1){
  641. $shop = $this->input->get('shop',true);
  642. $productid = $this->input->get('productid',true);
  643. $title = $this->input->get('title',true);
  644. $type = $this->input->get('type',true);
  645. $code = $this->input->get('code',true);
  646. $skuid = $this->input->get('skuid',true);
  647. $category = $this->input->get('category',true);
  648. $categorytypeclass = $this->typeclass->get_titleclassid($category,16);
  649. $where = "1=1 and (".rtrim($fgshop,'or').")";
  650. if($shop)
  651. {
  652. $where .= " and shop = '$shop'";
  653. }
  654. if($productid)
  655. {
  656. $where .= " and productid = '$productid'";
  657. }
  658. if($title)
  659. {
  660. $where .= " and title like '%$title%'";
  661. }
  662. if($type)
  663. {
  664. $where .= " and type = '$type'";
  665. }
  666. if($category)
  667. {
  668. $where .= " and category like '%".$categorytypeclass['id']."%'";
  669. }
  670. if($code)
  671. {
  672. $u = $this->commodityread->get_sku($code);
  673. $u = $u['productid'];
  674. $where .= " and productid = '$u'";
  675. }
  676. if($skuid)
  677. {
  678. $u = $this->commodityread->get_skuid($skuid);
  679. $u = $u['productid'];
  680. $where .= " and productid = '$u'";
  681. }
  682. //数据排序
  683. $order_str = "time desc";
  684. //取得信息列表
  685. $info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str);
  686. //格式化数据
  687. $final_list = [];
  688. $shop_where = trim($sid,'or');
  689. $shop_list = $this->shop->find_all($shop_where,'id,shopname');
  690. $shop_list = array_column($shop_list,'shopname','id');
  691. $typeclass = [];
  692. $classid = $this->classid->sku();
  693. $tcall = $this->typeclass->find_all();
  694. foreach ($tcall as $v)
  695. {
  696. //$tcjm[$v['id']] = array($v['jm'],$v['classid']);
  697. $typeclass[$v['id']] = array('zh'=>$v['zh'],'classid'=>$v['classid'],'bm'=>$v['bm'],'title'=>$v['title'],'jm'=>$v['jm']);
  698. }
  699. $dtctitle = [];
  700. foreach($tcall as $v){
  701. $dtctitle[$v['id']] = $v['title'];
  702. }
  703. $dtc = [];
  704. foreach ($tcall as $v) {
  705. if (stripos($v['spare'], '|') !== false) //如果有多个值
  706. {
  707. $v['spare'] = explode('|', $v['spare']);
  708. foreach ($v['spare'] as $k => $vs) {
  709. if (stripos($v['zh'], '|') !== false) {
  710. $vzh = explode('|', $v['zh']);
  711. $dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $vzh[$k], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
  712. } else {
  713. $dtc[strtolower($vs)] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $vs);
  714. }
  715. }
  716. } else {
  717. $dtc[strtolower($v['spare'])] = array('classid' => $v['classid'], 'zh' => $v['zh'], 'id' => $v['id'], 'jm' => $v['jm'], 'spare' => $v['spare']);
  718. }
  719. }
  720. foreach ($info_list as $key=>$v)
  721. {
  722. $shopname = isset($shop_list[$v['shop']])?$shop_list[$v['shop']]:'';
  723. $skulist = $this->commodityread->find_all("productid = ".$v['productid']);
  724. $tmp_list = [];
  725. foreach($skulist as $skv){
  726. $tmp_sku = str_replace('--','-',$skv['sku']);
  727. $u9_info = $this->logic_order->skuTransferFeatures($tmp_sku,$typeclass,$dtc,$dtctitle,$classid);
  728. $tmp_list[] = [
  729. 'codeid' => $skv['codeid'],
  730. 'erp_sku' => $skv['sku'],
  731. 'jm'=>$u9_info['jm'],
  732. 'zh'=>$u9_info['zh'],
  733. ];
  734. }
  735. $tmp_codeid = "";
  736. $tmp_erp_sku = "";
  737. $tmp_jm = "";
  738. $tmp_zh = "";
  739. foreach($tmp_list as $t){
  740. $tmp_codeid .= $t['codeid']."<br>";
  741. $tmp_erp_sku .= $t['erp_sku']."<br>";
  742. $tmp_jm .= $t['jm']."<br>";
  743. $tmp_zh .= $t['zh']."<br>";
  744. }
  745. $final_list[] = [
  746. 'shopname' => $shopname,
  747. 'productid' => "'".$v['productid'],
  748. 'codeid' => $tmp_codeid,
  749. 'erp_sku' => $tmp_erp_sku,
  750. 'jm' => $tmp_jm,
  751. 'zh' => $tmp_zh,
  752. 'title' => $v['title'],
  753. ];
  754. }
  755. //生成excel
  756. $filename = '速卖通-'.date('Ymd',time()).".xlsx";
  757. $str = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head>
  758. <!--[if gte mso 9]><xml>
  759. <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>
  760. <![endif]-->
  761. </head><body>";
  762. $str .= "<table border=1 style='font-family: Microsoft Yahei;font-size: 13px;'>";
  763. $str .= "<tr><td>店铺名称</td><td>速卖通产品编码</td><td>速卖通标题</td><td>速卖通SKU名称</td><td>erpSKU</td><td>用友简码</td><td>仓库品名</td></tr>";
  764. foreach ($final_list as $key=>$value)
  765. {
  766. $str .= "<tr>";
  767. $str .= "<td>". $value['shopname']. "</td>";
  768. $str .= "<td>". $value['productid']. "</td>";
  769. $str .= "<td>". $value['title']. "</td>";
  770. $str .= "<td>". $value['codeid']. "</td>";
  771. $str .= "<td>". $value['erp_sku']. "</td>";
  772. $str .= "<td>". $value['jm']. "</td>";
  773. $str .= "<td>". $value['zh']. "</td>";
  774. $str .= "</tr>";
  775. }
  776. $str .= "</table></body></html>";
  777. header( "Content-Type: application/vnd.ms-excel; name='excel'" );
  778. header( "Content-type: application/octet-stream" );
  779. header( "Content-Disposition: attachment; filename=".$filename );
  780. header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  781. header( "Pragma: no-cache" );
  782. header( "Expires: 0" );
  783. exit($str);
  784. }
  785. }
  786. }