Apipf.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. foreach($send_list as $item){
  108. if(empty($item['orders'])){
  109. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  110. $this->customer->save([
  111. 'more_three'=>3,
  112. 'is_tb'=>1,
  113. ],$item['customer_id']);
  114. continue;
  115. }
  116. $res = $this->sendHttp($url,$header,['data'=>[$item]]);
  117. var_dump($res);
  118. if($res['status']){
  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,[
  173. $item
  174. ]);
  175. if($res){
  176. $this->customer->save([
  177. 'is_tb'=>1,
  178. ],$item['customer_id']);
  179. }
  180. }
  181. }
  182. /**
  183. * $email 邮箱
  184. * $shop 店铺id
  185. * $type 1 获取全部订单列表 2 只获取昨天新增的订单列表 一般在凌晨1点执行
  186. */
  187. //要求订单必须为线下单,并且为processing或complete
  188. private function getOrderList($email,$shop,$type = 2){
  189. //var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
  190. $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');
  191. //var_dump($fdata);
  192. $ret_list = [];
  193. $order_mount = 0;//订单总金额
  194. $order_num = 0;//下单次数
  195. $order_qty = 0;//订单产品总数
  196. foreach ($fdata as $k=>$v)
  197. {
  198. //var_dump($v);
  199. $sku_list = [];
  200. $issku_arr = explode(",",trim($v['issku'],','));
  201. $quantity_arr = explode(";",trim($v['quantity'],";"));
  202. $product_arr = explode(",",trim($v['product'],";"));
  203. $tmp_type = 0;
  204. $tmp_qty = 0;
  205. $jisuan_qty = 0;//需要更新的产品的总数量上
  206. //货物的种类按照订单的原始sku种类 数量也是
  207. foreach($issku_arr as $key => $item){
  208. $flag_jishu = false;
  209. foreach($this->filter_words as $val){
  210. //判断sku中是否有这种东西
  211. if (strpos(strtolower($item),$val ) !== false) {
  212. $flag_jishu = true;
  213. }
  214. }
  215. //var_dump($flag_jishu);
  216. if(!$flag_jishu){
  217. $tmp_type++;
  218. if(isset($quantity_arr[$key])){
  219. $tmp_qty = $quantity_arr[$key] + $tmp_qty;
  220. }else{
  221. $tmp_qty = $tmp_qty + 1;
  222. }
  223. }
  224. if(isset($quantity_arr[$key])){
  225. $jisuan_qty = $quantity_arr[$key]*1 + $jisuan_qty;
  226. }else{
  227. $jisuan_qty = $jisuan_qty + 1;
  228. }
  229. $sku_list[] = [
  230. 'sku'=>$item,
  231. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  232. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  233. ];
  234. }
  235. $goods_list = [];
  236. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  237. foreach($fpdata_arr as $key => $item){
  238. $tmp_data = explode("|",$item);
  239. if(!empty($tmp_data[1])){
  240. $goods_list[] = [
  241. 'goods_name'=>$tmp_data[1],
  242. 'qty'=>$tmp_data[2]
  243. ];
  244. }else{
  245. continue;
  246. }
  247. }
  248. //如果产品种类少于2种 且产品数量也小于2 直接排除
  249. if(($tmp_type <2) && ($tmp_qty < 2)){
  250. continue;
  251. }
  252. // var_dump($v['orderinfo']);
  253. // var_dump($tmp_type);
  254. // var_dump($tmp_qty);
  255. // var_dump("------------------");
  256. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  257. $order_num++;//下单次数
  258. $order_qty = $order_qty + $jisuan_qty;//订单产品总数
  259. if($type == 1){
  260. $ret_list[] = [
  261. 'shop'=>$v['shop'],
  262. 'orderinfo'=>$v['orderinfo'],
  263. 'sku_list'=>$sku_list,
  264. 'goods_list'=>$goods_list,
  265. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  266. 'dtime'=>date('Y-m-d',$v['dtime']),
  267. 'name'=>$v['name'],
  268. 'email'=>$v['email'],
  269. 'shouldmoney'=>$v['shouldmoney'],
  270. 'shipremarks'=>$v['shipremarks']
  271. ];
  272. }else{
  273. $end_time = strtotime(date("Y-m-d"));
  274. $start_time = $end_time - 24* 60 *60;
  275. //这里只按照出库时间算
  276. if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
  277. $ret_list[] = [
  278. 'shop'=>$v['shop'],
  279. 'orderinfo'=>$v['orderinfo'],
  280. 'sku_list'=>$sku_list,
  281. 'goods_list'=>$goods_list,
  282. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  283. 'dtime'=>date('Y-m-d',$v['dtime']),
  284. 'name'=>$v['name'],
  285. 'email'=>$v['email'],
  286. 'shouldmoney'=>$v['shouldmoney'],
  287. 'shipremarks'=>$v['shipremarks']
  288. ];
  289. }
  290. }
  291. }
  292. $final_list = [];
  293. foreach($ret_list as $k=>$v){
  294. $orderitems = [];
  295. foreach($sku_list as $key1 => $val1){
  296. $orderitems[] = [
  297. 'product_name'=>$val1['product_name'],
  298. 'sku'=>$val1['sku'],
  299. 'qty'=>$val1['qty'],
  300. ];
  301. }
  302. $final_list[] = [
  303. 'shop'=>$v['shop'],
  304. 'increment id'=>$v['orderinfo'],
  305. 'status'=>$v['state'],
  306. 'grand_total'=>$v['shouldmoney'],
  307. 'order_created'=>$v['dtime'],
  308. //'product_name'=>array_column($sku_list,'product_name'),
  309. //'sku'=>array_column($sku_list,'sku'),
  310. //'erp_goods_name'=>array_column($goods_list,'goods_name'),
  311. //'qty'=>array_column($sku_list,'qty'),
  312. //'price'=>$v['shouldmoney'],
  313. 'orderitems'=>$orderitems,
  314. ];
  315. }
  316. return [
  317. 'order_mount'=>$order_mount,
  318. 'order_num'=>$order_num,
  319. 'order_qty'=>$order_qty,
  320. 'orders'=>$final_list,
  321. ];
  322. }
  323. public function _orderTbAllData(){
  324. $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
  325. $typeclass = array_column($typeclass,null,'id');
  326. $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
  327. foreach($list as $k=>$v){
  328. $order_list = $this->getOrder($v,$typeclass);
  329. if(empty($order_list)){
  330. continue;
  331. }
  332. }
  333. }
  334. private function getOrder($v,$typeclass){
  335. $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');
  336. $ret_list = [];
  337. foreach ($fdata as $k=>$v)
  338. {
  339. $sku_list = [];
  340. $issku_arr = explode(",",trim($v['issku'],','));
  341. $quantity_arr = explode(";",trim($v['quantity'],";"));
  342. $product_arr = explode(",",trim($v['product'],";"));
  343. foreach($issku_arr as $key => $item){
  344. $sku_list[] = [
  345. 'sku'=>$item,
  346. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  347. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  348. ];
  349. }
  350. $goods_list = [];
  351. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  352. if(!empty($tmp_data[1])){
  353. foreach($fpdata_arr as $key => $item){
  354. $tmp_data = explode("|",$item);
  355. $goods_list[] = [
  356. 'goods_name'=>$tmp_data[1],
  357. 'qty'=>$tmp_data[2]
  358. ];
  359. }
  360. }else{
  361. continue;
  362. }
  363. $ret_list[] = [
  364. 'shop'=>$v['shop'],
  365. 'orderinfo'=>$v['orderinfo'],
  366. 'sku_list'=>$sku_list,
  367. 'goods_list'=>$goods_list,
  368. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  369. 'dtime'=>date('Y-m-d',$v['dtime']),
  370. 'name'=>$v['name'],
  371. 'email'=>$v['email'],
  372. 'shouldmoney'=>$v['shouldmoney'],
  373. 'shipremarks'=>$v['shipremarks']
  374. ];
  375. }
  376. return $fdata;
  377. }
  378. public function _orderTbData(){
  379. $fdata = $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  380. $ret_list = [];
  381. echo "<pre>";
  382. foreach ($fdata as $k=>$v)
  383. {
  384. var_dump($v);
  385. $sku_list = [];
  386. $issku_arr = explode(",",trim($v['issku'],','));
  387. $quantity_arr = explode(";",trim($v['quantity'],";"));
  388. $product_arr = explode(",",trim($v['product'],";"));
  389. foreach($issku_arr as $key => $item){
  390. $sku_list[] = [
  391. 'sku'=>$item,
  392. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  393. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  394. ];
  395. }
  396. $goods_list = [];
  397. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  398. if(!empty($tmp_data[1])){
  399. foreach($fpdata_arr as $key => $item){
  400. $tmp_data = explode("|",$item);
  401. $goods_list[] = [
  402. 'goods_name'=>$tmp_data[1],
  403. 'qty'=>$tmp_data[2]
  404. ];
  405. }
  406. }else{
  407. continue;
  408. }
  409. $ret_list[] = [
  410. 'shop'=>$v['shop'],
  411. 'orderinfo'=>$v['orderinfo'],
  412. 'sku_list'=>$sku_list,
  413. 'goods_list'=>$goods_list,
  414. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  415. 'dtime'=>date('Y-m-d',$v['dtime']),
  416. 'name'=>$v['name'],
  417. 'email'=>$v['email'],
  418. 'shouldmoney'=>$v['shouldmoney'],
  419. 'shipremarks'=>$v['shipremarks']
  420. ];
  421. }
  422. echo "<pre>";
  423. var_dump($ret_list);
  424. }
  425. private function sendHttp($url,$header,$list){
  426. $ch = curl_init();
  427. curl_setopt($ch, CURLOPT_URL, $url);
  428. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  429. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  430. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  431. curl_setopt($ch, CURLOPT_POST, 1);
  432. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($list));
  433. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  434. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  435. $res = curl_exec($ch);
  436. $res = json_decode($res,true);
  437. return $res;
  438. }
  439. }