<commit_before>package org.broadleafcommerce.offer.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanComparator;
import org.broadleafcommerce.offer.domain.CandidateItemOffer;
import org.broadleafcommerce.offer.domain.CandidateItemOfferImpl;
import org.broadleafcommerce.offer.domain.Offer;
import org.broadleafcommerce.offer.domain.OfferCode;
import org.broadleafcommerce.order.domain.Order;
import org.broadleafcommerce.order.domain.OrderItem;
import org.broadleafcommerce.pricing.service.PricingService;
import org.broadleafcommerce.profile.domain.Customer;
import org.broadleafcommerce.type.OfferDiscountType;
import org.broadleafcommerce.type.OfferType;
import org.broadleafcommerce.util.money.Money;
import org.springframework.stereotype.Service;

/**
 * The Class OfferServiceImpl.
 */
@Service("offerService")
public class OfferServiceImpl implements OfferService {

    @Resource
    private PricingService pricingService;

    /*
     * (non-Javadoc)
     * @see org.broadleafcommerce.offer.service.OfferService#consumeOffer(org.broadleafcommerce.offer.domain.Offer, org.broadleafcommerce.profile.domain.Customer)
     */
    @Override
    public boolean consumeOffer(Offer offer, Customer customer) {
        // TODO Auto-generated method stub
        return false;
    }

