InvoiceLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * 发票逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-07
  7. */
  8. namespace app\crm\logic;
  9. use app\admin\controller\ApiCommon;
  10. use app\admin\model\Common;
  11. use app\crm\model\Invoice;
  12. use think\Db;
  13. class InvoiceLogic
  14. {
  15. private $invoiceType = ['增值税专用发票', '增值税普通发票', '国税通用机打发票', '地税通用机打发票', '收据'];
  16. /**
  17. * 列表
  18. *
  19. * @param $param
  20. * @param false $search
  21. * @return array
  22. * @throws \think\exception\DbException
  23. */
  24. public function index($param, $search = false)
  25. {
  26. $field = [
  27. 'invoice_id',
  28. 'invoice_apple_number',
  29. 'invoice_money',
  30. 'invoice_date',
  31. 'real_invoice_date',
  32. 'invoice_type',
  33. 'invoice_number',
  34. 'logistics_number',
  35. 'check_status',
  36. 'invoice_status',
  37. 'customer_id',
  38. 'contract_id',
  39. 'owner_user_id',
  40. 'flow_id'
  41. ];
  42. $limit = $param['limit'];
  43. $getCount = $param['getCount'];
  44. $userId = $param['user_id'];
  45. $invoiceIdArray = $param['invoiceIdArray']; // 待办事项提醒参数
  46. $dealt = $param['dealt'];
  47. unset($param['getCount']);
  48. unset($param['limit']);
  49. unset($param['page']);
  50. unset($param['user_id']);
  51. unset($param['invoiceIdArray']);
  52. unset($param['dealt']);
  53. $where = [];
  54. if ($search) {
  55. # 处理基本参数
  56. $scene_id = $param['scene_id'];
  57. unset($param['scene_id']);
  58. $common = new Common();
  59. # 高级搜索
  60. $request = $common->fmtRequest($param);
  61. $requestMap = !empty($request['map']) ? $request['map'] : [];
  62. unset($requestMap['search']);
  63. # 场景
  64. $sceneMap = [];
  65. if (!empty($scene_id) && $scene_id == 1) {
  66. # 我负责的
  67. $sceneMap['owner_user_id'] = $userId;
  68. }
  69. if (!empty($scene_id) && $scene_id == 2) {
  70. # 我下属负责的
  71. $subordinate = getSubUserId(false, 0, $userId);
  72. $sceneMap['owner_user_id'] = !empty($subordinate) ? ['in', $subordinate] : 0;
  73. }
  74. # 合并高级搜索和场景的查询条件
  75. $map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
  76. $map = where_arr($map, 'crm', 'invoice', 'index');
  77. # 替换掉字段前缀,不修改公共函数
  78. foreach ($map AS $key => $value) {
  79. $k = str_replace('invoice.', '', $key);
  80. $where[$k] = $value;
  81. }
  82. # 重置查询条件
  83. if ($where) $param = $where;
  84. }
  85. # 待办事项查询参数
  86. $dealtWhere = [];
  87. if (!empty($invoiceIdArray)) $dealtWhere['invoice_id'] = ['in', $invoiceIdArray];
  88. # 权限,不是待办事项,则加上列表权限
  89. $auth = [];
  90. if (empty($dealt)) {
  91. $userModel = new \app\admin\model\User();
  92. $authUserIds = $userModel->getUserByPer('crm', 'invoice', 'index');
  93. $auth['owner_user_id'] = ['in', $authUserIds];
  94. }
  95. # 查询数据
  96. $list = Invoice::with(['toCustomer', 'toContract', 'toAdminUser'])->field($field)->where($auth)
  97. ->where($param)->where($dealtWhere)->limit($limit)->order('update_time', 'desc')->paginate($limit)->toArray();
  98. # 处理发票类型
  99. // foreach ($list['data'] AS $key => $value) {
  100. // $list['data'][$key]['invoice_type'] = $this->invoiceType[$value['invoice_type']];
  101. // }
  102. return ['list' => $list['data'], 'dataCount' => $list['total']];
  103. }
  104. /**
  105. * 创建
  106. *
  107. * @param $param
  108. * @return Invoice|int|string
  109. */
  110. public function save($param)
  111. {
  112. return db('crm_invoice')->insert($param, false, true);
  113. }
  114. /**
  115. * 详情
  116. *
  117. * @param $invoiceId
  118. * @return array
  119. * @throws \think\Exception
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @throws \think\exception\DbException
  123. */
  124. public function read($invoiceId, $isUpdate)
  125. {
  126. $apiCommon = new ApiCommon();
  127. $userId = $apiCommon->userInfo['id'];
  128. $result = [];
  129. $dataObject = Invoice::with(['toCustomer', 'toContract'])->where('invoice_id', $invoiceId)->find();
  130. if (empty($dataObject)) return $result;
  131. $dataArray = $dataObject->toArray();
  132. if (!empty($isUpdate)) return $dataArray;
  133. # 主键ID
  134. $result['invoice_id'] = $dataArray['invoice_id'];
  135. # 是否显示撤回按钮
  136. $result['isShowRecall'] = 0;
  137. if ($userId == $dataArray['owner_user_id'] && $dataArray['check_status'] == 0) $result['isShowRecall'] = 1;
  138. $result['customer_name'] = $dataArray['customer_name']; # 客户名称
  139. $result['invoice_money'] = $dataArray['invoice_money']; # 开票金额
  140. $result['invoice_number'] = $dataArray['invoice_number']; # 发票号码
  141. $result['real_invoice_date'] = $dataArray['real_invoice_date']; # 开票日期
  142. $result['flow_id'] = $dataArray['flow_id']; # 审核ID
  143. $check=['0'=>'待审核','1'=>'审核中','2'=>'审核通过','3'=>'审核未通过','4'=>'撤销','5'=>'草稿(未提交)','6'=>'作废'];
  144. # 基本信息
  145. $result['essential'] = [
  146. 'invoice_apple_number' => $dataArray['invoice_apple_number'],
  147. 'customer_name' => $dataArray['customer_name'],
  148. 'contract_number' => $dataArray['contract_number'],
  149. 'contract_money' => $dataArray['contract_money'],
  150. 'invoice_money' => $dataArray['invoice_money'],
  151. 'invoice_date' => $dataArray['invoice_date'],
  152. 'invoice_type' => $dataArray['invoice_type'],
  153. 'remark' => $dataArray['remark'],
  154. 'create_user_name' => db('admin_user')->where('id', $dataArray['create_user_id'])->value('realname'),
  155. 'owner_user_name' => db('admin_user')->where('id', $dataArray['owner_user_id'])->value('realname'),
  156. 'create_time' => $dataArray['create_time'],
  157. 'update_time' => $dataArray['update_time'],
  158. 'invoice_number' => $dataArray['invoice_number'],
  159. 'real_invoice_date' => $dataArray['real_invoice_date'],
  160. 'customer_id' => $dataArray['customer_id'],
  161. 'check_status' => $check[$dataArray['check_status']]
  162. ];
  163. # 发票信息
  164. $result['invoice'] = [
  165. 'title_type' => $dataArray['title_type'],
  166. 'deposit_bank' => $dataArray['deposit_bank'],
  167. 'invoice_title' => $dataArray['invoice_title'],
  168. 'tax_number' => $dataArray['tax_number'],
  169. 'deposit_account' => $dataArray['deposit_account'],
  170. 'deposit_address' => $dataArray['deposit_address'],
  171. 'phone' => $dataArray['phone']
  172. ];
  173. # 邮寄信息
  174. $result['posting'] = [
  175. 'contacts_name' => $dataArray['contacts_name'],
  176. 'contacts_mobile' => $dataArray['contacts_mobile'],
  177. 'contacts_address' => $dataArray['contacts_address']
  178. ];
  179. return $result;
  180. }
  181. /**
  182. * 编辑
  183. *
  184. * @param $param
  185. * @return Invoice
  186. */
  187. public function update($param)
  188. {
  189. return Invoice::update($param);
  190. }
  191. /**
  192. * 删除
  193. *
  194. * @param $where
  195. * @return int
  196. */
  197. public function delete($where)
  198. {
  199. return Invoice::destroy($where);
  200. }
  201. /**
  202. * 获取审批状态
  203. *
  204. * @param $invoiceId
  205. * @param false $isDelete
  206. * @return bool|int|mixed|\PDOStatement|string|\think\Collection|null
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. * @throws \think\exception\DbException
  210. */
  211. public function getExamineStatus($invoiceId, $isDelete = false)
  212. {
  213. # 删除
  214. if ($isDelete) {
  215. return Invoice::field(['check_status'])->whereIn('invoice_id', $invoiceId)->select();
  216. }
  217. # 编辑
  218. return Invoice::where('invoice_id', $invoiceId)->value('check_status');
  219. }
  220. /**
  221. * 转移(变更负责人)
  222. *
  223. * @param $invoiceIds
  224. * @param $ownerUserId
  225. * @return Invoice
  226. */
  227. public function transfer($invoiceIds, $ownerUserId)
  228. {
  229. return Invoice::whereIn('invoice_id', $invoiceIds)->update(['owner_user_id' => $ownerUserId]);
  230. }
  231. /**
  232. * 设置开票
  233. *
  234. * @param $param
  235. * @return Invoice
  236. */
  237. public function setInvoice($param)
  238. {
  239. return Invoice::update($param);
  240. }
  241. /**
  242. * 获取发票审核信息
  243. *
  244. * @param $invoiceId
  245. * @return array|bool|\PDOStatement|string|\think\Model|null
  246. * @throws \think\db\exception\DataNotFoundException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. * @throws \think\exception\DbException
  249. */
  250. public function getExamineInfo($invoiceId)
  251. {
  252. $field = ['check_status', 'flow_id', 'order_id', 'check_user_id', 'flow_user_id', 'invoice_apple_number', 'owner_user_id', 'create_user_id'];
  253. return Invoice::field($field)->where('invoice_id', $invoiceId)->find();
  254. }
  255. /**
  256. * 设置审批信息
  257. *
  258. * @param $data
  259. * @return Invoice
  260. */
  261. public function setExamineInfo($data)
  262. {
  263. return Invoice::update($data);
  264. }
  265. /**
  266. * 添加撤销审核记录
  267. *
  268. * @param $invoiceId
  269. * @param $examineInfo
  270. * @param $realname
  271. * @param $content
  272. * @param $userId
  273. */
  274. public function createExamineRecord($invoiceId, $examineInfo, $realname, $content, $userId)
  275. {
  276. $data = [
  277. 'types' => 'crm_invoice',
  278. 'types_id' => $invoiceId,
  279. 'flow_id' => $examineInfo['flow_id'],
  280. 'order_id' => $examineInfo['order_id'],
  281. 'check_user_id' => $userId,
  282. 'check_time' => time(),
  283. 'status' => 2,
  284. 'content' => !empty($content) ? $content : $realname . ' 撤销了审核',
  285. ];
  286. Db::name('admin_examine_record')->insert($data);
  287. }
  288. /**
  289. * 检查发票编号是否重复
  290. *
  291. * @param $where
  292. * @return int|mixed|string|null
  293. */
  294. public function getInvoiceId($where)
  295. {
  296. return Db::name('crm_invoice')->where($where)->value('invoice_id');
  297. }
  298. }