InvoiceLogic.php 10KB

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. # 基本信息
  144. $result['essential'] = [
  145. 'invoice_apple_number' => $dataArray['invoice_apple_number'],
  146. 'customer_name' => $dataArray['customer_name'],
  147. 'contract_number' => $dataArray['contract_number'],
  148. 'contract_money' => $dataArray['contract_money'],
  149. 'invoice_money' => $dataArray['invoice_money'],
  150. 'invoice_date' => $dataArray['invoice_date'],
  151. 'invoice_type' => $dataArray['invoice_type'],
  152. 'remark' => $dataArray['remark'],
  153. 'create_user_name' => db('admin_user')->where('id', $dataArray['create_user_id'])->value('realname'),
  154. 'owner_user_name' => db('admin_user')->where('id', $dataArray['owner_user_id'])->value('realname'),
  155. 'create_time' => $dataArray['create_time'],
  156. 'update_time' => $dataArray['update_time'],
  157. 'invoice_number' => $dataArray['invoice_number'],
  158. 'real_invoice_date' => $dataArray['real_invoice_date'],
  159. 'customer_id' => $dataArray['customer_id'],
  160. 'contract_id' => $dataArray['contract_id']
  161. ];
  162. # 发票信息
  163. $result['invoice'] = [
  164. 'title_type' => $dataArray['title_type'],
  165. 'deposit_bank' => $dataArray['deposit_bank'],
  166. 'invoice_title' => $dataArray['invoice_title'],
  167. 'tax_number' => $dataArray['tax_number'],
  168. 'deposit_account' => $dataArray['deposit_account'],
  169. 'deposit_address' => $dataArray['deposit_address'],
  170. 'phone' => $dataArray['phone']
  171. ];
  172. # 邮寄信息
  173. $result['posting'] = [
  174. 'contacts_name' => $dataArray['contacts_name'],
  175. 'contacts_mobile' => $dataArray['contacts_mobile'],
  176. 'contacts_address' => $dataArray['contacts_address']
  177. ];
  178. return $result;
  179. }
  180. /**
  181. * 编辑
  182. *
  183. * @param $param
  184. * @return Invoice
  185. */
  186. public function update($param)
  187. {
  188. return Invoice::update($param);
  189. }
  190. /**
  191. * 删除
  192. *
  193. * @param $where
  194. * @return int
  195. */
  196. public function delete($where)
  197. {
  198. return Invoice::destroy($where);
  199. }
  200. /**
  201. * 获取审批状态
  202. *
  203. * @param $invoiceId
  204. * @param false $isDelete
  205. * @return bool|int|mixed|\PDOStatement|string|\think\Collection|null
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. * @throws \think\exception\DbException
  209. */
  210. public function getExamineStatus($invoiceId, $isDelete = false)
  211. {
  212. # 删除
  213. if ($isDelete) {
  214. return Invoice::field(['check_status'])->whereIn('invoice_id', $invoiceId)->select();
  215. }
  216. # 编辑
  217. return Invoice::where('invoice_id', $invoiceId)->value('check_status');
  218. }
  219. /**
  220. * 转移(变更负责人)
  221. *
  222. * @param $invoiceIds
  223. * @param $ownerUserId
  224. * @return Invoice
  225. */
  226. public function transfer($invoiceIds, $ownerUserId)
  227. {
  228. return Invoice::whereIn('invoice_id', $invoiceIds)->update(['owner_user_id' => $ownerUserId]);
  229. }
  230. /**
  231. * 设置开票
  232. *
  233. * @param $param
  234. * @return Invoice
  235. */
  236. public function setInvoice($param)
  237. {
  238. return Invoice::update($param);
  239. }
  240. /**
  241. * 获取发票审核信息
  242. *
  243. * @param $invoiceId
  244. * @return array|bool|\PDOStatement|string|\think\Model|null
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\ModelNotFoundException
  247. * @throws \think\exception\DbException
  248. */
  249. public function getExamineInfo($invoiceId)
  250. {
  251. $field = ['check_status', 'flow_id', 'order_id', 'check_user_id', 'flow_user_id', 'invoice_apple_number', 'owner_user_id', 'create_user_id'];
  252. return Invoice::field($field)->where('invoice_id', $invoiceId)->find();
  253. }
  254. /**
  255. * 设置审批信息
  256. *
  257. * @param $data
  258. * @return Invoice
  259. */
  260. public function setExamineInfo($data)
  261. {
  262. return Invoice::update($data);
  263. }
  264. /**
  265. * 添加撤销审核记录
  266. *
  267. * @param $invoiceId
  268. * @param $examineInfo
  269. * @param $realname
  270. * @param $content
  271. * @param $userId
  272. */
  273. public function createExamineRecord($invoiceId, $examineInfo, $realname, $content, $userId)
  274. {
  275. $data = [
  276. 'types' => 'crm_invoice',
  277. 'types_id' => $invoiceId,
  278. 'flow_id' => $examineInfo['flow_id'],
  279. 'order_id' => $examineInfo['order_id'],
  280. 'check_user_id' => $userId,
  281. 'check_time' => time(),
  282. 'status' => 2,
  283. 'content' => !empty($content) ? $content : $realname . ' 撤销了审核',
  284. ];
  285. Db::name('admin_examine_record')->insert($data);
  286. }
  287. /**
  288. * 检查发票编号是否重复
  289. *
  290. * @param $where
  291. * @return int|mixed|string|null
  292. */
  293. public function getInvoiceId($where)
  294. {
  295. return Db::name('crm_invoice')->where($where)->value('invoice_id');
  296. }
  297. }