public function destroy($id) { $ItemCategories = ItemCategories::findOrFail($id); $items = Items::where(['category_id' => $id])->get(); Helper::add($id, 'deleted items category ' . $ItemCategories->title); if (count($items) > 0) { $category = ItemCategories::where(['title' => 'Uncategorized'])->first(); if (!$category) { $category = ItemCategories::create(['title' => 'Uncategorized']); } Items::where(['category_id' => $id])->update(['category_id' => $category->id]); } if ($ItemCategories->title == 'Uncategorized' && count($items) > 0) { } else { $ItemCategories->delete(); } Session::flash('flash_message', $this->title . ' successfully deleted!'); return Redirect::action('ItemCategoriesController@index'); }
public function exportExcel($stock, $category) { $data = $this->getData($stock, $category); $c = ItemCategories::findOrFail($category); $s = StockPeriods::findOrFail($stock); $ready = []; $total = 0; foreach ($data['items'] as $key => $d) { $ready[$key] = ['ID' => $key, 'Title' => $d['title'], 'Price per unit (£)' => $d['purchases']['price'] ? $d['purchases']['price'] : 'not set', 'Opening Stock' => $d['last_stock'] . ' ' . $d['units'], 'Purchases' => $d['purchases']['value'] . ' ' . $d['units'], 'Sales' => $d['sales'] . ' ' . $d['units'], 'Wastage' => $d['wastage'] . ' ' . $d['units'], 'Predicted' => $d['must_stock'] . ' ' . $d['units'], 'Closing Stock' => $d['current_stock'] . ' ' . $d['units'], 'Difference' => $d['stock_difference'] . ' ' . $d['units'], 'Variance' => '£ ' . $d['variance']]; $total += $d['variance']; } $ready[] = ['ID' => '', 'Title' => '', 'Price per unit (£)' => '', 'Opening Stock' => '', 'Purchases' => '', 'Sales' => '', 'Wastage' => '', 'Predicted' => '', 'Closing Stock' => '', 'Difference' => 'TOTAL', 'Variance' => '£ ' . $total]; Excel::create($c->title, function ($excel) use($ready, $s, $c) { $excel->sheet('Sheetname', function ($sheet) use($ready, $s, $c) { $sheet->setAutoSize(true); $sheet->mergeCells('A1:K1'); $sheet->mergeCells('A2:K2'); //header $sheet->setHeight(1, 40); $sheet->row(1, function ($row) { $row->setFontSize(30); }); $sheet->row(1, array('Stock#' . $s->number . ' (' . $s->date_from . ' - ' . ($s->date_to ? $s->date_to : 'NOW') . ')')); //category $sheet->setHeight(2, 30); $sheet->row(2, function ($row) { $row->setFontSize(20); }); $sheet->row(2, array('Category: ' . $c->title)); //table headers $sheet->setHeight(3, 20); $sheet->row(3, function ($row) { $row->setFontWeight('bold'); }); $sheet->cells('C', function ($cells) { $cells->setAlignment('left'); $cells->setFontSize(10); }); //table data $sheet->setFontSize(10); $sheet->fromArray($ready, null, 'A3'); }); })->export('xls'); }