public function testCreateAndRetrieve() { // set up currency if (ActiveRecord::objectExists('Currency', 'USD')) { $this->usd = Currency::getInstanceByID('USD', Currency::LOAD_DATA); } else { $this->usd = Currency::getNewInstance('USD'); $this->usd->setAsDefault(); $this->usd->save(); } $products = array(); for ($k = 0; $k <= 2; $k++) { $products[$k] = Product::getNewInstance($this->root); $products[$k]->setPrice($this->usd, $k + 1); $products[$k]->save(); $bundled = ProductBundle::getNewInstance($this->container, $products[$k]); $bundled->save(); } $list = ProductBundle::getBundledProductSet($this->container); $this->assertEqual($list->size(), count($products)); foreach ($list as $index => $item) { $this->assertSame($item->relatedProduct->get(), $products[$index]); } $this->assertEqual(ProductBundle::getTotalBundlePrice($this->container, $this->usd), 6); }
function testTransactionCurrencyConverting() { $eur = Currency::getNewInstance('EUR'); $eur->rate->set('3.4528'); $eur->save(); $this->products[0]->setPrice($this->usd, '9.99'); $this->order->addProduct($this->products[0], 1); $this->order->save(); $this->order->changeCurrency($this->usd); //$this->order->finalize(); ActiveRecord::clearPool(); $order = CustomerOrder::getInstanceByID($this->order->getID(), true); $order->loadAll(); $details = new LiveCartTransaction($order, $eur); ActiveRecord::clearPool(); $order = CustomerOrder::getInstanceByID($this->order->getID(), true); $order->loadAll(); $this->assertEquals($details->amount->get(), '2.89'); $result = new TransactionResult(); $result->amount->set($details->amount->get()); $result->currency->set($details->currency->get()); $transaction = Transaction::getNewInstance($order, $result); $transaction->type->set(Transaction::TYPE_SALE); $this->assertEquals($transaction->amount->get(), '9.99'); $this->assertEquals($transaction->realAmount->get(), '2.89'); $transaction->save(); $this->assertFalse((bool) $order->isFinalized->get()); $order->finalize(); $this->assertTrue((bool) $order->isFinalized->get()); $this->assertEquals($order->getPaidAmount(), '9.99'); $this->assertEquals($order->totalAmount->get(), '9.99'); $this->assertTrue((bool) $order->isPaid->get()); }
/** * !Running tests not involving initOrder() method will not recreate Currency, * but setUp() method is wiping all Currecy records, * * Store frontend is not working without Currency object * * As workround this method can be called from test suite to recreate Currency * * @todo: reorganize tests to call DELETE FROM Currency only when setUpCurrency() method is called. * */ protected function setUpCurrency() { if (ActiveRecord::objectExists('Currency', 'USD')) { $this->usd = Currency::getInstanceByID('USD', Currency::LOAD_DATA); } else { $this->usd = Currency::getNewInstance('USD'); $this->usd->setAsDefault(); $this->usd->save(); } }
public function setUp() { parent::setUp(); // create a product without attributes $this->product = Product::getNewInstance($this->category, 'test'); $this->product->setValueByLang("name", "en", "TEST_PRODUCT"); $this->product->save(); $this->productAutoIncrementNumber = $this->product->getID(); for ($k = 1; $k < 4; $k++) { $currency = Currency::getNewInstance($k . 'zz'); $currency->save(); } }
public function ztestModelPlugins() { $this->getApplication()->registerPluginDirectory(ClassLoader::getRealPath('test.fixture.plugin')); $currency = Currency::getNewInstance('ZZZ'); $currency->save(); $this->assertEquals($currency->rate->get(), 0.5); $this->assertFalse((bool) $currency->isEnabled->get()); $currency->rate->set(0.6); $currency->save(); $this->assertTrue((bool) $currency->isEnabled->get()); $array = $currency->toArray(); $this->assertTrue($array['testValue']); $this->getApplication()->unregisterPluginDirectory(ClassLoader::getRealPath('test.fixture.plugin')); }
protected function getInstance($record, CsvImportProfile $profile) { $fields = $profile->getSortedFields(); if (isset($fields['Currency']['ID'])) { try { $instance = Currency::getInstanceByID($record[$fields['Currency']['ID']], true); } catch (ARNotFoundException $e) { } } else { return; } if (empty($instance)) { $instance = Currency::getNewInstance($record[$fields['Currency']['ID']]); } $this->setLastImportedRecordName($instance->getID()); return $instance; }
public function getNextCustomerOrder() { if (!($data = $this->loadRecord('SELECT * FROM ' . $this->getTablePrefix() . 'orders WHERE customerID > 0'))) { return null; } $user = ActiveRecordModel::getInstanceByIDIfExists('User', $this->getRealId('User', $data['customerID'], false)); if (!$user || !$user->isExistingRecord()) { return $this->getNextCustomerOrder(); } $currCode = $data['currency_code']; if (!($currency = ActiveRecordModel::getInstanceByIDIfExists('Currency', $currCode, false))) { $currency = Currency::getNewInstance($currCode); $currency->save(); } $order = CustomerOrder::getNewInstance($user); $order->currency->set($currency); $order->dateCompleted->set($data['order_time']); // products foreach ($this->getDataBySql('SELECT * FROM ' . $this->getTablePrefix() . 'ordered_carts WHERE orderID=' . $data['orderID']) as $item) { $product = null; // try to identify product by SKU preg_match('/\\[(.*)\\]/', $item['name'], $sku); if (isset($sku[1])) { $product = Product::getInstanceBySKU($sku[1]); } // if that doesn't work, then try to match the exact name if (!$product) { $productData = array_shift($this->getDataBySQL('SELECT productID FROM ' . $this->getTablePrefix() . 'products WHERE name="' . addslashes($item['name']) . '"')); if ($productData && is_array($productData)) { $product = Product::getInstanceByID($this->getRealId('Product', $productData['productID']), Product::LOAD_DATA); } } if ($product) { $order->addProduct($product, $item['Quantity'], true); $orderedItem = array_shift($order->getItemsByProduct($product)); $orderedItem->price->set($item['Price']); } } // addresses $order->shippingAddress->set($this->getUserAddress($data, 'shipping_')); $order->billingAddress->set($this->getUserAddress($data, 'billing_')); $order->status->set($this->getOrderStatus($data['statusID'])); if ($order->status->get() == CustomerOrder::STATUS_SHIPPED) { $order->isPaid->set(true); } $order->rawData = $data; return $order; }
public function testMustHavePurchasedToRate() { $user = User::getNewInstance('*****@*****.**'); $user->save(); self::getApplication()->getConfig()->set('ENABLE_REVIEWS', true); self::getApplication()->getConfig()->set('ENABLE_ANONYMOUS_RATINGS', false); self::getApplication()->getConfig()->set('REQUIRE_PURCHASE_TO_RATE', true); $this->request->set('rating_', 4); $this->request->set('ajax', 'true'); $this->assertRatingError(); $this->controller->setUser($user); // login not enough - should still fail $this->assertRatingError(); ActiveRecordModel::executeUpdate('DELETE FROM Currency'); $usd = Currency::getNewInstance('USD'); $usd->save(); $order = CustomerOrder::getNewInstance($user); $order->addProduct($this->product); $order->save(); // order not completed - should still fail $this->assertRatingError(); // order finalized - now works $order->finalize(); $id = $this->product->getID(); $this->setUp(); $this->controller->setUser($user); $this->request->set('id', $id); $this->assertRatingError(false); }
public function testOrderTotalsWithRoundedPrices() { $currency = Currency::getNewInstance('RON'); $currency->setRoundingRule(0, Currency::TRIM, 0.09); $currency->save(); $product = Product::getNewInstance(Category::getRootNode()); $product->isEnabled->set(true); $product->setPrice($currency, 1.26); $product->save(); $order = CustomerOrder::getNewInstance(SessionUser::getAnonymousUser()); $order->addProduct($product); $order->save(true); $item = array_shift($order->getItemsByProduct($product)); $this->assertEquals($product->getPrice($currency), 1.29); $this->assertEquals($item->getSubTotal(), 1.29); $item->count->set(2); $this->assertEquals($item->getSubTotal(), 2.58); $this->assertEquals($order->getTotal(true), 2.58); // add another currency to mix - no rounding rules $bgn = Currency::getNewInstance('BGN'); $bgn->rate->set(2); $bgn->save(); $item->count->set(2); $order->changeCurrency($bgn); $this->assertEquals($product->getPrice($bgn), 0.63); $this->assertEquals($item->getPrice(), 0.63); $this->assertSame($item->getCurrency(), $bgn); $this->assertEquals($item->getSubTotal(), 1.26); $this->assertEquals($order->getTotal(true), 1.26); // add rounding rules $bgn->clearRoundingRules(); $bgn->setRoundingRule(0, Currency::TRIM, 0.07000000000000001); $order->changeCurrency($bgn); $this->assertEquals($product->getPrice($bgn), 0.67); $this->assertEquals($item->getSubTotal(), 1.34); $this->assertEquals($order->getTotal(true), 1.34); }