protected function execute(InputInterface $input, OutputInterface $output)
 {
     $csvsPath = APP_CWD . '/../csv';
     $csvsDatesRegexp = 'd-m-Y';
     $menusDatesAlreadyLoaded = array_map(function ($date) use($csvsDatesRegexp) {
         return Carbon::parse($date['start_date'])->format($csvsDatesRegexp);
     }, Menu::all()->toArray());
     $finder = new \Symfony\Component\Finder\Finder();
     $finder = $finder->files()->name('*.csv');
     foreach ($menusDatesAlreadyLoaded as $date) {
         $finder->notPath($date);
     }
     $files = $finder->in($csvsPath);
     foreach ($files as $file) {
         list($d, $m, $Y) = explode('-', explode('_', $file->getFilename())[0]);
         $menuDate = Carbon::parse("{$Y}-{$m}-{$d}");
         $reader = \League\Csv\Reader::createFromPath($file->getPathname());
         $results = $reader->setDelimiter(';')->fetchAssoc();
         //creazione Menu ed aggiunta del prodotto
         $menu = Menu::write($menuDate, $menuDate->copy()->addHours(14));
         foreach ($results as $row) {
             if (empty($row['Prezzo'])) {
                 continue;
             }
             $price = floatval(ltrim(str_replace([','], '.', $row['Prezzo']), '€'));
             $category = Product::findCategory($row['Categoria'], false);
             //creazione Prodotto
             $product = Product::firstOrCreate(['code' => $row['Codice'], 'description' => $row['Descrizione'], 'category' => $category]);
             $product->price = $price;
             $product->update();
             $menu->addProduct($product);
         }
     }
 }
Ejemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $gmail = new \Jambon\GmailClient\GmailClient();
     $emails = $gmail->readEmails(['maxResults' => $input->getArgument('entries'), 'labelIds' => 'INBOX', 'includeSpamTrash' => true, 'q' => 'from:serviziomensa@mosaicoon.com conferma menu ' . $input->getArgument('extraString')], function (array $email) use($output) {
         $content = $email['body']['html'][0];
         // --------------------------------------------------------------------
         preg_match("/[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}/", $content, $matched);
         list($d, $m, $y) = explode('/', $matched[0]);
         $menuDate = Carbon::parse("{$y}-{$m}-{$d} 00:00:00");
         $output->writeLn("Importando Menu del " . $matched[0]);
         // --------------------------------------------------------------------
         if ($menu = Menu::where(['start_date' => $menuDate, 'type' => Menu::TYPE_PRANZO])->count() > 0) {
             //menu già registrato
             $output->writeLn("MENU del " . $matched[0] . " GIA SALVATO");
             return;
         }
         $menu = Menu::write($menuDate, $menuDate->copy()->addHours(14));
         $crawler = new \Symfony\Component\DomCrawler\Crawler();
         $crawler->addHTMLContent($content, 'UTF-8');
         $crawler->filter('table')->eq(0)->filter('tr')->each(function ($tr, $i) use($menu) {
             if ($i === 0) {
                 return;
             }
             $prodData = [];
             $tr->filter('td')->each(function ($td, $i) use(&$prodData) {
                 if ($i == 0) {
                     $prodData['category'] = str_replace(['<', '>', '[', ']'], null, $td->text());
                 } elseif ($i == 1) {
                     $desc = trim(str_replace(['[NUOVO]', '(NUOVO)', '(Nuovo)'], null, $td->text()));
                     $prodData['description'] = strip_tags($desc);
                 } elseif ($i == 2) {
                     $prodData['price'] = floatval(ltrim($td->text(), '€'));
                 }
             });
             //prevent insert injection code
             if (in_array($prodData['category'], ['', null, 'code', 'c'])) {
                 return;
             }
             $menu->addProduct(Product::firstOrCreate($prodData));
             echo '.';
         });
         $output->writeLn(" ");
     });
 }