| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864 | 
							- <?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\ReturnExchangeOrder;
 
- use App\Model\SalesOrder;
 
- 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 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(! in_array($booking['state'],[PaymentReceipt::STATE_ZERO,PaymentReceipt::State_minus_one])) return [false,'请确认收付款单状态,编辑失败'];
 
-         }
 
- //        list($status,$msg) = $this->limitingSendRequestBackgExpire("paymentReceipt" . $data['order_number']);
 
- //        if(! $status) return [false, $msg];
 
-         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();
 
-             if(count($search) != count($result)) {
 
-                 foreach ($result as $value){
 
-                     if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
 
-                 }
 
-             }
 
-             $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')
 
-                 ->get()->toArray();
 
-             $info_array = [];
 
-             foreach ($info as $value){
 
-                 if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
 
-                 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'];
 
-                 }
 
-             }
 
-             //退货退款金额
 
-             $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'));
 
-             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_return = $return_exchange_array[$value['order_number']] ?? 0;
 
-                     $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,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 = $info_array[$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();
 
-             if(count($search) != count($result)) {
 
-                 foreach ($result as $value){
 
-                     if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
 
-                 }
 
-             }
 
-             $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')
 
-                 ->get()->toArray();
 
-             $info_array = [];
 
-             foreach ($info as $value){
 
-                 if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
 
-                 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'];
 
-                 }
 
-             }
 
-             //退货退款金额
 
-             $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_return = $return_exchange_array[$value['order_number']] ?? 0;
 
-                     $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,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 = $info_array[$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){
 
-         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 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);
 
-     }
 
- }
 
 
  |