public function testRenderLoop()
 {
     $customerId = CustomerQuery::create()->findOne()->getId();
     $this->handler->expects($this->any())->method("buildDataSet")->willReturn($this->handler->renderLoop("address", ["customer" => $customerId]));
     $lang = Lang::getDefaultLanguage();
     $loop = $this->handler->buildDataSet($lang);
     $this->assertInstanceOf("Thelia\\Core\\Template\\Loop\\Address", $loop);
     $data = $this->handler->buildData($lang);
     $addresses = AddressQuery::create()->filterByCustomerId($customerId)->find()->toArray("Id");
     foreach ($data->getData() as $row) {
         $this->assertArrayHasKey("id", $row);
         $this->assertArrayHasKey($row["id"], $addresses);
         $this->assertEquals(count($addresses), $row["loop_total"]);
         $address = $addresses[$row["id"]];
         $this->assertEquals($row["address1"], $address["Address1"]);
         $this->assertEquals($row["address2"], $address["Address2"]);
         $this->assertEquals($row["address3"], $address["Address3"]);
         $this->assertEquals($row["cellphone"], $address["Cellphone"]);
         $this->assertEquals($row["city"], $address["City"]);
         $this->assertEquals($row["company"], $address["Company"]);
         $this->assertEquals($row["country"], $address["CountryId"]);
         $this->assertEquals($row["create_date"], $address["CreatedAt"]);
         $this->assertEquals($row["update_date"], $address["UpdatedAt"]);
         $this->assertEquals($row["firstname"], $address["Firstname"]);
         $this->assertEquals($row["lastname"], $address["Lastname"]);
         $this->assertEquals($row["id"], $address["Id"]);
         $this->assertEquals($row["label"], $address["Label"]);
         $this->assertEquals($row["phone"], $address["Phone"]);
         $this->assertEquals($row["title"], $address["TitleId"]);
         $this->assertEquals($row["zipcode"], $address["Zipcode"]);
     }
 }
Exemple #2
0
 public function buildModelCriteria()
 {
     $search = AddressQuery::create();
     $id = $this->getId();
     if (null !== $id && !in_array($id, array('*', 'any'))) {
         $search->filterById($id, Criteria::IN);
     }
     $customer = $this->getCustomer();
     if ($customer === 'current') {
         $currentCustomer = $this->securityContext->getCustomerUser();
         if ($currentCustomer === null) {
             return null;
         } else {
             $search->filterByCustomerId($currentCustomer->getId(), Criteria::EQUAL);
         }
     } else {
         $search->filterByCustomerId($customer, Criteria::EQUAL);
     }
     $default = $this->getDefault();
     if ($default === true) {
         $search->filterByIsDefault(1, Criteria::EQUAL);
     } elseif ($default === false) {
         $search->filterByIsDefault(0, Criteria::EQUAL);
     }
     $exclude = $this->getExclude();
     if (null !== $exclude && 'none' !== $exclude) {
         $search->filterById($exclude, Criteria::NOT_IN);
     }
     return $search;
 }
 protected function checkValidInvoice()
 {
     $order = $this->getSession()->getOrder();
     if (null === $order || null === $order->getChoosenInvoiceAddress() || null === $order->getPaymentModuleId() || null === AddressQuery::create()->findPk($order->getChoosenInvoiceAddress()) || null === ModuleQuery::create()->findPk($order->getPaymentModuleId())) {
         throw new RedirectException($this->retrieveUrlFromRouteId('order.invoice'));
     }
 }
Exemple #4
0
 /**
  * this method returns an array ***Thanks cap'tain obvious \(^.^)/***
  *->
  * @return array
  */
 public function buildArray()
 {
     // Find the address ... To find ! \m/
     $zipcode = $this->getZipcode();
     $city = $this->getCity();
     $address = $this->getAddress();
     $address = array("zipcode" => $zipcode, "city" => $city, "address" => "", "countrycode" => "FR");
     if (empty($zipcode) || empty($city)) {
         $search = AddressQuery::create();
         $customer = $this->securityContext->getCustomerUser();
         if ($customer !== null) {
             $search->filterByCustomerId($customer->getId());
             $search->filterByIsDefault("1");
         } else {
             throw new \ErrorException("Customer not connected.");
         }
         $search = $search->findOne();
         $address["zipcode"] = $search->getZipcode();
         $address["city"] = $search->getCity();
         $address["address"] = $search->getAddress1();
         $address["countrycode"] = $search->getCountry()->getIsoalpha2();
     }
     // Then ask the Web Service
     $request = new FindByAddress();
     $request->setAddress($address["address"])->setZipCode($address["zipcode"])->setCity($address["city"])->setCountryCode($address["countrycode"])->setFilterRelay("1")->setRequestId(md5(microtime()))->setLang("FR")->setOptionInter("1")->setShippingDate(date("d/m/Y"))->setAccountNumber(ConfigQuery::read('socolissimo_login'))->setPassword(ConfigQuery::read('socolissimo_pwd'));
     try {
         $response = $request->exec();
     } catch (InvalidArgumentException $e) {
         $response = array();
     } catch (\SoapFault $e) {
         $response = array();
     }
     return $response;
 }
