ShipmentTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Mail;
  4. use Webkul\Admin\Mail\Order\InventorySourceNotification;
  5. use Webkul\Admin\Mail\Order\ShippedNotification as AdminShippedNotification;
  6. use Webkul\Checkout\Models\Cart;
  7. use Webkul\Checkout\Models\CartAddress;
  8. use Webkul\Checkout\Models\CartItem;
  9. use Webkul\Checkout\Models\CartPayment;
  10. use Webkul\Checkout\Models\CartShippingRate;
  11. use Webkul\Core\Models\CoreConfig;
  12. use Webkul\Customer\Models\Customer;
  13. use Webkul\Customer\Models\CustomerAddress;
  14. use Webkul\Faker\Helpers\Product as ProductFaker;
  15. use Webkul\Sales\Models\Order;
  16. use Webkul\Sales\Models\OrderAddress;
  17. use Webkul\Sales\Models\OrderItem;
  18. use Webkul\Sales\Models\OrderPayment;
  19. use Webkul\Sales\Models\Shipment;
  20. use Webkul\Shop\Mail\Order\ShippedNotification as ShopShippedNotification;
  21. use function Pest\Laravel\get;
  22. use function Pest\Laravel\postJson;
  23. it('should returns the shipment page', function () {
  24. // Act and Assert.
  25. $this->loginAsAdmin();
  26. get(route('admin.sales.shipments.index'))
  27. ->assertOk()
  28. ->assertSeeText(trans('admin::app.sales.shipments.index.title'));
  29. });
  30. it('should fails the validation error when store the the shipment to the order', function () {
  31. // Arrange.
  32. $product = (new ProductFaker([
  33. 'attributes' => [
  34. 5 => 'new',
  35. ],
  36. 'attribute_value' => [
  37. 'new' => [
  38. 'boolean_value' => true,
  39. ],
  40. ],
  41. ]))
  42. ->getSimpleProductFactory()
  43. ->create();
  44. $customer = Customer::factory()->create();
  45. $cart = Cart::factory()->create([
  46. 'customer_id' => $customer->id,
  47. 'customer_first_name' => $customer->first_name,
  48. 'customer_last_name' => $customer->last_name,
  49. 'customer_email' => $customer->email,
  50. 'is_guest' => 0,
  51. ]);
  52. $additional = [
  53. 'product_id' => $product->id,
  54. 'rating' => '0',
  55. 'is_buy_now' => '0',
  56. 'quantity' => '1',
  57. ];
  58. $cartItem = CartItem::factory()->create([
  59. 'cart_id' => $cart->id,
  60. 'product_id' => $product->id,
  61. 'sku' => $product->sku,
  62. 'quantity' => $additional['quantity'],
  63. 'name' => $product->name,
  64. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  65. 'base_price' => $price,
  66. 'total' => $convertedPrice * $additional['quantity'],
  67. 'base_total' => $price * $additional['quantity'],
  68. 'weight' => $product->weight ?? 0,
  69. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  70. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  71. 'type' => $product->type,
  72. 'additional' => $additional,
  73. ]);
  74. $customerAddress = CustomerAddress::factory()->create([
  75. 'cart_id' => $cart->id,
  76. 'customer_id' => $customer->id,
  77. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  78. ]);
  79. $cartBillingAddress = CartAddress::factory()->create([
  80. 'cart_id' => $cart->id,
  81. 'customer_id' => $customer->id,
  82. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  83. ]);
  84. $cartShippingAddress = CartAddress::factory()->create([
  85. 'cart_id' => $cart->id,
  86. 'customer_id' => $customer->id,
  87. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  88. ]);
  89. $cartPayment = CartPayment::factory()->create([
  90. 'cart_id' => $cart->id,
  91. 'method' => $paymentMethod = 'cashondelivery',
  92. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  93. ]);
  94. $cartShippingRate = CartShippingRate::factory()->create([
  95. 'carrier' => 'free',
  96. 'carrier_title' => 'Free shipping',
  97. 'method' => 'free_free',
  98. 'method_title' => 'Free Shipping',
  99. 'method_description' => 'Free Shipping',
  100. 'cart_address_id' => $cartShippingAddress->id,
  101. ]);
  102. $order = Order::factory()->create([
  103. 'cart_id' => $cart->id,
  104. 'customer_id' => $customer->id,
  105. 'customer_email' => $customer->email,
  106. 'customer_first_name' => $customer->first_name,
  107. 'customer_last_name' => $customer->last_name,
  108. ]);
  109. $orderItem = OrderItem::factory()->create([
  110. 'product_id' => $product->id,
  111. 'order_id' => $order->id,
  112. 'sku' => $product->sku,
  113. 'type' => $product->type,
  114. 'name' => $product->name,
  115. ]);
  116. $orderBillingAddress = OrderAddress::factory()->create([
  117. 'cart_id' => $cart->id,
  118. 'customer_id' => $customer->id,
  119. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  120. ]);
  121. $orderShippingAddress = OrderAddress::factory()->create([
  122. 'cart_id' => $cart->id,
  123. 'customer_id' => $customer->id,
  124. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  125. ]);
  126. $orderPayment = OrderPayment::factory()->create([
  127. 'order_id' => $order->id,
  128. ]);
  129. foreach ($order->items as $item) {
  130. foreach ($order->channel->inventory_sources as $inventorySource) {
  131. $items[$item->id][$inventorySource->id] = $inventorySource->id;
  132. }
  133. }
  134. // Act and Assert.
  135. $this->loginAsAdmin();
  136. postJson(route('admin.sales.shipments.store', $order->id))
  137. ->assertJsonValidationErrorFor('shipment.source')
  138. ->assertUnprocessable();
  139. $cart->refresh();
  140. $cartItem->refresh();
  141. $cartBillingAddress->refresh();
  142. $cartShippingAddress->refresh();
  143. $orderBillingAddress->refresh();
  144. $orderShippingAddress->refresh();
  145. $order->refresh();
  146. $orderItem->refresh();
  147. $this->assertModelWise([
  148. Cart::class => [
  149. $this->prepareCart($cart),
  150. ],
  151. CartItem::class => [
  152. $this->prepareCartItem($cartItem),
  153. ],
  154. CartPayment::class => [
  155. $this->prepareCartPayment($cartPayment),
  156. ],
  157. CartAddress::class => [
  158. $this->prepareAddress($cartBillingAddress),
  159. ],
  160. CartAddress::class => [
  161. $this->prepareAddress($cartShippingAddress),
  162. ],
  163. CartShippingRate::class => [
  164. $this->prepareCartShippingRate($cartShippingRate),
  165. ],
  166. CustomerAddress::class => [
  167. $this->prepareAddress($customerAddress),
  168. ],
  169. Order::class => [
  170. $this->prepareOrder($order),
  171. ],
  172. OrderItem::class => [
  173. $this->prepareOrderItem($orderItem),
  174. ],
  175. OrderAddress::class => [
  176. $this->prepareAddress($orderBillingAddress),
  177. $this->prepareAddress($orderShippingAddress),
  178. ],
  179. OrderPayment::class => [
  180. $this->prepareOrderPayment($orderPayment),
  181. ],
  182. ]);
  183. });
  184. it('should store the shipment to the order', function () {
  185. // Arrange.
  186. $product = (new ProductFaker([
  187. 'attributes' => [
  188. 5 => 'new',
  189. ],
  190. 'attribute_value' => [
  191. 'new' => [
  192. 'boolean_value' => true,
  193. ],
  194. ],
  195. ]))
  196. ->getSimpleProductFactory()
  197. ->create();
  198. $customer = Customer::factory()->create();
  199. $cart = Cart::factory()->create([
  200. 'customer_id' => $customer->id,
  201. 'customer_first_name' => $customer->first_name,
  202. 'customer_last_name' => $customer->last_name,
  203. 'customer_email' => $customer->email,
  204. 'is_guest' => 0,
  205. ]);
  206. $additional = [
  207. 'product_id' => $product->id,
  208. 'rating' => '0',
  209. 'is_buy_now' => '0',
  210. 'quantity' => '1',
  211. ];
  212. $cartItem = CartItem::factory()->create([
  213. 'cart_id' => $cart->id,
  214. 'product_id' => $product->id,
  215. 'sku' => $product->sku,
  216. 'quantity' => $additional['quantity'],
  217. 'name' => $product->name,
  218. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  219. 'base_price' => $price,
  220. 'total' => $convertedPrice * $additional['quantity'],
  221. 'base_total' => $price * $additional['quantity'],
  222. 'weight' => $product->weight ?? 0,
  223. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  224. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  225. 'type' => $product->type,
  226. 'additional' => $additional,
  227. ]);
  228. $customerAddress = CustomerAddress::factory()->create([
  229. 'cart_id' => $cart->id,
  230. 'customer_id' => $customer->id,
  231. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  232. ]);
  233. $cartBillingAddress = CartAddress::factory()->create([
  234. 'cart_id' => $cart->id,
  235. 'customer_id' => $customer->id,
  236. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  237. ]);
  238. $cartShippingAddress = CartAddress::factory()->create([
  239. 'cart_id' => $cart->id,
  240. 'customer_id' => $customer->id,
  241. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  242. ]);
  243. $cartPayment = CartPayment::factory()->create([
  244. 'cart_id' => $cart->id,
  245. 'method' => $paymentMethod = 'cashondelivery',
  246. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  247. ]);
  248. $cartShippingRate = CartShippingRate::factory()->create([
  249. 'carrier' => 'free',
  250. 'carrier_title' => 'Free shipping',
  251. 'method' => 'free_free',
  252. 'method_title' => 'Free Shipping',
  253. 'method_description' => 'Free Shipping',
  254. 'cart_address_id' => $cartShippingAddress->id,
  255. ]);
  256. $order = Order::factory()->create([
  257. 'cart_id' => $cart->id,
  258. 'customer_id' => $customer->id,
  259. 'customer_email' => $customer->email,
  260. 'customer_first_name' => $customer->first_name,
  261. 'customer_last_name' => $customer->last_name,
  262. ]);
  263. $orderItem = OrderItem::factory()->create([
  264. 'product_id' => $product->id,
  265. 'order_id' => $order->id,
  266. 'sku' => $product->sku,
  267. 'type' => $product->type,
  268. 'name' => $product->name,
  269. ]);
  270. $orderBillingAddress = OrderAddress::factory()->create([
  271. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  272. 'cart_id' => $cart->id,
  273. 'customer_id' => $customer->id,
  274. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  275. 'order_id' => $order->id,
  276. ]);
  277. $orderShippingAddress = OrderAddress::factory()->create([
  278. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  279. 'cart_id' => $cart->id,
  280. 'customer_id' => $customer->id,
  281. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  282. 'order_id' => $order->id,
  283. ]);
  284. $orderPayment = OrderPayment::factory()->create([
  285. 'order_id' => $order->id,
  286. ]);
  287. $shipmentSources = $order->channel->inventory_sources->pluck('id')->toArray();
  288. foreach ($order->items as $item) {
  289. foreach ($order->channel->inventory_sources as $inventorySource) {
  290. $items[$item->id][$inventorySource->id] = $inventorySource->id;
  291. }
  292. }
  293. // Act and Assert.
  294. $this->loginAsAdmin();
  295. postJson(route('admin.sales.shipments.store', $order->id), [
  296. 'shipment' => $data = [
  297. 'source' => fake()->randomElement($shipmentSources),
  298. 'items' => $items,
  299. 'carrier_title' => fake()->name(),
  300. 'track_number' => fake()->numerify('##########'),
  301. ],
  302. ])
  303. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  304. ->isRedirection();
  305. $cart->refresh();
  306. $cartItem->refresh();
  307. $cartBillingAddress->refresh();
  308. $cartShippingAddress->refresh();
  309. $orderBillingAddress->refresh();
  310. $orderShippingAddress->refresh();
  311. $order->refresh();
  312. $orderItem->refresh();
  313. $this->assertModelWise([
  314. Cart::class => [
  315. $this->prepareCart($cart),
  316. ],
  317. CartItem::class => [
  318. $this->prepareCartItem($cartItem),
  319. ],
  320. CartPayment::class => [
  321. $this->prepareCartPayment($cartPayment),
  322. ],
  323. CartAddress::class => [
  324. $this->prepareAddress($cartBillingAddress),
  325. ],
  326. CartAddress::class => [
  327. $this->prepareAddress($cartShippingAddress),
  328. ],
  329. CartShippingRate::class => [
  330. $this->prepareCartShippingRate($cartShippingRate),
  331. ],
  332. CustomerAddress::class => [
  333. $this->prepareAddress($customerAddress),
  334. ],
  335. Order::class => [
  336. $this->prepareOrder($order),
  337. ],
  338. OrderItem::class => [
  339. $this->prepareOrderItem($orderItem),
  340. ],
  341. OrderAddress::class => [
  342. $this->prepareAddress($orderBillingAddress),
  343. $this->prepareAddress($orderShippingAddress),
  344. ],
  345. OrderPayment::class => [
  346. $this->prepareOrderPayment($orderPayment),
  347. ],
  348. Shipment::class => [
  349. [
  350. 'order_id' => $order->id,
  351. 'carrier_title' => $data['carrier_title'],
  352. 'track_number' => $data['track_number'],
  353. ],
  354. ],
  355. ]);
  356. });
  357. it('should store the shipment to the order and send email to the customer and admin and also send a mail to inventory source', function () {
  358. // Arrange.
  359. Mail::fake();
  360. CoreConfig::where('code', 'emails.general.notifications.emails.general.notifications.new_shipment')->update([
  361. 'value' => 1,
  362. ]);
  363. CoreConfig::where('code', 'emails.general.notifications.emails.general.notifications.new_shipment_mail_to_admin')->update([
  364. 'value' => 1,
  365. ]);
  366. CoreConfig::where('code', 'emails.general.notifications.emails.general.notifications.new_inventory_source')->update([
  367. 'value' => 1,
  368. ]);
  369. $product = (new ProductFaker([
  370. 'attributes' => [
  371. 5 => 'new',
  372. ],
  373. 'attribute_value' => [
  374. 'new' => [
  375. 'boolean_value' => true,
  376. ],
  377. ],
  378. ]))
  379. ->getSimpleProductFactory()
  380. ->create();
  381. $customer = Customer::factory()->create();
  382. $cart = Cart::factory()->create([
  383. 'customer_id' => $customer->id,
  384. 'customer_first_name' => $customer->first_name,
  385. 'customer_last_name' => $customer->last_name,
  386. 'customer_email' => $customer->email,
  387. 'is_guest' => 0,
  388. ]);
  389. $additional = [
  390. 'product_id' => $product->id,
  391. 'rating' => '0',
  392. 'is_buy_now' => '0',
  393. 'quantity' => '1',
  394. ];
  395. $cartItem = CartItem::factory()->create([
  396. 'cart_id' => $cart->id,
  397. 'product_id' => $product->id,
  398. 'sku' => $product->sku,
  399. 'quantity' => $additional['quantity'],
  400. 'name' => $product->name,
  401. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  402. 'base_price' => $price,
  403. 'total' => $convertedPrice * $additional['quantity'],
  404. 'base_total' => $price * $additional['quantity'],
  405. 'weight' => $product->weight ?? 0,
  406. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  407. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  408. 'type' => $product->type,
  409. 'additional' => $additional,
  410. ]);
  411. $customerAddress = CustomerAddress::factory()->create([
  412. 'cart_id' => $cart->id,
  413. 'customer_id' => $customer->id,
  414. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  415. ]);
  416. $cartBillingAddress = CartAddress::factory()->create([
  417. 'cart_id' => $cart->id,
  418. 'customer_id' => $customer->id,
  419. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  420. ]);
  421. $cartShippingAddress = CartAddress::factory()->create([
  422. 'cart_id' => $cart->id,
  423. 'customer_id' => $customer->id,
  424. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  425. ]);
  426. $cartPayment = CartPayment::factory()->create([
  427. 'cart_id' => $cart->id,
  428. 'method' => $paymentMethod = 'cashondelivery',
  429. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  430. ]);
  431. $cartShippingRate = CartShippingRate::factory()->create([
  432. 'carrier' => 'free',
  433. 'carrier_title' => 'Free shipping',
  434. 'method' => 'free_free',
  435. 'method_title' => 'Free Shipping',
  436. 'method_description' => 'Free Shipping',
  437. 'cart_address_id' => $cartShippingAddress->id,
  438. ]);
  439. $order = Order::factory()->create([
  440. 'cart_id' => $cart->id,
  441. 'customer_id' => $customer->id,
  442. 'customer_email' => $customer->email,
  443. 'customer_first_name' => $customer->first_name,
  444. 'customer_last_name' => $customer->last_name,
  445. ]);
  446. $orderItem = OrderItem::factory()->create([
  447. 'product_id' => $product->id,
  448. 'order_id' => $order->id,
  449. 'sku' => $product->sku,
  450. 'type' => $product->type,
  451. 'name' => $product->name,
  452. ]);
  453. $orderBillingAddress = OrderAddress::factory()->create([
  454. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  455. 'cart_id' => $cart->id,
  456. 'customer_id' => $customer->id,
  457. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  458. 'order_id' => $order->id,
  459. ]);
  460. $orderShippingAddress = OrderAddress::factory()->create([
  461. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  462. 'cart_id' => $cart->id,
  463. 'customer_id' => $customer->id,
  464. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  465. 'order_id' => $order->id,
  466. ]);
  467. $orderPayment = OrderPayment::factory()->create([
  468. 'order_id' => $order->id,
  469. ]);
  470. $shipmentSources = $order->channel->inventory_sources->pluck('id')->toArray();
  471. foreach ($order->items as $item) {
  472. foreach ($order->channel->inventory_sources as $inventorySource) {
  473. $items[$item->id][$inventorySource->id] = $inventorySource->id;
  474. }
  475. }
  476. // Act and Assert.
  477. $this->loginAsAdmin();
  478. postJson(route('admin.sales.shipments.store', $order->id), [
  479. 'shipment' => $data = [
  480. 'source' => fake()->randomElement($shipmentSources),
  481. 'items' => $items,
  482. 'carrier_title' => fake()->name(),
  483. 'track_number' => fake()->numerify('##########'),
  484. ],
  485. ])
  486. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  487. ->isRedirection();
  488. $cart->refresh();
  489. $cartItem->refresh();
  490. $cartBillingAddress->refresh();
  491. $cartShippingAddress->refresh();
  492. $orderBillingAddress->refresh();
  493. $orderShippingAddress->refresh();
  494. $order->refresh();
  495. $orderItem->refresh();
  496. $this->assertModelWise([
  497. Cart::class => [
  498. $this->prepareCart($cart),
  499. ],
  500. CartItem::class => [
  501. $this->prepareCartItem($cartItem),
  502. ],
  503. CartPayment::class => [
  504. $this->prepareCartPayment($cartPayment),
  505. ],
  506. CartAddress::class => [
  507. $this->prepareAddress($cartBillingAddress),
  508. ],
  509. CartAddress::class => [
  510. $this->prepareAddress($cartShippingAddress),
  511. ],
  512. CartShippingRate::class => [
  513. $this->prepareCartShippingRate($cartShippingRate),
  514. ],
  515. CustomerAddress::class => [
  516. $this->prepareAddress($customerAddress),
  517. ],
  518. Order::class => [
  519. $this->prepareOrder($order),
  520. ],
  521. OrderItem::class => [
  522. $this->prepareOrderItem($orderItem),
  523. ],
  524. OrderAddress::class => [
  525. $this->prepareAddress($orderBillingAddress),
  526. $this->prepareAddress($orderShippingAddress),
  527. ],
  528. OrderPayment::class => [
  529. $this->prepareOrderPayment($orderPayment),
  530. ],
  531. Shipment::class => [
  532. [
  533. 'order_id' => $order->id,
  534. 'carrier_title' => $data['carrier_title'],
  535. 'track_number' => $data['track_number'],
  536. ],
  537. ],
  538. ]);
  539. Mail::assertQueued(AdminShippedNotification::class);
  540. Mail::assertQueued(ShopShippedNotification::class);
  541. Mail::assertQueued(InventorySourceNotification::class);
  542. Mail::assertQueuedCount(3);
  543. });
  544. it('should return the view page of shipments', function () {
  545. // Arrange.
  546. $product = (new ProductFaker([
  547. 'attributes' => [
  548. 5 => 'new',
  549. ],
  550. 'attribute_value' => [
  551. 'new' => [
  552. 'boolean_value' => true,
  553. ],
  554. ],
  555. ]))
  556. ->getSimpleProductFactory()
  557. ->create();
  558. $customer = Customer::factory()->create();
  559. $cart = Cart::factory()->create([
  560. 'customer_id' => $customer->id,
  561. 'customer_first_name' => $customer->first_name,
  562. 'customer_last_name' => $customer->last_name,
  563. 'customer_email' => $customer->email,
  564. 'is_guest' => 0,
  565. ]);
  566. $additional = [
  567. 'product_id' => $product->id,
  568. 'rating' => '0',
  569. 'is_buy_now' => '0',
  570. 'quantity' => '1',
  571. ];
  572. $cartItem = CartItem::factory()->create([
  573. 'cart_id' => $cart->id,
  574. 'product_id' => $product->id,
  575. 'sku' => $product->sku,
  576. 'quantity' => $additional['quantity'],
  577. 'name' => $product->name,
  578. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  579. 'base_price' => $price,
  580. 'total' => $convertedPrice * $additional['quantity'],
  581. 'base_total' => $price * $additional['quantity'],
  582. 'weight' => $product->weight ?? 0,
  583. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  584. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  585. 'type' => $product->type,
  586. 'additional' => $additional,
  587. ]);
  588. $customerAddress = CustomerAddress::factory()->create([
  589. 'cart_id' => $cart->id,
  590. 'customer_id' => $customer->id,
  591. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  592. ]);
  593. $cartBillingAddress = CartAddress::factory()->create([
  594. 'cart_id' => $cart->id,
  595. 'customer_id' => $customer->id,
  596. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  597. ]);
  598. $cartShippingAddress = CartAddress::factory()->create([
  599. 'cart_id' => $cart->id,
  600. 'customer_id' => $customer->id,
  601. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  602. ]);
  603. $cartPayment = CartPayment::factory()->create([
  604. 'cart_id' => $cart->id,
  605. 'method' => $paymentMethod = 'cashondelivery',
  606. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  607. ]);
  608. $cartShippingRate = CartShippingRate::factory()->create([
  609. 'carrier' => 'free',
  610. 'carrier_title' => 'Free shipping',
  611. 'method' => 'free_free',
  612. 'method_title' => 'Free Shipping',
  613. 'method_description' => 'Free Shipping',
  614. 'cart_address_id' => $cartShippingAddress->id,
  615. ]);
  616. $order = Order::factory()->create([
  617. 'cart_id' => $cart->id,
  618. 'customer_id' => $customer->id,
  619. 'customer_email' => $customer->email,
  620. 'customer_first_name' => $customer->first_name,
  621. 'customer_last_name' => $customer->last_name,
  622. ]);
  623. $orderItem = OrderItem::factory()->create([
  624. 'product_id' => $product->id,
  625. 'order_id' => $order->id,
  626. 'sku' => $product->sku,
  627. 'type' => $product->type,
  628. 'name' => $product->name,
  629. ]);
  630. $orderBillingAddress = OrderAddress::factory()->create([
  631. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  632. 'cart_id' => $cart->id,
  633. 'customer_id' => $customer->id,
  634. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  635. 'order_id' => $order->id,
  636. ]);
  637. $orderShippingAddress = OrderAddress::factory()->create([
  638. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  639. 'cart_id' => $cart->id,
  640. 'customer_id' => $customer->id,
  641. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  642. 'order_id' => $order->id,
  643. ]);
  644. $orderPayment = OrderPayment::factory()->create([
  645. 'order_id' => $order->id,
  646. ]);
  647. $shipment = Shipment::factory()->create([
  648. 'total_qty' => rand(1, 10),
  649. 'total_weight' => rand(1, 10),
  650. 'carrier_code' => fake()->numerify('##########'),
  651. 'carrier_title' => fake()->word(),
  652. 'track_number' => fake()->numerify('##########'),
  653. 'customer_id' => $customer->id,
  654. 'customer_type' => Customer::class,
  655. 'order_id' => $order->id,
  656. 'order_address_id' => $orderBillingAddress->id,
  657. 'inventory_source_id' => 1,
  658. 'inventory_source_name' => 'Default',
  659. ]);
  660. // Act and Assert.
  661. $this->loginAsAdmin();
  662. get(route('admin.sales.shipments.view', $shipment->id))
  663. ->assertOk()
  664. ->assertSeeText(trans('admin::app.sales.shipments.view.title', ['shipment_id' => $shipment->id]))
  665. ->assertSeeText(trans('admin::app.account.edit.back-btn'));
  666. $cart->refresh();
  667. $cartItem->refresh();
  668. $cartBillingAddress->refresh();
  669. $cartShippingAddress->refresh();
  670. $orderBillingAddress->refresh();
  671. $orderShippingAddress->refresh();
  672. $order->refresh();
  673. $orderItem->refresh();
  674. $this->assertModelWise([
  675. Cart::class => [
  676. $this->prepareCart($cart),
  677. ],
  678. CartItem::class => [
  679. $this->prepareCartItem($cartItem),
  680. ],
  681. CartPayment::class => [
  682. $this->prepareCartPayment($cartPayment),
  683. ],
  684. CartAddress::class => [
  685. $this->prepareAddress($cartBillingAddress),
  686. ],
  687. CartAddress::class => [
  688. $this->prepareAddress($cartShippingAddress),
  689. ],
  690. CartShippingRate::class => [
  691. $this->prepareCartShippingRate($cartShippingRate),
  692. ],
  693. CustomerAddress::class => [
  694. $this->prepareAddress($customerAddress),
  695. ],
  696. Order::class => [
  697. $this->prepareOrder($order),
  698. ],
  699. OrderItem::class => [
  700. $this->prepareOrderItem($orderItem),
  701. ],
  702. OrderAddress::class => [
  703. $this->prepareAddress($orderBillingAddress),
  704. $this->prepareAddress($orderShippingAddress),
  705. ],
  706. OrderPayment::class => [
  707. $this->prepareOrderPayment($orderPayment),
  708. ],
  709. Shipment::class => [
  710. [
  711. 'total_qty' => $shipment->total_qty,
  712. 'total_weight' => $shipment->total_weight,
  713. 'carrier_code' => $shipment->carrier_code,
  714. 'carrier_title' => $shipment->carrier_title,
  715. 'track_number' => $shipment->track_number,
  716. 'customer_id' => $shipment->customer_id,
  717. 'customer_type' => $shipment->customer_type,
  718. 'order_id' => $shipment->order_id,
  719. 'order_address_id' => $shipment->order_address_id,
  720. 'inventory_source_id' => $shipment->inventory_source_id,
  721. 'inventory_source_name' => $shipment->inventory_source_name,
  722. ],
  723. ],
  724. ]);
  725. });