Ejemplo n.º 1
0
 public function setUp()
 {
     parent::setUp();
     $this->deliveryZone = DeliveryZone::getNewInstance();
     $this->deliveryZone->name->set('test zone');
     $this->deliveryZone->save();
 }
Ejemplo n.º 2
0
 public function setUp()
 {
     parent::setUp();
     $this->deliveryZone = DeliveryZone::getNewInstance();
     $this->deliveryZone->setValueByLang('name', 'en', 'test zone');
     $this->deliveryZone->isEnabled->set(true);
     $this->deliveryZone->save();
 }
Ejemplo n.º 3
0
 public function setUp()
 {
     parent::setUp();
     $this->deliveryZone = DeliveryZone::getNewInstance();
     $this->deliveryZone->name->set('test zone');
     $this->deliveryZone->save();
     $this->shippingService = ShippingService::getNewInstance($this->deliveryZone, 'test category', ShippingService::SUBTOTAL_BASED);
     $this->shippingService->save();
 }
Ejemplo n.º 4
0
 public function setUp()
 {
     parent::setUp();
     $this->zone = DeliveryZone::getNewInstance();
     $this->zone->name->set(':TEST_ZONE');
     $this->zone->isEnabled->set(1);
     $this->zone->isFreeShipping->set(1);
     $this->zone->save();
 }
Ejemplo n.º 5
0
 /**
  * Load rates from known delivery zone
  *
  * @param DeliveryZone $deliveryZone
  * @param bool $loadReferencedRecords
  *
  * @return ARSet
  */
 public static function getRecordSetByDeliveryZone(DeliveryZone $deliveryZone = null, $loadReferencedRecords = array('Tax'))
 {
     $filter = new ARSelectFilter();
     if (!$deliveryZone || $deliveryZone->isDefault()) {
         $filter->setCondition(new IsNullCond(new ARFieldHandle(__CLASS__, "deliveryZoneID")));
     } else {
         $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, "deliveryZoneID"), $deliveryZone->getID()));
     }
     return self::getRecordSet($filter, $loadReferencedRecords);
 }
Ejemplo n.º 6
0
 /**
  * @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'));
 }
Ejemplo n.º 7
0
 /**
  * @role update
  */
 public function create()
 {
     if (($deliveryZoneId = (int) $this->request->get('deliveryZoneID')) > 0) {
         $deliveryZone = DeliveryZone::getInstanceByID($deliveryZoneId, true);
     } else {
         $deliveryZone = null;
     }
     $shippingService = ShippingService::getNewInstance($deliveryZone, $this->request->get('name'), $this->request->get('rangeType'));
     return $this->save($shippingService);
 }
Ejemplo n.º 8
0
 public function testTaxClasses()
 {
     // default tax level
     TaxRate::getNewInstance($this->deliveryZone, $this->tax, 20)->save();
     // diferent tax rate for books
     $books = TaxClass::getNewInstance('Books');
     $books->save();
     $booksRate = TaxRate::getNewInstance($this->deliveryZone, $this->tax, 10);
     $booksRate->taxClass->set($books);
     $booksRate->save();
     // price = 100
     $cd = $this->product;
     $book = Product::getNewInstance(Category::getRootNode());
     $book->setPrice('USD', 50);
     $book->isEnabled->set(true);
     $book->taxClass->set($books);
     $book->save();
     // shipping tax class
     $shpClass = TaxClass::getNewInstance('Shipping');
     $shpClass->save();
     $shippingTaxRate = TaxRate::getNewInstance($this->deliveryZone, $this->tax, 17);
     $shippingTaxRate->taxClass->set($shpClass);
     $shippingTaxRate->save();
     $this->getApplication()->getConfig()->setRuntime('DELIVERY_TAX_CLASS', $shpClass->getID());
     $order = CustomerOrder::getNewInstance($this->user);
     $order->addProduct($cd, 1, true);
     $order->addProduct($book, 1, true);
     $order->currency->set($this->currency);
     $order->shippingAddress->set($this->address);
     $order->save();
     $this->assertEqual($order->getDeliveryZone()->getID(), $this->deliveryZone->getID());
     //$order->finalize();
     $this->assertEqual($order->getTotal(true), 120 + 55);
     $this->assertEqual($order->getTaxAmount(), 25);
     $service = ShippingService::getNewInstance($this->deliveryZone, 'def', ShippingService::SUBTOTAL_BASED);
     $service->save();
     $shippingRate = ShippingRate::getNewInstance($service, 0, 10000000);
     $shippingRate->flatCharge->set(100);
     $shippingRate->save();
     $shipment = $order->getShipments()->get(0);
     $rates = $order->getDeliveryZone()->getShippingRates($shipment);
     $shipment->setAvailableRates($rates);
     $shipment->setRateId($rates->get(0)->getServiceID());
     $shipment->save();
     $this->assertEqual($order->getTaxAmount(), 42);
     $this->assertEqual($order->getTotal(true), 120 + 55 + 117);
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
0
 private function getFieldName(DeliveryZone $zone = null, TaxClass $class = null)
 {
     $classID = $class ? $class->getID() : '-1';
     $zoneID = $zone ? $zone->getID() : '-1';
     if ($classID == 0) {
         $classID = -1;
     }
     if ($zoneID == 0) {
         $zoneID = -1;
     }
     return 'taxRate_' . $zoneID . '_' . $classID;
 }
Ejemplo n.º 11
0
 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');
 }
