| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Service;
- use App\Model\CalendarDetails;
- use App\Model\Device;
- use App\Model\Employee;
- use App\Model\ExpenseClaims;
- use App\Model\ExpenseClaimsDetails;
- use App\Model\Item;
- use App\Model\ItemDetails;
- use App\Model\RuleSet;
- use App\Model\RuleSetDetails;
- use Illuminate\Support\Facades\DB;
- class ExpenseClaimsService extends Service
- {
- public function expenseClaimsList($data,$user){
- $model = $this->expenseClaimsSetCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- private function expenseClaimsSetCommon($data,$user, $field = []){
- if(empty($field)) $field = ExpenseClaims::$field;
- $data['top_depart_id'] = $user['top_depart_id'];
- $model = ExpenseClaims::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('month', 'desc');
- if(! empty($data['month'])) $model->where('month', $data['month']);
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- return $model;
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
- }
- return $data;
- }
- public function expenseClaimsAdd($data,$user){
- list($status,$msg) = $this->expenseClaimsRule($data, $user);
- if(!$status) return [$status,$msg];
- //费用报销结构
- try {
- DB::beginTransaction();
- $month = strtotime(date("Ymd", strtotime($data['month'])));
- $model = new ExpenseClaims();
- $model->code = $this->generateBillNo([
- 'top_depart_id' => $user['top_depart_id'],
- 'type' => ExpenseClaims::Order_type,
- 'period' => $month
- ]);
- $model->month = $month;
- $model->crt_id = $user['id'];
- $model->top_depart_id = $user['top_depart_id'];
- $model->save();
- $data['top_depart_id'] = $user['top_depart_id'];
- $this->saveDetail($model->id, time(), $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getLine()."_".$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail($id, $time, $data){
- if(! empty($data['details'])){
- $unit = [];
- foreach ($data['details'] as $value){
- $unit[] = [
- 'expense_claims_id' => $id,
- 'item_id' => $value['item_id'],
- 'employee_id' => $value['employee_id'],
- 'fee_id' => $value['fee_id'],
- 'amount' => $value['amount'],
- 'remark' => $value['remark'],
- 'claim_date' => $value['claim_date'],
- 'entrust_type' => $value['entrust_type'],
- 'expense_type' => $value['expense_type'],
- // 'expense_attachments' => $value['expense_attachments'],
- 'voucher_no' => $data['voucher_no']??"",
- 'file_url' => $data['file_url']??"",
- 'file_name' => $data['file_name']??"",
- 'top_depart_id' => $data['top_depart_id'],
- 'crt_time' => $time,
- ];
- }
- if(!empty($unit)) ExpenseClaimsDetails::insert($unit);
- }
- }
- private function expenseClaimsRule($data,$user){
- if(!isset($data['month'])) return [false,"月份必传"];
- $monthStr = $data['month']; // 假设是 "2026-03"
- $monthStart = strtotime($monthStr); // 2026-03-01 00:00:00
- // 获取该月最后一秒:下个月 1 号减去 1 秒
- $monthEnd = strtotime("$monthStr +1 month") - 1;
- foreach ($data['details'] as $index => $item) {
- if (!isset($item['claim_date'])) {
- return [false, "第" . ($index + 1) . "项报销日期缺失"];
- }
- // 将报销日期转换为时间戳
- $claimTime = strtotime($item['claim_date']);
- // 判断:claim_date 必须早于 month
- // 如果 claim_date 是 2026-02-28,而 month 是 2026-03-01,则校验通过
- if ($claimTime < $monthStart || $claimTime > $monthEnd) {
- return [false, "第" . ($index + 1) . "项报销日期必须在当前月份内(" . $data['month'] . ")"];
- }
- }
- return [true,""];
- }
- public function expenseClaimsEdit($data,$user){
- list($status,$msg) = $this->expenseClaimsRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $month = strtotime(date("Ymd", strtotime($data['month'])));
- $model = ExpenseClaims::where('id',$data['id'])->first();
- $model->month = $month;
- $model->crt_id = $user['id'];
- $model->save();
- $time = time();
- ExpenseClaimsDetails::where('del_time',0)
- ->where('expense_claims_id', $model->id)
- ->update(['del_time' => $time]);
- $data['top_depart_id'] = $user['top_depart_id'];
- $this->saveDetail($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function expenseClaimsDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- ExpenseClaims::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- ExpenseClaimsDetails::where('del_time',0)
- ->whereIn('expense_claims_id', $data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function expenseClaimsDetail($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = ExpenseClaims::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'单据不存在或已被删除'];
- $customer = $customer->toArray();
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- $details = $this->getDetail($data['id']);
- $customer["details"] = $details;
- return [true, $customer];
- }
- private function getDetail($id){
- $data = ExpenseClaimsDetails::where('del_time',0)
- ->where('expense_claims_id', $id)
- ->select('*', DB::raw("FROM_UNIXTIME(claim_date, '%Y-%m-%d') as claim_date"))
- ->get()->toArray();
- return $data;
- }
- }
|