示例#1
0
 /**
  * Exclude object from result
  *
  * @param   ChildCart $cart Object to remove from the list of results
  *
  * @return ChildCartQuery The current query, for fluid interface
  */
 public function prune($cart = null)
 {
     if ($cart) {
         $this->addUsingAlias(CartTableMap::ID, $cart->getId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
示例#2
0
 /**
  * Filter the query by a related \Thelia\Model\Cart object
  *
  * @param \Thelia\Model\Cart|ObjectCollection $cart The related object(s) to use as filter
  * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return ChildCartItemQuery The current query, for fluid interface
  */
 public function filterByCart($cart, $comparison = null)
 {
     if ($cart instanceof \Thelia\Model\Cart) {
         return $this->addUsingAlias(CartItemTableMap::CART_ID, $cart->getId(), $comparison);
     } elseif ($cart instanceof ObjectCollection) {
         if (null === $comparison) {
             $comparison = Criteria::IN;
         }
         return $this->addUsingAlias(CartItemTableMap::CART_ID, $cart->toKeyValue('PrimaryKey', 'Id'), $comparison);
     } else {
         throw new PropelException('filterByCart() only accepts arguments of type \\Thelia\\Model\\Cart or Collection');
     }
 }
示例#3
0
文件: Order.php 项目: badelas/thelia
 /**
  * @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;
 }
示例#4
0
 /**
  * Set the cart to store in the current session.
  *
  * @param Cart|null The cart to store in session
  *
  * @return $this
  */
 public function setSessionCart(Cart $cart = null)
 {
     if (null === $cart || $cart->isNew()) {
         self::$transientCart = $cart;
         $this->remove("thelia.cart_id");
     } else {
         self::$transientCart = null;
         $this->set("thelia.cart_id", $cart->getId());
     }
     return $this;
 }
示例#5
0
 /**
  * Declares an association between this object and a ChildCart object.
  *
  * @param                  ChildCart $v
  * @return                 \Thelia\Model\Order The current object (for fluent API support)
  * @throws PropelException
  */
 public function setCart(ChildCart $v = null)
 {
     if ($v === null) {
         $this->setCartId(NULL);
     } else {
         $this->setCartId($v->getId());
     }
     $this->aCart = $v;
     // Add binding for other direction of this n:n relationship.
     // If this object has already been added to the ChildCart object, it will not be re-added.
     if ($v !== null) {
         $v->addOrder($this);
     }
     return $this;
 }
示例#6
0
 /**
  * @param  \Thelia\Core\HttpFoundation\Session\Session $session
  * @return \Thelia\Model\Cart
  */
 protected function createCart(Session $session)
 {
     $cart = new CartModel();
     $cart->setToken($this->generateCookie($session));
     $cart->setCurrency($session->getCurrency(true));
     if (null !== ($customer = $session->getCustomerUser())) {
         $cart->setCustomer($customer);
     }
     $cart->save();
     $session->setCart($cart->getId());
     return $cart;
 }
示例#7
0
 public function testGetCartWithExistingCartAndCustomerAndReferencesEachOther()
 {
     $session = $this->session;
     //create a fake customer just for test. If not persists test fails !
     $customer = new Customer();
     $customer->setFirstname("john test session");
     $customer->setLastname("doe");
     $customer->setTitleId(1);
     $customer->save();
     $session->setCustomerUser($customer);
     $testCart = new Cart();
     $testCart->setToken(uniqid("testSessionGetCart3", true));
     $testCart->setCustomerId($customer->getId());
     $testCart->save();
     $session->setCart($testCart->getId());
     $cart = $session->getCart();
     $this->assertNotNull($cart);
     $this->assertInstanceOf("\\Thelia\\Model\\Cart", $cart, '$cart must be an instance of Thelia\\Model\\Cart');
 }
示例#8
0
 public function import($startRecord = 0)
 {
     $count = 0;
     $errors = 0;
     $hdl = $this->t1db->query(sprintf("select * from commande order by id asc limit %d, %d", intval($startRecord), $this->getChunkSize()));
     while ($hdl && ($commande = $this->t1db->fetch_object($hdl))) {
         $count++;
         try {
             $this->order_corresp->getT2($commande->id);
             Tlog::getInstance()->warning("Order ID={$commande->id} already imported.");
             continue;
         } catch (ImportException $ex) {
             // Okay, the order was not imported.
         }
         try {
             if (null === ($customer = CustomerQuery::create()->findPk($this->cust_corresp->getT2($commande->client)))) {
                 throw new ImportException("Failed to find customer ID={$commande->client}");
             }
             if (null === ($status = OrderStatusQuery::create()->findPk($commande->statut))) {
                 throw new ImportException("Failed to find order status ID={$commande->statut}");
             }
             // Create invoice address
             if (false == ($adr_livr = $this->t1db->query_obj("select * from venteadr where id=?", array($commande->adrlivr)))) {
                 throw new ImportException("Failed to find delivery adresse ID={$commande->adrlivr}");
             }
             // Create invoice address
             if (false == ($adr_fact = $this->t1db->query_obj("select * from venteadr where id=?", array($commande->adrfact)))) {
                 throw new ImportException("Failed to find billing adresse ID={$commande->adrfact}");
             }
             $con = Propel::getConnection(OrderTableMap::DATABASE_NAME);
             $con->beginTransaction();
             try {
                 $order = new Order();
                 $delivery_adr = new OrderAddress();
                 $delivery_adr->setCustomerTitleId($this->getT2CustomerTitle($adr_livr->raison)->getId())->setCompany(isset($adr_livr->entreprise) ? $adr_livr->entreprise : '')->setFirstname($adr_livr->prenom)->setLastname($adr_livr->nom)->setAddress1($adr_livr->adresse1)->setAddress2($adr_livr->adresse2)->setAddress3($adr_livr->adresse3)->setZipcode($adr_livr->cpostal)->setCity($adr_livr->ville)->setPhone($adr_livr->tel)->setCountryId($this->getT2Country($adr_livr->pays)->getId())->save($con);
                 $billing_adr = new OrderAddress();
                 $billing_adr->setCustomerTitleId($this->getT2CustomerTitle($adr_fact->raison)->getId())->setCompany(isset($adr_fact->entreprise) ? $adr_fact->entreprise : '')->setFirstname($adr_fact->prenom)->setLastname($adr_fact->nom)->setAddress1($adr_fact->adresse1)->setAddress2($adr_fact->adresse2)->setAddress3($adr_fact->adresse3)->setZipcode($adr_fact->cpostal)->setCity($adr_fact->ville)->setPhone($adr_fact->tel)->setCountryId($this->getT2Country($adr_fact->pays)->getId())->save($con);
                 // Find the first availables delivery and payment modules, that's the best we can do.
                 $deliveryModule = ModuleQuery::create()->findOneByType(BaseModule::DELIVERY_MODULE_TYPE);
                 $paymentModule = ModuleQuery::create()->findOneByType(BaseModule::PAYMENT_MODULE_TYPE);
                 // Create a cart (now required)
                 $cart = new Cart();
                 $cart->save();
                 $order->setRef($commande->ref)->setCustomer($customer)->setInvoiceDate($commande->datefact)->setCurrency($this->getT2Currency($commande->devise))->setCurrencyRate($this->getT2Currency($commande->devise)->getRate())->setTransactionRef($commande->transaction)->setDeliveryRef($commande->livraison)->setInvoiceRef($commande->facture)->setPostage($commande->port)->setStatusId($status->getId())->setLang($this->getT2Lang($commande->lang))->setDeliveryOrderAddressId($delivery_adr->getId())->setInvoiceOrderAddressId($billing_adr->getId())->setDeliveryModuleId($deliveryModule->getId())->setPaymentModuleId($paymentModule->getId())->setDiscount($commande->remise)->setCartId($cart->getId())->save($con);
                 // Update the order reference
                 $order->setRef($commande->ref)->setCreatedAt($commande->date)->save();
                 if ($commande->remise > 0) {
                     $coupon = new OrderCoupon();
                     $coupon->setOrder($order)->setCode('Not Available')->setType('UNKNOWN')->setAmount($commande->remise)->setTitle('Imported from Thelia 1')->setShortDescription('Imported from Thelia 1')->setDescription('Imported from Thelia 1')->setExpirationDate(time())->setIsCumulative(false)->setIsRemovingPostage(false)->setIsAvailableOnSpecialOffers(false)->setSerializedConditions(new ConditionCollection(array()))->save($con);
                 }
                 $vps = $this->t1db->query_list("select * from venteprod where commande=?", array($commande->id));
                 foreach ($vps as $vp) {
                     $parent = 0;
                     if (isset($vp->parent) && $vp->parent != 0) {
                         $parent = $this->product_corresp->getT2($vp->parent);
                     }
                     $orderProduct = new OrderProduct();
                     $orderProduct->setOrder($order)->setProductRef($vp->ref)->setProductSaleElementsRef($vp->ref)->setTitle($vp->titre)->setChapo($vp->chapo)->setDescription($vp->description)->setPostscriptum("")->setQuantity($vp->quantite)->setPrice($vp->prixu / (1 + $vp->tva / 100))->setPromoPrice($vp->prixu / (1 + $vp->tva / 100))->setWasNew(false)->setWasInPromo(false)->setWeight(0)->setEanCode("")->setTaxRuleTitle("")->setTaxRuleDescription("")->setParent($parent)->save($con);
                     $orderProductTax = new OrderProductTax();
                     $orderProductTax->setAmount($orderProduct->getPrice() * ($vp->tva / 100))->setPromoAmount(0)->setOrderProduct($orderProduct)->setTitle("TVA {$vp->tva} %")->setDescription("TVA {$vp->tva} %")->save($con);
                 }
                 $con->commit();
             } catch (\Exception $ex) {
                 $con->rollBack();
                 throw $ex;
             }
         } catch (\Exception $ex) {
             Tlog::getInstance()->addError("Failed to import order ref {$commande->ref}: ", $ex->getMessage());
             $errors++;
         }
     }
     return new ImportChunkResult($count, $errors);
 }