home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / Transaction.m < prev    next >
Encoding:
Text File  |  1996-08-31  |  3.7 KB  |  121 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 "Transaction.h"
  10. #import "OneObjectDataSource.h"
  11. #import "ProductReturn.h"
  12.  
  13. #import <BusinessLogic/BusinessLogic.h>
  14. #import <BusinessLogic/NSObjectAdditions.h>
  15. #import <BusinessLogic/Fee.h>
  16.  
  17. @implementation Transaction
  18.  
  19. - initWithCustomerGlobalID:(EOGlobalID *)gid
  20. {
  21.     EOActionAssociation *association;
  22.  
  23.     [super init];
  24.  
  25.     //
  26.     // Load the nib file
  27.     //
  28.     // Make sure the nib create a NEW editing context
  29.     //
  30.     [EOEditingContext setSubstitutionEditingContext:nil];
  31.  
  32.     if (![NSBundle loadNibNamed:@"Transaction" owner:self]) {
  33.         NSLog(@"Unable to load nib file: Transaction.nib, exiting");
  34.         exit(1);
  35.     }
  36.  
  37.     //
  38.     // Create and install the association
  39.     //
  40.     association = [[[EOActionAssociation alloc] initWithObject:insertButton] autorelease];
  41.     [association bindAspect:@"action" displayGroup:customerDisplayGroup key:@"rentUnit:"];
  42.     [association bindAspect:@"argument" displayGroup:unitDisplayGroup key:@"self"];
  43.     [association bindAspect:@"enabled" displayGroup:unitDisplayGroup key:@"isAvailableForRent"];
  44.     [association establishConnection];
  45.  
  46.     //
  47.     // Install the customer in a custom datasource
  48.     //
  49.     [customerDisplayGroup setDataSource:[[[OneObjectDataSource alloc] initWithGlobalID:gid editingContext:[[customerDisplayGroup dataSource] editingContext]] autorelease]];
  50.  
  51.     // Fetch the customer
  52.     //
  53.     [customerDisplayGroup fetch];
  54.  
  55.     return self;
  56. }
  57.  
  58. - (void)displayGroup:(EODisplayGroup *)displayGroup didFetchObjects:(NSArray *)objects
  59. {
  60.     NSDecimalNumber *cost = [objects sumArrayWithKey:@"amount"];
  61.     NSDecimalNumber *taxToAdd = [cost decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@".085"]];
  62.  
  63.     [total setObjectValue:cost];
  64.     [tax setObjectValue:taxToAdd];
  65.     [totalDue setObjectValue:[cost decimalNumberByAdding:taxToAdd]];
  66. }
  67.  
  68. - (void)paymentDone:(id)sender
  69. {
  70.     EOEditingContext *ec = [[customerDisplayGroup dataSource] editingContext];
  71.     EOUndoManager *manager = [ec undoManager];
  72.     BOOL saveSucceed = YES;
  73.  
  74.     // begin grouping before payments so we can roll them back upon error
  75.     [manager beginUndoGrouping];  
  76.  
  77.     // Mark all the fees as paid.
  78.     [[feesDisplayGroup displayedObjects] makeObjectsPerform:@selector(pay)];
  79.  
  80.     // Attempt to save changes and catch an errors
  81.     NS_DURING
  82.         [ec saveChanges];
  83.     NS_HANDLER
  84.         NSRunAlertPanel(@"Ouch", [localException reason], nil, nil, nil);
  85.         saveSucceed = NO;
  86.     NS_ENDHANDLER
  87.  
  88.     [manager endUndoGrouping];
  89.  
  90.     if(saveSucceed && ![ec hasChanges]) {
  91.         // I should release everything.  For now, just close window
  92.         // and leak everything !
  93.         NSRunAlertPanel(@"Rental", @"Payment successfully recorded", nil, nil, nil);
  94.         [[insertButton window] close];
  95.     } else {
  96.         // Undo the payment.
  97.         [manager disableUndoRegistration];  // disable so we don't register redos
  98.         [manager undo];
  99.         [manager reenableUndoRegistration];
  100.     }
  101. }
  102.  
  103. - (void)productReturn:(id)sender
  104. {
  105.     id selectedCustomer = [memberDisplayGroup selectedObject];
  106.     EOGlobalID *customerGID;
  107.  
  108.     customerGID = [[selectedCustomer editingContext] globalIDForObject:selectedCustomer];
  109.  
  110.     [[ProductReturn alloc] initWithCustomerGlobalID:customerGID parentEditingContext:[selectedCustomer editingContext]];
  111. }
  112.  
  113. - (void)revert:(id)sender
  114. {
  115.     EOEditingContext *editingContext = [[customerDisplayGroup dataSource] editingContext];
  116.  
  117.     [editingContext revert:sender];
  118. }
  119.  
  120. @end
  121.