WordServer.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Servers\Wps;
  3. use App\Servers\System\CommonServer;
  4. use App\Servers\System\RedisDataServer;
  5. use PhpOffice\PhpWord\IOFactory;
  6. use PhpOffice\PhpWord\TemplateProcessor;
  7. use PhpOffice\PhpWord\Writer\HTML;
  8. class WordServer
  9. {
  10. static private $server = null;
  11. private $propertyKey = [
  12. 'property_id',
  13. 'property_contract_sn',
  14. 'property_name',
  15. 'property_original_credit',
  16. 'property_original_debtor',
  17. 'end_date',
  18. ];
  19. function __construct()
  20. {
  21. }
  22. /**
  23. * 创建对象
  24. * @return WordServer
  25. */
  26. static function creatServer()
  27. {
  28. if (empty(self::$server)) self::$server = new WordServer();
  29. return self::$server;
  30. }
  31. /**
  32. * 验证word文件是否包含某个字符串
  33. * @param $filePath
  34. * @param $searchChar
  35. * @return bool
  36. */
  37. function containText($filePath,$searchChar)
  38. {
  39. $path=public_path('storage/word_html/'.date('Ymd').'/');
  40. if(!is_dir($path)){
  41. CommonServer::creatServer()->creatDir('storage/word_html/'.date('Ymd').'/');
  42. }
  43. $htmlPath=$path.md5($filePath).'.html';
  44. if(!is_file($htmlPath)){
  45. $phpWord = IOFactory::load($filePath);
  46. $htmlWriter =new HTML($phpWord);
  47. $htmlWriter->save($htmlPath);
  48. }
  49. $htmlContent = file_get_contents($htmlPath);
  50. $htmlContent=strip_tags($htmlContent);
  51. return !strpos($htmlContent,$searchChar)===false;
  52. }
  53. /**
  54. * 合同保存的时候生成文本替换内容
  55. * @param FactoringContractFile $file
  56. * @return FactoringContractFile|\Illuminate\Database\Eloquent\Model
  57. * @throws \PhpOffice\PhpWord\Exception\Exception
  58. */
  59. function replaceWordText(FactoringContractFile $file)
  60. {
  61. $filePath=public_path($file->{'file_path'});
  62. $phpWord = new TemplateProcessor($filePath);
  63. $texts=FactoringContractText::where('level','2')->where('custom','<>','')->select(['id','custom','key'])->get();
  64. //获取项目合同
  65. $factoring_contract = FactoringContract::where('file_no',$file->{'file_no'})->first();
  66. //获取对应项目
  67. $factoring = Factoring::where('id',$factoring_contract->{'factoring_id'})->first();
  68. //获取资产
  69. $property = FactoringProperty::whereIn('id',$factoring_contract->{'property'})->get();
  70. foreach ($texts as $text){
  71. if(in_array($text->{'key'},['zrf_seal','bls_seal'])){
  72. continue;
  73. }
  74. $keyData=explode(':',$text['key']);
  75. if(count($keyData)!=2)continue;
  76. if ($keyData[0]=='property'){
  77. //如果涉及到资产
  78. $str = $this->getPropertyTextKey($property,$keyData[1]);
  79. }elseif($keyData[0]=='contract'){
  80. //合同和项目基础信息
  81. $str = $this->getContractTextKey($factoring_contract,$keyData[1]);
  82. }elseif(in_array($keyData[0],['zrf','yszw','hxqy','bls'])){
  83. //合同和项目基础信息
  84. $str = $this->getPropertyFirmTextKey($property,$keyData[1],$keyData[0]);
  85. }elseif($keyData[0]=='earning'){
  86. //合同和项目基础信息
  87. $str = $this->getEarningTextKey($factoring,$keyData[1]);
  88. }
  89. if(empty($str))continue;
  90. $text['custom']=str_replace(['${','}'],'',$text['custom']);
  91. $phpWord->setValue($text['custom'], $str);
  92. }
  93. $file_path = 'contractComplete/' . date('Ymd') . '/'.$file['file_no'].'/'.$file['version'].'/';
  94. CommonServer::creatServer()->creatDir('storage/'.$file_path);
  95. $file_name = $file->{'file_name'};
  96. $phpWord->saveAs(public_path('storage/'.$file_path.$file_name));
  97. $file_url = 'https://'.env('APP_ADMIN_HOST').'/storage/'.$file_path.$file_name;
  98. $res = FactoringContractFile::create([
  99. 'file_no' => $file['file_no'],
  100. 'file_name' => $file_name,
  101. 'file_url' => $file_url,
  102. 'file_path' => $file_path.$file_name,
  103. 'version' => $file['version'],
  104. 'type' => 2,
  105. 'is_end' => 1,
  106. ]);
  107. $factoring_contract->update(['contracts_file' => $file_url,'end_file_id'=>$res['id']]);
  108. return $res;
  109. }
  110. /**
  111. * 获取相关收益信息
  112. * @param $factoring
  113. * @param $type
  114. * @return string
  115. */
  116. function getEarningTextKey($factoring,$type)
  117. {
  118. $str='0%';
  119. $type=str_replace($type,'earning_type',$type);
  120. if(empty($type) || !is_numeric($type)){
  121. return $str;
  122. }
  123. $model=FactoringEarning::where('factoring_id',$factoring['id'])->where('earning_type',$type)->first();
  124. if(empty($model)){
  125. return $str;
  126. }
  127. return $model->{'earning_num'}.'%('.$model->{'earning_msg'}.')';
  128. }
  129. /**
  130. * 企业基本信息匹配
  131. * @param $propertys
  132. * @param $key
  133. * @param $type
  134. * @return false|string
  135. */
  136. function getPropertyFirmTextKey($propertys,$key,$type)
  137. {
  138. $data=[];
  139. foreach ($propertys as $property){
  140. if($type=='zrf'){
  141. $firmName=Firm::where('id',$property['firm_id'])->value('firm_name');
  142. }elseif ($type=='yszw'){
  143. $firmName=$property['original_debtor'];
  144. }elseif ($type=='hxqy'){
  145. $firmName=$property['core_enterprise'];
  146. }elseif ($type=='bls'){
  147. $firmName=Firm::where('id',$property['factor_id'])->value('firm_name');
  148. }
  149. if(empty($firmName)){
  150. return false;
  151. }
  152. if($key=='firm_name'){
  153. $data[]=$firmName;
  154. }else{
  155. $firm=$this->getFirmTextKey($firmName);
  156. if(empty($firm)){
  157. $data[]='无';
  158. }else{
  159. $data[]=empty($firm[$key])?'无':$firm[$key];
  160. }
  161. }
  162. }
  163. $data=array_unique($data);
  164. return implode(';',$data);
  165. }
  166. /**
  167. * 获取企业基本信息
  168. * @param $firmName
  169. * @return Firm|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
  170. */
  171. function getFirmTextKey($firmName)
  172. {
  173. $firmData=RedisDataServer::creatServer()->getData(md5($firmName),'json');
  174. if(empty($firmData)){
  175. $firmData=FirmServer::creatServer()->queryFirm($firmName);
  176. RedisDataServer::creatServer()->setData(md5($firmName),$firmData,'json');
  177. }
  178. return $firmData;
  179. }
  180. /**
  181. * 合同信息获取
  182. * @param $factoring_contract
  183. * @param $key
  184. * @return mixed|string
  185. */
  186. function getContractTextKey($factoring_contract,$key)
  187. {
  188. $str='';
  189. if($key=='contract_sn'){
  190. $str=$factoring_contract->{'contract_sn'};
  191. }
  192. return $str;
  193. }
  194. /**
  195. * 获取基础替换信息
  196. * @param $key
  197. * @param $factoring_contract
  198. * @param $factoring
  199. * @return mixed|void
  200. */
  201. function getTextKey($key,$factoring_contract,$factoring)
  202. {
  203. if (isset($factoring_contract[$key])){
  204. return $factoring_contract[$key];
  205. }
  206. if (isset($factoring[$key])){
  207. return $factoring[$key];
  208. }
  209. return '';
  210. }
  211. /**
  212. * 获取资产替换信息
  213. * @param $property
  214. * @param $key
  215. * @return string
  216. */
  217. function getPropertyTextKey($property,$key)
  218. {
  219. $data=[];
  220. foreach ($property as $value){
  221. if($key=='id'){
  222. $data[]=$value->{'id'} . '('.$value->{'property_name'}.')';
  223. }elseif ($key=='name'){
  224. $data[]=$value->{'property_name'};
  225. }elseif ($key=='contract_sn'){
  226. $data[]=$value->{'contract_sn'} . '('.$value->{'property_name'}.')';
  227. }elseif ($key=='original_credit'){
  228. $data[]=$value->{'original_credit'} . '('.$value->{'property_name'}.')';
  229. }elseif ($key=='original_debtor'){
  230. $data[]=$value->{'original_debtor'} . '('.$value->{'property_name'}.')';
  231. }elseif ($key=='end_date'){
  232. $data[]=$value->{'end_date'} . '('.$value->{'property_name'}.')';
  233. }elseif ($key=='due_date'){
  234. $data[]=$value->{'due_date'} . '('.$value->{'property_name'}.')';
  235. }elseif ($key=='property_sn'){
  236. $data[]=$value->{'property_sn'} . '('.$value->{'property_name'}.')';
  237. }
  238. }
  239. return implode(';',$data);
  240. }
  241. }