Exemplo n.º 1
0
 public function index()
 {
     $currentPeriodId = Helper::currentPeriodId();
     if ($currentPeriodId) {
         $search = Input::get('q');
         $filter = Input::get('filter');
         $not_updated = Items::select('count(*)')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->count();
         $items = $search ? Items::where('title', 'LIKE', '%' . $search . '%')->get() : ($filter == 'without_stock' ? Items::select('items.*')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->get() : Items::all());
         if (count($items) == 1 && $search) {
             return redirect()->action('StockCheckController@edit', ['item_id' => $items->first()->id]);
         }
         $last_period = StockPeriods::where(['number' => StockPeriods::findOrFail($currentPeriodId)->number - 1])->get();
         return view('StockCheck.index')->with(array('title' => $this->title, 'items' => $search ? Items::where('title', 'LIKE', '%' . $search . '%')->take(10)->get() : ($filter == 'without_stock' ? Items::select('items.*')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->orderBy("updated_at", 'DESC')->get() : Items::orderBy("updated_at", 'DESC')->take(10)->get()), 'item_list' => Items::lists('title'), 'search' => $search, 'period' => $currentPeriodId, 'previous' => count($last_period) > 0 ? $last_period->first()->id : 0, 'filter' => $filter, 'not_updated' => $not_updated));
     } else {
         Session::flash('flash_message', 'You need to start a period first!');
         return Redirect::action('StockPeriodsController@index');
     }
 }
Exemplo n.º 2
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     $last_period = Helper::defaultPeriodId();
     $current_period = Helper::periodAfterId($last_period);
     $item_purchases = [];
     $last_stock = [];
     $current_stock = [];
     $item_sales = [];
     $item_wastes = [];
     $items = [];
     $wastage = [];
     $sales_chart = [];
     $items_without_price = 0;
     $period = null;
     if ($last_period) {
         $purchases = Purchases::orderBy('date_created', 'ASC')->where(['stock_period_id' => $last_period])->get();
         foreach ($purchases as $purchase) {
             foreach ($purchase->purchases()->get() as $itemPurchase) {
                 if (array_key_exists($itemPurchase->item_id, $item_purchases)) {
                     $item_purchases[$itemPurchase->item_id]['value'] += $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['occurrences']++;
                     $item_purchases[$itemPurchase->item_id]['price'] += $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value;
                 } else {
                     $item_purchases[$itemPurchase->item_id]['value'] = $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['price'] = $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['occurrences'] = 1;
                 }
             }
         }
         foreach ($item_purchases as $key => $purchase) {
             $item_purchases[$key]['price'] = $purchase['price'] / $purchase['occurrences'];
         }
         $last_stock_items = StockItem::where(['stock_period_id' => $last_period])->get();
         foreach ($last_stock_items as $si) {
             if (array_key_exists($si->item_id, $last_stock)) {
                 $last_stock[$si->item_id] += $si->stock;
             } else {
                 $last_stock[$si->item_id] = $si->stock;
             }
         }
         $current_stock_items = StockItem::where(['stock_period_id' => $current_period])->get();
         foreach ($current_stock_items as $si) {
             if (array_key_exists($si->item_id, $current_stock)) {
                 $current_stock[$si->item_id] += $si->stock;
             } else {
                 $current_stock[$si->item_id] = $si->stock;
             }
         }
         $sales = Sales::where(['stock_period_id' => $last_period])->get();
         foreach ($sales as $sale) {
             foreach ($sale->sales()->orderBy('quantity', 'DESC')->get() as $sale_item) {
                 $menu = $sale_item->menu()->first();
                 if ($menu) {
                     $color = $this->rand_color();
                     $sales_chart[$menu->id] = ['value' => $sale_item->quantity, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $menu->title];
                     if ($menu->type == 'item') {
                         if (array_key_exists($menu->item_id, $item_sales)) {
                             $item_sales[$menu->item_id] += $menu->value * $sale_item->quantity;
                         } else {
                             $item_sales[$menu->item_id] = $menu->value * $sale_item->quantity;
                         }
                     } elseif ($menu->type == 'recipe') {
                         $recipe = $menu->recipe()->first();
                         if ($recipe) {
                             $usage = $this->countUsageFromRecipe($recipe);
                             foreach ($usage as $key => $use) {
                                 if (array_key_exists($key, $item_sales)) {
                                     $item_sales[$key] += $sale_item->quantity * $use;
                                 } else {
                                     $item_sales[$key] = $sale_item->quantity * $use;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $wastes = Wastes::where(['stock_period_id' => $last_period])->get();
         foreach ($wastes as $waste) {
             if (array_key_exists($waste->reason()->first()->id, $wastage)) {
                 $wastage[$waste->reason()->first()->id]['value'] = $wastage[$waste->reason()->first()->id]['value'] + 1;
             } else {
                 $color = $this->rand_color();
                 $wastage[$waste->reason()->first()->id] = ['value' => 1, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $waste->reason()->first()->reason];
             }
             if ($waste->type == 'item') {
                 if (array_key_exists($waste->item_id, $item_wastes)) {
                     $item_wastes[$waste->item_id] += $waste->value;
                 } else {
                     $item_wastes[$waste->item_id] = $waste->value;
                 }
             } elseif ($waste->type == 'recipe') {
                 $recipe = $waste->recipe()->first();
                 if ($recipe) {
                     $usage = $this->countUsageFromRecipe($recipe);
                     foreach ($usage as $key => $use) {
                         if (array_key_exists($key, $item_wastes)) {
                             $item_wastes[$key] += $waste->recipe_count * $use;
                         } else {
                             $item_wastes[$key] = $waste->recipe_count * $use;
                         }
                     }
                 }
             } elseif ($waste->type == 'menu') {
                 $menu = $waste->menu()->first();
                 if ($menu) {
                     if ($menu->type == 'item') {
                         if (array_key_exists($menu->item_id, $item_wastes)) {
                             $item_wastes[$menu->item_id] += $waste->menu_count * $menu->value;
                         } else {
                             $item_wastes[$menu->item_id] = $waste->menu_count * $menu->value;
                         }
                     } elseif ($menu->type == 'recipe') {
                         $recipe = $menu->recipe()->first();
                         if ($recipe) {
                             $usage = $this->countUsageFromRecipe($recipe);
                             foreach ($usage as $key => $use) {
                                 if (array_key_exists($key, $item_wastes)) {
                                     $item_wastes[$key] += $waste->menu_count * $use;
                                 } else {
                                     $item_wastes[$key] = $waste->menu_count * $use;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $period = StockPeriods::findOrFail($last_period);
     }
     $all_items = Items::all();
     $variance = 0;
     $count = 0;
     foreach ($all_items as $item) {
         if (!array_key_exists($item->category()->first()->id, $items)) {
             $items[$item->category()->first()->id]['category'] = $item->category()->first()->title;
             $items[$item->category()->first()->id]['variance'] = 0;
             $items[$item->category()->first()->id]['items'] = [];
         }
         $current_item = [];
         //$items[$item->id]['object'] = $item;
         $current_item['title'] = $item->title;
         $current_item['units'] = $item->units()->where(['default' => 1])->first()->unit()->first()->title;
         $current_item['wastage'] = array_key_exists($item->id, $item_wastes) ? $item_wastes[$item->id] : 0;
         $current_item['last_stock'] = array_key_exists($item->id, $last_stock) ? $last_stock[$item->id] : 0;
         $current_item['current_stock'] = array_key_exists($item->id, $current_stock) ? $current_stock[$item->id] : 0;
         if (array_key_exists($item->id, $item_purchases)) {
             $current_item['purchases'] = $item_purchases[$item->id];
         } else {
             $price = 0;
             $item_price = ItemPurchases::where(['item_id' => $item->id])->orderBy('created_at', 'DESC')->first();
             if ($item_price) {
                 $value = $item_price->value;
                 if ($value == 0) {
                     $value = 1;
                 }
                 $price = $item_price->price / $value;
             } else {
                 if ($item->price) {
                     $price = $item->price;
                 } else {
                     $items_without_price++;
                 }
             }
             $current_item['purchases'] = ['value' => 0, 'price' => $price, 'occurrences' => 0];
         }
         $current_item['sales'] = array_key_exists($item->id, $item_sales) ? $item_sales[$item->id] : 0;
         $current_item['must_stock'] = $current_item['last_stock'] + $current_item['purchases']['value'] - $current_item['sales'] - $current_item['wastage'];
         $current_item['stock_difference'] = $current_item['current_stock'] - $current_item['must_stock'];
         $current_item['variance'] = round($current_item['stock_difference'] * $current_item['purchases']['price'], 2);
         $variance += $current_item['variance'];
         $items[$item->category()->first()->id]['variance'] += $current_item['variance'];
         $items[$item->category()->first()->id]['items'][$item->id] = $current_item;
         $count++;
     }
     $summary_stock = Items::select('count(*)')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $current_period . ' and stock_items.item_id = items.id)')->count();
     $summary_invoices = Purchases::where(['stock_period_id' => $last_period])->count();
     $summary_sales = Sales::where(['stock_period_id' => $last_period])->count();
     $summary_menu = Menu::where(['checked' => 0])->count();
     $summary_wastes = Wastes::where(['stock_period_id' => $last_period])->count();
     return view('Client.index')->with(array('last_period' => $last_period, 'date_from' => $period ? date('Y-m-d', strtotime($period->date_from)) : date('Y-m-d', strtotime('-30 days')), 'date_to' => $period ? date('Y-m-d', strtotime($period->date_to)) : date('Y-m-d', time()), 'last_stock_summary_items' => $items, 'variance' => $variance, 'items' => $items, 'count' => $count, 'sales' => count($sales_chart) > 10 ? array_slice($sales_chart, 0, 10) : $sales_chart, 'wastage' => $wastage, 'summary' => ['stock' => $summary_stock, 'invoices' => $summary_invoices, 'sales' => $summary_sales, 'menu' => $summary_menu, 'no_price' => $items_without_price, 'wastes' => $summary_wastes]));
 }
Exemplo n.º 3
0
 function countAverages()
 {
     $items = [];
     $items_all = Items::select('items.price as price', 'items.title as title', 'item_categories.title as category', 'units.title as unit', 'suppliers.title as supplier', 'items.id as id', DB::raw('avg(item_purchases.price/item_purchases.value) AS average'))->leftJoin('item_categories', 'items.category_id', '=', 'item_categories.id')->leftJoin('item_purchases', 'items.id', '=', 'item_purchases.item_id')->leftJoin('purchases', 'item_purchases.purchase_id', '=', 'purchases.id')->leftJoin('suppliers', 'purchases.supplier_id', '=', 'suppliers.id')->leftJoin('item_units', 'items.id', '=', 'item_units.item_id')->leftJoin('units', 'item_units.unit_id', '=', 'units.id')->where(['item_units.default' => 1])->groupBy('items.id', 'suppliers.id')->orderBy('item_categories.title', 'ASC')->orderBy('items.title', 'ASC')->get();
     foreach ($items_all as $item) {
         $item_data['title'] = $item->title;
         $item_data['category'] = $item->category;
         $item_data['unit'] = $item->unit;
         $item_data['supplier'] = $item->supplier;
         if ($item->supplier) {
             $item_data['price'] = $item->average;
         } else {
             $item_data['price'] = $item->price;
         }
         $items[] = $item_data;
     }
     return $items;
 }