Skip to content

Commit

Permalink
fix quantity sold calculation in breeder area
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin POCHAT committed Jan 24, 2022
1 parent 26d198b commit 3ab39cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public float calculatePercentageSold(Delivery delivery) {
List<Order> orders = new ArrayList<>();
orderRepository.getOrdersByDelivery(delivery).spliterator().forEachRemaining(orders::add);
float totalWeightSold = orders.stream()
.filter(order -> Stream.of(PAYED, BOOKED).anyMatch(status -> status.equals(order.getStatus())))
.filter(order -> order.getStatus().isSold())
.flatMap(order -> order.getOrderedItems().stream())
.map(item -> item.getQuantity() * item.getBatch().getProduct().getNetWeight())
.reduce(0f, Float::sum);
Expand All @@ -45,7 +45,7 @@ public float calculatePercentageSold(Delivery delivery) {

public void updateQuantitySoldInBatches(Delivery delivery) {
StreamSupport.stream(orderItemRepository.findByDelivery(delivery).spliterator(), false)
.filter(orderItem -> Stream.of(PAYED, BOOKED).anyMatch(status -> status.equals(orderItem.getOrder().getStatus()))) // Stream<Order>
.filter(orderItem -> orderItem.getOrder().getStatus().isSold()) // Stream<Order>
.collect(Collectors.groupingBy(OrderItem::getBatch, Collectors.summingInt(OrderItem::getQuantity))) // Map<Batch, Long>
.entrySet()
.forEach(entry -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public enum OrderStatus {
PAYED,
DELIVERED,
CANCELLED;

public boolean isSold() {
return this.equals(BOOKED) || this.equals(PAYED) || this.equals(DELIVERED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DeliveryService } from 'src/app/breeder-area/services/delivery.service'
import { DeliveryOrdersComponent } from '../delivery-orders/delivery-orders.component';
import { PieChartComponent } from 'src/app/commons/components/piechart/piechart.component';
import { DomSanitizer } from '@angular/platform-browser';
import { OrderStatusUtils } from 'src/app/commons/models/order-status.model';

@Component({
selector: 'app-delivery',
Expand Down Expand Up @@ -43,9 +44,8 @@ export class DeliveryComponent implements OnInit {
this.deliveryService.setOrderTotalPrice(order);
});
const netWeightSoldCalculator = (accumulator: number, order: Order): number => accumulator + order.totalNetWeight;
this.netWeightSold = this.slaughter.delivery.orders.reduce(netWeightSoldCalculator, 0);
this.netWeightSold = this.slaughter.delivery.orders.filter(order => OrderStatusUtils.isSold(order.status)).reduce(netWeightSoldCalculator, 0);
this.pieChartComponent.setRadius(this.slaughter.animal.meatWeight > 0 && this.netWeightSold > 0 ? (360 * this.netWeightSold / this.slaughter.animal.meatWeight) : 0);

});
}
}
Expand Down
4 changes: 4 additions & 0 deletions ui/src/app/commons/models/order-status.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export class OrderStatusUtils {
return '';
}
}

static isSold(orderStatus: OrderStatus) {
return [OrderStatus.BOOKED, OrderStatus.PAYED, OrderStatus.BOOKED].includes(orderStatus);
}
}

0 comments on commit 3ab39cd

Please sign in to comment.