SalesLog.m 4.15 KB
//
//  SalesLog.m
//  DUREX Vendor Control
//
//  Created by Imanol Barba on 07/09/14.
//  Copyright (c) 2014 Emmoco. All rights reserved.
//

#import "SalesLog.h"

@implementation SalesLog

- (id) init
{
    [self setSales:[[NSMutableArray alloc] init]];
    return self;
}

- (void) setResponseValue:(NSString *)response
{
    [self setResponse: response];
    [self parseResponse];
}

- (void) parseResponse
{
    NSInteger currentSale = 0;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *saleDate = [[NSDateComponents alloc] init];
    NSInteger currentYear = [[calendar components:units fromDate:[NSDate date]] year];
    currentYear = (currentYear/100)*100;
    while(![[[self response] substringWithRange:NSMakeRange((currentSale*SALE_STRING_LENGTH), 4)] isEqualToString:@"P2P2"])
    {
        Sale *sale = [[Sale alloc] init];
        [saleDate setYear:(currentYear + [[[self response] substringWithRange:NSMakeRange(2 + currentSale*SALE_STRING_LENGTH, 2)] intValue])];
        [saleDate setMonth:[[[self response] substringWithRange:NSMakeRange(4 + currentSale*SALE_STRING_LENGTH, 2)] intValue]];
        [saleDate setDay:[[[self response] substringWithRange:NSMakeRange(6 + currentSale*SALE_STRING_LENGTH, 2)] intValue]];
        [saleDate setHour:[[[self response] substringWithRange:NSMakeRange(8 + currentSale*SALE_STRING_LENGTH, 2)] intValue]];
        [saleDate setMinute:[[[self response] substringWithRange:NSMakeRange(10 + currentSale*SALE_STRING_LENGTH, 2)] intValue]];
        [sale setSaleTime:[saleDate date]];
        NSLog(@"date: %@",saleDate);
        [[sale moneyPaid] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(12 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:0];
        [[sale moneyPaid] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(14 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:1];
        [[sale moneyPaid] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(16 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:2];
        [[sale moneyPaid] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(18 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:3];
        [[sale moneyPaid] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(20 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:4];
        NSLog(@"moneyPaid: %@",[sale moneyPaid]);
        [sale setChannel:[[self response] substringWithRange:NSMakeRange(22 + currentSale*SALE_STRING_LENGTH, 1)]];
        NSLog(@"channel: %@",[sale channel]);
        [sale setProductCode:[[self response] substringWithRange:NSMakeRange(23 + currentSale*SALE_STRING_LENGTH, 1)]];
         NSLog(@"productCode: %@",[sale productCode]);
        [sale setNormalPrice: [NSString stringWithFormat:@"%d,%02d",[[[self response] substringWithRange:NSMakeRange(24 + currentSale*SALE_STRING_LENGTH, 2)] intValue],[[[self response] substringWithRange:NSMakeRange(26 + currentSale*SALE_STRING_LENGTH, 2)] intValue]]];
        NSLog(@"normalPrice: %@",[sale normalPrice]);
        [sale setDiscountedPrice: [NSString stringWithFormat:@"%d,%02d",[[[self response] substringWithRange:NSMakeRange(28 + currentSale*SALE_STRING_LENGTH, 2)] intValue],[[[self response] substringWithRange:NSMakeRange(30 + currentSale*SALE_STRING_LENGTH, 2)] intValue]]];
        NSLog(@"discountedPrice: %@",[sale discountedPrice]);
        [[sale moneyReturned] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(32 + currentSale*SALE_STRING_LENGTH, 2)] intValue]] atIndex:0];
        [[sale moneyReturned] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:NSMakeRange(34 + currentSale*SALE_STRING_LENGTH , 2)] intValue]] atIndex:1];
        NSLog(@"moneyReturned: %@",[sale moneyReturned]);
        [[self sales] insertObject:sale atIndex:currentSale];
        currentSale++;
    }
    NSLog(@"%@",[self sales]);
}

@end