	UPDATE vn.componentType 
		SET isBase = 1
		WHERE code = 'MANA';

	CREATE OR REPLACE TEMPORARY TABLE tmp.discountSale
		(PRIMARY KEY (saleFk))
		SELECT bs.saleFk,
				bs.amount, 
				s.discount,
				SUM(IF(ct.isBase, s.quantity * sc.value, 0)) importe,
				SUM(IF(ct.isBase, 0, s.quantity * sc.value)) recargo
			FROM bs.sale bs
				JOIN vn.sale s ON s.id = bs.saleFk
				JOIN vn.saleComponent sc ON sc.saleFk = s.id 
				JOIN vn.component c ON c.id = sc.componentFk 
				JOIN vn.componentType ct ON ct.id = c.typeFk
			WHERE s.discount
			GROUP BY bs.saleFk;
	UPDATE bs.sale bs
		JOIN tmp.discountSale ts ON ts.saleFk = bs.saleFk  
		SET bs.amount = ts.importe
			bs.surcharge = ts.recargo;

	DROP TEMPORARY TABLE tmp.discountSale;
		
	DELETE FROM comparative
			WHERE timePeriod BETWEEN '202101' AND '202409';

	INSERT INTO comparative(
		timePeriod,
		itemFk,
		warehouseFk,
		quantity,
		price,
		countryFk
	)
	SELECT tm.period,
			s.itemFk,
			t.warehouseFk,
			sum(s.quantity),
			sum(v.importe),
			p.countryFk
		FROM bs.ventas v
			JOIN time tm ON tm.dated = v.fecha
			JOIN sale s ON s.id = v.Id_Movimiento
			JOIN itemType tp ON tp.id = v.tipo_id
			JOIN itemCategory ic ON ic.id = tp.categoryFk
			JOIN ticket t ON t.id = s.ticketFk
			JOIN client c ON c.id = t.clientFk
			JOIN warehouse w ON w.id = t.warehouseFk
			JOIN address ad ON ad.id = t.addressFk
			LEFT JOIN province p ON p.id = ad.provinceFk
		WHERE tm.period BETWEEN '202101' AND '202409'
			AND c.typeFk <> 'loses'
			AND NOT w.code = 'inv'
		GROUP BY p.countryFk, s.itemFk, tm.period, t.warehouseFk;
