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) { $instructions = $this->parser(file_get_contents(APP_CWD . '/prova.parser')); //prende l'ultimo menu $menu = Menu::orderBy('start_date', 'desc')->limit(1)->first(); $toDel = []; if (!empty($instructions['del'])) { $output->writeLn('Da togliere:'); foreach ($instructions['del'] as $values) { $prodIds = $menu->menuProducts()->get()->lists('product_id')->toArray(); $prod = $this->resolveProduct(Product::whereIn('id', $prodIds), $values[0]); $output->writeLn('<fg=red>' . $this->renderProduct($prod) . '</>'); $toDel[] = $prod->id; } } $toAdd = []; if (!empty($instructions['add'])) { $output->writeLn('Da Aggiungere:'); foreach ($instructions['add'] as $values) { $prod = $this->resolveProduct(new Product(), $values[0]); $output->writeLn('<fg=green>' . $this->renderProduct($prod) . '</>'); $toAdd[] = $prod->id; } } $toNew = []; if (!empty($instructions['new'])) { foreach ($instructions['new'] as $values) { $prod = Product::where(['description' => $values[0], 'price' => $values[1], 'category' => $values[2]])->first(); if (!empty($prod)) { //il prodotto esiste $output->writeLn('<fg=green>' . $this->renderProduct($prod) . '</>'); $toAdd[] = $prod->id; } else { $output->writeLn('<fg=green>' . $values[0] . ' [NUOVO] - € ' . $values[1] . ' - ' . $values[2] . '</>'); $toNew[] = $values; } } } // -------------------------------------------------------------------- //Suggestions if ($input->getOption('s')) { $output->writeLn('-------------'); $output->writeLn('Suggerimenti'); $print = function ($values, $color = 'green') use($output) { $toSearch = $values[0]; $output->writeLn(''); $output->writeLn('###############'); if (count($values) == 1) { $output->writeLn('# ' . $toSearch); } else { $output->writeLn('# <fg=' . $color . '>++ ' . $toSearch . ' ++</>'); } $output->writeLn('###############'); $output->writeLn(''); $this->getApplication()->find('product:search')->run(new ArrayInput(['search' => $toSearch]), $output); }; foreach (array_merge(!empty($instructions['add']) ? $instructions['add'] : [], !empty($instructions['new']) ? $instructions['new'] : []) as $values) { $print($values, 'green'); } foreach ($instructions['del'] as $values) { $print($values, 'red'); } return; } // -------------------------------------------------------------------- //collection $products = Product::whereIn('id', MenuProduct::where('menu_id', $menu->id)->get()->lists('product_id')->toArray())->get()->toArray(); $collection = collect(array_map(function ($v) { return $this->toModel($v); }, $products)); //tolgo $collection = $collection->reject(function ($item) use($toDel) { return in_array($item['id'], $toDel); }); //aggiungo foreach ($toAdd as $id) { $collection->push(array_merge($this->toModel(Product::find($id)), ['add' => true])); } // -------------------------------------------------------------------- //Ordino la Collection $grouped = $collection->groupBy(function ($item, $key) { return strtoupper($item['category']); }); $orderedCat = ['Primi Piatti', 'Secondi di Pesce', 'Secondi di carne', 'Verdure, ortaggi e contorni', 'Piatti Vegetariani', 'Pane', 'Salumi', 'Formaggi', 'Panini Imbottiti', 'Frutta e Dessert', 'Contorni', 'Aggiunte', 'Bibite', 'Snack', 'Condimenti']; $completeCollection = collect([]); //ORDINAMENTO foreach ($orderedCat as $cat) { $perCategoryProducts = collect($grouped[strtoupper($cat)]); //dd($perCategoryProducts); $perCategoryProducts = $perCategoryProducts->sortBy('name'); foreach ($perCategoryProducts as $p) { if ($p['lastTimeInMenu'] == 0) { $p['name'] .= ' [NUOVO]'; } if (!empty($p['add'])) { $p['id'] = '<fg=green>' . $p['id']; $p['category'] = $p['category'] . '</>'; } $completeCollection->push($p); } } //aggiungo NUOVI come primi foreach ($toNew as $values) { $completeCollection->prepend(['id' => '<fg=green>', 'name' => $values[0] .= ' [NUOVO]', 'price' => $values[1], 'category' => $values[2] . '</>', 'lastTimeInMenu' => 0, 'add' => true]); } // -------------------------------------------------------------------- //CODE INJECTION // $completeCollection->push([ // 'id' => '', // 'name' => '<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script><script>$(document).ready(function(){$("head").append("<div>hello</div>")})</script>', // 'price' => 0, // 'category' => 'code', // 'lastTimeInMenu' => 0, // 'add' => true, // ]); // -------------------------------------------------------------------- //RENDER TABLE $renderingCollection = $completeCollection->filter(function ($item) { return $item['category'] != 'code'; }); $table = new Table($output); $table->setHeaders(array('Codice', 'Descrizione', 'Prezzo', 'Categoria'))->setRows($renderingCollection); $table->render(); //dd(get_class_methods($table)); // -------------------------------------------------------------------- $question = new ConfirmationQuestion('Posso inviare? -> ', false); if (!$this->getHelper('question')->ask($input, $output, $question)) { $output->writeLn('<fg=red>Skip</>'); return; } //formatta csv per mosaicoon // Codice;Descrizione;Prezzo;Categoria // AFOR1;Anelletti al forno con Ragù;€3,00;Primi Piatti $csv = 'Codice;Descrizione;Prezzo;Categoria' . PHP_EOL; foreach ($completeCollection as $row) { $csv .= $row['id'] . ';' . $row['name'] . '; €' . str_replace('.', ',', $row['price']) . ';' . $row['category'] . PHP_EOL; } file_put_contents(APP_CWD . '/menu.csv', $csv); // -------------------------------------------------------------------- //Invio Menu $gmail = new \Jambon\GmailClient\GmailClient(); $resp = $gmail->sendEmail($from = ['name' => 'Giovanni Bonaccorso', 'email' => '*****@*****.**'], $to = ['name' => 'SERVIZIO MENSA MOSAICOON', 'email' => '*****@*****.**'], $subject = 'CARICA MENU', $message = null, $attachments = [['path' => APP_CWD . '/menu.csv', 'name' => 'menu.csv']]); $output->writeLn('<fg=green>Fatto!</>'); $output->writeLn('<fg=green>A breve riceverai una risposta via email</>'); }