php - Symfony combine the calculated price for number of person and duration -
i have these 2 tables trying calculate price of reservation system number of persons , number of days can stay.
table availabilities:
+--------+--------------------------------------+------------+----------+-----------------+-------------------+--------------+--------------+------------+ | id | property_id | date | quantity | arrival_allowed | departure_allowed | minimum_stay | maximum_stay | version | +--------+--------------------------------------+------------+----------+-----------------+-------------------+--------------+--------------+------------+ | 7739 | 71438849 | 2017-05-18 | 1 | 1 | 1 | 1 | 21 | 14 | | 7731 | 71438849 | 2017-05-19 | 1 | 1 | 1 | 1 | 21 | 14 | | 777 | 71438849 | 2017-05-20 | 1 | 1 | 1 | 1 | 21 | 14 | explanation availabilities fields: property_id = uuid of property date = self explaining quantity = number of available units of property arrival_allowed = allowed arrive on date departure_allowed = allowed depart on date minimum_stay = minimum number of nights should stay if date included in stay maximum_stay = maximum number of nights may stay if date included in stay
table prices:
+---------+--------------------------------------+----------+--------+----------+-----------+---------------+--------------+--------------+--------------------+-----------------------------+-------------+-------------+------------+ | id | property_id | duration | amount | currency | persons | weekdays | minimum_stay | maximum_stay | extra_person_price | extra_person_price_currency | period_from | period_till | version | +---------+--------------------------------------+----------+--------+----------+-----------+---------------+--------------+--------------+--------------------+-----------------------------+-------------+-------------+------------+ | 5291223 | 71438849 | 1 | 12300 | eur | 1 | 0|1|2|3|4|5|6 | 1 | 21 | 0 | eur | 2017-05-18 | 2017-05-18 | 14 | | 5291225 | 71438849 | 1 | 12300 | eur | 2|3|4|5|6 | 0|1|2|3|4|5|6 | 1 | 21 | 1500 | eur | 2017-05-18 | 2017-05-18 | 14 | | 5291227 | 71438849 | 1 | 13900 | eur | 1 | 0|1|2|3|4|5|6 | 1 | 21 | 0 | eur | 2017-05-19 | 2017-05-19 | 14 | explanation prices fields: property_id = uuid of property duration = number of nights price valid. e.g. price 1 week have duration of 7. amount = actual rent price, in cents (so 1 eur = 100) currency = currency of price persons = number of persons price valid, separated pipe sign. weekdays = days of week on price valid, separated pipe sign. 0 = sunday, 6 = saturday (e.g. if price valid on friday , saturday, value "5|6" minimum_stay = minimum number of nights should stay if use price (e.g. duration==1, minimum_stay==3, means nightly rate minimum of 3 nights) maximum_stay = maximum number of nights may stay if use price extra_person_price = price additional person extra_person_price_currency = currency person price
availabilitiesrepository:
public function available (){ $sql= ' select distinct a.property_id, a.date, a.minimum_stay,a.maximum_stay,a.quantity, p.duration, p.persons, p.amount, p.extra_person_price, p.minimum_stay price_minimum_stay, p.maximum_stay price_maximum_stay, p.weekdays availabilities join prices p on a.property_id=p.property_id a.minimum_stay >0 , a.maximum_stay < 22 , a.date >= p.period_from , a.date <= p.period_till '; $stm= $this->getentitymanager()->getconnection()->prepare($sql); //$stm->bindparam('ss',$data); $stm->execute(); return $stm->fetchall(pdoconnection::fetch_assoc); }
homecontroller:
class homecontroller extends controller { /** * @route("/", name="homepage") */ public function indexaction(request $request) { $em= $this->getdoctrine()->getentitymanager(); $availables = $em->getrepository('appbundle:availabilities')->available(); $result=''; foreach ($availables $available) { $duration = $available['duration']; $amount = $available['amount']; $extra_person_price = $available['extra_person_price']; $persons = explode('|',$available['persons']); $weekdays= explode('|', $available['weekdays']); $this->calculatepriceperperson($persons, $extra_person_price, $duration, $amount); } return $this->render('home/index.html.twig', array( 'available'=>$available ) ); } private function calculatepriceperperson($persons, $extra_person_price, $duration, $amount){ foreach ($persons $person) { // ($person = 0; $person < sizeof($persons); $person++) { if ($person > 1) { $price_person = (($person * $extra_person_price) + $amount) * $duration; $price_person_euro = number_format($price_person / 100, 2, '.', ','); // $price_days= $price_person * $duration; } else { $price_person_euro = number_format($amount / 100, 2, '.', ','); } echo '<pre>'; print_r($price_person_euro); } } }
index.html.twig:
<table id="example" cellspacing="0" width="100%"> <tbody> {% in available %} <tr> <th>{{ a.property_id }}</th> <th>{{ a.date }}</th> <th>{{ a.person }}</th> <th>{{ a.duration }}</th> </tr> {% endfor %} </tbody>
i have calculated price don't know how show calculated results in table. question how can combine calculated price with date, persons , duration. duration 1 21. preferably example 1. have 1 , 2 number of days , price calculated. if unclear. how can show example 2
example 1:
date |person|1 |2 | ----------------------------------- 2017-01-01 | 1 | 50.00 | 100.00 | 2017-01-01 | 2 | 70.00 | 140.00 | 2017-01-01 | 3 | 90.00 | 180.00 | 2017-01-02 | 4 | 50.00 | 100.00 | 2017-01-02 | 2 | 70.00 | 140.00 |
example 2:
date |person|duration|price | ----------------------------------- 2017-01-01 | 1 | 1 | 50.00 | 2017-01-01 | 2 | 2 | 140.00 | 2017-01-01 | 3 | 3 | 210.00 | 2017-01-02 | 4 | 4 | 280.00 |
Comments
Post a Comment