DashboardTest.php 54 KB

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