You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/crm/model/ReceivablesPlan.php

496 lines
18 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | Description: 回款计划计划
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\model;
3 years ago
use app\admin\traits\FieldVerificationTrait;
4 years ago
use think\Db;
use app\admin\model\Common;
use app\crm\model\Contract as ContractModel;
use think\Request;
use think\Validate;
class ReceivablesPlan extends Common
{
3 years ago
use FieldVerificationTrait;
4 years ago
/**
* 为了数据库的整洁同时又不影响Model和Controller的名称
* 我们约定每个模块的数据表都加上相同的前缀比如CRM模块用crm作为数据表前缀
*/
protected $name = 'crm_receivables_plan';
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $autoWriteTimestamp = true;
/**
* [getDataList 回款计划list]
* @author Michael_xu
* @param [string] $map [查询条件]
* @param [number] $page [当前页数]
* @param [number] $limit [每页数量]
* @param [string] $types 1 未使用的回款计划
* @return [array] [description]
*/
public function getDataList($request)
4 years ago
{
4 years ago
$userModel = new \app\admin\model\User();
$search = $request['search'];
$user_id = $request['user_id'];
$scene_id = (int)$request['scene_id'];
$check_status = $request['check_status'];
$types = $request['types'];
$getCount = $request['getCount'];
$status = isset($request['status']) ? $request['status'] : 1;
4 years ago
$dealt = $request['dealt']; # 待办事项
4 years ago
unset($request['scene_id']);
unset($request['search']);
unset($request['user_id']);
unset($request['check_status']);
unset($request['types']);
unset($request['getCount']);
unset($request['status']);
4 years ago
unset($request['dealt']);
4 years ago
$request = $this->fmtRequest( $request );
$map = $request['map'] ? : [];
if (isset($map['search'])) {
//普通筛选
$map['name'] = ['like', '%'.$map['search'].'%'];
unset($map['search']);
} else {
3 years ago
// 高级筛选
$map = advancedQuery($map, 'crm', 'receivables_plan', 'index');
4 years ago
}
if ($map['receivables_plan.owner_user_id']) {
$map['contract.owner_user_id'] = $map['receivables_plan.owner_user_id'];
unset($map['receivables_plan.owner_user_id']);
}
$whereData = [];
if ($check_status) {
unset($map['receivables_plan.check_status']);
if ($check_status == 2) {
$map['receivables.check_status'] = $check_status;
} else {
unset($map['receivables_plan.receivables_id']);
$data = [];
$data['check_status'] = $check_status;
$whereData = function($query) use ($data){
$query->where(['receivables_plan.receivables_id'=> ['eq',0]])
->whereOr(['receivables.check_status' => $data['check_status']]);
};
}
}
// @ymob 2019-12-11 17:51:54
// 修改回款时,回款计划选项列表应该包含该回款对应的回款计划 不能过滤
// 将types改为statusstatus可用的回款计划 fanqi
4 years ago
if (empty($dealt)) { # 不是待办事项
if ($request['map']['receivables_id']) {
if (!empty($request['map']['contract_id'])) {
$map = "
4 years ago
(`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = {$request['map']['receivables_id']})
OR
(`receivables_plan`.`contract_id` = {$request['map']['contract_id']} AND `receivables_plan`.`receivables_id` = 0)
";
4 years ago
} else {
$map = " (`receivables_plan`.`receivables_id` = 0 )";
}
} elseif ($status == 0) {
$map['receivables_plan.receivables_id'] = 0;
4 years ago
}
4 years ago
}
4 years ago
$dataCount = db('crm_receivables_plan')
->alias('receivables_plan')
->join('__CRM_CONTRACT__ contract','receivables_plan.contract_id = contract.contract_id','LEFT')
->join('__CRM_CUSTOMER__ customer','receivables_plan.customer_id = customer.customer_id','LEFT')
->join('__CRM_RECEIVABLES__ receivables','receivables_plan.plan_id = receivables.plan_id','LEFT')
->where($map)
->where($whereData)
->count('receivables_plan.plan_id');
if (!empty($getCount) && $getCount == 1) {
$data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
return $data;
}
$list = db('crm_receivables_plan')
->alias('receivables_plan')
->join('__CRM_CONTRACT__ contract','receivables_plan.contract_id = contract.contract_id','LEFT')
->join('__CRM_CUSTOMER__ customer','receivables_plan.customer_id = customer.customer_id','LEFT')
->join('__CRM_RECEIVABLES__ receivables','receivables_plan.plan_id = receivables.plan_id','LEFT')
->limit($request['offset'], $request['length'])
->field('receivables_plan.*,customer.name as customer_name,contract.num as contract_name,receivables.receivables_id,receivables.check_status')
->where($map)
->where($whereData)
->select();
foreach ($list as $k=>$v) {
$list[$k]['create_user_id_info'] = $userModel->getUserById($v['create_user_id']);
$list[$k]['contract_id_info']['name'] = $v['contract_name'] ? : '';
$list[$k]['contract_id_info']['contract_id'] = $v['contract_id'] ? : '';
$list[$k]['customer_id_info']['name'] = $v['customer_name'] ? : '';
$list[$k]['customer_id_info']['customer_id'] = $v['customer_id'] ? : '';
}
$data = [];
$data['list'] = $list;
$data['dataCount'] = $dataCount ? : 0;
return $data ? : [];
}
/**
* 创建回款计划信息
* @author Michael_xu
* @param
* @return
*/
public function createData($param)
3 years ago
{
$userId = $param['user_id'];
unset($param['user_id']);
4 years ago
if (!$param['contract_id']) {
$this->error = '请先选择合同';
return false;
} else {
$res = ContractModel::where(['contract_id' => $param['contract_id']])->value('check_status');
if (6 == $res) {
$this->error = '合同已作废';
return false;
}
if (!in_array($res,['2'])) {
$this->error = '当前合同未审核通过,不能添加回款';
return false;
}
}
if ($param['remind'] > 90) {
$this->error = '提前提醒最大时间为 90 天';
return false;
}
3 years ago
// // 自动验证
// $validate = validate($this->name);
// if (!$validate->check($param)) {
// $this->error = $validate->getError();
// return false;
// }
// 数据验证
$validateResult = $this->fieldDataValidate($param, 'crm_receivables_plan', $userId);
if (!empty($validateResult)) {
$this->error = $validateResult;
return false;
}
4 years ago
if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
//期数规则1,2,3..
$maxNum = db('crm_receivables_plan')->where(['contract_id' => $param['contract_id']])->max('num');
$param['num'] = $maxNum ? $maxNum+1 : 1;
//提醒日期
$param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
3 years ago
$fieldModel = new \app\admin\model\Field();
// 处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_receivables_plan');
foreach ($arrFieldAtt AS $key => $value) {
$param[$value] = !empty($param[$value]) ? arrayToString($param[$value]) : '';
}
// 处理日期date类型
$dateField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date');
foreach ($dateField AS $key => $value) {
$param[$value] = !empty($param[$value]) ? $param[$value] : null;
}
// 处理手写签名类型
$handwritingField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'handwriting_sign');
foreach ($handwritingField AS $key => $value) {
$param[$value] = !empty($param[$value]['file_id']) ? $param[$value]['file_id'] : '';
}
// 处理地址、定位、日期区间、明细表格类型字段
$receivablesPlanData = [];
$positionField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'position');
$locationField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'location');
$dateIntervalField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date_interval');
$detailTableField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'detail_table');
foreach ($param AS $key => $value) {
// 处理地址类型字段数据
if (in_array($key, $positionField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$positionNames = array_column($value, 'name');
$param[$key] = implode(',', $positionNames);
} else {
$param[$key] = '';
}
}
// 处理定位类型字段数据
if (in_array($key, $locationField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $value['address'];
} else {
$param[$key] = '';
}
}
// 处理日期区间类型字段数据
if (in_array($key, $dateIntervalField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = implode('_', $value);
} else {
$param[$key] = '';
}
}
// 处理明细表格类型字段数据
if (in_array($key, $detailTableField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $key;
} else {
$param[$key] = '';
}
}
}
4 years ago
if ($this->data($param)->allowField(true)->save()) {
$data = [];
$data['plan_id'] = $this->plan_id;
3 years ago
// 添加回款计划扩展数据
array_walk($receivablesPlanData, function (&$val) use ($data) {
$val['plan_id'] = $data['plan_id'];
});
db('crm_receivables_plan_data')->insertAll($receivablesPlanData);
4 years ago
return $data;
} else {
$this->error = '添加失败';
return false;
}
}
/**
* 编辑回款计划
* @author Michael_xu
* @param
* @return
*/
public function updateDataById($param, $plan_id = '')
{
3 years ago
$userId = $param['user_id'];
unset($param['user_id']);
4 years ago
$dataInfo = $this->getDataById($plan_id);
if (!$dataInfo) {
$this->error = '数据不存在或已删除';
return false;
}
$param['plan_id'] = $plan_id;
//过滤不能修改的字段
$unUpdateField = ['num','create_user_id','is_deleted','delete_time','delete_user_id'];
foreach ($unUpdateField as $v) {
unset($param[$v]);
}
3 years ago
// // 自动验证
// $validate = validate($this->name);
// if (!$validate->check($param)) {
// $this->error = $validate->getError();
// return false;
// }
// 数据验证
$validateResult = $this->fieldDataValidate($param, 'crm_receivables_plan', $userId, $plan_id);
if (!empty($validateResult)) {
$this->error = $validateResult;
return false;
}
4 years ago
if ($param['file_ids']) $param['file'] = arrayToString($param['file_ids']); //附件
//提醒日期
3 years ago
$param['remind_date'] = $param['remind'] ? date('Y-m-d',strtotime($param['return_date'])-86400*$param['remind']) : $param['return_date'];
$fieldModel = new \app\admin\model\Field();
// 处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_receivables_plan');
foreach ($arrFieldAtt AS $key => $value) {
$param[$value] = !empty($param[$value]) ? arrayToString($param[$value]) : '';
}
// 处理日期date类型
$dateField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date');
foreach ($dateField AS $key => $value) {
$param[$value] = !empty($param[$value]) ? $param[$value] : null;
}
// 处理手写签名类型
$handwritingField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'handwriting_sign');
foreach ($handwritingField AS $key => $value) {
$param[$value] = !empty($param[$value]['file_id']) ? $param[$value]['file_id'] : '';
}
// 处理地址、定位、日期区间、明细表格类型字段
$receivablesPlanData = [];
$positionField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'position');
$locationField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'location');
$dateIntervalField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'date_interval');
$detailTableField = $fieldModel->getFieldByFormType('crm_receivables_plan', 'detail_table');
foreach ($param AS $key => $value) {
// 处理地址类型字段数据
if (in_array($key, $positionField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$positionNames = array_column($value, 'name');
$param[$key] = implode(',', $positionNames);
} else {
$param[$key] = '';
}
}
// 处理定位类型字段数据
if (in_array($key, $locationField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $value['address'];
} else {
$param[$key] = '';
}
}
// 处理日期区间类型字段数据
if (in_array($key, $dateIntervalField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = implode('_', $value);
} else {
$param[$key] = '';
}
}
// 处理明细表格类型字段数据
if (in_array($key, $detailTableField)) {
if (!empty($value)) {
$receivablesPlanData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $key;
} else {
$param[$key] = '';
}
}
}
4 years ago
if ($this->allowField(true)->save($param, ['plan_id' => $plan_id])) {
$data = [];
$data['plan_id'] = $plan_id;
3 years ago
// 添加回款计划扩展数据
db('crm_receivables_plan_data')->where('plan_id', $data['plan_id'])->delete();
array_walk($receivablesPlanData, function (&$val) use ($data) {
$val['plan_id'] = $data['plan_id'];
});
db('crm_receivables_plan_data')->insertAll($receivablesPlanData);
4 years ago
return $data;
} else {
$this->error = '编辑失败';
return false;
}
}
/**
* 回款计划数据
* @param $id 回款计划ID
* @return
*/
public function getDataById($id = '')
{
$map['plan_id'] = $id;
$dataInfo = $this->where($map)->find();
if (!$dataInfo) {
$this->error = '暂无此数据';
return false;
}
$userModel = new \app\admin\model\User();
$dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
$dataInfo['plan_id'] = $id;
return $dataInfo;
}
//模拟自定义字段返回
public function getField()
{
$field_arr = [
'0' => [
'field' => 'customer_id',
'name' => '客户名称',
'form_type' => 'customer',
'setting' => []
],
'1' => [
'field' => 'contract_id',
'name' => '合同名称',
'form_type' => 'contract',
'setting' => []
],
'2' => [
'field' => 'money',
'name' => '计划回款金额',
'form_type' => 'floatnumber',
'setting' => []
],
'3' => [
'field' => 'return_date',
'name' => '计划回款日期',
'form_type' => 'date',
'setting' => []
],
'4' => [
'field' => 'return_type',
'name' => '计划回款方式',
'form_type' => 'select',
'setting' => '支付宝\n微信\n转账'
],
'5' => [
'field' => 'remind',
'name' => '提前几日提醒',
'form_type' => 'number',
'setting' => []
],
'6' => [
'field' => 'remark',
'name' => '备注',
'form_type' => 'textarea',
'setting' => []
],
'7' => [
'field' => 'file',
'name' => '附件',
'form_type' => 'file',
'setting' => []
]
];
return $field_arr;
}
}