public function current() { do { $order = parent::current(); $getNext = false; if ($this->rangeDate !== null && ($order[OrderTableMap::CREATED_AT] < $this->rangeDate['start'] || $order[OrderTableMap::CREATED_AT] > $this->rangeDate['end'])) { $this->next(); $getNext = true; } } while ($getNext && $this->valid()); $locale = $this->language->getLocale(); $query = OrderQuery::create()->useCurrencyQuery()->addAsColumn('currency_CODE', CurrencyTableMap::CODE)->endUse()->useCustomerQuery()->addAsColumn('customer_REF', CustomerTableMap::REF)->endUse()->useOrderProductQuery()->useOrderProductTaxQuery(null, Criteria::LEFT_JOIN)->addAsColumn('product_TAX', 'IF(' . OrderProductTableMap::WAS_IN_PROMO . ',' . 'SUM(' . OrderProductTaxTableMap::PROMO_AMOUNT . '),' . 'SUM(' . OrderProductTaxTableMap::AMOUNT . ')' . ')')->addAsColumn('tax_TITLE', OrderProductTableMap::TAX_RULE_TITLE)->endUse()->addAsColumn('product_TITLE', OrderProductTableMap::TITLE)->addAsColumn('product_PRICE', 'IF(' . OrderProductTableMap::WAS_IN_PROMO . ',' . OrderProductTableMap::PROMO_PRICE . ',' . OrderProductTableMap::PRICE . ')')->addAsColumn('product_QUANTITY', OrderProductTableMap::QUANTITY)->addAsColumn('product_WAS_IN_PROMO', OrderProductTableMap::WAS_IN_PROMO)->groupById()->endUse()->orderById()->groupById()->useOrderCouponQuery(null, Criteria::LEFT_JOIN)->addAsColumn('coupon_COUPONS', 'GROUP_CONCAT(' . OrderCouponTableMap::TITLE . ')')->groupBy(OrderCouponTableMap::ORDER_ID)->endUse()->useModuleRelatedByPaymentModuleIdQuery('payment_module')->addAsColumn('payment_module_TITLE', '`payment_module`.CODE')->endUse()->useModuleRelatedByDeliveryModuleIdQuery('delivery_module')->addAsColumn('delivery_module_TITLE', '`delivery_module`.CODE')->endUse()->useOrderAddressRelatedByDeliveryOrderAddressIdQuery('delivery_address_join')->useCustomerTitleQuery('delivery_address_customer_title_join')->useCustomerTitleI18nQuery('delivery_address_customer_title_i18n_join')->addAsColumn('delivery_address_TITLE', '`delivery_address_customer_title_i18n_join`.SHORT')->endUse()->endUse()->useCountryQuery('delivery_address_country_join')->useCountryI18nQuery('delivery_address_country_i18n_join')->addAsColumn('delivery_address_country_TITLE', '`delivery_address_country_i18n_join`.TITLE')->endUse()->addAsColumn('delivery_address_COMPANY', '`delivery_address_join`.COMPANY')->addAsColumn('delivery_address_FIRSTNAME', '`delivery_address_join`.FIRSTNAME')->addAsColumn('delivery_address_LASTNAME', '`delivery_address_join`.LASTNAME')->addAsColumn('delivery_address_ADDRESS1', '`delivery_address_join`.ADDRESS1')->addAsColumn('delivery_address_ADDRESS2', '`delivery_address_join`.ADDRESS2')->addAsColumn('delivery_address_ADDRESS3', '`delivery_address_join`.ADDRESS3')->addAsColumn('delivery_address_ZIPCODE', '`delivery_address_join`.ZIPCODE')->addAsColumn('delivery_address_CITY', '`delivery_address_join`.CITY')->addAsColumn('delivery_address_PHONE', '`delivery_address_join`.PHONE')->endUse()->endUse()->useOrderAddressRelatedByInvoiceOrderAddressIdQuery('invoice_address_join')->useCustomerTitleQuery('invoice_address_customer_title_join')->useCustomerTitleI18nQuery('invoice_address_customer_title_i18n_join')->addAsColumn('invoice_address_TITLE', '`invoice_address_customer_title_i18n_join`.SHORT')->endUse()->endUse()->useCountryQuery('invoice_address_country_join')->useCountryI18nQuery('invoice_address_country_i18n_join')->addAsColumn('invoice_address_country_TITLE', '`invoice_address_country_i18n_join`.TITLE')->endUse()->endUse()->addAsColumn('invoice_address_COMPANY', '`invoice_address_join`.COMPANY')->addAsColumn('invoice_address_FIRSTNAME', '`invoice_address_join`.FIRSTNAME')->addAsColumn('invoice_address_LASTNAME', '`invoice_address_join`.LASTNAME')->addAsColumn('invoice_address_ADDRESS1', '`invoice_address_join`.ADDRESS1')->addAsColumn('invoice_address_ADDRESS2', '`invoice_address_join`.ADDRESS2')->addAsColumn('invoice_address_ADDRESS3', '`invoice_address_join`.ADDRESS3')->addAsColumn('invoice_address_ZIPCODE', '`invoice_address_join`.ZIPCODE')->addAsColumn('invoice_address_CITY', '`invoice_address_join`.CITY')->addAsColumn('invoice_address_PHONE', '`invoice_address_join`.PHONE')->endUse()->useOrderStatusQuery()->useOrderStatusI18nQuery()->addAsColumn('order_status_TITLE', OrderStatusI18nTableMap::TITLE)->endUse()->endUse()->select([OrderTableMap::REF, 'customer_REF', 'product_TITLE', 'product_PRICE', 'product_TAX', 'tax_TITLE', 'product_QUANTITY', 'product_WAS_IN_PROMO', OrderTableMap::DISCOUNT, 'coupon_COUPONS', OrderTableMap::POSTAGE, 'payment_module_TITLE', OrderTableMap::INVOICE_REF, OrderTableMap::DELIVERY_REF, 'delivery_module_TITLE', 'delivery_address_TITLE', 'delivery_address_COMPANY', 'delivery_address_FIRSTNAME', 'delivery_address_LASTNAME', 'delivery_address_ADDRESS1', 'delivery_address_ADDRESS2', 'delivery_address_ADDRESS3', 'delivery_address_ZIPCODE', 'delivery_address_CITY', 'delivery_address_country_TITLE', 'delivery_address_PHONE', 'invoice_address_TITLE', 'invoice_address_COMPANY', 'invoice_address_FIRSTNAME', 'invoice_address_LASTNAME', 'invoice_address_ADDRESS1', 'invoice_address_ADDRESS2', 'invoice_address_ADDRESS3', 'invoice_address_ZIPCODE', 'invoice_address_CITY', 'invoice_address_country_TITLE', 'invoice_address_PHONE', 'order_status_TITLE', 'currency_CODE', OrderTableMap::CREATED_AT])->orderByCreatedAt(Criteria::DESC); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, '`delivery_address_customer_title_join`.ID', CustomerTitleI18nTableMap::ID, '`delivery_address_customer_title_i18n_join`.LOCALE', $locale); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, '`invoice_address_customer_title_join`.ID', CustomerTitleI18nTableMap::ID, '`invoice_address_customer_title_i18n_join`.LOCALE', $locale); I18n::addI18nCondition($query, CountryI18nTableMap::TABLE_NAME, '`delivery_address_country_join`.ID', CountryI18nTableMap::ID, '`delivery_address_country_i18n_join`.LOCALE', $locale); I18n::addI18nCondition($query, CountryI18nTableMap::TABLE_NAME, '`invoice_address_country_join`.ID', CountryI18nTableMap::ID, '`invoice_address_country_i18n_join`.LOCALE', $locale); I18n::addI18nCondition($query, OrderStatusI18nTableMap::TABLE_NAME, OrderStatusI18nTableMap::ID, OrderStatusTableMap::ID, OrderStatusI18nTableMap::LOCALE, $locale); $data = $query->filterById($order[OrderTableMap::ID])->findOne(); $order = (new Order())->setId($order[OrderTableMap::ID]); $order->setNew(false); $tax = 0; $data['order_TOTAL_TTC'] = $order->getTotalAmount($tax, false, false); $data['order_TOTAL_WITH_DISCOUNT'] = $order->getTotalAmount($tax, false, true); $data['order_TOTAL_WITH_DISCOUNT_AND_POSTAGE'] = $order->getTotalAmount($tax, true, true); return $data; }
/** * Save the attribute combinations for the order from our cart item attribute combinations. * * @param OrderEvent $event * * @throws PropelException */ public function createOrderProductAttributeCombinations(OrderEvent $event) { $legacyCartItemAttributeCombinations = LegacyCartItemAttributeCombinationQuery::create()->findByCartItemId($event->getCartItemId()); // works with Thelia 2.2 if (method_exists($event, 'getId')) { $orderProductId = $event->getId(); } else { // Thelia 2.1 however does not provides the order product id in the event // Since the order contains potentially identical (for Thelia) cart items that are only differentiated // by the cart item attribute combinations that we are storing ourselves, we cannot use information // such as PSE id to cross reference the cart item we are given to the order product that was created from // it (as far as I can tell). // So we will ASSUME that the order product with the higher id is the one created from this cart item. // This is PROBABLY TRUE on a basic Thelia install with no modules messing with the cart and orders in a way // that create additional order products, BUT NOT IN GENERAL ! // This also assumes that ids are generated incrementally, which is NOT GUARANTEED (but true for MySQL // with default settings). // The creation date was previously used but is even less reliable. // FIXME: THIS IS NOT A SANE WAY TO DO THIS $orderProductId = OrderProductQuery::create()->orderById(Criteria::DESC)->findOne()->getId(); } $lang = $this->request->getSession()->getLang(); /** @var LegacyCartItemAttributeCombination $legacyCartItemAttributeCombination */ foreach ($legacyCartItemAttributeCombinations as $legacyCartItemAttributeCombination) { /** @var Attribute $attribute */ $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $legacyCartItemAttributeCombination->getAttributeId()); /** @var AttributeAv $attributeAv */ $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $legacyCartItemAttributeCombination->getAttributeAvId()); (new OrderProductAttributeCombination())->setOrderProductId($orderProductId)->setAttributeTitle($attribute->getTitle())->setAttributeChapo($attribute->getChapo())->setAttributeDescription($attribute->getDescription())->setAttributePostscriptum($attribute->getPostscriptum())->setAttributeAvTitle($attributeAv->getTitle())->setAttributeAvChapo($attributeAv->getChapo())->setAttributeAvDescription($attributeAv->getDescription())->setAttributeAvPostscriptum($attributeAv->getPostscriptum())->save(); } }
protected function getQuery(Lang $lang) { $locale = $lang->getLocale(); $query = OrderQuery::create()->useCurrencyQuery()->addAsColumn("currency_CODE", CurrencyTableMap::CODE)->endUse()->useCustomerQuery()->addAsColumn("customer_REF", CustomerTableMap::REF)->endUse()->useOrderProductQuery()->useOrderProductTaxQuery(null, Criteria::LEFT_JOIN)->addAsColumn("product_TAX", "IF(" . OrderProductTableMap::WAS_IN_PROMO . "," . "SUM(" . OrderProductTaxTableMap::PROMO_AMOUNT . ")," . "SUM(" . OrderProductTaxTableMap::AMOUNT . ")" . ")")->addAsColumn("tax_TITLE", OrderProductTableMap::TAX_RULE_TITLE)->endUse()->addAsColumn("product_TITLE", OrderProductTableMap::TITLE)->addAsColumn("product_PRICE", "IF(" . OrderProductTableMap::WAS_IN_PROMO . "," . OrderProductTableMap::PROMO_PRICE . "," . OrderProductTableMap::PRICE . ")")->addAsColumn("product_QUANTITY", OrderProductTableMap::QUANTITY)->addAsColumn("product_WAS_IN_PROMO", OrderProductTableMap::WAS_IN_PROMO)->groupById()->endUse()->orderById()->groupById()->useOrderCouponQuery(null, Criteria::LEFT_JOIN)->addAsColumn("coupon_COUPONS", "GROUP_CONCAT(" . OrderCouponTableMap::TITLE . ")")->groupBy(OrderCouponTableMap::ORDER_ID)->endUse()->useModuleRelatedByPaymentModuleIdQuery("payment_module")->addAsColumn("payment_module_TITLE", "`payment_module`.CODE")->endUse()->useModuleRelatedByDeliveryModuleIdQuery("delivery_module")->addAsColumn("delivery_module_TITLE", "`delivery_module`.CODE")->endUse()->useOrderAddressRelatedByDeliveryOrderAddressIdQuery("delivery_address_join")->useCustomerTitleQuery("delivery_address_customer_title_join")->useCustomerTitleI18nQuery("delivery_address_customer_title_i18n_join")->addAsColumn("delivery_address_TITLE", "`delivery_address_customer_title_i18n_join`.SHORT")->endUse()->endUse()->useCountryQuery("delivery_address_country_join")->useCountryI18nQuery("delivery_address_country_i18n_join")->addAsColumn("delivery_address_country_TITLE", "`delivery_address_country_i18n_join`.TITLE")->endUse()->addAsColumn("delivery_address_COMPANY", "`delivery_address_join`.COMPANY")->addAsColumn("delivery_address_FIRSTNAME", "`delivery_address_join`.FIRSTNAME")->addAsColumn("delivery_address_LASTNAME", "`delivery_address_join`.LASTNAME")->addAsColumn("delivery_address_ADDRESS1", "`delivery_address_join`.ADDRESS1")->addAsColumn("delivery_address_ADDRESS2", "`delivery_address_join`.ADDRESS2")->addAsColumn("delivery_address_ADDRESS3", "`delivery_address_join`.ADDRESS3")->addAsColumn("delivery_address_ZIPCODE", "`delivery_address_join`.ZIPCODE")->addAsColumn("delivery_address_CITY", "`delivery_address_join`.CITY")->addAsColumn("delivery_address_PHONE", "`delivery_address_join`.PHONE")->endUse()->endUse()->useOrderAddressRelatedByInvoiceOrderAddressIdQuery("invoice_address_join")->useCustomerTitleQuery("invoice_address_customer_title_join")->useCustomerTitleI18nQuery("invoice_address_customer_title_i18n_join")->addAsColumn("invoice_address_TITLE", "`invoice_address_customer_title_i18n_join`.SHORT")->endUse()->endUse()->useCountryQuery("invoice_address_country_join")->useCountryI18nQuery("invoice_address_country_i18n_join")->addAsColumn("invoice_address_country_TITLE", "`invoice_address_country_i18n_join`.TITLE")->endUse()->endUse()->addAsColumn("invoice_address_COMPANY", "`invoice_address_join`.COMPANY")->addAsColumn("invoice_address_FIRSTNAME", "`invoice_address_join`.FIRSTNAME")->addAsColumn("invoice_address_LASTNAME", "`invoice_address_join`.LASTNAME")->addAsColumn("invoice_address_ADDRESS1", "`invoice_address_join`.ADDRESS1")->addAsColumn("invoice_address_ADDRESS2", "`invoice_address_join`.ADDRESS2")->addAsColumn("invoice_address_ADDRESS3", "`invoice_address_join`.ADDRESS3")->addAsColumn("invoice_address_ZIPCODE", "`invoice_address_join`.ZIPCODE")->addAsColumn("invoice_address_CITY", "`invoice_address_join`.CITY")->addAsColumn("invoice_address_PHONE", "`invoice_address_join`.PHONE")->endUse()->useOrderStatusQuery()->useOrderStatusI18nQuery()->addAsColumn("order_status_TITLE", OrderStatusI18nTableMap::TITLE)->endUse()->endUse()->select([OrderTableMap::REF, "customer_REF", "product_TITLE", "product_PRICE", "product_TAX", "tax_TITLE", "product_QUANTITY", "product_WAS_IN_PROMO", OrderTableMap::DISCOUNT, "coupon_COUPONS", OrderTableMap::POSTAGE, "payment_module_TITLE", OrderTableMap::INVOICE_REF, OrderTableMap::DELIVERY_REF, "delivery_module_TITLE", "delivery_address_TITLE", "delivery_address_COMPANY", "delivery_address_FIRSTNAME", "delivery_address_LASTNAME", "delivery_address_ADDRESS1", "delivery_address_ADDRESS2", "delivery_address_ADDRESS3", "delivery_address_ZIPCODE", "delivery_address_CITY", "delivery_address_country_TITLE", "delivery_address_PHONE", "invoice_address_TITLE", "invoice_address_COMPANY", "invoice_address_FIRSTNAME", "invoice_address_LASTNAME", "invoice_address_ADDRESS1", "invoice_address_ADDRESS2", "invoice_address_ADDRESS3", "invoice_address_ZIPCODE", "invoice_address_CITY", "invoice_address_country_TITLE", "invoice_address_PHONE", "order_status_TITLE", "currency_CODE", OrderTableMap::CREATED_AT])->orderByCreatedAt(Criteria::DESC); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, "`delivery_address_customer_title_join`.ID", CustomerTitleI18nTableMap::ID, "`delivery_address_customer_title_i18n_join`.LOCALE", $locale); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, "`invoice_address_customer_title_join`.ID", CustomerTitleI18nTableMap::ID, "`invoice_address_customer_title_i18n_join`.LOCALE", $locale); I18n::addI18nCondition($query, CountryI18nTableMap::TABLE_NAME, "`delivery_address_country_join`.ID", CountryI18nTableMap::ID, "`delivery_address_country_i18n_join`.LOCALE", $locale); I18n::addI18nCondition($query, CountryI18nTableMap::TABLE_NAME, "`invoice_address_country_join`.ID", CountryI18nTableMap::ID, "`invoice_address_country_i18n_join`.LOCALE", $locale); I18n::addI18nCondition($query, OrderStatusI18nTableMap::TABLE_NAME, OrderStatusI18nTableMap::ID, OrderStatusTableMap::ID, OrderStatusI18nTableMap::LOCALE, $locale); return $query; }
public function buildDataSet(Lang $lang) { /** @var \Thelia\Model\AttributeCombinationQuery $query */ $query = parent::buildDataSet($lang); $pseJoin = new Join(AttributeCombinationTableMap::PRODUCT_SALE_ELEMENTS_ID, ProductSaleElementsTableMap::ID); $pseJoin->setRightTableAlias("pse_tax_join"); $productJoin = new Join(ProductSaleElementsTableMap::ID, ProductTableMap::ID); $productJoin->setRightTableAlias("product_tax_join"); $taxJoin = new Join("`product_tax_join`.TAX_RULE_ID", TaxRuleTableMap::ID, Criteria::LEFT_JOIN); $taxI18nJoin = new Join(TaxRuleTableMap::ID, TaxRuleI18nTableMap::ID, Criteria::LEFT_JOIN); $query->addJoinObject($pseJoin, "pse_tax_join")->addJoinObject($productJoin, "product_tax_join")->addJoinObject($productJoin)->addJoinObject($taxJoin)->addJoinObject($taxI18nJoin)->addAsColumn("product_TAX_TITLE", TaxRuleI18nTableMap::TITLE)->addAsColumn("tax_ID", TaxRuleTableMap::ID)->select($query->getSelect() + ["product_TAX_TITLE", "tax_ID"]); I18n::addI18nCondition($query, TaxRuleI18nTableMap::TABLE_NAME, TaxRuleTableMap::ID, TaxRuleI18nTableMap::ID, TaxRuleI18nTableMap::LOCALE, $lang->getLocale()); $dataSet = $query->keepQuery(true)->find()->toArray(); $productSaleElements = ProductSaleElementsQuery::create()->find()->toKeyIndex("Id"); $currencies = CurrencyQuery::create()->find()->toKeyIndex("Code"); foreach ($dataSet as &$line) { /** @var \Thelia\Model\ProductSaleElements $pse */ $pse = $productSaleElements[$line["product_sale_elements_ID"]]; $pricesTools = $pse->getPricesByCurrency($currencies[$line["currency_CODE"]]); $line["price_PRICE"] = $pricesTools->getPrice(); $line["price_PROMO_PRICE"] = $pricesTools->getPromoPrice(); } return $dataSet; }
/** * @param EventDispatcherInterface $dispatcher * @param ModelOrder $sessionOrder * @param CurrencyModel $currency * @param LangModel $lang * @param CartModel $cart * @param UserInterface $customer * @param bool $manageStock decrement stock when order is created if true * @return ModelOrder * @throws \Exception * @throws \Propel\Runtime\Exception\PropelException */ protected function createOrder(EventDispatcherInterface $dispatcher, ModelOrder $sessionOrder, CurrencyModel $currency, LangModel $lang, CartModel $cart, UserInterface $customer, $manageStock) { $con = Propel::getConnection(OrderTableMap::DATABASE_NAME); $con->beginTransaction(); $placedOrder = $sessionOrder->copy(); $placedOrder->setDispatcher($dispatcher); $deliveryAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenDeliveryAddress()); $taxCountry = $deliveryAddress->getCountry(); $invoiceAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenInvoiceAddress()); $cartItems = $cart->getCartItems(); /* fulfill order */ $placedOrder->setCustomerId($customer->getId()); $placedOrder->setCurrencyId($currency->getId()); $placedOrder->setCurrencyRate($currency->getRate()); $placedOrder->setLangId($lang->getId()); /* hard save the delivery and invoice addresses */ $deliveryOrderAddress = new OrderAddress(); $deliveryOrderAddress->setCustomerTitleId($deliveryAddress->getTitleId())->setCompany($deliveryAddress->getCompany())->setFirstname($deliveryAddress->getFirstname())->setLastname($deliveryAddress->getLastname())->setAddress1($deliveryAddress->getAddress1())->setAddress2($deliveryAddress->getAddress2())->setAddress3($deliveryAddress->getAddress3())->setZipcode($deliveryAddress->getZipcode())->setCity($deliveryAddress->getCity())->setPhone($deliveryAddress->getPhone())->setCountryId($deliveryAddress->getCountryId())->save($con); $invoiceOrderAddress = new OrderAddress(); $invoiceOrderAddress->setCustomerTitleId($invoiceAddress->getTitleId())->setCompany($invoiceAddress->getCompany())->setFirstname($invoiceAddress->getFirstname())->setLastname($invoiceAddress->getLastname())->setAddress1($invoiceAddress->getAddress1())->setAddress2($invoiceAddress->getAddress2())->setAddress3($invoiceAddress->getAddress3())->setZipcode($invoiceAddress->getZipcode())->setCity($invoiceAddress->getCity())->setPhone($invoiceAddress->getPhone())->setCountryId($invoiceAddress->getCountryId())->save($con); $placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId()); $placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId()); $placedOrder->setStatusId(OrderStatusQuery::getNotPaidStatus()->getId()); $placedOrder->setCartId($cart->getId()); /* memorize discount */ $placedOrder->setDiscount($cart->getDiscount()); $placedOrder->save($con); /* fulfill order_products and decrease stock */ foreach ($cartItems as $cartItem) { $product = $cartItem->getProduct(); /* get translation */ /** @var ProductI18n $productI18n */ $productI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'Product', $product->getId()); $pse = $cartItem->getProductSaleElements(); // get the virtual document path $virtualDocumentEvent = new VirtualProductOrderHandleEvent($placedOrder, $pse->getId()); // essentially used for virtual product. modules that handles virtual product can // allow the use of stock even for virtual products $useStock = true; $virtual = 0; // if the product is virtual, dispatch an event to collect information if ($product->getVirtual() === 1) { $dispatcher->dispatch(TheliaEvents::VIRTUAL_PRODUCT_ORDER_HANDLE, $virtualDocumentEvent); $useStock = $virtualDocumentEvent->isUseStock(); $virtual = $virtualDocumentEvent->isVirtual() ? 1 : 0; } /* check still in stock */ if ($cartItem->getQuantity() > $pse->getQuantity() && true === ConfigQuery::checkAvailableStock() && $useStock) { throw new TheliaProcessException("Not enough stock", TheliaProcessException::CART_ITEM_NOT_ENOUGH_STOCK, $cartItem); } if ($useStock && $manageStock) { /* decrease stock for non virtual product */ $allowNegativeStock = intval(ConfigQuery::read('allow_negative_stock', 0)); $newStock = $pse->getQuantity() - $cartItem->getQuantity(); //Forbid negative stock if ($newStock < 0 && 0 === $allowNegativeStock) { $newStock = 0; } $pse->setQuantity($newStock); $pse->save($con); } /* get tax */ /** @var TaxRuleI18n $taxRuleI18n */ $taxRuleI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'TaxRule', $product->getTaxRuleId()); $taxDetail = $product->getTaxRule()->getTaxDetail($product, $taxCountry, $cartItem->getPrice(), $cartItem->getPromoPrice(), $lang->getLocale()); $orderProduct = new OrderProduct(); $orderProduct->setOrderId($placedOrder->getId())->setProductRef($product->getRef())->setProductSaleElementsRef($pse->getRef())->setProductSaleElementsId($pse->getId())->setTitle($productI18n->getTitle())->setChapo($productI18n->getChapo())->setDescription($productI18n->getDescription())->setPostscriptum($productI18n->getPostscriptum())->setVirtual($virtual)->setVirtualDocument($virtualDocumentEvent->getPath())->setQuantity($cartItem->getQuantity())->setPrice($cartItem->getPrice())->setPromoPrice($cartItem->getPromoPrice())->setWasNew($pse->getNewness())->setWasInPromo($cartItem->getPromo())->setWeight($pse->getWeight())->setTaxRuleTitle($taxRuleI18n->getTitle())->setTaxRuleDescription($taxRuleI18n->getDescription())->setEanCode($pse->getEanCode())->setCartItemId($cartItem->getId())->setDispatcher($dispatcher)->save($con); /* fulfill order_product_tax */ /** @var OrderProductTax $tax */ foreach ($taxDetail as $tax) { $tax->setOrderProductId($orderProduct->getId()); $tax->save($con); } /* fulfill order_attribute_combination and decrease stock */ foreach ($pse->getAttributeCombinations() as $attributeCombination) { /** @var \Thelia\Model\Attribute $attribute */ $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $attributeCombination->getAttributeId()); /** @var \Thelia\Model\AttributeAv $attributeAv */ $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $attributeCombination->getAttributeAvId()); $orderAttributeCombination = new OrderProductAttributeCombination(); $orderAttributeCombination->setOrderProductId($orderProduct->getId())->setAttributeTitle($attribute->getTitle())->setAttributeChapo($attribute->getChapo())->setAttributeDescription($attribute->getDescription())->setAttributePostscriptum($attribute->getPostscriptum())->setAttributeAvTitle($attributeAv->getTitle())->setAttributeAvChapo($attributeAv->getChapo())->setAttributeAvDescription($attributeAv->getDescription())->setAttributeAvPostscriptum($attributeAv->getPostscriptum())->save($con); } } $con->commit(); return $placedOrder; }
/** * @param $untaxedPrice * @param null $taxCollection returns OrderProductTaxCollection * @param null $askedLocale * * @return int * @throws \Thelia\Exception\TaxEngineException */ public function getTaxedPrice($untaxedPrice, &$taxCollection = null, $askedLocale = null) { if (null === $this->taxRulesCollection) { throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION); } if (null === $this->product) { throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT); } if (false === filter_var($untaxedPrice, FILTER_VALIDATE_FLOAT)) { throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT); } $taxedPrice = $untaxedPrice; $currentPosition = 1; $currentTax = 0; if (null !== $taxCollection) { $taxCollection = new OrderProductTaxCollection(); } foreach ($this->taxRulesCollection as $taxRule) { $position = (int) $taxRule->getTaxRuleCountryPosition(); $taxType = $taxRule->getTypeInstance(); if ($currentPosition !== $position) { $taxedPrice += $currentTax; $currentTax = 0; $currentPosition = $position; } $taxAmount = $taxType->calculate($this->product, $taxedPrice); $currentTax += $taxAmount; if (null !== $taxCollection) { $taxI18n = I18n::forceI18nRetrieving($askedLocale, 'Tax', $taxRule->getId()); $orderProductTax = new OrderProductTax(); $orderProductTax->setTitle($taxI18n->getTitle()); $orderProductTax->setDescription($taxI18n->getDescription()); $orderProductTax->setAmount($taxAmount); $taxCollection->addTax($orderProductTax); } } $taxedPrice += $currentTax; return $taxedPrice; }
/** * @param Lang $lang * @return array|\Propel\Runtime\ActiveQuery\ModelCriteria * * The tax engine of Thelia is in PHP, so we can't compute orders for each customers * directly in SQL, we need two SQL queries, and some computing to get the last order amount and total amount. */ public function buildDataSet(Lang $lang) { $locale = $lang->getLocale(); /** * This first query get each customer info and addresses. */ $newsletterJoin = new Join(CustomerTableMap::EMAIL, NewsletterTableMap::EMAIL, Criteria::LEFT_JOIN); $query = CustomerQuery::create()->useCustomerTitleQuery("customer_title_")->useCustomerTitleI18nQuery("customer_title_i18n_")->addAsColumn("title_TITLE", "customer_title_i18n_.SHORT")->endUse()->endUse()->useAddressQuery()->useCountryQuery()->useCountryI18nQuery()->addAsColumn("address_COUNTRY", CountryI18nTableMap::TITLE)->endUse()->endUse()->useCustomerTitleQuery("address_title")->useCustomerTitleI18nQuery("address_title_i18n")->addAsColumn("address_TITLE", "address_title_i18n.SHORT")->endUse()->endUse()->addAsColumn("address_LABEL", AddressTableMap::LABEL)->addAsColumn("address_FIRST_NAME", AddressTableMap::FIRSTNAME)->addAsColumn("address_LAST_NAME", AddressTableMap::LASTNAME)->addAsColumn("address_COMPANY", AddressTableMap::COMPANY)->addAsColumn("address_ADDRESS1", AddressTableMap::ADDRESS1)->addAsColumn("address_ADDRESS2", AddressTableMap::ADDRESS2)->addAsColumn("address_ADDRESS3", AddressTableMap::ADDRESS3)->addAsColumn("address_ZIPCODE", AddressTableMap::ZIPCODE)->addAsColumn("address_CITY", AddressTableMap::CITY)->addAsColumn("address_PHONE", AddressTableMap::PHONE)->addAsColumn("address_CELLPHONE", AddressTableMap::CELLPHONE)->addAsColumn("address_IS_DEFAULT", AddressTableMap::IS_DEFAULT)->endUse()->addJoinObject($newsletterJoin)->addAsColumn("newsletter_IS_REGISTRED", "IF(NOT ISNULL(" . NewsletterTableMap::EMAIL . "),1,0)")->select([CustomerTableMap::ID, CustomerTableMap::REF, CustomerTableMap::LASTNAME, CustomerTableMap::FIRSTNAME, CustomerTableMap::EMAIL, CustomerTableMap::DISCOUNT, CustomerTableMap::CREATED_AT, "title_TITLE", "address_TITLE", "address_LABEL", "address_COMPANY", "address_FIRST_NAME", "address_LAST_NAME", "address_ADDRESS1", "address_ADDRESS2", "address_ADDRESS3", "address_ZIPCODE", "address_CITY", "address_COUNTRY", "address_PHONE", "address_CELLPHONE", "address_IS_DEFAULT", "newsletter_IS_REGISTRED"])->orderById(); I18n::addI18nCondition($query, CountryI18nTableMap::TABLE_NAME, CountryTableMap::ID, CountryI18nTableMap::ID, CountryI18nTableMap::LOCALE, $locale); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, "`customer_title_`.ID", "`customer_title_i18n_`.ID", "`customer_title_i18n_`.LOCALE", $locale); I18n::addI18nCondition($query, CustomerTitleI18nTableMap::TABLE_NAME, "`address_title`.ID", "`address_title_i18n`.ID", "`address_title_i18n`.LOCALE", $locale); /** @var CustomerQuery $query */ $results = $query->find()->toArray(); /** * Then get the orders */ $orders = OrderQuery::create()->useCustomerQuery()->orderById()->endUse()->find(); /** * And add them info the array */ $orders->rewind(); $arrayLength = count($results); $previousCustomerId = null; for ($i = 0; $i < $arrayLength; ++$i) { $currentCustomer =& $results[$i]; $currentCustomerId = $currentCustomer[CustomerTableMap::ID]; unset($currentCustomer[CustomerTableMap::ID]); if ($currentCustomerId === $previousCustomerId) { $currentCustomer["title_TITLE"] = ""; $currentCustomer[CustomerTableMap::LASTNAME] = ""; $currentCustomer[CustomerTableMap::FIRSTNAME] = ""; $currentCustomer[CustomerTableMap::EMAIL] = ""; $currentCustomer["address_COMPANY"] = ""; $currentCustomer["newsletter_IS_REGISTRED"] = ""; $currentCustomer[CustomerTableMap::CREATED_AT] = ""; $currentCustomer[CustomerTableMap::DISCOUNT] = ""; $currentCustomer += ["order_TOTAL" => "", "last_order_AMOUNT" => "", "last_order_DATE" => ""]; } else { /** * Reformat created_at date */ $date = $currentCustomer[CustomerTableMap::CREATED_AT]; $dateTime = new \DateTime($date); $currentCustomer[CustomerTableMap::CREATED_AT] = $dateTime->format($lang->getDatetimeFormat()); /** * Then compute everything about the orders */ $total = 0; $lastOrderAmount = 0; $lastOrderDate = null; $lastOrder = null; $lastOrderCurrencyCode = null; $lastOrderId = 0; $defaultCurrency = Currency::getDefaultCurrency(); $defaultCurrencyCode = $defaultCurrency->getCode(); if (empty($defaultCurrencyCode)) { $defaultCurrencyCode = $defaultCurrency->getCode(); } $formattedDate = null; /** @var \Thelia\Model\Order $currentOrder */ while (false !== ($currentOrder = $orders->current())) { if ($currentCustomerId != $currentOrder->getCustomerId()) { break; } $amount = $currentOrder->getTotalAmount($tax); if (0 < ($rate = $currentOrder->getCurrencyRate())) { $amount = round($amount / $rate, 2); } $total += $amount; /** @var \DateTime $date */ $date = $currentOrder->getCreatedAt(); if (null === $lastOrderDate || $date >= $lastOrderDate && $lastOrderId < $currentOrder->getId()) { $lastOrder = $currentOrder; $lastOrderDate = $date; $lastOrderId = $currentOrder->getId(); } $orders->next(); } if ($lastOrderDate !== null) { $formattedDate = $lastOrderDate->format($lang->getDatetimeFormat()); $orderCurrency = $lastOrder->getCurrency(); $lastOrderCurrencyCode = $orderCurrency->getCode(); if (empty($lastOrderCurrencyCode)) { $lastOrderCurrencyCode = $orderCurrency->getCode(); } $lastOrderAmount = $lastOrder->getTotalAmount($tax_); } $currentCustomer += ["order_TOTAL" => $total . " " . $defaultCurrencyCode, "last_order_AMOUNT" => $lastOrderAmount === 0 ? "" : $lastOrderAmount . " " . $lastOrderCurrencyCode, "last_order_DATE" => $formattedDate]; } $previousCustomerId = $currentCustomerId; } return $results; }
protected function createOrder(EventDispatcherInterface $dispatcher, ModelOrder $sessionOrder, CurrencyModel $currency, LangModel $lang, CartModel $cart, CustomerModel $customer) { $con = \Propel\Runtime\Propel::getConnection(OrderTableMap::DATABASE_NAME); $con->beginTransaction(); $placedOrder = $sessionOrder->copy(); $placedOrder->setDispatcher($dispatcher); $deliveryAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenDeliveryAddress()); $taxCountry = $deliveryAddress->getCountry(); $invoiceAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenInvoiceAddress()); $cartItems = $cart->getCartItems(); /* fulfill order */ $placedOrder->setCustomerId($customer->getId()); $placedOrder->setCurrencyId($currency->getId()); $placedOrder->setCurrencyRate($currency->getRate()); $placedOrder->setLangId($lang->getId()); /* hard save the delivery and invoice addresses */ $deliveryOrderAddress = new OrderAddress(); $deliveryOrderAddress->setCustomerTitleId($deliveryAddress->getTitleId())->setCompany($deliveryAddress->getCompany())->setFirstname($deliveryAddress->getFirstname())->setLastname($deliveryAddress->getLastname())->setAddress1($deliveryAddress->getAddress1())->setAddress2($deliveryAddress->getAddress2())->setAddress3($deliveryAddress->getAddress3())->setZipcode($deliveryAddress->getZipcode())->setCity($deliveryAddress->getCity())->setPhone($deliveryAddress->getPhone())->setCountryId($deliveryAddress->getCountryId())->save($con); $invoiceOrderAddress = new OrderAddress(); $invoiceOrderAddress->setCustomerTitleId($invoiceAddress->getTitleId())->setCompany($invoiceAddress->getCompany())->setFirstname($invoiceAddress->getFirstname())->setLastname($invoiceAddress->getLastname())->setAddress1($invoiceAddress->getAddress1())->setAddress2($invoiceAddress->getAddress2())->setAddress3($invoiceAddress->getAddress3())->setZipcode($invoiceAddress->getZipcode())->setCity($invoiceAddress->getCity())->setPhone($invoiceAddress->getPhone())->setCountryId($invoiceAddress->getCountryId())->save($con); $placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId()); $placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId()); $placedOrder->setStatusId(OrderStatusQuery::getNotPaidStatus()->getId()); $placedOrder->setCart($cart); /* memorize discount */ $placedOrder->setDiscount($cart->getDiscount()); $placedOrder->save($con); /* fulfill order_products and decrease stock */ foreach ($cartItems as $cartItem) { $product = $cartItem->getProduct(); /* get translation */ $productI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'Product', $product->getId()); $pse = $cartItem->getProductSaleElements(); /* check still in stock */ if ($cartItem->getQuantity() > $pse->getQuantity() && true === ConfigQuery::checkAvailableStock() && 0 === $product->getVirtual()) { throw new TheliaProcessException("Not enough stock", TheliaProcessException::CART_ITEM_NOT_ENOUGH_STOCK, $cartItem); } if (0 === $product->getVirtual()) { /* decrease stock for non virtual product */ $allowNegativeStock = intval(ConfigQuery::read('allow_negative_stock', 0)); $newStock = $pse->getQuantity() - $cartItem->getQuantity(); //Forbid negative stock if ($newStock < 0 && 0 === $allowNegativeStock) { $newStock = 0; } $pse->setQuantity($newStock); $pse->save($con); } /* get tax */ $taxRuleI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'TaxRule', $product->getTaxRuleId()); $taxDetail = $product->getTaxRule()->getTaxDetail($product, $taxCountry, $cartItem->getPrice(), $cartItem->getPromoPrice(), $lang->getLocale()); // get the virtual document path $virtualDocumentPath = null; if ($product->getVirtual() === 1) { // try to find the associated document if (null !== ($documentId = MetaDataQuery::getVal('virtual', MetaDataModel::PSE_KEY, $pse->getId()))) { $productDocument = ProductDocumentQuery::create()->findPk($documentId); if (null !== $productDocument) { $virtualDocumentPath = $productDocument->getFile(); } } } $orderProduct = new OrderProduct(); $orderProduct->setOrderId($placedOrder->getId())->setProductRef($product->getRef())->setProductSaleElementsRef($pse->getRef())->setProductSaleElementsId($pse->getId())->setTitle($productI18n->getTitle())->setChapo($productI18n->getChapo())->setDescription($productI18n->getDescription())->setPostscriptum($productI18n->getPostscriptum())->setVirtual($product->getVirtual())->setVirtualDocument($virtualDocumentPath)->setQuantity($cartItem->getQuantity())->setPrice($cartItem->getPrice())->setPromoPrice($cartItem->getPromoPrice())->setWasNew($pse->getNewness())->setWasInPromo($cartItem->getPromo())->setWeight($pse->getWeight())->setTaxRuleTitle($taxRuleI18n->getTitle())->setTaxRuleDescription($taxRuleI18n->getDescription())->setEanCode($pse->getEanCode())->setCartIemId($cartItem->getId())->setDispatcher($dispatcher)->save($con); /* fulfill order_product_tax */ foreach ($taxDetail as $tax) { $tax->setOrderProductId($orderProduct->getId()); $tax->save($con); } /* fulfill order_attribute_combination and decrease stock */ foreach ($pse->getAttributeCombinations() as $attributeCombination) { $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $attributeCombination->getAttributeId()); $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $attributeCombination->getAttributeAvId()); $orderAttributeCombination = new OrderProductAttributeCombination(); $orderAttributeCombination->setOrderProductId($orderProduct->getId())->setAttributeTitle($attribute->getTitle())->setAttributeChapo($attribute->getChapo())->setAttributeDescription($attribute->getDescription())->setAttributePostscriptum($attribute->getPostscriptum())->setAttributeAvTitle($attributeAv->getTitle())->setAttributeAvChapo($attributeAv->getChapo())->setAttributeAvDescription($attributeAv->getDescription())->setAttributeAvPostscriptum($attributeAv->getPostscriptum())->save($con); } } $con->commit(); return $placedOrder; }
/** * @param Cart $cart * @param $areaId * @return |null */ protected function getSlicePostage(Cart $cart, Country $country) { $config = self::getConfig(); $currency = $cart->getCurrency(); $areaId = $country->getAreaId(); $query = CustomDeliverySliceQuery::create()->filterByAreaId($areaId); if ($config['method'] != CustomDelivery::METHOD_PRICE) { $query->filterByWeightMax($cart->getWeight(), Criteria::GREATER_THAN); $query->orderByWeightMax(Criteria::ASC); } if ($config['method'] != CustomDelivery::METHOD_WEIGHT) { $total = $cart->getTotalAmount(); // convert amount to the default currency if (0 == $currency->getByDefault()) { $total = $total / $currency->getRate(); } $query->filterByPriceMax($total, Criteria::GREATER_THAN); $query->orderByPriceMax(Criteria::ASC); } $slice = $query->findOne(); $postage = null; if (null !== $slice) { $postage = new OrderPostage(); if (0 == $currency->getByDefault()) { $price = $slice->getPrice() * $currency->getRate(); } else { $price = $slice->getPrice(); } $price = round($price, 2); $postage->setAmount($price); $postage->setAmountTax(0); // taxed amount if (0 !== $config['tax']) { $taxRuleI18N = I18n::forceI18nRetrieving($this->getRequest()->getSession()->getLang()->getLocale(), 'TaxRule', $config['tax']); $taxRule = TaxRuleQuery::create()->findPk($config['tax']); if (null !== $taxRule) { $taxCalculator = new Calculator(); $taxCalculator->loadTaxRuleWithoutProduct($taxRule, $country); $postage->setAmount(round($taxCalculator->getTaxedPrice($price), 2)); $postage->setAmountTax($postage->getAmount() - $price); $postage->setTaxRuleTitle($taxRuleI18N->getTitle()); } } } return $postage; }