Пример #1
0
 /**
  * AJAX product class adding
  *
  * @return void
  */
 protected function doActionAdd()
 {
     $data = new \XLite\Model\ProductClass();
     $data->setName(\XLite\Core\Request::getInstance()->name);
     $data = \XLite\Core\Database::getRepo('\\XLite\\Model\\ProductClass')->insert($data);
     $this->data['data'] = array('id' => $data->getId(), 'name' => $data->getName());
     $this->data['status'] = static::STATUS_SUCCESS;
 }
Пример #2
0
 /**
  * Get cached product class by its name
  *
  * @param string $name Product class name
  *
  * @return \XLite\Model\ProductClass Product class object
  */
 protected function getProductClass($name, $create = false)
 {
     if (!isset($this->classesCache[$name])) {
         $this->classesCache[$name] = \XLite\Core\Database::getRepo('XLite\\Model\\ProductClass')->findOneByName($name);
     }
     if ($create && !empty($name) && !$this->classesCache[$name]) {
         $entity = new \XLite\Model\ProductClass();
         $entity->setName($name);
         $productClass = \XLite\Core\Database::getRepo('XLite\\Model\\ProductClass')->insert($entity);
         \XLite\Core\Database::getEM()->persist($productClass);
         $this->classesCache[$name] = $productClass;
     }
     return $this->classesCache[$name];
 }
Пример #3
0
 /**
  * Update product class
  *
  * @return void
  */
 protected function doActionUpdateProductClass()
 {
     $updateClass = false;
     if (-1 == \XLite\Core\Request::getInstance()->productClass) {
         $name = trim(\XLite\Core\Request::getInstance()->newProductClass);
         if ($name) {
             $productClass = new \XLite\Model\ProductClass();
             $productClass->setName($name);
             \XLite\Core\Database::getRepo('\\XLite\\Model\\ProductClass')->insert($productClass);
             $updateClass = true;
         }
     } else {
         $productClass = \XLite\Core\Database::getRepo('\\XLite\\Model\\ProductClass')->find(\XLite\Core\Request::getInstance()->productClass);
         $updateClass = true;
     }
     if ($updateClass) {
         $productClassChanged = $productClass && (!$this->getProduct()->getProductClass() || $productClass->getId() != $this->getProduct()->getProductClass()->getId());
         $this->getProduct()->setProductClass($productClass);
         if ($productClassChanged) {
             \XLite\Core\Database::getRepo('\\XLite\\Model\\Attribute')->generateAttributeValues($this->getProduct(), true);
         }
         \XLite\Core\Database::getEM()->flush();
         \XLite\Core\TopMessage::addInfo('Product class have been updated successfully');
     } else {
         \XLite\Core\TopMessage::addWarning('Product class name is empty');
     }
 }
Пример #4
0
 /**
  * Import classes
  *
  * @param \XLite\Model\Product $product Product
  * @param string               $data    Data
  *
  * @return void
  */
 protected function importClasses(\XLite\Model\Product $product, $data)
 {
     // Remove old classes
     foreach ($product->getClasses() as $class) {
         $class->getProducts()->removeElement($product);
     }
     $product->getClasses()->clear();
     if ($data) {
         // Add classes links
         foreach (explode(';', $data) as $name) {
             $name = trim($name);
             $translation = \XLite\Core\Database::getRepo('XLite\\Model\\ProductClassTranslation')->findOneBy(array('name' => $name));
             if ($translation) {
                 $class = $translation->getOwner();
             } else {
                 $class = new \XLite\Model\ProductClass();
                 $class->setName($name);
                 \XLite\Core\Database::getEM()->persist($class);
             }
             $class->addProducts($product);
             $product->addClasses($class);
         }
     }
 }
