/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, ['file' => 'required', 'city' => 'required', 'year' => 'required']); $city = City::findOrFail($request->city); $tags = Keyword::get(); $parser = new SheetHandler($request->file); if (empty($request->month)) { $investiments = $parser->extractInvestimentsEachMonth(); foreach ($investiments as $investimentIdx => $investiment) { foreach ($investiment as $month => $singleMonth) { if (!empty($singleMonth->domain)) { $flag = 1; $singleMonth->city()->associate($city); $singleMonth->made_at = $request->year . '-' . sprintf("%02s", $month) . '-' . 1; foreach ($tags as $tag) { if (strpos(mb_strtolower($singleMonth, 'UTF-8'), $tag->name) !== FALSE) { $flag = 0; $singleMonth->category()->associate(Category::findOrFail($tag->category_id)); break; } } if ($flag) { $singleMonth->category()->associate(Category::where('name', 'Outros')->firstOrFail()); } } else { unset($investiments[$investimentIdx]); } } } foreach ($investiments as $investiment) { foreach ($investiment as $singleMonth) { $singleMonth->save(); } } } else { $investiments = $parser->extractInvestiments(); foreach ($investiments as $investimentIdx => $investiment) { if (!empty($investiment->domain)) { $flag = 1; $investiment->city()->associate($city); $investiment->made_at = $request->year . '-' . sprintf("%02s", $request->month) . '-' . 1; foreach ($tags as $tag) { if (strpos(mb_strtolower($investiment, 'UTF-8'), $tag->name) !== FALSE) { $flag = 0; $investiment->category()->associate(Category::findOrFail($tag->category_id)); break; } } if ($flag) { $investiment->category()->associate(Category::where('name', 'Outros')->firstOrFail()); } } else { unset($investiments[$investimentIdx]); } } foreach ($investiments as $invesitment) { $investiment->save(); } } return Redirect::route('admin.investimentos.index'); }