public function testProcess() { $customer = new Customer(); $customer->setOriginId(1); $transport = $this->getMockBuilder('OroCRM\\Bundle\\MagentoBundle\\Entity\\MagentoSoapTransport')->disableOriginalConstructor()->getMock(); $transport->expects($this->once())->method('getIsExtensionInstalled')->will($this->returnValue(true)); $channel = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Entity\\Channel')->disableOriginalConstructor()->getMock(); $channel->expects($this->once())->method('getTransport')->will($this->returnValue($transport)); $order = new Order(); $cart = new Cart(); $cart->setOriginId(1); $order->setCustomer($customer); $order->setChannel($channel); $order->setCart($cart); $this->databaseHelper->expects($this->once())->method('findOneByIdentity')->with($channel)->will($this->returnValue($channel)); $strategy = $this->getStrategy(); $execution = $this->getMock('Akeneo\\Bundle\\BatchBundle\\Item\\ExecutionContext'); $this->jobExecution->expects($this->any())->method('getExecutionContext')->will($this->returnValue($execution)); $strategy->setStepExecution($this->stepExecution); $orderItemDate = ['customerId' => uniqid()]; /** @var \PHPUnit_Framework_MockObject_MockObject|ContextInterface $context */ $context = $this->getMock('Oro\\Bundle\\ImportExportBundle\\Context\\ContextInterface'); $context->expects($this->once())->method('getValue')->will($this->returnValue($orderItemDate)); $strategy->setImportExportContext($context); $execution->expects($this->exactly(3))->method('get')->with($this->isType('string')); $execution->expects($this->exactly(3))->method('put')->with($this->isType('string'), $this->isType('array')); $this->assertNull($strategy->process($order)); }
/** * @dataProvider isCompletedDataProvider * @param $status * @param $result */ public function testIsCompleted($status, $result) { $this->entity->setStatus($status); if ($result) { $this->assertTrue($this->entity->isCompleted()); } else { $this->assertFalse($this->entity->isCompleted()); } }
/** * @param Order|object $order * @return bool */ protected function isOrderValid($order) { if (!$order instanceof Order) { return false; } $customer = $order->getCustomer(); if (!$customer || !$customer instanceof Customer) { return false; } return true; }
/** * BC layer to find existing collection items by old identity filed values * * {@inheritdoc} */ protected function findExistingEntity($entity, array $searchContext = []) { $existingEntity = parent::findExistingEntity($entity, $searchContext); if (!$existingEntity && $entity instanceof OrderAddress) { /** @var OrderAddress $existingEntity */ $existingEntity = $this->existingEntity->getAddresses()->filter(function (OrderAddress $address) use($entity) { $isMatched = true; $fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; foreach ($fieldsToMatch as $fieldToMatch) { $addressValue = $this->getPropertyAccessor()->getValue($address, $fieldToMatch); $entityValue = $this->getPropertyAccessor()->getValue($entity, $fieldToMatch); $isMatched = $isMatched && $addressValue === $entityValue; } return $isMatched; })->first(); if ($existingEntity && $entity->getOriginId()) { $existingEntity->setOriginId($entity->getOriginId()); } } if ($entity instanceof OrderItem && is_null($entity->getName())) { //name can't be null, so to avoid import job failing empty string is used $entity->setName(''); } return $existingEntity; }
public function testCustomerSyncAction() { $newCustomerOrder = $this->getModifiedCustomerOrder($this->customer); $orderIterator = new StubIterator([$newCustomerOrder]); $this->soapTransport->expects($this->any())->method('call'); /*$this->soapTransport->expects($this->once())->method('getCustomer')->will( $this->returnValue($customerIterator) );*/ $this->soapTransport->expects($this->once())->method('getOrders')->will($this->returnValue($orderIterator)); $this->client->request('GET', $this->getUrl('orocrm_magento_orderplace_new_customer_order_sync', ['id' => $this->customer->getId()]), [], [], ['HTTP_X-Requested-With' => 'XMLHttpRequest']); $result = $this->client->getResponse(); $this->assertJsonResponseStatusCodeEquals($result, 200); $arrayJson = json_decode($result->getContent(), 1); $this->assertEquals($arrayJson['statusType'], 'success'); $this->assertEquals($arrayJson['message'], 'Data successfully synchronized.'); $this->assertEquals($arrayJson['url'], $this->getUrl('orocrm_magento_order_view', ['id' => $this->order->getId()])); $this->client->request('GET', $this->getUrl('orocrm_magento_customer_view', ['id' => $this->customer->getId()])); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $this->assertContains('General Information', $result->getContent()); $this->assertContains('100000307', $result->getContent()); $this->assertContains('$750', $result->getContent()); $this->assertContains('pending', $result->getContent()); $this->assertContains('$755', $result->getContent()); $this->assertContains('$755', $result->getContent()); }
/** * @param Order $order * @return bool */ protected function isProcessingAllowed(Order $order) { $isProcessingAllowed = true; $customer = $this->findExistingEntity($order->getCustomer()); $customerOriginId = $order->getCustomer()->getOriginId(); if (!$customer && $customerOriginId) { $this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $customerOriginId); $isProcessingAllowed = false; } // Do not try to load cart if bridge does not installed /** @var MagentoSoapTransport $transport */ $channel = $this->databaseHelper->findOneByIdentity($order->getChannel()); $transport = $channel->getTransport(); if ($transport->getIsExtensionInstalled()) { $cart = $this->findExistingEntity($order->getCart()); $cartOriginId = $order->getCart()->getOriginId(); if (!$cart && $cartOriginId) { $this->appendDataToContext(ContextCartReader::CONTEXT_POST_PROCESS_CARTS, $cartOriginId); $isProcessingAllowed = false; } } if (!$customer && $order->getIsGuest() && $transport->getGuestCustomerSync()) { $this->appendDataToContext('postProcessGuestCustomers', $this->context->getValue('itemData')); $isProcessingAllowed = false; } return $isProcessingAllowed; }
/** * @return array */ public function itemsProvider() { $order1 = new Order(); $order2 = new Order(); $order1->setIncrementId('1111'); $order2->setIncrementId('2222'); $order3 = clone $order1; $cart1 = new Cart(); $cart2 = new Cart(); $cart1->setOriginId(1111); $cart2->setOriginId(2222); $cart3 = clone $cart1; $customer1 = new Customer(); $customer1->setOriginId(111); $customer2 = clone $customer1; $someEntity = new \stdClass(); $someEntity2 = new \stdClass(); return ['should skip non-unique orders' => ['$items' => [$order1, $order2, $order3], '$expectedItems' => [$order3->getIncrementId() => $order3, $order2->getIncrementId() => $order2]], 'should skip non-unique carts' => ['$items' => [$cart1, $cart2, $cart3], '$expectedItems' => [$cart3->getOriginId() => $cart3, $cart2->getOriginId() => $cart2]], 'should skip non-unique customers' => ['$items' => [$customer1, $customer2], '$expectedItems' => [$customer2->getOriginId() => $customer2]], 'should not break logic with entities that not consist originId' => ['$items' => [$someEntity, $someEntity2], '$expectedItems' => [$someEntity, $someEntity2]]]; }
/** * {@inheritdoc} */ public function load(ObjectManager $manager) { $userManager = $this->container->get('oro_user.manager'); $admin = $userManager->findUserByEmail(LoadAdminUserData::DEFAULT_ADMIN_EMAIL); $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); foreach ($this->orderData as $data) { $entity = new Order(); $entity->setOwner($admin); $entity->setOrganization($organization); $created = new \DateTime('now', new \DateTimeZone('UTC')); $entity->setCreatedAt($created->sub(new \DateInterval($data['createdSub']))); $updated = new \DateTime('now', new \DateTimeZone('UTC')); $entity->setUpdatedAt($updated->sub(new \DateInterval($data['updatedSub']))); $data['channel'] = $this->getReference('integration'); $data['dataChannel'] = $this->getReference('default_channel'); $data['cart'] = $this->getReference('cart'); $data['store'] = $this->getReference('store'); $data['customer'] = $this->getReference('customer'); $this->setEntityPropertyValues($entity, $data, ['reference', 'createdSub', 'updatedSub']); $this->setReference($data['reference'], $entity); $manager->persist($entity); } $manager->remove($this->getReference('order')); $manager->flush(); }
/** * "Success" form handler * * @param Order $entity */ protected function onSuccess(Order $entity) { if (null === $entity->getOrganization()) { $entity->setOrganization($this->organization); } /** @var OrderAddress $address */ foreach ($entity->getAddresses() as $address) { if (null === $address->getOwner()) { $address->setOwner($entity); } if (null === $address->getOrganization()) { $address->setOrganization($this->organization); } } /** @var OrderItem $item */ foreach ($entity->getItems() as $item) { if (null === $item->getOrder()) { $item->setOrder($entity); } } $this->manager->persist($entity); $this->manager->flush(); }
/** * @param array $data * * @dataProvider dataProvider */ public function testRead(array $data) { $this->executionContext->expects($this->once())->method('get')->will($this->returnCallback(function ($key) use($data) { if (empty($data[$key])) { return null; } return $data[$key]; })); $originId = 321; $expectedData = new Order(); $expectedData->setIncrementId($originId); $this->context->expects($this->any())->method('getConfiguration')->will($this->returnValue(['data' => $expectedData])); $this->transport->expects($this->once())->method('getOrderInfo')->will($this->returnCallback(function ($incrementId) { $object = new \stdClass(); $object->origin_id = $incrementId; $object->store_id = 0; return $object; })); $reader = $this->getReader(); $reader->setStepExecution($this->stepExecutionMock); $this->assertEquals(['origin_id' => $originId, 'store_id' => 0], $reader->read()); $this->assertNull($reader->read()); }
/** * BC layer to find existing collection items by old identity filed values * * {@inheritdoc} */ protected function findExistingEntity($entity, array $searchContext = []) { $existingEntity = parent::findExistingEntity($entity, $searchContext); if (!$existingEntity && $entity instanceof OrderAddress) { $propertyAccessor = PropertyAccess::createPropertyAccessor(); /** @var OrderAddress $existingEntity */ $existingEntity = $this->existingEntity->getAddresses()->filter(function (OrderAddress $address) use($entity, $propertyAccessor) { $isMatched = true; $fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; foreach ($fieldsToMatch as $fieldToMatch) { $addressValue = $propertyAccessor->getValue($address, $fieldToMatch); $entityValue = $propertyAccessor->getValue($entity, $fieldToMatch); $isMatched = $isMatched && $addressValue === $entityValue; } return $isMatched; })->first(); if ($existingEntity && $entity->getOriginId()) { $existingEntity->setOriginId($entity->getOriginId()); } } return $existingEntity; }
/** * @param Order $entityToUpdate * @param Order $entityToImport */ protected function processItems(Order $entityToUpdate, Order $entityToImport) { $importedOriginIds = $entityToImport->getItems()->map(function (OrderItem $item) { return $item->getOriginId(); })->toArray(); // insert new and update existing items /** @var OrderItem $item - imported order item */ foreach ($entityToImport->getItems() as $item) { $originId = $item->getOriginId(); $existingItem = $entityToUpdate->getItems()->filter(function (OrderItem $item) use($originId) { return $item->getOriginId() == $originId; })->first(); if ($existingItem) { $this->strategyHelper->importEntity($existingItem, $item, ['id', 'order']); $item = $existingItem; } if (!$item->getOrder()) { $item->setOrder($entityToUpdate); } if (!$entityToUpdate->getItems()->contains($item)) { $entityToUpdate->getItems()->add($item); } } // delete order items that not exists in remote order $deleted = $entityToUpdate->getItems()->filter(function (OrderItem $item) use($importedOriginIds) { return !in_array($item->getOriginId(), $importedOriginIds); }); foreach ($deleted as $item) { $entityToUpdate->getItems()->remove($item); } }
/** * @param Cart $cart * @param Customer $customer * * @return Order */ protected function createOrder(Cart $cart, Customer $customer) { $order = new Order(); $order->setChannel($this->integration); $order->setDataChannel($this->channel); $order->setStatus('open'); $order->setIncrementId('one'); $order->setCreatedAt(new \DateTime('now')); $order->setUpdatedAt(new \DateTime('now')); $order->setCart($cart); $order->setStore($this->store); $order->setCustomer($customer); $order->setCustomerEmail('*****@*****.**'); $order->setDiscountAmount(34.4); $order->setTaxAmount(12.47); $order->setShippingAmount(5); $order->setTotalPaidAmount(17.85); $order->setTotalInvoicedAmount(11); $order->setTotalRefundedAmount(4); $order->setTotalCanceledAmount(0); $order->setShippingMethod('some unique shipping method'); $order->setRemoteIp('unique ip'); $order->setGiftMessage('some very unique gift message'); $order->setOwner($this->getUser()); $order->setOrganization($this->organization); $this->em->persist($order); return $order; }
/** * @param EntityManager $entityManager * @param Order $order */ protected function updateCustomerLifetime(EntityManager $entityManager, Order $order) { /** @var CustomerRepository $customerRepository */ $customerRepository = $entityManager->getRepository('OroCRMMagentoBundle:Customer'); $subtotalAmount = $order->getSubtotalAmount(); if ($subtotalAmount) { $discountAmount = $order->getDiscountAmount(); $lifetimeValue = $discountAmount ? $subtotalAmount - abs($discountAmount) : $subtotalAmount; // if order status changed to canceled we should remove order lifetime value from customer lifetime if ($order->isCanceled()) { $lifetimeValue *= -1; } $customer = $order->getCustomer(); $customerRepository->updateCustomerLifetimeValue($customer, $lifetimeValue); // schedule lifetime history update if ($customer->getAccount()) { $this->channelDoctrineListener->scheduleEntityUpdate($customer, $customer->getAccount(), $customer->getDataChannel()); } } }
/** * @Route("/actualize/{id}", name="orocrm_magento_order_actualize", requirements={"id"="\d+"})) * @AclAncestor("orocrm_magento_order_view") * @param Order $order * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function actualizeAction(Order $order) { $result = false; try { $result = $this->loadOrderInformation($order->getChannel(), ['filters' => ['increment_id' => $order->getIncrementId()]]); } catch (\LogicException $e) { $this->get('logger')->addCritical($e->getMessage(), ['exception' => $e]); } if ($result === true) { $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('orocrm.magento.controller.synchronization_success')); } else { $this->get('session')->getFlashBag()->add('error', $this->get('translator')->trans('orocrm.magento.controller.synchronization_error')); } return $this->redirect($this->generateUrl('orocrm_magento_order_view', ['id' => $order->getId()])); }
public function testNotScheduleLifetimeValueHistoryWithoutAccount() { $expectedLifetime = 200; $order = new Order(); $customer = new Customer(); $order->setCustomer($customer)->setSubtotalAmount($expectedLifetime); $entityManager = $this->createEntityManagerMock(); $this->customerRepository->expects($this->once())->method('updateCustomerLifetimeValue')->with($this->isInstanceOf('OroCRM\\Bundle\\MagentoBundle\\Entity\\Customer'), $expectedLifetime); $this->listener->expects($this->never())->method('scheduleEntityUpdate'); $listener = new OrderListener($this->listener); $listener->prePersist(new LifecycleEventArgs($order, $entityManager)); }
protected function getMainEntityId() { return self::$order->getid(); }
/** * @Route("/actualize/{id}", name="orocrm_magento_order_actualize", requirements={"id"="\d+"})) * @AclAncestor("orocrm_magento_order_view") */ public function actualizeAction(Order $order) { try { $processor = $this->get('oro_integration.sync.processor'); $processor->process($order->getChannel(), 'order', ['filters' => ['increment_id' => $order->getIncrementId()]]); $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('orocrm.magento.controller.synchronization_success')); } catch (\LogicException $e) { $this->get('logger')->addCritical($e->getMessage(), ['exception' => $e]); $this->get('session')->getFlashBag()->add('error', $this->get('translator')->trans('orocrm.magento.controller.synchronization_error')); } return $this->redirect($this->generateUrl('orocrm_magento_order_view', ['id' => $order->getId()])); }
/** * @param ObjectManager $om * @param Store $store * @param Integration $integration * @param Customer $customer * @param string $status * @param Cart $cart * @param string $paymentMethod * @param string $paymentMethodDetails * @param mixed $origin * * @return Order */ protected function generateOrder(ObjectManager $om, Store $store, Integration $integration, Customer $customer, $status, Cart $cart, $paymentMethod, $paymentMethodDetails, $origin) { $order = new Order(); $order->setOrganization($this->organization); $order->setChannel($integration); $order->setCustomer($customer); $order->setOwner($customer->getOwner()); $order->setStatus($status); $order->setStore($store); $order->setStoreName($store->getName()); $order->setIsGuest(0); $order->setIncrementId((string) $origin); $order->setCreatedAt(new \DateTime('now')); $order->setUpdatedAt(new \DateTime('now')); $order->setCart($cart); $order->setCurrency($cart->getBaseCurrencyCode()); $order->setTotalAmount($cart->getGrandTotal()); $order->setTotalInvoicedAmount($cart->getGrandTotal()); $order->setDataChannel($this->dataChannel); if ($status == 'Completed') { $order->setTotalPaidAmount($cart->getGrandTotal()); } $order->setSubtotalAmount($cart->getSubTotal()); $order->setShippingAmount(rand(5, 10)); $order->setPaymentMethod($paymentMethod); $order->setPaymentDetails($paymentMethodDetails); $order->setShippingMethod('flatrate_flatrate'); $address = $this->getOrderAddress($om); $order->addAddress($address); $address->setOwner($order); $om->persist($order); return $order; }
/** * @param Customer|object|null $customer * @param float $subtotal * @return Order */ protected function createOrder($customer = null, $subtotal = 10.1) { $order = new Order(); $order->setId(1); if ($customer) { $order->setCustomer($customer); } $order->setSubtotalAmount($subtotal); return $order; }