/** * @param OrderEvent $event * @throws \Thelia\Form\Exception\FormValidationException */ public function cellphoneCheck(OrderEvent $event) { if (Predict::getModuleId() === $event->getDeliveryModule()) { $cellphone = $this->request->request->get("predict_cellphone"); $cellphone = str_replace(array(' ', '.', '-', ',', ';', '/', '\\', '(', ')'), '', $cellphone); $partial_number = ""; if (empty($cellphone) || !preg_match('#^[0|\\+33][6-7]([0-9]{8})$#', $cellphone, $partial_number)) { throw new FormValidationException(Translator::getInstance()->trans("You must give a cellphone number in order to use Predict services", [], Predict::MESSAGE_DOMAIN)); } $cellphone = str_replace("+33", "0", $cellphone); $banned_cellphones = array('00000000', '11111111', '22222222', '33333333', '44444444', '55555555', '66666666', '77777777', '88888888', '99999999', '12345678', '23456789', '98765432'); if (in_array($partial_number[1], $banned_cellphones)) { throw new FormValidationException(Translator::getInstance()->trans("This phone number is not valid", [], Predict::MESSAGE_DOMAIN)); } /** @var \Thelia\Model\Customer $customer */ $customer = $this->getRequest()->getSession()->getCustomerUser(); $address = $customer->getDefaultAddress(); $addressEvent = new AddressCreateOrUpdateEvent($address->getLabel(), $address->getTitleId(), $address->getFirstname(), $address->getLastname(), $address->getAddress1(), $address->getAddress2(), $address->getAddress3(), $address->getZipcode(), $address->getCity(), $address->getCountryId(), $cellphone, $address->getPhone(), $address->getCompany()); $addressEvent->setAddress($address); $dispatcher = $event->getDispatcher(); $dispatcher->dispatch(TheliaEvents::ADDRESS_UPDATE, $addressEvent); } }
protected function createOrUpdate(AddressModel $addressModel, AddressCreateOrUpdateEvent $event) { $addressModel->setDispatcher($event->getDispatcher()); $con = Propel::getWriteConnection(AddressTableMap::DATABASE_NAME); $con->beginTransaction(); try { $addressModel->setLabel($event->getLabel())->setTitleId($event->getTitle())->setFirstname($event->getFirstname())->setLastname($event->getLastname())->setAddress1($event->getAddress1())->setAddress2($event->getAddress2())->setAddress3($event->getAddress3())->setZipcode($event->getZipcode())->setCity($event->getCity())->setCountryId($event->getCountry())->setStateId($event->getState())->setCellphone($event->getCellphone())->setPhone($event->getPhone())->setCompany($event->getCompany())->save(); if ($event->getIsDefault() && !$addressModel->getIsDefault()) { $addressModel->makeItDefault(); } $event->setAddress($addressModel); $con->commit(); } catch (PropelException $e) { $con->rollback(); throw $e; } }
/** * 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(); }
public function processGetOrders(ApiCallEvent $event) { $api = $event->getApi(); $response = $api->getResponse(); if ($response->isInError()) { $this->logger->error($response->getFormattedError()); throw new BadResponseException($response->getFormattedError()); } $dispatcher = $event->getDispatcher(); $orders = $response->getGroup("Orders"); $validOrders = []; /** @var \Thelia\Model\Country $country */ $shopCountry = CountryQuery::create()->findOneByShopCountry(true); $calculator = new Calculator(); /** * Check if there is only one order: reformat the array in that case */ if (array_key_exists("IdOrder", $orders["Order"])) { $orders = array("Order" => [$orders["Order"]]); } $notImportedOrders = []; /** * Then treat the orders */ foreach ($orders["Order"] as $orderArray) { /** * I) create the addresses */ /** * Get delivery address, and format empty fields */ $deliveryAddressArray =& $orderArray["ShippingAddress"]; $deliveryCountryId = CountryQuery::create()->findOneByIsoalpha2(strtolower($deliveryAddressArray["Country"]))->getId(); foreach ($deliveryAddressArray as &$value) { if (is_array($value)) { $value = ""; } } /** * Same for invoice address */ $invoiceAddressArray =& $orderArray["BillingAddress"]; $invoiceCountry = CountryQuery::create()->findOneByIsoalpha2(strtolower($invoiceAddressArray["Country"])); $invoiceCountryId = $invoiceCountry->getId(); foreach ($invoiceAddressArray as &$value) { if (is_array($value)) { $value = ""; } } $title = CustomerTitleQuery::create()->findOne()->getId(); /** * Create the order addresses */ $deliveryAddressEvent = new AddressCreateOrUpdateEvent("Delivery address", $title, $deliveryAddressArray["FirstName"], $deliveryAddressArray["LastName"], $deliveryAddressArray["Street"], $deliveryAddressArray["Street1"], $deliveryAddressArray["Street2"], $deliveryAddressArray["PostalCode"], $deliveryAddressArray["Town"], $deliveryCountryId, $deliveryAddressArray["PhoneMobile"], $deliveryAddressArray["Phone"], $deliveryAddressArray["Company"]); $deliveryAddressEvent->setCustomer($this->shoppingFluxCustomer); $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $deliveryAddressEvent); $invoiceAddressEvent = new AddressCreateOrUpdateEvent("Invoice address", $title, $invoiceAddressArray["FirstName"], $invoiceAddressArray["LastName"], $invoiceAddressArray["Street"], $invoiceAddressArray["Street1"], $invoiceAddressArray["Street2"], $invoiceAddressArray["PostalCode"], $invoiceAddressArray["Town"], $deliveryCountryId, $invoiceAddressArray["PhoneMobile"], $invoiceAddressArray["Phone"], $invoiceAddressArray["Company"]); $invoiceAddressEvent->setCustomer($this->shoppingFluxCustomer); $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $invoiceAddressEvent); /** * II) Add the products to a cart */ /** * Format the products array */ if ($orderArray["NumberOfProducts"] == "1") { $orderArray["Products"] = array("Product" => [$orderArray["Products"]["Product"]]); } $productsArray =& $orderArray["Products"]["Product"]; /** * Create a fake cart */ $cart = new Cart(); /** * And fulfil it with the products */ foreach ($productsArray as &$productArray) { $ids = explode("_", $productArray["SKU"]); $cartPse = ProductSaleElementsQuery::create()->findPk($ids[1]); $calculator->load($cartPse->getProduct(), $shopCountry); $price = $calculator->getUntaxedPrice((double) $productArray["Price"]); $cart->addCartItem((new CartItem())->setProductSaleElements($cartPse)->setProduct($cartPse->getProduct())->setQuantity($productArray["Quantity"])->setPrice($price)->setPromoPrice(0)->setPromo(0)); } /** * III) Create/Save the order */ /** * Construct order model */ $lang = LangQuery::create()->findOneByLocale($invoiceCountry->getLocale()); $currency = CurrencyQuery::create()->findOneByCode("EUR"); $order = OrderQuery::create()->findOneByRef($orderArray["IdOrder"]); if ($order !== null) { $order->delete(); } $order = new Order(); $order->setPostage($orderArray["TotalShipping"])->setChoosenDeliveryAddress($deliveryAddressEvent->getAddress()->getId())->setChoosenInvoiceAddress($invoiceAddressEvent->getAddress()->getId())->setDeliveryModuleId(ShoppingFluxConfigQuery::getDeliveryModuleId())->setPaymentModuleId($this->shoppingFluxPaymentModuleId)->setTransactionRef($orderArray["Marketplace"]); /** * Construct event */ $orderEvent = new OrderManualEvent($order, $currency, $lang, $cart, $this->shoppingFluxCustomer); $orderEvent->setDispatcher($dispatcher); $dispatcher->dispatch(TheliaEvents::ORDER_CREATE_MANUAL, $orderEvent); $placedOrder = $orderEvent->getPlacedOrder(); $placedOrder->setRef($orderArray["IdOrder"])->setPaid(); $validOrders[] = ["IdOrder" => $placedOrder->getRef(), "Marketplace" => $placedOrder->getTransactionRef()]; } /** * IV) Valid the orders to Shopping Flux */ $request = new Request("ValidOrders"); foreach ($validOrders as $validOrder) { $request->addOrder($validOrder); } $validOrdersApi = new ValidOrders($response->getToken(), $response->getMode()); $validOrdersApi->setRequest($request); $validOrdersResponse = $validOrdersApi->getResponse(); if ($validOrdersResponse->isInError()) { $this->logger->error($response->getFormattedError()); } }
public function import($startRecord = 0) { $count = 0; $errors = 0; $hdl = $this->t1db->query(sprintf("select * from client order by id asc limit %d, %d", intval($startRecord), $this->getChunkSize())); while ($hdl && ($client = $this->t1db->fetch_object($hdl))) { $count++; try { $this->cust_corresp->getT2($client->id); Tlog::getInstance()->warning("Customer ID={$client->id} ref={$client->ref} already imported."); continue; } catch (ImportException $ex) { // Okay, the order was not imported. } try { $title = $this->getT2CustomerTitle($client->raison); $lang = $this->getT2Lang($client->lang); $country = $this->getT2Country($client->pays); try { $sponsor = $this->cust_corresp->getT2($client->parrain); } catch (ImportException $ex) { $sponsor = ''; } $event = new CustomerCreateOrUpdateEvent($title->getId(), $client->prenom, $client->nom, $client->adresse1, $client->adresse2, $client->adresse3, $client->telfixe, $client->telport, $client->cpostal, $client->ville, $country->getId(), $client->email, $client->email, $lang->getId(), $client->type == 1 ? true : false, $sponsor, $client->pourcentage, $client->entreprise, $client->ref); $this->dispatcher->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $event); Tlog::getInstance()->info("Created customer " . $event->getCustomer()->getId() . " from {$client->ref} ({$client->id})"); if (empty($client->email)) { $client->email = 'empty-mail' . $event->getCustomer()->getId() . '@fake-email.com'; } $customerTemp = new CustomerTemp(); $customerTemp->setEmail($client->email)->setPassword($client->motdepasse)->save(); // Import customer addresses $a_hdl = $this->t1db->query("select * from adresse where client=?", array($client->id)); while ($a_hdl && ($adresse = $this->t1db->fetch_object($a_hdl))) { try { $title = $this->getT2CustomerTitle($adresse->raison); $country = $this->getT2Country($adresse->pays); $adr_event = new AddressCreateOrUpdateEvent($adresse->libelle, $title->getId(), $adresse->prenom, $adresse->nom, $adresse->adresse1, $adresse->adresse2, $adresse->adresse3, $adresse->cpostal, $adresse->ville, $country->getId(), '', $adresse->tel, isset($adresse->entreprise) ? $adresse->entreprise : ''); $adr_event->setCustomer($event->getCustomer()); $this->dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $adr_event); Tlog::getInstance()->info("Created address " . $adr_event->getAddress()->getId() . " for customer {$client->ref} ({$client->id})"); } catch (ImportException $ex) { Tlog::getInstance()->addError("Failed to create address ID {$adresse->id} for customer ref {$client->ref}:", $ex->getMessage()); } } $this->cust_corresp->addEntry($client->id, $event->getCustomer()->getId()); } catch (\Exception $ex) { Tlog::getInstance()->addError("Failed to import customer ref {$client->ref} :", $ex->getMessage()); $errors++; } } return new ImportChunkResult($count, $errors); }