TransactionTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Webkul\Checkout\Models\Cart;
  4. use Webkul\Checkout\Models\CartAddress;
  5. use Webkul\Checkout\Models\CartItem;
  6. use Webkul\Checkout\Models\CartPayment;
  7. use Webkul\Checkout\Models\CartShippingRate;
  8. use Webkul\Customer\Models\Customer;
  9. use Webkul\Customer\Models\CustomerAddress;
  10. use Webkul\Faker\Helpers\Product as ProductFaker;
  11. use Webkul\Sales\Generators\InvoiceSequencer;
  12. use Webkul\Sales\Models\Invoice;
  13. use Webkul\Sales\Models\InvoiceItem;
  14. use Webkul\Sales\Models\Order;
  15. use Webkul\Sales\Models\OrderAddress;
  16. use Webkul\Sales\Models\OrderItem;
  17. use Webkul\Sales\Models\OrderPayment;
  18. use Webkul\Sales\Models\OrderTransaction;
  19. use function Pest\Laravel\get;
  20. use function Pest\Laravel\postJson;
  21. it('should return the index page of transactions', function () {
  22. // Act and Assert.
  23. $this->loginAsAdmin();
  24. get(route('admin.sales.transactions.index'))
  25. ->assertOk()
  26. ->assertSeeText(trans('admin::app.sales.transactions.index.title'));
  27. });
  28. it('should fails the validation error when store the transaction when certain inputs not provided', function () {
  29. // Arrange.
  30. $product = (new ProductFaker([
  31. 'attributes' => [
  32. 5 => 'new',
  33. ],
  34. 'attribute_value' => [
  35. 'new' => [
  36. 'boolean_value' => true,
  37. ],
  38. ],
  39. ]))
  40. ->getSimpleProductFactory()
  41. ->create();
  42. $customer = Customer::factory()->create();
  43. $cart = Cart::factory()->create([
  44. 'customer_id' => $customer->id,
  45. 'customer_first_name' => $customer->first_name,
  46. 'customer_last_name' => $customer->last_name,
  47. 'customer_email' => $customer->email,
  48. 'is_guest' => 0,
  49. ]);
  50. $additional = [
  51. 'product_id' => $product->id,
  52. 'rating' => '0',
  53. 'is_buy_now' => '0',
  54. 'quantity' => '1',
  55. ];
  56. $cartItem = CartItem::factory()->create([
  57. 'cart_id' => $cart->id,
  58. 'product_id' => $product->id,
  59. 'sku' => $product->sku,
  60. 'quantity' => $additional['quantity'],
  61. 'name' => $product->name,
  62. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  63. 'base_price' => $price,
  64. 'total' => $convertedPrice * $additional['quantity'],
  65. 'base_total' => $price * $additional['quantity'],
  66. 'weight' => $product->weight ?? 0,
  67. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  68. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  69. 'type' => $product->type,
  70. 'additional' => $additional,
  71. ]);
  72. $customerAddress = CustomerAddress::factory()->create([
  73. 'cart_id' => $cart->id,
  74. 'customer_id' => $customer->id,
  75. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  76. ]);
  77. $cartBillingAddress = CartAddress::factory()->create([
  78. 'cart_id' => $cart->id,
  79. 'customer_id' => $customer->id,
  80. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  81. ]);
  82. $cartShippingAddress = CartAddress::factory()->create([
  83. 'cart_id' => $cart->id,
  84. 'customer_id' => $customer->id,
  85. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  86. ]);
  87. $cartPayment = CartPayment::factory()->create([
  88. 'cart_id' => $cart->id,
  89. 'method' => $paymentMethod = 'cashondelivery',
  90. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  91. ]);
  92. $order = Order::factory()->create([
  93. 'cart_id' => $cart->id,
  94. 'customer_id' => $customer->id,
  95. 'customer_email' => $customer->email,
  96. 'customer_first_name' => $customer->first_name,
  97. 'customer_last_name' => $customer->last_name,
  98. ]);
  99. $orderItem = OrderItem::factory()->create([
  100. 'product_id' => $product->id,
  101. 'order_id' => $order->id,
  102. 'sku' => $product->sku,
  103. 'type' => $product->type,
  104. 'name' => $product->name,
  105. ]);
  106. $orderBillingAddress = OrderAddress::factory()->create([
  107. 'cart_id' => $cart->id,
  108. 'customer_id' => $customer->id,
  109. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  110. ]);
  111. $orderShippingAddress = OrderAddress::factory()->create([
  112. 'cart_id' => $cart->id,
  113. 'customer_id' => $customer->id,
  114. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  115. ]);
  116. $orderPayment = OrderPayment::factory()->create([
  117. 'order_id' => $order->id,
  118. ]);
  119. $invoice = Invoice::factory([
  120. 'order_id' => $order->id,
  121. 'state' => 'paid',
  122. ])->create();
  123. $invoiceItem = InvoiceItem::factory()->create([
  124. 'invoice_id' => $invoice->id,
  125. 'order_item_id' => $orderItem->id,
  126. 'name' => $orderItem->name,
  127. 'sku' => $orderItem->sku,
  128. 'qty' => 1,
  129. 'price' => $orderItem->price,
  130. 'base_price' => $orderItem->base_price,
  131. 'total' => $orderItem->price,
  132. 'base_total' => $orderItem->base_price,
  133. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  134. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  135. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  136. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  137. 'product_id' => $orderItem->product_id,
  138. 'product_type' => $orderItem->product_type,
  139. 'additional' => $orderItem->additional,
  140. ]);
  141. // Act and Assert.
  142. $this->loginAsAdmin();
  143. postJson(route('admin.sales.transactions.store'))
  144. ->assertJsonValidationErrorFor('invoice_id')
  145. ->assertJsonValidationErrorFor('payment_method')
  146. ->assertJsonValidationErrorFor('amount')
  147. ->assertUnprocessable();
  148. $cart->refresh();
  149. $cartItem->refresh();
  150. $cartBillingAddress->refresh();
  151. $cartShippingAddress->refresh();
  152. $orderBillingAddress->refresh();
  153. $orderShippingAddress->refresh();
  154. $order->refresh();
  155. $orderItem->refresh();
  156. $invoiceItem->refresh();
  157. $this->assertModelWise([
  158. Cart::class => [
  159. $this->prepareCart($cart),
  160. ],
  161. CartItem::class => [
  162. $this->prepareCartItem($cartItem),
  163. ],
  164. CartPayment::class => [
  165. $this->prepareCartPayment($cartPayment),
  166. ],
  167. CartAddress::class => [
  168. $this->prepareAddress($cartBillingAddress),
  169. ],
  170. CartAddress::class => [
  171. $this->prepareAddress($cartShippingAddress),
  172. ],
  173. CustomerAddress::class => [
  174. $this->prepareAddress($customerAddress),
  175. ],
  176. Order::class => [
  177. $this->prepareOrder($order),
  178. ],
  179. OrderItem::class => [
  180. $this->prepareOrderItem($orderItem),
  181. ],
  182. OrderAddress::class => [
  183. $this->prepareAddress($orderBillingAddress),
  184. $this->prepareAddress($orderShippingAddress),
  185. ],
  186. OrderPayment::class => [
  187. $this->prepareOrderPayment($orderPayment),
  188. ],
  189. InvoiceItem::class => [
  190. $this->prepareInvoiceItem($invoiceItem),
  191. ],
  192. ]);
  193. });
  194. it('should store the order transaction', function () {
  195. // Arrange.
  196. $product = (new ProductFaker([
  197. 'attributes' => [
  198. 5 => 'new',
  199. ],
  200. 'attribute_value' => [
  201. 'new' => [
  202. 'boolean_value' => true,
  203. ],
  204. ],
  205. ]))
  206. ->getSimpleProductFactory()
  207. ->create();
  208. $customer = Customer::factory()->create();
  209. $cart = Cart::factory()->create([
  210. 'customer_id' => $customer->id,
  211. 'customer_first_name' => $customer->first_name,
  212. 'customer_last_name' => $customer->last_name,
  213. 'customer_email' => $customer->email,
  214. 'is_guest' => 0,
  215. ]);
  216. $additional = [
  217. 'product_id' => $product->id,
  218. 'rating' => '0',
  219. 'is_buy_now' => '0',
  220. 'quantity' => '1',
  221. ];
  222. $cartItem = CartItem::factory()->create([
  223. 'cart_id' => $cart->id,
  224. 'product_id' => $product->id,
  225. 'sku' => $product->sku,
  226. 'quantity' => $additional['quantity'],
  227. 'name' => $product->name,
  228. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  229. 'base_price' => $price,
  230. 'total' => $convertedPrice * $additional['quantity'],
  231. 'base_total' => $price * $additional['quantity'],
  232. 'weight' => $product->weight ?? 0,
  233. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  234. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  235. 'type' => $product->type,
  236. 'additional' => $additional,
  237. ]);
  238. $customerAddress = CustomerAddress::factory()->create([
  239. 'cart_id' => $cart->id,
  240. 'customer_id' => $customer->id,
  241. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  242. ]);
  243. $cartBillingAddress = CartAddress::factory()->create([
  244. 'cart_id' => $cart->id,
  245. 'customer_id' => $customer->id,
  246. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  247. ]);
  248. $cartShippingAddress = CartAddress::factory()->create([
  249. 'cart_id' => $cart->id,
  250. 'customer_id' => $customer->id,
  251. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  252. ]);
  253. $cartPayment = CartPayment::factory()->create([
  254. 'cart_id' => $cart->id,
  255. 'method' => $paymentMethod = 'cashondelivery',
  256. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  257. ]);
  258. $cartShippingRate = CartShippingRate::factory()->create([
  259. 'carrier' => 'free',
  260. 'carrier_title' => 'Free shipping',
  261. 'method' => 'free_free',
  262. 'method_title' => 'Free Shipping',
  263. 'method_description' => 'Free Shipping',
  264. 'cart_address_id' => $cartShippingAddress->id,
  265. ]);
  266. $order = Order::factory()->create([
  267. 'cart_id' => $cart->id,
  268. 'customer_id' => $customer->id,
  269. 'customer_email' => $customer->email,
  270. 'customer_first_name' => $customer->first_name,
  271. 'customer_last_name' => $customer->last_name,
  272. 'status' => 'processing',
  273. 'sub_total_invoiced' => $product->price,
  274. 'base_sub_total_invoiced' => $product->price,
  275. ]);
  276. $orderItem = OrderItem::factory()->create([
  277. 'product_id' => $product->id,
  278. 'order_id' => $order->id,
  279. 'sku' => $product->sku,
  280. 'type' => $product->type,
  281. 'name' => $product->name,
  282. 'qty_invoiced' => 1,
  283. 'base_total_invoiced' => $product->price,
  284. ]);
  285. $orderBillingAddress = OrderAddress::factory()->create([
  286. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  287. 'cart_id' => $cart->id,
  288. 'customer_id' => $customer->id,
  289. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  290. 'order_id' => $order->id,
  291. ]);
  292. $orderShippingAddress = OrderAddress::factory()->create([
  293. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  294. 'cart_id' => $cart->id,
  295. 'customer_id' => $customer->id,
  296. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  297. 'order_id' => $order->id,
  298. ]);
  299. $orderPayment = OrderPayment::factory()->create([
  300. 'order_id' => $order->id,
  301. 'method' => 'cashondelivery',
  302. ]);
  303. $invoice = Invoice::factory([
  304. 'order_id' => $order->id,
  305. 'sub_total' => $order->grand_total,
  306. 'base_sub_total' => $order->grand_total,
  307. 'grand_total' => $order->grand_total,
  308. 'base_grand_total' => $order->grand_total,
  309. 'increment_id' => app(InvoiceSequencer::class)->resolveGeneratorClass(),
  310. ])->create();
  311. $invoiceItem = InvoiceItem::factory()->create([
  312. 'invoice_id' => $invoice->id,
  313. 'order_item_id' => $orderItem->id,
  314. 'name' => $orderItem->name,
  315. 'sku' => $orderItem->sku,
  316. 'price' => $orderItem->price,
  317. 'base_price' => $orderItem->base_price,
  318. 'total' => $orderItem->price,
  319. 'base_total' => $orderItem->base_price,
  320. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  321. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  322. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  323. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  324. 'product_id' => $orderItem->product_id,
  325. 'product_type' => $orderItem->product_type,
  326. 'additional' => $orderItem->additional,
  327. ]);
  328. // Act and Assert.
  329. $this->loginAsAdmin();
  330. postJson(route('admin.sales.transactions.store'), [
  331. 'invoice_id' => $invoice->id,
  332. 'payment_method' => $orderPayment->method,
  333. 'amount' => $order->grand_total,
  334. ])
  335. ->assertOk()
  336. ->assertJsonPath('message', trans('admin::app.sales.transactions.index.create.transaction-saved'));
  337. $cart->refresh();
  338. $cartItem->refresh();
  339. $cartBillingAddress->refresh();
  340. $cartShippingAddress->refresh();
  341. $orderBillingAddress->refresh();
  342. $orderShippingAddress->refresh();
  343. $order->refresh();
  344. $orderItem->refresh();
  345. $invoiceItem->refresh();
  346. $this->assertModelWise([
  347. Cart::class => [
  348. $this->prepareCart($cart),
  349. ],
  350. CartItem::class => [
  351. $this->prepareCartItem($cartItem),
  352. ],
  353. CartPayment::class => [
  354. $this->prepareCartPayment($cartPayment),
  355. ],
  356. CartAddress::class => [
  357. $this->prepareAddress($cartBillingAddress),
  358. ],
  359. CartAddress::class => [
  360. $this->prepareAddress($cartShippingAddress),
  361. ],
  362. CartShippingRate::class => [
  363. $this->prepareCartShippingRate($cartShippingRate),
  364. ],
  365. CustomerAddress::class => [
  366. $this->prepareAddress($customerAddress),
  367. ],
  368. Order::class => [
  369. $this->prepareOrder($order),
  370. ],
  371. OrderItem::class => [
  372. $this->prepareOrderItem($orderItem),
  373. ],
  374. OrderAddress::class => [
  375. $this->prepareAddress($orderBillingAddress),
  376. $this->prepareAddress($orderShippingAddress),
  377. ],
  378. OrderPayment::class => [
  379. $this->prepareOrderPayment($orderPayment),
  380. ],
  381. Invoice::class => [
  382. [
  383. 'order_id' => $order->id,
  384. 'state' => 'paid',
  385. ],
  386. ],
  387. InvoiceItem::class => [
  388. $this->prepareInvoiceItem($invoiceItem),
  389. ],
  390. OrderTransaction::class => [
  391. [
  392. 'status' => 'paid',
  393. 'invoice_id' => $invoice->id,
  394. 'order_id' => $order->id,
  395. ],
  396. ],
  397. ]);
  398. });
  399. it('should view the transaction', function () {
  400. // Arrange.
  401. $product = (new ProductFaker([
  402. 'attributes' => [
  403. 5 => 'new',
  404. ],
  405. 'attribute_value' => [
  406. 'new' => [
  407. 'boolean_value' => true,
  408. ],
  409. ],
  410. ]))
  411. ->getSimpleProductFactory()
  412. ->create();
  413. $customer = Customer::factory()->create();
  414. $cart = Cart::factory()->create([
  415. 'customer_id' => $customer->id,
  416. 'customer_first_name' => $customer->first_name,
  417. 'customer_last_name' => $customer->last_name,
  418. 'customer_email' => $customer->email,
  419. 'is_guest' => 0,
  420. ]);
  421. $additional = [
  422. 'product_id' => $product->id,
  423. 'rating' => '0',
  424. 'is_buy_now' => '0',
  425. 'quantity' => '1',
  426. ];
  427. $cartItem = CartItem::factory()->create([
  428. 'cart_id' => $cart->id,
  429. 'product_id' => $product->id,
  430. 'sku' => $product->sku,
  431. 'quantity' => $additional['quantity'],
  432. 'name' => $product->name,
  433. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  434. 'base_price' => $price,
  435. 'total' => $convertedPrice * $additional['quantity'],
  436. 'base_total' => $price * $additional['quantity'],
  437. 'weight' => $product->weight ?? 0,
  438. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  439. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  440. 'type' => $product->type,
  441. 'additional' => $additional,
  442. ]);
  443. $customerAddress = CustomerAddress::factory()->create([
  444. 'cart_id' => $cart->id,
  445. 'customer_id' => $customer->id,
  446. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  447. ]);
  448. $cartBillingAddress = CartAddress::factory()->create([
  449. 'cart_id' => $cart->id,
  450. 'customer_id' => $customer->id,
  451. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  452. ]);
  453. $cartShippingAddress = CartAddress::factory()->create([
  454. 'cart_id' => $cart->id,
  455. 'customer_id' => $customer->id,
  456. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  457. ]);
  458. $cartPayment = CartPayment::factory()->create([
  459. 'cart_id' => $cart->id,
  460. 'method' => $paymentMethod = 'cashondelivery',
  461. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  462. ]);
  463. $cartShippingRate = CartShippingRate::factory()->create([
  464. 'carrier' => 'free',
  465. 'carrier_title' => 'Free shipping',
  466. 'method' => 'free_free',
  467. 'method_title' => 'Free Shipping',
  468. 'method_description' => 'Free Shipping',
  469. 'cart_address_id' => $cartShippingAddress->id,
  470. ]);
  471. $order = Order::factory()->create([
  472. 'cart_id' => $cart->id,
  473. 'customer_id' => $customer->id,
  474. 'customer_email' => $customer->email,
  475. 'customer_first_name' => $customer->first_name,
  476. 'customer_last_name' => $customer->last_name,
  477. 'status' => 'processing',
  478. 'sub_total_invoiced' => $product->price,
  479. 'base_sub_total_invoiced' => $product->price,
  480. ]);
  481. $orderItem = OrderItem::factory()->create([
  482. 'product_id' => $product->id,
  483. 'order_id' => $order->id,
  484. 'sku' => $product->sku,
  485. 'type' => $product->type,
  486. 'name' => $product->name,
  487. 'qty_invoiced' => 1,
  488. 'base_total_invoiced' => $product->price,
  489. ]);
  490. $orderBillingAddress = OrderAddress::factory()->create([
  491. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  492. 'cart_id' => $cart->id,
  493. 'customer_id' => $customer->id,
  494. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  495. 'order_id' => $order->id,
  496. ]);
  497. $orderShippingAddress = OrderAddress::factory()->create([
  498. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  499. 'cart_id' => $cart->id,
  500. 'customer_id' => $customer->id,
  501. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  502. 'order_id' => $order->id,
  503. ]);
  504. $orderPayment = OrderPayment::factory()->create([
  505. 'order_id' => $order->id,
  506. 'method' => 'cashondelivery',
  507. ]);
  508. $invoice = Invoice::factory()->create([
  509. 'order_id' => $order->id,
  510. 'state' => 'paid',
  511. 'total_qty' => 1,
  512. 'base_currency_code' => $order->base_currency_code,
  513. 'channel_currency_code' => $order->channel_currency_code,
  514. 'order_currency_code' => $order->order_currency_code,
  515. 'email_sent' => 1,
  516. 'discount_amount' => 0,
  517. 'base_discount_amount' => 0,
  518. 'sub_total' => $orderItem->base_price,
  519. 'base_sub_total' => $orderItem->base_price,
  520. 'grand_total' => $orderItem->price,
  521. 'base_grand_total' => $orderItem->price,
  522. ]);
  523. $invoiceItem = InvoiceItem::factory()->create([
  524. 'invoice_id' => $invoice->id,
  525. 'order_item_id' => $orderItem->id,
  526. 'name' => $orderItem->name,
  527. 'sku' => $orderItem->sku,
  528. 'qty' => 1,
  529. 'price' => $orderItem->price,
  530. 'base_price' => $orderItem->base_price,
  531. 'total' => $orderItem->price,
  532. 'base_total' => $orderItem->base_price,
  533. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  534. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  535. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  536. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  537. 'product_id' => $orderItem->product_id,
  538. 'product_type' => $orderItem->product_type,
  539. 'additional' => $orderItem->additional,
  540. ]);
  541. $orderTransaction = OrderTransaction::factory()->create([
  542. 'transaction_id' => md5(uniqid()),
  543. 'type' => 'cashondelivery',
  544. 'payment_method' => 'cashondelivery',
  545. 'status' => $invoice->state,
  546. 'order_id' => $invoice->order->id,
  547. 'invoice_id' => $invoice->id,
  548. 'amount' => $invoice->grand_total,
  549. ]);
  550. // Act and Assert.
  551. $this->loginAsAdmin();
  552. get(route('admin.sales.transactions.view', $orderTransaction->id))
  553. ->assertOk()
  554. ->assertJsonPath('data.id', $orderTransaction->id)
  555. ->assertJsonPath('data.payment_title', $orderTransaction->payment_title)
  556. ->assertJsonPath('data.invoice_id', $orderTransaction->invoice_id)
  557. ->assertJsonPath('data.order_id', $orderTransaction->order_id);
  558. $cart->refresh();
  559. $cartItem->refresh();
  560. $cartBillingAddress->refresh();
  561. $cartShippingAddress->refresh();
  562. $orderBillingAddress->refresh();
  563. $orderShippingAddress->refresh();
  564. $order->refresh();
  565. $orderItem->refresh();
  566. $invoiceItem->refresh();
  567. $orderTransaction->refresh();
  568. $this->assertModelWise([
  569. Cart::class => [
  570. $this->prepareCart($cart),
  571. ],
  572. CartItem::class => [
  573. $this->prepareCartItem($cartItem),
  574. ],
  575. CartPayment::class => [
  576. $this->prepareCartPayment($cartPayment),
  577. ],
  578. CartAddress::class => [
  579. $this->prepareAddress($cartBillingAddress),
  580. ],
  581. CartAddress::class => [
  582. $this->prepareAddress($cartShippingAddress),
  583. ],
  584. CartShippingRate::class => [
  585. $this->prepareCartShippingRate($cartShippingRate),
  586. ],
  587. CustomerAddress::class => [
  588. $this->prepareAddress($customerAddress),
  589. ],
  590. Order::class => [
  591. $this->prepareOrder($order),
  592. ],
  593. OrderItem::class => [
  594. $this->prepareOrderItem($orderItem),
  595. ],
  596. OrderAddress::class => [
  597. $this->prepareAddress($orderBillingAddress),
  598. $this->prepareAddress($orderShippingAddress),
  599. ],
  600. OrderPayment::class => [
  601. $this->prepareOrderPayment($orderPayment),
  602. ],
  603. Invoice::class => [
  604. $this->prepareInvoice($order, $orderItem),
  605. ],
  606. InvoiceItem::class => [
  607. $this->prepareInvoiceItem($invoiceItem),
  608. ],
  609. OrderTransaction::class => [
  610. $this->prepareOrderTransaction($orderTransaction),
  611. ],
  612. ]);
  613. });