Esempio n. 1
0
	public function anyImportProduct(){
		
		\Excel::load('kho-2015.xls', function($reader) {
		    $results = $reader->get();
		    $company = Company::getDistributeList()->get()->toArray();
		    $arr_company = array();
		    foreach ($company as $key => $value) {
		    	$arr_company[$value['name']] = $value['id'];
		    }
		    // pr($arr_company);die;
		    $arr_oum = array();
		    $oums = Oum::orderBy('name')->get()->toArray();
		    foreach ($oums as $key => $value) {
		    	$arr_oum[$value['name']] = $value['id'];
		    }
		 	foreach ($results as $row) {

		 	 	$product = new Product;
				$product->name = $row['ten_san_pham'];
				$product->sku = $row['sku'];
				// $name_product = $row['ten_san_pham'];
				// $check1 = is_numeric(mb_strpos($name_product, "Áo"));
				// $check2 = is_numeric(mb_strpos($name_product, 'Váy'));
				// $check3 = is_numeric(mb_strpos($name_product, 'Quần'));
				// $check3 = is_numeric(mb_strpos($name_product, 'Đầm'));
				// $check4 = is_numeric(mb_strpos($name_product, 'Bộ')) && is_numeric(mb_strpos($name_product, 'ton'));
				// $check5 = is_numeric(mb_strpos($name_product, 'Bộ')) && is_numeric(mb_strpos($name_product, 'bo'));
				// $check6 = is_numeric(mb_strpos($name_product, 'Bộ')) && is_numeric(mb_strpos($name_product, 'thun'));
				// $check = $check1 || $check2 || $check3 || $check4 || $check5 || $check6;
				if($row['loai_san_pham'] == "Đồ dùng"){
					$product->product_type = 2;
				}else{
					$product->product_type = 1;
				}
				$product->status=0;
				$product->created_by = \Auth::user()->id;
				if($product->save()){
					$mproduct = new MProduct;
					$mproduct->product_id	=	$product->id;
					$mproduct->module_id	= 	0;
					$mproduct->company_id	= 	$arr_company[$row['nha_cung_cap']];
					$mproduct->module_type	=	'in_stock';
					$mproduct->specification	=	$row['quy_cach'];
					$mproduct->oum_id		=	$arr_oum[$row['don_vi_ban']];
					$mproduct->origin_price	=	intval(str_replace(",","",$row['gia_goc']));
					$mproduct->quantity		=	$row['so_luong'];
					$mproduct->invest		=	intval(str_replace(",","",$row['gia_goc']))*intval($row['so_luong'])*intval($row['quy_cach']);
					if($mproduct->save()){
						Product::where('id','=',$product->id)->update(['check_in_stock'=>1]);
						if(intval($row['gia_npp'])>0){
							$sell_price = new SellPrice;
							$sell_price->name = "Giá NPP";
							$sell_price->price = intval($row['gia_npp']);
							$sell_price->product_id = $product->id;
							$sell_price->m_product_id = $mproduct->id;
							$sell_price->save();
						}
						if(intval($row['dai_ly'])>0){
							$sell_price = new SellPrice;
							$sell_price->name = "Giá đại lý";
							$sell_price->price = intval($row['dai_ly']);
							$sell_price->product_id = $product->id;
							$sell_price->m_product_id = $mproduct->id;
							$sell_price->save();
						}
						if(intval($row['shop'])>0){
							$sell_price = new SellPrice;
							$sell_price->name = "Giá bán shop";
							$sell_price->price = intval($row['shop']);
							$sell_price->product_id = $product->id;
							$sell_price->m_product_id = $mproduct->id;
							$sell_price->save();
						}
						if(intval($row['ban_le'])>0){
							$sell_price = new SellPrice;
							$sell_price->name = "Giá bán lẻ";
							$sell_price->price = intval($row['ban_le']);
							$sell_price->product_id = $product->id;
							$sell_price->m_product_id = $mproduct->id;
							$sell_price->save();
						}
					}
				}
		 	}
		 	echo "Done";
		 	die;
		});
		
	}
	public function postUpdateMproduct(Request $request){
		$arr_return= array('status'=>'error','amount'=>0);
		$id = $request->has('id')?$request->input('id'):0;
		$log="";
		if($id){
			$mproduct = MProduct::find($id);
			$mproduct_po = Mproduct::find($mproduct->m_product_id);
			$mproduct_so = Mproduct::find($mproduct->m_product_id_so);

			$mproduct_rso_before = Mproduct::where('m_product_id_so','=',$mproduct_so->id)
							->where('module_type','=','App\\ReturnSaleorder')
							->where('company_id','=',$mproduct->company_id)
							->get();
			$mproduct_so->con_lai = $mproduct_so->quantity * $mproduct_so->specification;
			
			foreach ($mproduct_rso_before as $key => $value) {
				$mproduct_so->con_lai -=  $value->quantity * $value->specification;
			}
			$so_luong_con = $mproduct_so->con_lai;
			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->sell_price =  $request->has('sell_price')?$request->input('sell_price'):0;
			$mproduct->specification =  $request->has('specification')?$request->input('specification'):0;
			$old_quantity = $mproduct->quantity ;
			$mproduct->quantity =  $request->has('quantity')?$request->input('quantity'):0;
			$check_return = ($mproduct_so->con_lai - $mproduct->quantity*$mproduct->specification) >=0;

			$mproduct->origin_price = $mproduct_so->origin_price;
			$mproduct->amount = $mproduct->specification* $mproduct->quantity* $mproduct->sell_price;
			$mproduct->invest = $mproduct->specification* $mproduct->quantity* $mproduct->origin_price;
			// if($product_stock->in_stock >=0){
				if( $check_return ){
					if($mproduct->save()){
						$product = Product::find($mproduct->product_id);
						Log::create_log(\Auth::user()->id,'App\ReturnSaleorder','cập nhật '.$log.' sản phẩm '.$product->sku.' đơn hàng đại lý trả số '.session('current_returnsaleorder'));
						$arr_return['status'] = 'success';
						$arr_return['amount'] = number_format( $mproduct->amount );
					}else{ 
						$arr_return['message'] = 'Saving fail !';
					}
				}else{
					$arr_return['message'] = 'Số lượng sản phẩm trả về lớn hơn số lượng còn lại trong kho <br/> Số lượng còn lại trong kho là '.$so_luong_con.' cái';
				}
			// }else{
			// 	$arr_return['message'] = 'Số lượng trả hàng lớn hơn số lượng đã bán';
			// }
		}

		//Init array
		$list_product = array();
		//Get value
		$returnsaleorder = ReturnSaleorder::find(session('current_returnsaleorder'));
		$list_product = MProduct::select('m_products.*','products.sku','products.name')->where('module_type','=','App\ReturnSaleorder')
						->where('module_id','=',$id)
						->where('company_id','=',$returnsaleorder['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_rso'.\Auth::user()->id, $list_product, 30);

		
		$returnsaleorder->updated_by = \Auth::user()->id;
		$returnsaleorder->save();
		self::getListProduct();
		return $arr_return;
	}
	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;
	}
Esempio n. 4
0
	 public function getDeleteOum($id)
	{
		$arr_return = array(
			"status"=>"error"
		);
		$oum = Oum::find($id);
		if($oum->delete()){
			$arr_return['status'] = 'success';
		}else{
			$arr_return['message']= "Delete fail";
		}
		return $arr_return;
	}