WeeeTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Model\Total\Creditmemo;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. class WeeeTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Weee\Model\Total\Creditmemo\Weee
  12. */
  13. protected $model;
  14. /**
  15. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $order;
  18. /**
  19. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  20. */
  21. protected $objectManager;
  22. /**
  23. * @var \Magento\Sales\Model\Order\Creditmemo|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $creditmemo;
  26. /**
  27. * @var \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $invoice;
  30. /**
  31. * @var \Magento\Weee\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $weeeData;
  34. protected function setUp()
  35. {
  36. $this->weeeData = $this->getMockBuilder(\Magento\Weee\Helper\Data::class)
  37. ->setMethods(
  38. [
  39. 'getRowWeeeTaxInclTax',
  40. 'getBaseRowWeeeTaxInclTax',
  41. 'getWeeeAmountInvoiced',
  42. 'getBaseWeeeAmountInvoiced',
  43. 'getWeeeAmountRefunded',
  44. 'getBaseWeeeAmountRefunded',
  45. 'getWeeeTaxAmountInvoiced',
  46. 'getBaseWeeeTaxAmountInvoiced',
  47. 'getWeeeTaxAmountRefunded',
  48. 'getBaseWeeeTaxAmountRefunded',
  49. 'getApplied',
  50. 'setApplied',
  51. 'includeInSubtotal',
  52. ]
  53. )->disableOriginalConstructor()
  54. ->getMock();
  55. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  56. $serializer = $this->objectManager->getObject(Json::class);
  57. /** @var \Magento\Sales\Model\Order\Invoice\Total\Tax $model */
  58. $this->model = $this->objectManager->getObject(
  59. \Magento\Weee\Model\Total\Creditmemo\Weee::class,
  60. [
  61. 'weeeData' => $this->weeeData,
  62. 'serializer' => $serializer
  63. ]
  64. );
  65. $this->order = $this->createPartialMock(\Magento\Sales\Model\Order::class, [
  66. '__wakeup'
  67. ]);
  68. $this->creditmemo = $this->createPartialMock(\Magento\Sales\Model\Order\Creditmemo::class, [
  69. 'getAllItems',
  70. 'getInvoice',
  71. 'roundPrice',
  72. 'getStore',
  73. '__wakeup',
  74. ]);
  75. }
  76. /**
  77. * @param array $creditmemoData
  78. * @param array $expectedResults
  79. * @dataProvider collectDataProvider
  80. */
  81. public function testCollect($creditmemoData, $expectedResults)
  82. {
  83. $roundingDelta = [];
  84. //Set up weeeData mock
  85. $this->weeeData->expects($this->once())
  86. ->method('includeInSubtotal')
  87. ->will($this->returnValue($creditmemoData['include_in_subtotal']));
  88. //Set up invoice mock
  89. /** @var \Magento\Sales\Model\Order\Invoice\Item[] $creditmemoItems */
  90. $creditmemoItems = [];
  91. foreach ($creditmemoData['items'] as $itemKey => $creditmemoItemData) {
  92. $creditmemoItems[$itemKey] = $this->getInvoiceItem($creditmemoItemData);
  93. }
  94. $this->creditmemo->expects($this->once())
  95. ->method('getAllItems')
  96. ->will($this->returnValue($creditmemoItems));
  97. foreach ($creditmemoData['data_fields'] as $key => $value) {
  98. $this->creditmemo->setData($key, $value);
  99. }
  100. $this->creditmemo->expects($this->any())
  101. ->method('roundPrice')
  102. ->will($this->returnCallback(
  103. function ($price, $type) use (&$roundingDelta) {
  104. if (!isset($roundingDelta[$type])) {
  105. $roundingDelta[$type] = 0;
  106. }
  107. $roundedPrice = round($price + $roundingDelta[$type], 2);
  108. $roundingDelta[$type] = $price - $roundedPrice;
  109. return $roundedPrice;
  110. }
  111. ));
  112. $this->model->collect($this->creditmemo);
  113. //verify invoice data
  114. foreach ($expectedResults['creditmemo_data'] as $key => $value) {
  115. $this->assertEquals(
  116. $value,
  117. $this->creditmemo->getData($key),
  118. 'Creditmemo data field '.$key.' is incorrect'
  119. );
  120. }
  121. //verify invoice item data
  122. foreach ($expectedResults['creditmemo_items'] as $itemKey => $itemData) {
  123. $creditmemoItem = $creditmemoItems[$itemKey];
  124. foreach ($itemData as $key => $value) {
  125. if ($key == 'tax_ratio') {
  126. $taxRatio = json_decode($creditmemoItem->getData($key), true);
  127. $this->assertEquals($value['weee'], $taxRatio['weee'], "Tax ratio is incorrect");
  128. } else {
  129. $this->assertEquals(
  130. $value,
  131. $creditmemoItem->getData($key),
  132. 'Creditmemo item field '.$key.' is incorrect'
  133. );
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  140. * @return array
  141. */
  142. public function collectDataProvider()
  143. {
  144. $result = [];
  145. // scenario 1: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, full creditmemo
  146. $result['complete_creditmemo'] = [
  147. 'creditmemo_data' => [
  148. 'items' => [
  149. 'item_1' => [
  150. 'order_item' => [
  151. 'qty_ordered' => 3,
  152. 'weee_tax_applied_row_amount' => 30,
  153. 'base_weee_tax_applied_row_amnt' => 30,
  154. 'row_weee_tax_incl_tax' => 32.47,
  155. 'base_row_weee_tax_incl_tax' => 32.47,
  156. 'weee_amount_invoiced' => 30,
  157. 'base_weee_amount_invoiced' => 30,
  158. 'weee_amount_refunded' => 0,
  159. 'base_weee_amount_refunded' => 0,
  160. 'weee_tax_amount_invoiced' => 2.47,
  161. 'base_weee_tax_amount_invoiced' => 2.47,
  162. 'weee_tax_amount_refunded' => 0,
  163. 'base_weee_tax_amount_refunded' => 0,
  164. 'applied_weee' => [
  165. [
  166. 'title' => 'recycling_fee',
  167. 'base_row_amount' => 30,
  168. 'row_amount' => 30,
  169. 'base_row_amount_incl_tax' => 32.47,
  170. 'row_amount_incl_tax' => 32.47,
  171. ],
  172. ],
  173. 'qty_invoiced' => 3,
  174. ],
  175. 'is_last' => true,
  176. 'data_fields' => [
  177. 'qty' => 3,
  178. 'applied_weee' => [
  179. [
  180. ],
  181. ],
  182. ],
  183. ],
  184. ],
  185. 'include_in_subtotal' => false,
  186. 'data_fields' => [
  187. 'grand_total' => 300,
  188. 'base_grand_total' => 300,
  189. 'subtotal' => 300,
  190. 'base_subtotal' => 300,
  191. 'subtotal_incl_tax' => 324.75,
  192. 'base_subtotal_incl_tax' => 324.75,
  193. 'tax_amount' => 0,
  194. 'base_tax_amount' => 0,
  195. ],
  196. ],
  197. 'expected_results' => [
  198. 'creditmemo_items' => [
  199. 'item_1' => [
  200. 'applied_weee' => [
  201. [
  202. 'title' => 'recycling_fee',
  203. 'base_row_amount' => 30,
  204. 'row_amount' => 30,
  205. 'base_row_amount_incl_tax' => 32.47,
  206. 'row_amount_incl_tax' => 32.47,
  207. ],
  208. ],
  209. 'tax_ratio' => ["weee" => 1.0],
  210. 'weee_tax_applied_row_amount' => 30,
  211. 'base_weee_tax_applied_row_amount' => 30,
  212. ],
  213. ],
  214. 'creditmemo_data' => [
  215. 'grand_total' => 332.47,
  216. 'base_grand_total' => 332.47,
  217. 'tax_amount' => 2.47,
  218. 'base_tax_amount' => 2.47,
  219. 'subtotal' => 300,
  220. 'base_subtotal' => 300,
  221. 'subtotal_incl_tax' => 357.22,
  222. 'base_subtotal_incl_tax' => 357.22,
  223. ],
  224. ],
  225. ];
  226. // Scenario 2: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, 2 item creditmemo
  227. $result['partial_creditmemo'] = [
  228. 'creditmemo_data' => [
  229. 'items' => [
  230. 'item_1' => [
  231. 'order_item' => [
  232. 'qty_ordered' => 3,
  233. 'weee_tax_applied_row_amount' => 30,
  234. 'base_weee_tax_applied_row_amnt' => 30,
  235. 'row_weee_tax_incl_tax' => 32.47,
  236. 'base_row_weee_tax_incl_tax' => 32.47,
  237. 'weee_amount_invoiced' => 30,
  238. 'base_weee_amount_invoiced' => 30,
  239. 'weee_amount_refunded' => 0,
  240. 'base_weee_amount_refunded' => 0,
  241. 'weee_tax_amount_invoiced' => 2.47,
  242. 'base_weee_tax_amount_invoiced' => 2.47,
  243. 'weee_tax_amount_refunded' => 0,
  244. 'base_weee_tax_amount_refunded' => 0,
  245. 'applied_weee' => [
  246. [
  247. 'title' => 'recycling_fee',
  248. 'base_row_amount' => 30,
  249. 'row_amount' => 30,
  250. 'base_row_amount_incl_tax' => 32.47,
  251. 'row_amount_incl_tax' => 32.47,
  252. ],
  253. ],
  254. 'qty_invoiced' => 3,
  255. ],
  256. 'is_last' => false,
  257. 'data_fields' => [
  258. 'qty' => 2,
  259. 'applied_weee' => [
  260. [
  261. ],
  262. ],
  263. ],
  264. ],
  265. ],
  266. 'include_in_subtotal' => false,
  267. 'data_fields' => [
  268. 'grand_total' => 200,
  269. 'base_grand_total' => 200,
  270. 'subtotal' => 200,
  271. 'base_subtotal' => 200,
  272. 'subtotal_incl_tax' => 216.5,
  273. 'base_subtotal_incl_tax' => 216.5,
  274. 'tax_amount' => 0,
  275. 'base_tax_amount' => 0,
  276. ],
  277. ],
  278. 'expected_results' => [
  279. 'creditmemo_items' => [
  280. 'item_1' => [
  281. 'applied_weee' => [
  282. [
  283. 'title' => 'recycling_fee',
  284. 'base_row_amount' => 20,
  285. 'row_amount' => 20,
  286. 'base_row_amount_incl_tax' => 21.65,
  287. 'row_amount_incl_tax' => 21.65,
  288. ],
  289. ],
  290. 'tax_ratio' => ['weee' => 1.65 / 2.47],
  291. 'weee_tax_applied_row_amount' => 20,
  292. 'base_weee_tax_applied_row_amount' => 20,
  293. ],
  294. ],
  295. 'creditmemo_data' => [
  296. 'grand_total' => 221.65,
  297. 'base_grand_total' => 221.65,
  298. 'tax_amount' => 1.65,
  299. 'base_tax_amount' => 1.65,
  300. 'subtotal' => 200,
  301. 'base_subtotal' => 200,
  302. 'subtotal_incl_tax' => 238.15,
  303. 'base_subtotal_incl_tax' => 238.15,
  304. ],
  305. ],
  306. ];
  307. // Scenario 3: 3 item_1, $100 with $weee, 8.25 tax rate, 3 items invoiced, 2 item returned
  308. $result['last_partial_creditmemo'] = [
  309. 'creditmemo_data' => [
  310. 'items' => [
  311. 'item_1' => [
  312. 'order_item' => [
  313. 'qty_ordered' => 3,
  314. 'weee_tax_applied_row_amount' => 30,
  315. 'base_weee_tax_applied_row_amnt' => 30,
  316. 'row_weee_tax_incl_tax' => 32.47,
  317. 'base_row_weee_tax_incl_tax' => 32.47,
  318. 'weee_amount_invoiced' => 30,
  319. 'base_weee_amount_invoiced' => 30,
  320. 'weee_amount_refunded' => 20,
  321. 'base_weee_amount_refunded' => 20,
  322. 'weee_tax_amount_invoiced' => 2.47,
  323. 'base_weee_tax_amount_invoiced' => 2.47,
  324. 'weee_tax_amount_refunded' => 1.64,
  325. 'base_weee_tax_amount_refunded' => 1.64,
  326. 'applied_weee' => [
  327. [
  328. 'title' => 'recycling_fee',
  329. 'base_row_amount' => 30,
  330. 'row_amount' => 30,
  331. 'base_row_amount_incl_tax' => 32.47,
  332. 'row_amount_incl_tax' => 32.47,
  333. ],
  334. ],
  335. 'qty_invoiced' => 3,
  336. ],
  337. 'is_last' => true,
  338. 'data_fields' => [
  339. 'qty' => 1,
  340. 'applied_weee' => [
  341. [
  342. ],
  343. ],
  344. ],
  345. ],
  346. ],
  347. 'include_in_subtotal' => false,
  348. 'data_fields' => [
  349. 'grand_total' => 100,
  350. 'base_grand_total' => 100,
  351. 'subtotal' => 100,
  352. 'base_subtotal' => 100,
  353. 'subtotal_incl_tax' => 108.25,
  354. 'base_subtotal_incl_tax' => 108.25,
  355. 'tax_amount' => 0,
  356. 'base_tax_amount' => 0,
  357. ],
  358. ],
  359. 'expected_results' => [
  360. 'creditmemo_items' => [
  361. 'item_1' => [
  362. 'applied_weee' => [
  363. [
  364. 'title' => 'recycling_fee',
  365. 'base_row_amount' => 10,
  366. 'row_amount' => 10,
  367. 'base_row_amount_incl_tax' => 10.82,
  368. 'row_amount_incl_tax' => 10.82,
  369. ],
  370. ],
  371. 'tax_ratio' => ['weee' => 0.83 / 2.47],
  372. 'weee_tax_applied_row_amount' => 10,
  373. 'base_weee_tax_applied_row_amount' => 10,
  374. ],
  375. ],
  376. 'creditmemo_data' => [
  377. 'grand_total' => 110.83,
  378. 'base_grand_total' => 110.83,
  379. 'tax_amount' => 0.83,
  380. 'base_tax_amount' => 0.83,
  381. 'subtotal' => 100,
  382. 'base_subtotal' => 100,
  383. 'subtotal_incl_tax' => 119.07,
  384. 'base_subtotal_incl_tax' => 119.07,
  385. ],
  386. ],
  387. ];
  388. // scenario 4: 3 item_1, $100 with $weee, 8.25 tax rate. Returning qty 0.
  389. $result['zero_return'] = [
  390. 'creditmemo_data' => [
  391. 'items' => [
  392. 'item_1' => [
  393. 'order_item' => [
  394. 'qty_ordered' => 3,
  395. 'weee_tax_applied_row_amount' => 30,
  396. 'base_weee_tax_applied_row_amnt' => 30,
  397. 'row_weee_tax_incl_tax' => 32.47,
  398. 'base_row_weee_tax_incl_tax' => 32.47,
  399. 'weee_amount_invoiced' => 30,
  400. 'base_weee_amount_invoiced' => 30,
  401. 'weee_amount_refunded' => 0,
  402. 'base_weee_amount_refunded' => 0,
  403. 'weee_tax_amount_invoiced' => 2.47,
  404. 'base_weee_tax_amount_invoiced' => 2.47,
  405. 'weee_tax_amount_refunded' => 0,
  406. 'base_weee_tax_amount_refunded' => 0,
  407. 'applied_weee' => [
  408. [
  409. 'title' => 'recycling_fee',
  410. 'base_row_amount' => 30,
  411. 'row_amount' => 30,
  412. 'base_row_amount_incl_tax' => 32.47,
  413. 'row_amount_incl_tax' => 32.47,
  414. ],
  415. ],
  416. 'qty_invoiced' => 3,
  417. ],
  418. 'is_last' => true,
  419. 'data_fields' => [
  420. 'qty' => 0,
  421. 'applied_weee' => [
  422. [
  423. ],
  424. ],
  425. ],
  426. ],
  427. ],
  428. 'include_in_subtotal' => false,
  429. 'data_fields' => [
  430. 'grand_total' => 300,
  431. 'base_grand_total' => 300,
  432. 'subtotal' => 300,
  433. 'base_subtotal' => 300,
  434. 'subtotal_incl_tax' => 324.75,
  435. 'base_subtotal_incl_tax' => 324.75,
  436. 'tax_amount' => 0,
  437. 'base_tax_amount' => 0,
  438. ],
  439. ],
  440. 'expected_results' => [
  441. 'creditmemo_items' => [
  442. 'item_1' => [
  443. 'applied_weee' => [
  444. [
  445. 'title' => 'recycling_fee',
  446. 'base_row_amount' => 0,
  447. 'row_amount' => 0,
  448. 'base_row_amount_incl_tax' => 0,
  449. 'row_amount_incl_tax' => 0,
  450. ],
  451. ],
  452. ],
  453. ],
  454. 'creditmemo_data' => [
  455. 'subtotal' => 300,
  456. 'base_subtotal' => 300,
  457. ],
  458. ],
  459. ];
  460. return $result;
  461. }
  462. /**
  463. * @param $creditmemoItemData array
  464. * @return \Magento\Sales\Model\Order\Creditmemo\Item|\PHPUnit_Framework_MockObject_MockObject
  465. */
  466. protected function getInvoiceItem($creditmemoItemData)
  467. {
  468. /** @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject $orderItem */
  469. $orderItem = $this->createPartialMock(\Magento\Sales\Model\Order\Item::class, [
  470. 'isDummy',
  471. '__wakeup'
  472. ]);
  473. foreach ($creditmemoItemData['order_item'] as $key => $value) {
  474. $orderItem->setData($key, $value);
  475. }
  476. $this->weeeData->expects($this->once())
  477. ->method('getRowWeeeTaxInclTax')
  478. ->with($orderItem)
  479. ->will($this->returnValue($orderItem->getRowWeeeTaxInclTax()));
  480. $this->weeeData->expects($this->once())
  481. ->method('getBaseRowWeeeTaxInclTax')
  482. ->with($orderItem)
  483. ->will($this->returnValue($orderItem->getBaseRowWeeeTaxInclTax()));
  484. $this->weeeData->expects($this->once())
  485. ->method('getWeeeAmountInvoiced')
  486. ->with($orderItem)
  487. ->will($this->returnValue($orderItem->getWeeeAmountInvoiced()));
  488. $this->weeeData->expects($this->once())
  489. ->method('getBaseWeeeAmountInvoiced')
  490. ->with($orderItem)
  491. ->will($this->returnValue($orderItem->getBaseWeeeAmountInvoiced()));
  492. $this->weeeData->expects($this->once())
  493. ->method('getWeeeTaxAmountInvoiced')
  494. ->with($orderItem)
  495. ->will($this->returnValue($orderItem->getWeeeTaxAmountInvoiced()));
  496. $this->weeeData->expects($this->once())
  497. ->method('getBaseWeeeTaxAmountInvoiced')
  498. ->with($orderItem)
  499. ->will($this->returnValue($orderItem->getBaseWeeeTaxAmountInvoiced()));
  500. $this->weeeData->expects($this->once())
  501. ->method('getWeeeAmountRefunded')
  502. ->with($orderItem)
  503. ->will($this->returnValue($orderItem->getWeeeAmountRefunded()));
  504. $this->weeeData->expects($this->once())
  505. ->method('getBaseWeeeAmountRefunded')
  506. ->with($orderItem)
  507. ->will($this->returnValue($orderItem->getBaseWeeeAmountRefunded()));
  508. $this->weeeData->expects($this->once())
  509. ->method('getWeeeTaxAmountRefunded')
  510. ->with($orderItem)
  511. ->will($this->returnValue($orderItem->getWeeeTaxAmountRefunded()));
  512. $this->weeeData->expects($this->once())
  513. ->method('getBaseWeeeTaxAmountRefunded')
  514. ->with($orderItem)
  515. ->will($this->returnValue($orderItem->getBaseWeeeTaxAmountRefunded()));
  516. /** @var \Magento\Sales\Model\Order\Invoice\Item|\PHPUnit_Framework_MockObject_MockObject $invoiceItem */
  517. $invoiceItem = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice\Item::class, [
  518. 'getOrderItem',
  519. 'isLast',
  520. '__wakeup'
  521. ]);
  522. $invoiceItem->expects($this->any())->method('getOrderItem')->will($this->returnValue($orderItem));
  523. $invoiceItem->expects($this->any())
  524. ->method('isLast')
  525. ->will($this->returnValue($creditmemoItemData['is_last']));
  526. foreach ($creditmemoItemData['data_fields'] as $key => $value) {
  527. $invoiceItem->setData($key, $value);
  528. }
  529. $this->weeeData->expects($this->any())
  530. ->method('getApplied')
  531. ->will($this->returnCallback(
  532. function ($item) {
  533. return $item->getAppliedWeee();
  534. }
  535. ));
  536. $this->weeeData->expects($this->any())
  537. ->method('setApplied')
  538. ->will($this->returnCallback(
  539. function ($item, $weee) {
  540. return $item->setAppliedWeee($weee);
  541. }
  542. ));
  543. return $invoiceItem;
  544. }
  545. }