Advertisement
Badal_hs_shah

past price item

Jan 31st, 2023
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "RW3PastPriceItem.h"
  2. #import "RW3AlternativePriceReason.h"
  3.  
  4. #import "NSDate+RW3.h"
  5. #import "NSString+RW3.h"
  6.  
  7. @implementation RW3PastPriceItem
  8.  
  9. @dynamic upcPadded;
  10. @dynamic pricePerLb;
  11. @dynamic scanDate;
  12.  
  13. @dynamic regularCount;
  14. @dynamic regularPrice;
  15.  
  16. @dynamic activeCount;
  17. @dynamic activePrice;
  18. @dynamic activePriceReason;
  19.  
  20. @dynamic tertiaryCount;
  21. @dynamic tertiaryPrice;
  22. @dynamic tertiaryPriceReason;
  23.  
  24. - (NSNumber *)regularPriceTotal
  25. {
  26.    double priceValue = [self.regularPrice doubleValue];
  27.    double quantityValue = [self.regularCount doubleValue];
  28.    
  29.    priceValue = quantityValue ? priceValue / quantityValue : priceValue;
  30.    
  31.    return @(priceValue);
  32. }
  33.  
  34. - (NSNumber *)activePriceTotal
  35. {
  36.    double priceValue = [self.activePrice doubleValue];
  37.    double quantityValue = [self.activeCount doubleValue];
  38.    
  39.    priceValue = quantityValue ? priceValue / quantityValue : priceValue;
  40.    
  41.    return @(priceValue);
  42. }
  43.  
  44. - (NSNumber *)tertiaryPriceTotal
  45. {
  46.    double priceValue = [self.tertiaryPrice doubleValue];
  47.    double quantityValue = [self.tertiaryCount doubleValue];
  48.    
  49.    priceValue = quantityValue ? priceValue / quantityValue : priceValue;
  50.    
  51.    return @(priceValue);
  52. }
  53.  
  54. #pragma mark - JSON
  55.  
  56. - (void)populateWithJSON:(NSDictionary *)JSON alternativePriceReasons:(NSArray *)alternativePriceReasons
  57. {
  58.    if (JSON[@"Upc"]) self.upcPadded = JSON[@"Upc"];
  59.    
  60.    self.regularCount = JSON[@"RegularQuantity"] == nil ? @0 : JSON[@"RegularQuantity"];
  61.    self.regularPrice = JSON[@"RegularQuantityPrice"] == nil ? @0 : JSON[@"RegularQuantityPrice"];
  62.    if(JSON[@"RegularPricePerLb"] != nil) self.pricePerLb = [JSON[@"RegularPricePerLb"] boolValue];
  63.    
  64.    self.activeCount = JSON[@"ActiveQuantity"] == nil ? @0 : JSON[@"ActiveQuantity"];
  65.    self.activePrice = JSON[@"ActiveQuantityPrice"] == nil ? @0 : JSON[@"ActiveQuantityPrice"];
  66.    if(JSON[@"ActivePricePerLb"] != nil) self.pricePerLb = [JSON[@"ActivePricePerLb"] boolValue];
  67.    
  68.    self.tertiaryCount = JSON[@"TertiaryQuantity"] == nil ? @0 : JSON[@"TertiaryQuantity"];
  69.    self.tertiaryPrice = JSON[@"TertiaryQuantityPrice"] == nil ? @0 : JSON[@"TertiaryQuantityPrice"];
  70.    if(JSON[@"TertiaryPricePerLb"] != nil) self.pricePerLb = [JSON[@"TertiaryPricePerLb"] boolValue];
  71.    
  72.    if (JSON[@"ScanDate"]) self.scanDate = [JSON[@"ScanDate"] changeDateStringToDate];
  73.    
  74.    if (JSON[@"PecosActivePriceReasonId"]) {
  75.       NSNumber *activePriceReasonID = JSON[@"PecosActivePriceReasonId"];
  76.       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", activePriceReasonID];
  77.       NSArray *filteredAlternateivePriceReasons = [alternativePriceReasons filteredArrayUsingPredicate:predicate];
  78.       self.activePriceReason = [filteredAlternateivePriceReasons firstObject];
  79.    }
  80.    
  81.    if (JSON[@"PecosTertiaryPriceReasonId"]) {
  82.       NSNumber *tertiaryPriceReasonID = JSON[@"PecosTertiaryPriceReasonId"];
  83.       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", tertiaryPriceReasonID];
  84.       NSArray *filteredAlternateivePriceReasons = [alternativePriceReasons filteredArrayUsingPredicate:predicate];
  85.       self.tertiaryPriceReason = [filteredAlternateivePriceReasons firstObject];
  86.    }
  87. }
  88.  
  89. - (NSDictionary *)JSON
  90. {
  91.    NSMutableDictionary *JSON = [[NSMutableDictionary alloc] init];
  92.    
  93.    if (self.upcPadded) JSON[@"UpcPadded"] = self.upcPadded;
  94.    
  95.    if (![self.regularPrice isEqualToNumber:@0])
  96.    {
  97.       JSON[@"RegularQuantityPrice"] = self.regularPrice;
  98.       JSON[@"RegularPricePerLb"] = @(self.pricePerLb);
  99.       JSON[@"RegularQuantity"] = self.regularCount;
  100.    }
  101.    
  102.    if (![self.activePrice isEqualToNumber:@0])
  103.    {
  104.       JSON[@"ActiveQuantityPrice"] = self.activePrice;
  105.       JSON[@"ActivePricePerLb"] = @(self.pricePerLb);
  106.       JSON[@"ActiveQuantity"] = self.activeCount;
  107.       JSON[@"PecosActivePriceReasonId"] = self.activePriceReason.id;
  108.    }
  109.    
  110.    if (![self.tertiaryPrice isEqualToNumber:@0])
  111.    {
  112.       JSON[@"TertiaryQuantityPrice"] = self.tertiaryPrice;
  113.       JSON[@"TertiaryPricePerLb"] = @(self.pricePerLb);
  114.       JSON[@"TertiaryQuantity"] = self.tertiaryCount;
  115.       JSON[@"PecosTertiaryPriceReasonId"] = self.tertiaryPriceReason.id;
  116.    }
  117.    
  118.    if (self.scanDate) JSON[@"ScanDate"] = [self.scanDate toDateTimeString];
  119.    
  120.    return [JSON copy];
  121. }
  122.  
  123. #pragma mark -
  124.  
  125. - (NSString*)stringValue
  126. {
  127.    NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:[self JSON]];
  128.    return [NSString stringWithFormat:@"%@", data];
  129. }
  130.  
  131. @end
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement