@ -29,8 +29,47 @@ class Field extends Model
private $queryStr = ''; //sql语句
private $__db_prefix; //数据库表前缀
private $types_arr = ['crm_leads', 'crm_customer', 'crm_contacts', 'crm_product', 'crm_business', 'crm_contract', 'oa_examine', 'hrm_parroll', 'admin_user', 'crm_receivables', 'crm_receivables_plan', 'crm_invoice', 'crm_visit']; //支持自定义字段的表,不包含表前缀
private $formtype_arr = ['text', 'textarea', 'mobile', 'email', 'number', 'floatnumber', 'radio', 'select', 'checkbox', 'date', 'datetime', 'address', 'user', 'file', 'structure'];
private $types_arr = [
'crm_leads',
'crm_customer',
'crm_contacts',
'crm_product',
'crm_business',
'crm_contract',
'oa_examine',
'hrm_parroll',
'admin_user',
'crm_receivables',
'crm_receivables_plan',
'crm_invoice',
'crm_visit',
]; //支持自定义字段的表,不包含表前缀
private $formtype_arr = [
'text',
'textarea',
'mobile',
'email',
'number',
'floatnumber',
'radio',
'select',
'checkbox',
'date',
'datetime',
'address',
'user',
'file',
'structure',
'boolean_value', # 布尔值
'percent', # 百分数
'website', # 网址
'position', # 地址
'location', # 地址
'handwriting_sign', # 手写签名
'date_interval', # 日期区间
'desc_text', # 描述类型
'detail_table', # 明细表格
];
protected $type = [
'form_value' => 'array',
];
@ -199,14 +238,39 @@ class Field extends Model
if ($param['types'] == 'crm_customer') {
$map['field'] = array('not in', ['deal_status']);
}
$list = Db::name('AdminField')->where($map)->order('order_id')->select();
$list = Db::name('AdminField')->where($map)->order('form_position', 'asc')->select();
$detailTableList = db('admin_field_extend')->field(['field', 'content'])->where('types', $types)->select();
$detailTableData = [];
foreach ($detailTableList as $key => $value) {
$detailTableData[$value['field']] = !empty($value['content']) ? json_decode($value['content']) : [];
}
foreach ($list as $k => $v) {
$list[$k]['setting'] = $v['setting'] ? explode(chr(10), $v['setting']) : [];
if ($v['form_type'] == 'checkbox') {
$list[$k]['default_value'] = $v['default_value'] ? explode(',', $v['default_value']) : array();
}
if (in_array($v['form_type'], ['position', 'date_interval'])) {
$list[$k]['default_value'] = !empty($v['default_value']) ? json_decode($v['default_value'], true) : [];
}
if ($v['form_type'] == 'detail_table') {
$list[$k]['fieldExtendList'] = !empty($detailTableData[$v['field']]) ? $detailTableData[$v['field']] : [];
}
if (!empty($v['form_position'])) {
$coordinate = explode(',', $v['form_position']);
$list[$k]['xaxis'] = (int)$coordinate[0];
$list[$k]['yaxis'] = (int)$coordinate[1];
}
if (!empty($v['options'])) {
$list[$k]['optionsData'] = json_decode($v['options'], true);
} else {
$list[$k]['options'] = $v['setting'];
}
// 处理数值范围字段
$list[$k]['minNumRestrict'] = $v['min_num_restrict'];
$list[$k]['maxNumRestrict'] = $v['max_num_restrict'];
}
return $list ?: [];
return getFieldGroupOrderData((array)$list);
}
/**
@ -238,9 +302,25 @@ class Field extends Model
# 用户自定义字段
$userFields = db('admin_user_field')->field(['id', 'datas'])->where('types', $types)->select();
# 获取最大formAssistId
$formAssistId = db('admin_field')->where('types', $types)->order('formAssistId', 'desc')->value('formAssistId');
$formAssistId = !empty($formAssistId) ? $formAssistId : 1000;
$error_message = [];
$i = 0;
foreach ($param as $k => $data) {
// 设置$formAssistId值
$formAssistId += 1;
$data['formAssistId'] = $formAssistId;
// 清除坐标
unset($data['xaxis']);
unset($data['yaxis']);
unset($data['maxNumRestrict']);
unset($data['minNumRestrict']);
// 设置明细表格类型的默认值为空, 防止为null的报错。
if ($data['form_type'] == 'detail_table') {
$data['default_value'] = '';
}
$i++;
$data['types'] = $types;
if ($types == 'oa_examine' & & !$data['types_id']) {
@ -251,17 +331,17 @@ class Field extends Model
$error_message[] = $data['name'] . ',字段类型错误';
}
//生成字段名
if (!$data['field']) $data['field'] = $this->createField($types);
if (!$data['field']) $data['field'] = $this->createField($types, $types == 'oa_examine' ? 'oa_' : 'crm_' );
$rule = [
'field' => ['regex' => '/^[a-z]([a-z]|_)+[a-z]$/i'],
'name' => 'require',
// 'name' => 'require',
'types' => 'require',
'form_type' => 'require',
];
$msg = [
'field.regex' => '字段名称格式不正确!',
'name.require' => '字段标识必须填写',
// 'name.require' => '字段标识必须填写',
'types.require' => '分类必须填写',
'form_type.require' => '字段类型必须填写',
];
@ -290,6 +370,30 @@ class Field extends Model
$data['form_value'] = $new_form_value;
}
# 处理日期区间、地址类型的默认数据
if (in_array($data['form_type'], ['position', 'date_interval']) & & !empty($data['default_value'])) {
$data['default_value'] = json_encode($data['default_value'], JSON_NUMERIC_CHECK);
}
# 处理明细表格中的字段数据
if ($data['form_type'] == 'detail_table' & & !empty($data['fieldExtendList']) & & $this->setDetailTableData($types, $data['field'], $data['fieldExtendList']) === false) {
$error_message[] = '创建明细表单失败!';
}
# 处理选项中的逻辑表单数据
// if (in_array($data['form_type'], ['select', 'checkbox'])) {
// $data['options'] = !empty($data['options']) ? json_encode($data['options']) : '';
// }
# 设置描述文字类型的字段名称
if (empty($data['name']) & & $data['form_type'] == 'desc_text') {
$data['name'] = '描述文字';
}
// 数值范围
if (!empty($data['minNumRestrict'])) $data['min_num_restrict'] = $data['minNumRestrict'];
if (!empty($data['maxNumRestrict'])) $data['max_num_restrict'] = $data['maxNumRestrict'];
unset($data['field_id']);
if ($i > 1) {
@ -299,12 +403,14 @@ class Field extends Model
}
# 处理公海字段数据
if ($types == 'crm_customer') {
foreach ($poolList AS $k1 => $poolId) {
foreach ($poolList as $k1 => $poolId) {
$poolData[] = [
'pool_id' => $poolId,
'name' => $data['name'],
'field_name' => $data['field'],
'form_type' => $data['form_type'],
'is_null' => $data['is_null'],
'is_unique' => $data['is_unique'],
'is_hidden' => 1
];
}
@ -331,9 +437,7 @@ class Field extends Model
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
break;
case 'number' :
$defaultvalue = abs(intval($data['default_value'])) > 2147483647 ? 2147483647 : intval($data['default_value']);
$maxlength = 11;
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` int ( " . $maxlength . " ) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'floatnumber' :
$defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
@ -353,19 +457,63 @@ class Field extends Model
case 'form' :
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "'";
break;
case 'boolean_value' :
# 布尔值类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TINYINT(1) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'percent' :
# 百分数类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'position' :
# 地址类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$addressValue = [];
if (!empty($data['default_value'])) {
$data['default_value'] = json_decode($data['default_value'], true);
foreach ($data['default_value'] as $kk => $vv) {
if (!empty($vv['name'])) $addressValue[] = $vv['name'];
}
}
$defaultValue = !empty($addressValue) ? "DEFAULT '" . implode(',', $addressValue) . "'" : "DEFAULT NULL";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
break;
case 'location' :
# 定位类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'handwriting_sign' :
# 手写签名类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` INT(10) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'date_interval' :
# 日期区间类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$defaultValue = !empty($data['default_value']) ? "DEFAULT '" . implode('_', json_decode($data['default_value'], true)) . "'" : "DEFAULT NULL";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
break;
case 'desc_text' :
# 描述文字类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(1000) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'detail_table' :
# 明细表格类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
default :
$maxlength = 255;
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
}
if (!empty($this->queryStr)) {
$resData = Db::execute($this->queryStr);
if ($resData === false) {
$this->where(['field_id' => $this->field_id])->delete();
$error_message[] = $data['name'] . ',添加失败';
}
}
# 处理用户自定义字段数据
if ($resData !== false & & !empty($userFields)) {
foreach ($userFields AS $key => $value) {
if (!empty($userFields)) {
foreach ($userFields as $key => $value) {
if (in_array($data['form_type'], ['handwriting_sign', 'desc_text', 'detail_table'])) continue;
$userFields[$key]['datas'] = json_decode($value['datas'], true);
$userFields[$key]['datas'][$data['field']] = ['width' => '', 'is_hide' => 0];
$userFields[$key]['datas'] = json_encode($userFields[$key]['datas']);
@ -379,7 +527,7 @@ class Field extends Model
}
# 更新用户自定义字段
if (!empty($userFields)) {
foreach ($userFields AS $key => $value) {
foreach ($userFields as $key => $value) {
db('admin_user_field')->where('id', $value['id'])->update(['datas' => $value['datas']]);
}
}
@ -438,16 +586,34 @@ class Field extends Model
$this->error = '参数错误';
return false;
}
# 查询老数据
// 查询老数据
$oldData = [];
if (!empty($types) & & $types == 'crm_customer') {
$oldList = db('admin_field')->field(['field', 'name'])->where('types', $types)->select();
foreach ($oldList AS $key => $value) {
foreach ($oldList as $key => $value) {
$oldData[$value['field']] = $value['name'];
}
}
// 获取最大formAssistId
$formAssistId = db('admin_field')->where('types', $types)->order('formAssistId', 'desc')->value('formAssistId');
$formAssistId = !empty($formAssistId) ? $formAssistId : 1000;
$i = 0;
foreach ($param as $data) {
// 设置formAssistId
if (empty($data['formAssistId'])) {
$formAssistId += 1;
$data['formAssistId'] = $formAssistId;
}
// 清除坐标
unset($data['xaxis']);
unset($data['yaxis']);
unset($data['maxNumRestrict']);
unset($data['minNumRestrict']);
// 设置明细表格类型的默认值为空, 防止为null的报错。
if ($data['form_type'] == 'detail_table') {
$data['default_value'] = '';
}
$i++;
$field_id = intval($data['field_id']);
if (!$field_id) {
@ -472,10 +638,33 @@ class Field extends Model
// unset($data['field']);
$data['field'] = $dataInfo['field'];
unset($data['operating']);
$box_form_type = array('checkbox', 'select', 'radio');
if ((in_array($dataInfo['form_type'], $box_form_type) & & !in_array($data['form_type'], $box_form_type)) || !in_array($dataInfo['form_type'], $box_form_type)) {
unset($data['form_type']);
// $box_form_type = array('checkbox', 'select', 'radio');
// if ((in_array($dataInfo['form_type'], $box_form_type) & & !in_array($data['form_type'], $box_form_type)) || !in_array($dataInfo['form_type'], $box_form_type)) {
// unset($data['form_type']);
// }
# 处理日期区间、地址类型的默认数据
if (in_array($data['form_type'], ['position', 'date_interval']) & & !empty($data['default_value'])) {
$data['default_value'] = json_encode($data['default_value'], JSON_NUMERIC_CHECK);
}
# 处理明细表格中的字段数据
if ($data['form_type'] == 'detail_table' & & !empty($data['fieldExtendList']) & & $this->setDetailTableData($data['types'], $data['field'], $data['fieldExtendList']) === false) {
$error_message[] = '创建明细表单失败!';
}
unset($data['fieldExtendList']);
# 处理选项中的逻辑表单数据
// if (in_array($data['form_type'], ['select', 'checkbox'])) {
// $data['options'] = json_encode($data['options'], JSON_NUMERIC_CHECK);
// }
// 数值范围
if (!empty($data['minNumRestrict'])) $data['min_num_restrict'] = $data['minNumRestrict'];
if (!empty($data['maxNumRestrict'])) $data['max_num_restrict'] = $data['maxNumRestrict'];
if (isset($data['minNumRestrict'])) unset($data['minNumRestrict']);
if (isset($data['maxNumRestrict'])) unset($data['maxNumRestrict']);
// $resField = $this->allowField(true)->save($data, ['field_id' => $field_id]);
unset($data['showSetting']);
unset($data['componentName']);
@ -486,7 +675,7 @@ class Field extends Model
if ($resField) {
# 更新公海字段
if (!empty($oldData[$data['field']]) & & $data['name'] != $oldData[$data['field']]['name']) {
db('crm_customer_pool_field_setting')->where('field_name', $data['field'])->update(['name' => $data['name']]);
db('crm_customer_pool_field_setting')->where('field_name', $data['field'])->update(['name' => $data['name'], 'is_null' => $data['is_null'], 'is_unique' => $data['is_unique'] ]);
}
//actionLog($field_id); //操作日志
$this->tableName = $dataInfo['types'];
@ -508,9 +697,7 @@ class Field extends Model
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
break;
case 'number' :
$defaultvalue = abs(intval($data['default_value'])) > 2147483647 ? 2147483647 : intval($data['default_value']);
$maxlength = 11;
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` int ( " . $maxlength . " ) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'floatnumber' :
$defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
@ -527,15 +714,58 @@ class Field extends Model
case 'file' :
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR ( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "' ";
break;
case 'boolean_value' :
# 布尔值类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` TINYINT(1) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'percent' :
# 百分数类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'position' :
# 地址类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$addressValue = [];
if (!empty($data['default_value'])) {
$data['default_value'] = json_decode($data['default_value'], true);
foreach ($data['default_value'] as $kk => $vv) {
if (!empty($vv['name'])) $addressValue[] = $vv['name'];
}
}
$defaultValue = !empty($addressValue) ? "DEFAULT '" . implode(',', $addressValue) . "'" : "DEFAULT NULL";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
break;
case 'location' :
# 定位类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'handwriting_sign' :
# 手写签名类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` INT(10) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'date_interval' :
# 日期区间类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$defaultValue = !empty($data['default_value']) ? "DEFAULT '" . implode('_', json_decode($data['default_value'], true)) . "'" : "DEFAULT NULL";
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
break;
case 'desc_text' :
# 描述文字类型字段
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(1000) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
case 'detail_table' :
# 明细表格类型字段, 存放在相应的数据扩展表中, 比如crm_customer_extra_data
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
default :
$maxlength = 255;
$this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
break;
}
if (!empty($this->queryStr)) {
$resData = Db::execute($this->queryStr);
if ($resData === false) {
$error_message[] = $data['name'] . ',修改失败';
}
}
} else {
$error_message[] = $data['name'] . ',修改失败';
}
@ -573,6 +803,7 @@ class Field extends Model
$delMessage[] = $dataInfo['name'] . ',系统字段,不能删除';
} else {
$resDel = $this->where(['field_id' => $id])->delete(); //删除自定义字段信息
// 客户模块下的栏目,成功删除自定义字段后做相应的处理
if ($resDel & & $dataInfo['types'] !== 'oa_examine') {
$this->tableName = $dataInfo['types'];
if ($dataInfo['form_type'] == 'img') {
@ -589,7 +820,6 @@ class Field extends Model
//删除列表字段配置数据
$userFieldList = db('admin_user_field')->where(['types' => $dataInfo['types']])->select();
foreach ($userFieldList as $key => $val) {
$datas = [];
$datas = json_decode($val['datas'], true);
if ($datas) {
foreach ($datas as $k => $v) {
@ -605,7 +835,6 @@ class Field extends Model
//删除场景字段数据
$sceneFieldList = db('admin_scene')->where(['types' => $dataInfo['types']])->select();
foreach ($sceneFieldList as $key => $val) {
$data = [];
$data = json_decode($val['data'], true);
if ($data) {
foreach ($data as $k => $v) {
@ -613,12 +842,26 @@ class Field extends Model
}
$data = $data ?: [];
$sceneModel = new \app\admin\model\Scene();
$resScene = $ sceneModel->updateData($data, $val['scene_id']);
$sceneModel->updateData($data, $val['scene_id']);
}
}
# 处理删除公海字段的条件
// 处理删除公海字段的条件
if (!empty($types) & & $types == 'crm_customer' & & !empty($dataInfo['field'])) $poolWhere[] = $dataInfo['field'];
} else {
}
// 删除字段成功后做相应处理
if ($resDel) {
// 删除明细表格字段数据
if (!empty($dataInfo['form_type']) & & $dataInfo['form_type'] == 'detail_table') {
db('admin_field_extend')->where(['types' => $dataInfo['types'], 'field' => $dataInfo['field']])->delete();
}
// 删除相应模块扩展数据: crm_leads_data、crm_customer_data、oa_examine ...
if (!empty($types)) db($types . '_data')->where('field', $dataInfo['field'])->delete();
}
// 删除失败
if (!$resDel) {
$delMessage[] = $dataInfo['name'] . ',删除失败';
}
}
@ -701,7 +944,7 @@ class Field extends Model
$types = 'crm_customer_pool';
}
if ($param['action'] == 'excel') {
$map['form_type'] = array('not in', ['file', 'form','deal_status']);//删除了过滤structure user字段类型数据 添加deal_status
$map['form_type'] = array('not in', ['file', 'form','user','structure', 'checkbox' ,' deal_status','position','location','handwriting_sign','date_interval','detail_table','desc_text','boolean_value ']);//删除了过滤structure user字段类型数据 添加deal_status
} elseif ($param['action'] == 'index') {
$map['form_type'] = array('not in', ['file', 'form']);
}
@ -725,55 +968,45 @@ class Field extends Model
$field_list[$k]['writeStatus'] = $status['write'];
}
}
# 客户模块增加锁定状态、距进入公海天数字段
if (!$userLevel & & $param['types'] == 'crm_customer' & & !empty($grantData[$param['types']])) {
$poolDayStatus = getFieldGrantStatus('pool_day', $grantData[$param['types']]);
if (!empty($poolDayStatus['read'])) {
if ($param['types'] == 'crm_invoice') {
$field_list[] = [
'field' => 'pool_day ',
'name' => '距进入公海天数 ',
'field' => 'check_status',
'name' => '审核状态',
'form_type' => 'text',
'writeStatus' => 0,
'fieldName' => 'pool_day '
'fieldName' => 'check_status '
];
}
$isLockStatus = getFieldGrantStatus('is_lock', $grantData[$param['types']]);
if (!empty($isLockStatus['read'])) {
$field_list[] = [
'field' => "is_lock" ,
'fieldName' => "is_lock" ,
'form_type' => "text" ,
'name' => "锁定状态" ,
'writeStatus' => 0
'field' => 'invoice_number',
'name' => '发票号码',
'form_type' => 'text',
'writeStatus' => 0 ,
'fieldName' => 'invoice_number'
];
}
}
if ($userLevel) {
$field_list[] = [
'field' => 'pool_day ',
'name' => '距进入公海天数 ',
'form_type' => 'text ',
'field' => 'real_invoice_date',
'name' => '实际开票日期',
'form_type' => 'date',
'writeStatus' => 0,
'fieldName' => 'pool_day '
'fieldName' => 'real_invoice_date '
];
$field_list[] = [
'field' => "is_lock" ,
'fieldName' => "is_lock" ,
'form_type' => "text" ,
'name' => "锁定状态" ,
'writeStatus' => 0
'field' => 'logistics_number' ,
'name' => '物流单号' ,
'form_type' => 'text' ,
'writeStatus' => 0 ,
'fieldName' => 'logistics_number'
];
}
} else {
$field_list = db('admin_field')->where($map)->where( 'is_hidden',0)->field('field,types,name,form_type,default_value,is_unique,is_null,input_tips,setting,is_hidden')->order($order)->select();
$fields = 'field_id,field,types,name,form_type,default_value,is_unique,is_null,input_tips,setting,is_hidden,form_position,precisions,options,style_percent,formAssistId';
$field_list = db('admin_field')->field($fields)->where($map)->where('is_hidden', 0)->order($order)->select();
// 获取X坐标值
$x = $this->getFormPositionXValue($types, $types_id);
# 详情页面增加负责人字段
if ($param['action'] == 'read' & & !in_array($param['types'], ['crm_visit', 'crm_product','oa_examine'])) {
if ($param['action'] == 'read' & & !in_array($param['types'], ['crm_visit', 'crm_product', 'oa_examine', 'crm_invoic e'])) {
$field_list[] = [
'field' => 'owner_user_id',
'name' => '负责人',
@ -795,11 +1028,33 @@ class Field extends Model
'is_null' => 0,
'input_tips' => '',
'setting' => [],
'value' => []
'value' => [],
'style_percent' => 100,
'xaxis' => $x,
'yaxis' => 0,
'form_position' => $x . ',0'
];
}
// 商机下产品
if (in_array($param['types'], ['crm_business'])) {
$new_field_list[] = [
'field' => 'product',
'name' => '产品',
'form_type' => 'product',
'default_value' => '',
'is_unique' => 0,
'is_null' => 0,
'input_tips' => '',
'setting' => [],
'value' => [],
'style_percent' => 100,
'xaxis' => $x,
'yaxis' => 0,
'form_position' => $x . ',0'
];
}
//商机、合同下产品
if (in_array($param['types'], ['crm_business', 'crm_contract'])) {
// 合同下产品
if (in_array($param['types'], ['crm_contract'])) {
$new_field_list[] = [
'field' => 'product',
'name' => '产品',
@ -809,7 +1064,11 @@ class Field extends Model
'is_null' => 0,
'input_tips' => '',
'setting' => [],
'value' => []
'value' => [],
'style_percent' => 100,
'xaxis' => $x,
'yaxis' => 0,
'form_position' => $x . ',0'
];
}
# 产品基本信息增加负责人信息
@ -838,6 +1097,11 @@ class Field extends Model
$field_list[$k]['writeStatus'] = 0;
}
// 删除描述文字的name名称
if ($v['form_type'] == 'desc_text') {
$field_list[$k]['name'] = '';
}
//处理setting内容
$setting = [];
$default_value = $v['default_value'];
@ -918,6 +1182,11 @@ class Field extends Model
$value = $dataInfo[$v['field']] ? db('crm_contacts')->where(['contacts_id' => $dataInfo[$v['field']]])->field('contacts_id,name')->select() : [];
} elseif ($v['form_type'] == 'contract') {
$value = $dataInfo[$v['field']] ? db('crm_contract')->where(['contract_id' => $dataInfo[$v['field']]])->field('contract_id,num')->select() : [];
} elseif ($v['form_type'] == 'floatnumber' & & $v['field'] == 'contract_money' & & $types == 'crm_invoice') {
$contractMoney = db('crm_invoice')->alias('invoice')
->join('__CRM_CONTRACT__ contract', 'invoice.contract_id = contract.contract_id', 'LEFT')
->where('invoice.invoice_id', $param['action_id'])->value('contract.money');
$value = $contractMoney;
} elseif ($v['form_type'] == 'category') {
//产品类别
if ($param['action'] == 'read') {
@ -998,17 +1267,68 @@ class Field extends Model
$value = isset($dataInfo[$v['field']]) ? stringToArray($dataInfo[$v['field']]) : [];
} elseif ($v['form_type'] == 'date') {
$value = ($dataInfo[$v['field']] & & $dataInfo[$v['field']] !== '0000-00-00') ? $dataInfo[$v['field']] : '';
} elseif ($v['form_type'] == 'boolean_value') {
// 布尔类型
$value = !empty($dataInfo[$v['field']]) ? (string)$dataInfo[$v['field']] : '0';
} elseif ($v['form_type'] == 'percent') {
// 百分数
$value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
} elseif ($v['form_type'] == 'website') {
// 网址
$value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
} elseif ($v['form_type'] == 'handwriting_sign') {
// 手写签名
$fileData = !empty($dataInfo[$v['field']]) ? db('admin_file')->where('file_id', $dataInfo[$v['field']])->find() : '';
if (!empty($fileData['file_path'])) $fileData['file_path'] = getFullPath($fileData['file_path']);
if (!empty($fileData['file_path_thumb'])) $fileData['file_path_thumb'] = getFullPath($fileData['file_path_thumb']);
$value = !empty($fileData) ? ['file_id' => $fileData['file_id'], 'url' => $fileData['file_path']] : "";
} elseif ($v['form_type'] == 'desc_text') {
// 描述文字
$value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : $v['default_value'];
} elseif (in_array($v['form_type'], ['position', 'location', 'date_interval', 'detail_table'])) {
// 地址、定位、日期区间、明细表格
$primaryKey = getPrimaryKeyName($param['types']);
$positionJson = !empty($dataInfo[$primaryKey]) ? db($param['types'] . '_data')->where([$primaryKey => $dataInfo[$primaryKey], 'field' => $v['field']])->value($param['types'] == 'oa_examine' ? 'value' : 'content') : '';
$positionData = !empty($positionJson) ? json_decode($positionJson, true) : '';
$value = $positionData;
if ($v['form_type'] == 'detail_table') {
$content = db('admin_field_extend')->where(['types' => $types, 'field' => $v['field']])->value('content');
$field_list[$k]['fieldExtendList'] = json_decode($content, true);
}
} else {
$value = isset($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
}
$field_list[$k]['setting'] = $setting;
$field_list[$k]['default_value'] = $default_value;
$field_list[$k]['value'] = $value;
$field_list[$k]['options'] = !empty($field_list[$k]['options']) ? $field_list[$k]['options'] : '';
$field_list[$k]['optionsData'] = !empty($field_list[$k]['options']) ? json_decode($field_list[$k]['options'], true) : '';
}
}
return array_values($field_list) ?: [];
}
private function getFormPositionXValue($types, $typesId)
{
$positionArray = db('admin_field')->where(['types' => $types, 'types_id' => $typesId, 'form_position' => [['neq', ''], ['not null'], 'AND']])->column('form_position');
if (!empty($positionArray)) {
$positionString = implode('-', $positionArray);
$positionString = str_replace(',0', '', $positionString);
$positionString = str_replace(',1', '', $positionString);
$positionString = str_replace(',2', '', $positionString);
$positionString = str_replace(',3', '', $positionString);
$positionArray = explode('-', $positionString);
return max($positionArray) + 1;
}
return 0;
}
/**
* [fieldSearch 获取自定义字段高级筛选信息]
* @param $types 分类
@ -1027,7 +1347,7 @@ class Field extends Model
$userModel = new \app\admin\model\User();
$user_id = $param['user_id'];
$map['types'] = ['in', ['', $types]];
$map['form_type'] = ['not in', ['file', 'form', 'business_status']];
$map['form_type'] = ['not in', ['file', 'form', 'business_status', 'detail_table', 'desc_text', 'handwriting_sign', 'date_interval' ]];
$map['is_hidden'] = 0;
$field_list = db('admin_field')
->where($map)
@ -1063,6 +1383,14 @@ class Field extends Model
'setting' => []
];
}
if (in_array($param['types'], ['crm_customer', 'crm_leads', 'crm_contacts', 'crm_business', 'crm_contract'])) {
$field_arr[] = [
'field' => 'last_time',
'name' => '最后跟进时间',
'form_type' => 'datetime',
'setting' => []
];
}
if ($field_arr) $field_list = array_merge($field_list, $field_arr);
foreach ($field_list as $k => $v) {
//处理setting内容
@ -1183,9 +1511,10 @@ class Field extends Model
/**
* [getIndexField 列表展示字段]
* @param types 分类
* @param excel 导出使用
* @author Michael_xu
*/
public function getIndexFieldConfig($types, $user_id, $types_id = '')
public function getIndexFieldConfig($types, $user_id, $types_id = '',$excel='' )
{
$userFieldModel = new \app\admin\model\UserField();
$userFieldData = $userFieldModel->getConfig($types, $user_id);
@ -1193,7 +1522,7 @@ class Field extends Model
$grantData = getFieldGrantData($user_id);
$userLevel = isSuperAdministrators($user_id);
$fieldList = $this->getFieldList($types, $types_id);
$fieldList = $this->getFieldList($types, $types_id,$excel );
$where = [];
if ($userFieldData) {
$fieldArr = [];
@ -1238,7 +1567,7 @@ class Field extends Model
* @author Ymob
* @datetime 2019-10-23 17:32:57
*/
public function getFieldList($types, $types_id = '')
public function getFieldList($types, $types_id = '', $excel = '' )
{
$newTypes = $types;
$unField = ['-1'];
@ -1246,17 +1575,27 @@ class Field extends Model
$newTypes = 'crm_customer';
$unField = ['owner_user_id'];
}
$fieldArr = $this
->where([
if($excel=='excel'){
$where=[
'types' => ['IN', ['', $newTypes]],
'form_type' => ['not in', ['file', 'form']],
'form_type' => ['not in', ['file', 'form', 'deal_status','position','location','handwriting_sign','date_interval','detail_table','desc_text','boolean_value ']],
'field' => ['not in', $unField],
'types_id' => ['eq', $types_id],
'is_hidden' => 0
])
->field(['field', 'name', 'form_type,is_hidden'])
->order('order_id asc')
];
}else{
$where=[
'types' => ['IN', ['', $newTypes]],
'form_type' => ['not in', ['file', 'form', 'desc_text', 'detail_table']],
'field' => ['not in', $unField],
'types_id' => ['eq', $types_id],
'is_hidden' => 0
];
}
$fieldArr = $this
->where($where)
->field(['field', 'name', 'form_type', 'is_hidden'])
->order('field_id', 'asc')
->select();
$res = [];
@ -1274,6 +1613,21 @@ class Field extends Model
}
}
if ($types == 'crm_customer') {
$res[] = [
'field' => 'pool_day',
'name' => '距进入公海天数',
'form_type' => 'text',
'is_hidden' => 0
];
$res[] = [
'field' => 'is_lock',
'name' => '锁定状态',
'form_type' => 'text',
'is_hidden' => 0
];
}
return array_column($res, null, 'field');
}
@ -1360,6 +1714,16 @@ class Field extends Model
}
$sysField = ['visit.visit_id', 'visit.owner_user_id', 'visit.status', 'visit.customer_id', 'visit.contract_id', 'visit.create_time', 'visit.update_time', 'visit.create_user_id', 'visit.visit_time', 'visit.contacts_id'];
break;
case 'crm_invoice' :
$newList = [];
foreach ($dataList as $k => $v) {
$newList[] = 'invoice.' . $v;
if ($v == 'contract_money') {
unset($newList[$k]);
}
}
$sysField = ['invoice.invoice_id', 'invoice.owner_user_id', 'invoice.invoice_status', 'invoice.invoice_number', 'invoice.invoice_type', 'invoice.check_status', 'invoice.customer_id', 'invoice.contract_id', 'invoice.create_user_id', 'invoice.flow_id', 'invoice.real_invoice_date', 'invoice.logistics_number'];
break;
}
$listArr = $sysField ? array_unique(array_merge($newList, $sysField)) : $dataList;
} else {
@ -1418,10 +1782,6 @@ class Field extends Model
case 'admin_user' :
$field_arr = \app\hrm\model\Userdet::getField();
break;
case 'crm_invoice':
$field_arr = $this->getInvoiceSearch();
break;
default :
$data = [];
$data['types'] = $types;
@ -1430,22 +1790,28 @@ class Field extends Model
$field_arr = $this->fieldSearch($data);
}
if ($types == 'crm_visit') {
foreach ($field_arr AS $key => $value) {
foreach ($field_arr as $key => $value) {
if ($value['name'] == '负责人') unset($field_arr[(int)$key]);
}
}
if ($types == 'crm_invoice') {
$field_arr = array_merge($field_arr, $this->getInvoiceSearch());
}
return $field_arr;
}
/**
* 自定义字段验重
* @param $field 字段名, $val 值, $id 排除当前数据验重, $types 需要查询的模块名
* @return
* @author
*
* @param $field string 字段名称
* @param $val array|string 字段值
* @param $id int 数据id
* @param $types string 模块类型
* @return bool
*/
public function getValidate($field, $val, $id, $types)
{
$val = trim($val);
$val = is_array($val) ? $val : trim($val);
if (!$val) {
$this->error = '验证内容不能为空';
return false;
@ -1465,6 +1831,23 @@ class Field extends Model
return false;
}
// 人员、部门、文件、手写签名、描述文字、多选、明细表格跳过验证唯一性
if (in_array($field_info['form_type'], ['user', 'structure', 'file', 'handwriting_sign', 'desc_text', 'checkbox', 'detail_table'])) return true;
// 前端传来的定位数据有问题,在提交数据时做验证
if ($field_info['form_type'] == 'location') return true;
// 处理日期区间类型数据
if ($field_info['form_type'] == 'date_interval') {
$val = implode('_', $val);
}
// 处理地址类型数据
if ($field_info['form_type'] == 'position') {
$positionNames = array_column($val, 'name');
$val = implode(',', $positionNames);
}
$dataModel = '';
switch ($types) {
case 'crm_leads' :
@ -1488,14 +1871,19 @@ class Field extends Model
case 'crm_receivables' :
$dataModel = new \app\crm\model\Receivables();
break;
case 'crm_invoice' :
$dataModel = new \app\crm\model\Invoice();
break;
case 'oa_examine' :
$dataModel = db('oa_examine');
break;
}
$where = [];
$where[$field] = ['eq', $val];
if ($id) {
//为编辑时的验重
$where[$dataModel->getpk()] = ['neq', $id];
}
// 编辑时的验重
if ($id) $where[$dataModel->getpk()] = ['neq', $id];
if ($types == 'crm_product') {
$where['delete_user_id'] = 0;
}
@ -1566,7 +1954,7 @@ class Field extends Model
$val = db('crm_customer')->where(['customer_id' => $val])->value('name');
break;
case 'contract' :
$val = db('crm_contract')->where(['customer _id' => $val])->value('num');
$val = db('crm_contract')->where(['contract _id' => $val])->value('num');
break;
case 'business' :
$val = db('crm_business')->where(['business_id' => $val])->value('name');
@ -1683,16 +2071,16 @@ class Field extends Model
* @param string $types 模块类型
* @param string $action 行为
* @param array $data 字段数据
* @author fanqi
* @since 2021-04-06
* @return array
* @since 2021-04-06
* @author fanqi
*/
public function resetField($userId, $types, $action, $data)
{
$grantData = getFieldGrantData($userId);
$userLevel = isSuperAdministrators($userId);
foreach ($data AS $key => $value) {
foreach ($data as $key => $value) {
# 处理字段授权
if (!$userLevel & & !empty($grantData[$types])) {
$status = getFieldGrantStatus($value['field'], $grantData[$types]);
@ -1742,10 +2130,6 @@ class Field extends Model
$data[$key]['fieldName'] = $value['field'];
}
if (in_array($value['form_type'], ['user', 'structure']) & & !in_array($value['field'], ['create_user_id', 'owner_user_id'])) {
$data[$key]['fieldName'] = $value['field'] . '_name';
}
# 详情中不显示产品类别、回款期数
if ($action == 'read' & & in_array($value['field'], ['category_id'])) {
unset($data[(int)$key]);
@ -1766,7 +2150,7 @@ class Field extends Model
{
$poolFields = db('crm_customer_pool_field_setting')->field(['field_name AS field', 'form_type', 'name'])->where(['pool_id' => $poolId, 'is_hidden' => 0])->select();
foreach ($poolFields AS $key => $value) {
foreach ($poolFields as $key => $value) {
# 字段值
$poolFields[$key]['value'] = !empty($dataInfo[$value['field']]) ? $dataInfo[$value['field']] : '';
@ -1806,12 +2190,11 @@ class Field extends Model
private function getInvoiceSearch()
{
return [
['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
// ['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
['field' => 'real_invoice_date', 'form_type' => 'datetime', 'setting' => [], 'name' => '实际开票日期'],
['field' => 'logistics_number', 'form_type' => 'text', 'setting' => [], 'name' => '物流单号'],
['field' => 'invoice_status', 'form_type' => 'select', 'setting' => ['未开票', '已开票'], 'name' => '开票状态'],
['field' => 'check_status', 'form_type' => 'select', 'setting' => ['待审核', '审核中', '审核通过', '审核未通过', '撤回'], 'name' => '审核状态'],
['field' => 'owner_user_id', 'form_type' => 'user', 'setting' => [], 'name' => '负责人']
];
}
@ -1840,4 +2223,34 @@ class Field extends Model
return $parentIds;
}
/**
* 设置明细表格数据
*
* @param string $types 模块类型: crm_leads crm_customer ...
* @param string $field 字段名称
* @param array $data 明细表单中的字段数据
* @return bool
* @since 2021-04-27
* @author fanqi
*/
private function setDetailTableData($types, $field, $data)
{
$id = db('admin_field_extend')->where(['field' => $field, 'types' => $types])->value('id');
foreach ($data as $k1 => $v1) {
if (empty($v1['field'])) $data[$k1]['field'] = $this->createField($types, $types == 'oa_examine' ? 'oa_' : 'crm_');
$data[$k1]['default_value'] = !empty($v1['default_value']) ? $v1['default_value'] : '';
}
if (!empty($id)) {
return db('admin_field_extend')->where('id', $id)->update(['content' => json_encode($data, JSON_NUMERIC_CHECK)]);
}
if (empty($id)) {
return db('admin_field_extend')->insert(['types' => $types, 'field' => $field, 'content' => json_encode($data, JSON_NUMERIC_CHECK), 'create_time' => time()]);
}
return false;
}
}