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); } } }
protected function execute(InputInterface $input, OutputInterface $output) { $fixes = [['Carciofi a Cotoloetta', 'Carciofi a Cotoletta'], ['Insalata primavera con patate fagilini e pomodori', 'Insalata Primavera con patate, fagiolini e pomodori'], ['"Pasta al “macco"" di fave"', 'Pasta al macco di fave'], ['"Pasta al “macco"" di piselli secchi"', 'Pasta al macco di piselli secchi'], ['Pollo con Patate al forno', 'Cosce di Pollo con Patate al Forno', 3], [['description' => 'Pasta con Patate', 'price' => 2.8], 'Pasta con Patate', 3], ['Pasta con Patate (asciutta)', 'Pasta con Patate', 3], ['Pasta con Patate (Rigatoni)', 'Pasta con Patate', 3], ['Pasta e fagioli cannellini', 'Pasta e fagioli'], ['Pasta fredda con tonno speck e mozzarella', 'Pasta fredda con tonno, speck e mozzarella'], ['Funghi al forno (Pleus)', 'Funghi al forno'], ['Funghi Pleus al forno (a foglia larga)', 'Funghi al forno'], ['Pasta con lenticchie', 'Pasta e lenticchie'], [['description' => 'Pasta alla Amatriciana', 'price' => 3.0], 'Pasta alla Amatriciana', 3.8], [391, 6], [['description' => 'Penne alla Amatriciana', 'price' => 3.0], 'Pasta alla Amatriciana', 3.8], [['description' => 'Casarecce alla Amatriciana', 'price' => 3.0], 'Pasta alla Amatriciana', 3.8]]; foreach ($fixes as $fix) { $p = Product::fixProduct($fix[0], $fix[1], @$fix[2], @$fix[3]); if (!empty($p)) { echo '.'; } } $output->writeLn("Fatto!"); }
protected function execute(InputInterface $input, OutputInterface $output) { $search = $input->getArgument('search'); if (is_numeric($search)) { $q = Product::where('id', '=', $input); } else { $q = Product::whereLike($search); } $products = $q->select(['id', 'description', 'price', 'category'])->limit(15)->get(); $products = $products->toArray(); $table = new Table($output); $table->setHeaders(array('Codice', 'Descrizione', 'Prezzo', 'Categoria'))->setRows($products)->setStyle('borderless'); $table->render(); }
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(" "); }); }
/** * Turn this item object into a generic array * * @return array */ public function transform(Product $product) { return ['id' => $product->id, 'name' => $product->getName(), 'price' => $product->getPrice(), 'category' => $product->getCategory()]; }
/** * Aggiunge un prodotto al menu * * @param Product $product * @param int $availability (optional) -> infinite * @param float $price (optional) -> Product Inherithed * @return Menu */ public function addProduct(Product $product, $availability = null, $price = null) { $price = !is_null($price) && is_float($price) ? $price : $product->getPrice(); MenuProduct::create(['availability' => $availability, 'price' => $price, 'product_id' => $product->id, 'menu_id' => $this->id]); return $this; }
/** * Ritorna il prezzo del prodotto * * @return string */ public function scopeWhereLike($query, $search) { $searchWords = $this->toWords($search); $searchCallback = function (Product $p) use($searchWords) { $score = 0; $words = $this->toWords($p->getName()); foreach ($searchWords as $w) { if (in_array($w, $words)) { $score++; } } return $score; }; $products = Product::all()->sortBy($searchCallback); $ids = array_slice(array_reverse($products->lists('id')->toArray()), 0, 10); return $query->whereIn('id', $ids)->orderByRaw('FIELD(id, ' . implode(',', $ids) . ')'); }
/** * @param string $content * @return array */ private function parser($content) { $instructions = []; $parseRow = function ($row) { $values = array_filter(array_map(function ($v) { if (is_numeric($v)) { //caso di ID $prod = Product::find($v); if (!empty($prod)) { //var_dump($v, $prod->getName()); die; // -------------------------------------------------------------------- // $ps = Product::whereLike($prod->getName())->limit(1)->get(); // var_dump($ps->toArray()); // die; // -------------------------------------------------------------------- return $prod->id; } } else { return trim($v); } }, explode(';', trim(str_replace(['-', '+'], null, $row)))), function ($v) { return !empty($v); }); if (isset($values[1])) { $values[1] = (double) str_replace(',', '.', str_replace('€', null, $values[1])); } return $values; }; foreach (explode(PHP_EOL, $content) as $row) { //var_dump($row); continue; $row = trim($row); if (empty($row)) { continue; } $start = substr(trim($row), 0, 1); if ($start == '-') { //togli $instructions['del'][] = $parseRow($row); } elseif ($start == '+') { $plusOcc = substr_count($row, '+'); if ($plusOcc == 1) { //add $instructions['add'][] = $parseRow($row); } elseif ($plusOcc == 2) { //insert $instructions['new'][] = $parseRow($row); } } } //die; return $instructions; }
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(" "); }); }