/*
        Copyright (c) 1996, NeXT Software, Inc.
        All rights reserved.

        You may freely copy, distribute and reuse the code in this example.
        NeXT disclaims any warranty of any kind, expressed or implied,
        as to its fitness for any particular use.
*/
#import <BusinessLogic/Fee.h>
#import <BusinessLogic/Rental.h>
#import <BusinessLogic/Unit.h>
#import <BusinessLogic/Product.h>

@implementation Fee

//
// I'm using this method to set some default values in
// the object.
//
- (void)awakeFromInsertionInEditingContext:(EOEditingContext *)ctx
{
    // set default values for insertion
    if(!amount) {
        id rentalTerms = [[[rental unit] product] rentalTerms];
        amount = [[rentalTerms valueForKey:@"cost"] copy];
    }
    if(!feeType) {
        static EOGlobalID *gid = nil;

        // look up the default fee type
        if(!gid) {
            EOFetchSpecification *fetchSpec;
            NSArray *results;

            fetchSpec = [EOFetchSpecification fetchSpecificationWithEntityName:@"FeeType"
                           qualifier:[EOQualifier qualifierWithQualifierFormat:@"feeTypeID = 1"]
                                                           sortOrderings:nil];

            results = [ctx objectsWithFetchSpecification:fetchSpec
                                          editingContext:ctx];

            gid = [ctx globalIDForObject:[results objectAtIndex:0]];
        }
        feeType = [[ctx faultForGlobalID:gid
                          editingContext:ctx] retain];
    }
}

- (NSException *)validateForDelete
{
    if(![self isPaid])
        return [NSException validationExceptionWithFormat:@"You can't remove an unpaid fee"];

    return [super validateForDelete];
}


- (void)pay
{
    // Normally we would define setDatePaid: and it would do the willChange:
    [self willChange];
    datePaid = [[NSCalendarDate calendarDate] retain];

    // notify rental to perhaps remove itself if all the fees are paid.
    [rental feePaid];
}

- (BOOL)isPaid
{
    return datePaid ? YES : NO;
}

- initLateFeeWithDateDue:(NSCalendarDate *)date rental:(Rental *)aRental
{
    static EOGlobalID *gid = nil;
    int elapsedDays;
    id ctx = [aRental editingContext];
    
    [super init];

    // look up the late fee type
    if(!gid) {
        EOFetchSpecification *fetchSpec;
        NSArray *results;

        fetchSpec = [EOFetchSpecification fetchSpecificationWithEntityName:@"FeeType"
                                                                 qualifier:[EOQualifier qualifierWithQualifierFormat:@"feeTypeID = 2"]
                                                               sortOrderings:nil];

                results = [ctx objectsWithFetchSpecification:fetchSpec
                                              editingContext:ctx];

                gid = [ctx globalIDForObject:[results objectAtIndex:0]];
    }
    feeType = [[ctx faultForGlobalID:gid
                      editingContext:ctx] retain];

   [[NSCalendarDate calendarDate] years:NULL months:NULL days:&elapsedDays hours:NULL minutes:NULL seconds:NULL sinceDate:date];

   // $3 per days... This is the late fee !
   amount = [[NSDecimalNumber numberWithInt:(elapsedDays + 1)*3] retain];

   return self;
}

- (void)dealloc
{
    [datePaid release];
    [amount release];
    [rental release];
    [feeType release];
    [super dealloc];
}

- (NSString *)description { return [self eoDescription]; }

@end
