示例#1
0
 public function rent()
 {
     global $config;
     $maxUmb = $config['max'];
     $FUmbrella = new FUmbrella();
     $USession = new USession();
     $view = new VRent();
     $row = intval($view->getRow());
     $number = intval($view->getNumber());
     $start = $view->getStartDate();
     $end = $view->getEndDate();
     $owner = new DPerson($USession->getValue('name'), $USession->getValue('surname'), $USession->getValue('ssn'), $USession->getValue('phone'));
     $flag = true;
     $FRate = new FRate();
     $FRent = new FRent();
     $today = new DateTime('now');
     $numUmb = $FRent->getNumRent($USession->getValue('ssn'), $today->format('y-m-d'));
     $season = false;
     //$seasonRate = $FRate->getSeasonRates(); // tariffa stagionale
     $startSeason = new DateTime($config['season_start']);
     $endSeason = new DateTime($config['season_end']);
     try {
         if ($start == 'STAGIONE' && $end == 'STAGIONE') {
             $t = clone $startSeason;
             $end = $endSeason;
             $t->add(new DateInterval('P20D'));
             // data massima per stagionale
             if ($today > $t) {
                 $start = $today;
             } else {
                 $start = $startSeason;
                 $season = true;
             }
         } else {
             $start = new DateTime($start);
             $end = new DateTime($end);
             if ($start < new DateTime('yesterday') || $end < new DateTime('yesterday')) {
                 $flag = false;
             }
         }
         $FUmbrella = new FUmbrella();
         $umbrella = $FUmbrella->loadUmbrella($row, $number);
         if (!$umbrella) {
             $flag = false;
         }
         if (!$FUmbrella->isFree($start->format('y-m-d'), $end->format('y-m-d'), $umbrella)) {
             $flag = false;
         }
         if (!($FRate->isInSeason($start->format('y-m-d')) && $FRate->isInSeason($end->format('y-m-d')))) {
             $flag = false;
         }
         if ($end < $start) {
             $flag = false;
         }
         if ($numUmb >= $maxUmb) {
             $flag = false;
         }
     } catch (Exception $e) {
         $flag = false;
     }
     if ($flag) {
         $t = clone $startSeason;
         $t->add(new DateInterval('P20D'));
         // data entro la quale è stagionale
         $t1 = clone $end;
         $t1->add(new DateInterval('P10D'));
         if ($start <= $t && $t1 >= $endSeason) {
             $season = true;
         }
         if (!$season) {
             $rates = $FRate->getRateByLine($start->format('y-m-d'), $end->format('y-m-d'), $umbrella->getRow());
             // array di tariffe da considerare
             $period = date_diff($start, $end);
             $period = $period->format('%a') + 1;
             // periodo in giorni richiesto dal cliente
             $tar = count($rates);
             if ($tar > 0) {
                 // il peirodo scelto appartiene ad una sola tariffa(periodo)
                 $this->getRateArray($rates, $period, $start, $owner, $umbrella);
             } else {
                 $rates = $FRate->getOtherRateByLine($start->format('y-m-d'), $end->format('y-m-d'), $umbrella->getRow());
                 foreach ($rates as $r) {
                     $f = true;
                     if ($r[0]->getStartPeriod() <= $start) {
                         $period = date_diff($start, $r[0]->getEndPeriod());
                         $period = $period->format('%a') + 1;
                         $this->getRateArray($r, $period, $start, $owner, $umbrella);
                         $f = false;
                     }
                     if ($end <= $r[0]->getEndPeriod()) {
                         $period = date_diff($r[0]->getStartPeriod(), $end);
                         $period = $period->format('%a') + 1;
                         $this->getRateArray($r, $period, $r[0]->getStartPeriod(), $owner, $umbrella);
                         $f = false;
                     }
                     if ($f) {
                         $period = date_diff($r[0]->getStartPeriod(), $r[0]->getEndPeriod());
                         $period = $period->format('%a') + 1;
                         $this->getRateArray($r, $period, $r[0]->getStartPeriod(), $owner, $umbrella);
                     }
                 }
                 // fine foreach
             }
             // fine else
             echo '0';
         } else {
             $price = $seasonRate[$umbrella->getRow()]->getPrice();
             $Rent = new DRent($owner, $umbrella, false, $price, new DateTime($config['season_start']), new DateTime($config['season_end']));
             $FRent->saveRent($Rent);
             echo '0';
         }
     } else {
         echo 'error';
     }
     // codice se nel caso i dati arrivati sono sbagliati
 }
