InvoiceTest.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Mail;
  4. use Webkul\Admin\Mail\Order\InvoicedNotification as AdminInvoicedNotification;
  5. use Webkul\Checkout\Models\Cart;
  6. use Webkul\Checkout\Models\CartAddress;
  7. use Webkul\Checkout\Models\CartItem;
  8. use Webkul\Checkout\Models\CartPayment;
  9. use Webkul\Checkout\Models\CartShippingRate;
  10. use Webkul\Core\Models\CoreConfig;
  11. use Webkul\Customer\Models\Customer;
  12. use Webkul\Customer\Models\CustomerAddress;
  13. use Webkul\Faker\Helpers\Product as ProductFaker;
  14. use Webkul\Sales\Models\Invoice;
  15. use Webkul\Sales\Models\InvoiceItem;
  16. use Webkul\Sales\Models\Order;
  17. use Webkul\Sales\Models\OrderAddress;
  18. use Webkul\Sales\Models\OrderItem;
  19. use Webkul\Sales\Models\OrderPayment;
  20. use Webkul\Sales\Models\OrderTransaction;
  21. use Webkul\Shop\Mail\Order\InvoicedNotification as ShopInvoicedNotification;
  22. use function Pest\Laravel\get;
  23. use function Pest\Laravel\postJson;
  24. it('should returns the invoice index page', function () {
  25. // Act and Assert.
  26. $this->loginAsAdmin();
  27. get(route('admin.sales.invoices.index'))
  28. ->assertOk()
  29. ->assertSeeText(trans('admin::app.sales.invoices.index.title'));
  30. });
  31. it('should fails the validation error when the invoice item quantity not provided', function () {
  32. // Arrange.
  33. $product = (new ProductFaker([
  34. 'attributes' => [
  35. 5 => 'new',
  36. ],
  37. 'attribute_value' => [
  38. 'new' => [
  39. 'boolean_value' => true,
  40. ],
  41. ],
  42. ]))
  43. ->getSimpleProductFactory()
  44. ->create();
  45. $customer = Customer::factory()->create();
  46. $cart = Cart::factory()->create([
  47. 'customer_id' => $customer->id,
  48. 'customer_first_name' => $customer->first_name,
  49. 'customer_last_name' => $customer->last_name,
  50. 'customer_email' => $customer->email,
  51. 'is_guest' => 0,
  52. ]);
  53. $additional = [
  54. 'product_id' => $product->id,
  55. 'rating' => '0',
  56. 'is_buy_now' => '0',
  57. 'quantity' => '1',
  58. ];
  59. $cartItem = CartItem::factory()->create([
  60. 'cart_id' => $cart->id,
  61. 'product_id' => $product->id,
  62. 'sku' => $product->sku,
  63. 'quantity' => $additional['quantity'],
  64. 'name' => $product->name,
  65. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  66. 'base_price' => $price,
  67. 'total' => $convertedPrice * $additional['quantity'],
  68. 'base_total' => $price * $additional['quantity'],
  69. 'weight' => $product->weight ?? 0,
  70. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  71. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  72. 'type' => $product->type,
  73. 'additional' => $additional,
  74. ]);
  75. $customerAddress = CustomerAddress::factory()->create([
  76. 'cart_id' => $cart->id,
  77. 'customer_id' => $customer->id,
  78. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  79. ]);
  80. $cartBillingAddress = CartAddress::factory()->create([
  81. 'cart_id' => $cart->id,
  82. 'customer_id' => $customer->id,
  83. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  84. ]);
  85. $cartShippingAddress = CartAddress::factory()->create([
  86. 'cart_id' => $cart->id,
  87. 'customer_id' => $customer->id,
  88. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  89. ]);
  90. $cartPayment = CartPayment::factory()->create([
  91. 'cart_id' => $cart->id,
  92. 'method' => $paymentMethod = 'cashondelivery',
  93. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  94. ]);
  95. $cartShippingRate = CartShippingRate::factory()->create([
  96. 'carrier' => 'free',
  97. 'carrier_title' => 'Free shipping',
  98. 'method' => 'free_free',
  99. 'method_title' => 'Free Shipping',
  100. 'method_description' => 'Free Shipping',
  101. 'cart_address_id' => $cartShippingAddress->id,
  102. ]);
  103. $order = Order::factory()->create([
  104. 'cart_id' => $cart->id,
  105. 'customer_id' => $customer->id,
  106. 'customer_email' => $customer->email,
  107. 'customer_first_name' => $customer->first_name,
  108. 'customer_last_name' => $customer->last_name,
  109. ]);
  110. $orderItem = OrderItem::factory()->create([
  111. 'product_id' => $product->id,
  112. 'order_id' => $order->id,
  113. 'sku' => $product->sku,
  114. 'type' => $product->type,
  115. 'name' => $product->name,
  116. ]);
  117. $orderBillingAddress = OrderAddress::factory()->create([
  118. 'cart_id' => $cart->id,
  119. 'customer_id' => $customer->id,
  120. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  121. ]);
  122. $orderShippingAddress = OrderAddress::factory()->create([
  123. 'cart_id' => $cart->id,
  124. 'customer_id' => $customer->id,
  125. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  126. ]);
  127. $orderPayment = OrderPayment::factory()->create([
  128. 'order_id' => $order->id,
  129. ]);
  130. // Act and Assert.
  131. $this->loginAsAdmin();
  132. postJson(route('admin.sales.invoices.store', $order->id))
  133. ->assertJsonValidationErrorFor('invoice.items')
  134. ->assertUnprocessable();
  135. $cart->refresh();
  136. $cartItem->refresh();
  137. $cartBillingAddress->refresh();
  138. $cartShippingAddress->refresh();
  139. $orderBillingAddress->refresh();
  140. $orderShippingAddress->refresh();
  141. $order->refresh();
  142. $orderItem->refresh();
  143. $this->assertModelWise([
  144. Cart::class => [
  145. $this->prepareCart($cart),
  146. ],
  147. CartItem::class => [
  148. $this->prepareCartItem($cartItem),
  149. ],
  150. CartPayment::class => [
  151. $this->prepareCartPayment($cartPayment),
  152. ],
  153. CartAddress::class => [
  154. $this->prepareAddress($cartBillingAddress),
  155. ],
  156. CartAddress::class => [
  157. $this->prepareAddress($cartShippingAddress),
  158. ],
  159. CartShippingRate::class => [
  160. $this->prepareCartShippingRate($cartShippingRate),
  161. ],
  162. CustomerAddress::class => [
  163. $this->prepareAddress($customerAddress),
  164. ],
  165. Order::class => [
  166. $this->prepareOrder($order),
  167. ],
  168. OrderItem::class => [
  169. $this->prepareOrderItem($orderItem),
  170. ],
  171. OrderAddress::class => [
  172. $this->prepareAddress($orderBillingAddress),
  173. $this->prepareAddress($orderShippingAddress),
  174. ],
  175. OrderPayment::class => [
  176. $this->prepareOrderPayment($orderPayment),
  177. ],
  178. ]);
  179. });
  180. it('should fails the validation error when the invoice item quantity is string', function () {
  181. // Arrange.
  182. $product = (new ProductFaker([
  183. 'attributes' => [
  184. 5 => 'new',
  185. ],
  186. 'attribute_value' => [
  187. 'new' => [
  188. 'boolean_value' => true,
  189. ],
  190. ],
  191. ]))
  192. ->getSimpleProductFactory()
  193. ->create();
  194. $customer = Customer::factory()->create();
  195. $cart = Cart::factory()->create([
  196. 'customer_id' => $customer->id,
  197. 'customer_first_name' => $customer->first_name,
  198. 'customer_last_name' => $customer->last_name,
  199. 'customer_email' => $customer->email,
  200. 'is_guest' => 0,
  201. ]);
  202. $additional = [
  203. 'product_id' => $product->id,
  204. 'rating' => '0',
  205. 'is_buy_now' => '0',
  206. 'quantity' => '1',
  207. ];
  208. $cartItem = CartItem::factory()->create([
  209. 'cart_id' => $cart->id,
  210. 'product_id' => $product->id,
  211. 'sku' => $product->sku,
  212. 'quantity' => $additional['quantity'],
  213. 'name' => $product->name,
  214. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  215. 'base_price' => $price,
  216. 'total' => $convertedPrice * $additional['quantity'],
  217. 'base_total' => $price * $additional['quantity'],
  218. 'weight' => $product->weight ?? 0,
  219. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  220. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  221. 'type' => $product->type,
  222. 'additional' => $additional,
  223. ]);
  224. $customerAddress = CustomerAddress::factory()->create([
  225. 'cart_id' => $cart->id,
  226. 'customer_id' => $customer->id,
  227. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  228. ]);
  229. $cartBillingAddress = CartAddress::factory()->create([
  230. 'cart_id' => $cart->id,
  231. 'customer_id' => $customer->id,
  232. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  233. ]);
  234. $cartShippingAddress = CartAddress::factory()->create([
  235. 'cart_id' => $cart->id,
  236. 'customer_id' => $customer->id,
  237. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  238. ]);
  239. $cartPayment = CartPayment::factory()->create([
  240. 'cart_id' => $cart->id,
  241. 'method' => $paymentMethod = 'cashondelivery',
  242. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  243. ]);
  244. $cartShippingRate = CartShippingRate::factory()->create([
  245. 'carrier' => 'free',
  246. 'carrier_title' => 'Free shipping',
  247. 'method' => 'free_free',
  248. 'method_title' => 'Free Shipping',
  249. 'method_description' => 'Free Shipping',
  250. 'cart_address_id' => $cartShippingAddress->id,
  251. ]);
  252. $order = Order::factory()->create([
  253. 'cart_id' => $cart->id,
  254. 'customer_id' => $customer->id,
  255. 'customer_email' => $customer->email,
  256. 'customer_first_name' => $customer->first_name,
  257. 'customer_last_name' => $customer->last_name,
  258. ]);
  259. $orderItem = OrderItem::factory()->create([
  260. 'product_id' => $product->id,
  261. 'order_id' => $order->id,
  262. 'sku' => $product->sku,
  263. 'type' => $product->type,
  264. 'name' => $product->name,
  265. ]);
  266. $orderBillingAddress = OrderAddress::factory()->create([
  267. 'cart_id' => $cart->id,
  268. 'customer_id' => $customer->id,
  269. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  270. ]);
  271. $orderShippingAddress = OrderAddress::factory()->create([
  272. 'cart_id' => $cart->id,
  273. 'customer_id' => $customer->id,
  274. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  275. ]);
  276. $orderPayment = OrderPayment::factory()->create([
  277. 'order_id' => $order->id,
  278. ]);
  279. // Act and Assert.
  280. $this->loginAsAdmin();
  281. postJson(route('admin.sales.invoices.store', $order->id), [
  282. 'invoice' => [
  283. 'items' => [
  284. 'INVALID_ITEM_ID',
  285. ],
  286. ],
  287. ])
  288. ->assertJsonValidationErrorFor('invoice.items.0')
  289. ->assertUnprocessable();
  290. $cart->refresh();
  291. $cartItem->refresh();
  292. $cartBillingAddress->refresh();
  293. $cartShippingAddress->refresh();
  294. $orderBillingAddress->refresh();
  295. $orderShippingAddress->refresh();
  296. $order->refresh();
  297. $orderItem->refresh();
  298. $this->assertModelWise([
  299. Cart::class => [
  300. $this->prepareCart($cart),
  301. ],
  302. CartItem::class => [
  303. $this->prepareCartItem($cartItem),
  304. ],
  305. CartPayment::class => [
  306. $this->prepareCartPayment($cartPayment),
  307. ],
  308. CartAddress::class => [
  309. $this->prepareAddress($cartBillingAddress),
  310. ],
  311. CartAddress::class => [
  312. $this->prepareAddress($cartShippingAddress),
  313. ],
  314. CartShippingRate::class => [
  315. $this->prepareCartShippingRate($cartShippingRate),
  316. ],
  317. CustomerAddress::class => [
  318. $this->prepareAddress($customerAddress),
  319. ],
  320. Order::class => [
  321. $this->prepareOrder($order),
  322. ],
  323. OrderItem::class => [
  324. $this->prepareOrderItem($orderItem),
  325. ],
  326. OrderAddress::class => [
  327. $this->prepareAddress($orderBillingAddress),
  328. $this->prepareAddress($orderShippingAddress),
  329. ],
  330. OrderPayment::class => [
  331. $this->prepareOrderPayment($orderPayment),
  332. ],
  333. ]);
  334. });
  335. it('should store the invoice', function () {
  336. // Arrange.
  337. $product = (new ProductFaker([
  338. 'attributes' => [
  339. 5 => 'new',
  340. ],
  341. 'attribute_value' => [
  342. 'new' => [
  343. 'boolean_value' => true,
  344. ],
  345. ],
  346. ]))
  347. ->getSimpleProductFactory()
  348. ->create();
  349. $customer = Customer::factory()->create();
  350. $cart = Cart::factory()->create([
  351. 'customer_id' => $customer->id,
  352. 'customer_first_name' => $customer->first_name,
  353. 'customer_last_name' => $customer->last_name,
  354. 'customer_email' => $customer->email,
  355. 'is_guest' => 0,
  356. ]);
  357. $additional = [
  358. 'product_id' => $product->id,
  359. 'rating' => '0',
  360. 'is_buy_now' => '0',
  361. 'quantity' => '1',
  362. ];
  363. $cartItem = CartItem::factory()->create([
  364. 'cart_id' => $cart->id,
  365. 'product_id' => $product->id,
  366. 'sku' => $product->sku,
  367. 'quantity' => $additional['quantity'],
  368. 'name' => $product->name,
  369. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  370. 'base_price' => $price,
  371. 'total' => $convertedPrice * $additional['quantity'],
  372. 'base_total' => $price * $additional['quantity'],
  373. 'weight' => $product->weight ?? 0,
  374. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  375. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  376. 'type' => $product->type,
  377. 'additional' => $additional,
  378. ]);
  379. $customerAddress = CustomerAddress::factory()->create([
  380. 'cart_id' => $cart->id,
  381. 'customer_id' => $customer->id,
  382. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  383. ]);
  384. $cartBillingAddress = CartAddress::factory()->create([
  385. 'cart_id' => $cart->id,
  386. 'customer_id' => $customer->id,
  387. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  388. ]);
  389. $cartShippingAddress = CartAddress::factory()->create([
  390. 'cart_id' => $cart->id,
  391. 'customer_id' => $customer->id,
  392. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  393. ]);
  394. $cartPayment = CartPayment::factory()->create([
  395. 'cart_id' => $cart->id,
  396. 'method' => $paymentMethod = 'cashondelivery',
  397. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  398. ]);
  399. $cartShippingRate = CartShippingRate::factory()->create([
  400. 'carrier' => 'free',
  401. 'carrier_title' => 'Free shipping',
  402. 'method' => 'free_free',
  403. 'method_title' => 'Free Shipping',
  404. 'method_description' => 'Free Shipping',
  405. 'cart_address_id' => $cartShippingAddress->id,
  406. ]);
  407. $order = Order::factory()->create([
  408. 'cart_id' => $cart->id,
  409. 'customer_id' => $customer->id,
  410. 'customer_email' => $customer->email,
  411. 'customer_first_name' => $customer->first_name,
  412. 'customer_last_name' => $customer->last_name,
  413. ]);
  414. $orderItem = OrderItem::factory()->create([
  415. 'product_id' => $product->id,
  416. 'order_id' => $order->id,
  417. 'sku' => $product->sku,
  418. 'type' => $product->type,
  419. 'name' => $product->name,
  420. ]);
  421. $orderBillingAddress = OrderAddress::factory()->create([
  422. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  423. 'cart_id' => $cart->id,
  424. 'customer_id' => $customer->id,
  425. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  426. 'order_id' => $order->id,
  427. ]);
  428. $orderShippingAddress = OrderAddress::factory()->create([
  429. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  430. 'cart_id' => $cart->id,
  431. 'customer_id' => $customer->id,
  432. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  433. 'order_id' => $order->id,
  434. ]);
  435. $orderPayment = OrderPayment::factory()->create([
  436. 'order_id' => $order->id,
  437. ]);
  438. $items = [];
  439. foreach ($order->items as $item) {
  440. $items[$item->id] = $item->qty_to_invoice;
  441. }
  442. // Act and Assert.
  443. $this->loginAsAdmin();
  444. postJson(route('admin.sales.invoices.store', $order->id), [
  445. 'invoice' => [
  446. 'items' => $items,
  447. ],
  448. ])
  449. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  450. ->isRedirection();
  451. $cart->refresh();
  452. $cartItem->refresh();
  453. $cartBillingAddress->refresh();
  454. $cartShippingAddress->refresh();
  455. $orderBillingAddress->refresh();
  456. $orderShippingAddress->refresh();
  457. $order->refresh();
  458. $orderItem->refresh();
  459. $this->assertModelWise([
  460. Cart::class => [
  461. $this->prepareCart($cart),
  462. ],
  463. CartItem::class => [
  464. $this->prepareCartItem($cartItem),
  465. ],
  466. CartPayment::class => [
  467. $this->prepareCartPayment($cartPayment),
  468. ],
  469. CartAddress::class => [
  470. $this->prepareAddress($cartBillingAddress),
  471. ],
  472. CartAddress::class => [
  473. $this->prepareAddress($cartShippingAddress),
  474. ],
  475. CartShippingRate::class => [
  476. $this->prepareCartShippingRate($cartShippingRate),
  477. ],
  478. CustomerAddress::class => [
  479. $this->prepareAddress($customerAddress),
  480. ],
  481. Order::class => [
  482. $this->prepareOrder($order),
  483. ],
  484. OrderItem::class => [
  485. $this->prepareOrderItem($orderItem),
  486. ],
  487. OrderAddress::class => [
  488. $this->prepareAddress($orderBillingAddress),
  489. $this->prepareAddress($orderShippingAddress),
  490. ],
  491. OrderPayment::class => [
  492. $this->prepareOrderPayment($orderPayment),
  493. ],
  494. Invoice::class => [
  495. $this->prepareInvoice($order, $orderItem),
  496. ],
  497. ]);
  498. });
  499. it('should store the invoice and send email to the customer and admin', function () {
  500. // Arrange.
  501. Mail::fake();
  502. CoreConfig::where('code', 'emails.general.notifications.emails.general.notifications.new_invoice_mail_to_admin')->update([
  503. 'value' => 1,
  504. ]);
  505. $product = (new ProductFaker([
  506. 'attributes' => [
  507. 5 => 'new',
  508. ],
  509. 'attribute_value' => [
  510. 'new' => [
  511. 'boolean_value' => true,
  512. ],
  513. ],
  514. ]))
  515. ->getSimpleProductFactory()
  516. ->create();
  517. $customer = Customer::factory()->create();
  518. $cart = Cart::factory()->create([
  519. 'customer_id' => $customer->id,
  520. 'customer_first_name' => $customer->first_name,
  521. 'customer_last_name' => $customer->last_name,
  522. 'customer_email' => $customer->email,
  523. 'is_guest' => 0,
  524. ]);
  525. $additional = [
  526. 'product_id' => $product->id,
  527. 'rating' => '0',
  528. 'is_buy_now' => '0',
  529. 'quantity' => '1',
  530. ];
  531. $cartItem = CartItem::factory()->create([
  532. 'cart_id' => $cart->id,
  533. 'product_id' => $product->id,
  534. 'sku' => $product->sku,
  535. 'quantity' => $additional['quantity'],
  536. 'name' => $product->name,
  537. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  538. 'base_price' => $price,
  539. 'total' => $convertedPrice * $additional['quantity'],
  540. 'base_total' => $price * $additional['quantity'],
  541. 'weight' => $product->weight ?? 0,
  542. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  543. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  544. 'type' => $product->type,
  545. 'additional' => $additional,
  546. ]);
  547. $customerAddress = CustomerAddress::factory()->create([
  548. 'cart_id' => $cart->id,
  549. 'customer_id' => $customer->id,
  550. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  551. ]);
  552. $cartBillingAddress = CartAddress::factory()->create([
  553. 'cart_id' => $cart->id,
  554. 'customer_id' => $customer->id,
  555. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  556. ]);
  557. $cartShippingAddress = CartAddress::factory()->create([
  558. 'cart_id' => $cart->id,
  559. 'customer_id' => $customer->id,
  560. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  561. ]);
  562. $cartPayment = CartPayment::factory()->create([
  563. 'cart_id' => $cart->id,
  564. 'method' => $paymentMethod = 'cashondelivery',
  565. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  566. ]);
  567. $cartShippingRate = CartShippingRate::factory()->create([
  568. 'carrier' => 'free',
  569. 'carrier_title' => 'Free shipping',
  570. 'method' => 'free_free',
  571. 'method_title' => 'Free Shipping',
  572. 'method_description' => 'Free Shipping',
  573. 'cart_address_id' => $cartShippingAddress->id,
  574. ]);
  575. $order = Order::factory()->create([
  576. 'cart_id' => $cart->id,
  577. 'customer_id' => $customer->id,
  578. 'customer_email' => $customer->email,
  579. 'customer_first_name' => $customer->first_name,
  580. 'customer_last_name' => $customer->last_name,
  581. ]);
  582. $orderItem = OrderItem::factory()->create([
  583. 'product_id' => $product->id,
  584. 'order_id' => $order->id,
  585. 'sku' => $product->sku,
  586. 'type' => $product->type,
  587. 'name' => $product->name,
  588. ]);
  589. $orderBillingAddress = OrderAddress::factory()->create([
  590. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  591. 'cart_id' => $cart->id,
  592. 'customer_id' => $customer->id,
  593. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  594. 'order_id' => $order->id,
  595. ]);
  596. $orderShippingAddress = OrderAddress::factory()->create([
  597. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  598. 'cart_id' => $cart->id,
  599. 'customer_id' => $customer->id,
  600. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  601. 'order_id' => $order->id,
  602. ]);
  603. $orderPayment = OrderPayment::factory()->create([
  604. 'order_id' => $order->id,
  605. ]);
  606. $items = [];
  607. foreach ($order->items as $item) {
  608. $items[$item->id] = $item->qty_to_invoice;
  609. }
  610. // Act and Assert.
  611. $this->loginAsAdmin();
  612. postJson(route('admin.sales.invoices.store', $order->id), [
  613. 'invoice' => [
  614. 'items' => $items,
  615. ],
  616. ])
  617. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  618. ->isRedirection();
  619. $cart->refresh();
  620. $cartItem->refresh();
  621. $cartBillingAddress->refresh();
  622. $cartShippingAddress->refresh();
  623. $orderBillingAddress->refresh();
  624. $orderShippingAddress->refresh();
  625. $order->refresh();
  626. $orderItem->refresh();
  627. $this->assertModelWise([
  628. Cart::class => [
  629. $this->prepareCart($cart),
  630. ],
  631. CartItem::class => [
  632. $this->prepareCartItem($cartItem),
  633. ],
  634. CartPayment::class => [
  635. $this->prepareCartPayment($cartPayment),
  636. ],
  637. CartAddress::class => [
  638. $this->prepareAddress($cartBillingAddress),
  639. ],
  640. CartAddress::class => [
  641. $this->prepareAddress($cartShippingAddress),
  642. ],
  643. CartShippingRate::class => [
  644. $this->prepareCartShippingRate($cartShippingRate),
  645. ],
  646. CustomerAddress::class => [
  647. $this->prepareAddress($customerAddress),
  648. ],
  649. Order::class => [
  650. $this->prepareOrder($order),
  651. ],
  652. OrderItem::class => [
  653. $this->prepareOrderItem($orderItem),
  654. ],
  655. OrderAddress::class => [
  656. $this->prepareAddress($orderBillingAddress),
  657. $this->prepareAddress($orderShippingAddress),
  658. ],
  659. OrderPayment::class => [
  660. $this->prepareOrderPayment($orderPayment),
  661. ],
  662. Invoice::class => [
  663. $this->prepareInvoice($order, $orderItem),
  664. ],
  665. ]);
  666. Mail::assertQueued(AdminInvoicedNotification::class);
  667. Mail::assertQueued(ShopInvoicedNotification::class);
  668. Mail::assertQueuedCount(2);
  669. });
  670. it('should return the view page of the invoice', function () {
  671. // Arrange.
  672. $product = (new ProductFaker([
  673. 'attributes' => [
  674. 5 => 'new',
  675. ],
  676. 'attribute_value' => [
  677. 'new' => [
  678. 'boolean_value' => true,
  679. ],
  680. ],
  681. ]))
  682. ->getSimpleProductFactory()
  683. ->create();
  684. $customer = Customer::factory()->create();
  685. $cart = Cart::factory()->create([
  686. 'customer_id' => $customer->id,
  687. 'customer_first_name' => $customer->first_name,
  688. 'customer_last_name' => $customer->last_name,
  689. 'customer_email' => $customer->email,
  690. 'is_guest' => 0,
  691. ]);
  692. $additional = [
  693. 'product_id' => $product->id,
  694. 'rating' => '0',
  695. 'is_buy_now' => '0',
  696. 'quantity' => '1',
  697. ];
  698. $cartItem = CartItem::factory()->create([
  699. 'cart_id' => $cart->id,
  700. 'product_id' => $product->id,
  701. 'sku' => $product->sku,
  702. 'quantity' => $additional['quantity'],
  703. 'name' => $product->name,
  704. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  705. 'base_price' => $price,
  706. 'total' => $convertedPrice * $additional['quantity'],
  707. 'base_total' => $price * $additional['quantity'],
  708. 'weight' => $product->weight ?? 0,
  709. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  710. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  711. 'type' => $product->type,
  712. 'additional' => $additional,
  713. ]);
  714. $customerAddress = CustomerAddress::factory()->create([
  715. 'cart_id' => $cart->id,
  716. 'customer_id' => $customer->id,
  717. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  718. ]);
  719. $cartBillingAddress = CartAddress::factory()->create([
  720. 'cart_id' => $cart->id,
  721. 'customer_id' => $customer->id,
  722. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  723. ]);
  724. $cartShippingAddress = CartAddress::factory()->create([
  725. 'cart_id' => $cart->id,
  726. 'customer_id' => $customer->id,
  727. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  728. ]);
  729. $cartPayment = CartPayment::factory()->create([
  730. 'cart_id' => $cart->id,
  731. 'method' => $paymentMethod = 'cashondelivery',
  732. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  733. ]);
  734. $cartShippingRate = CartShippingRate::factory()->create([
  735. 'carrier' => 'free',
  736. 'carrier_title' => 'Free shipping',
  737. 'method' => 'free_free',
  738. 'method_title' => 'Free Shipping',
  739. 'method_description' => 'Free Shipping',
  740. 'cart_address_id' => $cartShippingAddress->id,
  741. ]);
  742. $order = Order::factory()->create([
  743. 'cart_id' => $cart->id,
  744. 'customer_id' => $customer->id,
  745. 'customer_email' => $customer->email,
  746. 'customer_first_name' => $customer->first_name,
  747. 'customer_last_name' => $customer->last_name,
  748. 'status' => 'processing',
  749. 'sub_total_invoiced' => $product->price,
  750. 'base_sub_total_invoiced' => $product->price,
  751. ]);
  752. $orderItem = OrderItem::factory()->create([
  753. 'product_id' => $product->id,
  754. 'order_id' => $order->id,
  755. 'sku' => $product->sku,
  756. 'type' => $product->type,
  757. 'name' => $product->name,
  758. 'qty_invoiced' => 1,
  759. 'base_total_invoiced' => $product->price,
  760. ]);
  761. $orderBillingAddress = OrderAddress::factory()->create([
  762. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  763. 'cart_id' => $cart->id,
  764. 'customer_id' => $customer->id,
  765. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  766. 'order_id' => $order->id,
  767. ]);
  768. $orderShippingAddress = OrderAddress::factory()->create([
  769. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  770. 'cart_id' => $cart->id,
  771. 'customer_id' => $customer->id,
  772. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  773. 'order_id' => $order->id,
  774. ]);
  775. $orderPayment = OrderPayment::factory()->create([
  776. 'order_id' => $order->id,
  777. 'method' => 'cashondelivery',
  778. ]);
  779. $invoice = Invoice::factory()->create([
  780. 'order_id' => $order->id,
  781. 'state' => 'paid',
  782. 'total_qty' => 1,
  783. 'base_currency_code' => $order->base_currency_code,
  784. 'channel_currency_code' => $order->channel_currency_code,
  785. 'order_currency_code' => $order->order_currency_code,
  786. 'email_sent' => 1,
  787. 'discount_amount' => 0,
  788. 'base_discount_amount' => 0,
  789. 'sub_total' => $orderItem->base_price,
  790. 'base_sub_total' => $orderItem->base_price,
  791. 'grand_total' => $orderItem->price,
  792. 'base_grand_total' => $orderItem->price,
  793. ]);
  794. $invoiceItem = InvoiceItem::factory()->create([
  795. 'invoice_id' => $invoice->id,
  796. 'order_item_id' => $orderItem->id,
  797. 'name' => $orderItem->name,
  798. 'sku' => $orderItem->sku,
  799. 'qty' => 1,
  800. 'price' => $orderItem->price,
  801. 'base_price' => $orderItem->base_price,
  802. 'total' => $orderItem->price,
  803. 'base_total' => $orderItem->base_price,
  804. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  805. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  806. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  807. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  808. 'product_id' => $orderItem->product_id,
  809. 'product_type' => $orderItem->product_type,
  810. 'additional' => $orderItem->additional,
  811. ]);
  812. $orderTransaction = OrderTransaction::factory()->create([
  813. 'transaction_id' => md5(uniqid()),
  814. 'type' => 'cashondelivery',
  815. 'payment_method' => 'cashondelivery',
  816. 'status' => $invoice->state,
  817. 'order_id' => $invoice->order->id,
  818. 'invoice_id' => $invoice->id,
  819. 'amount' => $invoice->grand_total,
  820. ]);
  821. // Act and Assert.
  822. $this->loginAsAdmin();
  823. get(route('admin.sales.invoices.view', $invoice->id))
  824. ->assertOk()
  825. ->assertSeeText(trans('admin::app.sales.invoices.view.title', ['invoice_id' => $invoice->increment_id ?? $invoice->id]))
  826. ->assertSeeText(trans('admin::app.account.edit.back-btn'))
  827. ->assertSeeText(trans('admin::app.sales.invoices.view.print'));
  828. $cart->refresh();
  829. $cartItem->refresh();
  830. $cartBillingAddress->refresh();
  831. $cartShippingAddress->refresh();
  832. $orderBillingAddress->refresh();
  833. $orderShippingAddress->refresh();
  834. $order->refresh();
  835. $orderItem->refresh();
  836. $invoiceItem->refresh();
  837. $orderTransaction->refresh();
  838. $this->assertModelWise([
  839. Cart::class => [
  840. $this->prepareCart($cart),
  841. ],
  842. CartItem::class => [
  843. $this->prepareCartItem($cartItem),
  844. ],
  845. CartPayment::class => [
  846. $this->prepareCartPayment($cartPayment),
  847. ],
  848. CartAddress::class => [
  849. $this->prepareAddress($cartBillingAddress),
  850. ],
  851. CartAddress::class => [
  852. $this->prepareAddress($cartShippingAddress),
  853. ],
  854. CartShippingRate::class => [
  855. $this->prepareCartShippingRate($cartShippingRate),
  856. ],
  857. CustomerAddress::class => [
  858. $this->prepareAddress($customerAddress),
  859. ],
  860. Order::class => [
  861. $this->prepareOrder($order),
  862. ],
  863. OrderItem::class => [
  864. $this->prepareOrderItem($orderItem),
  865. ],
  866. OrderAddress::class => [
  867. $this->prepareAddress($orderBillingAddress),
  868. $this->prepareAddress($orderShippingAddress),
  869. ],
  870. OrderPayment::class => [
  871. $this->prepareOrderPayment($orderPayment),
  872. ],
  873. Invoice::class => [
  874. $this->prepareInvoice($order, $orderItem),
  875. ],
  876. InvoiceItem::class => [
  877. $this->prepareInvoiceItem($invoiceItem),
  878. ],
  879. OrderTransaction::class => [
  880. $this->prepareOrderTransaction($orderTransaction),
  881. ],
  882. ]);
  883. });
  884. it('should send duplicate mail to provided email address', function () {
  885. // Arrange.
  886. Mail::fake();
  887. $product = (new ProductFaker([
  888. 'attributes' => [
  889. 5 => 'new',
  890. ],
  891. 'attribute_value' => [
  892. 'new' => [
  893. 'boolean_value' => true,
  894. ],
  895. ],
  896. ]))
  897. ->getSimpleProductFactory()
  898. ->create();
  899. $customer = Customer::factory()->create();
  900. $cart = Cart::factory()->create([
  901. 'customer_id' => $customer->id,
  902. 'customer_first_name' => $customer->first_name,
  903. 'customer_last_name' => $customer->last_name,
  904. 'customer_email' => $customer->email,
  905. 'is_guest' => 0,
  906. ]);
  907. $additional = [
  908. 'product_id' => $product->id,
  909. 'rating' => '0',
  910. 'is_buy_now' => '0',
  911. 'quantity' => '1',
  912. ];
  913. $cartItem = CartItem::factory()->create([
  914. 'cart_id' => $cart->id,
  915. 'product_id' => $product->id,
  916. 'sku' => $product->sku,
  917. 'quantity' => $additional['quantity'],
  918. 'name' => $product->name,
  919. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  920. 'base_price' => $price,
  921. 'total' => $convertedPrice * $additional['quantity'],
  922. 'base_total' => $price * $additional['quantity'],
  923. 'weight' => $product->weight ?? 0,
  924. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  925. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  926. 'type' => $product->type,
  927. 'additional' => $additional,
  928. ]);
  929. $customerAddress = CustomerAddress::factory()->create([
  930. 'cart_id' => $cart->id,
  931. 'customer_id' => $customer->id,
  932. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  933. ]);
  934. $cartBillingAddress = CartAddress::factory()->create([
  935. 'cart_id' => $cart->id,
  936. 'customer_id' => $customer->id,
  937. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  938. ]);
  939. $cartShippingAddress = CartAddress::factory()->create([
  940. 'cart_id' => $cart->id,
  941. 'customer_id' => $customer->id,
  942. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  943. ]);
  944. $cartPayment = CartPayment::factory()->create([
  945. 'cart_id' => $cart->id,
  946. 'method' => $paymentMethod = 'cashondelivery',
  947. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  948. ]);
  949. $cartShippingRate = CartShippingRate::factory()->create([
  950. 'carrier' => 'free',
  951. 'carrier_title' => 'Free shipping',
  952. 'method' => 'free_free',
  953. 'method_title' => 'Free Shipping',
  954. 'method_description' => 'Free Shipping',
  955. 'cart_address_id' => $cartShippingAddress->id,
  956. ]);
  957. $order = Order::factory()->create([
  958. 'cart_id' => $cart->id,
  959. 'customer_id' => $customer->id,
  960. 'customer_email' => $customer->email,
  961. 'customer_first_name' => $customer->first_name,
  962. 'customer_last_name' => $customer->last_name,
  963. 'status' => 'processing',
  964. 'sub_total_invoiced' => $product->price,
  965. 'base_sub_total_invoiced' => $product->price,
  966. ]);
  967. $orderItem = OrderItem::factory()->create([
  968. 'product_id' => $product->id,
  969. 'order_id' => $order->id,
  970. 'sku' => $product->sku,
  971. 'type' => $product->type,
  972. 'name' => $product->name,
  973. 'qty_invoiced' => 1,
  974. 'base_total_invoiced' => $product->price,
  975. ]);
  976. $orderBillingAddress = OrderAddress::factory()->create([
  977. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  978. 'cart_id' => $cart->id,
  979. 'customer_id' => $customer->id,
  980. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  981. 'order_id' => $order->id,
  982. ]);
  983. $orderShippingAddress = OrderAddress::factory()->create([
  984. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  985. 'cart_id' => $cart->id,
  986. 'customer_id' => $customer->id,
  987. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  988. 'order_id' => $order->id,
  989. ]);
  990. $orderPayment = OrderPayment::factory()->create([
  991. 'order_id' => $order->id,
  992. 'method' => 'cashondelivery',
  993. ]);
  994. $invoice = Invoice::factory()->create([
  995. 'order_id' => $order->id,
  996. 'state' => 'paid',
  997. 'total_qty' => 1,
  998. 'base_currency_code' => $order->base_currency_code,
  999. 'channel_currency_code' => $order->channel_currency_code,
  1000. 'order_currency_code' => $order->order_currency_code,
  1001. 'email_sent' => 1,
  1002. 'discount_amount' => 0,
  1003. 'base_discount_amount' => 0,
  1004. 'sub_total' => $orderItem->base_price,
  1005. 'base_sub_total' => $orderItem->base_price,
  1006. 'grand_total' => $orderItem->price,
  1007. 'base_grand_total' => $orderItem->price,
  1008. ]);
  1009. $invoiceItem = InvoiceItem::factory()->create([
  1010. 'invoice_id' => $invoice->id,
  1011. 'order_item_id' => $orderItem->id,
  1012. 'name' => $orderItem->name,
  1013. 'sku' => $orderItem->sku,
  1014. 'qty' => 1,
  1015. 'price' => $orderItem->price,
  1016. 'base_price' => $orderItem->base_price,
  1017. 'total' => $orderItem->price,
  1018. 'base_total' => $orderItem->base_price,
  1019. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1020. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1021. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1022. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1023. 'product_id' => $orderItem->product_id,
  1024. 'product_type' => $orderItem->product_type,
  1025. 'additional' => $orderItem->additional,
  1026. ]);
  1027. $orderTransaction = OrderTransaction::factory()->create([
  1028. 'transaction_id' => md5(uniqid()),
  1029. 'type' => 'cashondelivery',
  1030. 'payment_method' => 'cashondelivery',
  1031. 'status' => $invoice->state,
  1032. 'order_id' => $invoice->order->id,
  1033. 'invoice_id' => $invoice->id,
  1034. 'amount' => $invoice->grand_total,
  1035. ]);
  1036. // Act and Assert.
  1037. $this->loginAsAdmin();
  1038. postJson(route('admin.sales.invoices.send_duplicate_email', $invoice->id), [
  1039. 'email' => fake()->email(),
  1040. ])
  1041. ->assertRedirect(route('admin.sales.invoices.view', $invoice->id))
  1042. ->isRedirection();
  1043. $cart->refresh();
  1044. $cartItem->refresh();
  1045. $cartBillingAddress->refresh();
  1046. $cartShippingAddress->refresh();
  1047. $orderBillingAddress->refresh();
  1048. $orderShippingAddress->refresh();
  1049. $order->refresh();
  1050. $orderItem->refresh();
  1051. $invoiceItem->refresh();
  1052. $orderTransaction->refresh();
  1053. $this->assertModelWise([
  1054. Cart::class => [
  1055. $this->prepareCart($cart),
  1056. ],
  1057. CartItem::class => [
  1058. $this->prepareCartItem($cartItem),
  1059. ],
  1060. CartPayment::class => [
  1061. $this->prepareCartPayment($cartPayment),
  1062. ],
  1063. CartAddress::class => [
  1064. $this->prepareAddress($cartBillingAddress),
  1065. ],
  1066. CartAddress::class => [
  1067. $this->prepareAddress($cartShippingAddress),
  1068. ],
  1069. CartShippingRate::class => [
  1070. $this->prepareCartShippingRate($cartShippingRate),
  1071. ],
  1072. CustomerAddress::class => [
  1073. $this->prepareAddress($customerAddress),
  1074. ],
  1075. Order::class => [
  1076. $this->prepareOrder($order),
  1077. ],
  1078. OrderItem::class => [
  1079. $this->prepareOrderItem($orderItem),
  1080. ],
  1081. OrderAddress::class => [
  1082. $this->prepareAddress($orderBillingAddress),
  1083. $this->prepareAddress($orderShippingAddress),
  1084. ],
  1085. OrderPayment::class => [
  1086. $this->prepareOrderPayment($orderPayment),
  1087. ],
  1088. Invoice::class => [
  1089. $this->prepareInvoice($order, $orderItem),
  1090. ],
  1091. InvoiceItem::class => [
  1092. $this->prepareInvoiceItem($invoiceItem),
  1093. ],
  1094. OrderTransaction::class => [
  1095. $this->prepareOrderTransaction($orderTransaction),
  1096. ],
  1097. ]);
  1098. Mail::assertQueued(ShopInvoicedNotification::class);
  1099. Mail::assertQueuedCount(1);
  1100. });
  1101. it('should print/download the invoice', function () {
  1102. // Arrange.
  1103. $product = (new ProductFaker([
  1104. 'attributes' => [
  1105. 5 => 'new',
  1106. ],
  1107. 'attribute_value' => [
  1108. 'new' => [
  1109. 'boolean_value' => true,
  1110. ],
  1111. ],
  1112. ]))
  1113. ->getSimpleProductFactory()
  1114. ->create();
  1115. $customer = Customer::factory()->create();
  1116. $cart = Cart::factory()->create([
  1117. 'customer_id' => $customer->id,
  1118. 'customer_first_name' => $customer->first_name,
  1119. 'customer_last_name' => $customer->last_name,
  1120. 'customer_email' => $customer->email,
  1121. 'is_guest' => 0,
  1122. ]);
  1123. $additional = [
  1124. 'product_id' => $product->id,
  1125. 'rating' => '0',
  1126. 'is_buy_now' => '0',
  1127. 'quantity' => '1',
  1128. ];
  1129. $cartItem = CartItem::factory()->create([
  1130. 'cart_id' => $cart->id,
  1131. 'product_id' => $product->id,
  1132. 'sku' => $product->sku,
  1133. 'quantity' => $additional['quantity'],
  1134. 'name' => $product->name,
  1135. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1136. 'base_price' => $price,
  1137. 'total' => $convertedPrice * $additional['quantity'],
  1138. 'base_total' => $price * $additional['quantity'],
  1139. 'weight' => $product->weight ?? 0,
  1140. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1141. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1142. 'type' => $product->type,
  1143. 'additional' => $additional,
  1144. ]);
  1145. $customerAddress = CustomerAddress::factory()->create([
  1146. 'cart_id' => $cart->id,
  1147. 'customer_id' => $customer->id,
  1148. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1149. ]);
  1150. $cartBillingAddress = CartAddress::factory()->create([
  1151. 'cart_id' => $cart->id,
  1152. 'customer_id' => $customer->id,
  1153. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1154. ]);
  1155. $cartShippingAddress = CartAddress::factory()->create([
  1156. 'cart_id' => $cart->id,
  1157. 'customer_id' => $customer->id,
  1158. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1159. ]);
  1160. $cartPayment = CartPayment::factory()->create([
  1161. 'cart_id' => $cart->id,
  1162. 'method' => $paymentMethod = 'cashondelivery',
  1163. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1164. ]);
  1165. $cartShippingRate = CartShippingRate::factory()->create([
  1166. 'carrier' => 'free',
  1167. 'carrier_title' => 'Free shipping',
  1168. 'method' => 'free_free',
  1169. 'method_title' => 'Free Shipping',
  1170. 'method_description' => 'Free Shipping',
  1171. 'cart_address_id' => $cartShippingAddress->id,
  1172. ]);
  1173. $order = Order::factory()->create([
  1174. 'cart_id' => $cart->id,
  1175. 'customer_id' => $customer->id,
  1176. 'customer_email' => $customer->email,
  1177. 'customer_first_name' => $customer->first_name,
  1178. 'customer_last_name' => $customer->last_name,
  1179. 'status' => 'processing',
  1180. 'sub_total_invoiced' => $product->price,
  1181. 'base_sub_total_invoiced' => $product->price,
  1182. ]);
  1183. $orderItem = OrderItem::factory()->create([
  1184. 'product_id' => $product->id,
  1185. 'order_id' => $order->id,
  1186. 'sku' => $product->sku,
  1187. 'type' => $product->type,
  1188. 'name' => $product->name,
  1189. 'qty_invoiced' => 1,
  1190. 'base_total_invoiced' => $product->price,
  1191. ]);
  1192. $orderBillingAddress = OrderAddress::factory()->create([
  1193. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1194. 'cart_id' => $cart->id,
  1195. 'customer_id' => $customer->id,
  1196. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1197. 'order_id' => $order->id,
  1198. ]);
  1199. $orderShippingAddress = OrderAddress::factory()->create([
  1200. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1201. 'cart_id' => $cart->id,
  1202. 'customer_id' => $customer->id,
  1203. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1204. 'order_id' => $order->id,
  1205. ]);
  1206. $orderPayment = OrderPayment::factory()->create([
  1207. 'order_id' => $order->id,
  1208. 'method' => 'cashondelivery',
  1209. ]);
  1210. $invoice = Invoice::factory()->create([
  1211. 'order_id' => $order->id,
  1212. 'state' => 'paid',
  1213. 'total_qty' => 1,
  1214. 'base_currency_code' => $order->base_currency_code,
  1215. 'channel_currency_code' => $order->channel_currency_code,
  1216. 'order_currency_code' => $order->order_currency_code,
  1217. 'email_sent' => 1,
  1218. 'discount_amount' => 0,
  1219. 'base_discount_amount' => 0,
  1220. 'sub_total' => $orderItem->base_price,
  1221. 'base_sub_total' => $orderItem->base_price,
  1222. 'grand_total' => $orderItem->price,
  1223. 'base_grand_total' => $orderItem->price,
  1224. ]);
  1225. $invoiceItem = InvoiceItem::factory()->create([
  1226. 'invoice_id' => $invoice->id,
  1227. 'order_item_id' => $orderItem->id,
  1228. 'name' => $orderItem->name,
  1229. 'sku' => $orderItem->sku,
  1230. 'qty' => 1,
  1231. 'price' => $orderItem->price,
  1232. 'base_price' => $orderItem->base_price,
  1233. 'total' => $orderItem->price,
  1234. 'base_total' => $orderItem->base_price,
  1235. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1236. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1237. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1238. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1239. 'product_id' => $orderItem->product_id,
  1240. 'product_type' => $orderItem->product_type,
  1241. 'additional' => $orderItem->additional,
  1242. ]);
  1243. $orderTransaction = OrderTransaction::factory()->create([
  1244. 'transaction_id' => md5(uniqid()),
  1245. 'type' => 'cashondelivery',
  1246. 'payment_method' => 'cashondelivery',
  1247. 'status' => $invoice->state,
  1248. 'order_id' => $invoice->order->id,
  1249. 'invoice_id' => $invoice->id,
  1250. 'amount' => $invoice->grand_total,
  1251. ]);
  1252. $fileName = 'invoice-'.$invoice->created_at->format('d-m-Y').'.pdf';
  1253. // Act and Assert.
  1254. $this->loginAsAdmin();
  1255. get(route('admin.sales.invoices.print', $invoice->id))
  1256. ->assertOk()
  1257. ->assertHeader('Content-Type', 'application/pdf')
  1258. ->assertHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"');
  1259. $cart->refresh();
  1260. $cartItem->refresh();
  1261. $cartBillingAddress->refresh();
  1262. $cartShippingAddress->refresh();
  1263. $orderBillingAddress->refresh();
  1264. $orderShippingAddress->refresh();
  1265. $order->refresh();
  1266. $orderItem->refresh();
  1267. $invoiceItem->refresh();
  1268. $orderTransaction->refresh();
  1269. $this->assertModelWise([
  1270. Cart::class => [
  1271. $this->prepareCart($cart),
  1272. ],
  1273. CartItem::class => [
  1274. $this->prepareCartItem($cartItem),
  1275. ],
  1276. CartPayment::class => [
  1277. $this->prepareCartPayment($cartPayment),
  1278. ],
  1279. CartAddress::class => [
  1280. $this->prepareAddress($cartBillingAddress),
  1281. ],
  1282. CartAddress::class => [
  1283. $this->prepareAddress($cartShippingAddress),
  1284. ],
  1285. CartShippingRate::class => [
  1286. $this->prepareCartShippingRate($cartShippingRate),
  1287. ],
  1288. CustomerAddress::class => [
  1289. $this->prepareAddress($customerAddress),
  1290. ],
  1291. Order::class => [
  1292. $this->prepareOrder($order),
  1293. ],
  1294. OrderItem::class => [
  1295. $this->prepareOrderItem($orderItem),
  1296. ],
  1297. OrderAddress::class => [
  1298. $this->prepareAddress($orderBillingAddress),
  1299. $this->prepareAddress($orderShippingAddress),
  1300. ],
  1301. OrderPayment::class => [
  1302. $this->prepareOrderPayment($orderPayment),
  1303. ],
  1304. Invoice::class => [
  1305. $this->prepareInvoice($order, $orderItem),
  1306. ],
  1307. InvoiceItem::class => [
  1308. $this->prepareInvoiceItem($invoiceItem),
  1309. ],
  1310. OrderTransaction::class => [
  1311. $this->prepareOrderTransaction($orderTransaction),
  1312. ],
  1313. ]);
  1314. });