|
@@ -0,0 +1,379 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+
|
|
|
+use App\Exports\ExportOrder;
|
|
|
+use App\Exports\MyExport;
|
|
|
+use App\Model\Area;
|
|
|
+use App\Model\BasicMaterial;
|
|
|
+use App\Model\BasicRollFilm;
|
|
|
+use App\Model\BasicType;
|
|
|
+use App\Model\CarDepart;
|
|
|
+use App\Model\CarFiles;
|
|
|
+use App\Model\CarType;
|
|
|
+use App\Model\Company;
|
|
|
+use App\Model\Construction;
|
|
|
+use App\Model\ConstructionOrder;
|
|
|
+use App\Model\ConstructionOrderImg;
|
|
|
+use App\Model\ConstructionOrderSub;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\FoursShop;
|
|
|
+use App\Model\InOutRecord;
|
|
|
+use App\Model\Inventory;
|
|
|
+use App\Model\InventorySub;
|
|
|
+use App\Model\Material;
|
|
|
+use App\Model\MaterialCharge;
|
|
|
+use App\Model\MaterialChargeSub;
|
|
|
+use App\Model\MaterialOrder;
|
|
|
+use App\Model\MaterialOrderApply;
|
|
|
+use App\Model\MaterialOrderIn;
|
|
|
+use App\Model\MaterialOrderSend;
|
|
|
+use App\Model\MaterialOrderSendSub;
|
|
|
+use App\Model\MaterialReturn;
|
|
|
+use App\Model\MaterialReturnSub;
|
|
|
+use App\Model\RollFilm;
|
|
|
+use App\Model\RollFilmCombine;
|
|
|
+use App\Model\RollFilmCompany;
|
|
|
+use App\Model\RollFilmInventory;
|
|
|
+use App\Model\SalesOrder;
|
|
|
+use App\Model\SalesOrderOtherFee;
|
|
|
+use App\Model\SalesOrderProductInfo;
|
|
|
+use App\Model\Storehouse;
|
|
|
+use App\Model\Transfer;
|
|
|
+use App\Model\TransferSub;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Maatwebsite\Excel\Facades\Excel;
|
|
|
+
|
|
|
+class ExportFileService extends Service
|
|
|
+{
|
|
|
+ //导出文件
|
|
|
+ const type_one = 1;
|
|
|
+ const type_two = 2;
|
|
|
+ const type_three = 3;
|
|
|
+ const type_four = 4;
|
|
|
+ const type_five = 5;
|
|
|
+ const type_six = 6;
|
|
|
+ const type_seven = 7;
|
|
|
+
|
|
|
+ //导出文件方法
|
|
|
+ protected static $fuc = [
|
|
|
+ self::type_one => '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 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;
|
|
|
+ }
|
|
|
+}
|