示例#2
0
 private function getPrice($umb, $sdate, $edate, $season)
 {
     if (!$season) {
         $FRate = new FRate();
         $sd = $sdate->format('y-m-d');
         $ed = $edate->format('y-m-d');
         $rates = $FRate->getRateByLine($sd, $ed, $umb->getRow());
         // array di tariffe
         $tar = count($rates);
         $period = date_diff($sdate, $edate);
         $period = $period->format('%a') + 1;
         // periodo da considerare
         if ($tar > 0) {
             $price = 0;
             $p1 = $period;
             $x = 0;
             while ($x < $tar) {
                 $tp = $rates[$x]->getPeriod();
                 if ($p1 - $tp >= 0) {
                     $price = $price + $rates[$x]->getPrice();
                     $p1 = $p1 - $tp;
                 } else {
                     $x++;
                 }
             }
         } else {
             $rates = $FRate->getOtherRateByLine($sd, $ed, $umb->getRow());
             $price = 0;
             $currentDate = new DateTime('0000-00-00');
             foreach ($rates as $r) {
                 $ok = false;
                 $line = $umb->getRow();
                 // fila a cui è applicata la tariffa
                 $sDate = $r->getStartPeriod();
                 // data inizio tariffa
                 $eDate = $r->getEndPeriod();
                 // data fine tariffa
                 $p = $r->getPeriod();
                 // periodo della tariffa
                 $tro = false;
                 // indica se la tariffa contiene la data iniziale o finale
                 if ($currentDate != $sDate) {
                     $flag = false;
                     $currentDate = $sDate;
                 }
                 if (!$flag) {
                     if ($sDate <= $sdate) {
                         $period = date_diff($sdate, $eDate);
                         $period = $period->format('%a') + 1;
                         // periodo in giorni
                         $tro = true;
                         $flag = true;
                     }
                     if ($edate <= $eDate) {
                         $period = date_diff($sDate, $edate);
                         $tro = true;
                         $period = $period->format('%a') + 1;
                         // periodo in giorni
                         $flag = true;
                     }
                     if (!$tro) {
                         $period = date_diff($sDate, $eDate);
                         $period = $period->format('%a') + 1;
                         // periodo in giorni
                         $flag = true;
                     }
                     $tro = false;
                 }
                 if ($sDate <= $sdate) {
                     $tro = true;
                     while (!$ok) {
                         if ($period - $p >= 0) {
                             $price = $price + $t->getPrice();
                             $period = $period - $p;
                         } else {
                             $ok = true;
                         }
                     }
                 }
                 // fine if
                 if ($edate <= $eDate) {
                     $tro = true;
                     while (!$ok) {
                         if ($period - $p >= 0) {
                             $price = $price + $t->getPrice();
                             $period = $period - $p;
                         } else {
                             $ok = true;
                         }
                     }
                 }
                 // fine if
                 if (!$tro) {
                     while (!$ok) {
                         if ($period - $p >= 0) {
                             $price = $price + $t->getPrice();
                             $period = $period - $p;
                         } else {
                             $ok = true;
                         }
                     }
                 }
                 // fine if
             }
             // fine foreach interno
         }
     } else {
         $FRate = new FRate();
         $seasonRates = $FRate->getSeasonRates();
         $price = $seasonRates[$umb->getRow()]->getPrice();
     }
     return $price;
 }