    /*
     * (non-Javadoc)
     * @see org.broadleafcommerce.offer.service.OfferService#applyOffersToOrder(java.util.List, org.broadleafcommerce.order.domain.Order)
     */
    @SuppressWarnings("unchecked")
    public void applyOffersToOrder(List<Offer> offers, Order order) {
        List<Offer> qualifiedOrderOffers = new ArrayList<Offer>();
        List<CandidateItemOffer> qualifiedItemOffers = new ArrayList<CandidateItemOffer>();
        order.removeAllOffers();
        order.setCandidateOffers(new ArrayList<Offer>());
        order = pricingService.calculateOrderTotal(order);
        List<Offer> offersWithValidDates = removeOutOfDateOffers(offers);
        if (offersWithValidDates != null) {
            //
            // . Pass One:
            //
            for (Offer offer : offersWithValidDates) {
                //
                // . Evaluate all offers and compute their discount amount as if they were the only offer on the order
                //
                if(offer.getType().equals(OfferType.ORDER)){
                    // TODO: Determine if order qualifies for offer
                    // Assume for now that all orders qualify
                    offer = calculateAtomOfferDiscount(offer, order.getSubTotal());
                    qualifiedOrderOffers.add(offer);
                }
                if(offer.getType().equals(OfferType.ORDER_ITEM)){
                    for (OrderItem orderItem : order.getOrderItems()) {
                        // TODO: Determine if orderItem qualifies for offer
                        // Assume for now that all orderItems qualify
                        if(couldOfferApplyToOrderItem(offer, order, orderItem))
                            qualifiedItemOffers.add(new CandidateItemOfferImpl(orderItem, offer ));
                    }
                }
                if(offer.getType().equals(OfferType.FULLFILLMENT_GROUP)){
                    // TODO: Handle Offer calculation for offer type of fullfillment group
                }
            }
            //
            // . Create a sorted list sorted by priority asc then amount desc
            //
            Collections.sort(qualifiedOrderOffers, new BeanComparator("discountedPrice"));
            Collections.reverse(qualifiedOrderOffers);
            Collections.sort(qualifiedOrderOffers, new BeanComparator("priority"));
            Collections.sort(qualifiedItemOffers, new BeanComparator("discountedPrice"));
            Collections.reverse(qualifiedItemOffers);
            Collections.sort(qualifiedItemOffers, new BeanComparator("priority"));
            //
            // . Add offers that could be used on the order to the order.candidateOffers and item.candidateOffers lists respectively
            //
            order.setCandidateOffers(qualifiedOrderOffers);<commit_msg>Fixed some basic if logic and collections sorting<commit_after>package org.broadleafcommerce.offer.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.broadleafcommerce.offer.domain.CandidateItemOffer;
import org.broadleafcommerce.offer.domain.CandidateItemOfferImpl;
import org.broadleafcommerce.offer.domain.Offer;
import org.broadleafcommerce.offer.domain.OfferCode;
import org.broadleafcommerce.order.domain.Order;
import org.broadleafcommerce.order.domain.OrderItem;
import org.broadleafcommerce.pricing.service.PricingService;
import org.broadleafcommerce.profile.domain.Customer;
import org.broadleafcommerce.type.OfferDiscountType;
import org.broadleafcommerce.type.OfferType;
import org.broadleafcommerce.util.money.Money;
import org.springframework.stereotype.Service;

/**
 * The Class OfferServiceImpl.
 */
@Service("offerService")
public class OfferServiceImpl implements OfferService {

    @Resource
    private PricingService pricingService;

    /*
     * (non-Javadoc)
     * @see org.broadleafcommerce.offer.service.OfferService#consumeOffer(org.broadleafcommerce.offer.domain.Offer, org.broadleafcommerce.profile.domain.Customer)
     */
    @Override
    public boolean consumeOffer(Offer offer, Customer customer) {
        // TODO Auto-generated method stub
        return false;
    }

    /*
     * (non-Javadoc)
     * @see org.broadleafcommerce.offer.service.OfferService#applyOffersToOrder(java.util.List, org.broadleafcommerce.order.domain.Order)
     */
    @SuppressWarnings("unchecked")
    public void applyOffersToOrder(List<Offer> offers, Order order) {
        List<Offer> qualifiedOrderOffers = new ArrayList<Offer>();
        List<CandidateItemOffer> qualifiedItemOffers = new ArrayList<CandidateItemOffer>();
        order.removeAllOffers();
        order.setCandidateOffers(new ArrayList<Offer>());
        order = pricingService.calculateOrderTotal(order);
        List<Offer> offersWithValidDates = removeOutOfDateOffers(offers);
        if (offersWithValidDates != null) {
            //
            // . Pass One:
            //
            for (Offer offer : offersWithValidDates) {
                //
                // . Evaluate all offers and compute their discount amount as if they were the only offer on the order
                //
                if(offer.getType().equals(OfferType.ORDER)){
                    // TODO: Determine if order qualifies for offer
                    // Assume for now that all orders qualify
                    offer = calculateAtomOfferDiscount(offer, order.getSubTotal());
                    qualifiedOrderOffers.add(offer);
                } else if(offer.getType().equals(OfferType.ORDER_ITEM)){
                    for (OrderItem orderItem : order.getOrderItems()) {
                        // TODO: Determine if orderItem qualifies for offer
                        // Assume for now that all orderItems qualify
                        if(couldOfferApplyToOrderItem(offer, order, orderItem))
                            qualifiedItemOffers.add(new CandidateItemOfferImpl(orderItem, offer ));
                    }
                } else if(offer.getType().equals(OfferType.FULLFILLMENT_GROUP)){
                    // TODO: Handle Offer calculation for offer type of fullfillment group
                }
            }
            //
            // . Create a sorted list sorted by priority asc then amount desc
            //
            Collections.sort(qualifiedOrderOffers, ComparatorUtils.reversedComparator(new BeanComparator("discountedPrice")));
            Collections.sort(qualifiedOrderOffers, new BeanComparator("priority"));

            //
            // . Add offers that could be used on the order to the order.candidateOffers and item.candidateOffers lists respectively
            //
            order.setCandidateOffers(qualifiedOrderOffers);
{'fraction_non_alphanumeric': 0.0886499402628435, 'fraction_numerical': 0.0, 'mean_word_length': 2.6554585152838426, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5532119', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2131, 'n_words': 444}