SalesReportTest.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395
  1. <?php
  2. use Carbon\Carbon;
  3. use Illuminate\Support\Arr;
  4. use Webkul\Checkout\Models\Cart;
  5. use Webkul\Checkout\Models\CartAddress;
  6. use Webkul\Checkout\Models\CartItem;
  7. use Webkul\Checkout\Models\CartPayment;
  8. use Webkul\Checkout\Models\CartShippingRate;
  9. use Webkul\Customer\Models\Customer;
  10. use Webkul\Customer\Models\CustomerAddress;
  11. use Webkul\Faker\Helpers\Product as ProductFaker;
  12. use Webkul\Sales\Models\Invoice;
  13. use Webkul\Sales\Models\InvoiceItem;
  14. use Webkul\Sales\Models\Order;
  15. use Webkul\Sales\Models\OrderAddress;
  16. use Webkul\Sales\Models\OrderItem;
  17. use Webkul\Sales\Models\OrderPayment;
  18. use Webkul\Sales\Models\OrderTransaction;
  19. use function Pest\Laravel\get;
  20. it('should return the sales index page', function () {
  21. // Act and Assert.
  22. $this->loginAsAdmin();
  23. get(route('admin.reporting.sales.index'))
  24. ->assertOk()
  25. ->assertSeeText(trans('admin::app.reporting.sales.index.title'))
  26. ->assertSeeText(trans('admin::app.reporting.sales.index.refunds'))
  27. ->assertSeeText(trans('admin::app.reporting.sales.index.total-sales'));
  28. });
  29. it('should returns the sales stats', function () {
  30. // Arrange.
  31. $product = (new ProductFaker([
  32. 'attributes' => [
  33. 5 => 'new',
  34. ],
  35. 'attribute_value' => [
  36. 'new' => [
  37. 'boolean_value' => true,
  38. ],
  39. ],
  40. ]))
  41. ->getSimpleProductFactory()
  42. ->create();
  43. $customer = Customer::factory()->create();
  44. $cart = Cart::factory()->create([
  45. 'customer_id' => $customer->id,
  46. 'customer_first_name' => $customer->first_name,
  47. 'customer_last_name' => $customer->last_name,
  48. 'customer_email' => $customer->email,
  49. 'is_guest' => 0,
  50. ]);
  51. $additional = [
  52. 'product_id' => $product->id,
  53. 'rating' => '0',
  54. 'is_buy_now' => '0',
  55. 'quantity' => '1',
  56. ];
  57. $cartItem = CartItem::factory()->create([
  58. 'cart_id' => $cart->id,
  59. 'product_id' => $product->id,
  60. 'sku' => $product->sku,
  61. 'quantity' => $additional['quantity'],
  62. 'name' => $product->name,
  63. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  64. 'base_price' => $price,
  65. 'total' => $convertedPrice * $additional['quantity'],
  66. 'base_total' => $price * $additional['quantity'],
  67. 'weight' => $product->weight ?? 0,
  68. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  69. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  70. 'type' => $product->type,
  71. 'additional' => $additional,
  72. ]);
  73. $customerAddress = CustomerAddress::factory()->create([
  74. 'cart_id' => $cart->id,
  75. 'customer_id' => $customer->id,
  76. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  77. ]);
  78. $cartBillingAddress = CartAddress::factory()->create([
  79. 'cart_id' => $cart->id,
  80. 'customer_id' => $customer->id,
  81. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  82. ]);
  83. $cartShippingAddress = CartAddress::factory()->create([
  84. 'cart_id' => $cart->id,
  85. 'customer_id' => $customer->id,
  86. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  87. ]);
  88. $cartPayment = CartPayment::factory()->create([
  89. 'cart_id' => $cart->id,
  90. 'method' => $paymentMethod = 'cashondelivery',
  91. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  92. ]);
  93. $cartShippingRate = CartShippingRate::factory()->create([
  94. 'carrier' => 'free',
  95. 'carrier_title' => 'Free shipping',
  96. 'method' => 'free_free',
  97. 'method_title' => 'Free Shipping',
  98. 'method_description' => 'Free Shipping',
  99. 'cart_address_id' => $cartShippingAddress->id,
  100. ]);
  101. $order = Order::factory()->create([
  102. 'cart_id' => $cart->id,
  103. 'customer_id' => $customer->id,
  104. 'customer_email' => $customer->email,
  105. 'customer_first_name' => $customer->first_name,
  106. 'customer_last_name' => $customer->last_name,
  107. 'status' => 'processing',
  108. 'sub_total_invoiced' => $product->price,
  109. 'base_sub_total_invoiced' => $product->price,
  110. ]);
  111. $orderItem = OrderItem::factory()->create([
  112. 'product_id' => $product->id,
  113. 'order_id' => $order->id,
  114. 'sku' => $product->sku,
  115. 'type' => $product->type,
  116. 'name' => $product->name,
  117. 'qty_invoiced' => 1,
  118. 'base_total_invoiced' => $product->price,
  119. ]);
  120. $orderBillingAddress = OrderAddress::factory()->create([
  121. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  122. 'cart_id' => $cart->id,
  123. 'customer_id' => $customer->id,
  124. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  125. 'order_id' => $order->id,
  126. ]);
  127. $orderShippingAddress = OrderAddress::factory()->create([
  128. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  129. 'cart_id' => $cart->id,
  130. 'customer_id' => $customer->id,
  131. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  132. 'order_id' => $order->id,
  133. ]);
  134. $orderPayment = OrderPayment::factory()->create([
  135. 'order_id' => $order->id,
  136. 'method' => 'cashondelivery',
  137. ]);
  138. $invoice = Invoice::factory()->create([
  139. 'order_id' => $order->id,
  140. 'state' => 'paid',
  141. 'total_qty' => 1,
  142. 'base_currency_code' => $order->base_currency_code,
  143. 'channel_currency_code' => $order->channel_currency_code,
  144. 'order_currency_code' => $order->order_currency_code,
  145. 'email_sent' => 1,
  146. 'discount_amount' => 0,
  147. 'base_discount_amount' => 0,
  148. 'sub_total' => $orderItem->base_price,
  149. 'base_sub_total' => $orderItem->base_price,
  150. 'grand_total' => $orderItem->price,
  151. 'base_grand_total' => $orderItem->price,
  152. ]);
  153. $invoiceItem = InvoiceItem::factory()->create([
  154. 'invoice_id' => $invoice->id,
  155. 'order_item_id' => $orderItem->id,
  156. 'name' => $orderItem->name,
  157. 'sku' => $orderItem->sku,
  158. 'qty' => 1,
  159. 'price' => $orderItem->price,
  160. 'base_price' => $orderItem->base_price,
  161. 'total' => $orderItem->price,
  162. 'base_total' => $orderItem->base_price,
  163. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  164. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  165. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  166. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  167. 'product_id' => $orderItem->product_id,
  168. 'product_type' => $orderItem->product_type,
  169. 'additional' => $orderItem->additional,
  170. ]);
  171. $orderTransaction = OrderTransaction::factory()->create([
  172. 'transaction_id' => md5(uniqid()),
  173. 'type' => 'cashondelivery',
  174. 'payment_method' => 'cashondelivery',
  175. 'status' => 'paid',
  176. 'status' => $invoice->state,
  177. 'order_id' => $invoice->order->id,
  178. 'invoice_id' => $invoice->id,
  179. 'amount' => $invoice->grand_total,
  180. ]);
  181. // Act and Assert.
  182. $this->loginAsAdmin();
  183. $response = get(route('admin.reporting.sales.stats', [
  184. 'type' => 'total-sales',
  185. ]))
  186. ->assertOk()
  187. ->assertJsonPath('statistics.sales.progress', 100)
  188. ->assertJsonPath('statistics.over_time.current.30.count', 1)
  189. ->assertJsonPath('statistics.sales.formatted_total', core()->formatBasePrice($order->grand_total));
  190. $this->assertPrice($order->grand_total, $response->json('statistics.sales.current'));
  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. ]);
  243. });
  244. it('should returns the abandoned carts stats', function () {
  245. // Arrange.
  246. $product = (new ProductFaker([
  247. 'attributes' => [
  248. 5 => 'new',
  249. ],
  250. 'attribute_value' => [
  251. 'new' => [
  252. 'boolean_value' => true,
  253. ],
  254. ],
  255. ]))
  256. ->getSimpleProductFactory()
  257. ->create();
  258. $additional = [
  259. 'product_id' => $product->id,
  260. 'rating' => '0',
  261. 'is_buy_now' => '0',
  262. 'quantity' => '1',
  263. ];
  264. $customer = Customer::factory()->create();
  265. $cart = Cart::factory()->create([
  266. 'customer_id' => $customer->id,
  267. 'customer_first_name' => $customer->first_name,
  268. 'customer_last_name' => $customer->last_name,
  269. 'customer_email' => $customer->email,
  270. 'created_at' => Carbon::now()->subMonth()->toDateString(),
  271. 'is_guest' => 0,
  272. ]);
  273. CartItem::factory()->create([
  274. 'cart_id' => $cart->id,
  275. 'product_id' => $product->id,
  276. 'sku' => $product->sku,
  277. 'quantity' => $additional['quantity'],
  278. 'name' => $product->name,
  279. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  280. 'base_price' => $price,
  281. 'total' => $convertedPrice * $additional['quantity'],
  282. 'base_total' => $price * $additional['quantity'],
  283. 'weight' => $product->weight ?? 0,
  284. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  285. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  286. 'type' => $product->type,
  287. 'additional' => $additional,
  288. ]);
  289. // Act and Assert.
  290. $this->loginAsAdmin();
  291. get(route('admin.reporting.sales.stats', [
  292. 'start' => Carbon::now()->copy()->subMonth()->toDateString(),
  293. 'type' => 'abandoned-carts',
  294. 'end' => Carbon::now()->addMonths(5)->toDateString(),
  295. ]))
  296. ->assertOk()
  297. ->assertJsonPath('statistics.carts.current', 1)
  298. ->assertJsonPath('statistics.rate.current', 100)
  299. ->assertJsonPath('statistics.rate.progress', 100)
  300. ->assertJsonPath('statistics.products.0.id', $product->id)
  301. ->assertJsonPath('statistics.products.0.name', $product->name)
  302. ->assertJsonPath('statistics.products.0.count', 1)
  303. ->assertJsonPath('statistics.products.0.progress', 100);
  304. });
  305. it('should returns the total orders stats', function () {
  306. // Arrange.
  307. $product = (new ProductFaker([
  308. 'attributes' => [
  309. 5 => 'new',
  310. ],
  311. 'attribute_value' => [
  312. 'new' => [
  313. 'boolean_value' => true,
  314. ],
  315. ],
  316. ]))
  317. ->getSimpleProductFactory()
  318. ->create();
  319. $customer = Customer::factory()->create();
  320. $cart = Cart::factory()->create([
  321. 'customer_id' => $customer->id,
  322. 'customer_first_name' => $customer->first_name,
  323. 'customer_last_name' => $customer->last_name,
  324. 'customer_email' => $customer->email,
  325. 'is_guest' => 0,
  326. ]);
  327. $additional = [
  328. 'product_id' => $product->id,
  329. 'rating' => '0',
  330. 'is_buy_now' => '0',
  331. 'quantity' => '1',
  332. ];
  333. $cartItem = CartItem::factory()->create([
  334. 'cart_id' => $cart->id,
  335. 'product_id' => $product->id,
  336. 'sku' => $product->sku,
  337. 'quantity' => $additional['quantity'],
  338. 'name' => $product->name,
  339. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  340. 'base_price' => $price,
  341. 'total' => $convertedPrice * $additional['quantity'],
  342. 'base_total' => $price * $additional['quantity'],
  343. 'weight' => $product->weight ?? 0,
  344. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  345. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  346. 'type' => $product->type,
  347. 'additional' => $additional,
  348. ]);
  349. $customerAddress = CustomerAddress::factory()->create([
  350. 'cart_id' => $cart->id,
  351. 'customer_id' => $customer->id,
  352. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  353. ]);
  354. $cartBillingAddress = CartAddress::factory()->create([
  355. 'cart_id' => $cart->id,
  356. 'customer_id' => $customer->id,
  357. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  358. ]);
  359. $cartShippingAddress = CartAddress::factory()->create([
  360. 'cart_id' => $cart->id,
  361. 'customer_id' => $customer->id,
  362. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  363. ]);
  364. $cartPayment = CartPayment::factory()->create([
  365. 'cart_id' => $cart->id,
  366. 'method' => $paymentMethod = 'cashondelivery',
  367. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  368. ]);
  369. $cartShippingRate = CartShippingRate::factory()->create([
  370. 'carrier' => 'free',
  371. 'carrier_title' => 'Free shipping',
  372. 'method' => 'free_free',
  373. 'method_title' => 'Free Shipping',
  374. 'method_description' => 'Free Shipping',
  375. 'cart_address_id' => $cartShippingAddress->id,
  376. ]);
  377. $order = Order::factory()->create([
  378. 'cart_id' => $cart->id,
  379. 'customer_id' => $customer->id,
  380. 'customer_email' => $customer->email,
  381. 'customer_first_name' => $customer->first_name,
  382. 'customer_last_name' => $customer->last_name,
  383. 'status' => 'processing',
  384. 'sub_total_invoiced' => $product->price,
  385. 'base_sub_total_invoiced' => $product->price,
  386. ]);
  387. $orderItem = OrderItem::factory()->create([
  388. 'product_id' => $product->id,
  389. 'order_id' => $order->id,
  390. 'sku' => $product->sku,
  391. 'type' => $product->type,
  392. 'name' => $product->name,
  393. 'qty_invoiced' => 1,
  394. 'base_total_invoiced' => $product->price,
  395. ]);
  396. $orderBillingAddress = OrderAddress::factory()->create([
  397. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  398. 'cart_id' => $cart->id,
  399. 'customer_id' => $customer->id,
  400. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  401. 'order_id' => $order->id,
  402. ]);
  403. $orderShippingAddress = OrderAddress::factory()->create([
  404. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  405. 'cart_id' => $cart->id,
  406. 'customer_id' => $customer->id,
  407. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  408. 'order_id' => $order->id,
  409. ]);
  410. $orderPayment = OrderPayment::factory()->create([
  411. 'order_id' => $order->id,
  412. 'method' => 'cashondelivery',
  413. ]);
  414. $invoice = Invoice::factory()->create([
  415. 'order_id' => $order->id,
  416. 'state' => 'paid',
  417. 'total_qty' => 1,
  418. 'base_currency_code' => $order->base_currency_code,
  419. 'channel_currency_code' => $order->channel_currency_code,
  420. 'order_currency_code' => $order->order_currency_code,
  421. 'email_sent' => 1,
  422. 'discount_amount' => 0,
  423. 'base_discount_amount' => 0,
  424. 'sub_total' => $orderItem->base_price,
  425. 'base_sub_total' => $orderItem->base_price,
  426. 'grand_total' => $orderItem->price,
  427. 'base_grand_total' => $orderItem->price,
  428. ]);
  429. $invoiceItem = InvoiceItem::factory()->create([
  430. 'invoice_id' => $invoice->id,
  431. 'order_item_id' => $orderItem->id,
  432. 'name' => $orderItem->name,
  433. 'sku' => $orderItem->sku,
  434. 'qty' => 1,
  435. 'price' => $orderItem->price,
  436. 'base_price' => $orderItem->base_price,
  437. 'total' => $orderItem->price,
  438. 'base_total' => $orderItem->base_price,
  439. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  440. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  441. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  442. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  443. 'product_id' => $orderItem->product_id,
  444. 'product_type' => $orderItem->product_type,
  445. 'additional' => $orderItem->additional,
  446. ]);
  447. $orderTransaction = OrderTransaction::factory()->create([
  448. 'transaction_id' => md5(uniqid()),
  449. 'type' => 'cashondelivery',
  450. 'payment_method' => 'cashondelivery',
  451. 'status' => 'paid',
  452. 'status' => $invoice->state,
  453. 'order_id' => $invoice->order->id,
  454. 'invoice_id' => $invoice->id,
  455. 'amount' => $invoice->grand_total,
  456. ]);
  457. // Act and Assert.
  458. $this->loginAsAdmin();
  459. get(route('admin.reporting.sales.stats', [
  460. 'start' => Carbon::now()->copy()->subMonth(),
  461. 'type' => 'total-orders',
  462. 'end' => Carbon::now()->addMonths(5),
  463. ]))
  464. ->assertOk()
  465. ->assertJsonPath('statistics.orders.current', 1)
  466. ->assertJsonPath('statistics.orders.progress', 100);
  467. $cart->refresh();
  468. $cartItem->refresh();
  469. $cartBillingAddress->refresh();
  470. $cartShippingAddress->refresh();
  471. $orderBillingAddress->refresh();
  472. $orderShippingAddress->refresh();
  473. $order->refresh();
  474. $orderItem->refresh();
  475. $invoiceItem->refresh();
  476. $orderTransaction->refresh();
  477. $this->assertModelWise([
  478. Cart::class => [
  479. $this->prepareCart($cart),
  480. ],
  481. CartItem::class => [
  482. $this->prepareCartItem($cartItem),
  483. ],
  484. CartPayment::class => [
  485. $this->prepareCartPayment($cartPayment),
  486. ],
  487. CartAddress::class => [
  488. $this->prepareAddress($cartBillingAddress),
  489. ],
  490. CartAddress::class => [
  491. $this->prepareAddress($cartShippingAddress),
  492. ],
  493. CartShippingRate::class => [
  494. $this->prepareCartShippingRate($cartShippingRate),
  495. ],
  496. CustomerAddress::class => [
  497. $this->prepareAddress($customerAddress),
  498. ],
  499. Order::class => [
  500. $this->prepareOrder($order),
  501. ],
  502. OrderItem::class => [
  503. $this->prepareOrderItem($orderItem),
  504. ],
  505. OrderAddress::class => [
  506. $this->prepareAddress($orderBillingAddress),
  507. $this->prepareAddress($orderShippingAddress),
  508. ],
  509. OrderPayment::class => [
  510. $this->prepareOrderPayment($orderPayment),
  511. ],
  512. Invoice::class => [
  513. $this->prepareInvoice($order, $orderItem),
  514. ],
  515. InvoiceItem::class => [
  516. $this->prepareInvoiceItem($invoiceItem),
  517. ],
  518. ]);
  519. });
  520. it('should returns the average sale stats', function () {
  521. // Arrange.
  522. $product = (new ProductFaker([
  523. 'attributes' => [
  524. 5 => 'new',
  525. ],
  526. 'attribute_value' => [
  527. 'new' => [
  528. 'boolean_value' => true,
  529. ],
  530. ],
  531. ]))
  532. ->getSimpleProductFactory()
  533. ->create();
  534. $customer = Customer::factory()->create();
  535. $cart = Cart::factory()->create([
  536. 'customer_id' => $customer->id,
  537. 'customer_first_name' => $customer->first_name,
  538. 'customer_last_name' => $customer->last_name,
  539. 'customer_email' => $customer->email,
  540. 'is_guest' => 0,
  541. ]);
  542. $additional = [
  543. 'product_id' => $product->id,
  544. 'rating' => '0',
  545. 'is_buy_now' => '0',
  546. 'quantity' => '1',
  547. ];
  548. $cartItem = CartItem::factory()->create([
  549. 'cart_id' => $cart->id,
  550. 'product_id' => $product->id,
  551. 'sku' => $product->sku,
  552. 'quantity' => $additional['quantity'],
  553. 'name' => $product->name,
  554. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  555. 'base_price' => $price,
  556. 'total' => $convertedPrice * $additional['quantity'],
  557. 'base_total' => $price * $additional['quantity'],
  558. 'weight' => $product->weight ?? 0,
  559. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  560. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  561. 'type' => $product->type,
  562. 'additional' => $additional,
  563. ]);
  564. $customerAddress = CustomerAddress::factory()->create([
  565. 'cart_id' => $cart->id,
  566. 'customer_id' => $customer->id,
  567. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  568. ]);
  569. $cartBillingAddress = CartAddress::factory()->create([
  570. 'cart_id' => $cart->id,
  571. 'customer_id' => $customer->id,
  572. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  573. ]);
  574. $cartShippingAddress = CartAddress::factory()->create([
  575. 'cart_id' => $cart->id,
  576. 'customer_id' => $customer->id,
  577. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  578. ]);
  579. $cartPayment = CartPayment::factory()->create([
  580. 'cart_id' => $cart->id,
  581. 'method' => $paymentMethod = 'cashondelivery',
  582. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  583. ]);
  584. $cartShippingRate = CartShippingRate::factory()->create([
  585. 'carrier' => 'free',
  586. 'carrier_title' => 'Free shipping',
  587. 'method' => 'free_free',
  588. 'method_title' => 'Free Shipping',
  589. 'method_description' => 'Free Shipping',
  590. 'cart_address_id' => $cartShippingAddress->id,
  591. ]);
  592. $order = Order::factory()->create([
  593. 'cart_id' => $cart->id,
  594. 'customer_id' => $customer->id,
  595. 'customer_email' => $customer->email,
  596. 'customer_first_name' => $customer->first_name,
  597. 'customer_last_name' => $customer->last_name,
  598. 'status' => 'processing',
  599. 'sub_total_invoiced' => $product->price,
  600. 'base_sub_total_invoiced' => $product->price,
  601. ]);
  602. $orderItem = OrderItem::factory()->create([
  603. 'product_id' => $product->id,
  604. 'order_id' => $order->id,
  605. 'sku' => $product->sku,
  606. 'type' => $product->type,
  607. 'name' => $product->name,
  608. 'qty_invoiced' => 1,
  609. 'base_total_invoiced' => $product->price,
  610. ]);
  611. $orderBillingAddress = OrderAddress::factory()->create([
  612. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  613. 'cart_id' => $cart->id,
  614. 'customer_id' => $customer->id,
  615. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  616. 'order_id' => $order->id,
  617. ]);
  618. $orderShippingAddress = OrderAddress::factory()->create([
  619. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  620. 'cart_id' => $cart->id,
  621. 'customer_id' => $customer->id,
  622. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  623. 'order_id' => $order->id,
  624. ]);
  625. $orderPayment = OrderPayment::factory()->create([
  626. 'order_id' => $order->id,
  627. 'method' => 'cashondelivery',
  628. ]);
  629. $invoice = Invoice::factory()->create([
  630. 'order_id' => $order->id,
  631. 'state' => 'paid',
  632. 'total_qty' => 1,
  633. 'base_currency_code' => $order->base_currency_code,
  634. 'channel_currency_code' => $order->channel_currency_code,
  635. 'order_currency_code' => $order->order_currency_code,
  636. 'email_sent' => 1,
  637. 'discount_amount' => 0,
  638. 'base_discount_amount' => 0,
  639. 'sub_total' => $orderItem->base_price,
  640. 'base_sub_total' => $orderItem->base_price,
  641. 'grand_total' => $orderItem->price,
  642. 'base_grand_total' => $orderItem->price,
  643. ]);
  644. $invoiceItem = InvoiceItem::factory()->create([
  645. 'invoice_id' => $invoice->id,
  646. 'order_item_id' => $orderItem->id,
  647. 'name' => $orderItem->name,
  648. 'sku' => $orderItem->sku,
  649. 'qty' => 1,
  650. 'price' => $orderItem->price,
  651. 'base_price' => $orderItem->base_price,
  652. 'total' => $orderItem->price,
  653. 'base_total' => $orderItem->base_price,
  654. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  655. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  656. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  657. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  658. 'product_id' => $orderItem->product_id,
  659. 'product_type' => $orderItem->product_type,
  660. 'additional' => $orderItem->additional,
  661. ]);
  662. $orderTransaction = OrderTransaction::factory()->create([
  663. 'transaction_id' => md5(uniqid()),
  664. 'type' => 'cashondelivery',
  665. 'payment_method' => 'cashondelivery',
  666. 'status' => 'paid',
  667. 'status' => $invoice->state,
  668. 'order_id' => $invoice->order->id,
  669. 'invoice_id' => $invoice->id,
  670. 'amount' => $invoice->grand_total,
  671. ]);
  672. // Act and Assert.
  673. $this->loginAsAdmin();
  674. $response = get(route('admin.reporting.sales.stats', [
  675. 'start' => Carbon::now()->copy()->subMonth(),
  676. 'type' => 'average-sales',
  677. 'end' => Carbon::now()->addMonths(5),
  678. ]))
  679. ->assertOk()
  680. ->assertJsonPath('statistics.sales.progress', 100)
  681. ->assertJsonPath('statistics.sales.formatted_total', core()->formatBasePrice($order->grand_total))
  682. ->assertJsonPath('statistics.over_time.current.30.count', 1);
  683. $this->assertPrice($order->grand_total, $response->json('statistics.over_time.current.30.total'));
  684. $cart->refresh();
  685. $cartItem->refresh();
  686. $cartBillingAddress->refresh();
  687. $cartShippingAddress->refresh();
  688. $orderBillingAddress->refresh();
  689. $orderShippingAddress->refresh();
  690. $order->refresh();
  691. $orderItem->refresh();
  692. $invoiceItem->refresh();
  693. $orderTransaction->refresh();
  694. $this->assertModelWise([
  695. Cart::class => [
  696. $this->prepareCart($cart),
  697. ],
  698. CartItem::class => [
  699. $this->prepareCartItem($cartItem),
  700. ],
  701. CartPayment::class => [
  702. $this->prepareCartPayment($cartPayment),
  703. ],
  704. CartAddress::class => [
  705. $this->prepareAddress($cartBillingAddress),
  706. ],
  707. CartAddress::class => [
  708. $this->prepareAddress($cartShippingAddress),
  709. ],
  710. CartShippingRate::class => [
  711. $this->prepareCartShippingRate($cartShippingRate),
  712. ],
  713. CustomerAddress::class => [
  714. $this->prepareAddress($customerAddress),
  715. ],
  716. Order::class => [
  717. $this->prepareOrder($order),
  718. ],
  719. OrderItem::class => [
  720. $this->prepareOrderItem($orderItem),
  721. ],
  722. OrderAddress::class => [
  723. $this->prepareAddress($orderBillingAddress),
  724. $this->prepareAddress($orderShippingAddress),
  725. ],
  726. OrderPayment::class => [
  727. $this->prepareOrderPayment($orderPayment),
  728. ],
  729. Invoice::class => [
  730. $this->prepareInvoice($order, $orderItem),
  731. ],
  732. InvoiceItem::class => [
  733. $this->prepareInvoiceItem($invoiceItem),
  734. ],
  735. ]);
  736. });
  737. it('should returns the shipping collected stats', function () {
  738. // Arrange.
  739. $product = (new ProductFaker([
  740. 'attributes' => [
  741. 5 => 'new',
  742. ],
  743. 'attribute_value' => [
  744. 'new' => [
  745. 'boolean_value' => true,
  746. ],
  747. ],
  748. ]))
  749. ->getSimpleProductFactory()
  750. ->create();
  751. $customer = Customer::factory()->create();
  752. $cart = Cart::factory()->create([
  753. 'customer_id' => $customer->id,
  754. 'customer_first_name' => $customer->first_name,
  755. 'customer_last_name' => $customer->last_name,
  756. 'customer_email' => $customer->email,
  757. 'is_guest' => 0,
  758. ]);
  759. $additional = [
  760. 'product_id' => $product->id,
  761. 'rating' => '0',
  762. 'is_buy_now' => '0',
  763. 'quantity' => '1',
  764. ];
  765. $cartItem = CartItem::factory()->create([
  766. 'cart_id' => $cart->id,
  767. 'product_id' => $product->id,
  768. 'sku' => $product->sku,
  769. 'quantity' => $additional['quantity'],
  770. 'name' => $product->name,
  771. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  772. 'base_price' => $price,
  773. 'total' => $convertedPrice * $additional['quantity'],
  774. 'base_total' => $price * $additional['quantity'],
  775. 'weight' => $product->weight ?? 0,
  776. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  777. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  778. 'type' => $product->type,
  779. 'additional' => $additional,
  780. ]);
  781. $customerAddress = CustomerAddress::factory()->create([
  782. 'cart_id' => $cart->id,
  783. 'customer_id' => $customer->id,
  784. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  785. ]);
  786. $cartBillingAddress = CartAddress::factory()->create([
  787. 'cart_id' => $cart->id,
  788. 'customer_id' => $customer->id,
  789. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  790. ]);
  791. $cartShippingAddress = CartAddress::factory()->create([
  792. 'cart_id' => $cart->id,
  793. 'customer_id' => $customer->id,
  794. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  795. ]);
  796. $cartPayment = CartPayment::factory()->create([
  797. 'cart_id' => $cart->id,
  798. 'method' => $paymentMethod = 'cashondelivery',
  799. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  800. ]);
  801. $cartShippingRate = CartShippingRate::factory()->create([
  802. 'carrier' => 'free',
  803. 'carrier_title' => 'Free shipping',
  804. 'method' => 'free_free',
  805. 'method_title' => 'Free Shipping',
  806. 'method_description' => 'Free Shipping',
  807. 'cart_address_id' => $cartShippingAddress->id,
  808. ]);
  809. $order = Order::factory()->create([
  810. 'cart_id' => $cart->id,
  811. 'customer_id' => $customer->id,
  812. 'customer_email' => $customer->email,
  813. 'customer_first_name' => $customer->first_name,
  814. 'customer_last_name' => $customer->last_name,
  815. 'status' => 'processing',
  816. 'sub_total_invoiced' => $product->price,
  817. 'base_sub_total_invoiced' => $product->price,
  818. ]);
  819. $orderItem = OrderItem::factory()->create([
  820. 'product_id' => $product->id,
  821. 'order_id' => $order->id,
  822. 'sku' => $product->sku,
  823. 'type' => $product->type,
  824. 'name' => $product->name,
  825. 'qty_invoiced' => 1,
  826. 'base_total_invoiced' => $product->price,
  827. ]);
  828. $orderBillingAddress = OrderAddress::factory()->create([
  829. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  830. 'cart_id' => $cart->id,
  831. 'customer_id' => $customer->id,
  832. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  833. 'order_id' => $order->id,
  834. ]);
  835. $orderShippingAddress = OrderAddress::factory()->create([
  836. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  837. 'cart_id' => $cart->id,
  838. 'customer_id' => $customer->id,
  839. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  840. 'order_id' => $order->id,
  841. ]);
  842. $orderPayment = OrderPayment::factory()->create([
  843. 'order_id' => $order->id,
  844. 'method' => 'cashondelivery',
  845. ]);
  846. $invoice = Invoice::factory()->create([
  847. 'order_id' => $order->id,
  848. 'state' => 'paid',
  849. 'total_qty' => 1,
  850. 'base_currency_code' => $order->base_currency_code,
  851. 'channel_currency_code' => $order->channel_currency_code,
  852. 'order_currency_code' => $order->order_currency_code,
  853. 'email_sent' => 1,
  854. 'discount_amount' => 0,
  855. 'base_discount_amount' => 0,
  856. 'sub_total' => $orderItem->base_price,
  857. 'base_sub_total' => $orderItem->base_price,
  858. 'grand_total' => $orderItem->price,
  859. 'base_grand_total' => $orderItem->price,
  860. ]);
  861. $invoiceItem = InvoiceItem::factory()->create([
  862. 'invoice_id' => $invoice->id,
  863. 'order_item_id' => $orderItem->id,
  864. 'name' => $orderItem->name,
  865. 'sku' => $orderItem->sku,
  866. 'qty' => 1,
  867. 'price' => $orderItem->price,
  868. 'base_price' => $orderItem->base_price,
  869. 'total' => $orderItem->price,
  870. 'base_total' => $orderItem->base_price,
  871. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  872. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  873. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  874. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  875. 'product_id' => $orderItem->product_id,
  876. 'product_type' => $orderItem->product_type,
  877. 'additional' => $orderItem->additional,
  878. ]);
  879. $orderTransaction = OrderTransaction::factory()->create([
  880. 'transaction_id' => md5(uniqid()),
  881. 'type' => 'cashondelivery',
  882. 'payment_method' => 'cashondelivery',
  883. 'status' => 'paid',
  884. 'status' => $invoice->state,
  885. 'order_id' => $invoice->order->id,
  886. 'invoice_id' => $invoice->id,
  887. 'amount' => $invoice->grand_total,
  888. ]);
  889. // Act and Assert.
  890. $this->loginAsAdmin();
  891. get(route('admin.reporting.sales.stats', [
  892. 'start' => Carbon::now()->copy()->subMonth(),
  893. 'type' => 'shipping-collected',
  894. 'end' => Carbon::now()->addMonths(5),
  895. ]))
  896. ->assertOk()
  897. ->assertJsonPath('statistics.top_methods.0.title', 'Free Shipping');
  898. $cart->refresh();
  899. $cartItem->refresh();
  900. $cartBillingAddress->refresh();
  901. $cartShippingAddress->refresh();
  902. $orderBillingAddress->refresh();
  903. $orderShippingAddress->refresh();
  904. $order->refresh();
  905. $orderItem->refresh();
  906. $invoiceItem->refresh();
  907. $orderTransaction->refresh();
  908. $this->assertModelWise([
  909. Cart::class => [
  910. $this->prepareCart($cart),
  911. ],
  912. CartItem::class => [
  913. $this->prepareCartItem($cartItem),
  914. ],
  915. CartPayment::class => [
  916. $this->prepareCartPayment($cartPayment),
  917. ],
  918. CartAddress::class => [
  919. $this->prepareAddress($cartBillingAddress),
  920. ],
  921. CartAddress::class => [
  922. $this->prepareAddress($cartShippingAddress),
  923. ],
  924. CartShippingRate::class => [
  925. $this->prepareCartShippingRate($cartShippingRate),
  926. ],
  927. CustomerAddress::class => [
  928. $this->prepareAddress($customerAddress),
  929. ],
  930. Order::class => [
  931. $this->prepareOrder($order),
  932. ],
  933. OrderItem::class => [
  934. $this->prepareOrderItem($orderItem),
  935. ],
  936. OrderAddress::class => [
  937. $this->prepareAddress($orderBillingAddress),
  938. $this->prepareAddress($orderShippingAddress),
  939. ],
  940. OrderPayment::class => [
  941. $this->prepareOrderPayment($orderPayment),
  942. ],
  943. Invoice::class => [
  944. $this->prepareInvoice($order, $orderItem),
  945. ],
  946. InvoiceItem::class => [
  947. $this->prepareInvoiceItem($invoiceItem),
  948. ],
  949. ]);
  950. });
  951. it('should returns the tax collected stats', function () {
  952. // Arrange.
  953. $product = (new ProductFaker([
  954. 'attributes' => [
  955. 5 => 'new',
  956. ],
  957. 'attribute_value' => [
  958. 'new' => [
  959. 'boolean_value' => true,
  960. ],
  961. ],
  962. ]))
  963. ->getSimpleProductFactory()
  964. ->create();
  965. $customer = Customer::factory()->create();
  966. $cart = Cart::factory()->create([
  967. 'customer_id' => $customer->id,
  968. 'customer_first_name' => $customer->first_name,
  969. 'customer_last_name' => $customer->last_name,
  970. 'customer_email' => $customer->email,
  971. 'is_guest' => 0,
  972. ]);
  973. $additional = [
  974. 'product_id' => $product->id,
  975. 'rating' => '0',
  976. 'is_buy_now' => '0',
  977. 'quantity' => '1',
  978. ];
  979. $cartItem = CartItem::factory()->create([
  980. 'cart_id' => $cart->id,
  981. 'product_id' => $product->id,
  982. 'sku' => $product->sku,
  983. 'quantity' => $additional['quantity'],
  984. 'name' => $product->name,
  985. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  986. 'base_price' => $price,
  987. 'total' => $convertedPrice * $additional['quantity'],
  988. 'base_total' => $price * $additional['quantity'],
  989. 'weight' => $product->weight ?? 0,
  990. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  991. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  992. 'type' => $product->type,
  993. 'additional' => $additional,
  994. ]);
  995. $customerAddress = CustomerAddress::factory()->create([
  996. 'cart_id' => $cart->id,
  997. 'customer_id' => $customer->id,
  998. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  999. ]);
  1000. $cartBillingAddress = CartAddress::factory()->create([
  1001. 'cart_id' => $cart->id,
  1002. 'customer_id' => $customer->id,
  1003. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1004. ]);
  1005. $cartShippingAddress = CartAddress::factory()->create([
  1006. 'cart_id' => $cart->id,
  1007. 'customer_id' => $customer->id,
  1008. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1009. ]);
  1010. $cartPayment = CartPayment::factory()->create([
  1011. 'cart_id' => $cart->id,
  1012. 'method' => $paymentMethod = 'cashondelivery',
  1013. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1014. ]);
  1015. $cartShippingRate = CartShippingRate::factory()->create([
  1016. 'carrier' => 'free',
  1017. 'carrier_title' => 'Free shipping',
  1018. 'method' => 'free_free',
  1019. 'method_title' => 'Free Shipping',
  1020. 'method_description' => 'Free Shipping',
  1021. 'cart_address_id' => $cartShippingAddress->id,
  1022. ]);
  1023. $order = Order::factory()->create([
  1024. 'cart_id' => $cart->id,
  1025. 'customer_id' => $customer->id,
  1026. 'customer_email' => $customer->email,
  1027. 'customer_first_name' => $customer->first_name,
  1028. 'customer_last_name' => $customer->last_name,
  1029. 'status' => 'processing',
  1030. 'sub_total_invoiced' => $product->price,
  1031. 'base_sub_total_invoiced' => $product->price,
  1032. 'base_tax_amount_invoiced' => $taxInvoiced = rand(20, 50),
  1033. 'base_grand_total_refunded' => $grantTotalRefunded = $taxInvoiced - 10,
  1034. ]);
  1035. $orderItem = OrderItem::factory()->create([
  1036. 'product_id' => $product->id,
  1037. 'order_id' => $order->id,
  1038. 'sku' => $product->sku,
  1039. 'type' => $product->type,
  1040. 'name' => $product->name,
  1041. 'qty_invoiced' => 1,
  1042. 'base_total_invoiced' => $product->price,
  1043. ]);
  1044. $orderBillingAddress = OrderAddress::factory()->create([
  1045. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1046. 'cart_id' => $cart->id,
  1047. 'customer_id' => $customer->id,
  1048. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1049. 'order_id' => $order->id,
  1050. ]);
  1051. $orderShippingAddress = OrderAddress::factory()->create([
  1052. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1053. 'cart_id' => $cart->id,
  1054. 'customer_id' => $customer->id,
  1055. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1056. 'order_id' => $order->id,
  1057. ]);
  1058. $orderPayment = OrderPayment::factory()->create([
  1059. 'order_id' => $order->id,
  1060. 'method' => 'cashondelivery',
  1061. ]);
  1062. $invoice = Invoice::factory()->create([
  1063. 'order_id' => $order->id,
  1064. 'state' => 'paid',
  1065. 'total_qty' => 1,
  1066. 'base_currency_code' => $order->base_currency_code,
  1067. 'channel_currency_code' => $order->channel_currency_code,
  1068. 'order_currency_code' => $order->order_currency_code,
  1069. 'email_sent' => 1,
  1070. 'discount_amount' => 0,
  1071. 'base_discount_amount' => 0,
  1072. 'sub_total' => $orderItem->base_price,
  1073. 'base_sub_total' => $orderItem->base_price,
  1074. 'grand_total' => $orderItem->price,
  1075. 'base_grand_total' => $orderItem->price,
  1076. ]);
  1077. $invoiceItem = InvoiceItem::factory()->create([
  1078. 'invoice_id' => $invoice->id,
  1079. 'order_item_id' => $orderItem->id,
  1080. 'name' => $orderItem->name,
  1081. 'sku' => $orderItem->sku,
  1082. 'qty' => 1,
  1083. 'price' => $orderItem->price,
  1084. 'base_price' => $orderItem->base_price,
  1085. 'total' => $orderItem->price,
  1086. 'base_total' => $orderItem->base_price,
  1087. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1088. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1089. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1090. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1091. 'product_id' => $orderItem->product_id,
  1092. 'product_type' => $orderItem->product_type,
  1093. 'additional' => $orderItem->additional,
  1094. ]);
  1095. $orderTransaction = OrderTransaction::factory()->create([
  1096. 'transaction_id' => md5(uniqid()),
  1097. 'type' => 'cashondelivery',
  1098. 'payment_method' => 'cashondelivery',
  1099. 'status' => $invoice->state,
  1100. 'order_id' => $invoice->order->id,
  1101. 'invoice_id' => $invoice->id,
  1102. 'amount' => $invoice->grand_total,
  1103. ]);
  1104. // Act and Assert.
  1105. $this->loginAsAdmin();
  1106. get(route('admin.reporting.sales.stats', [
  1107. 'start' => Carbon::now()->copy()->subMonth(),
  1108. 'type' => 'tax-collected',
  1109. 'end' => Carbon::now()->addMonths(5),
  1110. ]))
  1111. ->assertOk()
  1112. ->assertJsonPath('statistics.tax_collected.current', $taxInvoiced);
  1113. $cart->refresh();
  1114. $cartItem->refresh();
  1115. $cartBillingAddress->refresh();
  1116. $cartShippingAddress->refresh();
  1117. $orderBillingAddress->refresh();
  1118. $orderShippingAddress->refresh();
  1119. $order->refresh();
  1120. $orderItem->refresh();
  1121. $invoiceItem->refresh();
  1122. $orderTransaction->refresh();
  1123. $this->assertModelWise([
  1124. Cart::class => [
  1125. $this->prepareCart($cart),
  1126. ],
  1127. CartItem::class => [
  1128. $this->prepareCartItem($cartItem),
  1129. ],
  1130. CartPayment::class => [
  1131. $this->prepareCartPayment($cartPayment),
  1132. ],
  1133. CartAddress::class => [
  1134. $this->prepareAddress($cartBillingAddress),
  1135. ],
  1136. CartAddress::class => [
  1137. $this->prepareAddress($cartShippingAddress),
  1138. ],
  1139. CartShippingRate::class => [
  1140. $this->prepareCartShippingRate($cartShippingRate),
  1141. ],
  1142. CustomerAddress::class => [
  1143. $this->prepareAddress($customerAddress),
  1144. ],
  1145. Order::class => [
  1146. $this->prepareOrder($order),
  1147. ],
  1148. OrderItem::class => [
  1149. $this->prepareOrderItem($orderItem),
  1150. ],
  1151. OrderAddress::class => [
  1152. $this->prepareAddress($orderBillingAddress),
  1153. $this->prepareAddress($orderShippingAddress),
  1154. ],
  1155. OrderPayment::class => [
  1156. $this->prepareOrderPayment($orderPayment),
  1157. ],
  1158. Invoice::class => [
  1159. $this->prepareInvoice($order, $orderItem),
  1160. ],
  1161. InvoiceItem::class => [
  1162. $this->prepareInvoiceItem($invoiceItem),
  1163. ],
  1164. ]);
  1165. });
  1166. it('should returns the refunds stats', function () {
  1167. // Arrange.
  1168. $product = (new ProductFaker([
  1169. 'attributes' => [
  1170. 5 => 'new',
  1171. ],
  1172. 'attribute_value' => [
  1173. 'new' => [
  1174. 'boolean_value' => true,
  1175. ],
  1176. ],
  1177. ]))
  1178. ->getSimpleProductFactory()
  1179. ->create();
  1180. $customer = Customer::factory()->create();
  1181. $cart = Cart::factory()->create([
  1182. 'customer_id' => $customer->id,
  1183. 'customer_first_name' => $customer->first_name,
  1184. 'customer_last_name' => $customer->last_name,
  1185. 'customer_email' => $customer->email,
  1186. 'is_guest' => 0,
  1187. ]);
  1188. $additional = [
  1189. 'product_id' => $product->id,
  1190. 'rating' => '0',
  1191. 'is_buy_now' => '0',
  1192. 'quantity' => '1',
  1193. ];
  1194. $cartItem = CartItem::factory()->create([
  1195. 'cart_id' => $cart->id,
  1196. 'product_id' => $product->id,
  1197. 'sku' => $product->sku,
  1198. 'quantity' => $additional['quantity'],
  1199. 'name' => $product->name,
  1200. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1201. 'base_price' => $price,
  1202. 'total' => $convertedPrice * $additional['quantity'],
  1203. 'base_total' => $price * $additional['quantity'],
  1204. 'weight' => $product->weight ?? 0,
  1205. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1206. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1207. 'type' => $product->type,
  1208. 'additional' => $additional,
  1209. ]);
  1210. $customerAddress = CustomerAddress::factory()->create([
  1211. 'cart_id' => $cart->id,
  1212. 'customer_id' => $customer->id,
  1213. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1214. ]);
  1215. $cartBillingAddress = CartAddress::factory()->create([
  1216. 'cart_id' => $cart->id,
  1217. 'customer_id' => $customer->id,
  1218. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1219. ]);
  1220. $cartShippingAddress = CartAddress::factory()->create([
  1221. 'cart_id' => $cart->id,
  1222. 'customer_id' => $customer->id,
  1223. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1224. ]);
  1225. $cartPayment = CartPayment::factory()->create([
  1226. 'cart_id' => $cart->id,
  1227. 'method' => $paymentMethod = 'cashondelivery',
  1228. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1229. ]);
  1230. $cartShippingRate = CartShippingRate::factory()->create([
  1231. 'carrier' => 'free',
  1232. 'carrier_title' => 'Free shipping',
  1233. 'method' => 'free_free',
  1234. 'method_title' => 'Free Shipping',
  1235. 'method_description' => 'Free Shipping',
  1236. 'cart_address_id' => $cartShippingAddress->id,
  1237. ]);
  1238. $order = Order::factory()->create([
  1239. 'cart_id' => $cart->id,
  1240. 'customer_id' => $customer->id,
  1241. 'customer_email' => $customer->email,
  1242. 'customer_first_name' => $customer->first_name,
  1243. 'customer_last_name' => $customer->last_name,
  1244. 'status' => 'processing',
  1245. 'sub_total_invoiced' => $product->price,
  1246. 'base_sub_total_invoiced' => $product->price,
  1247. 'base_tax_amount_invoiced' => $taxInvoiced = rand(20, 50),
  1248. 'base_grand_total_refunded' => $grantTotalRefunded = $taxInvoiced - 10,
  1249. ]);
  1250. $orderItem = OrderItem::factory()->create([
  1251. 'product_id' => $product->id,
  1252. 'order_id' => $order->id,
  1253. 'sku' => $product->sku,
  1254. 'type' => $product->type,
  1255. 'name' => $product->name,
  1256. 'qty_invoiced' => 1,
  1257. 'base_total_invoiced' => $product->price,
  1258. ]);
  1259. $orderBillingAddress = OrderAddress::factory()->create([
  1260. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1261. 'cart_id' => $cart->id,
  1262. 'customer_id' => $customer->id,
  1263. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1264. 'order_id' => $order->id,
  1265. ]);
  1266. $orderShippingAddress = OrderAddress::factory()->create([
  1267. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1268. 'cart_id' => $cart->id,
  1269. 'customer_id' => $customer->id,
  1270. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1271. 'order_id' => $order->id,
  1272. ]);
  1273. $orderPayment = OrderPayment::factory()->create([
  1274. 'order_id' => $order->id,
  1275. 'method' => 'cashondelivery',
  1276. ]);
  1277. $invoice = Invoice::factory()->create([
  1278. 'order_id' => $order->id,
  1279. 'state' => 'paid',
  1280. 'total_qty' => 1,
  1281. 'base_currency_code' => $order->base_currency_code,
  1282. 'channel_currency_code' => $order->channel_currency_code,
  1283. 'order_currency_code' => $order->order_currency_code,
  1284. 'email_sent' => 1,
  1285. 'discount_amount' => 0,
  1286. 'base_discount_amount' => 0,
  1287. 'sub_total' => $orderItem->base_price,
  1288. 'base_sub_total' => $orderItem->base_price,
  1289. 'grand_total' => $orderItem->price,
  1290. 'base_grand_total' => $orderItem->price,
  1291. ]);
  1292. $invoiceItem = InvoiceItem::factory()->create([
  1293. 'invoice_id' => $invoice->id,
  1294. 'order_item_id' => $orderItem->id,
  1295. 'name' => $orderItem->name,
  1296. 'sku' => $orderItem->sku,
  1297. 'qty' => 1,
  1298. 'price' => $orderItem->price,
  1299. 'base_price' => $orderItem->base_price,
  1300. 'total' => $orderItem->price,
  1301. 'base_total' => $orderItem->base_price,
  1302. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1303. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1304. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1305. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1306. 'product_id' => $orderItem->product_id,
  1307. 'product_type' => $orderItem->product_type,
  1308. 'additional' => $orderItem->additional,
  1309. ]);
  1310. $orderTransaction = OrderTransaction::factory()->create([
  1311. 'transaction_id' => md5(uniqid()),
  1312. 'type' => 'cashondelivery',
  1313. 'payment_method' => 'cashondelivery',
  1314. 'status' => $invoice->state,
  1315. 'order_id' => $invoice->order->id,
  1316. 'invoice_id' => $invoice->id,
  1317. 'amount' => $invoice->grand_total,
  1318. ]);
  1319. // Act and Assert.
  1320. $this->loginAsAdmin();
  1321. get(route('admin.reporting.sales.stats', [
  1322. 'start' => Carbon::now()->copy()->subMonth(),
  1323. 'type' => 'refunds',
  1324. 'end' => Carbon::now()->addMonths(5),
  1325. ]))
  1326. ->assertOk()
  1327. ->assertJsonPath('statistics.refunds.current', $grantTotalRefunded);
  1328. $cart->refresh();
  1329. $cartItem->refresh();
  1330. $cartBillingAddress->refresh();
  1331. $cartShippingAddress->refresh();
  1332. $orderBillingAddress->refresh();
  1333. $orderShippingAddress->refresh();
  1334. $order->refresh();
  1335. $orderItem->refresh();
  1336. $invoiceItem->refresh();
  1337. $orderTransaction->refresh();
  1338. $this->assertModelWise([
  1339. Cart::class => [
  1340. $this->prepareCart($cart),
  1341. ],
  1342. CartItem::class => [
  1343. $this->prepareCartItem($cartItem),
  1344. ],
  1345. CartPayment::class => [
  1346. $this->prepareCartPayment($cartPayment),
  1347. ],
  1348. CartAddress::class => [
  1349. $this->prepareAddress($cartBillingAddress),
  1350. ],
  1351. CartAddress::class => [
  1352. $this->prepareAddress($cartShippingAddress),
  1353. ],
  1354. CartShippingRate::class => [
  1355. $this->prepareCartShippingRate($cartShippingRate),
  1356. ],
  1357. CustomerAddress::class => [
  1358. $this->prepareAddress($customerAddress),
  1359. ],
  1360. Order::class => [
  1361. $this->prepareOrder($order),
  1362. ],
  1363. OrderItem::class => [
  1364. $this->prepareOrderItem($orderItem),
  1365. ],
  1366. OrderAddress::class => [
  1367. $this->prepareAddress($orderBillingAddress),
  1368. $this->prepareAddress($orderShippingAddress),
  1369. ],
  1370. OrderPayment::class => [
  1371. $this->prepareOrderPayment($orderPayment),
  1372. ],
  1373. Invoice::class => [
  1374. $this->prepareInvoice($order, $orderItem),
  1375. ],
  1376. InvoiceItem::class => [
  1377. $this->prepareInvoiceItem($invoiceItem),
  1378. ],
  1379. ]);
  1380. });
  1381. it('should returns the top payment methods stats', function () {
  1382. // Arrange.
  1383. $product = (new ProductFaker([
  1384. 'attributes' => [
  1385. 5 => 'new',
  1386. ],
  1387. 'attribute_value' => [
  1388. 'new' => [
  1389. 'boolean_value' => true,
  1390. ],
  1391. ],
  1392. ]))
  1393. ->getSimpleProductFactory()
  1394. ->create();
  1395. $customer = Customer::factory()->create();
  1396. $cart = Cart::factory()->create([
  1397. 'customer_id' => $customer->id,
  1398. 'customer_first_name' => $customer->first_name,
  1399. 'customer_last_name' => $customer->last_name,
  1400. 'customer_email' => $customer->email,
  1401. 'is_guest' => 0,
  1402. ]);
  1403. $additional = [
  1404. 'product_id' => $product->id,
  1405. 'rating' => '0',
  1406. 'is_buy_now' => '0',
  1407. 'quantity' => '1',
  1408. ];
  1409. $cartItem = CartItem::factory()->create([
  1410. 'cart_id' => $cart->id,
  1411. 'product_id' => $product->id,
  1412. 'sku' => $product->sku,
  1413. 'quantity' => $additional['quantity'],
  1414. 'name' => $product->name,
  1415. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1416. 'base_price' => $price,
  1417. 'total' => $convertedPrice * $additional['quantity'],
  1418. 'base_total' => $price * $additional['quantity'],
  1419. 'weight' => $product->weight ?? 0,
  1420. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1421. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1422. 'type' => $product->type,
  1423. 'additional' => $additional,
  1424. ]);
  1425. $customerAddress = CustomerAddress::factory()->create([
  1426. 'cart_id' => $cart->id,
  1427. 'customer_id' => $customer->id,
  1428. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1429. ]);
  1430. $cartBillingAddress = CartAddress::factory()->create([
  1431. 'cart_id' => $cart->id,
  1432. 'customer_id' => $customer->id,
  1433. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1434. ]);
  1435. $cartShippingAddress = CartAddress::factory()->create([
  1436. 'cart_id' => $cart->id,
  1437. 'customer_id' => $customer->id,
  1438. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1439. ]);
  1440. $cartPayment = CartPayment::factory()->create([
  1441. 'cart_id' => $cart->id,
  1442. 'method' => $paymentMethod = 'cashondelivery',
  1443. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1444. ]);
  1445. $cartShippingRate = CartShippingRate::factory()->create([
  1446. 'carrier' => 'free',
  1447. 'carrier_title' => 'Free shipping',
  1448. 'method' => 'free_free',
  1449. 'method_title' => 'Free Shipping',
  1450. 'method_description' => 'Free Shipping',
  1451. 'cart_address_id' => $cartShippingAddress->id,
  1452. ]);
  1453. $order = Order::factory()->create([
  1454. 'cart_id' => $cart->id,
  1455. 'customer_id' => $customer->id,
  1456. 'customer_email' => $customer->email,
  1457. 'customer_first_name' => $customer->first_name,
  1458. 'customer_last_name' => $customer->last_name,
  1459. 'status' => 'processing',
  1460. 'sub_total_invoiced' => $product->price,
  1461. 'base_sub_total_invoiced' => $product->price,
  1462. ]);
  1463. $orderItem = OrderItem::factory()->create([
  1464. 'product_id' => $product->id,
  1465. 'order_id' => $order->id,
  1466. 'sku' => $product->sku,
  1467. 'type' => $product->type,
  1468. 'name' => $product->name,
  1469. 'qty_invoiced' => 1,
  1470. 'base_total_invoiced' => $product->price,
  1471. ]);
  1472. $orderBillingAddress = OrderAddress::factory()->create([
  1473. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1474. 'cart_id' => $cart->id,
  1475. 'customer_id' => $customer->id,
  1476. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1477. 'order_id' => $order->id,
  1478. ]);
  1479. $orderShippingAddress = OrderAddress::factory()->create([
  1480. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1481. 'cart_id' => $cart->id,
  1482. 'customer_id' => $customer->id,
  1483. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1484. 'order_id' => $order->id,
  1485. ]);
  1486. $orderPayment = OrderPayment::factory()->create([
  1487. 'order_id' => $order->id,
  1488. 'method' => 'cashondelivery',
  1489. ]);
  1490. $invoice = Invoice::factory()->create([
  1491. 'order_id' => $order->id,
  1492. 'state' => 'paid',
  1493. 'total_qty' => 1,
  1494. 'base_currency_code' => $order->base_currency_code,
  1495. 'channel_currency_code' => $order->channel_currency_code,
  1496. 'order_currency_code' => $order->order_currency_code,
  1497. 'email_sent' => 1,
  1498. 'discount_amount' => 0,
  1499. 'base_discount_amount' => 0,
  1500. 'sub_total' => $orderItem->base_price,
  1501. 'base_sub_total' => $orderItem->base_price,
  1502. 'grand_total' => $orderItem->price,
  1503. 'base_grand_total' => $orderItem->price,
  1504. ]);
  1505. $invoiceItem = InvoiceItem::factory()->create([
  1506. 'invoice_id' => $invoice->id,
  1507. 'order_item_id' => $orderItem->id,
  1508. 'name' => $orderItem->name,
  1509. 'sku' => $orderItem->sku,
  1510. 'qty' => 1,
  1511. 'price' => $orderItem->price,
  1512. 'base_price' => $orderItem->base_price,
  1513. 'total' => $orderItem->price,
  1514. 'base_total' => $orderItem->base_price,
  1515. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1516. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1517. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1518. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1519. 'product_id' => $orderItem->product_id,
  1520. 'product_type' => $orderItem->product_type,
  1521. 'additional' => $orderItem->additional,
  1522. ]);
  1523. $orderTransaction = OrderTransaction::factory()->create([
  1524. 'transaction_id' => md5(uniqid()),
  1525. 'type' => 'cashondelivery',
  1526. 'payment_method' => 'cashondelivery',
  1527. 'status' => $invoice->state,
  1528. 'order_id' => $invoice->order->id,
  1529. 'invoice_id' => $invoice->id,
  1530. 'amount' => $invoice->grand_total,
  1531. ]);
  1532. // Act and Assert.
  1533. $this->loginAsAdmin();
  1534. get(route('admin.reporting.sales.stats', [
  1535. 'start' => Carbon::now()->copy()->subMonth(),
  1536. 'type' => 'top-payment-methods',
  1537. 'end' => Carbon::now()->addMonths(5),
  1538. ]))
  1539. ->assertOk()
  1540. ->assertJsonPath('statistics.0.method', 'cashondelivery')
  1541. ->assertJsonPath('statistics.0.title', 'Cash On Delivery');
  1542. $cart->refresh();
  1543. $cartItem->refresh();
  1544. $cartBillingAddress->refresh();
  1545. $cartShippingAddress->refresh();
  1546. $orderBillingAddress->refresh();
  1547. $orderShippingAddress->refresh();
  1548. $order->refresh();
  1549. $orderItem->refresh();
  1550. $invoiceItem->refresh();
  1551. $orderTransaction->refresh();
  1552. $this->assertModelWise([
  1553. Cart::class => [
  1554. $this->prepareCart($cart),
  1555. ],
  1556. CartItem::class => [
  1557. $this->prepareCartItem($cartItem),
  1558. ],
  1559. CartPayment::class => [
  1560. $this->prepareCartPayment($cartPayment),
  1561. ],
  1562. CartAddress::class => [
  1563. $this->prepareAddress($cartBillingAddress),
  1564. ],
  1565. CartAddress::class => [
  1566. $this->prepareAddress($cartShippingAddress),
  1567. ],
  1568. CartShippingRate::class => [
  1569. $this->prepareCartShippingRate($cartShippingRate),
  1570. ],
  1571. CustomerAddress::class => [
  1572. $this->prepareAddress($customerAddress),
  1573. ],
  1574. Order::class => [
  1575. $this->prepareOrder($order),
  1576. ],
  1577. OrderItem::class => [
  1578. $this->prepareOrderItem($orderItem),
  1579. ],
  1580. OrderAddress::class => [
  1581. $this->prepareAddress($orderBillingAddress),
  1582. $this->prepareAddress($orderShippingAddress),
  1583. ],
  1584. OrderPayment::class => [
  1585. $this->prepareOrderPayment($orderPayment),
  1586. ],
  1587. Invoice::class => [
  1588. $this->prepareInvoice($order, $orderItem),
  1589. ],
  1590. InvoiceItem::class => [
  1591. $this->prepareInvoiceItem($invoiceItem),
  1592. ],
  1593. ]);
  1594. });
  1595. it('should return the view page of sales stats', function () {
  1596. // Arrange.
  1597. $product = (new ProductFaker([
  1598. 'attributes' => [
  1599. 5 => 'new',
  1600. ],
  1601. 'attribute_value' => [
  1602. 'new' => [
  1603. 'boolean_value' => true,
  1604. ],
  1605. ],
  1606. ]))
  1607. ->getSimpleProductFactory()
  1608. ->create();
  1609. $customer = Customer::factory()->create();
  1610. $cart = Cart::factory()->create([
  1611. 'customer_id' => $customer->id,
  1612. 'customer_first_name' => $customer->first_name,
  1613. 'customer_last_name' => $customer->last_name,
  1614. 'customer_email' => $customer->email,
  1615. 'is_guest' => 0,
  1616. ]);
  1617. $additional = [
  1618. 'product_id' => $product->id,
  1619. 'rating' => '0',
  1620. 'is_buy_now' => '0',
  1621. 'quantity' => '1',
  1622. ];
  1623. $cartItem = CartItem::factory()->create([
  1624. 'cart_id' => $cart->id,
  1625. 'product_id' => $product->id,
  1626. 'sku' => $product->sku,
  1627. 'quantity' => $additional['quantity'],
  1628. 'name' => $product->name,
  1629. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1630. 'base_price' => $price,
  1631. 'total' => $convertedPrice * $additional['quantity'],
  1632. 'base_total' => $price * $additional['quantity'],
  1633. 'weight' => $product->weight ?? 0,
  1634. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1635. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1636. 'type' => $product->type,
  1637. 'additional' => $additional,
  1638. ]);
  1639. $customerAddress = CustomerAddress::factory()->create([
  1640. 'cart_id' => $cart->id,
  1641. 'customer_id' => $customer->id,
  1642. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1643. ]);
  1644. $cartBillingAddress = CartAddress::factory()->create([
  1645. 'cart_id' => $cart->id,
  1646. 'customer_id' => $customer->id,
  1647. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1648. ]);
  1649. $cartShippingAddress = CartAddress::factory()->create([
  1650. 'cart_id' => $cart->id,
  1651. 'customer_id' => $customer->id,
  1652. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1653. ]);
  1654. $cartPayment = CartPayment::factory()->create([
  1655. 'cart_id' => $cart->id,
  1656. 'method' => $paymentMethod = 'cashondelivery',
  1657. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1658. ]);
  1659. $cartShippingRate = CartShippingRate::factory()->create([
  1660. 'carrier' => 'free',
  1661. 'carrier_title' => 'Free shipping',
  1662. 'method' => 'free_free',
  1663. 'method_title' => 'Free Shipping',
  1664. 'method_description' => 'Free Shipping',
  1665. 'cart_address_id' => $cartShippingAddress->id,
  1666. ]);
  1667. $order = Order::factory()->create([
  1668. 'cart_id' => $cart->id,
  1669. 'customer_id' => $customer->id,
  1670. 'customer_email' => $customer->email,
  1671. 'customer_first_name' => $customer->first_name,
  1672. 'customer_last_name' => $customer->last_name,
  1673. 'status' => 'processing',
  1674. 'sub_total_invoiced' => $product->price,
  1675. 'base_sub_total_invoiced' => $product->price,
  1676. ]);
  1677. $orderItem = OrderItem::factory()->create([
  1678. 'product_id' => $product->id,
  1679. 'order_id' => $order->id,
  1680. 'sku' => $product->sku,
  1681. 'type' => $product->type,
  1682. 'name' => $product->name,
  1683. 'qty_invoiced' => 1,
  1684. 'base_total_invoiced' => $product->price,
  1685. ]);
  1686. $orderBillingAddress = OrderAddress::factory()->create([
  1687. ...Arr::except($cartBillingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1688. 'cart_id' => $cart->id,
  1689. 'customer_id' => $customer->id,
  1690. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1691. 'order_id' => $order->id,
  1692. ]);
  1693. $orderShippingAddress = OrderAddress::factory()->create([
  1694. ...Arr::except($cartShippingAddress->toArray(), ['id', 'created_at', 'updated_at']),
  1695. 'cart_id' => $cart->id,
  1696. 'customer_id' => $customer->id,
  1697. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1698. 'order_id' => $order->id,
  1699. ]);
  1700. $orderPayment = OrderPayment::factory()->create([
  1701. 'order_id' => $order->id,
  1702. 'method' => 'cashondelivery',
  1703. ]);
  1704. $invoice = Invoice::factory()->create([
  1705. 'order_id' => $order->id,
  1706. 'state' => 'paid',
  1707. 'total_qty' => 1,
  1708. 'base_currency_code' => $order->base_currency_code,
  1709. 'channel_currency_code' => $order->channel_currency_code,
  1710. 'order_currency_code' => $order->order_currency_code,
  1711. 'email_sent' => 1,
  1712. 'discount_amount' => 0,
  1713. 'base_discount_amount' => 0,
  1714. 'sub_total' => $orderItem->base_price,
  1715. 'base_sub_total' => $orderItem->base_price,
  1716. 'grand_total' => $orderItem->price,
  1717. 'base_grand_total' => $orderItem->price,
  1718. ]);
  1719. $invoiceItem = InvoiceItem::factory()->create([
  1720. 'invoice_id' => $invoice->id,
  1721. 'order_item_id' => $orderItem->id,
  1722. 'name' => $orderItem->name,
  1723. 'sku' => $orderItem->sku,
  1724. 'qty' => 1,
  1725. 'price' => $orderItem->price,
  1726. 'base_price' => $orderItem->base_price,
  1727. 'total' => $orderItem->price,
  1728. 'base_total' => $orderItem->base_price,
  1729. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  1730. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  1731. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  1732. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  1733. 'product_id' => $orderItem->product_id,
  1734. 'product_type' => $orderItem->product_type,
  1735. 'additional' => $orderItem->additional,
  1736. ]);
  1737. $orderTransaction = OrderTransaction::factory()->create([
  1738. 'transaction_id' => md5(uniqid()),
  1739. 'type' => 'cashondelivery',
  1740. 'payment_method' => 'cashondelivery',
  1741. 'status' => $invoice->state,
  1742. 'order_id' => $invoice->order->id,
  1743. 'invoice_id' => $invoice->id,
  1744. 'amount' => $invoice->grand_total,
  1745. ]);
  1746. // Act and Assert.
  1747. $this->loginAsAdmin();
  1748. get(route('admin.reporting.sales.view', [
  1749. 'type' => 'total-sales',
  1750. ]))
  1751. ->assertOk()
  1752. ->assertSeeText(trans('admin::app.reporting.sales.index.total-sales'));
  1753. $cart->refresh();
  1754. $cartItem->refresh();
  1755. $cartBillingAddress->refresh();
  1756. $cartShippingAddress->refresh();
  1757. $orderBillingAddress->refresh();
  1758. $orderShippingAddress->refresh();
  1759. $order->refresh();
  1760. $orderItem->refresh();
  1761. $invoiceItem->refresh();
  1762. $orderTransaction->refresh();
  1763. $this->assertModelWise([
  1764. Cart::class => [
  1765. $this->prepareCart($cart),
  1766. ],
  1767. CartItem::class => [
  1768. $this->prepareCartItem($cartItem),
  1769. ],
  1770. CartPayment::class => [
  1771. $this->prepareCartPayment($cartPayment),
  1772. ],
  1773. CartAddress::class => [
  1774. $this->prepareAddress($cartBillingAddress),
  1775. ],
  1776. CartAddress::class => [
  1777. $this->prepareAddress($cartShippingAddress),
  1778. ],
  1779. CartShippingRate::class => [
  1780. $this->prepareCartShippingRate($cartShippingRate),
  1781. ],
  1782. CustomerAddress::class => [
  1783. $this->prepareAddress($customerAddress),
  1784. ],
  1785. Order::class => [
  1786. $this->prepareOrder($order),
  1787. ],
  1788. OrderItem::class => [
  1789. $this->prepareOrderItem($orderItem),
  1790. ],
  1791. OrderAddress::class => [
  1792. $this->prepareAddress($orderBillingAddress),
  1793. $this->prepareAddress($orderShippingAddress),
  1794. ],
  1795. OrderPayment::class => [
  1796. $this->prepareOrderPayment($orderPayment),
  1797. ],
  1798. Invoice::class => [
  1799. $this->prepareInvoice($order, $orderItem),
  1800. ],
  1801. InvoiceItem::class => [
  1802. $this->prepareInvoiceItem($invoiceItem),
  1803. ],
  1804. ]);
  1805. });
  1806. it('should export the sales stats', function () {
  1807. // Arrange.
  1808. $product = (new ProductFaker([
  1809. 'attributes' => [
  1810. 5 => 'new',
  1811. ],
  1812. 'attribute_value' => [
  1813. 'new' => [
  1814. 'boolean_value' => true,
  1815. ],
  1816. ],
  1817. ]))
  1818. ->getSimpleProductFactory()
  1819. ->create();
  1820. $customer = Customer::factory()->create();
  1821. $cart = Cart::factory()->create([
  1822. 'customer_id' => $customer->id,
  1823. 'customer_first_name' => $customer->first_name,
  1824. 'customer_last_name' => $customer->last_name,
  1825. 'customer_email' => $customer->email,
  1826. 'is_guest' => 0,
  1827. ]);
  1828. $customer = Customer::factory()->create();
  1829. $cart = Cart::factory()->create([
  1830. 'customer_id' => $customer->id,
  1831. 'customer_first_name' => $customer->first_name,
  1832. 'customer_last_name' => $customer->last_name,
  1833. 'customer_email' => $customer->email,
  1834. 'is_guest' => 0,
  1835. ]);
  1836. $additional = [
  1837. 'product_id' => $product->id,
  1838. 'rating' => '0',
  1839. 'is_buy_now' => '0',
  1840. 'quantity' => '1',
  1841. ];
  1842. $cartItem = CartItem::factory()->create([
  1843. 'cart_id' => $cart->id,
  1844. 'product_id' => $product->id,
  1845. 'sku' => $product->sku,
  1846. 'quantity' => $additional['quantity'],
  1847. 'name' => $product->name,
  1848. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1849. 'base_price' => $price,
  1850. 'total' => $convertedPrice * $additional['quantity'],
  1851. 'base_total' => $price * $additional['quantity'],
  1852. 'weight' => $product->weight ?? 0,
  1853. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1854. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1855. 'type' => $product->type,
  1856. 'additional' => $additional,
  1857. ]);
  1858. $customerAddress = CustomerAddress::factory()->create([
  1859. 'cart_id' => $cart->id,
  1860. 'customer_id' => $customer->id,
  1861. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1862. ]);
  1863. $cartBillingAddress = CartAddress::factory()->create([
  1864. 'cart_id' => $cart->id,
  1865. 'customer_id' => $customer->id,
  1866. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1867. ]);
  1868. $cartShippingAddress = CartAddress::factory()->create([
  1869. 'cart_id' => $cart->id,
  1870. 'customer_id' => $customer->id,
  1871. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1872. ]);
  1873. $cartPayment = CartPayment::factory()->create([
  1874. 'cart_id' => $cart->id,
  1875. 'method' => $paymentMethod = 'cashondelivery',
  1876. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1877. ]);
  1878. $cartShippingRate = CartShippingRate::factory()->create([
  1879. 'carrier' => 'free',
  1880. 'carrier_title' => 'Free shipping',
  1881. 'method' => 'free_free',
  1882. 'method_title' => 'Free Shipping',
  1883. 'method_description' => 'Free Shipping',
  1884. 'cart_address_id' => $cartShippingAddress->id,
  1885. ]);
  1886. $order = Order::factory()->create([
  1887. 'cart_id' => $cart->id,
  1888. 'customer_id' => $customer->id,
  1889. 'customer_email' => $customer->email,
  1890. 'customer_first_name' => $customer->first_name,
  1891. 'customer_last_name' => $customer->last_name,
  1892. ]);
  1893. $orderItem = OrderItem::factory()->create([
  1894. 'product_id' => $product->id,
  1895. 'order_id' => $order->id,
  1896. 'sku' => $product->sku,
  1897. 'type' => $product->type,
  1898. 'name' => $product->name,
  1899. ]);
  1900. $orderBillingAddress = OrderAddress::factory()->create([
  1901. 'cart_id' => $cart->id,
  1902. 'customer_id' => $customer->id,
  1903. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1904. ]);
  1905. $orderShippingAddress = OrderAddress::factory()->create([
  1906. 'cart_id' => $cart->id,
  1907. 'customer_id' => $customer->id,
  1908. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1909. ]);
  1910. $orderPayment = OrderPayment::factory()->create([
  1911. 'order_id' => $order->id,
  1912. ]);
  1913. // Act and Assert.
  1914. $this->loginAsAdmin();
  1915. $period = fake()->randomElement(['day', 'month', 'year']);
  1916. $start = Carbon::now();
  1917. if ($period === 'day') {
  1918. $end = $start->copy()->addDay();
  1919. } elseif ($period === 'month') {
  1920. $end = $start->copy()->addMonth();
  1921. } else {
  1922. $end = $start->copy()->addYear();
  1923. }
  1924. get(route('admin.reporting.sales.export', [
  1925. 'start' => $start->toDateString(),
  1926. 'end' => $end->toDateString(),
  1927. 'format' => $format = fake()->randomElement(['csv', 'xls']),
  1928. 'period' => $period,
  1929. 'type' => 'total-sales',
  1930. ]))
  1931. ->assertOk()
  1932. ->assertDownload('total-sales.'.$format);
  1933. $cart->refresh();
  1934. $cartItem->refresh();
  1935. $cartBillingAddress->refresh();
  1936. $cartShippingAddress->refresh();
  1937. $orderBillingAddress->refresh();
  1938. $orderShippingAddress->refresh();
  1939. $order->refresh();
  1940. $orderItem->refresh();
  1941. $this->assertModelWise([
  1942. Cart::class => [
  1943. $this->prepareCart($cart),
  1944. ],
  1945. CartItem::class => [
  1946. $this->prepareCartItem($cartItem),
  1947. ],
  1948. CartPayment::class => [
  1949. $this->prepareCartPayment($cartPayment),
  1950. ],
  1951. CartAddress::class => [
  1952. $this->prepareAddress($cartBillingAddress),
  1953. ],
  1954. CartAddress::class => [
  1955. $this->prepareAddress($cartShippingAddress),
  1956. ],
  1957. CartShippingRate::class => [
  1958. $this->prepareCartShippingRate($cartShippingRate),
  1959. ],
  1960. CustomerAddress::class => [
  1961. $this->prepareAddress($customerAddress),
  1962. ],
  1963. Order::class => [
  1964. $this->prepareOrder($order),
  1965. ],
  1966. OrderItem::class => [
  1967. $this->prepareOrderItem($orderItem),
  1968. ],
  1969. OrderAddress::class => [
  1970. $this->prepareAddress($orderBillingAddress),
  1971. $this->prepareAddress($orderShippingAddress),
  1972. ],
  1973. OrderPayment::class => [
  1974. $this->prepareOrderPayment($orderPayment),
  1975. ],
  1976. ]);
  1977. });