//    Posics Saleculator - Billing System.
//    Copyright (C) 2009 Posics
//
//    This file is part of Posics Saleculator.
//
// Managed discount chooser with manual amount/% fallback.

import com.posics.pos.inventory.DiscountInfo;

action = "change";
if (sales.hasAppliedDiscount()) {
    action = sales.selectAppliedDiscountAction();
}

if ("remove".equals(action)) {
    sales.removeAppliedDiscount();
} else if ("change".equals(action)) {
    discounts = sales.getEligibleDiscounts();
    selected = sales.selectDiscount(discounts);

    if (selected != null) {
        sales.applyDiscount((DiscountInfo) selected);
    } else {
        String discounttext = sales.readText("Select a managed discount or enter a manual discount:\nUse 10% for percentage or 10 for fixed amount.");
        if (discounttext != null && discounttext.trim().length() > 0) {
            discounttext = discounttext.trim();
            if (discounttext.endsWith("%")) {
                sales.applyManualDiscountPercent(Double.parseDouble(discounttext.substring(0, discounttext.length() - 1)) / 100.00);
            } else {
                sales.applyManualDiscountAmount(Double.parseDouble(discounttext));
            }
        }
    }
}
