Esempio n. 1
0
 public function reservationUpdate($detail)
 {
     $reservationDetailID = $detail->id;
     if ($reservationDetailID == null) {
         return false;
     }
     $reservationDetailsModel = new RM_ReservationDetails();
     $reservationDetail = $reservationDetailsModel->find($reservationDetailID)->current();
     if ($reservationDetail == null) {
         return false;
     }
     $summaryModel = new RM_ReservationSummary();
     $extrasModel = new RM_Extras();
     $unitObject = new RM_Units();
     $taxSystem = RM_Environment::getInstance()->getTaxSystem();
     $taxes = $taxSystem->getAllTaxes($unitObject->find($reservationDetail->unit_id)->current());
     // we need to create a new reservation details object so that the tax can be calculated
     $periodObj = new RM_Reservation_Period(new RM_Date(strtotime($reservationDetail->start_datetime)), new RM_Date(strtotime($reservationDetail->end_datetime)));
     $persons = new RM_Reservation_Persons(array("adults" => $reservationDetail->adults, "children" => $reservationDetail->children, "infants" => $reservationDetail->infants));
     $fullReservationDetails = new RM_Reservation_Details($reservationDetail->findUnit(), $periodObj, $persons, array());
     $extras = $detail->extras;
     foreach ($extras as $extra) {
         $extraRow = $extrasModel->find($extra->id)->current();
         if ($extraRow == null) {
             continue;
         }
         $extraTotal = $extraRow->calculate($reservationDetail);
         $quantity = (int) $extra->value;
         // calculate the taxes
         $taxTotal = 0;
         foreach ($taxes as $tax) {
             $taxTotal = $taxTotal + $tax->calculate($extraTotal, $fullReservationDetails);
         }
         // multiply by the selection
         $extraTotal = $quantity * $extraTotal;
         $summaryRow = $summaryModel->getByDetail($reservationDetail, RM_Module_Extras::SUMMARY_TYPE, $extra->id);
         // tax
         $summaryTaxRow = $summaryModel->getByDetail($reservationDetail, RM_Module_Extras::SUMMARY_TYPE_TAX, $extra->id);
         // remove the row if the quantity is zero
         if ($summaryRow !== null && $quantity === 0) {
             $delResult = $summaryModel->delete("reservation_detail_id='" . $reservationDetail->id . "' AND type='" . RM_Module_Extras::SUMMARY_TYPE . "' AND row_id='" . $extra->id . "'");
             if ($delResult) {
                 // delete the tax row
                 $summaryTaxRow->delete("reservation_detail_id='" . $reservationDetail->id . "' AND type='" . RM_Module_Extras::SUMMARY_TYPE_TAX . "' AND row_id='" . $extra->id . "'");
             }
             continue;
         }
         if ($summaryRow !== null) {
             $summaryRow->value = $quantity;
             $summaryRow->total_amount = $extraTotal;
             $summaryRow->save();
         } else {
             if ($quantity > 0) {
                 $summaryRow = $summaryModel->createRow(array('reservation_id' => null, 'reservation_detail_id' => $reservationDetail->id, 'type' => RM_Module_Extras::SUMMARY_TYPE, 'row_id' => $extra->id, 'value' => $quantity, 'total_amount' => $extraTotal, 'name' => $extraRow->getName(RM_Environment::getInstance()->getLocale())));
                 $summaryRow->save();
             }
         }
         // tax
         if ($summaryTaxRow !== null) {
             $summaryTaxRow->value = $quantity;
             $summaryTaxRow->total_amount = $taxTotal * $quantity;
             $summaryTaxRow->save();
         } else {
             if ($quantity > 0) {
                 $summaryTaxRow = $summaryModel->createRow(array('reservation_id' => null, 'reservation_detail_id' => $reservationDetail->id, 'type' => RM_Module_Extras::SUMMARY_TYPE_TAX, 'row_id' => $extra->id, 'value' => $quantity, 'total_amount' => $taxTotal * $quantity, 'name' => "Extras Tax"));
                 $summaryTaxRow->save();
             }
         }
     }
     return true;
 }