| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930 |
- <?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;
- use app\admin\traits\FieldVerificationTrait;
- 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
- {
- use AutoNumberTrait, FieldVerificationTrait;
-
- /**
- * 用于判断权限
- * @permission 无限制
- * @allow 登录用户可访问
- * @other 其他根据系统设置
- **/
- public function _initialize()
- {
- $action = [
- 'permission' => [],
- 'allow' => ['check', 'revokecheck', 'count', 'read']
- ];
- Hook::listen('check_auth', $action);
- $request = Request::instance();
- $a = strtolower($request->action());
- if (!in_array($a, $action['permission'])) {
- parent::_initialize();
- }
- }
-
- /**
- * 列表
- *
- * @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'];
-
- $data = $invoiceLogic->index($param);
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 创建
- *
- * @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)
- {
- 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'];
- # 审批是否停用
- $examineStatus = $param['examineStatus'];
- # 删除无用参数
- unset($param['examineStatus']);
- unset($param['customer_name']);
- unset($param['contract_money']);
- unset($param['contract_number']);
- # 设置创建人负责人ID
- $param['create_user_id'] = $userId;
- $param['owner_user_id'] = $userId;
- # 创建更新日期
- $param['create_time'] = time();
- $param['update_time'] = time();
-
- # 自动设置发票编号
- $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' => '发票编号重复!']);
- }
-
- 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);
- $next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
- $check_user_id = $next_user_ids ?: [];
- $param['order_id'] = 1;
- } else {
- # 授权审批流
- $check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
- }
- if (!$check_user_id) {
- return resultArray(['error' => '无可用审批人,请联系管理员']);
- }
- $param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
- } else {
- # 审批流停用,将状态改为审核通过
- $param['check_status'] = 7;
- }
-
- // 数据验证
- // $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] = '';
- }
- }
- }
-
-
- if (!$invoice_id = $invoiceLogic->save($param)) {
- return resultArray(['error' => '创建失败!']);
- }
- $send_user_id = stringToArray($param['check_user_id']);
- (new Message())->send(
- Message::INVOICE_TO_DO,
- [
- 'title' => $param['invoice_apple_number'],
- 'action_id' => $invoice_id
- ],
- $send_user_id
- );
- # 更新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, '', '', '创建了发票');
- RecordActionLog($param['create_user_id'], 'crm_invoice', 'save', $param['invoice_apple_number'], '', '', '新增了发票' . $param['invoice_apple_number']);
-
- # 创建待办事项的关联数据
- $checkUserIds = db('crm_invoice')->where('invoice_id', $invoice_id)->value('check_user_id');
- $checkUserIdArray = stringToArray($checkUserIds);
- $dealtData = [];
- foreach ($checkUserIdArray as $kk => $vv) {
- $dealtData[] = [
- 'types' => 'crm_invoice',
- 'types_id' => $invoice_id,
- 'user_id' => $vv
- ];
- }
- if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
-
- // 添加发票扩展数据
- array_walk($invoiceData, function (&$val) use ($invoice_id) {
- $val['invoice_id'] = $invoice_id;
- });
- db('crm_invoice_data')->insertAll($invoiceData);
-
- return resultArray(['data' => '创建成功!']);
- }
-
- /**
- * 详情
- *
- * @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'];
- $isUpdate = !empty($this->param['is_update']) ? $this->param['is_update'] : 0;
- $userInfo = $this->userInfo;
-
- $data = $invoiceLogic->read($invoiceId, $isUpdate);
- $readStatus = false;
-
- # 角色权限
- $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');
-
- # 详情权限ID
- $invoiceAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'invoice', 'level' => 2])->value('id');
- $invoiceReadAuthId = db('admin_rule')->where(['types' => 2, 'name' => 'read', 'level' => 3, 'pid' => $invoiceAuthId])->value('id');
-
- foreach ($authArray as $key => $value) {
- if (!empty($value) && in_array($invoiceReadAuthId, stringToArray($value))) $readStatus = true;
- }
-
- if (!isSuperAdministrators($userInfo['id']) && $readStatus === false) {
- $authData['dataAuth'] = (int)0;
- return resultArray(['data' => $authData]);
- }
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 编辑
- *
- * @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;
- 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' => '请选择抬头类型!']);
- $userId = $this->userInfo['id'];
- $dataInfo = db('crm_invoice')->where('invoice_id',$param['invoice_id'])->find();
- # 审批是否停用
- $examineStatus = $param['examineStatus'];
- # 删除无用参数
- unset($param['examineStatus']);
- unset($param['customer_name']);
- unset($param['contract_money']);
- unset($param['contract_number']);
- # 设置负责人ID
- $param['update_time'] = time();
-
- # 已进行审批,不能编辑
- // $dataInfo = $invoiceLogic->read($param['invoice_id'], '');
- // if (!$dataInfo) {
- // $this->error = '数据不存在或已删除';
- // return false;
- // }
-
- $checkStatus = $invoiceLogic->getExamineStatus($param['invoice_id']);
- if (!in_array($checkStatus, ['3', '4', '5', '6', '7']) && $checkStatus!=0) return resultArray(['error' => '当前状态为审批中或已审批通过,不可编辑']);
-
- # 自动设置发票编号
- $numberInfo = [];
- if (empty($param['invoice_apple_number'])) {
- $numberInfo = $this->getAutoNumbers(4);
- if (empty($numberInfo['number'])) return resultArray(['error' => '请填写发票编号!']);
- $param['invoice_apple_number'] = $numberInfo['number'];
- }
-
- # 检查发票编号是否重复
- $invoiceWhere['invoice_apple_number'] = $param['invoice_apple_number'];
- $invoiceWhere['invoice_id'] = ['neq', $this->param['invoice_id']];
- if ($invoiceLogic->getInvoiceId($invoiceWhere)) return resultArray(['error' => '发票编号重复!']);
-
- if ($param['is_draft'] || (!empty($param['check_status']) && $param['check_status'] == 5)) {
- //保存为草稿
- $param['check_status'] = 5; //草稿(未提交)
- $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
- } 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);
- $next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
- $check_user_id = $next_user_ids ?: [];
- $param['order_id'] = 1;
- } else {
- # 授权审批流
- $check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
- }
- if ($param['is_draft']) {
- //保存为草稿
- $param['check_status'] = 5;
- $param['check_user_id'] = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
- } else {
- if (!$check_user_id) {
- return resultArray(['error' => '无可用审批人,请联系管理员']);
- }
- $param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
- $param['check_status'] = 0;
- }
- $param['flow_user_id'] = '';
- }
- }
-
- // 数据验证
- // $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] = '';
- }
- }
- }
-
- if (!$invoiceLogic->update($param)) {
- return resultArray(['error' => '编辑失败!']);
- }
-
- //将审批记录至为无效
- $examineRecordModel = new \app\admin\model\ExamineRecord();
- $examineRecordModel->setEnd(['types' => 'crm_invoice', 'types_id' => $param['invoice_id']]);
-
- # 更新crm_number_sequence表中的last_date、create_time字段
- if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
- //修改记录
- updateActionLog($param['user_id'], 'crm_invoice', $param['invoice_id'], $dataInfo, $param);
- RecordActionLog($param['user_id'], 'crm_invoice', 'update', $dataInfo['invoice_apple_number'], $dataInfo, $param);
- # 删除待办事项的关联数据
- 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 = [];
- foreach ($checkUserIdArray as $kk => $vv) {
- $dealtData[] = [
- 'types' => 'crm_invoice',
- 'types_id' => $param['invoice_id'],
- 'user_id' => $vv
- ];
- }
- if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
-
- // 添加客户扩展数据
- $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);
-
- return resultArray(['data' => '编辑成功!']);
- }
-
- /**
- * 删除
- *
- * @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)
- {
- $actionRecordModel = new \app\admin\model\ActionRecord();
- $fileModel = new \app\admin\model\File();
- $idArray = $this->param['id'];
- $userinfo = $this->userInfo['id'];
-
- if (!is_array($idArray)) return resultArray(['error' => '发票ID类型错误!']);
-
- $idString = implode(',', $idArray);
- $status = true;
-
- if (!isSuperAdministrators($userinfo['id'])) {
- $list = $invoiceLogic->getExamineStatus($idString, true);
- foreach ($list as $key => $value) {
- if (!in_array($value['check_status'], [4, 5])) {
- $status = false;
- break;
- }
- }
- }
-
- if (!$status) return resultArray(['error' => '不能删除审批中或审批结束的发票信息!']);
- $dataInfo = db('crm_invoice')->where('invoice_id', ['in', $idArray])->select();
- if (!$invoiceLogic->delete($idArray)) return resultArray(['error' => '删除失败!']);
-
- # 删除附件
- $fileModel->delRFileByModule('crm_invoice', $idArray);
- //删除关联操作记录
- $actionRecordModel->delDataById(['types' => 'crm_invoice', 'action_id' => $idArray]);
- $userInfo = $this->userInfo;
- foreach ($dataInfo as $k => $v) {
- RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $v['invoice_apple_number'], '', '', '删除了回款:' . $v['invoice_apple_number']);
- }
- return resultArray(['data' => '删除成功!']);
- }
-
- /**
- * 转移(变更负责人)
- *
- * @param InvoiceLogic $invoiceLogic
- * @return \think\response\Json
- */
- public function transfer(InvoiceLogic $invoiceLogic)
- {
- $ownerUserId = $this->param['owner_user_id'];
- $invoiceIds = $this->param['invoice_id'];
- $userModel = new \app\admin\model\User();
- $userInfo = $this->userInfo;
- if (empty($ownerUserId)) return resultArray(['error' => '请选择负责人!']);
- if (empty($invoiceIds)) return resultArray(['error' => '请选择发票!']);
- if (!is_array($invoiceIds)) return resultArray(['error' => '发票ID类型错误!']);
-
- if ($invoiceLogic->transfer($invoiceIds, $ownerUserId) === false) return resultArray(['error' => '操作失败!']);
- $owner_user_info = $userModel->getUserById($ownerUserId);
- foreach ($invoiceIds as $v) {
- $invoice_info = db('crm_invoice')->where('invoice_id', $v)->find();
- updateActionLog($userInfo['id'], 'crm_invoice', $v, '', '', '将发票转移给:' . $owner_user_info['realname']);
- RecordActionLog($userInfo['id'], 'crm_invoice', 'transfer', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '转移给:' . $owner_user_info['realname']);
- }
- return resultArray(['data' => '操作成功!']);
- }
-
- /**
- * 设置开票
- *
- * @param InvoiceLogic $invoiceLogic
- * @return \think\response\Json
- */
- public function setInvoice(InvoiceLogic $invoiceLogic)
- {
- if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
- // 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' => '请选择开票日期!']);
-
- $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
-
- # 开票状态
- $this->param['invoice_status'] = 1;
-
- # 设置开票信息
- if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
-
- return resultArray(['data' => '操作成功!']);
- }
-
- /**
- * 审核
- *
- * @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)
- {
- $param = $this->param;
- $user_id = $this->userInfo['id'];
-
- $examineStepModel = new \app\admin\model\ExamineStep();
- $examineRecordModel = new \app\admin\model\ExamineRecord();
- $examineFlowModel = new \app\admin\model\ExamineFlow();
-
- $invoiceData = [];
- $invoiceData['invoice_id'] = $param['id'];
- $invoiceData['update_time'] = time();
- $invoiceData['check_status'] = 1;
- # 权限判断
- if (!$examineStepModel->checkExamine($user_id, 'crm_invoice', $param['id'])) {
- return resultArray(['error' => $examineStepModel->getError()]);
- }
-
- # 审批主体详情
- $dataInfo = $invoiceLogic->getExamineInfo($param['id']);
- $flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
-
- # 1审批结束
- $is_end = 0;
-
- # 1通过,0驳回
- $status = !empty($param['status']) && $param['status'] == 1 ? 1 : 0;
-
- # 审批记录
- $checkData = [];
- $checkData['check_user_id'] = $user_id;
- $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;
-
- 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);
- $next_user_ids = $nextStepData['next_user_ids'] ?: [];
- $invoiceData['order_id'] = $nextStepData['order_id'] ?: '';
- if (!$next_user_ids) {
- $is_end = 1;
- # 审批结束
- $checkData['check_status'] = !empty($status) ? 2 : 3;
- $invoiceData['check_user_id'] = '';
- } else {
- # 修改主体相关审批信息
- $invoiceData['check_user_id'] = arrayToString($next_user_ids);
- }
- } else {
- # 自选流程
- $is_end = $param['is_end'] ? 1 : '';
- $check_user_id = $param['check_user_id'] ?: '';
- if ($is_end !== 1 && empty($check_user_id)) {
- return resultArray(['error' => '请选择下一审批人']);
- }
- $invoiceData['check_user_id'] = arrayToString($param['check_user_id']);
- }
- if ($is_end == 1) {
- $checkData['check_status'] = !empty($status) ? 2 : 3;
- $invoiceData['check_user_id'] = '';
- $invoiceData['check_status'] = 2;
- }
- } else {
- # 审批驳回
- $is_end = 1;
- $invoiceData['check_status'] = 3;
- }
- # 已审批人ID
- $invoiceData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']), [$user_id])) : arrayToString([$user_id]);
- $resContract = $invoiceLogic->setExamineInfo($invoiceData);
- if ($resContract) {
- # 审批记录
- $examineRecordModel->createData($checkData);
-
- # 发送站内信
- if ($is_end == 1 && !empty($status)) {
- # 审批流程结束,将审批通过消息告知负责人
- (new Message())->send(
- Message::INVOICE_PASS,
- [
- 'title' => $dataInfo['invoice_apple_number'],
- 'action_id' => $param['id']
- ],
- stringToArray($dataInfo['owner_user_id'])
- );
- } else {
- if (!empty($status)) {
- # 审批流程未结束,将待审批提醒发送给下一级负责人
- (new Message())->send(
- Message::INVOICE_TO_DO,
- [
- 'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
- 'title' => $dataInfo['invoice_apple_number'],
- 'action_id' => $param['id']
- ],
- stringToArray($invoiceData['check_user_id'])
- );
- } else {
- # 将审批被驳回的消息告知负责人
- (new Message())->send(
- Message::INVOICE_REJECT,
- [
- 'title' => $dataInfo['invoice_apple_number'],
- 'action_id' => $param['id']
- ],
- stringToArray($dataInfo['owner_user_id'])
- );
- }
- }
-
- return resultArray(['data' => '审批成功']);
- } else {
- return resultArray(['error' => '审批失败,请重试!']);
- }
- }
-
- /**
- * 撤销审核
- *
- * @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'];
- $content = $this->param['content'];
- $realname = $this->userInfo['realname'];
- $userInfo = $this->userInfo;
- $user_id = $userInfo['id'];
-
- if (empty($invoiceId)) return resultArray(['error' => '请选择要撤回审核的发票!']);
-
- $examineInfo = $invoiceLogic->getExamineInfo($invoiceId);
-
- 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' => '没有权限']);
- }
-
- # 修改发票审核状态
- if (!$invoiceLogic->update(['invoice_id' => $invoiceId, 'check_status' => 4, 'check_user_id' => '', 'flow_user_id' => ''])) {
- return resultArray(['error' => '操作失败!']);
- }
-
- # 添加撤销审核的记录
- $invoiceLogic->createExamineRecord($invoiceId, $examineInfo, $realname, $content, $user_id);
-
- return resultArray(['data' => '操作成功!']);
- }
-
- /**
- * table栏数量统计
- *
- * @return \think\response\Json
- * @throws \think\Exception
- */
- public function count()
- {
- if (empty($this->param['invoice_id'])) return resultArray(['error' => '参数错误!']);
-
- # 附件
- $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();
-
- return resultArray(['data' => ['fileCount' => $fileCount]]);
- }
-
- /**
- * 重置开票信息
- *
- * @param InvoiceLogic $invoiceLogic
- * @return \think\response\Json
- */
- public function resetInvoiceStatus(InvoiceLogic $invoiceLogic)
- {
- if (empty($this->param['invoice_id'])) resultArray(['error' => '参数错误!']);
- $userInfo = $this->userInfo;
- $this->param['real_invoice_date'] = !empty($this->param['real_invoice_date']) ? $this->param['real_invoice_date'] : date('Y-m-d');
-
- # 开票状态
- $this->param['invoice_status'] = 1;
- $invoice_info = db('crm_invoice')->where('invoice_id', $this->param['invoice_id'])->find();
- if (!$invoiceLogic->setInvoice($this->param)) return resultArray(['error' => '操作失败!']);
- RecordActionLog($userInfo['id'], 'crm_invoice', 'update', $invoice_info['invoice_apple_number'], '', '', '将发票:' . $invoice_info['invoice_apple_number'] . '重置开票状态');
- return resultArray(['data' => '操作成功!']);
-
- }
-
- /**
- * 导出
- *
- * @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;
- });
- }
- }
|