12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Employee;
- use App\Model\PaymentReceipt;
- use App\Model\PaymentReceiptInfo;
- use App\Model\PurchaseOrder;
- use App\Model\PurchaseOrderSpecial;
- use App\Model\ReturnExchangeOrder;
- use App\Model\SalesOrder;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- class PaymentReceiptService extends Service
- {
- public function paymentReceiptGet($data,$user){
- $order_number = (new OrderNoService())->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,
- ];
- if(in_array($value['url'], $old)) {
- foreach ($old as $o_k => $o_v){
- if($o_v == $value['url']) unset($old[$o_k]);
- }
- }else{
- $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_type' => $data['type'],
- '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()];
- }
- if(! empty($data['check'])) {
- list($status,$msg) = (new CheckService())->checkAll([
- "id" => $data['id'],
- "order_number" => $data['order_number'],
- "opt_case" => CheckService::ten,
- "menu_id" => $data['menu_id']
- ],$user);
- // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
- }
- return [true, ['file' => ['new' => $new, 'old' => $old]]];
- }
- 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();
- $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,
- ];
- if(! empty($value['url'])) $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_type' => $data['type'],
- '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();
- if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
- return [false, '收付款单编号已存在,请重新获取!'];
- }
- return [false,$exception->getMessage()];
- }
- if(! empty($data['check'])) {
- list($status,$msg) = (new CheckService())->checkAll([
- "id" => $model->id,
- "order_number" => $data['order_number'],
- "opt_case" => CheckService::ten,
- "menu_id" => $data['menu_id']
- ],$user);
- // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
- }
- return [true, ['file' => ['new' => $new]]];
- }
- 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()];
- }
- return [true, ['file' => ['old' => $old]]];
- }
- public function customerDetail($data){
- if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
- if(! empty($data['id'])){
- $customer = PaymentReceipt::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- }else{
- $customer = PaymentReceipt::where('del_time',0)
- ->where('order_number',$data['order_number'])
- ->first();
- $data['id'] = empty($customer->id) ? 0 : $customer->id;
- }
- 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['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']): '';
- //订单状态数据组织
- $state_array = $this->getStateMake([$customer]);
- $customer['state_title'] = $this->makeState($customer,$state_array);
- $file = PaymentReceiptInfo::where('del_time',0)
- ->whereIn('type',[PaymentReceiptInfo::type_one,PaymentReceiptInfo::type_two])
- ->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))
- ->pluck('emp_name','id')
- ->toArray();
- $customer['file'] = $customer['employee_one'] = [];
- $fileUploadService = new FileUploadService();
- foreach ($file as $value){
- if($value['type'] == PaymentReceiptInfo::type_one){
- $tmp = [
- 'url' => $value['file'],
- 'name' => $value['name'],
- 'show_url' => $fileUploadService->getFileShow($value['file']),
- ];
- $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;
- }
- }
- }
- }
- //组织金额
- $customer['amount_list'] = $this->makeDetail($customer);
- return [true, $customer];
- }
- public function makeDetail($data){
- $return = [];
- $info = PaymentReceiptInfo::where('del_time',0)
- ->where('type',PaymentReceiptInfo::type_three)
- ->where('payment_receipt_id', $data['id'])
- ->select('payment_receipt_id','data_order_type','data_type','amount','data_order_no')
- ->get()->toArray();
- $order_no = array_column($info,'data_order_no');
- //退换货金额
- if($data['type'] == PaymentReceipt::type_one){
- if($data['data_type'] == PaymentReceipt::data_type_one){
- $order = SalesOrder::where('del_time',0)
- ->whereIn('order_number',$order_no)
- ->pluck('contract_fee','order_number')
- ->toArray();
- $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order));
- }else{
- $order = PurchaseOrder::where('del_time',0)
- ->whereIn('order_number',$order_no)
- ->pluck('purchase_total','order_number')
- ->toArray();
- $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order), ReturnExchangeOrder::Order_type2);
- }
- }
- //除自己这个收付款单外 所有订单已填的金额
- $infos = PaymentReceiptInfo::where('del_time',0)
- ->where('type',PaymentReceiptInfo::type_three)
- ->where('data_type',$data['type'])
- ->where('payment_receipt_id','<>',$data['id'])
- ->whereIn('data_order_no',$order_no)
- ->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'];
- }
- }
- //收款金额(审核后)
- $info_one_array = [];
- if($data['type'] == PaymentReceipt::type_three){
- $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.state',PaymentReceipt::STATE_TWO)
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->where('a.data_order_type',PaymentReceipt::data_type_one)
- ->whereIn('a.data_order_no',$order_no)
- ->where('a.data_type',PaymentReceipt::type_one)
- ->select('a.data_order_no','a.amount')
- ->get()->toArray();
- $info_one_array = [];
- foreach ($info_one as $value){
- if(isset($info_one_array[$value['data_order_no']])){
- $info_one_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_one_array[$value['data_order_no']] = $value['amount'];
- }
- }
- }
- foreach ($info as $value){
- $total = $tmp_receipt = 0;
- if($data['type'] == PaymentReceipt::type_one){
- //收款 = 总金额 - 已收 - 退货退款
- //总金额
- $total_amount = $order[$value['data_order_no']] ?? 0;
- //已收
- $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
- //退货退款
- $tmp_return = $getDifferentAmountALL[$value['data_order_no']] ?? 0;
- //收款
- $total = bcsub(bcsub($total_amount, $tmp_receipt,2), $tmp_return, 2);
- }elseif($data['type'] == PaymentReceipt::type_three){
- //红冲 = 审核后的收款金额 - 已红冲
- //总金额 收款金额(审核后)
- $total_amount = $info_one_array[$value['data_order_no']] ?? 0;
- //已红冲
- $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
- //红冲
- $total = bcsub($total_amount, $tmp_receipt, 2);
- }
- $tmp = [
- 'data_order_no' => $value['data_order_no'],
- 'total_amount' => $total,//总共能填的金额
- 'has_amount' => $tmp_receipt,//已经填的金额
- 'amount' => $value['amount'],//本单金额
- 'receipt_amount' => $value['amount'],//本单金额
- ];
- $return[] = $tmp;
- }
- return $return;
- }
- public function customerCommon($data,$user,$field = []){
- if(empty($field)){
- $field = ['type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date'];
- }
- $model = PaymentReceipt::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->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);
- }
- if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
- $model->where('crt_time','>=',$data['wx_crt_time'][0]);
- $model->where('crt_time','<=',$data['wx_crt_time'][1]);
- }
- return $model;
- }
- public function customerList($data,$user){
- $model = $this->customerCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list, $data);
- $count = $this->countData("crt_time", $data, $user);
- $list['today'] = $count[0] ?? 0;
- $list['yesterday'] = $count[1] ?? 0;
- 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(! isset(PaymentReceipt::$data_type[$data['data_type']])) return [false, '关联单号类型不存在'];
- if(empty($data['type'])) return [false,'收付款类型不能为空'];
- if(! isset(PaymentReceipt::$model_type[$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,'金额不能为空'];
- if(floatval($value['amount']) == 0) return [false, '金额请输入大于0的数值'];
- $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(! in_array($booking['state'],[PaymentReceipt::STATE_ZERO,PaymentReceipt::State_minus_one])) return [false,'请确认收付款单状态,编辑失败'];
- }
- list($status,$msg) = $this->checkRule($data);
- if(! $status) return [false, $msg];
- return [true, ''];
- }
- public function checkRule($data){
- $payment_receipt_id = $data['id'] ?? 0;
- $map = [];
- foreach ($data['amount_list'] as $value){
- $map[$value['data_order_no']] = $value['amount'];
- }
- $search = array_keys($map);
- if($data['data_type'] == PaymentReceipt::data_type_one){ //合同逻辑
- //总金额
- $result = SalesOrder::where('del_time',0)
- ->whereIn('order_number',$search)
- ->select('id','order_number','contract_fee as total_amount')
- ->get()->toArray();
- $result_map = array_column($result,null,'order_number');
- if(count($search) != count($result)) {
- foreach ($search as $value){
- if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
- }
- }
- $result_map = array_column($result,'order_number','id');
- //收付款金额
- $info = PaymentReceiptInfo::where('del_time',0)
- ->where('type',PaymentReceiptInfo::type_three)
- ->where('data_order_type',PaymentReceipt::data_type_one)
- ->whereIn('data_order_no',$search)
- // ->where('data_type',$data['type'])
- ->select('payment_receipt_id','data_order_no','amount','data_type')
- ->get()->toArray();
- $info_array = $red = [];
- foreach ($info as $value){
- if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
- if($value['data_type'] == PaymentReceipt::type_one){
- if(isset($info_array[$value['data_order_no']])){
- $info_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_array[$value['data_order_no']] = $value['amount'];
- }
- }elseif($value['data_type'] == PaymentReceipt::type_three){
- if(isset($red[$value['data_order_no']])){
- $red[$value['data_order_no']] += $value['amount'];
- }else{
- $red[$value['data_order_no']] = $value['amount'];
- }
- }
- }
- //退货退款金额
- $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'));
- if($data['type'] == PaymentReceipt::type_one){
- // order_number => amount
- $return_exchange_array = [];
- foreach ($result_map as $key => $value){
- if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
- }
- foreach ($result as $value){
- //收款 = 总金额 - (已收 - 红冲) - 退货退款
- //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
- $tmp_receipt = $info_array[$value['order_number']] ?? 0;
- $tmp_red = $red[$value['order_number']] ?? 0;
- $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
- $max = bcsub(bcsub($value['total_amount'], bcsub($tmp_receipt,$tmp_red,2),2), $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }elseif($data['type'] == PaymentReceipt::type_three){
- //收款金额(审核后)
- $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.state',PaymentReceipt::STATE_TWO)
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->where('a.data_order_type',PaymentReceipt::data_type_one)
- ->whereIn('a.data_order_no',$search)
- ->where('a.data_type',PaymentReceipt::type_one)
- ->select('a.payment_receipt_id','a.data_order_no','a.amount')
- ->get()->toArray();
- $info_one_array = [];
- foreach ($info_one as $value){
- if(isset($info_one_array[$value['data_order_no']])){
- $info_one_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_one_array[$value['data_order_no']] = $value['amount'];
- }
- }
- foreach ($result as $value){
- //红冲 = 审核后的收款单金额 - 已红冲
- $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
- $tmp_return = $red[$value['order_number']] ?? 0;
- $max = bcsub($tmp_receipt, $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }
- }elseif ($data['data_type'] == PaymentReceipt::data_type_two){//采购逻辑
- //总金额
- $result = PurchaseOrder::where('del_time',0)
- ->whereIn('order_number',$search)
- ->select('id','order_number','purchase_total as total_amount')
- ->get()->toArray();
- $result_map = array_column($result,null,'order_number');
- if(count($search) != count($result)) {
- foreach ($search as $value){
- if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
- }
- }
- $result_map = array_column($result,'order_number','id');
- //收付款金额
- $info = PaymentReceiptInfo::where('del_time',0)
- ->where('type',PaymentReceiptInfo::type_three)
- ->where('data_order_type',PaymentReceipt::data_type_two)
- ->whereIn('data_order_no',$search)
- // ->where('data_type',$data['type'])
- ->select('payment_receipt_id','data_order_no','amount','data_type')
- ->get()->toArray();
- $info_array = $red = [];
- foreach ($info as $value){
- if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
- if($value['data_type'] == PaymentReceipt::type_one){
- if(isset($info_array[$value['data_order_no']])){
- $info_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_array[$value['data_order_no']] = $value['amount'];
- }
- }elseif($value['data_type'] == PaymentReceipt::type_three){
- if(isset($red[$value['data_order_no']])){
- $red[$value['data_order_no']] += $value['amount'];
- }else{
- $red[$value['data_order_no']] = $value['amount'];
- }
- }
- }
- //退货退款金额
- $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'),1);
- if($data['type'] == PaymentReceipt::type_one){
- $return_exchange_array = [];
- foreach ($result_map as $key => $value){
- // order_number => amount
- if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
- }
- foreach ($result as $value){
- //收款 = 总金额 - (已收 - 红冲) - 退货退款
- //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
- $tmp_receipt = $info_array[$value['order_number']] ?? 0;
- $tmp_red = $red[$value['order_number']] ?? 0;
- $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
- $max = bcsub(bcsub($value['total_amount'], bcsub($tmp_receipt,$tmp_red,2),2), $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }elseif($data['type'] == PaymentReceipt::type_three){
- //收款金额(审核后)
- $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.state',PaymentReceipt::STATE_TWO)
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->where('a.data_order_type',PaymentReceipt::data_type_one)
- ->whereIn('a.data_order_no',$search)
- ->where('a.data_type',PaymentReceipt::type_one)
- ->select('a.payment_receipt_id','a.data_order_no','a.amount')
- ->get()->toArray();
- $info_one_array = [];
- foreach ($info_one as $value){
- if(isset($info_one_array[$value['data_order_no']])){
- $info_one_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_one_array[$value['data_order_no']] = $value['amount'];
- }
- }
- foreach ($result as $value){
- //红冲 = 审核后的收款单金额 - 已红冲
- $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
- $tmp_return = $red[$value['order_number']] ?? 0;
- $max = bcsub($tmp_receipt, $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }
- }elseif ($data['data_type'] == PaymentReceipt::data_type_three){//虚拟采购
- //总金额
- $result = PurchaseOrderSpecial::where('del_time',0)
- ->whereIn('order_number',$search)
- ->select('id','order_number','purchase_total as total_amount','other_fee','sales_order_id')
- ->get()->toArray();
- $result_map = array_column($result,null,'order_number');
- if(count($search) != count($result)) {
- foreach ($search as $value){
- if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
- }
- }
- $result_map = array_column($result,'order_number','sales_order_id');
- //收付款金额
- $info = PaymentReceiptInfo::where('del_time',0)
- ->where('type',PaymentReceiptInfo::type_three)
- ->where('data_order_type',PaymentReceipt::data_type_three)
- ->whereIn('data_order_no',$search)
- // ->where('data_type',$data['type'])
- ->select('payment_receipt_id','data_order_no','amount','data_type')
- ->get()->toArray();
- $info_array = $red = [];
- foreach ($info as $value){
- if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
- if($value['data_type'] == PaymentReceipt::type_one){
- if(isset($info_array[$value['data_order_no']])){
- $info_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_array[$value['data_order_no']] = $value['amount'];
- }
- }elseif($value['data_type'] == PaymentReceipt::type_three){
- if(isset($red[$value['data_order_no']])){
- $red[$value['data_order_no']] += $value['amount'];
- }else{
- $red[$value['data_order_no']] = $value['amount'];
- }
- }
- }
- //退货退款金额
- $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'sales_order_id'));
- if($data['type'] == PaymentReceipt::type_one){
- // order_number => amount
- $return_exchange_array = [];
- foreach ($result_map as $key => $value){
- if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
- }
- foreach ($result as $value){
- //收款 = 总金额(订单金额 + 其他费用) - (已收 - 红冲) - 退换货
- $tmp_receipt = $info_array[$value['order_number']] ?? 0;
- $tmp_red = $red[$value['order_number']] ?? 0;
- $total = bcadd($value['total_amount'],$value['other_fee'],2);
- $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
- $max = bcsub($total, bcsub($tmp_receipt,$tmp_red,2), 2);
- $max = bcsub($max, $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }elseif($data['type'] == PaymentReceipt::type_three){
- //收款金额(审核后)
- $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.state',PaymentReceipt::STATE_TWO)
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->where('a.data_order_type',PaymentReceipt::data_type_three)
- ->whereIn('a.data_order_no',$search)
- ->where('a.data_type',PaymentReceipt::type_one)
- ->select('a.payment_receipt_id','a.data_order_no','a.amount')
- ->get()->toArray();
- $info_one_array = [];
- foreach ($info_one as $value){
- if(isset($info_one_array[$value['data_order_no']])){
- $info_one_array[$value['data_order_no']] += $value['amount'];
- }else{
- $info_one_array[$value['data_order_no']] = $value['amount'];
- }
- }
- foreach ($result as $value){
- //红冲 = 审核后的收款单金额 - 已红冲
- $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
- $tmp_return = $red[$value['order_number']] ?? 0;
- $max = bcsub($tmp_receipt, $tmp_return, 2);
- if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
- }
- }
- }
- return [true, ''];
- }
- public function fillData($data, $ergs){
- 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'];
- }
- //订单状态数据组织
- $state_array = $this->getStateMake($data['data']);
- 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'] = $this->makeState($data['data'][$key], $state_array);
- $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 countData($column = "", $data, $user){
- if(empty($column) || empty($data['from_wx'])) return [0, 0];
- // 获取今天的开始和结束时间戳
- $todayStart = Carbon::today()->startOfDay()->timestamp;
- $todayEnd = Carbon::today()->endOfDay()->timestamp;
- // 获取昨天的开始和结束时间戳
- $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
- $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
- $data['wx_' . $column] = [$todayStart, $todayEnd];
- $model = $this->customerCommon($data, $user);
- // 查询今天的数据条数
- $todayCount = $model->count();
- $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
- $model = $this->customerCommon($data, $user);
- // 查询昨天的数据条数
- $yesterdayCount = $model->count();
- return [$todayCount, $yesterdayCount];
- }
- public function getStateMake($data){
- if(empty($data)) return [];
- $order_no = [];
- foreach ($data as $value){
- if(! in_array($value['state'], [PaymentReceipt::State_minus_one,PaymentReceipt::STATE_ONE])) continue;
- $order_no[] = $value['order_number'];
- }
- return (new OaService())->getOaTeamDetailList($order_no);
- }
- public function makeState($value, $state_array){
- if(! empty($state_array[$value['order_number']])){
- $return = $state_array[$value['order_number']];
- if($value['state'] == PaymentReceipt::State_minus_one){
- $state = "驳回:" . $return;
- }else{
- $state = "待" . $return . "审核";
- }
- }elseif($value['state'] == PaymentReceipt::STATE_ZERO){
- $state = "待" . $value['crt_name'] . "提交";
- }else{
- $state = PaymentReceipt::$name[$value['state']] ?? '';
- }
- return $state;
- }
- //详情里
- public function getPaymentReceiptDataList($data,$type){
- $data['data_order_no'] = $data['order_number'];
- $info_array = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->where('a.data_order_type',$type)
- ->where('a.data_order_no',$data['data_order_no'])
- ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date','b.crt_time','b.order_number')
- ->get()->toArray();
- $emp_id = PaymentReceiptInfo::where('del_time',0)
- ->whereIn('payment_receipt_id',array_column($info_array,'payment_receipt_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['payment_receipt_id']])){
- $info[$value['payment_receipt_id']] .= ',' . $name;
- }else{
- $info[$value['payment_receipt_id']] = $name;
- }
- }
- }
- //四个金额类型
- $one = $two = $three = $four = $not_confirm_receipt_amount = 0;
- foreach ($info_array as $key => $value){
- //归属人
- $info_array[$key]['belong'] = $info[$value['payment_receipt_id']] ?? '';
- $info_array[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
- $info_array[$key]['type_title'] = PaymentReceipt::$model_type[$value['data_type']] ?? '';
- $info_array[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
- if($value['data_type'] == PaymentReceipt::type_one) $not_confirm_receipt_amount += $value['amount'];
- if($value['data_type'] == PaymentReceipt::type_one && $value['state'] == PaymentReceipt::STATE_TWO){
- $one += $value['amount'];
- }elseif ($value['data_type'] == PaymentReceipt::type_three && $value['state'] == PaymentReceipt::STATE_TWO){
- $three += $value['amount'];
- }
- }
- $return['receipt_amount'] = bcsub($one, $three, 2); // 已回款金额
- $return['not_receipt_amount'] = 0;
- $return['red_amount'] = $three;// 已红冲金额
- $return['bad_amount'] = $four;
- $return['all_count'] = count($info_array);
- $return['not_confirm_receipt_amount'] = $not_confirm_receipt_amount;
- $return['list'] = $info_array;
- return $return;
- }
- //列表里 默认:(收款)
- public function getPaymentReceiptDataCountList($data){
- $data_order_no = $data;
- if(empty($data_order_no)) return [];
- $order = PaymentReceiptInfo::from('payment_receipt_info as a')
- ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
- ->where('b.del_time',0)
- ->where('a.del_time',0)
- ->where('a.type',PaymentReceiptInfo::type_three)
- ->whereIn('a.data_order_no',$data_order_no)
- ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date')
- ->get()->toArray();
- // $order = PaymentReceiptInfo::where('del_time',0)
- // ->where('type',PaymentReceiptInfo::type_three)
- // ->whereIn('data_order_no',$data_order_no)
- // ->get()->toArray();
- // 所有状态都统计 审核成功的统计
- $return = $return1 = [];
- foreach ($order as $value){
- $key = $value['data_order_no'] . $value['data_type'];
- if(isset($return[$key])){
- $return[$key] += $value['amount'];
- }else{
- $return[$key] = $value['amount'];
- }
- if($value['state'] == PaymentReceipt::STATE_TWO){
- if(isset($return1[$key])){
- $return1[$key] += $value['amount'];
- }else{
- $return1[$key] = $value['amount'];
- }
- }
- }
- return [$return, $return1];
- }
- public function maked(){
- $payment = PaymentReceipt::where('del_time',0)
- ->where('data_order_no','LIKE', '%'."T9SO.".'%')
- ->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'],
- 'data_type' => PaymentReceipt::type_one,
- ];
- }
- if(! empty($insert)) PaymentReceiptInfo::insert($insert);
- }
- public function checkForEdit($data_order_no = ""){
- if(empty($data_order_no)) return [false, '校验参数异常'];
- $bool = PaymentReceipt::where('data_order_no', $data_order_no)
- ->where('del_time',0)
- ->exists();
- if(! $bool) return [true, ''];
- return [false, "单号:" . $data_order_no . "已建收付款单,编辑操作有可能改变单据原始总金额导致收付款金额错乱,请先删除收付款单据!"];
- }
- }
|