Add void and printer type
This commit is contained in:
@@ -532,7 +532,6 @@ func (p *OrderProcessorImpl) VoidOrder(ctx context.Context, req *models.VoidOrde
|
||||
}
|
||||
|
||||
if req.Type == "ALL" {
|
||||
// Update order status to cancelled and mark as voided in a single transaction
|
||||
if err := p.orderRepo.VoidOrderWithStatus(ctx, req.OrderID, entities.OrderStatusCancelled, req.Reason, voidedBy); err != nil {
|
||||
return fmt.Errorf("failed to void order: %w", err)
|
||||
}
|
||||
@@ -552,62 +551,53 @@ func (p *OrderProcessorImpl) VoidOrder(ctx context.Context, req *models.VoidOrde
|
||||
return fmt.Errorf("order item not found: %w", err)
|
||||
}
|
||||
|
||||
// Verify the order item belongs to this order
|
||||
if orderItem.OrderID != req.OrderID {
|
||||
return fmt.Errorf("order item does not belong to this order")
|
||||
}
|
||||
|
||||
// Validate void quantity
|
||||
if itemVoid.Quantity > orderItem.Quantity {
|
||||
return fmt.Errorf("void quantity cannot exceed original quantity for item %d", itemVoid.OrderItemID)
|
||||
}
|
||||
|
||||
// Calculate voided amounts
|
||||
voidedAmount := float64(itemVoid.Quantity) * orderItem.UnitPrice
|
||||
voidedCost := float64(itemVoid.Quantity) * orderItem.UnitCost
|
||||
|
||||
totalVoidedAmount += voidedAmount
|
||||
totalVoidedCost += voidedCost
|
||||
|
||||
// Void the order item
|
||||
if err := p.orderItemRepo.VoidOrderItem(ctx, orderItemID, itemVoid.Quantity, req.Reason, voidedBy); err != nil {
|
||||
return fmt.Errorf("failed to void order item %d: %w", itemVoid.OrderItemID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Get outlet information for tax rate
|
||||
outlet, err := p.outletRepo.GetByID(ctx, order.OutletID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("outlet not found: %w", err)
|
||||
}
|
||||
|
||||
// Update order totals
|
||||
order.Subtotal -= totalVoidedAmount
|
||||
order.TotalCost -= totalVoidedCost
|
||||
order.TaxAmount = order.Subtotal * outlet.TaxRate // Recalculate tax using outlet's tax rate
|
||||
order.TotalAmount = order.Subtotal + order.TaxAmount - order.DiscountAmount
|
||||
|
||||
// Update the order
|
||||
if err := p.orderRepo.Update(ctx, order); err != nil {
|
||||
return fmt.Errorf("failed to update order totals: %w", err)
|
||||
}
|
||||
|
||||
// Check if all items are voided, then void the entire order
|
||||
remainingItems, err := p.orderItemRepo.GetByOrderID(ctx, req.OrderID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get remaining order items: %w", err)
|
||||
}
|
||||
|
||||
allItemsVoided := true
|
||||
hasActiveItems := false
|
||||
for _, item := range remainingItems {
|
||||
if item.Quantity > 0 {
|
||||
allItemsVoided = false
|
||||
if item.Status != entities.OrderItemStatusCancelled {
|
||||
hasActiveItems = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if allItemsVoided {
|
||||
// Update order status to cancelled and mark as voided when all items are voided
|
||||
if !hasActiveItems {
|
||||
if err := p.orderRepo.VoidOrderWithStatus(ctx, req.OrderID, entities.OrderStatusCancelled, req.Reason, voidedBy); err != nil {
|
||||
return fmt.Errorf("failed to void order after all items voided: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user