InventoryCompareController.php 4.3 KB

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