InventoryCompareController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. use Oursdreams\Export\Export;
  14. class InventoryCompareController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. //
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return \Illuminate\Http\Response
  29. */
  30. public function create()
  31. {
  32. //
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. //
  43. }
  44. /**
  45. * Display the specified resource.
  46. *
  47. * @param \App\InventoryCompare $inventoryCompare
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function show(InventoryCompare $inventoryCompare)
  51. {
  52. //
  53. }
  54. /**
  55. * Show the form for editing the specified resource.
  56. *
  57. * @param \App\InventoryCompare $inventoryCompare
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function edit(InventoryCompare $inventoryCompare)
  61. {
  62. //
  63. }
  64. /**
  65. * Update the specified resource in storage.
  66. *
  67. * @param \Illuminate\Http\Request $request
  68. * @param \App\InventoryCompare $inventoryCompare
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function update(Request $request, InventoryCompare $inventoryCompare)
  72. {
  73. //
  74. }
  75. /**
  76. * Remove the specified resource from storage.
  77. *
  78. * @param \App\InventoryCompare $inventoryCompare
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function destroy(InventoryCompare $inventoryCompare)
  82. {
  83. //
  84. }
  85. function inventoryCompare(Request $request,OwnerService $ownerService){
  86. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  87. $owners = $ownerService->getIntersectPermitting();
  88. $inventoryCompares=app('InventoryCompareService')->getInventoryCompare($request->all());
  89. $param = $request->input();
  90. return view('inventory.statement.inventoryCompare',compact('owners','inventoryCompares','param'));
  91. }
  92. function importExcel(Request $request){
  93. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  94. // $owner_id=$request->owner_id;
  95. // if(!$owner_id) return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">您还未选择相应货主!</p></h1>';
  96. $fileSuffix = $request->file()['file']->getClientOriginalExtension();
  97. if (in_array($fileSuffix, ['xlsx', 'xlsm', 'xltx', 'xltm', 'xls', 'xlt', 'ods', 'ots', 'slk', 'xml', 'gnumeric', 'htm', 'html', 'csv', 'tsv'])) {
  98. ini_set('max_execution_time', 2100);
  99. ini_set('memory_limit', '512M');
  100. $extension = $request->file()['file']->getClientOriginalExtension();
  101. $extension[0] = strtoupper($extension[0]);
  102. Excel::import(new InventoryCompareImport(), $request->file('file')->path(), null, $extension);
  103. if (Cache::has('error')) {
  104. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">' . Cache::pull('error') . '</p></h1>';
  105. } else {
  106. $exception = Cache::get('exception');
  107. // if ($exception){
  108. $a = '';
  109. for ($i = 0; $i < count($exception); $i++) {
  110. $a .= implode(',', $exception[$i]) . '&#10';
  111. };
  112. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  113. return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">' . $a . '</textarea></h1>';
  114. // }else {
  115. // return '<h1 class="text-danger">导入Excel成功</h1>';
  116. // }
  117. }
  118. } else {
  119. return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
  120. }
  121. }
  122. public function exportInventoryCompare(Request $request){
  123. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  124. if ($request->checkAllSign){
  125. $params = $request->input();
  126. unset($params['checkAllSign']);
  127. $sql=app('InventoryCompareService')->getSql($params);
  128. }else{
  129. $sql=app('InventoryCompareService')->getSql(['id'=>$request->data]);
  130. }
  131. $e = new Export();
  132. $e->setMysqlConnection(config('database.connections.mysql.host'),
  133. config('database.connections.mysql.port'),config('database.connections.mysql.database')
  134. ,config('database.connections.mysql.username'),config('database.connections.mysql.password'));
  135. $e->setFileName("库存对比记录");
  136. return $e->sql($sql,[
  137. "owner_name"=>"货主","mission_code"=>"任务号",
  138. "created_at"=>"生产时间","commodity_name"=>"商品名称",
  139. "commodity_sku"=>"商品编码","commodity_barcode_code"=>"商品条码",
  140. "custom_location"=>"属性仓","quality"=>"质量状态",
  141. "amount_in_sys"=>"宝时库存","amount_in_compare"=>"参考库存",
  142. "differ"=>"差值"
  143. ])->direct();
  144. }
  145. }