NewTransportViewController.m 14.8 KB
Newer Older
陈俊俊's avatar
陈俊俊 committed
1 2 3 4 5 6 7 8 9 10 11 12
//
//  NewTransportViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/9/6.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "NewTransportViewController.h"
#import "TopTransportView.h"
#import "BottomTransportView.h"
#import "TransportPurchaseViewController.h"
陈俊俊's avatar
陈俊俊 committed
13 14
#import "NewCostViewController.h"
#import "TransportPdtDetail.h"
陈俊俊's avatar
陈俊俊 committed
15
#import "FeeAcountDetail.h"
陈俊俊's avatar
陈俊俊 committed
16 17
#import "TransportViewController.h"
#import "ShopDetaileViewController.h"
陈俊俊's avatar
陈俊俊 committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
typedef enum : NSUInteger {
    SaveTag = 7000,
    CommitTag,
} BtnTag;

#define TopHeight 284
#define BottomHeight 300
#define SpaceHeight 20
#define BottomViewHeight 60
@interface NewTransportViewController ()<TopTransportViewDelegate,UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    TopTransportView *_sheetView;
    BottomTransportView *_bottomView;
    id uuidObject;
    id billNumberObject;
    NSNumber *versionObject;
}

@end

@implementation NewTransportViewController
陈俊俊's avatar
陈俊俊 committed
40 41 42
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
陈俊俊's avatar
陈俊俊 committed
43 44 45
- (instancetype)init{
    self = [super init];
    if (self) {
陈俊俊's avatar
陈俊俊 committed
46 47 48
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choseTransportPurchase) name:KNOTIFICATION_ChoseTransportPurchase object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addTransportCost:) name:KNOTIFICATION_AddTransportCost object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPurchaseProduct:) name:KNOTIFICATION_AddPurchaseProduct object:nil];
陈俊俊's avatar
陈俊俊 committed
49 50 51 52 53 54 55 56 57 58 59
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //布局
    [self bulidLayout];
    uuidObject = [NSNull null];
    billNumberObject = [NSNull null];
    versionObject = [NSNumber numberWithInteger:0];
陈俊俊's avatar
陈俊俊 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    if (self.transport) {
        [self getDataFromServer];
    }
}

- (void)getDataFromServer{
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                NSDictionary *dictData = data[@"data"];
                Transport *transport = [[Transport alloc]init];
                [transport setValuesForKeysWithDictionary:dictData];
                self.transport = transport;
                [strongSelf fetchtTransportDetail];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"     无记录     "];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getTransportResultWithTransportUuid:self.transport.uuid success:succ failure:fail];
陈俊俊's avatar
陈俊俊 committed
92
}
陈俊俊's avatar
陈俊俊 committed
93 94 95 96 97 98 99 100

- (void)fetchtTransportDetail
{
    _sheetView.transport = self.transport;
    [_bottomView refreshCost:self.transport.accountDetails];
    [_bottomView refreshTranProduct:self.transport.pdtDetails];
}

陈俊俊's avatar
陈俊俊 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
- (void)bulidLayout{
    self.view.backgroundColor  = XXFBgColor;
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, ScreenSize.width, ScreenSize.height- 64 - BottomViewHeight )];
    _scrollView.showsHorizontalScrollIndicator  = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(ScreenSize.width, TopHeight+ BottomHeight + SpaceHeight*2);
    [self.view addSubview:_scrollView];
    
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenSize.height - BottomViewHeight - 64, ScreenSize.width, BottomViewHeight)];
    UIButton *saveBtn =  [IBTCustomButtom creatButtonWithFrame:CGRectMake(15, 8, (ScreenSize.width - 15*3)/2, 44) target:self sel:@selector(btnClick:) tag:SaveTag image:nil title:@"保存" titleColor: [UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_SAVE_COLOR];
    [footView addSubview:saveBtn];
    
    UIButton *commitBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(CGRectGetMaxX(saveBtn.frame) + 15, 8, (ScreenSize.width - 15*3)/2, 44) target:self sel:@selector(btnClick:) tag:CommitTag image:nil title:@"提交" titleColor: [UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_COMMIT_COLOR];
    [footView addSubview:commitBtn];
    
    [self.view addSubview:footView];
    
    _sheetView = [[TopTransportView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, TopHeight)];
    _sheetView.delegate = self;
    [_scrollView addSubview:_sheetView];
    
陈俊俊's avatar
陈俊俊 committed
123
    _bottomView = [[BottomTransportView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_sheetView.frame) + 20, ScreenSize.width,BottomHeight) withHidden:NO];
