StoreBlindReceiveController.php 3.3 KB

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