TaskSystem/app/common/logic/contract/ContractLogic.php
2023-08-24 16:32:54 +08:00

356 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\logic\contract;
use app\api\controller\JunziqianController;
use app\common\model\contract\Contract;
use app\common\logic\BaseLogic;
use app\common\model\Company;
use app\common\model\user\User;
use think\facade\Db;
use app\api\logic\SmsLogic;
use app\common\logic\CompanyLogic;
use app\common\logic\UserLogic;
/**
* Contract逻辑
* Class ContractLogic
* @package app\adminapi\logic\contract
*/
class ContractLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/18 14:28
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
Contract::create([
'company_id' => $params['company_id'],
'contract_type' => $params['contract_type'],
'contract_no' => $params['contract_no'],
'file' => $params['file'],
'status' => $params['status'],
'party_a' => $params['party_a'],
'party_b' => $params['party_b'],
'area_manager' => $params['area_manager']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/18 14:28
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
Contract::where('id', $params['id'])->update([
'company_id' => $params['company_id'],
'contract_type' => $params['contract_type'],
'contract_no' => $params['contract_no'],
'file' => $params['file'],
'status' => $params['status'],
'party_a' => $params['party_a'],
'party_b' => $params['party_b'],
'area_manager' => $params['area_manager']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/18 14:28
*/
public static function delete(array $params): bool
{
return Contract::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/07/18 14:28
*/
public static function detail($params)
{
$data = Db::name('contract')->where('id', $params['id'])
->withAttr('party_b_info', function ($value, $data) {
if ($data['type'] == 1) {
$field = ['id,company_name,company_type,company_type company_type_name,organization_code,
province,city,area,street,village,brigade,address,province province_name,city city_name,area area_name,street street_name,village village_name,brigade brigade_name,master_phone,master_name,
qualification'];
$company = Company::where(['id' => $data['party_b']])->field($field)->find();
if(empty($company)){
return [];
}else{
$company=$company->toArray();
}
$company['qualification'] = json_decode($company['qualification'], true);
if($company['qualification'] && isset($company['qualification']['other_qualifications'])){
$company['qualification']['other_qualifications']=json_decode($company['qualification']['other_qualifications'],true);
}
return $company;
} else {
$filed = ['id,id_card,avatar,nickname,sex,mobile,
province,city,area,street,village,brigade,address,province province_name,city city_name,area area_name,street street_name,village village_name,brigade brigade_name,nickname master_name,
qualification'];
$user = User::where('id', $data['party_b'])->field($filed)->find();
if(empty($user)){
return [];
}else{
$user=$user->toArray();
}
$user['qualification'] = json_decode($user['qualification'], true);
if($user['qualification'] && isset($user['qualification']['other_qualifications'])){
$user['qualification']['other_qualifications']=json_decode($user['qualification']['other_qualifications'],true);
}
return $user;
}
})
->withAttr('party_a_info', function ($value, $data) {
$field = ['id,company_name,company_type,company_type company_type_name,organization_code,
province,city,area,street,village,brigade,address,province province_name,city city_name,area area_name,street street_name,village village_name,brigade brigade_name,master_phone,master_name,
qualification'];
$company = Company::where(['id' => $data['party_a']])->field($field)->find()->toArray();
$company['qualification'] = json_decode($company['qualification'], true);
if($company['qualification'] && isset($company['qualification']['other_qualifications'])){
$company['qualification']['other_qualifications']=json_decode($company['qualification']['other_qualifications'],true);
}
return $company;
})
->withAttr('area_manager_name', function ($value, $data) {
return Db::name('admin')->where('id', $data['area_manager'])->value('name');
})
->withAttr('contract_type_name', function ($value, $data) {
return Db::name('dict_data')->where('id', $data['contract_type'])->value('name');
})
->withAttr('type_name', function ($value, $data) {
return $data['type'] == 1 ? '公司' : '个人';
})
->withAttr('status_name', function ($value, $data) {
return $data['status'] == 1 ? '已签约' : '未签约';
})
->find();
//如果是租赁合同则获取租赁的车辆信息
if($data && $data['contract_type'] == 29){
$url = 'http://logistics.lihaink.cn/api/getRentRecord?contract_id='.$params['id'];
$result = curl_get($url);
if($result['code'] == 1){
$data['carsRentInfo'] = $result['data'];
}
}
return $data;
}
//生成合同
public static function Initiate_contract($data)
{
$model = Contract::where(['party_b' => $data['party_b']])->find();
if (empty($model)) {
$model = new Contract();
$model->contract_no = time();
$model->create_time = time();
}
if($data['party_a']<=0){
return self::setError('甲方不能为空');
}
if($data['party_b']<=0){
return self::setError('乙方不能为空');
}
if($data['type']==1){
if($data['party_a'] == $data['party_b']){
return self::setError('甲方和乙方不能是同一个公司');
}
}
try {
$model->check_status = 1;
$model->update_time = time();
$model->setAttrs($data);
$res = $model->save($data);
//如果是租赁合同,调用接口通知物流系统
if($data['contract_type'] == 29){
curl_post('http://logistics.lihaink.cn/api/setContract',[],[
'party_a' => $data['party_a'],
'party_b' => $data['party_b'],
'num' => $data['num'],
'rent_type' => 1
]);
}
if($res){
return ['code'=>1,'msg'=>'发起成功,等待平台风控部上传合同','data'=>['id'=>$model->id]];
}else{
return ['code'=>0,'msg'=>'发起失败,请稍后重试',];
}
}catch(\Exception $e){
return ['code'=>0,'msg'=>$e->getMessage()];
}
}
// /**发送合同 */
public static function Draftingcontracts($params,$type=1)
{
if($type==1){
$result = CompanyLogic::detail($params);
}else{
$result = UserLogic::detail($params['id']);
}
if ($result && isset($result['contract']) && isset($result['contract']['file']) && $result['contract']['file'] != '') {
if ($result['contract']['check_status'] == 3) {
return self::setError('你已经生成过合同,请问重复生成');
}
if($type==1){
$name=$result['company_name'];
$data = [
'name' => $name . '的合同',
'signatories' => [
['fullName' => $name, 'identityType' => 12, 'identityCard' => $result['organization_code'], 'email' => $result['master_email'], 'noNeedVerify' => 1, 'signLevel' => 1],
['fullName' => $result['contract']['party_a_info']['company_name'], 'identityType' => 12, 'identityCard' => $result['contract']['party_a_info']['organization_code'], 'email' => $result['contract']['party_a_info']['master_email'], 'noNeedVerify' => 1, 'signLevel' => 1]
],
'url' => $result['contract']['file']
];
}else{
$name=$result['nickname'];
$data = [
'name' => $name . '的合同',
'signatories' => [
['fullName' => $name, 'identityType' => 1, 'identityCard' => $result['id_card'], 'mobile' => $result['mobile'], 'noNeedVerify' => 1, 'signLevel' => 1],
['fullName' => $result['contract']['party_a_info']['company_name'], 'identityType' => 12, 'identityCard' => $result['contract']['party_a_info']['organization_code'], 'email' => $result['contract']['party_a_info']['master_email'], 'noNeedVerify' => 1, 'signLevel' => 1]
],
'url' => $result['contract']['file']
];
}
$res = app(JunziqianController::class)->Signing($data, $result['contract']['id']);
if ($res->success == true) {
Db::name('contract')->where('id', $result['contract']['id'])->update(['contract_no' => $res->data, 'check_status' => 3]);
self::postsms(['id'=>$result['contract']['id']]);
return true;
} else {
return self::setError($res->msg);
}
} else {
return self::setError('生成合同成功失败,联系管理员');
}
}
//**发送短信 */
public static function postsms($params)
{
$result = self::detail($params);
if ($result && $result['file'] != '') {
//发送短信
$data = [
[
"applyNo" => $result['contract_no'], //TODO *
"fullName" => $result['party_a_info']['company_name'], //TODO *
"identityCard" => $result['party_a_info']['organization_code'], //TODO *
"identityType" => 12, //TODO *
"master_phone" => $result['party_a_info']['master_phone'],
"type"=>"party_a"
],
];
if($result['type']==1){
$data[]= [
"applyNo" => $result['contract_no'], //TODO *
"fullName" => $result['party_b_info']['company_name'], //TODO *
"identityCard" => $result['party_b_info']['organization_code'], //TODO *
"identityType" => 12, //TODO *
"master_phone" => $result['party_b_info']['master_phone'],
"type"=>"party_b"
];
}else{
$data[]= [
"applyNo" => $result['contract_no'], //TODO *
"fullName" => $result['party_b_info']['nickname'], //TODO *
"identityCard" => $result['party_b_info']['id_card'], //TODO *
"identityType" => 1, //TODO *
"master_phone" => $result['party_b_info']['mobile'],
"type"=>"party_b"
];
}
$find = Db::name('contract')->where('id', $params['id'])
->withAttr('contract_type_name', function ($value, $data) {
return Db::name('dict_data')->where('id', $data['contract_type'])->value('name');
})->find();
$url = [];
foreach ($data as $k => $v) {
$res = app(JunziqianController::class)->SigningLink($v);
if ($res->success == true) {
if ($v['type'] == 'party_a') {
$url['party_a'] =$res->data;
} else {
$url['party_b'] =$res->data;
}
//发送短信
$sms = [
'mobile' => $v['master_phone'],
'name' => $v['fullName'],
'type' => '《' . $find['contract_type_name'] . '》',
'code' => 'api/Hetong/url?id=' . $find['id'].'&type='.$v['type'],
'scene' => 'WQ'
];
$result = SmsLogic::contractUrl($sms);
if ($result != true) {
return self::setError(SmsLogic::getError());
}
} else {
return self::setError($res->msg);
}
}
Db::name('contract')->where('id', $find['id'])->update(['url' => json_encode($url)]);
return true;
}else{
return self::setError('没找到合同,联系管理员');
}
}
}