陈俊俊's avatar
陈俊俊 committed
124 125 126 127 128 129 130 131
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_bottomView];
}
- (void)btnClick:(UIButton *)btn{
    switch (btn.tag) {
        case SaveTag:
        {
            if ([self checkTransport]) {
陈俊俊's avatar
陈俊俊 committed
132
                [self getDataFromServer:TRANSPORT_STATE_INITIAL msg:@"正在保存..."];
陈俊俊's avatar
陈俊俊 committed
133 134 135 136 137
            }
        }
            break;
        case CommitTag:{
            if ([self checkTransport]) {
陈俊俊's avatar
陈俊俊 committed
138
                [self getDataFromServer:TRANSPORT_STATE_UNRECEIVED msg:@"正在提交..."];
陈俊俊's avatar
陈俊俊 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                
            }
        }
            break;
        default:
            break;
    }
}
- (void)getDataFromServer:(NSString *)state msg:(NSString *)msg{
    //保存
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message = data[@"message"];
            if (success == 1) {
                //成功
                if ([state isEqualToString:PURCHASE_STATE_INITIAL]) {//保存成功
                    NSString *uuidS = data[@"data"][@"uuid"];
                    NSString *billNumberS = data[@"data"][@"billNumber"];
                    NSNumber *versionN = data[@"data"][@"version"];
                    uuidObject = uuidS;
                    billNumberObject = billNumberS;
                    versionObject = versionN;
                    [IBTLoadingView showTips:[NSString stringWithFormat:@"当前单据%@已保存成功",billNumberS]];
                }else{//提交成功
陈俊俊's avatar
陈俊俊 committed
165 166
                    TransportViewController *svc = [TransportViewController new];
                    [self PushViewController:svc animated:YES];
陈俊俊's avatar
陈俊俊 committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                }
            }else{
                [IBTLoadingView showTips:message];
            }
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    
    if(self.transport.uuid.length > 0 && uuidObject == [NSNull null]){
        uuidObject = self.transport.uuid;
    }
    if (self.transport.billnumber > 0 && billNumberObject == [NSNull null]) {
        billNumberObject = self.transport.billnumber;
    }
陈俊俊's avatar
陈俊俊 committed
184 185 186
    if (self.transport && [versionObject integerValue] == 0) {
        versionObject = self.transport.version;
    }
陈俊俊's avatar
陈俊俊 committed
187
    NSMutableArray *billProducts = [NSMutableArray array];
陈俊俊's avatar
陈俊俊 committed
188 189
    for (TransportPdtDetail *pdtDetail in _bottomView.productVC.transportProductArr) {
        [billProducts  addObject:[pdtDetail dictForCommit]];
陈俊俊's avatar
陈俊俊 committed
190 191 192 193 194 195
    }
    NSMutableArray *costs = [NSMutableArray array];
    for (FeeAcountDetail *fee in _bottomView.costVC.costArr) {
        [costs  addObject:[fee dictForCommit]];
    }
//    self.state = state;
陈俊俊's avatar
陈俊俊 committed
196
    
陈俊俊's avatar
陈俊俊 committed
197
    NSDictionary *dict = @{@"uuid":uuidObject,
陈俊俊's avatar
陈俊俊 committed
198
                           @"version":versionObject,
陈俊俊's avatar
陈俊俊 committed
199
                           @"billnumber":billNumberObject,
陈俊俊's avatar
陈俊俊 committed
200
                           @"enterprise":[ICRUserUtil sharedInstance].orgId,
陈俊俊's avatar
陈俊俊 committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214
                           @"state":state,
                           @"warehouseUuid":[IBTCommon checkString:_sheetView.warehouseUuid],
                           @"warehouseCode":[IBTCommon checkString:_sheetView.warehouseCode],
                           @"warehouseName":[IBTCommon checkString:_sheetView.warehouseName],
                           @"rwarehouseUuid":[IBTCommon checkString:_sheetView.rwarehouseUuid],
                           @"rwarehouseCode":[IBTCommon checkString:_sheetView.rwarehouseCode],
                           @"rwarehouseName":[IBTCommon checkString:_sheetView.rwarehouseName],
                           @"carnumber":[IBTCommon checkString:_sheetView.carnumber],
                           @"type":[IBTCommon checkString:_sheetView.type],
                           @"carphone":[IBTCommon checkString:_sheetView.carphone],
                           @"note":_sheetView.note,
                           @"pdtDetails":billProducts,
                           @"accountDetails":costs};
    [IBTLoadingView showProgressLabel:msg];
陈俊俊's avatar
陈俊俊 committed
215
    [[ICRHTTPController sharedController] saveTransportWithData:dict success:succ failure:fail];
陈俊俊's avatar
陈俊俊 committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}



