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
							        ]);
	}
	public function anyExportPdf(){
		$id_template = 3;
		$arr_print = 	[
				'arr_list' =>	[
						'arr_key' => 	[
								'sku',
								'name',
								'oum_name',
								'specification',
								'quantity',
								'origin_price',
								'invest'
								],
						'arr_head' => 	[
								['text'=>'Mã','class'=>''],
								['text'=>'Tên sản phẩm','class'=>''],
								['text'=>'Đơn vị','class'=>'center'],
								['text'=>'Quy cách','class'=>'center'],
								['text'=>'Số lượng','class'=>'center'],
								['text'=>'Đơn giá','class'=>'money'],
								['text'=>'Thành tiền','class'=>'money']
								],
						'arr_body'=>[],
						'arr_sum'=>[]
						],
				'arr_data'=>	[
							'id'=>session('current_returnpurchaseorder')
						]
				];	
		if (\Cache::has('list_product_rpo'.\Auth::user()->id)){
			$rpo = ReturnPurchaseorder::select('return_purchaseorders.*','companies.name')->where('return_purchaseorders.id','=',session('current_returnpurchaseorder'))
							->leftJoin('companies','companies.id','=','return_purchaseorders.company_id')->get()->first();
			$month = intval(date('m',strtotime($rpo->date)));
			$year = intval(date('Y',strtotime($rpo->date)));
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = $rpo->date;
			$list_order = array();
			$key_order = 1;
			$arr_print['arr_data']['date'] = date('d-m-Y',strtotime($rpo->date));
			$arr_print['arr_data']['company_name'] = $rpo->name;
			$arr_print['arr_data']['phone'] = $rpo->company_phone;
			$arr_print['arr_data']['address'] = '';
			$arr_dress = Address::select('addresses.*','provinces.name as province_name')
						->where('addresses.id','=',$rpo->address_id)
						->leftJoin('provinces','provinces.id','=','addresses.province_id')
						->get()->first();
			$arr_print['arr_data']['address'] .= $arr_dress->address?$arr_dress->address.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->town_city?$arr_dress->town_city.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->province_name?$arr_dress->province_name:'';

			$receipt_month_prev = ReceiptMonth::where('type_receipt','=','distribute')
								->where('company_id','=',$rpo->company_id)
								->where(function($query) use ($month,$year){
									$query->where(function($query2) use ($month,$year){
										$query2->where('month','<',$month)
											->where('year','=',$year);
									})->orWhere(function($query2) use ($month,$year){
										$query2->where('year','<',$year);
									});
								})
								->orderBy('year','desc')
								->orderBy('month','desc')
								->limit(1);
			$receipt_month_prev = $receipt_month_prev->first();

			if($receipt_month_prev){
				$arr_print['arr_data']['no_cu'] = $receipt_month_prev->con_lai;
			}else{
				$arr_print['arr_data']['no_cu'] = 0;
			}
			
			$list_po = Purchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rpo->company_id)
					->get()->toArray();
			foreach ($list_po as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
				$key_order++;
			}
			$list_rpo = ReturnPurchaseorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$rpo->company_id)
					->get()->toArray();

			foreach ($list_rpo as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
				$key_order++;
			}

			$list_paid = Paid::where('date','>=',$begin)
						->where('date','<',$end)
						->where('company_id','=',$rpo->company_id)
						->where('type_paid','=','distribute')
						->get()->toArray();
			foreach ($list_paid as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_paid'];
				$key_order++;
			}
			$date = array();
			foreach ($list_order as $key => $value) {
				$date[$key] = $value['date'];
			}
			array_multisort($date,SORT_ASC,$list_order);
			foreach ($list_order as $key => $value) {
				$arr_print['arr_data']['no_cu']+=$value['sum_amount'];
			}

			$arr_cache = \Cache::get('list_product_rpo'.\Auth::user()->id);
			$sum_invest = 0;
			foreach ($arr_cache as $key => $value) {
				$sum_invest += $value['invest'];
			}
			if($rpo->status==0){
				$arr_print['arr_data']['no_cu'] = 0;
			}
			$arr_print['arr_data']['toa_moi'] = $sum_invest;
			$arr_print['arr_data']['tong_cong'] = $arr_print['arr_data']['no_cu'] - $arr_print['arr_data']['toa_moi'];

			$arr_print['arr_data']['no_cu'] = number_format($arr_print['arr_data']['no_cu']);
			$arr_print['arr_data']['toa_moi'] = number_format(-$arr_print['arr_data']['toa_moi']);
			$arr_print['arr_data']['tong_cong'] = number_format($arr_print['arr_data']['tong_cong']);
			$arr_print['arr_list']['arr_sum'][] = [
				['value'=>'Tổng cộng:','colspan'=>'6'],
				['value'=>$sum_invest]
			];
			$arr_print['arr_list']['arr_body'] = $arr_cache;
			$link = ExportsController::getCreatePrintPdf($arr_print,$id_template,'phieu_tra_hang_ncc_so_'.$rpo->id,'potrait');
			Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','In đơn hàng trả nhà cung cấp số '.session('current_returnpurchaseorder'));
			return redirect($link);
		}
		die;
	}
	public function anyExportExcel(){
		$id_template = 5;
		$arr_print = 	[
				'arr_list' =>	[
						'arr_key' => 	[
								'sku',
								'name',
								'oum_name',
								'specification',
								'quantity',
								'sell_price',
								'amount'
								],
						'arr_head' => 	[
								['text'=>'STT','class'=>''],
								['text'=>'Mã','class'=>''],
								['text'=>'Tên sản phẩm','class'=>''],
								['text'=>'Đơn vị','class'=>'center'],
								['text'=>'Quy cách','class'=>'center'],
								['text'=>'Số lượng','class'=>'center'],
								['text'=>'Đơn giá','class'=>'money'],
								['text'=>'Thành tiền','class'=>'money']
								],
						'arr_body'=>[],
						'arr_sum'=>[]
						],
				'arr_data'=>	[
							'id'=>session('current_saleorder')
						]
				];	
		if (\Cache::has('list_product_so'.\Auth::user()->id)){
			$so = Saleorder::select('saleorders.*','companies.name')
							->where('saleorders.id','=',session('current_saleorder'))
							->leftJoin('companies','companies.id','=','saleorders.company_id')->get()->first();
			$month = intval(date('m',strtotime($so->date)));
			$year = intval(date('Y',strtotime($so->date)));
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = $so->date;
			$list_order = array();
			$key_order = 1;
			$arr_print['arr_data']['date'] = date('d-m-Y',strtotime($so->date));
			$arr_print['arr_data']['company_name'] = $so->name;
			$arr_print['arr_data']['phone'] = $so->company_phone;
			$arr_print['arr_data']['address'] = '';
			$arr_dress = Address::select('addresses.*','provinces.name as province_name')
						->where('addresses.id','=',$so->address_id)
						->leftJoin('provinces','provinces.id','=','addresses.province_id')
						->get()->first();
			$arr_print['arr_data']['address'] .= $arr_dress->address?$arr_dress->address.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->town_city?$arr_dress->town_city.', ':'';
			$arr_print['arr_data']['address'] .= $arr_dress->province_name?$arr_dress->province_name:'';

			$receipt_month_prev = ReceiptMonth::where('type_receipt','=','customer')
								->where('company_id','=',$so->company_id)
								->where(function($query) use ($month,$year){
									$query->where(function($query2) use ($month,$year){
										$query2->where('month','<',$month)
											->where('year','=',$year);
									})->orWhere(function($query2) use ($month,$year){
										$query2->where('year','<',$year);
									});
								})
								->orderBy('year','desc')
								->orderBy('month','desc')
								->limit(1);
			$receipt_month_prev = $receipt_month_prev->first();
			if($receipt_month_prev){
				$arr_print['arr_data']['no_cu'] = $receipt_month_prev->con_lai;
			}else{
				$arr_print['arr_data']['no_cu'] = 0;
			}

			$list_po = Saleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$so->company_id)
					->get()->toArray();
			foreach ($list_po as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
				$key_order++;
			}
			$list_so = ReturnSaleorder::where('date','>=',$begin)
					->where('date','<',$end)
					->where('status','=',1)
					->where('company_id','=',$so->company_id)
					->get()->toArray();

			foreach ($list_so as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
				$key_order++;
			}

			$list_paid = Paid::where('date','>=',$begin)
						->where('date','<',$end)
						->where('company_id','=',$so->company_id)
						->where('type_paid','=','customer')
						->get()->toArray();
			
			foreach ($list_paid as $key => $value) {
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_paid'];
				$key_order++;
			}

			$date = array();
			foreach ($list_order as $key => $value) {
				$date[$key] = $value['date'];
			}
			array_multisort($date,SORT_ASC,$list_order);
			foreach ($list_order as $key => $value) {
				$arr_print['arr_data']['no_cu']+=$value['sum_amount'];
			}

			$arr_cache = \Cache::get('list_product_so'.\Auth::user()->id);
			$sum_amount = 0;
			$arr_check_idproduct = array();
			$arr_check_mproduct = array();

			foreach ($arr_cache as $key => $value) {
				if(!in_array($value['product_id'],$arr_check_idproduct)){
					$arr_check_idproduct[] = $value['product_id'];
					$arr_check_mproduct[$value['product_id']][]=$value;
					$arr_cache_tmp[$value['product_id']]=$value;
				}else{
					foreach ($arr_check_mproduct[$value['product_id']] as $key2 => $m_product) {
						if($value['oum_id']==$m_product['oum_id'] && $value['specification']==$m_product['specification'] && $value['sell_price']==$m_product['sell_price']){
							$arr_check_mproduct[$value['product_id']][$key2]['quantity']+= $value['quantity'];
							$arr_check_mproduct[$value['product_id']][$key2]['amount']+= $value['amount'];
						}else{
							$arr_check_mproduct[$value['product_id']][]=$value;
						}
					}
				}
			}
			$arr_cache = array();
			foreach ($arr_check_mproduct as $key => $value) {
				foreach ($value as $key2 => $value2) {
					$arr_cache[] = $value2;
				}
			}
			$index=2;
			foreach ($arr_cache as $key => $value) {
				$sum_amount += $value['sell_price']*$value['quantity']*$value['specification'];
				$arr_cache[$key]['amount'] = '=E'.$index.'*F'.$index."*G".$index;
				$index++;
			}
			$arr_print['arr_data']['toa_moi'] = $sum_amount;
			if($so->status==0){
				$arr_print['arr_data']['no_cu'] = 0;
			}
			$arr_print['arr_data']['tong_cong'] = $arr_print['arr_data']['no_cu'] + $arr_print['arr_data']['toa_moi'];

			$arr_print['arr_data']['no_cu'] = number_format($arr_print['arr_data']['no_cu']);
			$arr_print['arr_data']['toa_moi'] = number_format($arr_print['arr_data']['toa_moi']);
			$arr_print['arr_data']['tong_cong'] = number_format($arr_print['arr_data']['tong_cong']);
			$arr_print['arr_list']['arr_sum'][] = [
				['value'=>'Tổng cộng:','colspan'=>'6'],
				['value'=>$sum_amount]
			];
			$arr_print['arr_list']['arr_body'] = $arr_cache;

			$link = ExportsController::getCreatePrintExcel($arr_print,$id_template,'don_hang_so_'.$so->id);

			Log::create_log(\Auth::user()->id,'App\Saleorder','In đơn hàng số '.session('current_saleorder'));
			return redirect($link);
		}
		die;
	}
	public function getDistributeYear(){
		$min_year = ReceiptMonth::min('year');
		if($min_year==0){
			$min_year = intval(date('Y'));
		}
		$this->layout->content = view('revenue.revenue-distribute-year',[	
							'min_year'		=>	$min_year
						]);
	}
	static public function anyUpdateReceiptMonthCompany(){
		$list_company = Company::get()->toArray();
		foreach ($list_company as $key2 => $value2) {
			$company_id = $value2['id'];
			echo $company_id;
			$list_distribute = ReceiptMonth::where("company_id","=",$company_id)
									  ->where('type_receipt','=','distribute')
									  ->orderBy('year','ASC')
									  ->orderBy('month','ASC')
									  ->get()->toArray();
			if(count($list_distribute)){
				foreach ($list_distribute as $key => $value) {
					if($key==0){
						$current = ReceiptMonth::where("id","=",$value['id'])->first();
						$current->con_lai = $current->sum_amount - $current->paid + $current->no_cu;
						$current->save();
					}else{
						$current = ReceiptMonth::where("id","=",$value['id'])->first();
						$current->no_cu = $list_distribute[$key-1]['con_lai'];
						$current->con_lai = $current->sum_amount - $current->paid + $current->no_cu;
						$current->save();
					}
				}
			}

			$list_customer = ReceiptMonth::where("company_id","=",$company_id)
									  ->where('type_receipt','=','customer')
									  ->orderBy('year','ASC')
									  ->orderBy('month','ASC')
									  ->get()->toArray();
			if(count($list_customer)){
				foreach ($list_customer as $key => $value) {
					if($key==0){
						$current = ReceiptMonth::where("id","=",$value['id'])->first();
						$current->con_lai = $current->sum_amount - $current->paid + $current->no_cu;
						$current->save();
					}else{
						$current = ReceiptMonth::where("id","=",$value['id'])->first();
						$current->no_cu = $list_customer[$key-1]['con_lai'];
						$current->con_lai = $current->sum_amount - $current->paid + $current->no_cu;
						$current->save();
					}
				}
			}
		}
		
		echo "Done!";
		die;
	}