Exemple #5
0
 public function testFormatAddress()
 {
     // Test for address in France
     $countryFR = CountryQuery::create()->filterByIsoalpha2('FR')->findOne();
     $address = AddressQuery::create()->findOne();
     $address->setCountryId($countryFR->getId())->save();
     $data = $this->renderString('{format_address address=$address locale="fr_FR"}', ['address' => $address->getId()]);
     $title = $address->getCustomerTitle()->setLocale('fr_FR')->getShort();
     $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="postal-code">%s</span> <span class="locality">%s</span><br>', $address->getZipcode(), $address->getCity()), '<span class="country">France</span>', '</p>'];
     $this->assertEquals($data, implode("\n", $expected));
     // Test for address in USA
     $stateDC = StateQuery::create()->filterByIsocode('DC')->findOne();
     $countryUS = $stateDC->getCountry();
     $address->setCountryId($countryUS->getId())->setStateId($stateDC->getId())->save();
     $data = $this->renderString('{format_address address=$address locale="en_US"}', ['address' => $address->getId()]);
     $title = $address->getCustomerTitle()->setLocale('en_US')->getShort();
     $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="locality">%s</span>, <span class="administrative-area">%s</span> <span class="postal-code">%s</span><br>', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), '<span class="country">United States</span>', '</p>'];
     $this->assertEquals($data, implode("\n", $expected));
     // Test html tag
     $data = $this->renderString('{format_address html_tag="address" html_class="a_class" html_id="an_id" address=$address}', ['address' => $address->getId()]);
     $this->assertTrue(strpos($data, '<address class="a_class" id="an_id">') !== false);
     // Test plain text
     $data = $this->renderString('{format_address html="0" address=$address locale="en_US"}', ['address' => $address->getId()]);
     $expected = [sprintf('%s %s %s', $title, $address->getLastname(), $address->getFirstname()), sprintf('%s', $address->getAddress1()), sprintf('%s, %s %s', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), 'United States'];
     $this->assertEquals($data, implode("\n", $expected));
 }
Exemple #6
0
 public function verifyDeliveryAddress($value, ExecutionContextInterface $context)
 {
     $address = AddressQuery::create()->findPk($value);
     if (null === $address) {
         $context->addViolation(Translator::getInstance()->trans("Address ID not found"));
     }
 }
 public function isModuleDpdPickup(OrderEvent $event)
 {
     $address = AddressIcirelaisQuery::create()->findPk($event->getDeliveryAddress());
     if ($this->check_module($event->getDeliveryModule())) {
         //tmp solution
         $request = $this->getRequest();
         $pr_code = $request->request->get('pr_code');
         if (!empty($pr_code)) {
             // Get details w/ SOAP
             $con = new \SoapClient(__DIR__ . "/../Config/exapaq.wsdl", array('soap_version' => SOAP_1_2));
             $response = $con->GetPudoDetails(array("pudo_id" => $pr_code));
             $xml = new \SimpleXMLElement($response->GetPudoDetailsResult->any);
             if (isset($xml->ERROR)) {
                 throw new \ErrorException("Error while choosing pick-up & go store: " . $xml->ERROR);
             }
             $customer_name = AddressQuery::create()->findPk($event->getDeliveryAddress());
             $request->getSession()->set('DpdPickupDeliveryId', $event->getDeliveryAddress());
             if ($address === null) {
                 $address = new AddressIcirelais();
                 $address->setId($event->getDeliveryAddress());
             }
             // France Métropolitaine
             $address->setCode($pr_code)->setCompany((string) $xml->PUDO_ITEMS->PUDO_ITEM->NAME)->setAddress1((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS1)->setAddress2((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS2)->setAddress3((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS3)->setZipcode((string) $xml->PUDO_ITEMS->PUDO_ITEM->ZIPCODE)->setCity((string) $xml->PUDO_ITEMS->PUDO_ITEM->CITY)->setFirstname($customer_name->getFirstname())->setLastname($customer_name->getLastname())->setTitleId($customer_name->getTitleId())->setCountryId($customer_name->getCountryId())->save();
         } else {
             throw new \ErrorException("No pick-up & go store chosen for DpdPickup delivery module");
         }
     } elseif (null !== $address) {
         $address->delete();
     }
 }
Exemple #8
0
 /**
  * Return an Address a CouponManager can process
  *
  * @return \Thelia\Model\Address
  */
 public function getDeliveryAddress()
 {
     try {
         return AddressQuery::create()->findPk($this->getRequest()->getSession()->getOrder()->getChoosenDeliveryAddress());
     } catch (\Exception $ex) {
         throw new \LogicException("Failed to get delivery address (" . $ex->getMessage() . ")");
     }
 }
 /**
  * Coupon consuming
  */
 public function consumeAction()
 {
     $this->checkCartNotEmpty();
     $message = false;
     $couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME);
     try {
         $form = $this->validateForm($couponCodeForm, 'post');
         $couponCode = $form->get('coupon-code')->getData();
         if (null === $couponCode || empty($couponCode)) {
             $message = true;
             throw new \Exception($this->getTranslator()->trans('Coupon code can\'t be empty', [], Front::MESSAGE_DOMAIN));
         }
         $couponConsumeEvent = new CouponConsumeEvent($couponCode);
         // Dispatch Event to the Action
         $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
         /* recalculate postage amount */
         $order = $this->getSession()->getOrder();
         if (null !== $order) {
             $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
             $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
             if (null !== $deliveryModule && null !== $deliveryAddress) {
                 $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
                 $orderEvent = new OrderEvent($order);
                 try {
                     $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
                     $orderEvent->setPostage($postage->getAmount());
                     $orderEvent->setPostageTax($postage->getAmountTax());
                     $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
                 } catch (DeliveryException $ex) {
                     // The postage has been chosen, but changes dues to coupon causes an exception.
                     // Reset the postage data in the order
                     $orderEvent->setDeliveryModule(0);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
                 }
             }
         }
         return $this->generateSuccessRedirect($couponCodeForm);
     } catch (FormValidationException $e) {
         $message = $this->getTranslator()->trans('Please check your coupon code: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN);
     } catch (UnmatchableConditionException $e) {
         $message = $this->getTranslator()->trans('You should <a href="%sign">sign in</a> or <a href="%register">register</a> to use this coupon', ['%sign' => $this->retrieveUrlFromRouteId('customer.login.view'), '%register' => $this->retrieveUrlFromRouteId('customer.create.view')], Front::MESSAGE_DOMAIN);
     } catch (PropelException $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
     } catch (\Exception $e) {
         $message = $this->getTranslator()->trans('Sorry, an error occurred: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN);
     }
     if ($message !== false) {
         Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
         $couponCodeForm->setErrorMessage($message);
         $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message);
     }
     return $this->generateErrorRedirect($couponCodeForm);
 }
Exemple #10
0
 /**
  * this method returns an array
  *
  * @return array
  */
 public function buildArray()
 {
     $id = $this->getId();
     /** @var \Thelia\Core\HttpFoundation\Session\Session $session */
     $session = $this->container->get('request')->getSession();
     $address = AddressQuery::create()->filterByCustomerId($session->getCustomerUser()->getId())->findPk($id);
     if ($address === null) {
         throw new Exception("The requested address doesn't exist");
     }
     /** @var \Thelia\Model\Customer $customer */
     $customer = $session->getCustomerUser();
     return array('Id' => '0', 'Label' => $address->getLabel(), 'CustomerId' => $address->getCustomerId(), 'TitleId' => $address->getTitleId(), 'Company' => ConfigQuery::read('store_name'), 'Firstname' => $customer->getFirstname(), 'Lastname' => $customer->getLastname(), 'Address1' => ConfigQuery::read('store_address1'), 'Address2' => ConfigQuery::read('store_address2'), 'Address3' => ConfigQuery::read('store_address3'), 'Zipcode' => ConfigQuery::read('store_zipcode'), 'City' => ConfigQuery::read('store_city'), 'CountryId' => ConfigQuery::read('store_country'), 'Phone' => $address->getPhone(), 'Cellphone' => $address->getCellphone(), 'IsDefault' => '0');
 }
 protected function migrateAddress(MigrateCountryEvent $event)
 {
     $con = Propel::getWriteConnection(AddressTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         $updatedRows = AddressQuery::create()->filterByCountryId($event->getCountry())->update(['CountryId' => $event->getNewCountry(), 'StateId' => $event->getNewState()]);
         $con->commit();
         return $updatedRows;
     } catch (PropelException $e) {
         $con->rollback();
         throw $e;
     }
 }
Exemple #12
0
 public function checkValidDeliveryFunction($params, &$smarty)
 {
     $order = $this->request->getSession()->getOrder();
     /* Does address and module still exists ? We assume address owner can't change neither module type */
     if ($order !== null) {
         $checkAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
         $checkModule = ModuleQuery::create()->findPk($order->getDeliveryModuleId());
     }
     if (null === $order || null == $checkAddress || null === $checkModule) {
         throw new OrderException('Delivery must be defined', OrderException::UNDEFINED_DELIVERY, array('missing' => 1));
     }
     return "";
 }
Exemple #13
0
 /**
  * Coupon consuming
  */
 public function consumeAction()
 {
     $this->checkAuth();
     $this->checkCartNotEmpty();
     $message = false;
     $couponCodeForm = new CouponCode($this->getRequest());
     try {
         $form = $this->validateForm($couponCodeForm, 'post');
         $couponCode = $form->get('coupon-code')->getData();
         if (null === $couponCode || empty($couponCode)) {
             $message = true;
             throw new \Exception('Coupon code can\'t be empty');
         }
         $couponConsumeEvent = new CouponConsumeEvent($couponCode);
         // Dispatch Event to the Action
         $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
         /* recalculate postage amount */
         $order = $this->getSession()->getOrder();
         if (null !== $order) {
             $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
             $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
             if (null !== $deliveryModule && null !== $deliveryAddress) {
                 $moduleInstance = $deliveryModule->getModuleInstance($this->container);
                 $orderEvent = new OrderEvent($order);
                 try {
                     $postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
                     $orderEvent->setPostage($postage);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
                 } catch (DeliveryException $ex) {
                     // The postage has been chosen, but changes dues to coupon causes an exception.
                     // Reset the postage data in the order
                     $orderEvent->setDeliveryModule(0);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
                 }
             }
         }
         return $this->generateSuccessRedirect($couponCodeForm);
     } catch (FormValidationException $e) {
         $message = sprintf('Please check your coupon code: %s', $e->getMessage());
     } catch (PropelException $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
     } catch (\Exception $e) {
         $message = sprintf('Sorry, an error occurred: %s', $e->getMessage());
     }
     if ($message !== false) {
         Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
         $couponCodeForm->setErrorMessage($message);
         $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message);
     }
 }
Exemple #14
0
 public function preImport()
 {
     // Empty address, customer and customer title table
     OrderQuery::create()->deleteAll();
     AddressQuery::create()->deleteAll();
     OrderAddressQuery::create()->deleteAll();
     CustomerQuery::create()->deleteAll();
     // Also empty url rewriting table
     $con = Propel::getConnection(RewritingUrlTableMap::DATABASE_NAME);
     $con->exec('SET FOREIGN_KEY_CHECKS=0');
     RewritingUrlQuery::create()->deleteAll();
     $con->exec('SET FOREIGN_KEY_CHECKS=1');
     $this->cust_corresp->reset();
     if ($this->thelia_version > 150) {
         $this->importCustomerTitle();
     }
 }
 public function buildModelCriteria()
 {
     $zipcode = $this->getZipcode();
     $city = $this->getCity();
     if (!empty($zipcode) and !empty($city)) {
         $this->zipcode = $zipcode;
         $this->city = $city;
         $this->addressflag = false;
     } else {
         $search = AddressQuery::create();
         $customer = $this->securityContext->getCustomerUser();
         if ($customer !== null) {
             $search->filterByCustomerId($customer->getId());
             $search->filterByIsDefault("1");
         } else {
             throw new \ErrorException("Customer not connected.");
         }
         return $search;
     }
 }
Exemple #16
0
 public function getDeliveryModuleListAjaxAction()
 {
     $this->checkXmlHttpRequest();
     // Change the delivery address if customer has changed it
     $session = $this->getSession();
     $addressId = $this->getRequest()->get('address_id', null);
     if (null !== $addressId && $addressId !== $session->getOrder()->getChoosenDeliveryAddress()) {
         $address = AddressQuery::create()->findPk($addressId);
         if (null !== $address && $address->getCustomerId() === $session->getCustomerUser()->getId()) {
             $session->getOrder()->setChoosenDeliveryAddress($addressId);
         }
     }
     // seems to be not necessary anymore since we have the new adress selected.
     // keep for compatibility with old template
     // TODO remove in version 2.3
     $countryId = $this->getRequest()->get('country_id', $this->container->get('thelia.taxEngine')->getDeliveryCountry()->getId());
     $state = $this->container->get('thelia.taxEngine')->getDeliveryState();
     $stateId = $state !== null ? $state->getId() : null;
     $stateId = $this->getRequest()->get('state_id', $stateId);
     $args = array('country' => $countryId, 'state' => $stateId, 'address' => $session->getOrder()->getChoosenDeliveryAddress());
     return $this->render('ajax/order-delivery-module-list', $args);
 }
Exemple #17
0
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Customer is new, it will return
  * an empty collection; or if this Customer has previously
  * been saved, it will retrieve related Addresses from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in Customer.
  *
  * @param      Criteria $criteria optional Criteria object to narrow the query
  * @param      ConnectionInterface $con optional connection object
  * @param      string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN)
  * @return Collection|ChildAddress[] List of ChildAddress objects
  */
 public function getAddressesJoinCountry($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN)
 {
     $query = ChildAddressQuery::create(null, $criteria);
     $query->joinWith('Country', $joinBehavior);
     return $this->getAddresses($query, $con);
 }
 /**
  * Load an existing object from the database
  */
 protected function getExistingObject()
 {
     return AddressQuery::create()->findPk($this->getRequest()->get('address_id'));
 }
 public function deleteAction($address_id)
 {
     $this->checkAuth();
     $error_message = false;
     $customer = $this->getSecurityContext()->getCustomerUser();
     $address = AddressQuery::create()->findPk($address_id);
     if (!$address || $customer->getId() != $address->getCustomerId()) {
         // If Ajax Request
         if ($this->getRequest()->isXmlHttpRequest()) {
             return $this->jsonResponse(json_encode(array("success" => false, "message" => "Error during address deletion process")));
         } else {
             return $this->generateRedirectFromRoute('default');
         }
     }
     try {
         $this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address));
     } catch (\Exception $e) {
         $error_message = $e->getMessage();
     }
     \Thelia\Log\Tlog::getInstance()->error(sprintf('Error during address deletion : %s', $error_message));
     // If Ajax Request
     if ($this->getRequest()->isXmlHttpRequest()) {
         if ($error_message) {
             $response = $this->jsonResponse(json_encode(array("success" => false, "message" => $error_message)));
         } else {
             $response = $this->jsonResponse(json_encode(array("success" => true, "message" => "")));
         }
         return $response;
     } else {
         return $this->generateRedirectFromRoute('default', array('view' => 'account'));
     }
 }