- (BOOL)checkTransport{
    _sheetView.note = _sheetView.remarkTextView.text;
    _sheetView.carnumber = _sheetView.carTextFiled.text;
    _sheetView.carphone = _sheetView.phoneTextFiled.text;

    if (_sheetView.warehouseName.length == 0 ) {
        ShowMessage(@"发货仓库不能为空");
        return NO;
    }
    if (_sheetView.rwarehouseUuid.length == 0 ) {
        ShowMessage(@"收货仓库不能为空");
        return NO;
    }
    
    if (_sheetView.type.length == 0 ) {
        ShowMessage(@"运输类型不能为空");
        return NO;
    }
    if (_sheetView.carnumber.length == 0) {
        ShowMessage(@"车牌号不能为空");
        return NO;
    }
    if (_sheetView.carphone.length == 0) {
        ShowMessage(@"司机电话不能为空");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
246
    if (_bottomView.productVC.transportProductArr.count == 0) {
陈俊俊's avatar
陈俊俊 committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        ShowMessage(@"商品不能为空");
        return NO;
    }
    if (_bottomView.costVC.costArr.count == 0) {
        ShowMessage(@"费用不能为空");
        return NO;
    }
    return YES;
}



- (void)choseTransportPurchase{
    TransportPurchaseViewController *tpv = [TransportPurchaseViewController new];
    tpv.getProchaseProduct = ^(NSArray *products){
陈俊俊's avatar
陈俊俊 committed
262 263 264
        if (products.count > 0) {
            [_bottomView reProduct:products];
        }
陈俊俊's avatar
陈俊俊 committed
265 266 267
    };
    [self PushViewController:tpv animated:YES];
}
陈俊俊's avatar
陈俊俊 committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
- (void)addTransportCost:(NSNotification *)fination{
    
    NewCostViewController *nvc = [NewCostViewController new];
    NSIndexPath *indexPath = [[fination userInfo] objectForKey:@"indexPath"];
    FeeAcountDetail *fee = [[fination userInfo] objectForKey:@"feeAccountDetail"];

    if (indexPath) {
        nvc.navTitle = @"编辑费用";
        nvc.indexPath = indexPath;
        nvc.accountDetail = fee;
    }else{
        nvc.navTitle = @"添加费用";
    }
    nvc.getTransportCost = ^(FeeAcountDetail *fee,NSIndexPath *indexPath){
        if (fee) {
            [_bottomView refreshCost:fee indexPath:indexPath];
        }
    };
    nvc.deleteTransportCost = ^(FeeAcountDetail *fee,NSIndexPath *indexPath){
        if (fee) {
            [_bottomView refreshDelCost:fee indexPath:indexPath];
        }
    };
    [self PushViewController:nvc animated:YES];

}
- (void)addPurchaseProduct:(NSNotification *)fication{
陈俊俊's avatar
陈俊俊 committed
295 296 297
    TransportPdtDetail *billProduct = [[fication userInfo] objectForKey:@"transportPdtDetail"];
    NSInteger indexTag = [[[fication userInfo] objectForKey:@"indexTag"] integerValue];
    
陈俊俊's avatar
陈俊俊 committed
298
    ShopDetaileViewController *svc = [[ShopDetaileViewController alloc] init];
陈俊俊's avatar
陈俊俊 committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    if (billProduct) {
        ShopDetail *shopDetail=[self coverShopDetail:billProduct];
        shopDetail.IsXiuGai=YES;
        svc.choseShopDetail = ^(ShopDetail *Detail){
            TransportPdtDetail *tdetail = [self coverTransportPdtDetail:Detail];
            if (Detail.IsDeleted) {
                [_bottomView refreshDelProduct:tdetail tag:indexTag];
            }else{
                [_bottomView refreshEditProduct:tdetail tag:indexTag];
            }
        };
        svc.shopDetail=shopDetail;
    }else{
        // 回调
        svc.choseShopDetail = ^(ShopDetail *shopDetail){
            TransportPdtDetail *tdetail = [self coverTransportPdtDetail:shopDetail];
            [_bottomView refreshProduct:tdetail];
        };
    }
    
陈俊俊's avatar
陈俊俊 committed
319 320 321
    svc.title = [IBTCommon localizableString:@"AddShopDetail"];
    [self PushViewController:svc animated:YES];
}
陈俊俊's avatar
陈俊俊 committed
322

陈俊俊's avatar
陈俊俊 committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

- (TransportPdtDetail *)coverTransportPdtDetail:(ShopDetail *)shopDetail{
    TransportPdtDetail *billProduct = [[TransportPdtDetail alloc]init];
    billProduct.uuid = shopDetail.uuid;
    billProduct.productUuid = shopDetail.product_uuid;
    billProduct.productCode = shopDetail.product_code;
    billProduct.productName = shopDetail.merchandise;
    billProduct.qpc = [NSNumber numberWithFloat:[shopDetail.packageSpecification floatValue]];
    billProduct.unit = shopDetail.packageUnit;
    billProduct.qty = [NSNumber numberWithFloat:[shopDetail.packageQuantity floatValue]];
    billProduct.price = [NSNumber numberWithFloat:[shopDetail.packageUnitPrice floatValue]];
    billProduct.baseQty = [NSNumber numberWithFloat:[shopDetail.foundationQuantity floatValue]];
    billProduct.total = [NSNumber numberWithFloat:[shopDetail.totalMoney floatValue]];
    billProduct.note = shopDetail.remark;
    billProduct.qpcStr = @"fdsfdsfdsa";
    return billProduct;
}
- (ShopDetail *)coverShopDetail:(TransportPdtDetail *)billProduct{
    ShopDetail *shopDetail = [[ShopDetail alloc]init];
    shopDetail.uuid = billProduct.uuid;
    shopDetail.product_uuid = billProduct.productUuid;
    shopDetail.product_code = billProduct.productCode;
    shopDetail.merchandise  = billProduct.productName;
    shopDetail.packageSpecification  =  [billProduct.qpc stringValue];
    shopDetail.packageUnit = billProduct.unit;
    shopDetail.packageQuantity = [billProduct.qty stringValue];
    shopDetail.packageUnitPrice  = [billProduct.price stringValue];
#warning 少一个包装单价
    shopDetail.foundationQuantity= [billProduct.price stringValue];
    shopDetail.foundationUnitPrice= [billProduct.baseQty  stringValue];
    shopDetail.totalMoney= [billProduct.total stringValue];
    shopDetail.remark  = billProduct.note;
    return shopDetail;
    
}


陈俊俊's avatar
陈俊俊 committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
#pragma mark - TopPurchaseViewDelegate

- (void)pushNextViewController:(id)vc{
    [self PushViewController:vc animated:YES];
}
#pragma mark - 协议方法
- (void)hiddenKeyBoard{
    [self keyboardHidden];
}
- (void)keyboardHidden{
    [_sheetView.phoneTextFiled resignFirstResponder];
    [_sheetView.carTextFiled resignFirstResponder];
    [_sheetView.remarkTextView resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

陈俊俊's avatar
陈俊俊 committed
378

陈俊俊's avatar
陈俊俊 committed
379 380 381 382 383 384 385 386 387 388 389
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end