Gossamer Forum
Home : Products : DBMan : Installation :

adding values from fields/ price calculation

Quote Reply
adding values from fields/ price calculation
I am working on a booking system.
I have the price for every day in a column, and when the customer do a range search on days to see if rooms are available, I would like to show the price for the chosen time.
Can I sum up the prices for each day in that range, i.e. add the values in column "price" from ,say, day 183 to day 212, if that is choosen?

And can I likewise make an evaluation on the field "available" , that can be "yes" or "no" for that time, so that if all "yes" output is "yes" and if "no"s appear output is "no" - we do not want to see all the yes and nos.?

Quote Reply
Re: adding values from fields/ price calculation In reply to
Now you're getting tricky! Smile

I'm not sure exactly how you have things set up. Does the user enter the dates he's interested in? When you say "I have the price for every day in a column," do you mean that there's just one price for each day, or that there are different prices for, say Sunday, Monday or Tuesday?

Regarding your "yes,no" question: Is it that you want it to print out "Not available" if there are any dates when the availability is "No"?

This is intriguing and I'll have to think about it a bit. I'll let my brain work on it for a bit and get back to you.


------------------
JPD

All my advice is offered on the basis of kindness-ware. If I've helped you to solve a problem, go out of your way to be kind to someone today.

Quote Reply
Re: adding values from fields/ price calculation In reply to
Yes, this is tricky. But it seems the impossible gets possible in this group:-)
The customer will look in the database to see if rooms are available at a given time. He will enter date of arrival and number of nights. At the moment a list of each day is printed on the screen, stating whether a room is available or not on that particular day.

The prices for one day varies so that the day-price will be raised every 14-days or so towards the high-season, and then fall again.
It is a little complicated to do the calculation with javascript, because, if the duration spans over several periods of different prices, depending on the week of the year, a lot of coding of conditions must be done. It would be very much simpler , I believe, to return all the prices from a field in the database, in the timespan the customer is asking for. Like 3 weeks at some time. If I can only get the values listed (with a + between them) I can sum (add) them together and get the total price right away using javascript on the page.
It might be from a hidden field.

>Regarding your "yes,no" question: Is it that you want it to print out "Not available" if
> there are any dates when the availability is "No"?

YES, yes that it right.

Wonder if this is possible?



Quote Reply
Re: adding values from fields/ price calculation In reply to
Your search comes up with the information about each of the dates that are requested. (Just getting things straight in my mind. Smile )

Borrowing some code from Jaime Ortega from a while back, you might try this:

