/** * @role update */ public function save() { $taxes = Tax::getAllTaxes(); $classes = TaxClass::getAllClasses(); if (($zoneID = (int) $this->request->get('id')) <= 0) { $taxRates = TaxRate::getRecordSetByDeliveryZone(null); $deliveryZone = DeliveryZone::getDefaultZoneInstance(); } else { $deliveryZone = DeliveryZone::getInstanceByID($zoneID, true); $taxRates = $deliveryZone->getTaxRates(); } ActiveRecord::beginTransaction(); // delete all rates foreach ($taxRates as $rate) { $rate->delete(); } foreach ($taxes as $tax) { $this->saveRate($deliveryZone, $tax, null); foreach ($classes as $class) { $this->saveRate($deliveryZone, $tax, $class); } } ActiveRecord::commit(); return new JSONResponse(false, 'success', $this->translate('_tax_rates_have_been_successfully_saved')); }
protected function getInstance($record, CsvImportProfile $profile) { $fields = $profile->getSortedFields(); // get delivery zone if (isset($fields['DeliveryZone']['ID'])) { try { $zone = DeliveryZone::getInstanceByID($record[$fields['DeliveryZone']['ID']], true); } catch (ARNotFoundException $e) { $zone = DeliveryZone::getDefaultZoneInstance(); } } else { $zone = DeliveryZone::getDefaultZoneInstance(); } // get shipping service $f = select(new EqualsCond(MultiLingualObject::getLangSearchHandle(new ARFieldHandle('ShippingService', 'name'), $this->application->getDefaultLanguageCode()), $record[$fields['ShippingService']['name']])); if ($zone->isDefault()) { $f->mergeCondition(new IsNullCond(f('ShippingService.deliveryZoneID'))); } else { $f->mergeCondition(eq(f('ShippingService.deliveryZoneID'), $zone->getID())); } $services = ActiveRecordModel::getRecordSet('ShippingService', $f); if ($services->get(0)) { $service = $services->get(0); // temporary $service->deleteRelatedRecordSet('ShippingRate'); } else { $service = ShippingService::getNewInstance($zone, '', 0); $service->rangeType->set(ShippingService::SUBTOTAL_BASED); } $this->importInstance($record, $profile, $service); $this->setLastImportedRecordName($service->getValueByLang('name')); // get rate instance $rate = ShippingRate::getNewInstance($service, 0, 1000000); $rate->subtotalRangeStart->set(0); $rate->subtotalRangeEnd->set(1000000); return $rate; }
private function saveTaxRates(Tax $tax) { $zones = DeliveryZone::getAll(); $zones->add(DeliveryZone::getDefaultZoneInstance()); $classes = TaxClass::getAllClasses(); ActiveRecord::beginTransaction(); foreach ($zones as $zone) { // delete all zone tax rates $taxRates = $zone->getTaxRates(); foreach ($taxRates as $rate) { if ($rate->taxID->get()->getID() == $tax->getID()) { $rate->delete(); } } $this->saveRate($zone, $tax); foreach ($classes as $class) { $this->saveRate($zone, $tax, $class); } } ActiveRecord::commit(); }
public function setConfig() { if (!$this->buildConfigValidator()->isValid()) { return new ActionRedirectResponse('install', 'config'); } Language::deleteCache(); // site name $this->config->setValueByLang('STORE_NAME', $this->request->get('language'), $this->request->get('name')); $this->config->save(); ClassLoader::import('application.model.Currency'); // create currency if (ActiveRecord::objectExists('Currency', $this->request->get('curr'))) { $currency = Currency::getInstanceByID($this->request->get('curr'), Currency::LOAD_DATA); } else { $currency = ActiveRecord::getNewInstance('Currency'); $currency->setID($this->request->get('curr')); $currency->isEnabled->set(true); $currency->isDefault->set(true); $currency->save(ActiveRecord::PERFORM_INSERT); } ClassLoader::import('application.model.system.Language'); // create language if (ActiveRecord::objectExists('Language', $this->request->get('language'))) { $language = Language::getInstanceByID($this->request->get('language'), Language::LOAD_DATA); } else { $language = ActiveRecord::getNewInstance('Language'); $language->setID($this->request->get('language')); $language->save(ActiveRecord::PERFORM_INSERT); $language->isEnabled->set(true); $language->isDefault->set(true); $language->save(); } // set root category name to "LiveCart" ClassLoader::import('application.model.category.Category'); $root = Category::getInstanceById(Category::ROOT_ID, Category::LOAD_DATA); $root->setValueByLang('name', $language->getID(), 'LiveCart'); $root->save(); // create a default shipping service ClassLoader::import('application.model.delivery.DeliveryZone'); ClassLoader::import('application.model.delivery.ShippingService'); ClassLoader::import('application.model.delivery.ShippingRate'); $service = ShippingService::getNewInstance(DeliveryZone::getDefaultZoneInstance(), 'Default Service', ShippingService::SUBTOTAL_BASED); $service->save(); $rate = ShippingRate::getNewInstance($service, 0, 100000); $rate->flatCharge->set(10); $rate->save(); // create a couple of blank static pages ClassLoader::import('application.model.staticpage.StaticPage'); $page = StaticPage::getNewInstance(); $page->setValueByLang('title', $language->getID(), 'Contact Info'); $page->setValueByLang('text', $language->getID(), 'Enter your contact information here'); $page->menu->set(array('INFORMATION' => true)); $page->save(); $page = StaticPage::getNewInstance(); $page->setValueByLang('title', $language->getID(), 'Shipping Policy'); $page->setValueByLang('text', $language->getID(), 'Enter your shipping rate & policy information here'); $page->menu->set(array('INFORMATION' => true)); $page->save(); // create an example site news post ClassLoader::import('application.model.sitenews.NewsPost'); $news = ActiveRecordModel::getNewInstance('NewsPost'); $news->setValueByLang('title', $language->getID(), 'Our store is open'); $news->setValueByLang('text', $language->getID(), 'Powered by LiveCart software, we have gone live! Of course, we will have to go to <a href="../backend">the backend area</a> and add some categories and products first...'); $news->setValueByLang('moreText', $language->getID(), 'Do not forget to delete this post when you actually go live :)'); $news->isEnabled->set(true); $news->save(); return new ActionRedirectResponse('install', 'finish'); }
/** * Load delivery services record by Delivery zone * * @param DeliveryZone $deliveryZone * @param bool $loadReferencedRecords * * @return ARSet */ public static function getByDeliveryZone(DeliveryZone $deliveryZone = null, $loadReferencedRecords = false) { $filter = new ARSelectFilter(); $filter->setOrder(new ARFieldHandle(__CLASS__, "position"), 'ASC'); if (!$deliveryZone) { $deliveryZone = DeliveryZone::getDefaultZoneInstance(); } if ($deliveryZone->isDefault()) { $filter->setCondition(new IsNullCond(new ARFieldHandle(__CLASS__, "deliveryZoneID"))); } else { $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, "deliveryZoneID"), $deliveryZone->getID())); } $services = self::getRecordSet($filter, $loadReferencedRecords); if ($deliveryZone->isDefault()) { foreach ($services as $service) { $service->deliveryZone->set($deliveryZone); } } return $services; }
public function getDeliveryZone($forceReset = false) { ClassLoader::import("application.model.delivery.DeliveryZone"); if (!$this->deliveryZone || $forceReset) { if ($this->isShippingRequired() && $this->shippingAddress->get()) { $this->deliveryZone = DeliveryZone::getZoneByAddress($this->shippingAddress->get(), DeliveryZone::SHIPPING_RATES); } else { $this->deliveryZone = DeliveryZone::getDefaultZoneInstance(); } } return $this->deliveryZone; }
public function testDefaultZoneVATWithAnotherZone() { TaxRate::getNewInstance(DeliveryZone::getDefaultZoneInstance(), $this->tax, 10)->save(); TaxRate::getNewInstance($this->deliveryZone, $this->tax, 10)->save(); DeliveryZoneCountry::getNewInstance($this->deliveryZone, 'US')->save(); $order = CustomerOrder::getNewInstance($this->user); $order->addProduct($this->product, 1, true); $order->currency->set($this->currency); $order->shippingAddress->set($this->address); $order->save(); $this->assertEqual($order->getTotal($this->currency), 100); $order->finalize(); $this->assertDefaultZoneOrder($order, $this->currency); ActiveRecord::clearPool(); $reloaded = CustomerOrder::getInstanceById($order->getID(), true); $this->assertDefaultZoneOrder($reloaded, $this->currency); }
public function getDeliveryZone() { $address = $this->getShippingAddress(); $addressID = $address ? $address->getID() : 0; if (!isset($this->deliveryZones[$addressID])) { if ($address) { $this->deliveryZones[$addressID] = DeliveryZone::getZoneByAddress($address, DeliveryZone::SHIPPING_RATES); } else { $this->deliveryZones[$addressID] = DeliveryZone::getDefaultZoneInstance(); } $this->event('getDeliveryZone'); } return $this->deliveryZones[$addressID]; }
public function reduceBaseTaxes($price, $product = null) { $product = $product ? $product : $this->getProduct(); if (!is_array($product)) { $class = $product->getParent()->taxClass->get(); } else { $product = empty($product['Parent']) ? $product : $product['Parent']; $class = empty($product['taxClassID']) ? null : ActiveRecordModel::getInstanceById('TaxClass', $product['taxClassID']); } foreach (DeliveryZone::getDefaultZoneInstance()->getTaxRates() as $rate) { if ($rate->taxClass->get() === $class) { $price = $price / (1 + $rate->rate->get() / 100); } } return $price; }
public function testShippingClasses() { // set up classes $books = ShippingClass::getNewInstance('Books'); $books->save(); $cds = ShippingClass::getNewInstance('CDs'); $cds->save(); $shoes = ShippingClass::getNewInstance('Shoes'); $shoes->save(); $zone = DeliveryZone::getDefaultZoneInstance(); $service = ShippingService::getNewInstance($zone, 'def', ShippingService::SUBTOTAL_BASED); $service->save(); $shippingRate = ShippingRate::getNewInstance($service, 0, 10000000); // no rate is being set for the Shoes class, so it should use the default one $shippingRate->perItemCharge->set(100); $shippingRate->setClassItemCharge($books, 10); $shippingRate->setClassItemCharge($cds, 5); $shippingRate->save(); $this->products[0]->shippingClass->set($shoes); $this->products[1]->shippingClass->set($books); $this->products[2]->shippingClass->set($cds); for ($k = 0; $k <= 2; $k++) { $this->products[$k]->isSeparateShipment->set(false); $this->order->addProduct($this->products[$k], 1, false); } $this->order->save(); // set shipping rate $shipment = $this->order->getShipments()->get(0); $rates = $this->order->getDeliveryZone()->getShippingRates($shipment); $shipment->setAvailableRates($rates); $shipment->setRateId($rates->get(0)->getServiceID()); $shipment->save(); $this->assertEquals($rates->get(0)->getAmountByCurrency($this->usd), 115); }
public function getHandler($returnUrl = '', $cancelUrl = '') { $application = ActiveRecordModel::getApplication(); $GLOBALS['merchant_id'] = $this->getConfigValue('merchant_id'); if ($this->getConfigValue('sandbox') && !defined('PHPGCHECKOUT_USE_SANDBOX')) { define('PHPGCHECKOUT_USE_SANDBOX', true); } include_once dirname(dirname(__FILE__)) . '/library/google/config.php'; if ($this->order) { $GLOBALS['GCheckout_currency'] = $this->getValidCurrency($this->order->getCurrency()->getID()); } $handler = new gCart($this->getConfigValue('merchant_id'), $this->getConfigValue('merchant_key')); $handler->setMerchantCheckoutFlowSupport($returnUrl, $cancelUrl, $this->application->getConfig()->get('REQUIRE_PHONE')); // add cart items if ($this->order) { $items = array(); foreach ($this->order->getOrderedItems() as $item) { if (!$item->isSavedForLater->get()) { $gItem = new gItem(htmlspecialchars($item->product->get()->getValueByLang('name')), htmlspecialchars($item->product->get()->getValueByLang('shortDescription')), $item->count->get(), $item->price->get()); $gItem->setPrivateItemData('<item-id>' . $item->getID() . '</item-id><order-id>' . $this->order->getID() . '</order-id>'); $items[] = $gItem; } // add discounts if ($discounts = $this->order->getFixedDiscountAmount()) { $items[] = new gItem($application->translate('_discount'), '', 1, $discounts * -1); } $handler->addItems($items); } // get shipping rates for all zones - silly, eh? if ($this->order->isShippingRequired()) { $shipment = $this->order->getShipments()->get(0); $zoneCountries = $zoneStates = $zoneZips = array(); foreach (DeliveryZone::getAll() as $zone) { $countries = $zone->getCountries()->extractField('countryCode'); $states = array(); foreach ($zone->getStates()->extractReferencedItemSet('state') as $state) { if ($state->countryID == 'US') { $states[] = $state->code->get(); } else { $countries[] = $state->countryID->get(); } } $countries = array_intersect(array_unique($countries), $this->getSupportedCountries()); $zipMasks = $zone->getZipMasks()->extractField('mask'); foreach ($zone->getShippingRates($shipment)->toArray() as $rate) { $name = $rate['serviceName'] ? $rate['serviceName'] : $rate['ShippingService']['name_lang']; $gRate = new gShipping($name, round($rate['costAmount'], 2), 'merchant-calculated-shipping'); // @todo: remove this. chokes up on non-US postal codes $zipMasks = array(); $gRate->addAllowedAreas($countries, $states, $zipMasks); $shipping[$name] = $gRate; } $zoneCountries = array_merge($zoneCountries, $countries); $zoneStates = array_merge($zoneStates, $states); $zoneZips = array_merge($zoneZips, $zipMasks); } // default zone $enabledCountries = array_keys($application->getConfig()->get('ENABLED_COUNTRIES')); $defCountries = array_intersect($enabledCountries, $zoneCountries, $this->getSupportedCountries()); foreach (DeliveryZone::getDefaultZoneInstance()->getShippingRates($shipment)->toArray() as $rate) { $gRate = new gShipping($rate['serviceName'] ? $rate['serviceName'] : $rate['ShippingService']['name_lang'], round($rate['costAmount'], 2), 'merchant-calculated-shipping'); $gRate->addAllowedAreas($defCountries, array(), array()); $shipping[] = $gRate; } $handler->_setShipping($shipping); } } // set merchant calculations $router = CustomerOrder::getApplication()->getRouter(); $calcUrl = $router->createFullUrl($router->createUrl(array('controller' => 'googleCheckout', 'action' => 'index')), !$this->getConfigValue('sandbox')); $handler->setMerchantCalculations(new gMerchantCalculations($calcUrl, $this->getConfigValue('coupons'))); $handler->setDefaultTaxTable(new gTaxTable('Tax', array(new gTaxRule(0)))); return $handler; }
/** * Returns the delivery zone, which matches the required address * * @return DeliveryZone * @todo implement */ public static function getZoneByAddress(UserAddress $address, $type = 0) { $zones = self::getAllZonesByAddress($address, $type); return $zones ? array_shift($zones) : DeliveryZone::getDefaultZoneInstance(); }
public function testTaxRounding() { $tax = Tax::getNewInstance('VAT'); $tax->save(); TaxRate::getNewInstance(DeliveryZone::getDefaultZoneInstance(), $tax, 19)->save(); foreach (array(2 => true, 1 => false) as $shipments => $isSeparate) { $this->initOrder(); foreach (array(635.99, 228.69, 61.59) as $key => $price) { $this->products[$key]->setPrice('USD', $price); if (!$isSeparate) { $this->products[$key]->isSeparateShipment->set(false); } $this->order->addProduct($this->products[$key], 1, false); } $this->order->save(); $this->assertEquals(count($this->order->getShipments()), $shipments); $this->assertEquals($this->order->getTotal(), 926.27); } }