Exemple #20
0
 /**
  * @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;
 }
Exemple #21
0
 /**
  * Get the associated ChildAddress object
  *
  * @param      ConnectionInterface $con Optional Connection object.
  * @return                 ChildAddress The associated ChildAddress object.
  * @throws PropelException
  */
 public function getAddressRelatedByAddressInvoiceId(ConnectionInterface $con = null)
 {
     if ($this->aAddressRelatedByAddressInvoiceId === null && $this->address_invoice_id !== null) {
         $this->aAddressRelatedByAddressInvoiceId = ChildAddressQuery::create()->findPk($this->address_invoice_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aAddressRelatedByAddressInvoiceId->addCartsRelatedByAddressInvoiceId($this);
            */
     }
     return $this->aAddressRelatedByAddressInvoiceId;
 }
Exemple #22
0
 protected function afterModifyCart()
 {
     /* recalculate postage amount */
     $order = $this->getSession()->getOrder();
     if (null !== $order) {
         $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
         $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
         if (null !== $deliveryModule && null !== $deliveryAddress) {
             $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
             $orderEvent = new OrderEvent($order);
             try {
                 $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
                 $orderEvent->setPostage($postage->getAmount());
                 $orderEvent->setPostageTax($postage->getAmountTax());
                 $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
                 $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
             } catch (DeliveryException $ex) {
                 // The postage has been chosen, but changes in the cart causes an exception.
                 // Reset the postage data in the order
                 $orderEvent->setDeliveryModule(0);
                 $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
             }
         }
     }
 }
 /**
  * Retrieve the chosen delivery address for a cart or the default customer address if not exists
  *
  * @return null|Address
  */
 protected function getCustomerAddress()
 {
     $deliveryAddress = null;
     $addressId = $this->getSession()->getOrder()->getChoosenDeliveryAddress();
     if (null === $addressId) {
         $customer = $this->getSecurityContext()->getCustomerUser();
         $deliveryAddress = AddressQuery::create()->filterByCustomerId($customer->getId())->orderByIsDefault(Criteria::DESC)->findOne();
         if (null !== $deliveryAddress) {
             $this->getSession()->getOrder()->setChoosenDeliveryAddress($deliveryAddress->getId());
         }
     } else {
         $deliveryAddress = AddressQuery::create()->findPk($addressId);
     }
     return $deliveryAddress;
 }
Exemple #24
0
 /**
  * Find Tax Country Id
  * First look for a picked delivery address country
  * Then look at the current customer default address country
  * Else look at the default website country
  * @return null|Country
  */
 public function getDeliveryCountry()
 {
     if (null === $this->taxCountry) {
         /* is there a logged in customer ? */
         if (null !== ($customer = $this->session->getCustomerUser())) {
             if (null !== $this->session->getOrder() && null !== $this->session->getOrder()->getChoosenDeliveryAddress() && null !== ($currentDeliveryAddress = AddressQuery::create()->findPk($this->session->getOrder()->getChoosenDeliveryAddress()))) {
                 $this->taxCountry = $currentDeliveryAddress->getCountry();
                 $this->taxState = $currentDeliveryAddress->getState();
             } else {
                 $customerDefaultAddress = $customer->getDefaultAddress();
                 $this->taxCountry = $customerDefaultAddress->getCountry();
                 $this->taxState = $customerDefaultAddress->getState();
             }
         }
         if (null == $this->taxCountry) {
             $this->taxCountry = CountryQuery::create()->findOneByByDefault(1);
             $this->taxState = null;
         }
     }
     return $this->taxCountry;
 }
Exemple #25
0
 public function getDeliveryModuleListAjaxAction()
 {
     $this->checkXmlHttpRequest();
     $country = $this->getRequest()->get('country_id', $this->container->get('thelia.taxEngine')->getDeliveryCountry()->getId());
     // Change the delivery address if customer has changed it
     $session = $this->getSession();
     $addressId = $this->getRequest()->get('address_id', null);
     if (null !== $addressId && $addressId !== $session->getOrder()->getChoosenDeliveryAddress()) {
         $address = AddressQuery::create()->findPk($addressId);
         if (null !== $address && $address->getCustomerId() === $session->getCustomerUser()->getId()) {
             $session->getOrder()->setChoosenDeliveryAddress($addressId);
             $args["address"] = $addressId;
         }
     }
     $args = array('country' => $country, 'address' => $session->getOrder()->getChoosenDeliveryAddress());
     return $this->render('ajax/order-delivery-module-list', $args);
 }
Exemple #26
0
 /**
  * Bug found in Thelia 2.0.2
  */
 public function testUpdateDefaultAddress()
 {
     /**
      * Disable propel cache in order to get a new instance of the
      * active record in $updatedAddress
      */
     Propel::disableInstancePooling();
     /**
      * Get a customer and it's default address
      */
     $customer = CustomerQuery::create()->findOne();
     $defaultAddress = $customer->getDefaultAddress();
     $addressId = $defaultAddress->getId();
     /**
      * Try to update the address, and set the isDefault argument,
      * that should keep this address as the default one.
      */
     $addressEvent = new AddressCreateOrUpdateEvent("", 1, "Thelia modif", "Thelia modif", "cour des étoiles", "rue des miracles", "", "63000", "clermont-ferrand", 64, "0102030405", "", "", 1);
     $addressEvent->setAddress($defaultAddress);
     $addressEvent->setDispatcher($this->getMock("Symfony\\Component\\EventDispatcher\\EventDispatcherInterface"));
     /**
      * Do the update
      */
     $actionAddress = new Address();
     $actionAddress->update($addressEvent);
     $updatedAddress = AddressQuery::create()->findPk($addressId);
     /**
      * This address should still be the default address
      */
     $this->assertEquals(1, $updatedAddress->getIsDefault());
     /**
      * Renable it after
      */
     Propel::enableInstancePooling();
 }
Exemple #27
0
 public function testCreate()
 {
     $validDeliveryAddress = AddressQuery::create()->findOneByCustomerId($this->customer->getId());
     $validInvoiceAddress = AddressQuery::create()->filterById($validDeliveryAddress->getId(), Criteria::NOT_EQUAL)->findOneByCustomerId($this->customer->getId());
     $deliveryModule = ModuleQuery::create()->filterByType(BaseModule::DELIVERY_MODULE_TYPE)->filterByActivate(1)->findOne();
     if (null === $deliveryModule) {
         throw new \Exception('No Delivery Module fixture found');
     }
     $paymentModule = ModuleQuery::create()->filterByType(BaseModule::PAYMENT_MODULE_TYPE)->filterByActivate(1)->findOne();
     if (null === $paymentModule) {
         throw new \Exception('No Payment Module fixture found');
     }
     /* define payment module in container */
     $paymentModuleClass = $paymentModule->getFullNamespace();
     $this->container->set(sprintf('module.%s', $paymentModule->getCode()), new $paymentModuleClass());
     $this->orderEvent->getOrder()->setChoosenDeliveryAddress($validDeliveryAddress->getId());
     $this->orderEvent->getOrder()->setChoosenInvoiceAddress($validInvoiceAddress->getId());
     $this->orderEvent->getOrder()->setDeliveryModuleId($deliveryModule->getId());
     $this->orderEvent->getOrder()->setPostage(20);
     $this->orderEvent->getOrder()->setPaymentModuleId($paymentModule->getId());
     /* memorize current stocks */
     $itemsStock = array();
     foreach ($this->cartItems as $index => $cartItem) {
         $itemsStock[$index] = $cartItem->getProductSaleElements()->getQuantity();
     }
     $this->orderAction->create($this->orderEvent);
     $placedOrder = $this->orderEvent->getPlacedOrder();
     $this->assertNotNull($placedOrder);
     $this->assertNotNull($placedOrder->getId());
     /* check customer */
     $this->assertEquals($this->customer->getId(), $placedOrder->getCustomerId(), 'customer i does not  match');
     /* check delivery address */
     $deliveryOrderAddress = $placedOrder->getOrderAddressRelatedByDeliveryOrderAddressId();
     $this->assertEquals($validDeliveryAddress->getCustomerTitle()->getId(), $deliveryOrderAddress->getCustomerTitleId(), 'delivery address title does not match');
     $this->assertEquals($validDeliveryAddress->getCompany(), $deliveryOrderAddress->getCompany(), 'delivery address company does not match');
     $this->assertEquals($validDeliveryAddress->getFirstname(), $deliveryOrderAddress->getFirstname(), 'delivery address fistname does not match');
     $this->assertEquals($validDeliveryAddress->getLastname(), $deliveryOrderAddress->getLastname(), 'delivery address lastname does not match');
     $this->assertEquals($validDeliveryAddress->getAddress1(), $deliveryOrderAddress->getAddress1(), 'delivery address address1 does not match');
     $this->assertEquals($validDeliveryAddress->getAddress2(), $deliveryOrderAddress->getAddress2(), 'delivery address address2 does not match');
     $this->assertEquals($validDeliveryAddress->getAddress3(), $deliveryOrderAddress->getAddress3(), 'delivery address address3 does not match');
     $this->assertEquals($validDeliveryAddress->getZipcode(), $deliveryOrderAddress->getZipcode(), 'delivery address zipcode does not match');
     $this->assertEquals($validDeliveryAddress->getCity(), $deliveryOrderAddress->getCity(), 'delivery address city does not match');
     $this->assertEquals($validDeliveryAddress->getPhone(), $deliveryOrderAddress->getPhone(), 'delivery address phone does not match');
     $this->assertEquals($validDeliveryAddress->getCountryId(), $deliveryOrderAddress->getCountryId(), 'delivery address country does not match');
     /* check invoice address */
     $invoiceOrderAddress = $placedOrder->getOrderAddressRelatedByInvoiceOrderAddressId();
     $this->assertEquals($validInvoiceAddress->getCustomerTitle()->getId(), $invoiceOrderAddress->getCustomerTitleId(), 'invoice address title does not match');
     $this->assertEquals($validInvoiceAddress->getCompany(), $invoiceOrderAddress->getCompany(), 'invoice address company does not match');
     $this->assertEquals($validInvoiceAddress->getFirstname(), $invoiceOrderAddress->getFirstname(), 'invoice address fistname does not match');
     $this->assertEquals($validInvoiceAddress->getLastname(), $invoiceOrderAddress->getLastname(), 'invoice address lastname does not match');
     $this->assertEquals($validInvoiceAddress->getAddress1(), $invoiceOrderAddress->getAddress1(), 'invoice address address1 does not match');
     $this->assertEquals($validInvoiceAddress->getAddress2(), $invoiceOrderAddress->getAddress2(), 'invoice address address2 does not match');
     $this->assertEquals($validInvoiceAddress->getAddress3(), $invoiceOrderAddress->getAddress3(), 'invoice address address3 does not match');
     $this->assertEquals($validInvoiceAddress->getZipcode(), $invoiceOrderAddress->getZipcode(), 'invoice address zipcode does not match');
     $this->assertEquals($validInvoiceAddress->getCity(), $invoiceOrderAddress->getCity(), 'invoice address city does not match');
     $this->assertEquals($validInvoiceAddress->getPhone(), $invoiceOrderAddress->getPhone(), 'invoice address phone does not match');
     $this->assertEquals($validInvoiceAddress->getCountryId(), $invoiceOrderAddress->getCountryId(), 'invoice address country does not match');
     /* check currency */
     $this->assertEquals($this->cart->getCurrencyId(), $placedOrder->getCurrencyId(), 'currency id does not  match');
     $this->assertEquals($this->cart->getCurrency()->getRate(), $placedOrder->getCurrencyRate(), 'currency rate does not  match');
     /* check delivery module */
     $this->assertEquals(20, $placedOrder->getPostage(), 'postage does not  match');
     $this->assertEquals($deliveryModule->getId(), $placedOrder->getDeliveryModuleId(), 'delivery module does not  match');
     /* check payment module */
     $this->assertEquals($paymentModule->getId(), $placedOrder->getPaymentModuleId(), 'payment module does not  match');
     /* check status */
     $this->assertEquals(OrderStatus::CODE_NOT_PAID, $placedOrder->getOrderStatus()->getCode(), 'status does not  match');
     /* check lang */
     $this->assertEquals($this->request->getSession()->getLang()->getId(), $placedOrder->getLangId(), 'lang does not  match');
     /* check ordered product */
     foreach ($this->cartItems as $index => $cartItem) {
         $orderProduct = OrderProductQuery::create()->filterByOrderId($placedOrder->getId())->filterByProductRef($cartItem->getProduct()->getRef())->filterByProductSaleElementsRef($cartItem->getProductSaleElements()->getRef())->filterByQuantity($cartItem->getQuantity())->filterByPrice($cartItem->getPrice(), Criteria::LIKE)->filterByPromoPrice($cartItem->getPromoPrice(), Criteria::LIKE)->filterByWasNew($cartItem->getProductSaleElements()->getNewness())->filterByWasInPromo($cartItem->getPromo())->filterByWeight($cartItem->getProductSaleElements()->getWeight())->findOne();
         $this->assertNotNull($orderProduct);
         /* check attribute combinations */
         $this->assertEquals($cartItem->getProductSaleElements()->getAttributeCombinations()->count(), $orderProduct->getOrderProductAttributeCombinations()->count());
         if ($orderProduct->getVirtual()) {
             /* check same stock*/
             $this->assertEquals($itemsStock[$index], $cartItem->getProductSaleElements()->getQuantity());
         } else {
             /* check stock decrease */
             $this->assertEquals($itemsStock[$index] - $orderProduct->getQuantity(), $cartItem->getProductSaleElements()->getQuantity());
         }
         /* check tax */
         $orderProductTaxList = $orderProduct->getOrderProductTaxes();
         foreach ($cartItem->getProduct()->getTaxRule()->getTaxDetail($cartItem->getProduct(), $validDeliveryAddress->getCountry(), $cartItem->getPrice(), $cartItem->getPromoPrice()) as $index => $tax) {
             $orderProductTax = $orderProductTaxList[$index];
             $this->assertEquals($tax->getAmount(), $orderProductTax->getAmount());
             $this->assertEquals($tax->getPromoAmount(), $orderProductTax->getPromoAmount());
         }
     }
     return $placedOrder;
 }
 /**
  * Retrieve the delivery country for a customer
  *
  * The rules :
  *  - the country of the delivery address of the customer related to the
  *      cart if it exists
  *  - the country saved in cookie if customer have changed
  *      the default country
  *  - the default country for the shop if it exists
  *
  *
  * @param  \Thelia\Model\Customer $customer
  * @return \Thelia\Model\Country
  */
 protected function getDeliveryInformation(Customer $customer = null)
 {
     $address = null;
     // get the selected delivery address
     if (null !== ($addressId = $this->getCurrentRequest()->getSession()->getOrder()->getChoosenDeliveryAddress())) {
         if (null !== ($address = AddressQuery::create()->findPk($addressId))) {
             $this->isCustomizable = false;
             return [$address, $address->getCountry(), null];
         }
     }
     // get country from customer addresses
     if (null !== $customer) {
         $address = AddressQuery::create()->filterByCustomerId($customer->getId())->filterByIsDefault(1)->findOne();
         if (null !== $address) {
             $this->isCustomizable = false;
             return [$address, $address->getCountry(), null];
         }
     }
     // get country from cookie
     $cookieName = ConfigQuery::read('front_cart_country_cookie_name', 'fcccn');
     if ($this->getCurrentRequest()->cookies->has($cookieName)) {
         $cookieVal = $this->getCurrentRequest()->cookies->getInt($cookieName, 0);
         if (0 !== $cookieVal) {
             $country = CountryQuery::create()->findPk($cookieVal);
             if (null !== $country) {
                 return [null, $country, null];
             }
         }
     }
     // get default country for store.
     try {
         $country = Country::getDefaultCountry();
         return [null, $country, null];
     } catch (\LogicException $e) {
     }
     return [null, null, null];
 }
Exemple #29
0
 /**
  * Retrieve the delivery country for a customer
  *
  * The rules :
  *  - the country of the delivery address of the customer related to the
  *      cart if it exists
  *  - the country saved in cookie if customer have changed
  *      the default country
  *  - the default country for the shop if it exists
  *
  *
  * @param  \Thelia\Model\Customer $customer
  * @return \Thelia\Model\Country
  */
 protected function getDeliveryCountry(Customer $customer = null)
 {
     // get country from customer addresses
     if (null !== $customer) {
         $address = AddressQuery::create()->filterByCustomerId($customer->getId())->filterByIsDefault(1)->findOne();
         if (null !== $address) {
             $this->isCustomizable = false;
             return $address->getCountry();
         }
     }
     // get country from cookie
     $cookieName = ConfigQuery::read('front_cart_country_cookie_name', 'fcccn');
     if ($this->request->cookies->has($cookieName)) {
         $cookieVal = $this->request->cookies->getInt($cookieName, 0);
         if (0 !== $cookieVal) {
             $country = CountryQuery::create()->findPk($cookieVal);
             if (null !== $country) {
                 return $country;
             }
         }
     }
     // get default country for store.
     try {
         $country = Country::getDefaultCountry();
         return $country;
     } catch (\LogicException $e) {
     }
     return null;
 }
 public function makeAddressDefaultAction($addressId)
 {
     $this->checkAuth();
     $address = AddressQuery::create()->filterByCustomer($this->getSecurityContext()->getCustomerUser())->findPk($addressId);
     if (null === $address) {
         $this->pageNotFound();
     }
     try {
         $event = new AddressEvent($address);
         $this->dispatch(TheliaEvents::ADDRESS_DEFAULT, $event);
     } catch (\Exception $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
         return $this->render("account");
     }
     return $this->generateRedirectFromRoute('default', array('view' => 'account'));
 }