ExpenseClaimsService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CalendarDetails;
  4. use App\Model\Device;
  5. use App\Model\Employee;
  6. use App\Model\ExpenseClaims;
  7. use App\Model\ExpenseClaimsDetails;
  8. use App\Model\Item;
  9. use App\Model\ItemDetails;
  10. use App\Model\RuleSet;
  11. use App\Model\RuleSetDetails;
  12. use Illuminate\Support\Facades\DB;
  13. class ExpenseClaimsService extends Service
  14. {
  15. public function expenseClaimsList($data,$user){
  16. $model = $this->expenseClaimsSetCommon($data, $user);
  17. $list = $this->limit($model,'',$data);
  18. $list = $this->fillData($list);
  19. return [true, $list];
  20. }
  21. private function expenseClaimsSetCommon($data,$user, $field = []){
  22. if(empty($field)) $field = ExpenseClaims::$field;
  23. $data['top_depart_id'] = $user['top_depart_id'];
  24. $model = ExpenseClaims::Clear($user,$data);
  25. $model = $model->where('del_time',0)
  26. ->select($field)
  27. ->orderby('month', 'desc');
  28. if(! empty($data['month'])) $model->where('month', $data['month']);
  29. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  30. return $model;
  31. }
  32. public function fillData($data){
  33. if(empty($data['data'])) return $data;
  34. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  35. foreach ($data['data'] as $key => $value){
  36. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  37. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  38. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  39. }
  40. return $data;
  41. }
  42. public function expenseClaimsAdd($data,$user){
  43. list($status,$msg) = $this->expenseClaimsRule($data, $user);
  44. if(!$status) return [$status,$msg];
  45. //费用报销结构
  46. try {
  47. DB::beginTransaction();
  48. $month = strtotime(date("Ymd", strtotime($data['month'])));
  49. $model = new ExpenseClaims();
  50. $model->code = $this->generateBillNo([
  51. 'top_depart_id' => $user['top_depart_id'],
  52. 'type' => ExpenseClaims::Order_type,
  53. 'period' => $month
  54. ]);
  55. $model->month = $month;
  56. $model->crt_id = $user['id'];
  57. $model->top_depart_id = $user['top_depart_id'];
  58. $model->save();
  59. $data['top_depart_id'] = $user['top_depart_id'];
  60. $this->saveDetail($model->id, time(), $data);
  61. DB::commit();
  62. }catch (\Exception $exception){
  63. DB::rollBack();
  64. return [false,$exception->getLine()."_".$exception->getMessage()];
  65. }
  66. return [true, ''];
  67. }
  68. private function saveDetail($id, $time, $data){
  69. if(! empty($data['details'])){
  70. $unit = [];
  71. foreach ($data['details'] as $value){
  72. $unit[] = [
  73. 'expense_claims_id' => $id,
  74. 'item_id' => $value['item_id'],
  75. 'employee_id' => $value['employee_id'],
  76. 'fee_id' => $value['fee_id'],
  77. 'amount' => $value['amount'],
  78. 'remark' => $value['remark'],
  79. 'claim_date' => $value['claim_date'],
  80. 'entrust_type' => $value['entrust_type'],
  81. 'expense_type' => $value['expense_type'],
  82. 'expense_attachments' => $value['expense_attachments'],
  83. 'voucher_no' => $data['voucher_no']??"",
  84. 'file_url' => $data['file_url']??"",
  85. 'file_name' => $data['file_name']??"",
  86. 'top_depart_id' => $data['top_depart_id'],
  87. 'crt_time' => $time,
  88. ];
  89. }
  90. if(!empty($unit)) ExpenseClaimsDetails::insert($unit);
  91. }
  92. }
  93. private function expenseClaimsRule($data,$user){
  94. if(!isset($data['month'])) return [false,"月份必传"];
  95. $monthStr = $data['month']; // 假设是 "2026-03"
  96. $monthStart = strtotime($monthStr); // 2026-03-01 00:00:00
  97. // 获取该月最后一秒:下个月 1 号减去 1 秒
  98. $monthEnd = strtotime("$monthStr +1 month") - 1;
  99. foreach ($data['details'] as $index => $item) {
  100. if (!isset($item['claim_date'])) {
  101. return [false, "第" . ($index + 1) . "项报销日期缺失"];
  102. }
  103. // 将报销日期转换为时间戳
  104. $claimTime = strtotime($item['claim_date']);
  105. // 判断:claim_date 必须早于 month
  106. // 如果 claim_date 是 2026-02-28,而 month 是 2026-03-01,则校验通过
  107. if ($claimTime < $monthStart || $claimTime > $monthEnd) {
  108. return [false, "第" . ($index + 1) . "项报销日期必须早于当前结算月份(" . $data['month'] . ")"];
  109. }
  110. }
  111. return [true,""];
  112. }
  113. public function expenseClaimsEdit($data,$user){
  114. list($status,$msg) = $this->expenseClaimsRule($data, $user, false);
  115. if(!$status) return [$status,$msg];
  116. try {
  117. DB::beginTransaction();
  118. $month = strtotime(date("Ymd", strtotime($data['month'])));
  119. $model = ExpenseClaims::where('id',$data['id'])->first();
  120. $model->month = $month;
  121. $model->crt_id = $user['id'];
  122. $model->save();
  123. $time = time();
  124. ExpenseClaimsDetails::where('del_time',0)
  125. ->where('expense_claims_id', $model->id)
  126. ->update(['del_time' => $time]);
  127. $data['top_depart_id'] = $user['top_depart_id'];
  128. $this->saveDetail($model->id, $time, $data);
  129. DB::commit();
  130. }catch (\Exception $exception){
  131. DB::rollBack();
  132. return [false,$exception->getMessage()];
  133. }
  134. return [true, ''];
  135. }
  136. public function expenseClaimsDel($data){
  137. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  138. try {
  139. DB::beginTransaction();
  140. $time = time();
  141. ExpenseClaims::where('del_time',0)
  142. ->whereIn('id',$data['id'])
  143. ->update(['del_time' => $time]);
  144. ExpenseClaimsDetails::where('del_time',0)
  145. ->whereIn('expense_claims_id', $data['id'])
  146. ->update(['del_time' => $time]);
  147. DB::commit();
  148. }catch (\Exception $exception){
  149. DB::rollBack();
  150. return [false,$exception->getMessage()];
  151. }
  152. return [true, ''];
  153. }
  154. public function expenseClaimsDetail($data, $user){
  155. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  156. $customer = ExpenseClaims::where('del_time',0)
  157. ->where('id',$data['id'])
  158. ->first();
  159. if(empty($customer)) return [false,'单据不存在或已被删除'];
  160. $customer = $customer->toArray();
  161. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  162. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  163. $details = $this->getDetail($data['id']);
  164. $customer = array_merge($customer, $details);
  165. return [true, $customer];
  166. }
  167. private function getDetail($id){
  168. $data = ExpenseClaimsDetails::where('del_time',0)
  169. ->where('expense_claims_id', $id)
  170. ->select('*')
  171. ->get()->toArray();
  172. return $data;
  173. }
  174. }