@@ -560,10 +560,11 @@ private function getDiff( $old, $new ) {
560
560
*/
561
561
private function computeDiff ( $ old , $ new , $ operation )
562
562
{
563
- if ( $ operation === ' decrease ' ) {
564
- return ( ( $ old - $ new ) / $ old ) * 100 ;
563
+ if ( $ new == 0 ) {
564
+ return 100 ;
565
565
} else {
566
- return ( ( $ new - $ old ) / $ old ) * 100 ;
566
+ $ change = ( ( $ old - $ new ) / $ new ) * 100 ;
567
+ return $ operation === 'increase ' ? abs ( $ change ) : $ change ;
567
568
}
568
569
}
569
570
@@ -632,16 +633,14 @@ private function getBestRecords( $previousDates, $sort )
632
633
$ orderProductTable . '.unit_name as unit_name ' ,
633
634
$ orderProductTable . '.product_id as product_id ' ,
634
635
$ orderProductTable . '.name as name ' ,
635
- $ orderTable . '.created_at as created_at ' ,
636
636
DB ::raw ( 'SUM( quantity ) as quantity ' ),
637
637
DB ::raw ( 'SUM( total_price ) as total_price ' ),
638
638
DB ::raw ( 'SUM( ' . env ( 'DB_PREFIX ' ) . $ orderProductTable . '.tax_value ) as tax_value ' ),
639
639
])
640
640
->groupBy (
641
641
$ orderProductTable . '.unit_name ' ,
642
642
$ orderProductTable . '.product_id ' ,
643
- $ orderProductTable . '.name ' ,
644
- $ orderTable . '.created_at ' ,
643
+ $ orderProductTable . '.name '
645
644
)
646
645
->orderBy ( $ sorting [ 'column ' ], $ sorting [ 'direction ' ] )
647
646
->join ( $ orderTable , $ orderTable . '.id ' , '= ' , $ orderProductTable . '.order_id ' )
@@ -657,9 +656,10 @@ private function getBestRecords( $previousDates, $sort )
657
656
foreach ( $ previousDates [ 'current ' ][ 'products ' ] as $ id => &$ product ) {
658
657
$ default = new stdClass ;
659
658
$ default ->total_price = 0 ;
659
+ $ default ->quantity = 0 ;
660
660
661
- $ oldProduct = collect ( $ previousDates [ 'previous ' ][ 'products ' ] )->filter ( function ( $ product ) use ( $ id ) {
662
- return $ product ->product_id === $ id ;
661
+ $ oldProduct = collect ( $ previousDates [ 'previous ' ][ 'products ' ] )->filter ( function ( $ previousProduct ) use ( $ product ) {
662
+ return $ previousProduct ->product_id === $ product -> product_id ;
663
663
})->first () ?: $ default ;
664
664
665
665
$ product ->old_total_price = $ oldProduct ->total_price ;
@@ -669,7 +669,7 @@ private function getBestRecords( $previousDates, $sort )
669
669
$ product ->total_price
670
670
) : 100 ;
671
671
672
- $ product ->evolution = $ product ->total_price > $ oldProduct ->total_price ? 'progress ' : 'regress ' ;
672
+ $ product ->evolution = $ product ->quantity > $ oldProduct ->quantity ? 'progress ' : 'regress ' ;
673
673
}
674
674
675
675
$ previousDates [ 'current ' ][ 'total_price ' ] = collect ( $ previousDates [ 'current ' ][ 'products ' ] )
0 commit comments