StoreBlindReceiveController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\InventoryBlindReceiveExcelExport;
  4. use App\InventoryBlindReceiveExcel;
  5. use Carbon\Carbon;
  6. use Illuminate\Http\Request;
  7. use Maatwebsite\Excel\Facades\Excel;
  8. class StoreBlindReceiveController extends Controller
  9. {
  10. public function index()
  11. {
  12. $excels=InventoryBlindReceiveExcel::orderBy('id','desc')->get();
  13. return view('store.blindReceive.excel.index',['excels'=>$excels]);
  14. }
  15. public function create()
  16. {
  17. }
  18. public function store(Request $request)
  19. {
  20. //
  21. }
  22. public function apiStore(Request $request)
  23. {
  24. $request=json_decode($request->getContent(),true);
  25. $goodses=$request['goodses']??'';
  26. $excelFileName=$request['filename']??'';
  27. if(!$goodses||!is_array($goodses)||count($goodses)==0){
  28. return ['result'=>'failure','fail_info'=>'没有数据提交!'];
  29. }
  30. $goodsesExcelArray=[['隔口','数量','条码','生产日期','失效日期','批次号']];
  31. foreach ($goodses as $goods){
  32. array_push($goodsesExcelArray,[$goods['bin'],$goods['amount'],$goods['barcode'].' '
  33. ,$goods['produce_date'],$goods['valid_date'],$goods['batch_number']]);
  34. }
  35. $fileName=Carbon::now()->toDateTimeLocalString().microtime(true).'.xlsx';
  36. $fileName = str_replace( ':', '_',$fileName);
  37. $goodsNeateningExcel = new InventoryBlindReceiveExcel(['file' => $fileName, 'goods_amount' => count($goodses), 'name'=>$excelFileName]);
  38. $goodsNeateningExcel->save();
  39. Excel::store(new InventoryBlindReceiveExcelExport($goodsesExcelArray),$fileName,'public');
  40. $this->removeExpiredExcel();
  41. return ['result'=>'success'];
  42. }
  43. private function removeExpiredExcel(){
  44. $excels=InventoryBlindReceiveExcel::where('created_at','<',Carbon::now()->subDays(100)->toDateTimeString())->get();
  45. $excels->each(function(InventoryBlindReceiveExcel $excel){
  46. $excel->delete();
  47. });
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param \App\InventoryBlindReceiveExcel $goodsNeateningExcel
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function show(InventoryBlindReceiveExcel $goodsNeateningExcel)
  56. {
  57. //
  58. }
  59. /**
  60. * Show the form for editing the specified resource.
  61. *
  62. * @param \App\InventoryBlindReceiveExcel $goodsNeateningExcel
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function edit(InventoryBlindReceiveExcel $goodsNeateningExcel)
  66. {
  67. //
  68. }
  69. /**
  70. * Update the specified resource in storage.
  71. *
  72. * @param \Illuminate\Http\Request $request
  73. * @param \App\InventoryBlindReceiveExcel $goodsNeateningExcel
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function update(Request $request, InventoryBlindReceiveExcel $goodsNeateningExcel)
  77. {
  78. //
  79. }
  80. /**
  81. * Remove the specified resource from storage.
  82. *
  83. * @param \App\InventoryBlindReceiveExcel $goodsNeateningExcel
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function destroy(InventoryBlindReceiveExcel $goodsNeateningExcel)
  87. {
  88. //
  89. }
  90. }