Ejemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $date = $input->getArgument('date');
     if (!empty($date)) {
         $date = dateEuToCarbon($date);
         $menu = Menu::where('start_date', '=', $date)->limit(1)->firstOrFail();
     } else {
         //prendi l'ultimo menu
         $menu = Menu::orderBy('start_date', 'desc')->limit(1)->first();
     }
     //var_dump($lastMenu->start_date);
     $menuProducts = $menu->menuProducts()->get();
     $entries = [];
     foreach ($menuProducts as $mp) {
         $entry = [];
         $product = $mp->product()->first();
         $entry[] = $product->id;
         $entry[] = substr($product->getName(), 0, 60);
         $entry[] = $mp->getPrice();
         $entry[] = $product->getCategory();
         $entry[] = $mp->menuOrderProducts()->get()->sum('quantity');
         $entries[] = $entry;
         //$entries[] = new TableSeparator();
     }
     // usort($entries, function($a, $b) {
     //     return $a[3] > $b[3];
     // });
     $table = new Table($output);
     $table->setHeaders(array('ID', 'Prodotto', 'Prezzo', 'Categoria', 'Ordini del giorno precedente'))->setRows($entries);
     $table->render();
 }
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(" ");
     });
 }
 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 invio ordine ' . $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");
         //$menuDate = "{$y}-{$m}-{$d} 00:00:00";
         $output->writeLn("Importando Ordini del " . $matched[0]);
         // --------------------------------------------------------------------
         $agency = Agency::firstOrCreate(['name' => 'Mosaicoon']);
         $c = Order::where(['delivery_time' => $menuDate->copy()->addHours(13), 'agency_id' => $agency->id])->count();
         if ($c !== 0) {
             //ordine già registrato
             $output->writeLn("Ordine del " . $matched[0] . " GIA SALVATO");
             return;
         }
         //prendi menu del giorno
         try {
             $menu = Menu::where(['start_date' => $menuDate, 'type' => Menu::TYPE_PRANZO])->firstOrFail();
         } catch (\Exception $e) {
             $output->writeLn("Menu del " . $matched[0] . " NON PRESENTE");
             return;
         }
         $crawler = new \Symfony\Component\DomCrawler\Crawler();
         $crawler->addHTMLContent($content, 'UTF-8');
         $crawler->filter('table')->eq(1)->filter('tr')->each(function ($tr, $i) use($menu, $agency, $menuDate) {
             if ($i === 0) {
                 return;
             }
             $ordData = [];
             $tr->filter('td')->each(function ($td, $i) use(&$ordData) {
                 if ($i == 0) {
                     $ordData['user'] = $td->text();
                 } elseif ($i == 2) {
                     $ordData['product-name'] = trim(str_replace(['[NUOVO]', '(NUOVO)', '(Nuovo)'], null, $td->text()));
                 } elseif ($i == 3) {
                     $ordData['qta'] = (int) $td->text();
                 } elseif ($i == 1) {
                     $ordData['category'] = trim($td->text());
                 }
             });
             if ($ordData['user'] === 'TOTALE') {
                 return;
             }
             //prevent insert injection code
             if (in_array($ordData['category'], ['', null, 'code', 'c'])) {
                 return;
             }
             try {
                 $pid = $menu->menuProducts()->get()->lists('product_id');
                 $product = Product::whereIn('id', $pid)->where('description', '=', $ordData['product-name'])->firstOrFail();
             } catch (\Exception $e) {
                 //Prodotto Mancante
                 echo 'P';
                 return;
             }
             //dd($product);
             //da rivedere...
             $user = User::firstOrCreate(['agency_id' => $agency->id, 'display_name' => $ordData['user'], 'role' => User::ROLE_USER]);
             $order = Order::firstOrCreate(['user_id' => $user->id, 'agency_id' => $agency->id, 'delivery_time' => $menuDate->copy()->addHours(13)]);
             try {
                 $order->addMenuProduct($menu, $product, $ordData['qta']);
             } catch (\Exception $e) {
                 //Associazione NON TROVATA
                 echo 'X';
                 return;
             }
             echo '.';
         });
         $output->writeLn(" ");
     });
 }