manager.on('sentOfferChanged', function(offer, oldState) {
	console.log("Offer #" + offer.id);
	if(offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
		connection.query('SELECT `id`,`steamid` FROM `offers` WHERE `offerid` = \''+offer.id+'\' and status = 2').then(function(w) {			 	
			console.log("#2");
			offer.getReceivedItems(function(err, items) {
				if(err) {
					console.log("Couldn't get received items: " + err);
				} else {
					console.log("#3");
					var l = 0;
					var suma = 0;
					
			    	items.some(function(item) {
			    		console.log(item);
			    		tab_prices.some(function(ob) {
			    			
			    			if(item.market_hash_name == ob.name)
			    			{
			    				//console.log("#5");
			    				if(ob.price >= 0.3)
			    				{	
			    					//console.log("#6");
			    					var a = { name: item.market_hash_name, img: item.icon_url, asset_id: item.id, price:ob.price, offerid: offer.id};
			                        connection.query('INSERT INTO items SET ?', a, function(err,res){
				                          if(err) throw err;
				                        suma = suma + parseFloat(ob.price);
				                      
				                        if(items.length == l)
				                        {
					                        connection.query('UPDATE `offers` SET `status`= 3 WHERE `offerid`=\''+offer.id+'\' and status = 2').then(function(o){
												 if(o.changedRows == 1){
												 	var tax = suma*0.1;
												 	var pkt = suma - tax;
												 	var employee = { earned: tax, typ_wpln: 1, offers_id: offer.id};
												 	
												 	connection.query('INSERT INTO money SET ?', employee, function(err,res){
												 		console.log(err);
														connection.query('UPDATE `users` SET `points` = `points`+ \''+pkt+'\' WHERE `steamid`=\''+w[0].steamid+'\'').then(function({
															if(b.changedRows > 0)
															console.log('#OfferID:' + offer.id + ' #Pkt: ' + pkt + ' #SteamID: ' + w[0].steamid + ' Tax: ' + tax);	
														});
													});
												 }
											});	
				                        }
				                    
			                    	});	
			    				} else {
									connection.query('UPDATE `offers` SET `status`= \''+offer.state+'\' WHERE `offerid`=\''+offer.id+'\' and status = 2').then(function(o){
										console.log('#OfferID: ' + offer.id + ' #Status '+ offer.state);
									});
								}
			    			 l++;	
			    			}
			    		});	
			    	});
				}
			});
		});
	} else {
		connection.query('UPDATE `offers` SET `status`= \''+offer.state+'\' WHERE `offerid`=\''+offer.id+'\' and status = 2').then(function(o){
			console.log('#OfferID: ' + offer.id + ' #Status '+ offer.state);
		});
	}
});
setInterval(function(){
		manager.loadInventory(730, 2, true, function(err, inventory, currencies) {
		    if (err)
		        console.log(err);
		    else {
		        connection.query('SELECT `items`.`asset_id`,`items`.`id` as `itemid`,`items`.`steamid`,`users`.`tradelink` as `tradelnk` FROM `items` JOIN `users` ON `users`.`id` = `items`.`buyer_id` WHERE `items`.`status`= 1').then(function(row) {
		            console.log(row);
		            row.forEach(function(item) {
		            	console.log(item.tradelnk);	
						var offer = manager.createOffer(item.tradelnk); 
						inventory.forEach(function(gun) {
							console.log(gun.id, item.asset_id);
							if(gun.assetid == item.asset_id)
							{
								offer.addMyItem(gun);
								var uniqcode = Math.random().toString(36);
								offer.send("Uniqcode:" + uniqcode, item.tradeaccess, function(err, csgo) {
			                    if (err) {
			                    	connection.query('UPDATE `items` SET `status`= 12 WHERE `id`=\''+item.itemid+'\'').then(function(o){
			                      	 
			                      	 var d = new Date();		
			                      	 console.log("[Data]: "+ d +" [OfferID]: " + offer.id + " [UniqCode]: " + uniqcode + " [ItemID]: " + item.itemid + " [Error]: " + err);
			                  		});
			                       
			                    } else
			                    {
			                    	connection.query('UPDATE `items` SET `link`= \''+offer.id+'\', `status`= 2,`uniqcode`=\''+uniqcode+'\' WHERE `id`=\''+item.itemid+'\'').then(function(o){			                      	
			                      		var d = new Date();		
			                      	 	console.log("[Data]: "+ d +" [OfferID]: " + offer.id + " [UniqCode]: " + uniqcode + " [ItemID]: " + item.itemid );	                     	 
			                  		});
			                    			
			                    }                                      
			             		});
							}
						});
					});
		        }); 
		    }
		});}, 30000);