Invoice.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\ExamineRecord;
  4. use addons\qingdongams\model\File;
  5. use addons\qingdongams\model\Invoice as InvoiceModel;
  6. use addons\qingdongams\model\Message;
  7. use addons\qingdongams\model\Staff;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * 发票接口
  12. */
  13. class Invoice extends StaffApi
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = [];
  17. //添加
  18. public function addInvoice()
  19. {
  20. $params = $this->request->post();
  21. // 表单验证
  22. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  23. $this->error($result);
  24. }
  25. Db::startTrans();
  26. try {
  27. $lastId = InvoiceModel::createInvoice($params);
  28. Db::commit();
  29. } catch (Exception $e) {
  30. Db::rollback();
  31. $this->error($e->getMessage());
  32. }
  33. if ($result) {
  34. $this->success('添加发票成功',['id'=>$lastId]);
  35. }
  36. }
  37. //编辑
  38. public function editInvoice()
  39. {
  40. $params = $this->request->post();
  41. // 表单验证
  42. if (($result = $this->qingdongamsValidate($params, get_class(),'create')) !== true) {
  43. $this->error($result);
  44. }
  45. Db::startTrans();
  46. try {
  47. $result = InvoiceModel::updateInvoice($params);
  48. Db::commit();
  49. } catch (Exception $e) {
  50. Db::rollback();
  51. $this->error($e->getMessage());
  52. }
  53. if ($result) {
  54. $this->success('编辑发票成功');
  55. }
  56. }
  57. //获取列表
  58. public function getList()
  59. {
  60. $limit = input("limit/d", 10);
  61. $customer_id = input('customer_id');
  62. $contract_id = input('contract_id');
  63. $params = $this->request->post();
  64. if (isset($params['createtime']) && $params['createtime']) {//
  65. $createtime = $params['createtime'];
  66. $where['createtime'] = ['between', setTimes($createtime,'time')];
  67. }
  68. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  69. if (isset($params['type']) && $params['type']) {//分类
  70. if ($params['type'] == 1) {//我的创建
  71. $where['owner_staff_id'] = $this->auth->id;
  72. } elseif ($params['type'] == 2) {//下属创建
  73. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  74. }
  75. }
  76. if ($customer_id) {
  77. $where['customer_id'] = $customer_id;
  78. }
  79. if ($contract_id) {
  80. $where['contract_id'] = $contract_id;
  81. }
  82. $records = InvoiceModel::where($where)->with(['ownerStaff','customer','contract'])->order('id desc')->paginate($limit);
  83. $this->success('请求成功', $records);
  84. }
  85. //获取详情
  86. public function getDetail()
  87. {
  88. $id = input('id', '', 'intval');
  89. $invoice = InvoiceModel::where(['id' => $id])
  90. ->with(['ownerStaff', 'customer','contract'])->find();
  91. if (empty($invoice)) {
  92. $this->error('发票不存在');
  93. }
  94. if ($invoice['check_status'] == 0 || $invoice['check_status'] == 1) {
  95. $invoice['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::INVOICE_TYPE, $id);
  96. } else {
  97. $invoice['is_examine'] = 0;
  98. }
  99. //标记通知已读
  100. Message::setRead(Message::INVOICE_TYPE, $id, $this->auth->id);
  101. $this->success('请求成功', $invoice);
  102. }
  103. //获取编号
  104. public function getNumber()
  105. {
  106. $this->success('请求成功', ['number' => InvoiceModel::getNumber()]);
  107. }
  108. //标记为开票
  109. public function startInvoice(){
  110. $params = $this->request->post();
  111. if(!$params['id']){
  112. $this->error('参数不正确');
  113. }
  114. if(!$params['invoice_time']){
  115. $this->error('实际开票日期不能为空');
  116. }
  117. Db::startTrans();
  118. try {
  119. if($params['image']){
  120. $params['image'] = File::where(['id'=>$params['image']])->value('file_path');
  121. }
  122. $result = InvoiceModel::updateInfo($params);
  123. Db::commit();
  124. } catch (Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if ($result) {
  129. $this->success('编辑发票成功');
  130. }
  131. }
  132. }