Пример #5
0
 public function testCalculation()
 {
     $tax = \XLite\Core\Database::getRepo('XLite\\Module\\CDev\\VAT\\Model\\Tax')->getTax();
     foreach ($tax->getRates() as $rate) {
         $rate->setTax(null);
         \XLite\Core\Database::getEM()->remove($rate);
     }
     $tax->getRates()->clear();
     $tax = \XLite\Core\Database::getRepo('XLite\\Module\\CDev\\SalesTax\\Model\\Tax')->getTax();
     foreach ($tax->getRates() as $rate) {
         \XLite\Core\Database::getEM()->remove($rate);
     }
     $tax->getRates()->clear();
     $rate = new \XLite\Module\CDev\SalesTax\Model\Tax\Rate();
     $rate->setValue(10);
     $rate->setPosition(1);
     \XLite\Core\Database::getEM()->persist($rate);
     $tax->addRates($rate);
     $rate->setTax($tax);
     \XLite\Core\Database::getEM()->flush();
     $order = $this->getTestOrder();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.1);
     $this->assertEquals($etalon, $cost, 'check tax cost 10%');
     // 10%
     $rate = new \XLite\Module\CDev\SalesTax\Model\Tax\Rate();
     $rate->setValue(20);
     $rate->setPosition(2);
     \XLite\Core\Database::getEM()->persist($rate);
     $tax->addRates($rate);
     $rate->setTax($tax);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.1);
     $this->assertEquals($etalon, $cost, 'check tax cost 10% #2');
     // 20%
     $rate->setPosition(0);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.2);
     $this->assertEquals($etalon, $cost, 'check tax cost 20%');
     // Limit by membership (order)
     $memberships = \XLite\Core\Database::getRepo('XLite\\Model\\Membership')->findAll();
     $membership = array_shift($memberships);
     $membership2 = array_shift($memberships);
     $order->getProfile()->setMembership($membership);
     $rate->setMembership($membership);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.2);
     $this->assertEquals($etalon, $cost, 'check tax cost 20% (membership)');
     // Limit by membership (order and rate)
     $rate->setMembership($membership2);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.1);
     $this->assertEquals($etalon, $cost, 'check tax cost 10% (membership)');
     // Limit by product class (rate)
     $rate->setMembership(null);
     foreach (\XLite\Core\Database::getRepo('XLite\\Model\\ProductClass')->findAll() as $pc) {
         \XLite\Core\Database::getEM()->remove($pc);
     }
     $pc = new \XLite\Model\ProductClass();
     $pc->setName('Test PC');
     \XLite\Core\Database::getEM()->persist($pc);
     $rate->setProductClass($pc);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.1);
     $this->assertEquals($etalon, $cost, 'check tax cost 10% (pc)');
     // Limit by product class (item and rate)
     $order->getItems()->get(0)->getProduct()->addClasses($pc);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.2);
     $this->assertEquals($etalon, $cost, 'check tax cost 20% (pc)');
     // Limit by product class (only item)
     $rate->setProductClass(null);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.2);
     $this->assertEquals($etalon, $cost, 'check tax cost 20% (pc)');
     // Remove test product class
     $order->getItems()->get(0)->getProduct()->getClasses()->removeElement($pc);
     \XLite\Core\Database::getEM()->remove($pc);
     \XLite\Core\Database::getEM()->flush();
     // Zone (order)
     foreach (\XLite\Core\Database::getRepo('XLite\\Model\\Zone')->findAll() as $z) {
         \XLite\Core\Database::getEM()->remove($z);
     }
     $z = new \XLite\Model\Zone();
     $z->setZoneName('Test zone');
     $e = new \XLite\Model\ZoneElement();
     $e->setElementValue('AF');
     $e->setElementType('C');
     $z->addZoneElements($e);
     \XLite\Core\Database::getEM()->persist($z);
     $rate->setZone($z);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.1);
     $this->assertEquals($etalon, $cost, 'check tax cost 10% (zone)');
     // Zone (order and rate)
     $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->findOneBy(array('code' => 'AF'));
     $order->getProfile()->getBillingAddress()->setCountry($country);
     \XLite\Core\Database::getEM()->flush();
     $order->calculate();
     $cost = $order->getCurrency()->formatValue($order->getSurchargeSumByType('tax'));
     $etalon = $order->getCurrency()->formatValue($order->getSubtotal() * 0.2);
     $this->assertEquals($etalon, $cost, 'check tax cost 20% (zone)');
 }
Пример #6
0
 /**
  * Return entities list
  *
  * @param \XLite\Core\CommonCell $cnd       Search condition
  * @param boolean                $countOnly Return items list or only its size OPTIONAL
  *
  * @return array|integer
  */
 protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false)
 {
     if ($countOnly) {
         $result = 1 + parent::getData($cnd, $countOnly);
     } else {
         $class = new \XLite\Model\ProductClass();
         $class->setName(static::t('Global attributes'));
         $result = array_merge(array($class), parent::getData($cnd, $countOnly));
     }
     return $result;
 }