Ejemplo n.º 1
0
	public function getDeleteFromList(Request $request,$id=0)
	{
		$id_product = $id;
		if($id_product){
			$product = Product::find($id_product);
			$product->delete();
			SellPrice::where('product_id','=',$id_product)->delete();
			Session::forget('current_product');
		}
		return redirect('products/list');
	}
	public function anyAddProduct(Request $request){
		$arr_return = array(
			"status"=>'success'
		);
		$id = $request->has('id')?$request->input('id'):0;
		$company_id = $request->has('company_id')?$request->input('company_id'):0;
		$module_id = session('current_purchaseorder');
		$module_type = 'App\Purchaseorder';
		$arr_product = session('product_of_po'.session('current_purchaseorder'));
		$arr_product_of_po = Mproduct::where('module_id','=',$module_id)
						->where('module_type','=',$module_type)
						->lists('product_id');
		$log = "";
		foreach ($arr_product as $key => $product_id) {
			if(!in_array($product_id, $arr_product_of_po)){
				$product = Product::find($product_id);
				$last_mproduct = Mproduct::where('product_id','=',$product_id)->get()->last();
				$mproduct = new MProduct;
				$mproduct->company_id	=	$company_id;
				$mproduct->product_id	=	$product_id;
				$mproduct->module_id	= 	$module_id;
				$mproduct->id	= 	$id;
				$mproduct->module_type	=	$module_type;
				if($last_mproduct){
					$mproduct->specification	=	$last_mproduct->specification;
					$mproduct->oum_id		=	$last_mproduct->oum_id;
					$mproduct->origin_price	=	$last_mproduct->origin_price;
				}else{
					$mproduct->specification	=	0;
					$mproduct->oum_id			=	0;
					$mproduct->origin_price		=	0;
				}

				$mproduct->save();
				$last_sellprice = SellPrice::where('product_id','=',$product_id)->orderBy('m_product_id','desc')->first();
				if($last_sellprice){
					$arr_sellprice = SellPrice::where('m_product_id','=',$last_sellprice->m_product_id)->get()->toArray();
					foreach ($arr_sellprice as $key => $value) {
						$sellprice = new SellPrice;
						$sellprice->name = $value['name'];
						$sellprice->price = $value['price'];
						$sellprice->m_product_id = $mproduct->id;
						$sellprice->product_id = $value['product_id'];
						$sellprice->save();
					}
				}

				if(!$product->check_in_stock){
					$product->check_in_stock = 1;
					$product->save();
				}
				if($log==""){
					$log .= "Thêm sản phẩm ".$product->sku;
				}else{
					$log .= ", ".$product->sku;
				}
			}
		}


		$log_delete = "";
		foreach ($arr_product_of_po as $key => $product_id) {
			if(!in_array($product_id, $arr_product)){
				$product = Product::find($product_id);
				$mproduct = MProduct::where('module_id','=',$module_id)
						->where('module_type','=',$module_type)
						->where('product_id','=',$product_id)->first()->toArray();

				$id_product = $mproduct['product_id'];
				$quantity = $mproduct['quantity'];
				$check = MProduct::where('module_id','=',$module_id)
						->where('module_type','=',$module_type)
						->where('product_id','=',$product_id)->delete();
				if($check){
					$product_stock = ProductStock::where('m_product_id','=',$mproduct['id'])->delete();
				}
				if($log==""){
					$log .= "xóa sản phẩm ".$product->sku;
				}else{
					$log .= ", ".$product->sku;
				}
			}
		}
		if($log_delete !="")
			Log::create_log(\Auth::user()->id,'App\Purchaseorder',$log.' và .'.$log_delete.' đơn hàng đại lý trả số '.session('current_purchaseorder'));
		else
			Log::create_log(\Auth::user()->id,'App\Purchaseorder',$log.'vào đơn hàng mua số '.session('current_purchaseorder'));
		$purchaseorder = Purchaseorder::find(session('current_purchaseorder'));
		$purchaseorder->updated_by = \Auth::user()->id;
		$purchaseorder->save();
		self::getListProduct();
		return $arr_return;
	}