Example #1
0
 public function testCreatedAddress()
 {
     $customer = CustomerQuery::create()->findOne();
     $AddressCreateOrUpdateEvent = new AddressCreateOrUpdateEvent("test address", 1, "Thelia", "Thelia", "5 rue rochon", "", "", "63000", "clermont-ferrand", 64, "0102030405", "", "");
     $AddressCreateOrUpdateEvent->setCustomer($customer);
     $AddressCreateOrUpdateEvent->setDispatcher($this->getMock("Symfony\\Component\\EventDispatcher\\EventDispatcherInterface"));
     $actionAddress = new Address();
     $actionAddress->create($AddressCreateOrUpdateEvent);
     $createdAddress = $AddressCreateOrUpdateEvent->getAddress();
     $this->assertInstanceOf("Thelia\\Model\\Address", $createdAddress);
     $this->assertFalse($createdAddress->isNew());
     $this->assertSame($customer, $createdAddress->getCustomer());
     $this->assertEquals($AddressCreateOrUpdateEvent->getLabel(), $createdAddress->getLabel());
     $this->assertEquals($AddressCreateOrUpdateEvent->getTitle(), $createdAddress->getTitleId());
     $this->assertEquals($AddressCreateOrUpdateEvent->getFirstname(), $createdAddress->getFirstname());
     $this->assertEquals($AddressCreateOrUpdateEvent->getLastname(), $createdAddress->getLastname());
     $this->assertEquals($AddressCreateOrUpdateEvent->getAddress1(), $createdAddress->getAddress1());
     $this->assertEquals($AddressCreateOrUpdateEvent->getAddress2(), $createdAddress->getAddress2());
     $this->assertEquals($AddressCreateOrUpdateEvent->getAddress3(), $createdAddress->getAddress3());
     $this->assertEquals($AddressCreateOrUpdateEvent->getZipcode(), $createdAddress->getZipcode());
     $this->assertEquals($AddressCreateOrUpdateEvent->getCity(), $createdAddress->getCity());
     $this->assertEquals($AddressCreateOrUpdateEvent->getCountry(), $createdAddress->getCountryId());
     $this->assertEquals($AddressCreateOrUpdateEvent->getPhone(), $createdAddress->getPhone());
     $this->assertEquals($AddressCreateOrUpdateEvent->getCellphone(), $createdAddress->getCellphone());
     $this->assertEquals($AddressCreateOrUpdateEvent->getCompany(), $createdAddress->getCompany());
 }
Example #2
0
 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());
     }
 }
Example #3
0
 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);
 }