InventoryCompareController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Imports\InventoryCompareImport;
  4. use App\InventoryCompare;
  5. use App\Services\OwnerService;
  6. use Exception;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Gate;
  11. use Illuminate\Support\Facades\Http;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. class InventoryCompareController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. //
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function create()
  30. {
  31. //
  32. }
  33. /**
  34. * Store a newly created resource in storage.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function store(Request $request)
  40. {
  41. //
  42. }
  43. /**
  44. * Display the specified resource.
  45. *
  46. * @param \App\InventoryCompare $inventoryCompare
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function show(InventoryCompare $inventoryCompare)
  50. {
  51. //
  52. }
  53. /**
  54. * Show the form for editing the specified resource.
  55. *
  56. * @param \App\InventoryCompare $inventoryCompare
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function edit(InventoryCompare $inventoryCompare)
  60. {
  61. //
  62. }
  63. /**
  64. * Update the specified resource in storage.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param \App\InventoryCompare $inventoryCompare
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function update(Request $request, InventoryCompare $inventoryCompare)
  71. {
  72. //
  73. }
  74. /**
  75. * Remove the specified resource from storage.
  76. *
  77. * @param \App\InventoryCompare $inventoryCompare
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function destroy(InventoryCompare $inventoryCompare)
  81. {
  82. //
  83. }
  84. function inventoryCompare(Request $request,OwnerService $ownerService){
  85. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  86. $owners = $ownerService->getIntersectPermitting();
  87. $inventoryCompares=app('InventoryCompareService')->getInventoryCompare($request->all());
  88. $param = $request->input();
  89. return view('inventory.statement.inventoryCompare',compact('owners','inventoryCompares','param'));
  90. }
  91. function importExcel(Request $request){
  92. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  93. // $owner_id=$request->owner_id;
  94. // if(!$owner_id) return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">您还未选择相应货主!</p></h1>';
  95. $fileSuffix = $request->file()['file']->getClientOriginalExtension();
  96. if (in_array($fileSuffix, ['xlsx', 'xlsm', 'xltx', 'xltm', 'xls', 'xlt', 'ods', 'ots', 'slk', 'xml', 'gnumeric', 'htm', 'html', 'csv', 'tsv'])) {
  97. ini_set('max_execution_time', 2100);
  98. ini_set('memory_limit', '512M');
  99. $extension = $request->file()['file']->getClientOriginalExtension();
  100. $extension[0] = strtoupper($extension[0]);
  101. Excel::import(new InventoryCompareImport(), $request->file('file')->path(), null, $extension);
  102. if (Cache::has('error')) {
  103. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">' . Cache::pull('error') . '</p></h1>';
  104. } else {
  105. $exception = Cache::get('exception');
  106. // if ($exception){
  107. $a = '';
  108. for ($i = 0; $i < count($exception); $i++) {
  109. $a .= implode(',', $exception[$i]) . '&#10';
  110. };
  111. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  112. return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">' . $a . '</textarea></h1>';
  113. // }else {
  114. // return '<h1 class="text-danger">导入Excel成功</h1>';
  115. // }
  116. }
  117. } else {
  118. return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
  119. }
  120. }
  121. public function exportInventoryCompare(Request $request){
  122. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  123. if ($request->checkAllSign){
  124. $params = $request->input();
  125. unset($params['checkAllSign']);
  126. $sql=app('InventoryCompareService')->getSql($params);
  127. }else{
  128. $sql=app('InventoryCompareService')->getSql(['id'=>$request->data]);
  129. }
  130. $post = Http::post(config('go.export.url'),['type'=>'inventoryCompare','sql'=>$sql]);
  131. if ($post->status() == 500){
  132. throw new Exception($post->header("Msg"));
  133. }
  134. return response($post,200, [
  135. "Content-type"=>"application/octet-stream",
  136. "Content-Disposition"=>"attachment; filename=库存对比记录-".date('ymdHis').'.xlsx',
  137. ]);
  138. }
  139. }