ExportFileService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\ExportOrder;
  4. use App\Exports\MyExport;
  5. use App\Model\Area;
  6. use App\Model\BasicMaterial;
  7. use App\Model\BasicRollFilm;
  8. use App\Model\BasicType;
  9. use App\Model\CarDepart;
  10. use App\Model\CarFiles;
  11. use App\Model\CarType;
  12. use App\Model\Company;
  13. use App\Model\Construction;
  14. use App\Model\ConstructionOrder;
  15. use App\Model\ConstructionOrderImg;
  16. use App\Model\ConstructionOrderSub;
  17. use App\Model\Employee;
  18. use App\Model\FoursShop;
  19. use App\Model\InOutRecord;
  20. use App\Model\Inventory;
  21. use App\Model\InventorySub;
  22. use App\Model\Material;
  23. use App\Model\MaterialCharge;
  24. use App\Model\MaterialChargeSub;
  25. use App\Model\MaterialOrder;
  26. use App\Model\MaterialOrderApply;
  27. use App\Model\MaterialOrderIn;
  28. use App\Model\MaterialOrderSend;
  29. use App\Model\MaterialOrderSendSub;
  30. use App\Model\MaterialReturn;
  31. use App\Model\MaterialReturnSub;
  32. use App\Model\RollFilm;
  33. use App\Model\RollFilmCombine;
  34. use App\Model\RollFilmCompany;
  35. use App\Model\RollFilmInventory;
  36. use App\Model\SalesOrder;
  37. use App\Model\SalesOrderOtherFee;
  38. use App\Model\SalesOrderProductInfo;
  39. use App\Model\Storehouse;
  40. use App\Model\Transfer;
  41. use App\Model\TransferSub;
  42. use Illuminate\Support\Facades\DB;
  43. use Maatwebsite\Excel\Facades\Excel;
  44. class ExportFileService extends Service
  45. {
  46. //导出文件
  47. const type_one = 1;
  48. const type_two = 2;
  49. const type_three = 3;
  50. const type_four = 4;
  51. const type_five = 5;
  52. const type_six = 6;
  53. const type_seven = 7;
  54. //导出文件方法
  55. protected static $fuc = [
  56. self::type_one => 'one',
  57. self::type_two => 'two',
  58. self::type_three => 'three',
  59. self::type_four => 'four',
  60. ];
  61. protected static $fuc_name = [
  62. self::type_one => '合同订单',
  63. self::type_two => '派工订单',
  64. self::type_three => '收付款单',
  65. self::type_four => '施工单',
  66. ];
  67. public static $filename = "";
  68. public function exportAll($data,$user){
  69. if(empty($data['id'])) return [false,'请选择导出数据'];
  70. if(empty($data['type']) || ! isset(self::$fuc[$data['type']])) return [false,'导出文件类型错误或者不存在'];
  71. self::$filename = self::$fuc_name[$data['type']] ?? "";
  72. //不超时
  73. ini_set('max_execution_time', 0);
  74. //内存设置
  75. ini_set('memory_limit', -1);
  76. $function = self::$fuc[$data['type']];
  77. $return = $this->$function($data);
  78. return [true, $return];
  79. }
  80. public function one($ergs){
  81. $id = $ergs['id'];
  82. // 导出数据
  83. $return = [];
  84. DB::table('sales_order')
  85. ->whereIn('id', $id)
  86. ->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')
  87. ->orderBy('id','desc')
  88. ->chunk(500,function ($data) use(&$return){
  89. $data = $data->toArray();
  90. $data_id = array_unique(array_column($data,'id'));
  91. $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id')))
  92. ->pluck('emp_name','id')
  93. ->toArray();
  94. //获取分社订货合同关联的发货单
  95. $id = [];
  96. foreach ($data as $value){
  97. $id[] = [
  98. 'id' => $value->id,
  99. 'invoice_state' => $value->invoice_state,
  100. ];
  101. }
  102. $invoiceData = (new SalesOrderService())->getInvoiceOrderData($id);
  103. $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')));
  104. $basic_map = BasicType::whereIn('id',$array)
  105. ->pluck('title','id')
  106. ->toArray();
  107. $product = [];
  108. $sales_p_info = SalesOrderProductInfo::where('del_time',0)
  109. ->whereIn('sales_order_id',$data_id)
  110. ->get()->toArray();
  111. $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray();
  112. $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id'));
  113. foreach ($sales_p_info as $value){
  114. $tmp = $map[$value['product_id']] ?? [];
  115. $value['title'] = $tmp['title'] ?? "";
  116. $value['code'] = $tmp['code'] ?? "";
  117. $value['size'] = $tmp['size'] ?? "";
  118. $value['bar_code'] = $tmp['bar_code'] ?? "";
  119. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  120. $value['install_time'] = $tmp['install_time'] ?? 0;
  121. $product[$value['sales_order_id']][] = $value;
  122. }
  123. $flag = [];
  124. foreach ($data as $value){
  125. $tmp = $invoiceData[$value->id] ?? [];
  126. $invoice_arr = implode(',', $tmp);
  127. $model_type_title = SalesOrder::$model_type_title[$value->model_type] ?? '';
  128. $plat_type_title = $basic_map[$value->plat_type] ?? '';
  129. $sales_order_type_title = SalesOrder::$order_type[$value->sales_order_type] ?? '';
  130. $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time);
  131. $sign_time = empty($value->sign_time) ? '' : date('Y-m-d',$value->sign_time);
  132. $product_tmp = $product[$value->id] ?? [];
  133. if($value->sales_order_type == SalesOrder::Order_type_one){
  134. if($value->model_type == SalesOrder::Model_type_four){
  135. $state = SalesOrder::$state[$value->state] ?? '';
  136. }else{
  137. $state = SalesOrder::$state_2[$value->state] ?? '';
  138. }
  139. }else{
  140. $state = SalesOrder::$state2[$value->state] ?? '';
  141. }
  142. foreach ($product_tmp as $val){
  143. if(in_array($val['sales_order_id'], $flag)){
  144. $return[] = [
  145. 'order_number' => "",
  146. 'model_type_title' => "",
  147. 'sales_order_type_title' => "",
  148. 'sign_time' => "",
  149. 'plat_order' => "",
  150. 'plat_type_title' => "",
  151. 'product_total' => "",
  152. 'other_fee' => "",
  153. 'discount_fee' => "",
  154. 'contract_fee' => "",
  155. 'crt_time' => "",
  156. 'crt_name' => "",
  157. 'state_title' => "",
  158. 'invoice_state_title' =>"",
  159. 'invoice_arr' => "",
  160. 'pay_way' => "",
  161. 'product_title' => $val['title'],
  162. 'product_code' => $val['code'],
  163. 'product_size' => $val['size'],
  164. 'product_rate' => $val['rate'],
  165. 'product_price' => $val['price'],
  166. 'product_num' => $val['number'],
  167. 'product_final_amount' => $val['final_amount'],
  168. ];
  169. }else{
  170. $flag[] = $val['sales_order_id'];
  171. $return[] = [
  172. 'order_number' => $value->order_number,
  173. 'model_type_title' => $model_type_title,
  174. 'sales_order_type_title' => $sales_order_type_title,
  175. 'sign_time' => $sign_time,
  176. 'plat_order' => $value->plat_order,
  177. 'plat_type_title' => $plat_type_title,
  178. 'product_total' => $value->product_total,
  179. 'other_fee' => $value->other_fee,
  180. 'discount_fee' => $value->discount_fee,
  181. 'contract_fee' => $value->contract_fee,
  182. 'crt_time' => $crt_time,
  183. 'crt_name' => $emp_map[$value->crt_id] ?? "",
  184. 'state_title' => $state,
  185. 'invoice_state_title' => SalesOrder::$invoice_state[$value->invoice_state] ?? '',
  186. 'invoice_arr' => $invoice_arr,
  187. 'pay_way' => $basic_map[$value->pay_way] ?? "",
  188. 'product_title' => $val['title'],
  189. 'product_code' => $val['code'],
  190. 'product_size' => $val['size'],
  191. 'product_rate' => $val['rate'],
  192. 'product_price' => $val['price'],
  193. 'product_num' => $val['number'],
  194. 'product_final_amount' => $val['final_amount'],
  195. ];
  196. }
  197. }
  198. }
  199. });
  200. $header = ['合同单号','销售类型','产品类型','签订时间','平台单号','平台类型','产品合计','其他费用','优惠金额','合同金额','创建时间','创建人','订单状态','发货状态','发货单号','付款方式','产品名称','产品编码','产品规格','产品税率','产品单价','产品数量','产品合同金额'];
  201. return $this->saveExportData($return,$header);
  202. }
  203. public function two($ergs){
  204. $id = $ergs['id'];
  205. // 导出数据
  206. $return = [];
  207. DB::table('sales_order')
  208. ->whereIn('id', $id)
  209. ->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')
  210. ->orderBy('id','desc')
  211. ->chunk(500,function ($data) use(&$return){
  212. $data = $data->toArray();
  213. $data_id = array_unique(array_column($data,'id'));
  214. $emp_map = Employee::whereIn('id',array_unique(array_column($data,'crt_id')))
  215. ->pluck('emp_name','id')
  216. ->toArray();
  217. //获取分社订货合同关联的发货单
  218. $id = [];
  219. foreach ($data as $value){
  220. $id[] = [
  221. 'id' => $value->id,
  222. 'invoice_state' => $value->invoice_state,
  223. 'state' => $value->state,
  224. ];
  225. }
  226. $invoiceData = (new SalesOrderService())->getInvoiceOrderData($id);
  227. $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')));
  228. $basic_map = BasicType::whereIn('id',$array)
  229. ->pluck('title','id')
  230. ->toArray();
  231. $product = [];
  232. $sales_p_info = SalesOrderProductInfo::where('del_time',0)
  233. ->whereIn('sales_order_id',$data_id)
  234. ->get()->toArray();
  235. $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray();
  236. $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id'));
  237. //指派金额
  238. $fee = [];
  239. $sales_o_info = SalesOrderOtherFee::where('del_time',0)
  240. ->whereIn('sales_order_id',$data_id)
  241. ->get()->toArray();
  242. foreach ($sales_o_info as $value){
  243. $fee[$value['sales_order_id']] = $value['other_fee_1'];
  244. }
  245. foreach ($sales_p_info as $value){
  246. $tmp = $map[$value['product_id']] ?? [];
  247. $value['title'] = $tmp['title'] ?? "";
  248. $value['code'] = $tmp['code'] ?? "";
  249. $value['size'] = $tmp['size'] ?? "";
  250. $value['bar_code'] = $tmp['bar_code'] ?? "";
  251. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  252. $value['install_time'] = $tmp['install_time'] ?? 0;
  253. $product[$value['sales_order_id']][] = $value;
  254. }
  255. //分派的总社或分社
  256. $dispatch = (new SalesOrderService())->getDispatchData($id);
  257. $flag = [];
  258. foreach ($data as $value){
  259. $tmp = $invoiceData[$value->id] ?? [];
  260. $invoice_arr = implode(',', $tmp);
  261. $model_type_title = SalesOrder::$model_type_title[$value->model_type] ?? '';
  262. $plat_type_title = $basic_map[$value->plat_type] ?? '';
  263. $sales_order_type_title = SalesOrder::$order_type[$value->sales_order_type] ?? '';
  264. $crt_time = empty($value->crt_time) ? '' : date('Y-m-d',$value->crt_time);
  265. $sign_time = empty($value->sign_time) ? '' : date('Y-m-d',$value->sign_time);
  266. $product_tmp = $product[$value->id] ?? [];
  267. if($value->sales_order_type == SalesOrder::Order_type_one){
  268. if($value->model_type == SalesOrder::Model_type_four){
  269. $state = SalesOrder::$state[$value->state] ?? '';
  270. }else{
  271. $state = SalesOrder::$state_2[$value->state] ?? '';
  272. }
  273. }else{
  274. $state = SalesOrder::$state2[$value->state] ?? '';
  275. }
  276. foreach ($product_tmp as $val){
  277. if(in_array($val['sales_order_id'], $flag)){
  278. $return[] = [
  279. 'order_number' => "",
  280. 'model_type_title' => "",
  281. 'sales_order_type_title' => "",
  282. 'sign_time' => "",
  283. 'plat_order' => "",
  284. 'plat_type_title' => "",
  285. 'product_total' => "",
  286. 'other_fee' => "",
  287. 'discount_fee' => "",
  288. 'contract_fee' => "",
  289. 'fee' => "",
  290. 'dispatch_company' => "",
  291. 'crt_time' => "",
  292. 'crt_name' => "",
  293. 'state_title' => "",
  294. 'invoice_state_title' =>"",
  295. 'invoice_arr' => "",
  296. 'pay_way' => "",
  297. 'product_title' => $val['title'],
  298. 'product_code' => $val['code'],
  299. 'product_size' => $val['size'],
  300. 'product_rate' => $val['rate'],
  301. 'product_price' => $val['price'],
  302. 'product_num' => $val['number'],
  303. 'product_final_amount' => $val['final_amount'],
  304. ];
  305. }else{
  306. $flag[] = $val['sales_order_id'];
  307. $return[] = [
  308. 'order_number' => $value->order_number,
  309. 'model_type_title' => $model_type_title,
  310. 'sales_order_type_title' => $sales_order_type_title,
  311. 'sign_time' => $sign_time,
  312. 'plat_order' => $value->plat_order,
  313. 'plat_type_title' => $plat_type_title,
  314. 'product_total' => $value->product_total,
  315. 'other_fee' => $value->other_fee,
  316. 'discount_fee' => $value->discount_fee,
  317. 'contract_fee' => $value->contract_fee,
  318. 'fee' => $fee[$value->id] ?? "",
  319. 'dispatch_company' => $dispatch[$value->id] ?? '',
  320. 'crt_time' => $crt_time,
  321. 'crt_name' => $emp_map[$value->crt_id] ?? "",
  322. 'state_title' => $state,
  323. 'invoice_state_title' => SalesOrder::$invoice_state[$value->invoice_state] ?? '',
  324. 'invoice_arr' => $invoice_arr,
  325. 'pay_way' => $basic_map[$value->pay_way] ?? "",
  326. 'product_title' => $val['title'],
  327. 'product_code' => $val['code'],
  328. 'product_size' => $val['size'],
  329. 'product_rate' => $val['rate'],
  330. 'product_price' => $val['price'],
  331. 'product_num' => $val['number'],
  332. 'product_final_amount' => $val['final_amount'],
  333. ];
  334. }
  335. }
  336. }
  337. });
  338. $header = ['合同单号','销售类型','产品类型','签订时间','平台单号','平台类型','产品合计','其他费用','优惠金额','合同金额','指派金额','派遣门店','创建时间','创建人','订单状态','发货状态','发货单号','付款方式','产品名称','产品编码','产品规格','产品税率','产品单价','产品数量','产品合同金额'];
  339. return $this->saveExportData($return,$header);
  340. }
  341. public function saveExportData($data, $headers, $type = 'default',$file_name = ''){
  342. if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
  343. $filename = $file_name . '.' . 'xlsx';
  344. $bool = Excel::store(new ExportOrder($data,$type,$headers),"/public/export/{$filename}", null, 'Xlsx', []);
  345. return $filename;
  346. }
  347. }