Apipf.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * 本类是为了处理 erp协同其他系统,通过其他系统传递的条件 返回所需信息
  5. */
  6. class Apipf extends Start_Controller{
  7. public function __construct(){
  8. parent::__construct();
  9. $this->load->_model('Model_logic_order','logic_order');
  10. $this->load->_model('Model_logic_tools','logic_tools');
  11. $this->load->_model("Model_logic_ding",'logic_ding');
  12. $this->load->_model('Model_customer','customer');
  13. $this->load->_model('Model_typeclass','typeclass');
  14. }
  15. private $url = "http://wholesale.hnwmzp.cn/syncerp/customer";
  16. private $key = "bpng!pjgirv6amnfr"; //加密所需要到的key
  17. private $iv = "k4k!94m66oojtm2w";//加密所需要到的iv
  18. private $api = "202503121009@ly";
  19. private $filter_words = ['bonus','flashsale','clearance','giftpack','gift'];
  20. private $shop_list = [
  21. 1=>'h',
  22. 2=>'as',
  23. 3=>'ap',
  24. 4=>"wk",
  25. 5=>"y",
  26. 6=>"wg",
  27. ];
  28. //定义方法的调用规则 获取URI第二段值
  29. public function _remap($arg,$arg_array)
  30. {
  31. if($arg == 'checkMoreThree')//添加
  32. {
  33. $this->_checkMoreThree();
  34. }
  35. elseif($arg == 'customer_all_data')
  36. {
  37. $this->_customerAllTbData();
  38. }
  39. elseif($arg == 'customer_depart_data')
  40. {
  41. $this->_customerDepartTbData();
  42. }
  43. else
  44. {
  45. exit('No direct script access allowed');
  46. }
  47. }
  48. //检测客户是否超过3次且为传输
  49. public function _checkMoreThree(){
  50. $api = $this->input->get('api',true);
  51. if($api != $this->api){
  52. die("No data to be executed");
  53. }
  54. $hour = date("H");
  55. $minute = date("i");
  56. if($hour != 20){
  57. die("Execution conditions hour do not allow");
  58. }
  59. if(($minute >= 10)&&($minute <=40)){
  60. $this->db->query("update crowd_customer set more_three = 1 where num >= 3 and more_three = 0");
  61. die("No executable data available");
  62. }else{
  63. die("Execution conditions minute do not allow !");
  64. }
  65. }
  66. /***
  67. * 上传没有给批发站没有传递的客户信息
  68. */
  69. public function _customerAllTbData(){
  70. echo "<pre>";
  71. $api = $this->input->get('api',true);
  72. if($api != $this->api){
  73. die("No data to be executed");
  74. }
  75. $list = $this->customer->find_all("more_three = 1","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,3);
  76. //$list = $this->customer->find_all("id = 71015","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  77. if(empty($list)){
  78. die("This is a request without data");
  79. }
  80. $send_list = [];
  81. foreach($list as $k=>$v){
  82. $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
  83. $send_list[] = [
  84. 'customer_id'=>$v['id'],
  85. 'name'=>$v['name'],
  86. 'email'=>$v['email'],
  87. 'telephone'=>$v['phone'],
  88. 'ordered_mount'=>$orderinfo['order_mount'],
  89. 'ordered_num'=>$orderinfo['order_num'],
  90. 'ordered_qty'=>$orderinfo['order_qty'],
  91. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  92. // 'country'=>$v['country'],
  93. // 'province'=>$v['province'],
  94. // 'city'=>$v['city'],
  95. // 'street'=>$v['street'],
  96. // 'address'=>$v['address'].$v['address2'],
  97. // 'zipcode'=>$v['zipcode']
  98. 'orders'=>$orderinfo['orders'],
  99. ];
  100. }
  101. if(empty($send_list)){
  102. die("There is no customer data to be synchronized");
  103. }
  104. $header = [];
  105. $url = $this->url;
  106. var_dump($send_list);
  107. die;
  108. foreach($send_list as $item){
  109. if(empty($item['orders'])){
  110. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  111. $this->customer->save([
  112. 'more_three'=>3,
  113. 'is_tb'=>1,
  114. ],$item['customer_id']);
  115. continue;
  116. }
  117. $res = $this->sendHttp($url,$header,$item);
  118. if($res){
  119. $this->customer->save([
  120. 'more_three'=>3,
  121. 'is_tb'=>1,
  122. ],$item['customer_id']);
  123. }
  124. }
  125. }
  126. //同步已经同步过的订单客户近期订单
  127. public function _customerDepartTbData(){
  128. $api = $this->input->get('api',true);
  129. if($api != $this->api){
  130. die("No data to be executed");
  131. }
  132. $list = $this->customer->find_all("more_three = 3 and is_tb = 0","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  133. //$list = $this->customer->find_all("id = 71015","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  134. if(empty($list)){
  135. die("This is a request without data");
  136. }
  137. $send_list = [];
  138. foreach($list as $k=>$v){
  139. $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
  140. $send_list[] = [
  141. 'customer_id'=>$v['id'],
  142. 'name'=>$v['name'],
  143. 'email'=>$v['email'],
  144. 'telephone'=>$v['phone'],
  145. 'ordered_mount'=>$orderinfo['order_mount'],
  146. 'ordered_num'=>$orderinfo['order_num'],
  147. 'ordered_qty'=>$orderinfo['order_qty'],
  148. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  149. // 'country'=>$v['country'],
  150. // 'province'=>$v['province'],
  151. // 'city'=>$v['city'],
  152. // 'street'=>$v['street'],
  153. // 'address'=>$v['address'].$v['address2'],
  154. // 'zipcode'=>$v['zipcode']
  155. 'orders'=>$orderinfo['orders'],
  156. ];
  157. }
  158. if(empty($send_list)){
  159. die("There is no customer data to be synchronized");
  160. }
  161. $header = [];
  162. $url = $this->url;
  163. var_dump($send_list);
  164. die;
  165. foreach($send_list as $item){
  166. if(empty($item['orders'])){
  167. $this->customer->save([
  168. 'is_tb'=>1,
  169. ],$item['customer_id']);
  170. continue;
  171. }
  172. $res = $this->sendHttp($url,$header,$item);
  173. if($res){
  174. $this->customer->save([
  175. 'is_tb'=>1,
  176. ],$item['customer_id']);
  177. }
  178. }
  179. }
  180. /**
  181. * $email 邮箱
  182. * $shop 店铺id
  183. * $type 1 获取全部订单列表 2 只获取昨天新增的订单列表 一般在凌晨1点执行
  184. */
  185. //要求订单必须为线下单,并且为processing或complete
  186. private function getOrderList($email,$shop,$type = 2){
  187. //var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
  188. $fdata = $this->fullorder->find_all('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks,librarytime,source','id desc');
  189. //var_dump($fdata);
  190. $ret_list = [];
  191. $order_mount = 0;//订单总金额
  192. $order_num = 0;//下单次数
  193. $order_qty = 0;//订单产品总数
  194. foreach ($fdata as $k=>$v)
  195. {
  196. //var_dump($v);
  197. $sku_list = [];
  198. $issku_arr = explode(",",trim($v['issku'],','));
  199. $quantity_arr = explode(";",trim($v['quantity'],";"));
  200. $product_arr = explode(",",trim($v['product'],";"));
  201. $tmp_type = 0;
  202. $tmp_qty = 0;
  203. $jisuan_qty = 0;//需要更新的产品的总数量上
  204. //货物的种类按照订单的原始sku种类 数量也是
  205. foreach($issku_arr as $key => $item){
  206. $flag_jishu = false;
  207. foreach($this->filter_words as $val){
  208. //判断sku中是否有这种东西
  209. if (strpos(strtolower($item),$val ) !== false) {
  210. $flag_jishu = true;
  211. }
  212. }
  213. //var_dump($flag_jishu);
  214. if(!$flag_jishu){
  215. $tmp_type++;
  216. if(isset($quantity_arr[$key])){
  217. $tmp_qty = $quantity_arr[$key] + $tmp_qty;
  218. }else{
  219. $tmp_qty = $tmp_qty + 1;
  220. }
  221. }
  222. if(isset($quantity_arr[$key])){
  223. $jisuan_qty = $quantity_arr[$key]*1 + $jisuan_qty;
  224. }else{
  225. $jisuan_qty = $jisuan_qty + 1;
  226. }
  227. $sku_list[] = [
  228. 'sku'=>$item,
  229. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  230. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  231. ];
  232. }
  233. $goods_list = [];
  234. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  235. foreach($fpdata_arr as $key => $item){
  236. $tmp_data = explode("|",$item);
  237. if(!empty($tmp_data[1])){
  238. $goods_list[] = [
  239. 'goods_name'=>$tmp_data[1],
  240. 'qty'=>$tmp_data[2]
  241. ];
  242. }else{
  243. continue;
  244. }
  245. }
  246. //如果产品种类少于2种 且产品数量也小于2 直接排除
  247. if(($tmp_type <2) && ($tmp_qty < 2)){
  248. continue;
  249. }
  250. // var_dump($v['orderinfo']);
  251. // var_dump($tmp_type);
  252. // var_dump($tmp_qty);
  253. // var_dump("------------------");
  254. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  255. $order_num++;//下单次数
  256. $order_qty = $order_qty + $jisuan_qty;//订单产品总数
  257. if($type == 1){
  258. $ret_list[] = [
  259. 'shop'=>$v['shop'],
  260. 'orderinfo'=>$v['orderinfo'],
  261. 'sku_list'=>$sku_list,
  262. 'goods_list'=>$goods_list,
  263. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  264. 'dtime'=>date('Y-m-d',$v['dtime']),
  265. 'name'=>$v['name'],
  266. 'email'=>$v['email'],
  267. 'shouldmoney'=>$v['shouldmoney'],
  268. 'shipremarks'=>$v['shipremarks']
  269. ];
  270. }else{
  271. $end_time = strtotime(date("Y-m-d"));
  272. $start_time = $end_time - 24* 60 *60;
  273. //这里只按照出库时间算
  274. if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
  275. $ret_list[] = [
  276. 'shop'=>$v['shop'],
  277. 'orderinfo'=>$v['orderinfo'],
  278. 'sku_list'=>$sku_list,
  279. 'goods_list'=>$goods_list,
  280. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  281. 'dtime'=>date('Y-m-d',$v['dtime']),
  282. 'name'=>$v['name'],
  283. 'email'=>$v['email'],
  284. 'shouldmoney'=>$v['shouldmoney'],
  285. 'shipremarks'=>$v['shipremarks']
  286. ];
  287. }
  288. }
  289. }
  290. $final_list = [];
  291. foreach($ret_list as $k=>$v){
  292. $orderitems = [];
  293. foreach($sku_list as $key1 => $val1){
  294. $orderitems[] = [
  295. 'product_name'=>$val1['product_name'],
  296. 'sku'=>$val1['sku'],
  297. 'qty'=>$val1['qty'],
  298. ];
  299. }
  300. $final_list[] = [
  301. 'shop'=>$v['shop'],
  302. 'increment id'=>$v['orderinfo'],
  303. 'status'=>$v['state'],
  304. 'grand_total'=>$v['shouldmoney'],
  305. 'order_created'=>$v['dtime'],
  306. //'product_name'=>array_column($sku_list,'product_name'),
  307. //'sku'=>array_column($sku_list,'sku'),
  308. //'erp_goods_name'=>array_column($goods_list,'goods_name'),
  309. //'qty'=>array_column($sku_list,'qty'),
  310. //'price'=>$v['shouldmoney'],
  311. 'orderitems'=>$orderitems,
  312. ];
  313. }
  314. return [
  315. 'order_mount'=>$order_mount,
  316. 'order_num'=>$order_num,
  317. 'order_qty'=>$order_qty,
  318. 'orders'=>$final_list,
  319. ];
  320. }
  321. public function _orderTbAllData(){
  322. $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
  323. $typeclass = array_column($typeclass,null,'id');
  324. $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
  325. foreach($list as $k=>$v){
  326. $order_list = $this->getOrder($v,$typeclass);
  327. if(empty($order_list)){
  328. continue;
  329. }
  330. }
  331. }
  332. private function getOrder($v,$typeclass){
  333. $fdata = $this->fullorder->find_all('email = "'.$customer['email'].'" and shop = "'.$customer['shop'].'" and mergeid = 0 and reviewtime <= ','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  334. $ret_list = [];
  335. foreach ($fdata as $k=>$v)
  336. {
  337. $sku_list = [];
  338. $issku_arr = explode(",",trim($v['issku'],','));
  339. $quantity_arr = explode(";",trim($v['quantity'],";"));
  340. $product_arr = explode(",",trim($v['product'],";"));
  341. foreach($issku_arr as $key => $item){
  342. $sku_list[] = [
  343. 'sku'=>$item,
  344. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  345. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  346. ];
  347. }
  348. $goods_list = [];
  349. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  350. if(!empty($tmp_data[1])){
  351. foreach($fpdata_arr as $key => $item){
  352. $tmp_data = explode("|",$item);
  353. $goods_list[] = [
  354. 'goods_name'=>$tmp_data[1],
  355. 'qty'=>$tmp_data[2]
  356. ];
  357. }
  358. }else{
  359. continue;
  360. }
  361. $ret_list[] = [
  362. 'shop'=>$v['shop'],
  363. 'orderinfo'=>$v['orderinfo'],
  364. 'sku_list'=>$sku_list,
  365. 'goods_list'=>$goods_list,
  366. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  367. 'dtime'=>date('Y-m-d',$v['dtime']),
  368. 'name'=>$v['name'],
  369. 'email'=>$v['email'],
  370. 'shouldmoney'=>$v['shouldmoney'],
  371. 'shipremarks'=>$v['shipremarks']
  372. ];
  373. }
  374. return $fdata;
  375. }
  376. public function _orderTbData(){
  377. $fdata = $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  378. $ret_list = [];
  379. echo "<pre>";
  380. foreach ($fdata as $k=>$v)
  381. {
  382. var_dump($v);
  383. $sku_list = [];
  384. $issku_arr = explode(",",trim($v['issku'],','));
  385. $quantity_arr = explode(";",trim($v['quantity'],";"));
  386. $product_arr = explode(",",trim($v['product'],";"));
  387. foreach($issku_arr as $key => $item){
  388. $sku_list[] = [
  389. 'sku'=>$item,
  390. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  391. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  392. ];
  393. }
  394. $goods_list = [];
  395. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  396. if(!empty($tmp_data[1])){
  397. foreach($fpdata_arr as $key => $item){
  398. $tmp_data = explode("|",$item);
  399. $goods_list[] = [
  400. 'goods_name'=>$tmp_data[1],
  401. 'qty'=>$tmp_data[2]
  402. ];
  403. }
  404. }else{
  405. continue;
  406. }
  407. $ret_list[] = [
  408. 'shop'=>$v['shop'],
  409. 'orderinfo'=>$v['orderinfo'],
  410. 'sku_list'=>$sku_list,
  411. 'goods_list'=>$goods_list,
  412. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  413. 'dtime'=>date('Y-m-d',$v['dtime']),
  414. 'name'=>$v['name'],
  415. 'email'=>$v['email'],
  416. 'shouldmoney'=>$v['shouldmoney'],
  417. 'shipremarks'=>$v['shipremarks']
  418. ];
  419. }
  420. echo "<pre>";
  421. var_dump($ret_list);
  422. }
  423. private function sendHttp($url,$header,$list){
  424. $ch = curl_init();
  425. curl_setopt($ch, CURLOPT_URL, $url);
  426. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  427. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  428. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  429. curl_setopt($ch, CURLOPT_POST, 1);
  430. curl_setopt($ch, CURLOPT_POSTFIELDS, $list);
  431. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  432. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  433. $res = curl_exec($ch);
  434. $res = json_decode($res,true);
  435. return $res;
  436. }
  437. }