To calculate the prices, you'd have to gather together all the possible dates, then add them together for each record. Here's a starting point:
trigger calcSum on Lead (before insert, before update) {
Set<Date> dates = new Set<Date>();
Map<Date, Decimal> cost = new Map<Date, Decimal>();
Lead[] validLeads = new Lead[0];
for(Lead record: Trigger.new) {
record.TotalDue__c = 0;
if(record.ArrivalDate__c != null && record.DepartureDate__c != null) {
validLeads.add(record);
Date tempDate = record.ArrivalDate__c;
while(tempDate < record.DepartureDate__c) {
dates.add(tempDate);
tempDate = tempDate.addDays(1);
}
}
}
for(Rate__c record:[SELECT Rate__c,Date__c FROM Rate__c WHERE Date__c IN :dates]) {
cost.put(record.Date__c, record.Rate__c);
}
for(Lead record: validLeads) {
Date tempDate = record.ArrivalDate__c;
while(tempDate < record.DepartureDate__c) {
Decimal dayRate = cost.get(tempDate);
if(dayRate != null) {
record.TotalDue__c += cost.get(tempDate);
}
tempDate = tempDate.addDays(1);
}
}
}
Of course, I presume there might be multiple motels, which will complicate the code (but not considerably so).