/** * returns the price of the product checking if there's an offer or coupon * @param boolean $calculate_VAT * @return float */ public function final_price($calculate_VAT = TRUE) { $final_price = $this->price; // no current valid offer, normal product price // no current valid coupon: check for valid curent offer if ($this->valid_coupon() === FALSE and $this->has_offer() and Date::mysql2unix($this->offer_valid) > time()) { // in case not any coupon returns the offer price if any valid one $final_price = $this->price_offer; } elseif ($this->valid_coupon() === TRUE) { //calculating price by applying either a discount amount or a discount percentage $discounted_price = abs(Model_Coupon::current()->discount_amount); if ($discounted_price > 0) { $discounted_price = round($this->price - $discounted_price, 2); } else { $discounted_price = abs(Model_Coupon::current()->discount_percentage); if ($discounted_price > 0) { $discounted_price = round($this->price - $this->price * $discounted_price / 100.0, 2); } else { // both discount_amount and discount_percentage are 0 $discounted_price = 0; } } //in case calculated price is negative $final_price = max($discounted_price, 0); } //do we need to charge vat? if (($vat = euvat::vat_percentage()) > 0 and $calculate_VAT === TRUE) { $final_price = $final_price + $vat * $final_price / 100; } //return the price return $final_price; }
/** * verifies pricing in an existing order * @return void */ public function check_pricing() { //original coupon so we dont lose it while we do operations $orig_coupon = $this->id_coupon; //remove the coupon forced by get/post if (core::request('coupon_delete') != NULL) { $this->id_coupon = NULL; } elseif ($this->product->valid_coupon() and $this->id_coupon != Model_Coupon::current()->id_coupon) { $this->id_coupon = Model_Coupon::current()->id_coupon; } elseif ($this->coupon->loaded() and (Date::mysql2unix($this->coupon->valid_date) < time() or $this->coupon->status == 0 or $this->coupon->number_coupons == 0)) { Alert::set(Alert::INFO, __('Coupon not valid, expired or already used.')); $this->coupon->clear(); $this->id_coupon = NULL; } $user = $this->user; //recalculate price since it change the coupon or user info if ($orig_coupon != $this->id_coupon or $this->country != $user->country or $this->city != $user->city or $this->VAT_number != $user->VAT_number or $this->postal_code != $user->postal_code or $this->address != $user->address) { //set variables just in case... $this->amount = $this->product->final_price(); $this->VAT = euvat::vat_percentage(); $this->VAT_number = $user->VAT_number; $this->country = $user->country; $this->city = $user->city; $this->postal_code = $user->postal_code; $this->address = $user->address; try { $this->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } }