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(); }
public function testGetServiceRates() { $service = ShippingService::getNewInstance($this->deliveryZone, 'Test service 1', ShippingService::SUBTOTAL_BASED); $service->save(); $rate1 = ShippingRate::getNewInstance($service, 1.1, 1.2); $rate1->save(); $rate2 = ShippingRate::getNewInstance($service, 1.3, 1.4); $rate2->save(); $rates = $service->getRates(); $this->assertTrue($rate1 === $rates->get(0)); $this->assertTrue($rate2 === $rates->get(1)); }
private function save(Shipment $shipment) { $validator = $this->createShipmentFormValidator(); if ($validator->isValid()) { if ($shippingServiceID = $this->request->get('shippingServiceID')) { $shippingService = ShippingService::getInstanceByID($shippingServiceID); $shipment->shippingService->set($shippingService); $shipment->setAvailableRates($shipment->getDeliveryZone()->getShippingRates($shipment)); $shipment->setRateId($shippingService->getID()); } if ($this->request->get('noStatus')) { $shipment->status->set($shipment->order->get()->status->get()); } else { if ($this->request->get('shippingServiceID') || (int) $this->request->get('status') < 3) { $shipment->status->set((int) $this->request->get('status')); } } $shipment->save(); return new JSONResponse(array('shipment' => array('ID' => $shipment->getID(), 'amount' => $shipment->amount->get(), 'shippingAmount' => $shipment->shippingAmount->get(), 'ShippingService' => array('ID' => $shipment->shippingService->get() ? $shipment->shippingService->get()->getID() : 0), 'taxAmount' => $shipment->taxAmount->get(), 'total' => $shipment->shippingAmount->get() + $shipment->amount->get() + (double) $shipment->taxAmount->get(), 'prefix' => $shipment->getCurrency()->pricePrefix->get(), 'status' => $shipment->status->get(), 'suffix' => $shipment->getCurrency()->priceSuffix->get())), 'success', $this->request->get('noStatus') ? false : $this->translate('_new_shipment_has_been_successfully_created')); } else { return new JSONResponse(array('errors' => $validator->getErrorList()), 'failure', $this->translate('_error_creating_new_shipment')); } }
private function save(ShippingService $shippingService) { $ratesData = $this->getRatesFromRequest(); $rates = array(); if (!($errors = $this->isNotValid($this->request->get('name'), $ratesData))) { $shippingService->loadRequestData($this->request); $shippingService->setValueArrayByLang(array('name'), $this->application->getDefaultLanguageCode(), $this->application->getLanguageArray(true, false), $this->request); $shippingService->isFinal->set($this->request->get('isFinal')); $shippingService->setValueArrayByLang(array('description'), $this->application->getDefaultLanguageCode(), $this->application->getLanguageArray(true, false), $this->request); $shippingService->deliveryTimeMinDays->set($this->request->get('deliveryTimeMinDays')); $shippingService->deliveryTimeMaxDays->set($this->request->get('deliveryTimeMaxDays')); $shippingService->save(); $shippingService->deleteShippingRates(); $shippingServiceArray = $shippingService->toArray(); $shippingServiceArray['newRates'] = array(); foreach ($ratesData as $id => $data) { if (!$id) { continue; } if ($shippingService->rangeType->get() == ShippingService::WEIGHT_BASED) { $rangeStart = $data['weightRangeStart']; $rangeEnd = $data['weightRangeEnd']; } else { if ($shippingService->rangeType->get() == ShippingService::SUBTOTAL_BASED) { $rangeStart = $data['subtotalRangeStart']; $rangeEnd = $data['subtotalRangeEnd']; } } $rate = ShippingRate::getNewInstance($shippingService, $rangeStart, $rangeEnd); foreach ($data as $var => $value) { $rate->{$var}->set($value); } $rate->save(); $shippingServiceArray['newRates'][$id] = $rate->getID(); } return new JSONResponse(array('service' => $shippingServiceArray), 'success'); } else { return new JSONResponse(array('errors' => $errors), 'failure', $this->translate('_could_note_save_shipping_service')); } }
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 service rates from known service * * @param ShippingService $service * @param bool $loadReferencedRecords * * @return ARSet */ public static function getRecordSetByService(ShippingService $service, $loadReferencedRecords = false) { $filter = new ARSelectFilter(); $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, "shippingServiceID"), $service->getID())); return self::getRecordSet($filter, $loadReferencedRecords); }
public function toArray($amount = null) { $array = parent::toArray(); if (!is_null($amount)) { $array['costAmount'] = $amount; } if (!$this->application) { $this->application = ActiveRecordModel::getApplication(); } $amountCurrency = Currency::getInstanceById($array['costCurrency']); $currencies = $this->application->getCurrencySet(); // get and format prices $prices = $formattedPrices = $taxPrices = $unformattedTaxPrices = array(); foreach ($currencies as $id => $currency) { $prices[$id] = $currency->convertAmount($amountCurrency, $array['costAmount']); $formattedPrices[$id] = $currency->getFormattedPrice($prices[$id]); $unformattedTaxPrices[$id] = $currency->convertAmount($amountCurrency, $this->amountWithTax); $taxPrices[$id] = $currency->getFormattedPrice($unformattedTaxPrices[$id]); $withoutTaxPrices[$id] = $currency->convertAmount($amountCurrency, $this->amountWithoutTax); $formattedWithoutTaxPrices[$id] = $currency->getFormattedPrice($withoutTaxPrices[$id]); } $array['price'] = $prices; $array['priceWithTax'] = $unformattedTaxPrices; $array['formattedPrice'] = $formattedPrices; $array['taxPrice'] = $taxPrices; $array['priceWithoutTax'] = $withoutTaxPrices; $array['formattedPriceWithoutTax'] = $formattedWithoutTaxPrices; // shipping service name $id = $this->getServiceID(); if (is_numeric($id)) { try { $service = ShippingService::getInstanceById($id, ShippingService::LOAD_DATA); $array['ShippingService'] = $service->toArray(); } catch (ARNotFoundException $e) { return array(); } } else { $array['ShippingService'] = array('name_lang' => $this->getServiceName(), 'provider' => $this->getProviderName()); } return $array; }
public function testTaxClassesWithDefaultZoneAndMultipleTaxes() { $order = CustomerOrder::getNewInstance($this->user); $zone = $order->getDeliveryZone(); $this->assertTrue($zone->isDefault()); // default tax level TaxRate::getNewInstance($zone, $this->tax, 10)->save(); $newTax = Tax::getNewInstance('test'); $newTax->save(); // diferent tax rate for books $books = TaxClass::getNewInstance('Books'); $books->save(); $booksRate = TaxRate::getNewInstance($zone, $this->tax, 5); $booksRate->taxClass->set($books); $booksRate->save(); $booksRate = TaxRate::getNewInstance($zone, $newTax, 20); $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(); $order->addProduct($cd, 1, true); $order->addProduct($book, 1, true); $order->currency->set($this->currency); $order->save(); $this->assertEqual($order->getTaxAmount(), 19.41); $this->assertEqual($order->getTotal(true), 150); $service = ShippingService::getNewInstance($order->getDeliveryZone(), '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->getTotal(true), 250); $this->assertEqual((string) $order->getTaxAmount(), (string) 28.5); }
public function getShippingService() { if ($this->shippingService->get()) { return $this->shippingService->get(); } else { if ($this->shippingServiceData->get()) { $rate = unserialize($this->shippingServiceData->get()); return ShippingService::getInstanceByID($rate->getServiceID()); } else { return null; } } }
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(); }
public function testGetZoneServices() { $zone = DeliveryZone::getNewInstance(); $zone->name->set(':TEST_ZONE'); $zone->save(); $service1 = ShippingService::getNewInstance($zone, 'Test service 1', ShippingService::SUBTOTAL_BASED); $service1->save(); $service2 = ShippingService::getNewInstance($zone, 'Test service 2', ShippingService::SUBTOTAL_BASED); $service2->save(); $services = $zone->getShippingServices(); $this->assertTrue($service1 === $services->get(0)); $this->assertTrue($service2 === $services->get(1)); }
/** * Get set of shipping sevices available in current zone * * @param boolean $loadReferencedRecords * @return ARSet */ public function getShippingServices($loadReferencedRecords = false) { return ShippingService::getByDeliveryZone($this, $loadReferencedRecords); }
public function testNetherlandsTax() { $tax = Tax::getNewInstance('VAT'); $tax->save(); // shipment delivery zone $zone = DeliveryZone::getDefaultZoneInstance(); $taxRate = TaxRate::getNewInstance($zone, $tax, 21); $taxRate->save(); $service = ShippingService::getNewInstance($zone, 'def', ShippingService::SUBTOTAL_BASED); $service->save(); $shippingRate = ShippingRate::getNewInstance($service, 0, 10000000); $shippingRate->flatCharge->set(6); $shippingRate->save(); $product = $this->products[0]; $product->setPrice('USD', 24.7); $product->save(); $this->order->addProduct($product, 4, 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->order->finalize(); $this->assertEquals($this->order->getTotal(), 104.8); }
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; }