Add void and printer type
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"apskel-pos-be/internal/entities"
|
||||
@@ -103,31 +104,65 @@ func (r *OrderItemRepositoryImpl) UpdateStatus(ctx context.Context, id uuid.UUID
|
||||
func (r *OrderItemRepositoryImpl) VoidOrderItem(ctx context.Context, id uuid.UUID, voidQuantity int, reason string, voidedBy uuid.UUID) error {
|
||||
now := time.Now()
|
||||
|
||||
// Get current order item
|
||||
var orderItem entities.OrderItem
|
||||
if err := r.db.WithContext(ctx).First(&orderItem, "id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Calculate new voided quantity
|
||||
newVoidedQuantity := orderItem.RefundQuantity + voidQuantity // Using refund_quantity field for voided quantity
|
||||
if voidQuantity >= orderItem.Quantity {
|
||||
voidedAmount := float64(voidQuantity) * orderItem.UnitPrice
|
||||
|
||||
// Determine if fully or partially voided
|
||||
isFullyVoided := newVoidedQuantity >= orderItem.Quantity
|
||||
isPartiallyVoided := newVoidedQuantity > 0 && newVoidedQuantity < orderItem.Quantity
|
||||
updates := map[string]interface{}{
|
||||
"refund_quantity": voidQuantity,
|
||||
"refund_amount": voidedAmount,
|
||||
"is_partially_refunded": false,
|
||||
"is_fully_refunded": true,
|
||||
"refund_reason": reason,
|
||||
"refunded_at": now,
|
||||
"refunded_by": voidedBy,
|
||||
"status": entities.OrderItemStatusCancelled,
|
||||
}
|
||||
|
||||
// Calculate voided amount
|
||||
voidedAmount := float64(voidQuantity) * orderItem.UnitPrice
|
||||
return r.db.WithContext(ctx).Model(&entities.OrderItem{}).
|
||||
Where("id = ?", id).
|
||||
Updates(updates).Error
|
||||
}
|
||||
|
||||
voidedOrderItem := entities.OrderItem{
|
||||
OrderID: orderItem.OrderID,
|
||||
ProductID: orderItem.ProductID,
|
||||
ProductVariantID: orderItem.ProductVariantID,
|
||||
Quantity: voidQuantity,
|
||||
UnitPrice: orderItem.UnitPrice,
|
||||
TotalPrice: float64(voidQuantity) * orderItem.UnitPrice,
|
||||
UnitCost: orderItem.UnitCost,
|
||||
TotalCost: float64(voidQuantity) * orderItem.UnitCost,
|
||||
RefundAmount: float64(voidQuantity) * orderItem.UnitPrice,
|
||||
RefundQuantity: voidQuantity,
|
||||
IsPartiallyRefunded: false,
|
||||
IsFullyRefunded: true,
|
||||
RefundReason: &reason,
|
||||
RefundedAt: &now,
|
||||
RefundedBy: &voidedBy,
|
||||
Modifiers: orderItem.Modifiers,
|
||||
Notes: orderItem.Notes,
|
||||
Metadata: orderItem.Metadata,
|
||||
Status: entities.OrderItemStatusCancelled,
|
||||
}
|
||||
|
||||
if err := r.db.WithContext(ctx).Create(&voidedOrderItem).Error; err != nil {
|
||||
return fmt.Errorf("failed to create voided order item: %w", err)
|
||||
}
|
||||
|
||||
remainingQuantity := orderItem.Quantity - voidQuantity
|
||||
remainingTotalPrice := float64(remainingQuantity) * orderItem.UnitPrice
|
||||
remainingTotalCost := float64(remainingQuantity) * orderItem.UnitCost
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"refund_quantity": newVoidedQuantity, // Reusing refund_quantity field for voided quantity
|
||||
"refund_amount": orderItem.RefundAmount + voidedAmount,
|
||||
"is_partially_refunded": isPartiallyVoided, // Reusing refunded flags for voided status
|
||||
"is_fully_refunded": isFullyVoided,
|
||||
"refund_reason": reason,
|
||||
"refunded_at": now,
|
||||
"refunded_by": voidedBy,
|
||||
"status": entities.OrderItemStatusCancelled, // Mark as cancelled when voided
|
||||
"quantity": remainingQuantity,
|
||||
"total_price": remainingTotalPrice,
|
||||
"total_cost": remainingTotalCost,
|
||||
"updated_at": now,
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).Model(&entities.OrderItem{}).
|
||||
|
||||
Reference in New Issue
Block a user