public function getDistributeMonth(){
		$min_year = ReceiptMonth::min('year');
		if($min_year==0){
			$min_year = intval(date('Y'));
		}
		$arr_month_year = ReceiptMonth::select('year','month')
						->groupBy('year','month')
						->where('type_receipt','=','distribute')
						->orderBy('year','DESC')
						->orderBy('month','DESC')
						->get()->toArray();

		$this->layout->content = view('revenue.revenue-distribute-month',[	
							'arr_month_year' 	=> 	$arr_month_year,
							'min_year'		=>	$min_year
						]);
	}
	public function anyTonDauThang(Request $request){


		if($request->has('input-sort')){
			$arr_sort = $request->input('input-sort');
			$arr_sort =(array) json_decode($arr_sort);
		}elseif( session('sort_filter_product_ton_dau_thang.arr_sort') !== null){
			$arr_sort = session('sort_filter_product_ton_dau_thang.arr_sort');
		}else{
			$arr_sort=array();
		}
		session('sort_filter_product_ton_dau_thang.arr_sort', $arr_sort);

		$list_month = ReceiptMonth::select('month','year')
									->distinct()->where('month','>',0)
									->orderBy('year','ASC')
									->orderBy('month','ASC')
									->get()->toArray();
		if($request->has('input-filter')){
			$arr_filter = $request->input('input-filter');
		}elseif( session('sort_filter_product_ton_dau_thang.arr_filter') !== null ){
			$arr_filter = session('sort_filter_product_ton_dau_thang.arr_filter');
		}else{
			$arr_filter=[
					'sku'=>'',
					'like_name'=>'',
					'name'=>'',
					'company_id'=>'',
					'oum_id'=>''
				       ];
			if(count($list_month)){
				$arr_filter['month'] = $list_month[0]['month'].'-'.$list_month[0]['year'];
			}
		}

		session(['sort_filter_product_ton_dau_thang.arr_sort'=>$arr_sort]);
		session(['sort_filter_product_ton_dau_thang.arr_filter'=> $arr_filter]);

		$list_all_product = Product::select('sku','name')->orderBy('sku')->get()->toArray();
		$list_month = array();
		//Init array
		$distributes = array();
		$oums = array();
		$producttypes = array();
		$list_sku = array();
		//Get value array
		$distributes = Company::getDistributeList()->get()->toArray();
		$oums = Oum::orderBy('name')->get()->toArray();
		$producttypes = ProductType::get()->toArray();
		$list_all_product = Product::select('sku','name')->orderBy('sku')->get()->toArray();

		$list_product = TonDauThang::select(
					'products.id',
					'products.name',
					'products.sku',
					'ton_dau_thangs.quantity',
					'ton_dau_thangs.month',
					'ton_dau_thangs.year',
					'm_products.oum_id',
					'm_products.specification',
					'm_products.origin_price',
					'companies.name as company_name',
					'oums.name as oum_name'
		            )
					->addSelect(DB::raw('product_stocks.in_stock/m_products.specification as real_in_stock'))
					->leftJoin('m_products','m_products.id','=','ton_dau_thangs.m_product_id')
					->leftJoin('products','products.id','=','m_products.product_id')
					->leftJoin('oums','oums.id','=','m_products.oum_id')	
					->leftJoin('companies','companies.id','=','m_products.company_id')
					->leftJoin('product_stocks','m_products.id','=','product_stocks.m_product_id')
					->groupBy('products.id','m_products.company_id','m_products.oum_id','m_products.specification','m_products.origin_price')
					->where('ton_dau_thangs.quantity','>',0);

					
		foreach ($arr_filter as $key => $value) {
			if($value!=''){
				if($arr_filter['sku']!=''){
					$list_product->where('products.sku',$arr_filter['sku']);
				}
				if($key == 'like_name'){
					$list_product->where('products.name','LIKE',"%".$arr_filter['like_name']."%");
					$arr_filter['name']='';
				}
				if($key == 'name' && $arr_filter['name']!=''){
					$list_product->where('products.name',$arr_filter['name']);
				}
				if($key == 'company_id'){
					$list_product->where('m_products.company_id',$arr_filter['company_id']);
				}
				if($key == 'status'){
					$list_product->where('products.status',$arr_filter['status']);
				}
				if($key == 'oum_id'){
					$list_product->where('m_products.oum_id',$arr_filter['oum_id']);
				}
				if($key == 'month' && $arr_filter['month'] !=''){
					$month_year = explode('-',$arr_filter['month']);
					$list_product->where('ton_dau_thangs.month','=',$month_year[0])
								->where('ton_dau_thangs.year','=',$month_year[1]);
				}
			}
		}

		foreach ($arr_sort as $key => $value) {
			if($key=='company_id'){
				$list_product = $list_product->orderBy('companies.name',$value);
			}elseif($key=='oum_id'){
				$list_product = $list_product->orderBy('oums.name',$value);
			}elseif($key=='id'){
				$list_product = $list_product->orderBy('products.id',$value);
			}elseif($key=='specification'){
				$list_product = $list_product->orderBy('m_products.specification',$value);
			}elseif($key=='quantity'){
				$list_product = $list_product->orderBy('ton_dau_thangs.quantity',$value);
			}else{
				$list_product = $list_product->orderBy($key,$value);
			}
		}
		$list_month = ReceiptMonth::select('month','year')
									->distinct()->where('month','>',0)
									->orderBy('year','ASC')
									->orderBy('month','ASC')
									->get()->toArray();
		$list_product = $list_product->orderBy('products.id','asc');
		\Cache::put('list_product'.\Auth::user()->id, $list_product->get()->toArray(), 30);
		$sum_invest =  0;

		foreach ($list_product->get()->toArray() as $key => $value) {
			$sum_invest += $value['origin_price'] * $value['quantity'];
		}
		$list_product = $list_product->paginate(100);

		$this->layout->content=view('product.list-ton-dau-thang', [
								'distributes'			=> $distributes,
								'oums'					=> $oums,
								'producttypes'			=> $producttypes,
								'list_product'			=> $list_product,
								'list_all_product'		=> $list_all_product,
								'arr_sort' 				=> $arr_sort,
								'arr_filter' 			=> $arr_filter,
								'sum_invest'			=> $sum_invest,
								'list_month'			=> $list_month
							        ]);
	}
	static public function anyUpdateReceiptMonth(){
		$year = intval(date('Y'));
		$month = intval(date('m'));

		// Customer
		$list_order = ReceiptMonth::select('receipt_months.*','companies.name as company_name','companies.id as company_id')
					->where('year','=',$year)
					->where('month','=',$month)
					->where('type_receipt','=','customer')
					->leftJoin('companies','companies.id','=','receipt_months.company_id')
					->orderBy('companies.name')
					->get()->toArray();
		foreach ($list_order as $key => $value) {
			ReceiptMonth::where('year','=',$value['year'])
					->where('month','=',$value['month'])
					->where('company_id','=',$value['company_id'])
					->where('type_receipt','=',$value['type_receipt'])
					->update([
							'con_lai' => ($value['sum_amount'] - $value['paid'] + $value['no_cu'])
						]);
			$list_order[$key]['con_lai']=$list_order[$key]['sum_amount'] - $list_order[$key]['paid'] + $list_order[$key]['no_cu'];
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		// Distribute
		$list_order = ReceiptMonth::select('receipt_months.*','companies.name as company_name','companies.id as company_id')
					->where('year','=',$year)
					->where('month','=',$month)
					->where('type_receipt','=','distribute')
					->leftJoin('companies','companies.id','=','receipt_months.company_id')
					->orderBy('companies.name')
					->get()->toArray();
		foreach ($list_order as $key => $value) {
			ReceiptMonth::where('year','=',$value['year'])
					->where('month','=',$value['month'])
					->where('company_id','=',$value['company_id'])
					->where('type_receipt','=',$value['type_receipt'])
					->update([
							'con_lai' => ($value['sum_amount'] - $value['paid'] + $value['no_cu'])
						]);
			$list_order[$key]['con_lai']=$list_order[$key]['sum_amount'] - $list_order[$key]['paid'] + $list_order[$key]['no_cu'];
		}
		date_default_timezone_set('Asia/Saigon');
		return date('d-m-Y G:i:s')."\n";
	}