Apipf.php 18 KB

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