Invoice.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *发票
  8. */
  9. class Invoice Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdongams_invoice';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. public static function getNumber()
  20. {
  21. return 'I' . date('ymd') . rand(100, 999);
  22. }
  23. public function getImageAttr($value) {
  24. if($value){
  25. return cdnurl($value, true);
  26. }
  27. return $value;
  28. }
  29. //客户
  30. public function customer() {
  31. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
  32. }
  33. //合同
  34. public function contract() {
  35. return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num');
  36. }
  37. //负责人
  38. public function ownerStaff() {
  39. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  40. }
  41. public function createStaff() {
  42. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
  43. }
  44. //添加
  45. public static function createInvoice($params) {
  46. foreach ($params as $name => $val) {
  47. if($params[$name] === ''){
  48. $params[$name]=NULL;
  49. }
  50. }
  51. $staff = Staff::info();
  52. $staff_id = $staff->id;
  53. $params['create_staff_id'] = $staff_id;
  54. $params['owner_staff_id'] = $staff->id;
  55. $params['check_status'] = 1;
  56. $flow = Flow::getsteplist(Flow::INVOICE_STATUS);
  57. $params['flow_id'] = $flow['flow_id'];
  58. $params['order_id'] = $flow['order_id'];
  59. if ($flow['status'] == 0) {//发起人自选
  60. $params['flow_staff_ids'] = trim($params['flow_staff_ids']??'');
  61. } else {
  62. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  63. }
  64. $Model = new self;
  65. $result = $Model->allowField(true)->save($params);
  66. if (false === $result) {
  67. // 验证失败 输出错误信息
  68. throw new Exception($Model->getError());
  69. }
  70. $lastId= $Model->getLastInsID();
  71. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  72. ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE,$lastId, $staff_id);
  73. return true;
  74. }
  75. //编辑
  76. public static function updateInvoice($params) {
  77. $params['check_status'] = 1;
  78. $flow = Flow::getsteplist(Flow::INVOICE_STATUS);
  79. $params['flow_id'] = $flow['flow_id'];
  80. $params['order_id'] = $flow['order_id'];
  81. if ($flow['status'] == 0) {//发起人自选
  82. if (empty($params['flow_staff_ids'])) {
  83. throw new Exception('审批人必须选择');
  84. }
  85. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  86. } else {
  87. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  88. }
  89. $Model = new self;
  90. // 调用当前模型对应的User验证器类进行数据验证
  91. $result = $Model->save($params,['id'=>$params['id']]);
  92. if (false === $result) {
  93. // 验证失败 输出错误信息
  94. throw new Exception($Model->getError());
  95. }
  96. if ($flow['status'] == 1) {//固定审批
  97. //发送审批通知
  98. Flow::sendStepRecord($flow,Flow::INVOICE_STATUS, $params['id']);
  99. } else {//发起人自选 依次审批
  100. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  101. if ($staff_id) {
  102. ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE, $params['id'], $staff_id);
  103. }
  104. }
  105. return true;
  106. }
  107. //标记开票
  108. public static function updateInfo($params){
  109. $Model = new self;
  110. // 调用当前模型对应的User验证器类进行数据验证
  111. $result = $Model->save($params,['id'=>$params['id']]);
  112. if (false === $result) {
  113. // 验证失败 输出错误信息
  114. throw new Exception($Model->getError());
  115. }
  116. $contractId = $Model->where(['id'=>$params['id']])->value('contract_id');
  117. if($contractId){
  118. $file ='';
  119. if($params['image']){
  120. $file = File::where(['file_path'=>$params['image']])->value('id');
  121. }
  122. $ret = array(
  123. 'invoice_date'=>$params['invoice_time'],
  124. 'invoice_logistics_number'=>$params['logistics'],
  125. 'invoice_file_ids'=>$file,
  126. );
  127. Contract::where(['id'=>$contractId])->update($ret);
  128. }
  129. return true;
  130. }
  131. }