public function postUpdateMproduct(Request $request){
		$arr_return= array('status'=>'error','invest'=>0);
		$id = $request->has('id')?$request->input('id'):0;
		$log="";
		if($id){
			$mproduct = MProduct::find($id);
			$mproduct_po = Mproduct::find($mproduct->m_product_id);
			if($request->has('oum_id')  && $mproduct->oum_id != $request->input('oum_id')){
				$old = Oum::find($mproduct->oum_id);
				$new = Oum::find($request->input('oum_id'));
				if($old){
					$log .= 'đơn vị từ "'.$old->name.'" thành "'.$new->name.'" ';
				}else{
					$log .= 'đơn vị từ " " thành "'.$new->name.'" ';
				}
			}
			if($request->has('sell_price')  && $mproduct->sell_price != $request->input('sell_price')){
				$log .= 'giá bán từ "'.$mproduct->sell_price.'" thành "'.$request->input('sell_price').'" ';
			}
			if($request->has('specification')  && $mproduct->specification != $request->input('specification')){
				$log .= 'quy cách từ "'.$mproduct->specification.'" thành "'.$request->input('specification').'" ';
			}
			if($request->has('quantity')  && $mproduct->quantity != $request->input('quantity')){
				$log .= 'số lượng từ "'.$mproduct->quantity.'" thành "'.$request->input('quantity').'" ';
			}
			$mproduct->oum_id =  $request->has('oum_id')?$request->input('oum_id'):0;
			$mproduct->origin_price =  $request->has('origin_price')?$request->input('origin_price'):0;
			$mproduct->specification =  $request->has('specification')?$request->input('specification'):0;
			$old_quantity = $mproduct->quantity ;
			$mproduct->quantity =  $request->has('quantity')?$request->input('quantity'):0;
			$product_stock = ProductStock::where('m_product_id','=',$mproduct_po->id)->first();
			$so_luong_con = $product_stock->in_stock;
			$product_stock->in_stock = $product_stock->in_stock - ($mproduct->quantity*$mproduct->specification);
			$mproduct->invest = $mproduct->specification* $mproduct->quantity* $mproduct->origin_price;
			if($product_stock->in_stock >=0){
				if( !$mproduct->status){
					if($mproduct->save()){
						$product = Product::find($mproduct->product_id);
						Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','cập nhật '.$log.' sản phẩm '.$product->sku.' đơn hàng trả nhà cung cấp số '.session('current_returnpurchaseorder'));
						$arr_return['status'] = 'success';
						$arr_return['invest'] = number_format( $mproduct->invest );
					}else{
						$arr_return['message'] = 'Saving fail !';
					}
				}else{
					$arr_return['message'] = 'Đơn hàng đã hoàn thành không thể cập nhật';
				}
			}else{
				$arr_return['message'] = 'Số lượng trả '.$mproduct->name.' lớn hơn số lượng đã nhập<br/> Số lượng đã nhập là '.$so_luong_con.' cái';
			}
		}

		$id = session('current_returnpurchaseorder');
		//Init array
		$list_product = array();

		//Get value
		$returnpurchaseorder = ReturnPurchaseorder::find(session('current_returnpurchaseorder'));
		$list_product = MProduct::select('m_products.*','products.sku','products.name')->where('module_type','=','App\ReturnPurchaseorder')
						->where('module_id','=',$id)
						->where('company_id','=',$returnpurchaseorder['company_id'])
						->leftJoin('products','products.id','=','m_products.product_id')
						->addSelect('oums.name as oum_name')
						->leftJoin('oums','oums.id','=','m_products.oum_id')
						->get()->toArray();
		\Cache::put('list_product_rpo'.\Auth::user()->id, $list_product, 30); 
		$returnpurchaseorder->updated_by = \Auth::user()->id;
		$returnpurchaseorder->save();
		self::getListProduct();
		return $arr_return;
	}
	public function anyDelete(Request $request)
	{
		$arr_return = [
			'status'		=>	'error'
		];
		$id_product = session('current_product') !== null ? session('current_product') : 0;
		if($id_product){

			$product = MProduct::where('product_id','=',$id_product)
						->leftJoin('purchaseorders',function($join){
							$join->on('m_products.module_id','=','purchaseorders.id');
						})
						->where('module_type','=','App\Purchaseorder')
						->first();
			if($product){
				$arr_return['message'] = 'Sản phẩm đã được tạo hóa đơn.<br/> Xin vui lòng xóa hết hóa đơn để có thể xóa sản phẩm.';
			}else{
				$product = Product::find($id_product);
				if($product->delete()){
					Log::create_log(\Auth::user()->id,'App\Product','Xóa sản phẩm số '.$id_product);
					MProduct::where('product_id','=',$id_product)->delete();
					SellPrice::where('product_id','=',$id_product)->delete();
					ProductStock::where('product_id','=',$id_product)->delete();
				}
				Session::forget('current_product');
				$arr_return['status'] = 'success';
			}


		}else{
			$arr_return['message'] = 'Không tìm thấy sản phẩm';
		}
		return $arr_return;

	}
	public function postUpdate(Request $request)
	{
		$arr_return = array(
					'status' => 'error',
					'message'=>''
				);
		$time =date('H:i:s', time());
		$id_return_saleorder = session('current_returnsaleorder') !== null ? session('current_returnsaleorder') : 0;
		if($id_return_saleorder){
			$returnsaleorder = ReturnSaleorder::find($id_return_saleorder);
			session(['current_returnsaleorder' => $returnsaleorder['id']]);
			$time = date('H:i:s', strtotime($returnsaleorder->date));
		}else{

			$returnsaleorder = new ReturnSaleorder;
			$returnsaleorder->date = date("Y-m-d H:i:s");
			$returnsaleorder->created_by = \Auth::user()->id;
			$returnsaleorder->save();
			Log::create_log(\Auth::user()->id,'App\ReturnSaleorder','Tạo mới đơn hàng đại lý trả số '.$returnsaleorder->id);
			session(['current_returnsaleorder' => $returnsaleorder->id]);
		}
		$log = '';
		$old_company_id = $returnsaleorder->company_id;
		if($returnsaleorder->status == 0){
			$address = Address::where('module_id','=',$returnsaleorder->id)
						->where('module_type','=','App\ReturnSaleorder')->first();
			if(!$address){
				$address = new Address;
			}
			if($request->has('company_id')  && $returnsaleorder->company_id != $request->input('company_id')){
				$old = Company::find($returnsaleorder->company_id);
				if(!$old){
					$old = (object) ['name'=>''];
				}
				$new = Company::find($request->input('company_id'));
				$log .= 'công ty từ "'.$old->name.'" thành "'.$new->name.'" ';
			}
			if($returnsaleorder->company_id == $request->input('company_id')){

				if($request->has('user_id')  && $returnsaleorder->user_id != $request->input('user_id')){
					$old = User::find($returnsaleorder->user_id);
					if(!$old){
						$old = (object) ['name'=>''];
					}
					$new = User::find($request->input('user_id'));
					$log .= 'người liên hệ từ "'.$old->name.'" thành "'.$new->name.'" ';
				}

				$old_date=date("Y-m-d",strtotime($returnsaleorder->date));
				$new_date = date("Y-m-d",strtotime($request->input('date')));
				if($request->has('date')  && $old_date != $new_date){
					$log .= 'ngày từ "'.$old_date.'" thành "'.$new_date.'" ';
				}

				if($request->has('company_phone')  && $returnsaleorder->company_phone != $request->input('company_phone')){
					$log .= 'số điện thoại từ "'.$returnsaleorder->company_phone.'" thành "'.$request->input('company_phone').'" ';
				}

				if($request->has('company_email')  && $returnsaleorder->company_email != $request->input('company_email')){
					$log .= 'email từ "'.$returnsaleorder->company_email.'" thành "'.$request->input('company_email').'" ';
				}
				if($request->has('address')  && $address->address != $request->input('address')){
					$log .= 'địa chỉ từ "'.$address->address.'" thành "'.$request->input('address').'" ';
				}
				if($request->has('town_city')  && $address->town_city != $request->input('town_city')){
					$log .= 'quận huyện từ "'.$address->town_city.'" thành "'.$request->input('town_city').'" ';
				}

				if($request->has('province_id')  && $address->province_id != $request->input('province_id')){
					$old = Province::find($address->province_id);
					$new = Province::find($request->input('province_id'));
					if(!$old){
						$old = (object) ['name'=>''];
					}
					$log .= 'tỉnh thành từ "'.$old->name.'" thành "'.$new->name.'" ';
				}
				if($request->has('country_id')  && $address->country_id != $request->input('country_id')){
					$old = Country::find($address->country_id);
					$new = Country::find($request->input('country_id'));
					if(!$old){
						$old = (object) ['name'=>''];
					}
					$log .= 'quốc gia từ "'.$old->name.'" thành "'.$new->name.'" ';
				}
			}
			
			$returnsaleorder->company_id = $request->has('company_id') ? $request->input('company_id') : 0;
			$returnsaleorder->user_id = $request->has('user_id') ? $request->input('user_id') : 0;
			$returnsaleorder->date = $request->has('date') ? date("Y-m-d H:i:s",strtotime($request->input('date').' '.$time)) : date("Y-m-d H:i:s");
			$returnsaleorder->company_phone = $request->has('company_phone') ? $request->input('company_phone') : '';
			$returnsaleorder->company_email = $request->has('company_email') ? $request->input('company_email') : '';
			$address_id = isset($returnsaleorder->address_id) ? $returnsaleorder->address_id : 0;

			

			$address->module_id  = $returnsaleorder->id;
			$address->module_type  = 'App\\ReturnSaleorder';
			$address->address  = $request->has('address') ? $request->input('address') : '';
			$address->town_city  = $request->has('town_city') ? $request->input('town_city') : '';
			$address->zip_postcode  = $request->has('zip_postcode') ? $request->input('zip_postcode') : '';
			$address->country_id  = $request->has('country_id') ? $request->input('country_id') : 0;
			$address->province_id  = $request->has('province_id') ? $request->input('province_id') : 0;
			$address->save();
			$returnsaleorder->address_id = $address->id;
		}else{
			$returnsaleorder->sum_amount = 0;
			$returnsaleorder->sum_invest = 0;
		}
		$old_status = $returnsaleorder->status;
		if($returnsaleorder->status != $request->has('status')){
			$log .= 'trạng thái từ "'.($returnsaleorder->status?'Hoàn thành':'Mới').'" thành "'.($request->has('status')?'Hoàn thành':'Mới').'" ';
		}
		$returnsaleorder->status = $request->has('status')?1:0;
		$check_save_in_stock = true;
		if($returnsaleorder->status){
			$arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name','m_product_id','amount','invest')
							->where('module_id', '=', $returnsaleorder->id)
							->where('module_type', '=', 'App\ReturnSaleorder')
							->leftJoin('products','products.id','=','m_products.product_id')
							->get()->toArray();
			foreach ($arr_mproduct as $key => $mproduct) {
				$returnsaleorder->sum_amount = $returnsaleorder->sum_amount + $mproduct['amount'];
				$returnsaleorder->sum_invest = $returnsaleorder->sum_invest + $mproduct['invest'];
				// $mproduct_po = Mproduct::find();
				$product_stock = ProductStock::where('m_product_id','=',$mproduct['m_product_id'])->first();
				$product_stock->in_stock = $product_stock->in_stock + ($mproduct['quantity']*$mproduct['specification']);
				if($product_stock->in_stock < 0){ 
					$check_save_in_stock = false;
					$arr_return['message'] .= 'Số lượng sản phẩm '.$mproduct['name'].' trả về lớn hơn số lượng đã mua<br/><br/>';
				}
			}
		}else{
			if($old_status != $returnsaleorder->status){
				$arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name','m_product_id')
								->where('module_id', '=', $returnsaleorder->id)
								->where('module_type', '=', 'App\ReturnSaleorder')
								->leftJoin('products','products.id','=','m_products.product_id')
								->get()->toArray();
				foreach ($arr_mproduct as $key => $mproduct) {
					// $mproduct_po = Mproduct::find($mproduct['m_product_id']);
					$product_stock = ProductStock::where('m_product_id','=',$mproduct['m_product_id'])->first();
					$product_stock->in_stock = $product_stock->in_stock - ($mproduct['quantity']*$mproduct['specification']);
					$product_stock->save();
				}
			}
		}

		if($check_save_in_stock){
			$returnsaleorder->updated_by = \Auth::user()->id;
			if($returnsaleorder->save()){
				Log::create_log(\Auth::user()->id,'App\ReturnSaleorder','Cập nhật '.$log.' đơn hàng đại lý trả số '.$returnsaleorder->id);
				if($returnsaleorder->status){
					foreach ($arr_mproduct as $key => $mproduct) {
						//$mproduct_po = Mproduct::find($mproduct['m_product_id']);
						$product_stock = ProductStock::where('m_product_id','=',$mproduct['m_product_id'])->first();
						$product_stock->in_stock = $product_stock->in_stock +  ($mproduct['quantity']*$mproduct['specification']);
						$product_stock->save();
					}
				}
				if($old_company_id != $returnsaleorder->company_id){
					Mproduct::where('module_id', '=', $returnsaleorder->id)
						->where('module_type', '=', 'App\ReturnSaleorder')
						->where('company_id','=',$old_company_id)
						->delete();
				}else{
					Mproduct::where('module_id', '=', $returnsaleorder->id)
						->where('module_type', '=', 'App\ReturnSaleorder')
						->update(['company_id' => $returnsaleorder->company_id ]);
				}
				$arr_return['status']= 'success';
			}else{
				$arr_return['message']= 'Saving fail !';
			}
		}
		return $arr_return;
	}