Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "RW3PastPriceItem.h"
- #import "RW3AlternativePriceReason.h"
- #import "NSDate+RW3.h"
- #import "NSString+RW3.h"
- @implementation RW3PastPriceItem
- @dynamic upcPadded;
- @dynamic pricePerLb;
- @dynamic scanDate;
- @dynamic regularCount;
- @dynamic regularPrice;
- @dynamic activeCount;
- @dynamic activePrice;
- @dynamic activePriceReason;
- @dynamic tertiaryCount;
- @dynamic tertiaryPrice;
- @dynamic tertiaryPriceReason;
- - (NSNumber *)regularPriceTotal
- {
- double priceValue = [self.regularPrice doubleValue];
- double quantityValue = [self.regularCount doubleValue];
- priceValue = quantityValue ? priceValue / quantityValue : priceValue;
- return @(priceValue);
- }
- - (NSNumber *)activePriceTotal
- {
- double priceValue = [self.activePrice doubleValue];
- double quantityValue = [self.activeCount doubleValue];
- priceValue = quantityValue ? priceValue / quantityValue : priceValue;
- return @(priceValue);
- }
- - (NSNumber *)tertiaryPriceTotal
- {
- double priceValue = [self.tertiaryPrice doubleValue];
- double quantityValue = [self.tertiaryCount doubleValue];
- priceValue = quantityValue ? priceValue / quantityValue : priceValue;
- return @(priceValue);
- }
- #pragma mark - JSON
- - (void)populateWithJSON:(NSDictionary *)JSON alternativePriceReasons:(NSArray *)alternativePriceReasons
- {
- if (JSON[@"Upc"]) self.upcPadded = JSON[@"Upc"];
- self.regularCount = JSON[@"RegularQuantity"] == nil ? @0 : JSON[@"RegularQuantity"];
- self.regularPrice = JSON[@"RegularQuantityPrice"] == nil ? @0 : JSON[@"RegularQuantityPrice"];
- if(JSON[@"RegularPricePerLb"] != nil) self.pricePerLb = [JSON[@"RegularPricePerLb"] boolValue];
- self.activeCount = JSON[@"ActiveQuantity"] == nil ? @0 : JSON[@"ActiveQuantity"];
- self.activePrice = JSON[@"ActiveQuantityPrice"] == nil ? @0 : JSON[@"ActiveQuantityPrice"];
- if(JSON[@"ActivePricePerLb"] != nil) self.pricePerLb = [JSON[@"ActivePricePerLb"] boolValue];
- self.tertiaryCount = JSON[@"TertiaryQuantity"] == nil ? @0 : JSON[@"TertiaryQuantity"];
- self.tertiaryPrice = JSON[@"TertiaryQuantityPrice"] == nil ? @0 : JSON[@"TertiaryQuantityPrice"];
- if(JSON[@"TertiaryPricePerLb"] != nil) self.pricePerLb = [JSON[@"TertiaryPricePerLb"] boolValue];
- if (JSON[@"ScanDate"]) self.scanDate = [JSON[@"ScanDate"] changeDateStringToDate];
- if (JSON[@"PecosActivePriceReasonId"]) {
- NSNumber *activePriceReasonID = JSON[@"PecosActivePriceReasonId"];
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", activePriceReasonID];
- NSArray *filteredAlternateivePriceReasons = [alternativePriceReasons filteredArrayUsingPredicate:predicate];
- self.activePriceReason = [filteredAlternateivePriceReasons firstObject];
- }
- if (JSON[@"PecosTertiaryPriceReasonId"]) {
- NSNumber *tertiaryPriceReasonID = JSON[@"PecosTertiaryPriceReasonId"];
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", tertiaryPriceReasonID];
- NSArray *filteredAlternateivePriceReasons = [alternativePriceReasons filteredArrayUsingPredicate:predicate];
- self.tertiaryPriceReason = [filteredAlternateivePriceReasons firstObject];
- }
- }
- - (NSDictionary *)JSON
- {
- NSMutableDictionary *JSON = [[NSMutableDictionary alloc] init];
- if (self.upcPadded) JSON[@"UpcPadded"] = self.upcPadded;
- if (![self.regularPrice isEqualToNumber:@0])
- {
- JSON[@"RegularQuantityPrice"] = self.regularPrice;
- JSON[@"RegularPricePerLb"] = @(self.pricePerLb);
- JSON[@"RegularQuantity"] = self.regularCount;
- }
- if (![self.activePrice isEqualToNumber:@0])
- {
- JSON[@"ActiveQuantityPrice"] = self.activePrice;
- JSON[@"ActivePricePerLb"] = @(self.pricePerLb);
- JSON[@"ActiveQuantity"] = self.activeCount;
- JSON[@"PecosActivePriceReasonId"] = self.activePriceReason.id;
- }
- if (![self.tertiaryPrice isEqualToNumber:@0])
- {
- JSON[@"TertiaryQuantityPrice"] = self.tertiaryPrice;
- JSON[@"TertiaryPricePerLb"] = @(self.pricePerLb);
- JSON[@"TertiaryQuantity"] = self.tertiaryCount;
- JSON[@"PecosTertiaryPriceReasonId"] = self.tertiaryPriceReason.id;
- }
- if (self.scanDate) JSON[@"ScanDate"] = [self.scanDate toDateTimeString];
- return [JSON copy];
- }
- #pragma mark -
- - (NSString*)stringValue
- {
- NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:[self JSON]];
- return [NSString stringWithFormat:@"%@", data];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement