<?php
class WarehouseStockSummary extends \BaseController {

	/**
	* Display a listing of the resource.
	*
	* @return Response
	*/
	public function index()
	{
		$maul = Warehouse::where('warehouse_id','=',1)->orderBy('item_code')->get();
		$nms = Warehouse::where('warehouse_id','=',6)->orderBy('item_code')->get();
		$jms = Warehouse::where('warehouse_id','=',22)->orderBy('item_code')->get();

		return View::make('warehouse.index')
		->with('maul',$maul)
		->with('nms',$nms)
		->with('jms',$jms);
	}

	public function create()
	{
		$warehouse = Supplier::whereIn('id', array(1,22,6))->lists('name', 'id');
		$commodity = Commodity::whereIn('id', array(58,33,34,35,36,37,38,39,40,65,68,43,44,45,50,47,48,140,13,352,61,64,73,63,10,59,140,13,14,61,64,63,18,19,20,21,22,23,24,25,26,322,316,29,30,31,55,454,94,95,54))->lists('name', 'id');
		return View::make('warehouse.create')
		->with('warehouse', $warehouse)
		->with('commodity', $commodity);
	}

	public function store()
	{
		//
		$rules = array(
			'item_code' => 'required',
			'commodity_id' => 'required',
			'batch_number' => 'required',
			'batch_quantity' => 'required',
			'shelf_life' => 'required|numeric');
			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails()) {
				return Redirect::back()->withErrors($validator);
			} else {

				$warehouse = new Warehouse;
				$warehouse->item_code = Input::get('item_code');
				$warehouse->warehouse_id = Input::get('warehouse_id');
				$warehouse->commodity_id = Input::get('commodity_id');
				$warehouse->total_quantity = Input::get('total_quantity');
				$warehouse->batch_number = Input::get('batch_number');
				$warehouse->batch_quantity = Input::get('batch_quantity');
				$warehouse->expiry_date = Input::get('expiry_date');
				$warehouse->shelf_life = Input::get('shelf_life');
				$warehouse->created_by = Auth::User()->id;
			}
			try{
				$warehouse->save();
				return Redirect::route('warehouse.index')
				->with('message', trans('Warehouse StockStatus succesfully Added	'));
			}catch(QueryException $e){
				Log::error($e);
			}
		}

		/**
		* Display the specified resource.
		*
		* @param  int  $id
		* @return Response
		*/
		public function show($id)
		{
			//
		}

		/**
		* Show the form for editing the specified resource.
		*
		* @param  int  $id
		* @return Response
		*/
		public function edit($id)
		{
			//
			$warehouse = Warehouse::find($id);
			$stores = Supplier::whereIn('id', array(1,22,6))->lists('name', 'id');
			$commodity = Commodity::whereIn('id', array(58,33,34,35,36,37,38,39,40,65,68,43,44,45,50,47,48,140,13,352,61,64,73,63,10,59,140,13,14,61,64,63,18,19,20,21,22,23,24,25,26,322,316,29,30,31,55,454,94,95,54))->lists('name', 'id');
        
				//Open the Edit View and pass to it the $patient
			return View::make('warehouse.edit')
			->with('stores', $stores)
			->with('commodity', $commodity)
			->with('warehouse', $warehouse);
		}
		/**
		* Update the specified resource in storage.
		*
		* @param  int  $id
		* @return Response
		*/
		public function update($id)
		{

			//
			$rules = array(
				'item_code' => 'required',
				'commodity_id' => 'required',
				'batch_number' => 'required',
				'batch_quantity' => 'required',
				'shelf_life' => 'required|numeric');
				$validator = Validator::make(Input::all(), $rules);

				if ($validator->fails()) {
					return Redirect::back()->withErrors($validator);
				} else {

					$warehouse = Warehouse::find($id);
					$warehouse->item_code = Input::get('item_code');
					$warehouse->warehouse_id = Input::get('warehouse_id');
					$warehouse->commodity_id = Input::get('commodity_id');
					$warehouse->total_quantity = Input::get('total_quantity');
					$warehouse->batch_number = Input::get('batch_number');
					$warehouse->batch_quantity = Input::get('batch_quantity');
					$warehouse->expiry_date = Input::get('expiry_date');
					$warehouse->shelf_life = Input::get('shelf_life');
					$warehouse->created_by = Auth::User()->id;
				}
				try{
					$warehouse->save();
					return Redirect::route('warehouse.index')
					->with('message', trans('Warehouse StockStatus succesfully Updated'));
				}catch(QueryException $e){
					Log::error($e);
				}

		}

		/**
		* Remove the specified resource from storage.
		*
		* @param  int  $id
		* @return Response
		*/

		public function delete($id)
		{
			//
			$commodity = Warehouse::find($id);
			$commodity->delete();

			return Redirect::route('warehouse.index')
			->with('message', trans('Record Succesfully Deleted'));
		}
	}
