PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/OroCRM/Bundle/MagentoBundle/Tests/Functional/Fixture/LoadMagentoChannel.php

https://github.com/jrcollado1987/crm
PHP | 484 lines | 317 code | 71 blank | 96 comment | 0 complexity | 648adc34e25f3c21a4e74b6982003b7b MD5 | raw file
  1. <?php
  2. namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\DataFixtures\AbstractFixture;
  5. use Doctrine\Common\Persistence\ObjectManager;
  6. use Oro\Bundle\UserBundle\Entity\User;
  7. use Oro\Bundle\AddressBundle\Entity\Address;
  8. use Oro\Bundle\IntegrationBundle\Entity\Channel;
  9. use Oro\Bundle\UserBundle\Model\Gender;
  10. use OroCRM\Bundle\MagentoBundle\Entity\Cart;
  11. use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
  12. use OroCRM\Bundle\MagentoBundle\Entity\CartAddress;
  13. use OroCRM\Bundle\MagentoBundle\Entity\Customer;
  14. use OroCRM\Bundle\MagentoBundle\Entity\Website;
  15. use OroCRM\Bundle\MagentoBundle\Entity\Store;
  16. use OroCRM\Bundle\AccountBundle\Entity\Account;
  17. use OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup;
  18. use OroCRM\Bundle\MagentoBundle\Entity\CartItem;
  19. use OroCRM\Bundle\MagentoBundle\Entity\Order;
  20. use OroCRM\Bundle\MagentoBundle\Entity\CartStatus;
  21. use OroCRM\Bundle\MagentoBundle\Entity\OrderItem;
  22. use OroCRM\Bundle\MagentoBundle\Entity\Address as MagentoAddress;
  23. class LoadMagentoChannel extends AbstractFixture
  24. {
  25. /** @var ObjectManager */
  26. private $em;
  27. /** @var Channel */
  28. private $channel;
  29. /** @var MagentoSoapTransport */
  30. private $transport;
  31. /** @var array */
  32. private $countries;
  33. /** @var array */
  34. private $regions;
  35. /** @var Website */
  36. private $website;
  37. /** @var Store */
  38. private $store;
  39. /** @var CustomerGroup */
  40. private $customerGroup;
  41. /**
  42. * @param ObjectManager $manager
  43. *
  44. * @return Channel
  45. */
  46. public function load(ObjectManager $manager)
  47. {
  48. $this->em = $manager;
  49. $this->countries = $this->loadStructure('OroAddressBundle:Country', 'getIso2Code');
  50. $this->regions = $this->loadStructure('OroAddressBundle:Region', 'getCombinedCode');
  51. $this->createTransport()
  52. ->createChannel()
  53. ->createWebSite()
  54. ->createCustomerGroup()
  55. ->createStore();
  56. $address1 = $this->createAddress($this->regions['US-AZ'], $this->countries['US']);
  57. $address2 = $this->createAddress($this->regions['US-AZ'], $this->countries['US']);
  58. $magentoAddress = $this->createMagentoAddress($this->regions['US-AZ'], $this->countries['US']);
  59. $account = $this->createAccount($address1, $address2);
  60. $customer = $this->createCustomer(1, $account, $magentoAddress);
  61. $cartAddress1 = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 1);
  62. $cartAddress2 = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 2);
  63. $cartItem = $this->createCartItem();
  64. $status = $this->getStatus();
  65. $items = new ArrayCollection();
  66. $items->add($cartItem);
  67. $cart = $this->createCart($cartAddress1, $cartAddress2, $customer, $items, $status);
  68. $this->updateCartItem($cartItem, $cart);
  69. $order = $this->createOrder($cart, $customer);
  70. $this->setReference('customer', $customer);
  71. $this->setReference('channel', $this->channel);
  72. $this->setReference('cart', $cart);
  73. $this->setReference('order', $order);
  74. $baseOrderItem = $this->createBaseOrderItem($order);
  75. $order->setItems([$baseOrderItem]);
  76. $this->em->persist($order);
  77. $this->em->flush();
  78. return $this->channel;
  79. }
  80. /**
  81. * @param $billing
  82. * @param $shipping
  83. * @param Customer $customer
  84. * @param ArrayCollection $item
  85. * @param CartStatus $status
  86. *
  87. * @return Cart
  88. */
  89. protected function createCart($billing, $shipping, Customer $customer, ArrayCollection $item, $status)
  90. {
  91. $cart = new Cart();
  92. $cart->setChannel($this->channel);
  93. $cart->setBillingAddress($billing);
  94. $cart->setShippingAddress($shipping);
  95. $cart->setCustomer($customer);
  96. $cart->setEmail('email@email.com');
  97. $cart->setCreatedAt(new \DateTime('now'));
  98. $cart->setUpdatedAt(new \DateTime('now'));
  99. $cart->setCartItems($item);
  100. $cart->setStatus($status);
  101. $cart->setItemsQty(0);
  102. $cart->setItemsCount(1);
  103. $cart->setBaseCurrencyCode('code');
  104. $cart->setStoreCurrencyCode('code');
  105. $cart->setQuoteCurrencyCode('usd');
  106. $cart->setStoreToBaseRate(12);
  107. $cart->setGrandTotal(2.54);
  108. $cart->setIsGuest(0);
  109. $cart->setStore($this->store);
  110. $cart->setOwner($this->getUser());
  111. $this->em->persist($cart);
  112. return $cart;
  113. }
  114. /**
  115. * @param $table
  116. * @param $method
  117. *
  118. * @return array
  119. */
  120. protected function loadStructure($table, $method)
  121. {
  122. $result = [];
  123. $response = $this->em->getRepository($table)->findAll();
  124. foreach ($response as $row) {
  125. $result[call_user_func([$row, $method])] = $row;
  126. }
  127. return $result;
  128. }
  129. /**
  130. * @return $this
  131. */
  132. protected function createChannel()
  133. {
  134. $channel = new Channel;
  135. $channel->setName('Demo Web store');
  136. $channel->setType('magento');
  137. $channel->setConnectors(["customer", "order", "cart", "region"]);
  138. $channel->setTransport($this->transport);
  139. $this->em->persist($channel);
  140. $this->channel = $channel;
  141. return $this;
  142. }
  143. /**
  144. * @return $this
  145. */
  146. protected function createTransport()
  147. {
  148. $transport = new MagentoSoapTransport;
  149. $transport->setAdminUrl('http://localhost/magento/admin');
  150. $transport->setApiKey('key');
  151. $transport->setApiUser('user');
  152. $transport->setIsExtensionInstalled(true);
  153. $transport->setIsWsiMode(false);
  154. $transport->setWebsiteId('1');
  155. $transport->setWsdlUrl('http://localhost/magento/api/v2_soap?wsdl=1');
  156. $transport->setWebsites([['id' => 1, 'label' => 'Website ID: 1, Stores: English, French, German']]);
  157. $this->em->persist($transport);
  158. $this->transport = $transport;
  159. return $this;
  160. }
  161. /**
  162. * @param $region
  163. * @param $country
  164. * @param $originId
  165. *
  166. * @return CartAddress
  167. */
  168. protected function createCartAddress($region, $country, $originId)
  169. {
  170. $cartAddress = new CartAddress;
  171. $cartAddress->setRegion($region);
  172. $cartAddress->setCountry($country);
  173. $cartAddress->setCity('City');
  174. $cartAddress->setStreet('street');
  175. $cartAddress->setPostalCode(123456);
  176. $cartAddress->setFirstName('John');
  177. $cartAddress->setLastName('Doe');
  178. $cartAddress->setOriginId($originId);
  179. $this->em->persist($cartAddress);
  180. return $cartAddress;
  181. }
  182. /**
  183. * @param $region
  184. * @param $country
  185. *
  186. * @return MagentoAddress
  187. */
  188. protected function createMagentoAddress($region, $country)
  189. {
  190. $address = new MagentoAddress;
  191. $address->setRegion($region);
  192. $address->setCountry($country);
  193. $address->setCity('City');
  194. $address->setStreet('street');
  195. $address->setPostalCode(123456);
  196. $address->setFirstName('John');
  197. $address->setLastName('Doe');
  198. $address->setLabel('label');
  199. $address->setPrimary(true);
  200. $address->setOrganization('oro');
  201. $address->setOriginId(1);
  202. $this->em->persist($address);
  203. return $address;
  204. }
  205. /**
  206. * @param $region
  207. * @param $country
  208. *
  209. * @return Address
  210. */
  211. protected function createAddress($region, $country)
  212. {
  213. $address = new Address;
  214. $address->setRegion($region);
  215. $address->setCountry($country);
  216. $address->setCity('City');
  217. $address->setStreet('street');
  218. $address->setPostalCode(123456);
  219. $address->setFirstName('John');
  220. $address->setLastName('Doe');
  221. $this->em->persist($address);
  222. return $address;
  223. }
  224. /**
  225. * @param $oid
  226. * @param Account $account
  227. * @param MagentoAddress $address
  228. *
  229. * @return Customer
  230. */
  231. protected function createCustomer($oid, Account $account, MagentoAddress $address)
  232. {
  233. $customer = new Customer();
  234. $customer->setChannel($this->channel);
  235. $customer->setFirstName('John');
  236. $customer->setLastName('Doe');
  237. $customer->setEmail('test@example.com');
  238. $customer->setOriginId($oid);
  239. $customer->setIsActive(true);
  240. $customer->setWebsite($this->website);
  241. $customer->setStore($this->store);
  242. $customer->setAccount($account);
  243. $customer->setGender(Gender::MALE);
  244. $customer->setGroup($this->customerGroup);
  245. $customer->setCreatedAt(new \DateTime('now'));
  246. $customer->setUpdatedAt(new \DateTime('now'));
  247. $customer->addAddress($address);
  248. $customer->setOwner($this->getUser());
  249. $this->em->persist($customer);
  250. return $customer;
  251. }
  252. /**
  253. * @return $this
  254. */
  255. protected function createWebSite()
  256. {
  257. $website = new Website();
  258. $website->setName('web site');
  259. $website->setOriginId(1);
  260. $website->setCode('web site code');
  261. $website->setChannel($this->channel);
  262. $this->em->persist($website);
  263. $this->website = $website;
  264. return $this;
  265. }
  266. /**
  267. * @return $this
  268. */
  269. protected function createStore()
  270. {
  271. $store = new Store;
  272. $store->setName('demo store');
  273. $store->setChannel($this->channel);
  274. $store->setCode(1);
  275. $store->setWebsite($this->website);
  276. $store->setOriginId(1);
  277. $this->em->persist($store);
  278. $this->store = $store;
  279. return $this;
  280. }
  281. /**
  282. * @param $billing
  283. * @param $shipping
  284. *
  285. * @return Account
  286. */
  287. protected function createAccount($billing, $shipping)
  288. {
  289. $account = new Account;
  290. $account->setName('acc');
  291. $account->setBillingAddress($billing);
  292. $account->setShippingAddress($shipping);
  293. $account->setOwner($this->getUser());
  294. $this->em->persist($account);
  295. return $account;
  296. }
  297. /**
  298. * @return $this
  299. */
  300. protected function createCustomerGroup()
  301. {
  302. $customerGroup = new CustomerGroup;
  303. $customerGroup->setName('group');
  304. $customerGroup->setChannel($this->channel);
  305. $customerGroup->setOriginId(1);
  306. $this->em->persist($customerGroup);
  307. $this->customerGroup = $customerGroup;
  308. return $this;
  309. }
  310. /**
  311. * @return CartItem
  312. */
  313. protected function createCartItem()
  314. {
  315. $cartItem = new CartItem();
  316. $cartItem->setName('item' . mt_rand(0, 99999));
  317. $cartItem->setDescription('something');
  318. $cartItem->setPrice(mt_rand(10, 99999));
  319. $cartItem->setProductId(1);
  320. $cartItem->setFreeShipping('true');
  321. $cartItem->setIsVirtual(1);
  322. $cartItem->setRowTotal(100);
  323. $cartItem->setTaxAmount(10);
  324. $cartItem->setProductType('type');
  325. $cartItem->setSku('sku');
  326. $cartItem->setQty(0);
  327. $cartItem->setDiscountAmount(0);
  328. $cartItem->setTaxPercent(0);
  329. $cartItem->setCreatedAt(new \DateTime('now'));
  330. $cartItem->setUpdatedAt(new \DateTime('now'));
  331. $this->em->persist($cartItem);
  332. return $cartItem;
  333. }
  334. /**
  335. * @return CartStatus
  336. */
  337. protected function getStatus()
  338. {
  339. $status = $this->em->getRepository('OroCRMMagentoBundle:CartStatus')->findOneBy(['name' => 'open']);
  340. return $status;
  341. }
  342. /**
  343. * @param CartItem $cartItem
  344. * @param Cart $cart
  345. *
  346. * @return $this
  347. */
  348. protected function updateCartItem(CartItem $cartItem, Cart $cart)
  349. {
  350. $cartItem->setCart($cart);
  351. $this->em->persist($cartItem);
  352. return $this;
  353. }
  354. /**
  355. * @param Cart $cart
  356. * @param Customer $customer
  357. *
  358. * @return Order
  359. */
  360. protected function createOrder(Cart $cart, Customer $customer)
  361. {
  362. $order = new Order();
  363. $order->setChannel($this->channel);
  364. $order->setStatus('open');
  365. $order->setIncrementId('one');
  366. $order->setCreatedAt(new \DateTime('now'));
  367. $order->setUpdatedAt(new \DateTime('now'));
  368. $order->setCart($cart);
  369. $order->setStore($this->store);
  370. $order->setCustomer($customer);
  371. $order->setCustomerEmail('customer@email.com');
  372. $order->setDiscountAmount(34.40);
  373. $order->setTaxAmount(12.47);
  374. $order->setShippingAmount(5);
  375. $order->setTotalPaidAmount(17.85);
  376. $order->setTotalInvoicedAmount(11);
  377. $order->setTotalRefundedAmount(4);
  378. $order->setTotalCanceledAmount(0);
  379. $order->setShippingMethod('some unique shipping method');
  380. $order->setRemoteIp('unique ip');
  381. $order->setGiftMessage('some very unique gift message');
  382. $order->setOwner($this->getUser());
  383. $this->em->persist($order);
  384. return $order;
  385. }
  386. protected function createBaseOrderItem(Order $order)
  387. {
  388. $orderItem = new OrderItem();
  389. $orderItem->setId(mt_rand(0, 9999));
  390. $orderItem->setName('some order item');
  391. $orderItem->setSku('some sku');
  392. $orderItem->setQty(1);
  393. $orderItem->setOrder($order);
  394. $orderItem->setCost(51.00);
  395. $orderItem->setPrice(75.00);
  396. $orderItem->setWeight(6.12);
  397. $orderItem->setTaxPercent(2);
  398. $orderItem->setTaxAmount(1.5);
  399. $orderItem->setDiscountPercent(4);
  400. $orderItem->setDiscountAmount(0);
  401. $orderItem->setRowTotal(234);
  402. $this->em->persist($orderItem);
  403. return $orderItem;
  404. }
  405. /**
  406. * @return User
  407. */
  408. protected function getUser()
  409. {
  410. $user = $this->em->getRepository('OroUserBundle:User')->findOneBy(['username' => 'admin']);
  411. return $user;
  412. }
  413. }