home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / Fee.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  3.4 KB  |  118 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import <BusinessLogic/Fee.h>
  10. #import <BusinessLogic/Rental.h>
  11. #import <BusinessLogic/Unit.h>
  12. #import <BusinessLogic/Product.h>
  13.  
  14. @implementation Fee
  15.  
  16. //
  17. // I'm using this method to set some default values in
  18. // the object.
  19. //
  20. - (void)awakeFromInsertionInEditingContext:(EOEditingContext *)ctx
  21. {
  22.     // set default values for insertion
  23.     if(!amount) {
  24.         id rentalTerms = [[[rental unit] product] rentalTerms];
  25.         amount = [[rentalTerms valueForKey:@"cost"] copy];
  26.     }
  27.     if(!feeType) {
  28.         static EOGlobalID *gid = nil;
  29.  
  30.         // look up the default fee type
  31.         if(!gid) {
  32.             EOFetchSpecification *fetchSpec;
  33.             NSArray *results;
  34.  
  35.             fetchSpec = [EOFetchSpecification fetchSpecificationWithEntityName:@"FeeType"
  36.                            qualifier:[EOQualifier qualifierWithQualifierFormat:@"feeTypeID = 1"]
  37.                                                            sortOrderings:nil];
  38.  
  39.             results = [ctx objectsWithFetchSpecification:fetchSpec
  40.                                           editingContext:ctx];
  41.  
  42.             gid = [ctx globalIDForObject:[results objectAtIndex:0]];
  43.         }
  44.         feeType = [[ctx faultForGlobalID:gid
  45.                           editingContext:ctx] retain];
  46.     }
  47. }
  48.  
  49. - (NSException *)validateForDelete
  50. {
  51.     if(![self isPaid])
  52.         return [NSException validationExceptionWithFormat:@"You can't remove an unpaid fee"];
  53.  
  54.     return [super validateForDelete];
  55. }
  56.  
  57.  
  58. - (void)pay
  59. {
  60.     // Normally we would define setDatePaid: and it would do the willChange:
  61.     [self willChange];
  62.     datePaid = [[NSCalendarDate calendarDate] retain];
  63.  
  64.     // notify rental to perhaps remove itself if all the fees are paid.
  65.     [rental feePaid];
  66. }
  67.  
  68. - (BOOL)isPaid
  69. {
  70.     return datePaid ? YES : NO;
  71. }
  72.  
  73. - initLateFeeWithDateDue:(NSCalendarDate *)date rental:(Rental *)aRental
  74. {
  75.     static EOGlobalID *gid = nil;
  76.     int elapsedDays;
  77.     id ctx = [aRental editingContext];
  78.     
  79.     [super init];
  80.  
  81.     // look up the late fee type
  82.     if(!gid) {
  83.         EOFetchSpecification *fetchSpec;
  84.         NSArray *results;
  85.  
  86.         fetchSpec = [EOFetchSpecification fetchSpecificationWithEntityName:@"FeeType"
  87.                                                                  qualifier:[EOQualifier qualifierWithQualifierFormat:@"feeTypeID = 2"]
  88.                                                                sortOrderings:nil];
  89.  
  90.                 results = [ctx objectsWithFetchSpecification:fetchSpec
  91.                                               editingContext:ctx];
  92.  
  93.                 gid = [ctx globalIDForObject:[results objectAtIndex:0]];
  94.     }
  95.     feeType = [[ctx faultForGlobalID:gid
  96.                       editingContext:ctx] retain];
  97.  
  98.    [[NSCalendarDate calendarDate] years:NULL months:NULL days:&elapsedDays hours:NULL minutes:NULL seconds:NULL sinceDate:date];
  99.  
  100.    // $3 per days... This is the late fee !
  101.    amount = [[NSDecimalNumber numberWithInt:(elapsedDays + 1)*3] retain];
  102.  
  103.    return self;
  104. }
  105.  
  106. - (void)dealloc
  107. {
  108.     [datePaid release];
  109.     [amount release];
  110.     [rental release];
  111.     [feeType release];
  112.     [super dealloc];
  113. }
  114.  
  115. - (NSString *)description { return [self eoDescription]; }
  116.  
  117. @end
  118.