123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- <?php
- namespace addons\qingdongams\model;
- use think\Exception;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- *合同表
- */
- class Contract Extends Model {
- use SoftDelete;
- // 表名,不含前缀
- protected $name = 'qingdongams_contract';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- //创建合同
- public static function createContract($params) {
- //自定义字段
- $other = [];
- foreach ($params as $name => $val) {
- if (strstr($name, 'other_') !== false) {
- if(is_array($val)){
- $other[$name] = implode(',',$val);
- }else{
- $other[$name] = $val;
- }
- unset($params[$name]);
- }else{
- if($params[$name] === ''){
- $params[$name]=NULL;
- }
- }
- }
- $product = [];
- if (isset($params['product']) && $params['product']) {
- $product = $params['product'];
- unset($params['product']);
- foreach ($product as $tkey => $t) {
- $parts=$t['parts']??[];
- $new=[];
- if($parts){
- foreach ($parts as $v){
- if(!isset($v['part_id'])){
- continue;
- }
- $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
- }
- }
- $product[$tkey]['parts'] = json_encode($new);
- }
- }
- if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
- $params['ratios'] = '';
- }
- if (isset($params['ratios']) && $params['ratios']) {
- foreach ($params['ratios'] as $v) {
- if (empty($v['staff_id'])) {
- throw new Exception('业绩归属人必须全部选择');
- }
- }
- $params['ratios'] = json_encode($params['ratios']);
- }
- $customer=Customer::where(['id'=>$params['customer_id']])->find();
- if(empty($customer)){
- throw new Exception('客户不存在');
- }
- $params['owner_staff_id'] = $customer->owner_staff_id;
- if (isset($params['plan'])) {
- $plan = $params['plan'];
- unset($params['plan']);
- }
- $staff = Staff::info();
- if (!empty($staff)) {
- $params['create_staff_id'] = $staff->id;
- }
- $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
- $params['flow_id'] = $flow['flow_id'];
- $params['order_id'] = $flow['order_id'];
- if ($flow['status'] == 0) {//发起人自选
- if (empty($params['flow_staff_ids'])) {
- throw new Exception('审批人必须选择');
- }
- $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
- } else {
- $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
- }
- $Model = new self;
- $result = $Model->allowField(true)->save($params);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $lastId = $Model->id;
- $otherModel = new ContractOther();
- if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
- // 验证失败 输出错误信息
- throw new Exception($otherModel->getError());
- }
- $addRatios = [];
- if (isset($params['ratios']) && $params['ratios']) {
- $ratios=json_decode($params['ratios'],true);
- foreach ($ratios as $v) {
- $addRatios[] = [
- 'contract_id' => $lastId,
- 'ratio' => $v['ratio'],
- 'staff_id' => $v['staff_id'],
- 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
- ];
- }
- }
- //业绩分成 默认自己全部
- if (empty($addRatios)) {
- $addRatios[] = [
- 'contract_id' => $lastId,
- 'ratio' => 100,
- 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id ,
- 'ratio_money' => $params['money']
- ];
- }
- if ($addRatios) {
- $ratioModel = new ContractRatio();
- $ratioModel->insertAll($addRatios);
- }
- if (isset($plan)) {
- $addPlan = [];
- foreach ($plan as $pl) {
- $addPlan[] = [
- 'contract_id' => $lastId,
- 'customer_id' => $params['customer_id'],
- 'money' => $pl['money'],
- 'return_date' => $pl['time'],
- 'num' => $pl['name'],
- 'create_staff_id' => $params['owner_staff_id'],
- 'owner_staff_id' => $params['owner_staff_id'],
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- }
- $planModel = new ReceivablesPlan();
- $planModel->insertAll($addPlan);
- }
- $addProduct = [];
- foreach ($product as $v) {
- if (isset($v['id'])){
- unset($v['id']);
- }
- $v['contract_id'] = $lastId;
- $addProduct[] = $v;
- }
- if ($addProduct) {
- $productModel = new ContractProduct();
- $productModel->allowField(true)->saveAll($addProduct);
- }
- if ($flow['status'] == 1) {//固定审批
- //发送审批通知
- Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $lastId);
- } else {//发起人自选 依次审批
- $staff_id = explode(',', $params['flow_staff_ids'])[0];
- if ($staff_id) {
- ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $lastId, $staff_id);
- }
- }
- OperationLog::createLog(OperationLog::CONTRACT_TYPE, $lastId, '创建合同');
- return $lastId;
- }
- //修改合同
- public static function updateContract($params) {
- $row=self::get($params['id']);
- //自定义字段
- $other = [];
- foreach ($params as $name => $val) {
- if (strstr($name, 'other_') !== false) {
- if(is_array($val)){
- $other[$name] = implode(',',$val);
- }else{
- $other[$name] = $val;
- }
- unset($params[$name]);
- }else{
- if($params[$name] === ''){
- $params[$name]=NULL;
- }
- }
- }
- $product = [];
- if (isset($params['product'])) {
- $product = $params['product'];
- unset($params['product']);
- foreach ($product as $tkey => $t) {
- $parts=$t['parts']??[];
- $new=[];
- if($parts){
- foreach ($parts as $v){
- if(!isset($v['part_id'])){
- continue;
- }
- $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
- }
- }
- $product[$tkey]['parts'] = json_encode($new);
- }
- }
- if (isset($params['plan'])) {
- $plan = $params['plan'];
- unset($params['plan']);
- }
- if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
- $params['ratios'] = '';
- }
- if (isset($params['ratios']) && $params['ratios']) {
- foreach ($params['ratios'] as $v) {
- if (empty($v['staff_id'])) {
- throw new Exception('业绩归属人必须全部选择');
- }
- }
- $params['ratios'] = json_encode($params['ratios']);
- }
- $params['owner_staff_id'] = $row->owner_staff_id;
- $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
- $params['flow_id'] = $flow['flow_id'];
- $params['order_id'] = $flow['order_id'];
- if ($flow['status'] == 0) {//发起人自选
- if (empty($params['flow_staff_ids'])) {
- throw new Exception('审批人必须选择');
- }
- $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
- } else {
- $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
- }
- $Model = new self;
- $params['check_status'] = 1;
- // 调用当前模型对应的User验证器类进行数据验证
- $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $otherModel = new ContractOther();
- if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
- // 验证失败 输出错误信息
- throw new Exception($otherModel->getError());
- }
- $addRatios = [];
- if (isset($params['ratios']) && $params['ratios']) {
- $ratios=json_decode($params['ratios'],true);
- foreach ($ratios as $v) {
- $addRatios[] = [
- 'contract_id' => $params['id'],
- 'ratio' => $v['ratio'],
- 'staff_id' => $v['staff_id'],
- 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
- ];
- }
- }
- //业绩分成 默认自己全部
- $staff = Staff::info();
- if (empty($addRatios)) {
- $addRatios[] = [
- 'contract_id' => $params['id'],
- 'ratio' => 100,
- 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id,
- 'ratio_money' => $params['money']
- ];
- }
- if (isset($plan)) {
- $addPlan = [];
- foreach ($plan as $pl) {
- $addPlan[] = [
- 'contract_id' => $params['id'],
- 'customer_id' => $params['customer_id'],
- 'money' => $pl['money'],
- 'return_date' => $pl['time'],
- 'num' => $pl['name'],
- 'create_staff_id' => $params['owner_staff_id'],
- 'owner_staff_id' => $params['owner_staff_id'],
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- }
- $planModel = new ReceivablesPlan();
- $planModel->where(['contract_id' => $params['id']])->delete();
- $planModel->insertAll($addPlan);
- }
- $ratioModel = new ContractRatio();
- $ratioModel->where(['contract_id' => $params['id']])->delete();
- if ($addRatios) {
- $ratioModel->insertAll($addRatios);
- }
- $addProduct = [];
- if($product){
- foreach ($product as $v) {
- if (isset($v['id'])){
- unset($v['id']);
- }
- $v['contract_id'] = $params['id'];
- $addProduct[] = $v;
- }
- }
- if ($addProduct) {
- $productModel = new ContractProduct();
- $productModel->where(['contract_id' => $params['id']])->delete();
- $productModel = new ContractProduct();
- $productModel->allowField(true)->saveAll($addProduct);
- }
- if ($flow['status'] == 1) {//固定审批
- //发送审批通知
- Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $params['id']);
- } else {//发起人自选 依次审批
- $staff_id = explode(',', $params['flow_staff_ids'])[0];
- if ($staff_id) {
- ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $params['id'], $staff_id);
- }
- }
- return true;
- }
- public static function updateContractAddress($params)
- {
- $row = self::get($params['id']);
- $Model = new self;
- // 调用当前模型对应的User验证器类进行数据验证
- $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $staff = Staff::info();
- $edit_reason = '';
- if ($params['delivery_address']) {
- $edit_reason = $params['delivery_address'] . $params['delivery_address_detail'];
- }
- OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '合同地址:' . $edit_reason);
- return true;
- }
- //修改合同乙方信息
- public static function updateContractPartyB($params)
- {
- $row = self::get($params['id']);
- $Model = new self;
- // 调用当前模型对应的User验证器类进行数据验证
- $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $edit_reason=$params['edit_reason'] ?? '';
- OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '修改合同乙方信息,原因:' . $edit_reason);
- return true;
- }
- //回执附件id
- public function getReceiptFileIdsAttr($value)
- {
- if (empty($value)) {
- return [];
- }
- $files = explode(',', $value);
- $result = [];
- foreach ($files as $fid) {
- if ($fid) {
- $result[] = cdnurl(File::getUrl($fid), true);
- }
- }
- return $result;
- }
- //添加发票物流
- public function getInvoiceFileIdsAttr($value)
- {
- if (empty($value)) {
- return [];
- }
- $files = explode(',', $value);
- $result = [];
- foreach ($files as $fid) {
- if ($fid) {
- $result[] = cdnurl(File::getUrl($fid), true);
- }
- }
- return $result;
- }
- //获取回款图片
- public function getCollectionImgAttr($value)
- {
- $files = explode(',', $value);
- $result = [];
- foreach ($files as $fid) {
- if ($fid) {
- $result[] = ['id' => $fid, 'url' => cdnurl(File::getUrl($fid), true)];
- }
- }
- return $result;
- }
- //获取附件
- public function getFilesAttr($value)
- {
- $files = explode(',', $value);
- $result = [];
- foreach ($files as $fid) {
- if ($fid) {
- $result[] = ['id' => $fid, 'url' => cdnurl(File::getUrl($fid), true)];
- }
- }
- return $result;
- }
- //添加合同回执单
- public static function receiptContract($params)
- {
- $result = self::where(['id' => $params['id']])->update([
- 'receipt_file_ids' => $params['receipt_file_ids'],//回访
- 'receipt_date' => $params['receipt_date'],//回访
- ]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception('修改信息错误');
- }
- OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '添加合同回执单');
- return true;
- }
- //添加合同发票物流
- public static function invoiceContract($params)
- {
- $result = self::where(['id' => $params['id']])->update([
- 'invoice_file_ids' => $params['invoice_file_ids'],//
- 'invoice_date' => $params['invoice_date'],//
- 'invoice_logistics_number' => $params['invoice_logistics_number'],//
- ]);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception('修改信息错误');
- }
- OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '添加合同发票物流');
- return true;
- }
- //获取更新时间
- public function getUpdatetimeAttr($value) {
- return date('Y-m-d H:i:s', $value);
- }
- //获取回款计划
- public function getPlanAttr($value)
- {
- return json_decode($value, true);
- }
- //负责人
- public function ownerStaff() {
- return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
- }
- //获取合同相关信息
- public function contractOther() {
- return $this->belongsTo(ContractOther::class,'id','id');
- }
- //客户
- public function customer() {
- return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
- }
- //商机
- public function business() {
- return $this->hasOne(Business::class, 'id', 'business_id')->field('id,name');
- }
- //联系人
- public function contacts() {
- return $this->hasOne(Contacts::class, 'id', 'contacts_id')->field('id,name');
- }
- //员工
- public function staff() {
- return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
- }
- //签约人
- public function orderStaff() {
- return $this->hasOne(Staff::class, 'id', 'order_staff_id')->field('id,name,mobile');
- }
- //产品
- public function product(){
- return $this->hasMany(ContractProduct::class,'contract_id','id')->with('product');
- }
- //回款统计
- public function receivables() {
- return $this->hasOne(Receivables::class, 'contract_id', 'id')->where(['check_status' => 2])->group('contract_id')->field('contract_id,sum(money) as repayment_money');
- }
- //发票
- public function invoice() {
- return $this->hasOne(Invoice::class, 'contract_id', 'id')->where(['check_status' => 2])->group('contract_id')->field('contract_id,sum(money) as invoice_money');
- }
- //导入
- public static function importContract($data) {
- $addContacts = [];
- $addOther = [];
- $addratio = [];
- foreach ($data as $params) {
- //自定义字段
- $other = [];
- foreach ($params as $name => $val) {
- if (strstr($name, 'other_') !== false) {
- if(is_array($val)){
- $other[$name] = implode(',',$val);
- }else{
- $other[$name] = $val;
- }
- unset($params[$name]);
- }else{
- if($params[$name] === ''){
- $params[$name]=NULL;
- }
- }
- }
- $params['createtime'] = time();
- $params['updatetime'] = time();
- $params['check_status'] = 2;
- $other['id'] = $params['id'];
- $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
- $addContacts[] = $params;
- $addratio[] = array(
- 'contract_id'=>$params['id'],
- 'ratio'=>100,
- 'staff_id'=>isset($params['owner_staff_id'])?$params['owner_staff_id']:0,
- 'ratio_money'=>isset($params['money'])?$params['money']:0,
- );
- }
- $model = new self;
- // 调用当前模型对应的User验证器类进行数据验证
- $result = $model->allowField(true)->insertAll($addContacts);
- $otherModel = new ContractOther();
- $otherModel->allowField(true)->insertAll($addOther);
- $modelRatio = new ContractRatio();
- $modelRatio->allowField(true)->insertAll($addratio);
- return true;
- }
- //获取合同编号
- public static function getNum()
- {
- return 'C' . date('Ymd') . rand(10000,99999);
- }
- }
|