Apipf.php 16 KB

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