createOrderNumber(PaymentReceipt::prefix); if(! $order_number) return [false,'工单编号生成失败!']; return [true,['order_number' => $order_number]]; } public function customerEdit($data,$user){ list($status,$msg) = $this->customerRule($data,$user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = PaymentReceipt::where('id',$data['id'])->first(); // $model->data_order_no = $data['data_order_no']; // $model->data_type = $data['data_type']; $model->type = $data['type']; $model->account = $data['account'] ?? 0; $model->pay_way = $data['pay_way'] ?? 0; // $model->amount = $data['amount'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0; $model->save(); $time = time(); $old = PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->select('file') ->get()->toArray(); $old = array_column($old,'file'); PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->update(['del_time' => $time]); $new = []; if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'file' => $value['url'], 'type' => PaymentReceiptInfo::type_one, 'name' => $value['name'], 'crt_time' => $time, ]; } $new[]= $value['url']; PaymentReceiptInfo::insert($insert); } if(! empty($data['employee_one'])){ $insert = []; foreach ($data['employee_one'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'data_id' => $value, 'type' => PaymentReceiptInfo::type_two, 'crt_time' => $time, ]; } PaymentReceiptInfo::insert($insert); } if(! empty($data['amount_list'])){ $insert = []; foreach ($data['amount_list'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'data_order_no' => $value['data_order_no'], 'data_order_type' => $data['data_type'], 'amount' => $value['amount'], 'type' => PaymentReceiptInfo::type_three, 'crt_time' => $time, ]; } PaymentReceiptInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } $this->delStorageFile($old, $new); return [true,'']; } public function customerAdd($data,$user){ list($status,$msg) = $this->customerRule($data,$user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new PaymentReceipt(); $model->order_number = $data['order_number']; // $model->data_order_no = $data['data_order_no']; $model->data_type = $data['data_type']; $model->type = $data['type']; $model->account = $data['account'] ?? 0; $model->pay_way = $data['pay_way'] ?? 0; // $model->amount = $data['amount'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->crt_id = $user['id']; $model->depart_id = $data['depart_id']; $model->top_depart_id = $data['top_depart_id']; $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0; $model->save(); $time = time(); if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'file' => $value['url'], 'type' => PaymentReceiptInfo::type_one, 'name' => $value['name'], 'crt_time' => $time, ]; } PaymentReceiptInfo::insert($insert); } if(! empty($data['employee_one'])){ $insert = []; foreach ($data['employee_one'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'data_id' => $value, 'type' => PaymentReceiptInfo::type_two, 'crt_time' => $time, ]; } PaymentReceiptInfo::insert($insert); } if(! empty($data['amount_list'])){ $insert = []; foreach ($data['amount_list'] as $value){ $insert[] = [ 'payment_receipt_id' => $model->id, 'data_order_no' => $value['data_order_no'], 'data_order_type' => $data['data_type'], 'amount' => $value['amount'], 'type' => PaymentReceiptInfo::type_three, 'crt_time' => $time, ]; } PaymentReceiptInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function customerDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first(); if(empty($booking)) return [false,'收付款单不存在或已被删除']; $booking = $booking->toArray(); if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,删除失败']; try { DB::beginTransaction(); $time = time(); $old = PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->select('file') ->get()->toArray(); $old = array_column($old,'file'); PaymentReceipt::where('id',$data['id'])->update([ 'del_time'=> $time ]); PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } $this->delStorageFile($old); return [true,'']; } public function customerDetail($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = PaymentReceipt::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'记录不存在或已被删除']; $customer = $customer->toArray(); $array = [ $customer['account'], $customer['pay_way'], ]; $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $customer['account_title'] = $basic_map[$customer['account']] ?? ""; $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? ""; $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? ""; $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? ""; $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? ""; $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): ''; $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0; $file = PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->get()->toArray(); $emp_id = []; $order_no = []; foreach ($file as $value){ if(in_array($value['type'],PaymentReceiptInfo::$man)){ $emp_id[] = $value['data_id']; } if(! empty($value['data_order_no'])) $order_no[] = $value['data_order_no']; } if($customer['data_type'] == PaymentReceipt::data_type_one){ $order = SalesOrder::where('del_time',0) ->whereIn('order_number',$order_no) ->pluck('contract_fee','order_number') ->toArray(); }else{ $order = PurchaseOrder::where('del_time',0) ->whereIn('order_number',$order_no) ->pluck('purchase_total','order_number') ->toArray(); } $infos = PaymentReceiptInfo::where('del_time',0) ->where('type',PaymentReceiptInfo::type_three) ->where('payment_receipt_id','<>',$customer['id']) ->get()->toArray(); $infos_map = []; foreach ($infos as $value){ if(isset($infos_map[$value['data_order_no']])){ $infos_map[$value['data_order_no']] += $value['amount']; }else{ $infos_map[$value['data_order_no']] = $value['amount']; } } $emp_map = Employee::whereIn('id',array_unique($emp_id)) ->where('del_time',0) ->pluck('emp_name','id') ->toArray(); $customer['file'] = $customer['employee_one'] = $customer['amount_list'] = []; foreach ($file as $value){ if($value['type'] == PaymentReceiptInfo::type_one){ $tmp = [ 'url' => $value['file'], 'name' => $value['name'], ]; $customer['file'][] = $tmp; }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){ $tt = $emp_map[$value['data_id']] ?? ''; if(! empty($tt)){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; if($value['type'] == PaymentReceiptInfo::type_two){ $customer['employee_one'][] = $tmp; } } }elseif ($value['type'] == PaymentReceiptInfo::type_three){ $tmp = [ 'data_order_no' => $value['data_order_no'], 'amount' => $value['amount'], 'total_amount' => $order[$value['data_order_no']] ?? 0, 'has_amount' => $infos_map[$value['data_order_no']] ?? 0, 'receipt_amount' => $customer['state'] == PaymentReceipt::STATE_TWO ? $value['amount'] : 0 ]; $customer['amount_list'][] = $tmp; } } return [true, $customer]; } public function customerList($data,$user){ $model = PaymentReceipt::Clear($user,$data); $model = $model->where('del_time',0) ->select('type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date') ->orderby('id', 'desc'); if(isset($data['state'])) $model->where('state', $data['state']); if(! empty($data['data_order_no'])) { $info = PaymentReceiptInfo::where('del_time',0) ->where('type',PaymentReceiptInfo::type_three) ->where('data_order_no', 'LIKE', '%'.$data['data_order_no'].'%') ->select('payment_receipt_id') ->get()->toArray(); $model->whereIn('id', array_unique(array_column($info,'payment_receipt_id'))); } if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%'); if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']); if(! empty($data['type'])) $model->where('type', $data['type']); if(! empty($data['account'])) $model->where('account',$data['account']); if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']); if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%'); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])){ $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']); $model->where('payment_receipt_date','>=',$return[0]); $model->where('payment_receipt_date','<=',$return[1]); } if(! empty($data['belong'])){ $id = (new RangeService())->paymentReceiptSearch($data); $model->whereIn('id',$id); } $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } public function customerRule(&$data, $user, $is_add = true){ if(empty($data['order_number'])) return [false,'收付款单编号不能为空']; if(empty($data['data_type'])) return [false,'单号类型不能为空']; if(empty($data['type'])) return [false,'收付款类型不能为空']; if(empty($data['amount_list']) || ! is_array($data['amount_list'])) return [false,'关联单号与回款金额不能为空']; foreach ($data['amount_list'] as $value){ if(empty($value['data_order_no'])) return [false,'关联单号不能为空']; if(empty($value['amount'])) return [false,'金额不能为空']; $res = $this->checkNumber($value['amount']); if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值']; } if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']); //所属部门 以及 顶级部门 if(empty($data['depart_id'])) { $data['depart_id'] = $this->getDepart($user); $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0; } if($is_add){ $bool = PaymentReceipt::where('del_time',0) ->where('order_number',$data['order_number']) ->exists(); if($bool) return [false,'收付款单编号已存在,请重新获取']; }else{ if(empty($data['id'])) return [false,'ID不能为空']; $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first(); if(empty($booking)) return [false,'收付款单不存在或已被删除']; $booking = $booking->toArray(); if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,编辑失败']; } return [true, '']; } public function fillData($data){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->pluck('emp_name','id') ->toArray(); $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $map = []; $info = PaymentReceiptInfo::where('del_time',0) ->where('type',PaymentReceiptInfo::type_three) ->whereIn('payment_receipt_id', array_unique(array_column($data['data'],'id'))) ->select('payment_receipt_id','data_order_no') ->get()->toArray(); foreach ($info as $value){ $map[$value['payment_receipt_id']][] = $value['data_order_no']; } 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]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $data['data'][$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? ''; $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? ''; $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? ''; $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? ''; $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? ''; $data['data'][$key]['data_order_no'] = $map[$value['id']] ?? []; } return $data; } public function getPaymentReceiptDataList($data){ $data['data_order_no'] = $data['order_number']; $info1 = PaymentReceiptInfo::where('del_time',0) ->where('data_order_no',$data['data_order_no']) ->where('type',PaymentReceiptInfo::type_three) ->get()->toArray(); $map = []; foreach ($info1 as $value){ if(isset($map[$value['payment_receipt_id']])){ $map[$value['payment_receipt_id']] += $value['amount']; }else{ $map[$value['payment_receipt_id']] = $value['amount']; } } $order = PaymentReceipt::where('del_time',0) ->whereIn('id',array_column($info1,'payment_receipt_id')) ->get()->toArray(); $emp_id = PaymentReceiptInfo::where('del_time',0) ->whereIn('payment_receipt_id',array_column($order,'id')) ->where('type',PaymentReceiptInfo::type_two) ->get()->toArray(); $info = []; if(! empty($emp_id)){ $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id'))) ->pluck('emp_name','id') ->toArray(); foreach ($emp_id as $value){ $name = $emp_map[$value['data_id']] ?? ""; if(isset($info[$value['data_id']])){ $info[$value['data_id']] .= ',' . $name; }else{ $info[$value['data_id']] = $name; } } } $finished = $not_finished = 0; foreach ($order as $key => $value){ $tmp = $info[$value['id']] ?? ''; $order[$key]['belong'] = $tmp; $order[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? ''; $order[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : ''; $amount = $map[$value['id']] ?? 0; $order[$key]['amount'] = $amount; $finished += $amount; // if($value['state'] == PaymentReceipt::STATE_TWO){ // $finished += $amount; // }else{ // $not_finished += $amount; // } } $return['receipt_amount'] = $finished; $return['not_receipt_amount'] = $not_finished; $return['all_count'] = count($order); $return['list'] = $order; return $return; } public function getPaymentReceiptDeatail($data){ $customer = PaymentReceipt::where('del_time',0) ->where('crt_time',$data['crt_time']) ->where('id',$data['id']) ->first(); if(empty($customer)) return []; $customer = $customer->toArray(); $array = [ $customer['account'], $customer['pay_way'], ]; $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $customer['account_title'] = $basic_map[$customer['account']] ?? ""; $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? ""; $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? ""; $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? ""; $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? ""; $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): ''; $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0; $file = PaymentReceiptInfo::where('del_time',0) ->where('payment_receipt_id',$data['id']) ->get()->toArray(); $emp_id = []; foreach ($file as $value){ if(in_array($value['type'],PaymentReceiptInfo::$man)){ $emp_id[] = $value['data_id']; } } $emp_map = Employee::whereIn('id',array_unique($emp_id)) ->where('del_time',0) ->pluck('emp_name','id') ->toArray(); $customer['file'] = $customer['employee_one'] = []; foreach ($file as $value){ if($value['type'] == PaymentReceiptInfo::type_one){ $tmp = [ 'url' => $value['file'], 'name' => $value['name'], ]; $customer['file'][] = $tmp; }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){ $tt = $emp_map[$value['data_id']] ?? ''; if(! empty($tt)){ $tmp = [ 'id' => $value['data_id'], 'name' => $emp_map[$value['data_id']] ?? '', ]; if($value['type'] == PaymentReceiptInfo::type_two){ $customer['employee_one'][] = $tmp; } } } } return $customer; } public function getPaymentReceiptDataCountList($data){ $data_order_no = []; foreach ($data as $value){ $data_order_no[] = $value['order_number']; } if(empty($data_order_no)) return []; $order = PaymentReceiptInfo::where('del_time',0) ->where('type',PaymentReceiptInfo::type_three) ->whereIn('data_order_no',$data_order_no) ->get()->toArray(); $return = []; foreach ($order as $value){ if(isset($return[$value['data_order_no']])){ $return[$value['data_order_no']] += $value['amount']; }else{ $return[$value['data_order_no']] = $value['amount']; } } return $return; } public function maked(){ $payment = PaymentReceipt::where('del_time',0) ->where('data_order_no','<>','') ->where('amount','>',0) ->get()->toArray(); $insert = []; foreach ($payment as $value){ $insert[] = [ 'payment_receipt_id' => $value['id'], 'crt_time' => $value['crt_time'], 'type' => PaymentReceiptInfo::type_three, 'data_order_no' => $value['data_order_no'], 'amount' => $value['amount'], 'data_order_type' => $value['type'], ]; } if(! empty($insert)) PaymentReceiptInfo::insert($insert); } }