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.

930 lines
39 KiB

4 years ago
<?php
/**
* 发票控制器
*
* @author qifan
* @date 2020-12-07
*/
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
use app\admin\model\Message;
use app\admin\model\User;
3 years ago
use app\admin\traits\FieldVerificationTrait;
4 years ago
use app\crm\logic\InvoiceLogic;
use app\crm\model\NumberSequence;
use app\crm\traits\AutoNumberTrait;
use think\Db;
use think\Hook;
use think\Request;
class Invoice extends ApiCommon
{
3 years ago
use AutoNumberTrait, FieldVerificationTrait;
4 years ago
/**
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
**/
public function _initialize()
{
$action = [
'permission' => [],
3 years ago
'allow' => ['check', 'revokecheck', 'count', 'read']
4 years ago
];
3 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
$a = strtolower($request->action());
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
}
3 years ago
4 years ago
/**
* 列表
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\exception\DbException
*/
public function index(InvoiceLogic $invoiceLogic)
{
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
3 years ago
$data = $invoiceLogic->index($param);
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 创建
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function save(InvoiceLogic $invoiceLogic)
{
3 years ago
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择客户!']);
if (empty($this->param['contract_id'])) return resultArray(['error' => '请选择合同!']);
if (empty($this->param['invoice_money'])) return resultArray(['error' => '请填写开票金额!']);
if (empty($this->param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
if (empty($this->param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
$param = $this->param;
$userId = $this->userInfo['id'];
4 years ago
# 审批是否停用
$examineStatus = $param['examineStatus'];
# 删除无用参数
unset($param['examineStatus']);
unset($param['customer_name']);
unset($param['contract_money']);
unset($param['contract_number']);
# 设置创建人负责人ID
$param['create_user_id'] = $userId;
3 years ago
$param['owner_user_id'] = $userId;
4 years ago
# 创建更新日期
3 years ago
$param['create_time'] = time();
$param['update_time'] = time();
4 years ago
# 自动设置发票编号
$numberInfo = [];
if (empty($param['invoice_apple_number'])) {
$numberInfo = $this->getAutoNumbers(4);
if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
$param['invoice_apple_number'] = $numberInfo['number'];
}
# 检查发票编号是否重复
if ($invoiceLogic->getInvoiceId(['invoice_apple_number' => $param['invoice_apple_number']])) {
return resultArray(['error' => '发票编号重复!']);
}
3 years ago
4 years ago
if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
$examineStepModel = new \app\admin\model\ExamineStep();
# 审核判断(是否有符合条件的审批流)
$examineFlowModel = new \app\admin\model\ExamineFlow();
if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
return resultArray(['error' => '暂无审批人,无法创建']);
}
# 添加审批相关信息
$examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
if (!$examineFlowData) {
return resultArray(['error' => '无可用审批流,请联系管理员']);
}
$param['flow_id'] = $examineFlowData['flow_id'];
# 获取审批人信息
if ($examineFlowData['config'] == 1) {
# 固定审批流
$nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
3 years ago
$next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
$check_user_id = $next_user_ids ?: [];
4 years ago
$param['order_id'] = 1;
} else {
# 授权审批流
3 years ago
$check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
4 years ago
}
if (!$check_user_id) {
return resultArray(['error' => '无可用审批人,请联系管理员']);
}
3 years ago
$param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
4 years ago
} else {
# 审批流停用,将状态改为审核通过
$param['check_status'] = 2;
}
3 years ago
// 数据验证
// $validateResult = $this->fieldDataValidate($param, 'crm_invoice', $userId);
// if (!empty($validateResult)) {
// $this->error = $validateResult;
// return false;
// }
// 发票扩展表数据
$invoiceData = [];
$fieldModel = new \app\admin\model\Field();
// 处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_invoice');
foreach ($arrFieldAtt as $k=>$v) {
$param[$v] = arrayToString($param[$v]);
}
// 处理日期date类型
$dateField = $fieldModel->getFieldByFormType('crm_invoice', 'date');
if (!empty($dateField)) {
foreach ($param AS $key => $value) {
if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
}
}
// 处理手写签名类型
$handwritingField = $fieldModel->getFieldByFormType('crm_invoice', 'handwriting_sign');
if (!empty($handwritingField)) {
foreach ($param AS $key => $value) {
if (in_array($key, $handwritingField)) {
$param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
}
}
}
// 处理地址、定位、日期区间、明细表格类型字段
$positionField = $fieldModel->getFieldByFormType('crm_invoice', 'position');
$locationField = $fieldModel->getFieldByFormType('crm_invoice', 'location');
$dateIntervalField = $fieldModel->getFieldByFormType('crm_invoice', 'date_interval');
$detailTableField = $fieldModel->getFieldByFormType('crm_invoice', 'detail_table');
foreach ($param AS $key => $value) {
// 处理地址类型字段数据
if (in_array($key, $positionField)) {
if (!empty($value)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $key;
} else {
$param[$key] = '';
}
}
}
4 years ago
if (!$invoice_id = $invoiceLogic->save($param)) {
return resultArray(['error' => '创建失败!']);
}
4 years ago
$send_user_id = stringToArray($param['check_user_id']);
(new Message())->send(
Message::INVOICE_TO_DO,
[
3 years ago
'title' => $param['invoice_apple_number'],
4 years ago
'action_id' => $invoice_id
],
$send_user_id
);
4 years ago
# 更新crm_number_sequence表中的last_date、create_time字段
if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
updateActionLog($param['create_user_id'], 'crm_invoice', $invoice_id, '', '', '创建了发票');
3 years ago
RecordActionLog($param['create_user_id'], 'crm_invoice', 'save', $param['invoice_apple_number'], '', '', '新增了发票' . $param['invoice_apple_number']);
4 years ago
# 创建待办事项的关联数据
$checkUserIds = db('crm_invoice')->where('invoice_id', $invoice_id)->value('check_user_id');
$checkUserIdArray = stringToArray($checkUserIds);
$dealtData = [];
3 years ago
foreach ($checkUserIdArray as $kk => $vv) {
4 years ago
$dealtData[] = [
3 years ago
'types' => 'crm_invoice',
4 years ago
'types_id' => $invoice_id,
3 years ago
'user_id' => $vv
4 years ago
];
}
if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
3 years ago
// 添加发票扩展数据
array_walk($invoiceData, function (&$val) use ($invoice_id) {
$val['invoice_id'] = $invoice_id;
});
db('crm_invoice_data')->insertAll($invoiceData);
4 years ago
return resultArray(['data' => '创建成功!']);
}
3 years ago
4 years ago
/**
* 详情
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function read(InvoiceLogic $invoiceLogic)
{
$invoiceId = $this->param['id'];
3 years ago
$isUpdate = !empty($this->param['is_update']) ? $this->param['is_update'] : 0;
$userInfo = $this->userInfo;
4 years ago
$data = $invoiceLogic->read($invoiceId, $isUpdate);
$readStatus = false;
3 years ago
4 years ago
# 角色权限
$authArray = db('admin_access')->alias('access')
->join('__ADMIN_GROUP__ group', 'group.id = access.group_id', 'left')
->where('access.user_id', $userInfo['id'])->column('group.rules');
3 years ago
4 years ago
# 详情权限ID
3 years ago
$invoiceAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'invoice', 'level' => 2])->value('id');
4 years ago
$invoiceReadAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'read', 'level' => 3, 'pid' => $invoiceAuthId])->value('id');
3 years ago
foreach ($authArray as $key => $value) {
4 years ago
if (!empty($value) && in_array($invoiceReadAuthId, stringToArray($value))) $readStatus = true;
}
3 years ago
4 years ago
if (!isSuperAdministrators($userInfo['id']) && $readStatus === false) {
4 years ago
$authData['dataAuth'] = (int)0;
4 years ago
return resultArray(['data' => $authData]);
}
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 编辑
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function update(InvoiceLogic $invoiceLogic)
{
$param = $this->param;
3 years ago
if (empty($param['invoice_id'])) return resultArray(['error' => '缺少发票ID']);
if (empty($param['invoice_type'])) return resultArray(['error' => '请选择开票类型!']);
if (empty($param['title_type'])) return resultArray(['error' => '请选择抬头类型!']);
3 years ago
$userId = $this->userInfo['id'];
3 years ago
$dataInfo = db('crm_invoice')->where('invoice_id',$param['invoice_id'])->find();
4 years ago
# 审批是否停用
$examineStatus = $param['examineStatus'];
# 删除无用参数
unset($param['examineStatus']);
unset($param['customer_name']);
unset($param['contract_money']);
unset($param['contract_number']);
# 设置负责人ID
$param['update_time'] = time();
3 years ago
4 years ago
# 已进行审批,不能编辑
// $dataInfo = $invoiceLogic->read($param['invoice_id'], '');
// if (!$dataInfo) {
// $this->error = '数据不存在或已删除';
// return false;
// }
3 years ago
4 years ago
$checkStatus = $invoiceLogic->getExamineStatus($param['invoice_id']);
if (!in_array($checkStatus, ['3', '4', '5', '6'])) return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
3 years ago
4 years ago
# 自动设置发票编号
$numberInfo = [];
if (empty($param['invoice_apple_number'])) {
$numberInfo = $this->getAutoNumbers(4);
if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
$param['invoice_apple_number'] = $numberInfo['number'];
}
3 years ago
4 years ago
# 检查发票编号是否重复
$invoiceWhere['invoice_apple_number'] = $param['invoice_apple_number'];
$invoiceWhere['invoice_id'] = ['neq', $this->param['invoice_id']];
if ($invoiceLogic->getInvoiceId($invoiceWhere)) return resultArray(['error' => '发票编号重复!']);
3 years ago
4 years ago
if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
//保存为草稿
$param['check_status'] = 5; //草稿(未提交)
3 years ago
$param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
4 years ago
} else {
# 将合同审批状态至为待审核,提交后重新进行审批
if (($examineStatus != false && $examineStatus != 'false') || $examineStatus == 1) {
# 审核判断(是否有符合条件的审批流)
$examineFlowModel = new \app\admin\model\ExamineFlow();
$examineStepModel = new \app\admin\model\ExamineStep();
if (!$examineFlowModel->checkExamine($userId, 'crm_invoice')) {
return resultArray(['error' => '暂无审批人,无法创建']);
}
# 添加审批相关信息
$examineFlowData = $examineFlowModel->getFlowByTypes($userId, 'crm_invoice');
if (!$examineFlowData) {
return resultArray(['error' => '无可用审批流,请联系管理员']);
}
$param['flow_id'] = $examineFlowData['flow_id'];
# 获取审批人信息
if ($examineFlowData['config'] == 1) {
# 固定审批流
$nextStepData = $examineStepModel->nextStepUser($userId, $examineFlowData['flow_id'], 'crm_invoice', 0, 0, 0);
3 years ago
$next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
$check_user_id = $next_user_ids ?: [];
4 years ago
$param['order_id'] = 1;
} else {
# 授权审批流
3 years ago
$check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
4 years ago
}
if ($param['is_draft']) {
//保存为草稿
$param['check_status'] = 5;
3 years ago
$param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
4 years ago
} else {
if (!$check_user_id) {
return resultArray(['error' => '无可用审批人,请联系管理员']);
}
3 years ago
$param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
4 years ago
$param['check_status'] = 0;
}
$param['flow_user_id'] = '';
}
}
3 years ago
// 数据验证
// $validateResult = $this->fieldDataValidate($param, 'crm_invoice', $userId);
// if (!empty($validateResult)) {
// $this->error = $validateResult;
// return false;
// }
// 发票扩展表数据
$invoiceData = [];
$fieldModel = new \app\admin\model\Field();
// 处理部门、员工、附件、多选类型字段
$arrFieldAtt = $fieldModel->getArrayField('crm_invoice');
foreach ($arrFieldAtt as $k=>$v) {
$param[$v] = arrayToString($param[$v]);
}
// 处理日期date类型
$dateField = $fieldModel->getFieldByFormType('crm_invoice', 'date');
if (!empty($dateField)) {
foreach ($param AS $key => $value) {
if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
}
}
// 处理手写签名类型
$handwritingField = $fieldModel->getFieldByFormType('crm_invoice', 'handwriting_sign');
if (!empty($handwritingField)) {
foreach ($param AS $key => $value) {
if (in_array($key, $handwritingField)) {
$param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
}
}
}
// 处理地址、定位、日期区间、明细表格类型字段
$positionField = $fieldModel->getFieldByFormType('crm_invoice', 'position');
$locationField = $fieldModel->getFieldByFormType('crm_invoice', 'location');
$dateIntervalField = $fieldModel->getFieldByFormType('crm_invoice', 'date_interval');
$detailTableField = $fieldModel->getFieldByFormType('crm_invoice', 'detail_table');
foreach ($param AS $key => $value) {
// 处理地址类型字段数据
if (in_array($key, $positionField)) {
if (!empty($value)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'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)) {
$invoiceData[] = [
'field' => $key,
'content' => json_encode($value, JSON_NUMERIC_CHECK),
'create_time' => time()
];
$param[$key] = $key;
} else {
$param[$key] = '';
}
}
}
4 years ago
if (!$invoiceLogic->update($param)) {
return resultArray(['error' => '编辑失败!']);
}
3 years ago
4 years ago
//将审批记录至为无效
$examineRecordModel = new \app\admin\model\ExamineRecord();
3 years ago
$examineRecordModel->setEnd(['types' => 'crm_invoice', 'types_id' => $param['invoice_id']]);
4 years ago
# 更新crm_number_sequence表中的last_date、create_time字段
if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
//修改记录
3 years ago
updateActionLog($param['user_id'], 'crm_invoice', $param['invoice_id'], $dataInfo, $param);
3 years ago
RecordActionLog($param['user_id'], 'crm_invoice', 'update', $dataInfo['invoice_apple_number'], $dataInfo, $param);
4 years ago
# 删除待办事项的关联数据
db('crm_dealt_relation')->where(['types' => ['eq', 'crm_invoice'], 'types_id' => ['eq', $param['invoice_id']]])->delete();
# 创建待办事项的关联数据
$checkUserIds = db('crm_invoice')->where('invoice_id', $param['invoice_id'])->value('check_user_id');
$checkUserIdArray = stringToArray($checkUserIds);
$dealtData = [];
3 years ago
foreach ($checkUserIdArray as $kk => $vv) {
4 years ago
$dealtData[] = [
3 years ago
'types' => 'crm_invoice',
4 years ago
'types_id' => $param['invoice_id'],
3 years ago
'user_id' => $vv
4 years ago
];
}
if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
4 years ago
3 years ago
// 添加客户扩展数据
$invoiceId = $param['invoice_id'];
db('crm_invoice_data')->where('invoice_id', $invoiceId)->delete();
array_walk($invoiceData, function (&$val) use ($invoiceId) {
$val['invoice_id'] = $invoiceId;
});
db('crm_invoice_data')->insertAll($invoiceData);
4 years ago
return resultArray(['data' => '编辑成功!']);
}
3 years ago
4 years ago
/**
* 删除
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function delete(InvoiceLogic $invoiceLogic)
{
4 years ago
$actionRecordModel = new \app\admin\model\ActionRecord();
$fileModel = new \app\admin\model\File();
3 years ago
$idArray = $this->param['id'];
4 years ago
$userinfo = $this->userInfo['id'];
3 years ago
4 years ago
if (!is_array($idArray)) return resultArray(['error' => '发票ID类型错误']);
3 years ago
4 years ago
$idString = implode(',', $idArray);
3 years ago
$status = true;
4 years ago
if (!isSuperAdministrators($userinfo['id'])) {
$list = $invoiceLogic->getExamineStatus($idString, true);
3 years ago
foreach ($list as $key => $value) {
if (!in_array($value['check_status'], [4, 5])) {
4 years ago
$status = false;
break;
}
}
}
3 years ago
4 years ago
if (!$status) return resultArray(['error' => '不能删除审批中或审批结束的发票信息!']);
3 years ago
$dataInfo = db('crm_invoice')->where('invoice_id', ['in', $idArray])->select();
4 years ago
if (!$invoiceLogic->delete($idArray)) return resultArray(['error' => '删除失败!']);
3 years ago
4 years ago
# 删除附件
$fileModel->delRFileByModule('crm_invoice', $idArray);
4 years ago
//删除关联操作记录
3 years ago
$actionRecordModel->delDataById(['types' => 'crm_invoice', 'action_id' => $idArray]);
3 years ago
$userInfo = $this->userInfo;
foreach ($dataInfo as $k => $v) {
RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['invoice_apple_number'], '', '', '删除了回款:' . $v['invoice_apple_number']);
}
4 years ago
return resultArray(['data' => '删除成功!']);
}
3 years ago
4 years ago
/**
* 转移(变更负责人)
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
*/
public function transfer(InvoiceLogic $invoiceLogic)
{
$ownerUserId = $this->param['owner_user_id'];
3 years ago
$invoiceIds = $this->param['invoice_id'];
3 years ago
$userModel = new \app\admin\model\User();
3 years ago
$userInfo = $this->userInfo;
if (empty($ownerUserId)) return resultArray(['error' => '请选择负责人!']);
if (empty($invoiceIds)) return resultArray(['error' => '请选择发票!']);
4 years ago
if (!is_array($invoiceIds)) return resultArray(['error' => '发票ID类型错误']);
3 years ago
4 years ago
if ($invoiceLogic->transfer($invoiceIds, $ownerUserId) === false) return resultArray(['error' => '操作失败!']);
3 years ago
$owner_user_info = $userModel->getUserById($ownerUserId);
3 years ago
foreach ($invoiceIds as $v) {
$invoice_info = db('crm_invoice')->where('invoice_id', $v)->find();
3 years ago
updateActionLog($userInfo['id'], 'crm_invoice', $v, '', '', '将发票转移给:' . $owner_user_info['realname']);
3 years ago
RecordActionLog($userInfo['id'], 'crm_invoice', 'transfer', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '转移给:' . $owner_user_info['realname']);
3 years ago
}
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 设置开票
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
*/
public function setInvoice(InvoiceLogic $invoiceLogic)
{
3 years ago
if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
4 years ago
// if (empty($this->param['invoice_number'])) return resultArray(['error' => '请填写发票号码!']);
// if (empty($this->param['logistics_number'])) return resultArray(['error' => '请填写物流单号!']);
// if (empty($this->param['real_invoice_date'])) return resultArray(['error' => '请选择开票日期!']);
3 years ago
4 years ago
$this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
3 years ago
4 years ago
# 开票状态
$this->param['invoice_status'] = 1;
3 years ago
4 years ago
# 设置开票信息
if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
3 years ago
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 审核
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function check(InvoiceLogic $invoiceLogic)
{
3 years ago
$param = $this->param;
$user_id = $this->userInfo['id'];
$examineStepModel = new \app\admin\model\ExamineStep();
4 years ago
$examineRecordModel = new \app\admin\model\ExamineRecord();
3 years ago
$examineFlowModel = new \app\admin\model\ExamineFlow();
4 years ago
$invoiceData = [];
3 years ago
$invoiceData['invoice_id'] = $param['id'];
$invoiceData['update_time'] = time();
4 years ago
$invoiceData['check_status'] = 1;
# 权限判断
if (!$examineStepModel->checkExamine($user_id, 'crm_invoice', $param['id'])) {
return resultArray(['error' => $examineStepModel->getError()]);
}
3 years ago
4 years ago
# 审批主体详情
$dataInfo = $invoiceLogic->getExamineInfo($param['id']);
$flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
3 years ago
4 years ago
# 1审批结束
$is_end = 0;
3 years ago
4 years ago
# 1通过0驳回
$status = !empty($param['status']) && $param['status'] == 1 ? 1 : 0;
3 years ago
4 years ago
# 审批记录
3 years ago
$checkData = [];
4 years ago
$checkData['check_user_id'] = $user_id;
3 years ago
$checkData['types'] = 'crm_invoice';
$checkData['types_id'] = $param['id'];
$checkData['check_time'] = time();
$checkData['content'] = $param['content'];
$checkData['flow_id'] = $dataInfo['flow_id'];
$checkData['order_id'] = $dataInfo['order_id'] ?: 1;
$checkData['status'] = $status;
4 years ago
if ($status == 1) {
if ($flowInfo['config'] == 1) {
# 固定流程
# 获取下一审批信息
$nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_invoice', $param['id'], $dataInfo['order_id'], $user_id);
3 years ago
$next_user_ids = $nextStepData['next_user_ids'] ?: [];
$invoiceData['order_id'] = $nextStepData['order_id'] ?: '';
4 years ago
if (!$next_user_ids) {
$is_end = 1;
# 审批结束
3 years ago
$checkData['check_status'] = !empty($status) ? 2 : 3;
4 years ago
$invoiceData['check_user_id'] = '';
} else {
# 修改主体相关审批信息
$invoiceData['check_user_id'] = arrayToString($next_user_ids);
}
} else {
# 自选流程
3 years ago
$is_end = $param['is_end'] ? 1 : '';
$check_user_id = $param['check_user_id'] ?: '';
4 years ago
if ($is_end !== 1 && empty($check_user_id)) {
return resultArray(['error' => '请选择下一审批人']);
}
$invoiceData['check_user_id'] = arrayToString($param['check_user_id']);
}
if ($is_end == 1) {
3 years ago
$checkData['check_status'] = !empty($status) ? 2 : 3;
4 years ago
$invoiceData['check_user_id'] = '';
3 years ago
$invoiceData['check_status'] = 2;
4 years ago
}
} else {
# 审批驳回
$is_end = 1;
$invoiceData['check_status'] = 3;
}
# 已审批人ID
3 years ago
$invoiceData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']), [$user_id])) : arrayToString([$user_id]);
4 years ago
$resContract = $invoiceLogic->setExamineInfo($invoiceData);
if ($resContract) {
# 审批记录
$examineRecordModel->createData($checkData);
3 years ago
4 years ago
# 发送站内信
if ($is_end == 1 && !empty($status)) {
# 审批流程结束,将审批通过消息告知负责人
(new Message())->send(
Message::INVOICE_PASS,
[
3 years ago
'title' => $dataInfo['invoice_apple_number'],
4 years ago
'action_id' => $param['id']
],
4 years ago
stringToArray($dataInfo['owner_user_id'])
4 years ago
);
} else {
if (!empty($status)) {
# 审批流程未结束,将待审批提醒发送给下一级负责人
(new Message())->send(
Message::INVOICE_TO_DO,
[
'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
3 years ago
'title' => $dataInfo['invoice_apple_number'],
4 years ago
'action_id' => $param['id']
],
stringToArray($invoiceData['check_user_id'])
);
} else {
# 将审批被驳回的消息告知负责人
(new Message())->send(
Message::INVOICE_REJECT,
[
3 years ago
'title' => $dataInfo['invoice_apple_number'],
4 years ago
'action_id' => $param['id']
],
4 years ago
stringToArray($dataInfo['owner_user_id'])
4 years ago
);
}
}
3 years ago
4 years ago
return resultArray(['data' => '审批成功']);
} else {
return resultArray(['error' => '审批失败,请重试!']);
}
}
3 years ago
4 years ago
/**
* 撤销审核
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function revokeCheck(InvoiceLogic $invoiceLogic)
{
$invoiceId = $this->param['id'];
3 years ago
$content = $this->param['content'];
$realname = $this->userInfo['realname'];
$userInfo = $this->userInfo;
$user_id = $userInfo['id'];
4 years ago
if (empty($invoiceId)) return resultArray(['error' => '请选择要撤回审核的发票!']);
3 years ago
4 years ago
$examineInfo = $invoiceLogic->getExamineInfo($invoiceId);
3 years ago
4 years ago
if ($examineInfo['check_status'] == 2) {
return resultArray(['error' => '已审批结束,不能撤销']);
}
if ($examineInfo['check_status'] == 4) {
return resultArray(['error' => '无需撤销']);
}
$userModel = new \app\admin\model\User();
$admin_user_ids = $userModel->getAdminId();
if ($examineInfo['owner_user_id'] !== $user_id && !in_array($user_id, $admin_user_ids)) {
return resultArray(['error' => '没有权限']);
}
3 years ago
4 years ago
# 修改发票审核状态
if (!$invoiceLogic->update(['invoice_id' => $invoiceId, 'check_status' => 4, 'check_user_id' => '', 'flow_user_id' => ''])) {
return resultArray(['error' => '操作失败!']);
}
3 years ago
4 years ago
# 添加撤销审核的记录
$invoiceLogic->createExamineRecord($invoiceId, $examineInfo, $realname, $content, $user_id);
3 years ago
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* table栏数量统计
*
* @return \think\response\Json
* @throws \think\Exception
*/
public function count()
{
if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
3 years ago
4 years ago
# 附件
$fileCount = Db::name('crm_invoice_file')->alias('invoice')->join('__ADMIN_FILE__ file', 'file.file_id = invoice.file_id', 'LEFT')->where('invoice_id', $this->param['invoice_id'])->count();
3 years ago
4 years ago
return resultArray(['data' => ['fileCount' => $fileCount]]);
}
3 years ago
4 years ago
/**
* 重置开票信息
*
* @param InvoiceLogic $invoiceLogic
* @return \think\response\Json
*/
public function resetInvoiceStatus(InvoiceLogic $invoiceLogic)
{
if (empty($this->param['invoice_id'])) resultArray(['error' => '参数错误!']);
3 years ago
$userInfo = $this->userInfo;
4 years ago
$this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
3 years ago
4 years ago
# 开票状态
$this->param['invoice_status'] = 1;
3 years ago
$invoice_info = db('crm_invoice')->where('invoice_id', $this->param['invoice_id'])->find();
4 years ago
if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
3 years ago
RecordActionLog($userInfo['id'], 'crm_invoice', 'update', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '重置开票状态');
4 years ago
return resultArray(['data' => '操作成功!']);
3 years ago
}
/**
* 导出
*
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/5/22 0022 14:34
*/
public function excelExport()
{
$param = $this->param;
$userInfo = $this->userInfo;
$action_name = '导出全部';
$param['is_excel'] = 1;
$invoiceLogic=new InvoiceLogic();
$excelModel = new \app\admin\model\Excel();
// 导出的字段列表
$fieldModel = new \app\admin\model\Field();
$field_list = $fieldModel->getIndexFieldConfig('crm_invoice', $userInfo['id'],'','excel');
if ($param['invoice_id']) {
$param['invoice_id'] = ['condition' => 'in', 'value' => $param['invoice_id'], 'form_type' => 'text', 'name' => ''];
$action_name = '导出选中';
}
$field = [
0 => [
'field' => 'check_status',
'name' => '审核状态',
'form_type' => 'text',
'is_hidden' => 0,
],
1 => [
'field' => 'invoice_status',
'name' => '开票状态',
'form_type' => 'text',
'is_hidden' => 0
],
2 => [
'field' => 'real_invoice_date',
'name' => '实际开票日期',
'form_type' => 'date',
'is_hidden' => 0
],
3 => [
'field' => 'invoice_number',
'name' => '发票号码',
'form_type' => 'text',
'is_hidden' => 0
],
4 => [
'field' => 'logistics_number',
'name' => '物流单号',
'form_type' => 'text',
'is_hidden' => 0,
]
];
$field_list=array_merge($field_list,$field);
// 文件名
$file_name = '5kcrm_invoice_' . date('Ymd');
$model = model('Invoice');
$temp_file = $param['temp_file'];
unset($param['temp_file']);
$page = $param['page']?: 1;
unset($param['page']);
unset($param['export_queue_index']);
// RecordActionLog($userInfo['id'],'crm_invoice','excelexport',$action_name,'','','导出客户');
return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list,$invoiceLogic) {
$param['page'] = $page;
$param['limit'] = $limit;
$data = $invoiceLogic->index($param);
$data['list'] = $model->exportHandle($data['list'], $field_list, 'invoice');
return $data;
});
4 years ago
}
}