JobInv.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\Admin;
  3. use think\facade\Db;
  4. use think\facade\Log;
  5. use think\queue\Job;
  6. class JobInv
  7. {
  8. public function fire(Job $job, $data){
  9. //....这里执行具体的任务
  10. $check = $this->checkJob($data);
  11. if(!$check){
  12. $job->delete();
  13. return;
  14. }
  15. echo "执行识别数据";
  16. $do=$this->doJob($data,$job);
  17. if($do){
  18. $job->delete();
  19. }else{
  20. if ($job->attempts() > 3) {
  21. //通过这个方法可以检查这个任务已经重试了几次了
  22. $job->delete();
  23. $this->failed($data);
  24. }
  25. }
  26. Log::write(var_export($job->attempts()),true);
  27. }
  28. public function failed($data){
  29. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(['status'=>2]);
  30. $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"=",2]])
  31. ->count();
  32. if($num>0){
  33. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>9]);
  34. }
  35. }
  36. private function doJob($data,$job){
  37. $result = OcrByInvoice(["url"=>$data['image']]);
  38. $result = json_decode($result,true);
  39. if(isset($result['code'])&&$result['code']==0 ){
  40. if(isset($object['error_code'])){
  41. if ($job->attempts() > 3) {
  42. //通过这个方法可以检查这个任务已经重试了几次了
  43. $job->delete();
  44. $this->failed($data);
  45. }else{
  46. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["remark"=>json_encode($result,JSON_UNESCAPED_UNICODE)]);
  47. return false;
  48. }
  49. }
  50. $object = $result['data']["result"];
  51. try{
  52. $ist = Db::name("invoice")->where([['code',"=",$object['code']],["number","=",$object['number']]])
  53. ->find();
  54. if($ist){
  55. if($ist['id']!=$data['id']){
  56. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["remark"=>"已存在发票{$object['code']}"]);
  57. $sql ="update cfp_pay_stages set remark=CONCAT(remark,'已存在发票{$object['code']};') where id={$data['payid']}";
  58. Db::query($sql);
  59. $job->delete();
  60. $this->failed($data);
  61. return false;
  62. }
  63. }
  64. $obje=json_encode($object,JSON_UNESCAPED_UNICODE);
  65. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["status"=>0,'invoice_info'=>$obje,"remark"=>""]);
  66. $info = Db::name("invoice_info")->where("invid","=",$data['id'])->find();
  67. if(empty($info)){
  68. $info=[];
  69. }
  70. Log::write(Db::name("invoice_img")->getLastSql());
  71. $info['payid']= $data['payid'];
  72. $info['invid']= $data['id'];
  73. $info['type']= $object['type'];
  74. $info['code']= $object['code'];
  75. $info['number']= $object['number'];
  76. $info['check_code']= $object['check_code'];
  77. $info['issue_date']= $object['issue_date'];
  78. $info['buyer_name']= $object['buyer_name'];
  79. $info['buyer_id']= $object['buyer_id'];
  80. $info['buyer_address']= $object['buyer_address'];
  81. $info['buyer_bank']= $object['buyer_bank'];
  82. $info['seller_name']= $object['seller_name'];
  83. $info['seller_id']= $object['seller_id'];
  84. $info['seller_address']= $object['seller_address'];
  85. $info['seller_bank']= $object['seller_bank'];
  86. // $info['subtotal_amount']= str_replace("¥",'',$object['subtotal_amount']);
  87. // $info['subtotal_tax']=str_replace("¥",'',$object['subtotal_tax']);
  88. // $info['total']= str_replace("¥",'',$object['total']);
  89. $info['subtotal_amount']= $object['subtotal_amount'];
  90. $info['subtotal_tax']=$object['subtotal_tax'];
  91. $info['total']= $object['total'];
  92. $info['item_list']= json_encode($object['item_list'],JSON_UNESCAPED_UNICODE);
  93. $info['status']= 0;
  94. $info['change_field']= "";
  95. Db::name("invoice_info")->save($info);
  96. $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"<>",0]])
  97. ->count();
  98. if($num<=0){
  99. // $this->lastCheck($data);
  100. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>5,"remark"=>""]);
  101. }
  102. return true;
  103. }catch (\Exception $e){
  104. echo $e->getMessage();
  105. return false;
  106. }
  107. }
  108. }
  109. private function checkJob($data){
  110. $list= Db::name("invoice_img")->where("id","=",$data['id'])->find();
  111. if($list['status']==0){
  112. return false;
  113. }
  114. return true;
  115. }
  116. private function CheckInvoice($invocie,$data){
  117. $paystage = Db::name("pay_stages")->where("id","=",$data['payid'])->find();
  118. $remak="";
  119. if($paystage['companyNo']!=""){
  120. $company = Db::name("company_info")->where("companyNo","=",$paystage['companyNo'])->find();
  121. if(!empty($company)){
  122. if($company['company_name']!=$invocie['buyer_name']){
  123. $remak.="\r\n 发票购买方企业名称不匹配";
  124. }
  125. if($company['company_license']!=$invocie['buyer_id']){
  126. $remak.="\r\n 发票购买方企业纳税识别号不匹配";
  127. }
  128. if(stripos($company['company_address'].$company['mobile'],trim($invocie['buyer_address']))===false){
  129. $remak.="\r\n 发票购买方企业地址不匹配";
  130. }
  131. if(stripos($company['bank_name'].$company['bankNo'],trim($invocie['buyer_bank']))===false){
  132. $remak.="\r\n 发票购买方企业银行账户不匹配";
  133. }
  134. }
  135. }
  136. // var_dump($company['company_address'].$company['mobile'],trim($invocie['buyer_address']));
  137. $invoiceInfo = Db::name("invoice")->where([["payid","=",$data['payid']],['status',"=",0]])->find();
  138. if(!empty($invoiceInfo)){
  139. if($invoiceInfo['seller_name']!=$invocie['seller_name']){
  140. $remak.="\r\n 发票销售方企业名称不匹配";
  141. }
  142. if($invoiceInfo['seller_id']!=$invocie['seller_id']){
  143. $remak.="\r\n 发票销售方企业纳税识别号不匹配";
  144. }
  145. if($invoiceInfo['seller_address']!=$invocie['seller_address']){
  146. $remak.="\r\n 发票销售方企业地址不匹配";
  147. }
  148. if($invoiceInfo['seller_bank']!=$invocie['seller_bank']){
  149. $remak.="\r\n 发票销售方企业银行账户不匹配";
  150. }
  151. }
  152. //var_dump($remak);
  153. if($remak!=""){
  154. $paystage['remark'].=$remak;
  155. $paystage['status']=9;
  156. $paystage['updatetime']=date("Y-m-d H:i:s");
  157. Db::name("pay_stages")->save($paystage);
  158. Log::write(Db::name("pay_stages")->getLastSql(),"pay");
  159. return false;
  160. }else{
  161. return true;
  162. }
  163. }
  164. private function lastCheck($data){
  165. $list = Db::name("invoice")->where("payid","=",$data["payid"])->select();
  166. $isbool = True;
  167. $remark="";
  168. $rate=0;
  169. $total = 0;//发票总额;
  170. $invid=[];
  171. $paystage = Db::name("pay_stages")->where("id","=",$data["payid"])->find();
  172. if(!empty($list)){
  173. foreach ($list as $key=>$value){
  174. $item= json_decode($value['item_list'],true);
  175. $total +=$value['total'];
  176. $temp_total = number_format($value['subtotal_amount']+$value['subtotal_tax'],2);
  177. if($temp_total!=$value["total"]){
  178. $isbool=false;
  179. $remark.="{$value['id']}发票价税总计不匹配;";
  180. $invid[]=$value['id'];
  181. }
  182. if(!empty($item)){
  183. $totla_amount =0;
  184. $totla_tax =0;
  185. $totla_fee =0;
  186. foreach ($item as $k=>$val){
  187. $rate_tax=substr($val['tax_rate'],0,-1);
  188. $temp_tax = number_format($val['amount']*$val['tax_rate']/100,2);
  189. if($temp_tax!=$val['tax']){
  190. $isbool=false;
  191. $remark.="{$value['id']}发票明细税额不匹配;";
  192. $invid[]=$value['id'];
  193. }
  194. $totla_amount+=$val['amount'];
  195. $totla_tax+=$val['tax'];
  196. if($rate==0){
  197. $rate=$rate_tax;
  198. }else{
  199. if($rate_tax!=$rate){
  200. $isbool=false;
  201. $remark.="{$value['id']}发票税率不匹配;";
  202. $invid[]=$value['id'];
  203. }
  204. }
  205. }
  206. $totla_amount = number_format($totla_amount,2);
  207. $totla_tax = number_format($totla_tax,2);
  208. if($totla_amount!==$value['subtotal_amount']){
  209. $isbool=false;
  210. $remark.="{$value['id']}发票明细总计金额不匹配;";
  211. $invid[]=$value['id'];
  212. }
  213. if($totla_tax!==$value['subtotal_tax']){
  214. $isbool=false;
  215. $remark.="{$value['id']}发票总计税额不匹配;";
  216. $invid[]=$value['id'];
  217. }
  218. }
  219. }
  220. $paytotal = number_format($paystage['pay_fee'],2);
  221. $total = number_format($total,2);
  222. if($paytotal!=$total){
  223. $isbool=false;
  224. $remark.="{$value['id']}发票总计金额不匹配;";
  225. $invid[]=$value['id'];
  226. }
  227. var_dump($remark,$invid);
  228. if($isbool){
  229. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>5,"updatetime"=>date("Y-m-d H:i:s")]);
  230. }else{
  231. $paystage['remark'].=$remark;
  232. $paystage['status']=9;
  233. $paystage['updatetime']=date("Y-m-d H:i:s");
  234. Db::name("pay_stages")->where("id","=",$data['payid'])->save($paystage);
  235. Db::name("invoice_img")->where("id","in",$invid)->save(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  236. }
  237. }
  238. }
  239. }