| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 | 
							- <?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;
 
-     }
 
- }
 
 
  |