insert into loadings(shipping_item_id, departure_id, colli, weight, processed, processed_date, recaped, notes, created_date, lc, rc, uid)
select spi.id, (
  select id from departures where driver_id is null and location_id = sp.location_id and departure_date::date = mrwo.recap_date::date
	and train_type_id = td.id
	and vehicle_id = (select id from vehicles where (UPPER(name) = UPPER(mrwo.vehicle_number)) 
									or UPPER(plate) = UPPER(mrwo.vehicle_number) LIMIT 1)			
) as departure_id,
mrwo.colli, mrwo.weight, true, mrwo.recap_date::date, true, mrwo.notes, mrwo.recap_date::date, 
case when mrwo.limas_color is null then '' else mrwo.limas_color end as lc, 
case when mrwo.relation_color is null then '' else mrwo.relation_color end as rc, 
mrwo.id::text
from mongo_recaps_wo_driver mrwo
join shipping_items spi on mrwo.item_id::text = spi.uid
join shippings sp on spi.shipping_id = sp.id
join type_definitions td on replace(replace(mrwo.train_type::text, '00000000', ''),'-','') = td.uid
