| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace App\Servers\Wps;
- use App\Servers\System\CommonServer;
- use App\Servers\System\RedisDataServer;
- use PhpOffice\PhpWord\IOFactory;
- use PhpOffice\PhpWord\TemplateProcessor;
- use PhpOffice\PhpWord\Writer\HTML;
- class WordServer
- {
- static private $server = null;
- private $propertyKey = [
- 'property_id',
- 'property_contract_sn',
- 'property_name',
- 'property_original_credit',
- 'property_original_debtor',
- 'end_date',
- ];
- function __construct()
- {
- }
- /**
- * 创建对象
- * @return WordServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new WordServer();
- return self::$server;
- }
- /**
- * 验证word文件是否包含某个字符串
- * @param $filePath
- * @param $searchChar
- * @return bool
- */
- function containText($filePath,$searchChar)
- {
- $path=public_path('storage/word_html/'.date('Ymd').'/');
- if(!is_dir($path)){
- CommonServer::creatServer()->creatDir('storage/word_html/'.date('Ymd').'/');
- }
- $htmlPath=$path.md5($filePath).'.html';
- if(!is_file($htmlPath)){
- $phpWord = IOFactory::load($filePath);
- $htmlWriter =new HTML($phpWord);
- $htmlWriter->save($htmlPath);
- }
- $htmlContent = file_get_contents($htmlPath);
- $htmlContent=strip_tags($htmlContent);
- return !strpos($htmlContent,$searchChar)===false;
- }
- /**
- * 合同保存的时候生成文本替换内容
- * @param FactoringContractFile $file
- * @return FactoringContractFile|\Illuminate\Database\Eloquent\Model
- * @throws \PhpOffice\PhpWord\Exception\Exception
- */
- function replaceWordText(FactoringContractFile $file)
- {
- $filePath=public_path($file->{'file_path'});
- $phpWord = new TemplateProcessor($filePath);
- $texts=FactoringContractText::where('level','2')->where('custom','<>','')->select(['id','custom','key'])->get();
- //获取项目合同
- $factoring_contract = FactoringContract::where('file_no',$file->{'file_no'})->first();
- //获取对应项目
- $factoring = Factoring::where('id',$factoring_contract->{'factoring_id'})->first();
- //获取资产
- $property = FactoringProperty::whereIn('id',$factoring_contract->{'property'})->get();
- foreach ($texts as $text){
- if(in_array($text->{'key'},['zrf_seal','bls_seal'])){
- continue;
- }
- $keyData=explode(':',$text['key']);
- if(count($keyData)!=2)continue;
- if ($keyData[0]=='property'){
- //如果涉及到资产
- $str = $this->getPropertyTextKey($property,$keyData[1]);
- }elseif($keyData[0]=='contract'){
- //合同和项目基础信息
- $str = $this->getContractTextKey($factoring_contract,$keyData[1]);
- }elseif(in_array($keyData[0],['zrf','yszw','hxqy','bls'])){
- //合同和项目基础信息
- $str = $this->getPropertyFirmTextKey($property,$keyData[1],$keyData[0]);
- }elseif($keyData[0]=='earning'){
- //合同和项目基础信息
- $str = $this->getEarningTextKey($factoring,$keyData[1]);
- }
- if(empty($str))continue;
- $text['custom']=str_replace(['${','}'],'',$text['custom']);
- $phpWord->setValue($text['custom'], $str);
- }
- $file_path = 'contractComplete/' . date('Ymd') . '/'.$file['file_no'].'/'.$file['version'].'/';
- CommonServer::creatServer()->creatDir('storage/'.$file_path);
- $file_name = $file->{'file_name'};
- $phpWord->saveAs(public_path('storage/'.$file_path.$file_name));
- $file_url = 'https://'.env('APP_ADMIN_HOST').'/storage/'.$file_path.$file_name;
- $res = FactoringContractFile::create([
- 'file_no' => $file['file_no'],
- 'file_name' => $file_name,
- 'file_url' => $file_url,
- 'file_path' => $file_path.$file_name,
- 'version' => $file['version'],
- 'type' => 2,
- 'is_end' => 1,
- ]);
- $factoring_contract->update(['contracts_file' => $file_url,'end_file_id'=>$res['id']]);
- return $res;
- }
- /**
- * 获取相关收益信息
- * @param $factoring
- * @param $type
- * @return string
- */
- function getEarningTextKey($factoring,$type)
- {
- $str='0%';
- $type=str_replace($type,'earning_type',$type);
- if(empty($type) || !is_numeric($type)){
- return $str;
- }
- $model=FactoringEarning::where('factoring_id',$factoring['id'])->where('earning_type',$type)->first();
- if(empty($model)){
- return $str;
- }
- return $model->{'earning_num'}.'%('.$model->{'earning_msg'}.')';
- }
- /**
- * 企业基本信息匹配
- * @param $propertys
- * @param $key
- * @param $type
- * @return false|string
- */
- function getPropertyFirmTextKey($propertys,$key,$type)
- {
- $data=[];
- foreach ($propertys as $property){
- if($type=='zrf'){
- $firmName=Firm::where('id',$property['firm_id'])->value('firm_name');
- }elseif ($type=='yszw'){
- $firmName=$property['original_debtor'];
- }elseif ($type=='hxqy'){
- $firmName=$property['core_enterprise'];
- }elseif ($type=='bls'){
- $firmName=Firm::where('id',$property['factor_id'])->value('firm_name');
- }
- if(empty($firmName)){
- return false;
- }
- if($key=='firm_name'){
- $data[]=$firmName;
- }else{
- $firm=$this->getFirmTextKey($firmName);
- if(empty($firm)){
- $data[]='无';
- }else{
- $data[]=empty($firm[$key])?'无':$firm[$key];
- }
- }
- }
- $data=array_unique($data);
- return implode(';',$data);
- }
- /**
- * 获取企业基本信息
- * @param $firmName
- * @return Firm|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
- */
- function getFirmTextKey($firmName)
- {
- $firmData=RedisDataServer::creatServer()->getData(md5($firmName),'json');
- if(empty($firmData)){
- $firmData=FirmServer::creatServer()->queryFirm($firmName);
- RedisDataServer::creatServer()->setData(md5($firmName),$firmData,'json');
- }
- return $firmData;
- }
- /**
- * 合同信息获取
- * @param $factoring_contract
- * @param $key
- * @return mixed|string
- */
- function getContractTextKey($factoring_contract,$key)
- {
- $str='';
- if($key=='contract_sn'){
- $str=$factoring_contract->{'contract_sn'};
- }
- return $str;
- }
- /**
- * 获取基础替换信息
- * @param $key
- * @param $factoring_contract
- * @param $factoring
- * @return mixed|void
- */
- function getTextKey($key,$factoring_contract,$factoring)
- {
- if (isset($factoring_contract[$key])){
- return $factoring_contract[$key];
- }
- if (isset($factoring[$key])){
- return $factoring[$key];
- }
- return '';
- }
- /**
- * 获取资产替换信息
- * @param $property
- * @param $key
- * @return string
- */
- function getPropertyTextKey($property,$key)
- {
- $data=[];
- foreach ($property as $value){
- if($key=='id'){
- $data[]=$value->{'id'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='name'){
- $data[]=$value->{'property_name'};
- }elseif ($key=='contract_sn'){
- $data[]=$value->{'contract_sn'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='original_credit'){
- $data[]=$value->{'original_credit'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='original_debtor'){
- $data[]=$value->{'original_debtor'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='end_date'){
- $data[]=$value->{'end_date'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='due_date'){
- $data[]=$value->{'due_date'} . '('.$value->{'property_name'}.')';
- }elseif ($key=='property_sn'){
- $data[]=$value->{'property_sn'} . '('.$value->{'property_name'}.')';
- }
- }
- return implode(';',$data);
- }
- }
|