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

449 lines
17 KiB

4 years ago
<?php
/**
* 发票逻辑类
*
* @author qifan
* @date 2020-12-07
*/
namespace app\crm\logic;
use app\admin\controller\ApiCommon;
use app\admin\model\Common;
use app\crm\model\Invoice;
use think\Db;
class InvoiceLogic
{
private $invoiceType = ['增值税专用发票', '增值税普通发票', '国税通用机打发票', '地税通用机打发票', '收据'];
3 years ago
private $check_status = ['待审核', '审核中', '审核通过', '审核未通过', '撤回'];
4 years ago
/**
* 列表
*
* @param $param
* @param false $search
* @return array
* @throws \think\exception\DbException
*/
3 years ago
public function index($param)
4 years ago
{
3 years ago
$fieldModel = new \app\admin\model\Field();
//列表展示字段
$field = $fieldModel->getIndexField('crm_invoice', $param['user_id'], 1) ?: array('name');
if (!empty($param['is_excel']) && !empty($param['invoice_id'])) {
$param['invoice_id'] = ['in', arrayToString($param['invoice_id'])];
}
4 years ago
$getCount = $param['getCount'];
3 years ago
$userId = $param['user_id'];
4 years ago
$invoiceIdArray = $param['invoiceIdArray']; // 待办事项提醒参数
$dealt = $param['dealt'];
3 years ago
$order_field = $param['order_field'];
$order_type = $param['order_type'];
$is_excel = $param['is_excel'];
$search = $param['search'];
$scene_id = $param['scene_id'];
$isMessage = !empty($param['isMessage']);
$common = new Common();
4 years ago
unset($param['getCount']);
3 years ago
// unset($param['limit']); 导出使用 暂未发现为何去掉分页参数
// unset($param['page']);
4 years ago
unset($param['user_id']);
unset($param['invoiceIdArray']);
unset($param['dealt']);
3 years ago
unset($param['search']);
unset($param['order_field']);
unset($param['order_type']);
unset($param['is_excel']);
unset($param['scene_id']);
unset($param['isMessage']);
$request = $common->fmtRequest($param);
4 years ago
$where = [];
3 years ago
# 高级搜索
$requestMap = !empty($request['map']) ? $request['map'] : [];
unset($requestMap['search']);
# 场景
$sceneMap = [];
$sceneModel = new \app\admin\model\Scene();
if ($scene_id) {
//自定义场景
$sceneMap = $sceneModel->getDataById($scene_id, $userId, 'invoice') ?: [];
} else {
//默认场景
$sceneMap = $sceneModel->getDefaultData('crm_invoice', $userId) ?: [];
}
//普通筛选
4 years ago
if ($search) {
# 处理基本参数
3 years ago
$searchWhere = function ($query) use ($search) {
$query->where(function ($query) use ($search) {
$query->whereLike('customer.name', '%' . $search . '%');
})->whereOr(function ($query) use ($search) {
$query->whereLike('contract.num', '%' . $search . '%');
})->whereOr(function ($query) use ($search) {
$query->whereLike('invoice.invoice_apple_number', '%' . $search . '%');
});
};
}
# 合并高级搜索和场景的查询条件
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
$map = where_arr($map, 'crm', 'invoice', 'index');
# 替换掉字段前缀,不修改公共函数
foreach ($map as $key => $value) {
$k = str_replace('invoice.', '', $key);
$where[$k] = $value;
4 years ago
}
4 years ago
# 待办事项查询参数
$dealtWhere = [];
3 years ago
if (!empty($invoiceIdArray)) $dealtWhere['invoice.invoice_id'] = ['in', $invoiceIdArray];
4 years ago
# 权限,不是待办事项,则加上列表权限
$auth = [];
3 years ago
$userModel = new \app\admin\model\User();
$a = 'index';
if ($is_excel) $a = 'excelExport';
$auth_user_ids = $userModel->getUserByPer('crm', 'invoice', $a);
4 years ago
if (empty($dealt)) {
3 years ago
//过滤权限
if (isset($map['invoice.owner_user_id']) && $map['invoice.owner_user_id'][0] != 'like') {
if (!is_array($map['invoice.owner_user_id'][1])) {
$map['invoice.owner_user_id'][1] = [$map['invoice.owner_user_id'][1]];
}
if (in_array($map['invoice.owner_user_id'][0], ['neq', 'notin'])) {
$auth_user_ids = array_diff($auth_user_ids, $map['invoice.owner_user_id'][1]) ?: []; //取差集
} else {
$auth_user_ids = array_intersect($map['invoice.owner_user_id'][1], $auth_user_ids) ?: []; //取交集
}
unset($map['invoice.owner_user_id']);
}
4 years ago
}
3 years ago
$auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
// 待办事项的待审核发票不一定是自己创建的
if (!$isMessage) {
$auth['invoice.owner_user_id'] = ['in', $auth_user_ids];
}
if ($order_type && $order_field) {
$order = $fieldModel->getOrderByFormtype('crm_invoice', 'invoice', $order_field, $order_type);
} else {
$order = 'invoice.update_time desc';
}
$join = [
['__CRM_CUSTOMER__ customer', 'customer.customer_id=invoice.customer_id', 'LEFT'],
['__CRM_CONTRACT__ contract', 'contract.contract_id=invoice.contract_id', 'LEFT'],
['__ADMIN_USER__ user', 'user.id=invoice.owner_user_id', 'LEFT'],
['__ADMIN_USER__ u', 'u.id=invoice.create_user_id', 'LEFT'],
];
4 years ago
# 查询数据
3 years ago
$list = db('crm_invoice')
->alias('invoice')
->join($join)
->field(array_merge($field, [
'customer.name' => 'customer_name',
'user.realname' => 'owner_user_name',
'contract.num' => 'contract_num',
'contract.money' => 'contract_money',
'u.realname' => 'create_user_name',
]))->where($auth)
->where($map)
->where($dealtWhere)
->where($searchWhere)
->limit($request['offset'], $request['length'])
->orderRaw($order)
->select();
$dataCount = db('crm_invoice')
->alias('invoice')
->join($join)
->field(array_merge($field, [
'customer.name' => 'customer_name',
'user.realname' => 'owner_user_name',
'contract.num' => 'contract_num',
'contract.money' => 'contract_money',
'u.realname' => 'create_user_name',
]))->where($auth)
->where($map)
->where($dealtWhere)->where($searchWhere)->count();
$userField = $fieldModel->getFieldByFormType('crm_invoice', 'user'); //人员类型
$structureField = $fieldModel->getFieldByFormType('crm_invoice', 'structure'); //部门类型
$datetimeField = $fieldModel->getFieldByFormType('crm_invoice', 'datetime'); //日期时间类型
$booleanField = $fieldModel->getFieldByFormType('crm_invoice', 'boolean_value'); //布尔值
$dateIntervalField = $fieldModel->getFieldByFormType('crm_invoice', 'date_interval'); // 日期区间类型字段
$positionField = $fieldModel->getFieldByFormType('crm_invoice', 'position'); // 地址类型字段
$handwritingField = $fieldModel->getFieldByFormType('crm_invoice', 'handwriting_sign'); // 手写签名类型字段
$locationField = $fieldModel->getFieldByFormType('crm_invoice', 'location'); // 定位类型字段
# 扩展数据
$extraData = [];
$invoice_id_list = !empty($list) ? array_column($list, 'invoice_id') : [];
$extraList = db('crm_invoice_data')->whereIn('invoice_id', $invoice_id_list)->select();
foreach ($extraList AS $key => $value) {
$extraData[$value['invoice_id']][$value['field']] = $value['content'];
}
4 years ago
# 处理发票类型
3 years ago
foreach ($list as $key => $value) {
$list[$key]['check_status_info'] = $this->check_status[$value['check_status']];
$list[$key]['invoice_status'] = !empty($value['invoice_status']) ? '已开票' : '未开票';
$list[$key]['create_time'] = !empty($value['create_time']) ? date('Y-m-d H:i:s',$value['create_time']) : null;
$list[$key]['update_time'] = !empty($value['update_time']) ? date('Y-m-d H:i:s',$value['update_time']) : null;
foreach ($userField as $k => $val) {
$usernameField = !empty($value[$val]) ? db('admin_user')->whereIn('id', stringToArray($value[$val]))->column('realname') : [];
$list[$key][$val] = implode($usernameField, ',');
}
foreach ($structureField as $k => $val) {
$structureNameField = !empty($value[$val]) ? db('admin_structure')->whereIn('id', stringToArray($value[$val]))->column('name') : [];
$list[$key][$val] = implode($structureNameField, ',');
}
foreach ($datetimeField as $k => $val) {
$list[$key][$val] = !empty($value[$val]) ? date('Y-m-d H:i:s', $value[$val]) : null;
}
foreach ($booleanField as $k => $val) {
$list[$key][$val] = !empty($value[$val]) ? (string)$value[$val] : '0';
}
// 处理日期区间类型字段的格式
foreach ($dateIntervalField as $k => $val) {
$list[$key][$val] = !empty($extraData[$value['invoice_id']][$val]) ? json_decode($extraData[$value['invoice_id']][$val], true) : null;
}
// 处理地址类型字段的格式
foreach ($positionField as $k => $val) {
$list[$key][$val] = !empty($extraData[$value['invoice_id']][$val]) ? json_decode($extraData[$value['invoice_id']][$val], true) : null;
}
// 手写签名类型字段
foreach ($handwritingField AS $k => $val) {
$handwritingData = !empty($value[$val]) ? db('admin_file')->where('file_id', $value[$val])->value('file_path') : null;
$list[$key][$val] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
}
// 定位类型字段
foreach ($locationField as $k => $val) {
$list[$key][$val] = !empty($extraData[$value['invoice_id']][$val]) ? json_decode($extraData[$value['invoice_id']][$val], true) : null;
}
}
$data = [];
$data['list'] = $list ?: [];
$data['dataCount'] = $dataCount ?: 0;
return $data;
4 years ago
}
3 years ago
4 years ago
/**
* 创建
*
* @param $param
* @return Invoice|int|string
*/
public function save($param)
{
return db('crm_invoice')->insert($param, false, true);
}
3 years ago
4 years ago
/**
* 详情
*
* @param $invoiceId
* @return array
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function read($invoiceId, $isUpdate)
{
$apiCommon = new ApiCommon();
3 years ago
$userId = $apiCommon->userInfo['id'];
$result = [];
4 years ago
$dataObject = Invoice::with(['toCustomer', 'toContract'])->where('invoice_id', $invoiceId)->find();
3 years ago
4 years ago
if (empty($dataObject)) return $result;
3 years ago
4 years ago
$dataArray = $dataObject->toArray();
3 years ago
4 years ago
if (!empty($isUpdate)) return $dataArray;
3 years ago
4 years ago
# 主键ID
$result['invoice_id'] = $dataArray['invoice_id'];
3 years ago
4 years ago
# 是否显示撤回按钮
$result['isShowRecall'] = 0;
if ($userId == $dataArray['owner_user_id'] && $dataArray['check_status'] == 0) $result['isShowRecall'] = 1;
3 years ago
$result['customer_name'] = $dataArray['customer_name']; # 客户名称
$result['invoice_money'] = $dataArray['invoice_money']; # 开票金额
$result['invoice_number'] = $dataArray['invoice_number']; # 发票号码
4 years ago
$result['real_invoice_date'] = $dataArray['real_invoice_date']; # 开票日期
3 years ago
$result['flow_id'] = $dataArray['flow_id']; # 审核ID
$check = ['0' => '待审核', '1' => '审核中', '2' => '审核通过', '3' => '审核未通过', '4' => '撤销', '5' => '草稿(未提交)', '6' => '作废'];
4 years ago
# 基本信息
$result['essential'] = [
'invoice_apple_number' => $dataArray['invoice_apple_number'],
3 years ago
'customer_name' => $dataArray['customer_name'],
'contract_num' => $dataArray['contract_number'],
'contract_money' => $dataArray['contract_money'],
'invoice_money' => $dataArray['invoice_money'],
'invoice_date' => $dataArray['invoice_date'],
'invoice_type' => $dataArray['invoice_type'],
'remark' => $dataArray['remark'],
'create_user_name' => db('admin_user')->where('id', $dataArray['create_user_id'])->value('realname'),
'owner_user_name' => db('admin_user')->where('id', $dataArray['owner_user_id'])->value('realname'),
'create_time' => $dataArray['create_time'],
'update_time' => $dataArray['update_time'],
'invoice_number' => $dataArray['invoice_number'],
'real_invoice_date' => $dataArray['real_invoice_date'],
'customer_id' => $dataArray['customer_id'],
'check_status' => $check[$dataArray['check_status']]
4 years ago
];
3 years ago
4 years ago
# 发票信息
$result['invoice'] = [
3 years ago
'title_type' => $dataArray['title_type'],
'deposit_bank' => $dataArray['deposit_bank'],
'invoice_title' => $dataArray['invoice_title'],
'tax_number' => $dataArray['tax_number'],
4 years ago
'deposit_account' => $dataArray['deposit_account'],
'deposit_address' => $dataArray['deposit_address'],
3 years ago
'phone' => $dataArray['phone']
4 years ago
];
3 years ago
4 years ago
# 邮寄信息
3 years ago
$result['posting'] = [
'contacts_name' => $dataArray['contacts_name'],
'contacts_mobile' => $dataArray['contacts_mobile'],
4 years ago
'contacts_address' => $dataArray['contacts_address']
];
3 years ago
4 years ago
return $result;
}
3 years ago
4 years ago
/**
* 编辑
*
* @param $param
* @return Invoice
*/
public function update($param)
{
return Invoice::update($param);
}
3 years ago
4 years ago
/**
* 删除
*
* @param $where
* @return int
*/
public function delete($where)
{
return Invoice::destroy($where);
}
3 years ago
4 years ago
/**
* 获取审批状态
*
* @param $invoiceId
* @param false $isDelete
* @return bool|int|mixed|\PDOStatement|string|\think\Collection|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getExamineStatus($invoiceId, $isDelete = false)
{
# 删除
if ($isDelete) {
return Invoice::field(['check_status'])->whereIn('invoice_id', $invoiceId)->select();
}
3 years ago
4 years ago
# 编辑
return Invoice::where('invoice_id', $invoiceId)->value('check_status');
}
3 years ago
4 years ago
/**
* 转移(变更负责人)
*
* @param $invoiceIds
* @param $ownerUserId
* @return Invoice
*/
public function transfer($invoiceIds, $ownerUserId)
{
return Invoice::whereIn('invoice_id', $invoiceIds)->update(['owner_user_id' => $ownerUserId]);
}
3 years ago
4 years ago
/**
* 设置开票
3 years ago
*
4 years ago
* @param $param
* @return Invoice
*/
public function setInvoice($param)
{
return Invoice::update($param);
}
3 years ago
4 years ago
/**
* 获取发票审核信息
*
* @param $invoiceId
* @return array|bool|\PDOStatement|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getExamineInfo($invoiceId)
{
$field = ['check_status', 'flow_id', 'order_id', 'check_user_id', 'flow_user_id', 'invoice_apple_number', 'owner_user_id', 'create_user_id'];
3 years ago
4 years ago
return Invoice::field($field)->where('invoice_id', $invoiceId)->find();
}
3 years ago
4 years ago
/**
* 设置审批信息
*
* @param $data
* @return Invoice
*/
public function setExamineInfo($data)
{
return Invoice::update($data);
}
3 years ago
4 years ago
/**
* 添加撤销审核记录
*
* @param $invoiceId
* @param $examineInfo
* @param $realname
* @param $content
* @param $userId
*/
public function createExamineRecord($invoiceId, $examineInfo, $realname, $content, $userId)
{
$data = [
3 years ago
'types' => 'crm_invoice',
'types_id' => $invoiceId,
'flow_id' => $examineInfo['flow_id'],
'order_id' => $examineInfo['order_id'],
4 years ago
'check_user_id' => $userId,
3 years ago
'check_time' => time(),
'status' => 2,
'content' => !empty($content) ? $content : $realname . ' 撤销了审核',
4 years ago
];
3 years ago
4 years ago
Db::name('admin_examine_record')->insert($data);
}
3 years ago
4 years ago
/**
* 检查发票编号是否重复
*
* @param $where
* @return int|mixed|string|null
*/
public function getInvoiceId($where)
{
return Db::name('crm_invoice')->where($where)->value('invoice_id');
}
}