Code:
$counter = 0;
$not_available = 0;
$total_price = 0;
AVAIL: for (0 .. $db_total_hits - 1) {
if ( $hits[($#db_cols+1) * $counter+4] eq "No") {
$not_available = 1;
last AVAIL;
}
$total_price += $hits[($#db_cols+1) * $counter+5]
++$counter;
}
if ($not_available) {
print "Not available on those dates";
}
else {
[print whatever you want to say if it is available,
including the variable $total_price]
}

Replace the 4 above with the number of your "Yes|No" availability field. Replace the 5 above with the number of your price field.

This would go in html_view_success.

This is so completely untested that I won't guarantee a thing. Seems like it should work, though. Smile

------------------
JPD

All my advice is offered on the basis of kindness-ware. If I've helped you to solve a problem, go out of your way to be kind to someone today.



[This message has been edited by JPDeni (edited March 02, 1999).]
Quote Reply
Re: adding values from fields/ price calculation In reply to
Absolutely Amazing.

It is working perfectly.
Thank you very much for your help, I am learning so much about programming here.

Now I have some more ideas I would like to put into the programme, firstly:

When we meet a date that says No, "room not available" I would like to have that date listed on the screen as occupied.
Can I do that in this bit of code, or do I have to put it in html_record_form as "if $rec{'available'} eq "no" print qq|Not available $rec{'Date'}" ??

Quote Reply
Re: adding values from fields/ price calculation In reply to
Excellent!! Smile It's great when you can begin to see the patterns and what they can do. We'll make you a programmer yet!!

Code:
AVAIL: for (0 .. $db_total_hits - 1) {
if ( $hits[($#db_cols+1) * $counter+4] eq "No") {
$not_available = 1;
last AVAIL;
}

to

Code:
for (0 .. $db_total_hits - 1) {
if ( $hits[($#db_cols+1) * $counter+4] eq "No") {
$not_available = 1;
push(@n_a_dates,$hits[($#db_cols+1) * $counter+6]
}

Notice I took out the "AVAIL" references. I used that because I figured if one date wasn't available you could just stop looking. However, if you want to list the unavailable dates, you'll need to go through the list.

Be sure to change the 6 above to the number of your date field.

Okay, now we want to print them out.

change

Code:
if ($not_available) {
print "Not available on those dates";
}

to

Code:
if ($not_available) {
print "Not available on the following dates:";
foreach $date (@n_a_dates) {
print "$date<BR>";
}
}

This gives all the non-available dates, one on each line. (You'll probably want to add <$font> tags or something to make it look nicer.)

---------------
JPD


[This message has been edited by JPDeni (edited March 04, 1999).]
Quote Reply
Re: adding values from fields/ price calculation In reply to
OK, this is working very neatly, now. Thanks again.
I had a few errors on the server so I went through the syntax, Very pedagogically, now I learn the syntax.

Do I dare to go on with this, and put yet another question?

I now have the code like this:
--------
if ( $hits[($#db_cols+1) * $counter+7] eq "nej") { $not_available = 1; push(@n_a_dates,$hits[($#db_cols+1) * $counter+1])
}
$total_price += $hits[($#db_cols+1) * $counter+9];
++$counter;
}
$total_price = $total_price + 275; #this is where I will do further math on the price later;
if ($not_available) {
print "Desværre ingen ledig lejlighed: <br>";
foreach $date(@n_a_dates) {
print "$date<BR>"; }
}
else {
-------------
But I have one more type of room with yet another price, in other columns. Can I do a search/calculation for just one set parameters concerning one of the flats "on demand". (Type1 chosen returns only Type1)?

Quote Reply
Re: adding values from fields/ price calculation In reply to
I'm afraid you've lost me. I'd really have to see what your fields are to be able to answer that.

But I'm really pleased that you got it to work so far! Smile


------------------
JPD


Quote Reply
Re: adding values from fields/ price calculation In reply to
I am very happy with it, too.
Ok, the code I use return the price for type46.
I also have a type68 in the same database.
I would like to do the same thing for type68, the only difference is that I will use data field 8 and 10 in place of data field 7 and 9 in my code, i.e. $counter+8, $counter+10, so I can do a specific type46 or a specific type 68 lookup.
I have a test running here:
http://www.familieferiebornholm.dk/bookinf/
(in danish).
Only prices for type46 in 1999 is avilable by now. Only three dates in May is set to "No" (occupied) all other "yes".
Quote Reply
Re: adding values from fields/ price calculation In reply to
Your database looks very nice! I tested it out and (even though I don't read breakfast pastries -- um, Danish Smile -- I could see that it worked very nicely.

You might be able to try:

Code:
if ($in{'type46'}) {
$avail_dates = 7;
$price = 9;
}
elsif ($in{'type68'}) {
$avail_dates = 8;
$price = 10;
}
if ( $hits[($#db_cols+1) * $counter+$aval_dates] eq "nej") { $not_available = 1;
push(@n_a_dates,$hits[($#db_cols+1) * $counter+1])
}
$total_price += $hits[($#db_cols+1) * $counter+$price];
++$counter;
}

If you have more than two types of rooms, you would probably want to create a hash of the different values. But if you have only two (or three) you might as well use the if -- elsif structure.

(Hope I didn't make any typos in there. Smile )



------------------
JPD




[This message has been edited by JPDeni (edited March 05, 1999).]
Quote Reply
Re: adding values from fields/ price calculation In reply to
Like Magic: It works very well, indeed.
I have now set it all up, just have to do some math with totalprice regarding rebates at certain periods.
BUT, one thing more:
I set up an array to extract the first day and the last day of the stay, but no way can I get the last day value printed, as the last entry in the array is blank.

I use:
-----
for (0 .. $db_total_hits - 1){
push(@a_dates,$hits[($#db_cols+1) * $counter1+1]);
++$counter1;
}
------
I have tried: for (0 .. $db_total_hits), but thats not good.
I have the dates in column 1, I search originally on ID-values.
Can I request the date-value from the (last + 1) hit ?
Quote Reply
Re: adding values from fields/ price calculation In reply to
Code:
print "start date = $a_dates[0]<BR>
end date = $a_dates[scalar(@dates)-1]";

Another way would be

Code:
print "start date = $a_dates[0]<BR>
end date = $a_dates[$#a_dates]";

You know, I've learned a lot from this exchange. I would never have investigated how to do this if you hadn't asked the questions. This has really worked out well for both of us. Smile


------------------
JPD


Quote Reply
Re: adding values from fields/ price calculation In reply to
It me and us, the users of this Forum, that must thank you for your exceptional help, you practically built the booking system.

I use the expressions you wrote to print , but the problem is that the end day is one to small : 5 nights starting on the May 1. should write from May 1. (which it does) to May 6. - but writes May 5.

I guess I will just search on one day more than I need and then subtract that day in the price and availability calculation.

Thanks again, I have learned a tremendous amount of programming from this.

Quote Reply
Re: adding values from fields/ price calculation In reply to
Right. Because you are counting the nights that start on one day, but the people will be leaving the next day. (I used to work in a hotel, and I always forgot that! Smile )

You can do some fancy things with &get_date, but I think it would be more work than its worth. Your solution is probably the best.



------------------
JPD


Quote Reply
Re: [poulR] adding values from fields/ price calculation In reply to
I viewed your site and that is exactly what I'd like to do for mine. I see that it will take quite a bit of time. Are you willing to share your finished code for this system? I emailed you but replies sometimes don't some through as I have spam protection set very high. Please let me know if we can work something out.
Quote Reply
Re: [pdottcomm] adding values from fields/ price calculation In reply to
   
hi,
i want to write it booking programme too. But my prices are not changing per day. it changes per mounth.

is there any easy solution to calculating price with given dates. and checking availability.


thanks
Quote Reply
Re: [fengshui] adding values from fields/ price calculation In reply to
I'm sure there would be a way of dividing the days by 30 to give a monthly rate. You would have to setup the database with all the calculations and then play around with the coding to have it produce what you need.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/