'one', self::type_two => 'two', self::type_three => 'three', self::type_four => 'four', ]; protected static $fuc_name = [ self::type_one => '合同订单', self::type_two => '派工订单', self::type_three => '收付款单', self::type_four => '施工单', ]; public static $filename = ""; public function exportAll($data,$user){ if(empty($data['id'])) return [false,'请选择导出数据']; if(empty($data['type']) || ! isset(self::$fuc[$data['type']])) return [false,'导出文件类型错误或者不存在']; self::$filename = self::$fuc_name[$data['type']] ?? ""; //不超时 ini_set('max_execution_time', 0); //内存设置 ini_set('memory_limit', -1); $function = self::$fuc[$data['type']]; $return = $this->$function($data); return [true, $return]; } public function one($ergs){ $id = $ergs['id']; // 导出数据 $return = []; DB::table('sales_order') ->whereIn('id', $id) ->select('id','order_number','model_type','sales_order_type','sign_time','plat_order','plat_type','product_total','other_fee','discount_fee','contract_fee','crt_time','crt_id','state','invoice_state','pay_way') ->orderBy('id','desc') ->chunk(500,function ($data) use(&$return){ $data = $data->toArray(); $data_id = array_unique(array_column($data,'id')); $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id'))) ->pluck('emp_name','id') ->toArray(); //获取分社订货合同关联的发货单 $id = []; foreach ($data as $value){ $id[] = [ 'id' => $value->id, 'invoice_state' => $value->invoice_state, ]; } $invoiceData = (new SalesOrderService())->getInvoiceOrderData($id); $array = array_unique(array_merge_recursive(array_column($data,'car_type'),array_column($data,'pay_way'),array_column($data,'install_position'),array_column($data,'install_method'),array_column($data,'plat_type'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $product = []; $sales_p_info = SalesOrderProductInfo::where('del_time',0) ->whereIn('sales_order_id',$data_id) ->get()->toArray(); $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray(); $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id')); foreach ($sales_p_info as $value){ $tmp = $map[$value['product_id']] ?? []; $value['title'] = $tmp['title'] ?? ""; $value['code'] = $tmp['code'] ?? ""; $value['size'] = $tmp['size'] ?? ""; $value['bar_code'] = $tmp['bar_code'] ?? ""; $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? ""; $value['install_time'] = $tmp['install_time'] ?? 0; $product[$value['sales_order_id']][] = $value; } $flag = []; foreach ($data as $value){ $tmp = $invoiceData[$value->id] ?? []; $invoice_arr = implode(',', $tmp); $model_type_title = SalesOrder::$model_type_title[$value->model_type] ?? ''; $plat_type_title = $basic_map[$value->plat_type] ?? ''; $sales_order_type_title = SalesOrder::$order_type[$value->sales_order_type] ?? ''; $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time); $sign_time = empty($value->sign_time) ? '' : date('Y-m-d',$value->sign_time); $product_tmp = $product[$value->id] ?? []; if($value->sales_order_type == SalesOrder::Order_type_one){ if($value->model_type == SalesOrder::Model_type_four){ $state = SalesOrder::$state[$value->state] ?? ''; }else{ $state = SalesOrder::$state_2[$value->state] ?? ''; } }else{ $state = SalesOrder::$state2[$value->state] ?? ''; } foreach ($product_tmp as $val){ if(in_array($val['sales_order_id'], $flag)){ $return[] = [ 'order_number' => "", 'model_type_title' => "", 'sales_order_type_title' => "", 'sign_time' => "", 'plat_order' => "", 'plat_type_title' => "", 'product_total' => "", 'other_fee' => "", 'discount_fee' => "", 'contract_fee' => "", 'crt_time' => "", 'crt_name' => "", 'state_title' => "", 'invoice_state_title' =>"", 'invoice_arr' => "", 'pay_way' => "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_rate' => $val['rate'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; }else{ $flag[] = $val['sales_order_id']; $return[] = [ 'order_number' => $value->order_number, 'model_type_title' => $model_type_title, 'sales_order_type_title' => $sales_order_type_title, 'sign_time' => $sign_time, 'plat_order' => $value->plat_order, 'plat_type_title' => $plat_type_title, 'product_total' => $value->product_total, 'other_fee' => $value->other_fee, 'discount_fee' => $value->discount_fee, 'contract_fee' => $value->contract_fee, 'crt_time' => $crt_time, 'crt_name' => $emp_map[$value->crt_id] ?? "", 'state_title' => $state, 'invoice_state_title' => SalesOrder::$invoice_state[$value->invoice_state] ?? '', 'invoice_arr' => $invoice_arr, 'pay_way' => $basic_map[$value->pay_way] ?? "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_rate' => $val['rate'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; } } } }); $header = ['合同单号','销售类型','产品类型','签订时间','平台单号','平台类型','产品合计','其他费用','优惠金额','合同金额','创建时间','创建人','订单状态','发货状态','发货单号','付款方式','产品名称','产品编码','产品规格','产品税率','产品单价','产品数量','产品合同金额']; return $this->saveExportData($return,$header); } public function two($ergs){ $id = $ergs['id']; // 导出数据 $return = []; DB::table('sales_order') ->whereIn('id', $id) ->select('id','order_number','model_type','sales_order_type','sign_time','plat_order','plat_type','product_total','other_fee','discount_fee','contract_fee','crt_time','crt_id','state','invoice_state','pay_way') ->orderBy('id','desc') ->chunk(500,function ($data) use(&$return){ $data = $data->toArray(); $data_id = array_unique(array_column($data,'id')); $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id'))) ->pluck('emp_name','id') ->toArray(); //获取分社订货合同关联的发货单 $id = []; foreach ($data as $value){ $id[] = [ 'id' => $value->id, 'invoice_state' => $value->invoice_state, 'state' => $value->state, ]; } $invoiceData = (new SalesOrderService())->getInvoiceOrderData($id); $array = array_unique(array_merge_recursive(array_column($data,'car_type'),array_column($data,'pay_way'),array_column($data,'install_position'),array_column($data,'install_method'),array_column($data,'plat_type'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $product = []; $sales_p_info = SalesOrderProductInfo::where('del_time',0) ->whereIn('sales_order_id',$data_id) ->get()->toArray(); $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray(); $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id')); //指派金额 $fee = []; $sales_o_info = SalesOrderOtherFee::where('del_time',0) ->whereIn('sales_order_id',$data_id) ->get()->toArray(); foreach ($sales_o_info as $value){ $fee[$value['sales_order_id']] = $value['other_fee_1']; } foreach ($sales_p_info as $value){ $tmp = $map[$value['product_id']] ?? []; $value['title'] = $tmp['title'] ?? ""; $value['code'] = $tmp['code'] ?? ""; $value['size'] = $tmp['size'] ?? ""; $value['bar_code'] = $tmp['bar_code'] ?? ""; $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? ""; $value['install_time'] = $tmp['install_time'] ?? 0; $product[$value['sales_order_id']][] = $value; } //分派的总社或分社 $dispatch = (new SalesOrderService())->getDispatchData($id); $flag = []; foreach ($data as $value){ $tmp = $invoiceData[$value->id] ?? []; $invoice_arr = implode(',', $tmp); $model_type_title = SalesOrder::$model_type_title[$value->model_type] ?? ''; $plat_type_title = $basic_map[$value->plat_type] ?? ''; $sales_order_type_title = SalesOrder::$order_type[$value->sales_order_type] ?? ''; $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time); $sign_time = empty($value->sign_time) ? '' : date('Y-m-d',$value->sign_time); $product_tmp = $product[$value->id] ?? []; if($value->sales_order_type == SalesOrder::Order_type_one){ if($value->model_type == SalesOrder::Model_type_four){ $state = SalesOrder::$state[$value->state] ?? ''; }else{ $state = SalesOrder::$state_2[$value->state] ?? ''; } }else{ $state = SalesOrder::$state2[$value->state] ?? ''; } foreach ($product_tmp as $val){ if(in_array($val['sales_order_id'], $flag)){ $return[] = [ 'order_number' => "", 'model_type_title' => "", 'sales_order_type_title' => "", 'sign_time' => "", 'plat_order' => "", 'plat_type_title' => "", 'product_total' => "", 'other_fee' => "", 'discount_fee' => "", 'contract_fee' => "", 'fee' => "", 'dispatch_company' => "", 'crt_time' => "", 'crt_name' => "", 'state_title' => "", 'invoice_state_title' =>"", 'invoice_arr' => "", 'pay_way' => "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_rate' => $val['rate'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; }else{ $flag[] = $val['sales_order_id']; $return[] = [ 'order_number' => $value->order_number, 'model_type_title' => $model_type_title, 'sales_order_type_title' => $sales_order_type_title, 'sign_time' => $sign_time, 'plat_order' => $value->plat_order, 'plat_type_title' => $plat_type_title, 'product_total' => $value->product_total, 'other_fee' => $value->other_fee, 'discount_fee' => $value->discount_fee, 'contract_fee' => $value->contract_fee, 'fee' => $fee[$value->id] ?? "", 'dispatch_company' => $dispatch[$value->id] ?? '', 'crt_time' => $crt_time, 'crt_name' => $emp_map[$value->crt_id] ?? "", 'state_title' => $state, 'invoice_state_title' => SalesOrder::$invoice_state[$value->invoice_state] ?? '', 'invoice_arr' => $invoice_arr, 'pay_way' => $basic_map[$value->pay_way] ?? "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_rate' => $val['rate'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; } } } }); $header = ['合同单号','销售类型','产品类型','签订时间','平台单号','平台类型','产品合计','其他费用','优惠金额','合同金额','指派金额','派遣门店','创建时间','创建人','订单状态','发货状态','发货单号','付款方式','产品名称','产品编码','产品规格','产品税率','产品单价','产品数量','产品合同金额']; return $this->saveExportData($return,$header); } public function three($ergs){ $id = $ergs['id']; // 导出数据 $return = []; DB::table('payment_receipt') ->whereIn('id', $id) ->select('id','order_number','account','pay_way','type','data_type','payment_receipt_date','mark','crt_time','crt_id','state') ->orderBy('id','desc') ->chunk(500,function ($data) use(&$return){ $data = $data->toArray(); $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id'))) ->pluck('emp_name','id') ->toArray(); $array = array_unique(array_merge_recursive(array_column($data,'account'),array_column($data,'pay_way'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $info = PaymentReceiptInfo::where('del_time',0) ->where('type',PaymentReceiptInfo::type_three) ->whereIn('payment_receipt_id',array_column($data,'id')) ->get()->toArray(); $map = []; foreach ($info as $value){ $map[$value['payment_receipt_id']][] = [ 'payment_receipt_id' => $value['payment_receipt_id'], 'data_order_no' => $value['data_order_no'], 'amount' => $value['amount'] ]; } $flag = []; foreach ($data as $value){ $account_title = $basic_map[$value->account] ?? ''; $pay_way_title = $basic_map[$value->pay_way] ?? ''; $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time); $payment_receipt_date= empty($value->payment_receipt_date) ? '' : date('Y-m-d',$value->payment_receipt_date); $state_title = PaymentReceipt::$name[$value->state] ?? ''; $product_tmp = $map[$value->id] ?? []; foreach ($product_tmp as $val){ if(in_array($val['payment_receipt_id'], $flag)){ $return[] = [ 'order_number' => "", 'account' => "", 'pay_way' => "", 'type' => "", 'data_type' => "", 'payment_receipt_date' => "", 'mark' => "", 'crt_time' => "", 'crt_name' => "", 'state_title' => "", 'data_order_no' => $val['data_order_no'], 'amount' => $val['amount'], ]; }else{ $flag[] = $val['payment_receipt_id']; $return[] = [ 'order_number' => $value->order_number, 'account' => $account_title, 'pay_way' => $pay_way_title, 'type' => PaymentReceipt::$model_type[$value->type] ?? '', 'data_type' => PaymentReceipt::$data_type[$value->data_type] ?? '', 'payment_receipt_date' => $payment_receipt_date, 'mark' => $value->mark, 'crt_time' => $crt_time, 'crt_name' => $emp_map[$value->crt_id] ?? "", 'state_title' => $state_title, 'data_order_no' => $val['data_order_no'], 'amount' => $val['amount'], ]; } } } }); $header = ['收付款单号','收付款账户','支付方式','收付款类型','关联单号类型','回款日期','备注','创建时间','创建人','订单状态','关联单号','金额']; return $this->saveExportData($return,$header); } public function four($ergs){ $id = $ergs['id']; // 导出数据 $return = []; DB::table('construction') ->whereIn('id', $id) ->select('id','order_number','title','sales_order_id','handover_time','install_method','install_position','mark','urgency','start_time','end_time','crt_time','crt_id','state','pq_state','day_start_stamp','day_end_stamp') ->orderBy('id','desc') ->chunk(500,function ($data) use(&$return){ $data = $data->toArray(); $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id'))) ->pluck('emp_name','id') ->toArray(); $sales = SalesOrder::whereIn('id',array_unique(array_column($data,'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray(); $sales_map = []; foreach ($sales as $value){ $sales_map[$value['id']] = $value; } $array = array_unique(array_merge_recursive(array_column($data,'install_method'),array_column($data,'urgency'),array_column($data,'install_position'))); $basic_map = BasicType::whereIn('id',$array) ->pluck('title','id') ->toArray(); $product = []; $p_info = ConstructionProductInfo::where('del_time',0) ->whereIn('construction_id',array_unique(array_column($data,'id'))) ->get()->toArray(); $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id')); foreach ($p_info as $value){ $tmp = $map[$value['product_id']] ?? []; $value['title'] = $tmp['title'] ?? ""; $value['code'] = $tmp['code'] ?? ""; $value['size'] = $tmp['size'] ?? ""; $value['unit'] = $tmp['unit'] ?? ""; $value['bar_code'] = $tmp['bar_code'] ?? ""; $product[$value['construction_id']][] = $value; } $flag = []; foreach ($data as $value){ $tmp_sales = $sales_map[$value->sales_order_id] ?? []; $handover_time = $value->handover_time ? date("Y-m-d") : ""; $sales_order_number = $tmp_sales['order_number']; $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time); $install_position_title = $basic_map[$value->install_position] ?? ''; $install_method_title = $basic_map[$value->install_position] ?? ''; $urgency_title = $basic_map[$value->urgency] ?? ''; $start_time = $value->start_time ? date("Y-m-d H:i",$value->start_time) : ''; $end_time = $value->end_time ? date("Y-m-d H:i",$value->end_time) : ''; $construction_period = $start_time . '——' . $end_time; $state = Construction::$name[$value->state] ?? ''; $pq_state = Construction::$pq_name[$value->pq_state] ?? ''; $str = ""; $start_time = $value->day_start_stamp ? date("Y-m-d H:i",$value->day_start_stamp) : ''; $end_time = $value->day_end_stamp ? date("Y-m-d H:i",$value->day_end_stamp) : ''; if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time; $pq_period = $str; $product_tmp = $product[$value->id] ?? []; foreach ($product_tmp as $val){ if(in_array($val['construction_id'], $flag)){ $return[] = [ 'order_number' => "", 'title' => "", 'sales_order_title' => "", 'handover_time' => "", 'install_method' => "", 'install_position' => "", 'mark' => "", 'urgency' => "", 'construction_period' => "", 'state_title' => "", 'pq_state' => "", 'pq_period' => "", 'crt_time' => "", 'crt_name' => "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; }else{ $flag[] = $val['construction_id']; $return[] = [ 'order_number' => $value->order_number, 'title' => $value->title, 'sales_order_title' => $sales_order_number, 'handover_time' => $handover_time, 'install_method' => $install_method_title, 'install_position' => $install_position_title, 'mark' => $value->mark, 'urgency' => $urgency_title, 'construction_period' => $construction_period, 'state_title' => $state, 'pq_state' => $pq_state, 'pq_period' => $pq_period, 'crt_time' => $crt_time, 'crt_name' => $emp_map[$value->crt_id] ?? "", 'product_title' => $val['title'], 'product_code' => $val['code'], 'product_size' => $val['size'], 'product_price' => $val['price'], 'product_num' => $val['number'], 'product_final_amount' => $val['final_amount'], ]; } } } }); $header = ['施工单号','工单来源','关联单号','交车日期','安装方式','安装地点','备注','紧急程度','施工时间','订单状态','是否排期','排期时间','创建时间','创建人','产品名称','产品编码','产品规格','产品单价','产品数量','产品合同金额']; return $this->saveExportData($return,$header); } public function saveExportData($data, $headers, $type = 'default',$file_name = ''){ if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999); $filename = $file_name . '.' . 'xlsx'; $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []); return $filename; } }