Ejemplo n.º 12
0
 /**
  * @param DeliveryZone $zone
  * 
  * @return ARSet
  */
 public static function getRecordSetByZone(DeliveryZone $zone, $loadReferencedRecords = false)
 {
     $filter = new ARSelectFilter();
     $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, 'deliveryZoneID'), $zone->getID()));
     return self::getRecordSet(__CLASS__, $filter, $loadReferencedRecords);
 }
Ejemplo n.º 13
0
 /**
  * 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;
 }
Ejemplo n.º 14
0
 public static function removeByZone(DeliveryZone $zone)
 {
     $filter = new ARDeleteFilter();
     $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, 'deliveryZoneID'), $zone->getID()));
     return ActiveRecord::deleteRecordSet(__CLASS__, $filter);
 }
Ejemplo n.º 15
0
 public function testFindZoneWithMasks()
 {
     $zone1 = DeliveryZone::getNewInstance();
     $zone1->name->set('With ZIP');
     $zone1->isEnabled->set(true);
     $zone1->save();
     DeliveryZoneZipMask::getNewInstance($zone1, 'asd')->save();
     DeliveryZoneCountry::getNewInstance($zone1, 'LT')->save();
     $zone2 = DeliveryZone::getNewInstance();
     $zone2->name->set('Without ZIP');
     $zone2->isEnabled->set(true);
     $zone2->save();
     DeliveryZoneCountry::getNewInstance($zone2, 'LT')->save();
     $address = UserAddress::getNewInstance();
     $address->countryID->set('LT');
     $this->assertSame(DeliveryZone::getZoneByAddress($address), $zone2);
     $address->postalCode->set('asd');
     $this->assertSame(DeliveryZone::getZoneByAddress($address), $zone1);
 }
Ejemplo n.º 16
0
 public function testAddress()
 {
     $address = UserAddress::getNewInstance();
     $address->loadRequestData($this->request);
     $zone = DeliveryZone::getZoneByAddress($address, $this->request->get('type'));
     return new JSONResponse($zone->toArray());
 }
Ejemplo n.º 17
0
 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;
 }
Ejemplo n.º 18
0
 public function getShippingTaxZone()
 {
     $shippingTaxZoneId = self::getApplication()->getConfig()->get('DELIVERY_TAX');
     return !is_numeric($shippingTaxZoneId) ? $this->getTaxZone() : DeliveryZone::getInstanceById($shippingTaxZoneId, DeliveryZone::LOAD_DATA);
 }
Ejemplo n.º 19
0
 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;
 }
Ejemplo n.º 20
0
 private function createOrderWithZone(DeliveryZone $zone = null)
 {
     if (is_null($zone)) {
         $zone = DeliveryZone::getNewInstance();
     }
     $zone->name->set('Latvia');
     $zone->isEnabled->set(true);
     $zone->save();
     $this->newZone = $zone;
     $country = DeliveryZoneCountry::getNewInstance($zone, 'LV');
     $country->save();
     $tax = Tax::getNewInstance('VAT');
     $tax->save();
     $taxRate = TaxRate::getNewInstance($zone, $tax, 20);
     $taxRate->save();
     $service = ShippingService::getNewInstance($zone, 'def', ShippingService::SUBTOTAL_BASED);
     $service->save();
     $this->newService = $service;
     $shippingRate = ShippingRate::getNewInstance($service, 0, 10000000);
     $shippingRate->flatCharge->set(100);
     $shippingRate->save();
     $this->newRate = $shippingRate;
     // user address
     $address = UserAddress::getNewInstance();
     $address->countryID->set('LV');
     $billingAddress = BillingAddress::getNewInstance($this->user, $address);
     $billingAddress->save();
     // set up order
     $this->order->user->set($this->user);
     $this->order->billingAddress->set($address);
     $this->order->shippingAddress->set($address);
     $this->order->save();
 }
Ejemplo n.º 21
0
 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);
     }
 }
Ejemplo n.º 22
0
 /**
  * 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();
 }
Ejemplo n.º 23
0
 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;
 }