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

#import "NewPurchaseViewController.h"
#import "TopPurchaseView.h"
陈俊俊's avatar
陈俊俊 committed
11
#import "ProductBillViewController.h"
n22's avatar
n22 committed
12
#import "PurchaseBillProduct.h"
陈俊俊's avatar
陈俊俊 committed
13
#import "PurchaseViewController.h"
陈俊俊's avatar
陈俊俊 committed
14 15
#import "ShopDetaileViewController.h"
#import "PurchaseBillProduct.h"
16
#import "GXFPopView.h"
陈俊俊's avatar
陈俊俊 committed
17
#import "NoticeProduct.h"
n22's avatar
n22 committed
18
#define TopHeight 328
n22's avatar
n22 committed
19 20 21 22 23 24 25 26
#define BottomHeight 300
#define SpaceHeight 20
#define BottomViewHeight 60
typedef enum : NSUInteger {
    SaveTag = 8000,
    CommitTag,
} BtnTag;

陈俊俊's avatar
陈俊俊 committed
27
@interface NewPurchaseViewController ()<TopPurchaseViewDelegate>
n22's avatar
n22 committed
28 29 30 31
{
    UIScrollView *_scrollView;
    TopPurchaseView *_purchaseView;
    UIView *_bottomView;
陈俊俊's avatar
陈俊俊 committed
32 33 34 35
    ProductBillViewController *_pvc ;
    id uuidObject;
    id billNumberObject;
    NSNumber *versionObject;
n22's avatar
n22 committed
36
}
n22's avatar
n22 committed
37
@property (nonatomic,strong)NSString *state;
38
@property (nonatomic,strong)GXFPopView *popView;
n22's avatar
n22 committed
39

n22's avatar
n22 committed
40 41 42 43
@end

@implementation NewPurchaseViewController

n22's avatar
n22 committed
44 45 46 47 48
- (instancetype)init{
    self = [super init];
    if (self) {
        //监听值的改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setProductTotalPrice:) name:SetProductTotalPrice object:nil];
陈俊俊's avatar
陈俊俊 committed
49
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPurchaseProduct:) name:KNOTIFICATION_AddPurchaseProduct object:nil];
n22's avatar
n22 committed
50 51 52 53
    }
    return self;
}

n22's avatar
n22 committed
54 55 56 57
- (void)viewDidLoad {
    [super viewDidLoad];
    //布局
    [self bulidLayout];
陈俊俊's avatar
陈俊俊 committed
58 59 60 61
    //数据默认值
    uuidObject = [NSNull null];
    billNumberObject = [NSNull null];
    versionObject = [NSNumber numberWithInteger:0];
陈俊俊's avatar
陈俊俊 committed
62 63 64
    if (self.purchaseBill) {
        [self getDataFromServer];
    }
陈俊俊's avatar
陈俊俊 committed
65 66 67 68 69 70 71
    if (self.purchaseNotice) {
        _purchaseView.noticeUuid = self.purchaseNotice.uuid;
        _purchaseView.noticeNumber = self.purchaseNotice.billnumber;
        //转换
        NSMutableArray *arr = [self coverPurchaseBillProduct:self.purchaseNotice.products];
        _pvc.productArr = arr;
    }
陈俊俊's avatar
陈俊俊 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}
- (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"];
                PurchaseBill *bill = [[PurchaseBill alloc]init];
                [bill setValuesForKeysWithDictionary:dictData];
                self.purchaseBill = bill;
                [strongSelf prepareDataInCell];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"     无记录     "];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
陈俊俊's avatar
陈俊俊 committed
99
    [[ICRHTTPController sharedController] getPurchaseResultWithPurchaseUuid:self.purchaseBill.uuid success:succ failure:fail];
陈俊俊's avatar
陈俊俊 committed
100 101 102 103 104 105 106 107 108 109 110 111
}
//赋值
- (void)prepareDataInCell{
    _purchaseView.bill = self.purchaseBill;
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *productDict in self.purchaseBill.products) {
        PurchaseBillProduct *billProduct = [[PurchaseBillProduct alloc]init];
        [billProduct setValuesForKeysWithDictionary:productDict];
        [arr addObject:billProduct];
    }
    _pvc.productArr = arr;
   [_pvc.tableView reloadData];
n22's avatar
n22 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

#pragma mark - 布局
- (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)];
陈俊俊's avatar
陈俊俊 committed
126
    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];
n22's avatar
n22 committed
127 128
    [footView addSubview:saveBtn];
    
陈俊俊's avatar
陈俊俊 committed
129
    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];
n22's avatar
n22 committed
130 131 132 133 134 135 136 137 138 139 140 141
    [footView addSubview:commitBtn];
    
    [self.view addSubview:footView];

    _purchaseView = [[TopPurchaseView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, TopHeight)];
    _purchaseView.delegate = self;
    [_scrollView addSubview:_purchaseView];
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_purchaseView.frame) + 20, ScreenSize.width,BottomHeight)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_bottomView];
    
陈俊俊's avatar
陈俊俊 committed
142
    _pvc = [[ProductBillViewController alloc]init];
陈俊俊's avatar
陈俊俊 committed
143 144
    _pvc.viewFrame = _bottomView.bounds;
    [_bottomView addSubview:_pvc.view];
n22's avatar
n22 committed
145 146 147
    
}

n22's avatar
n22 committed
148 149 150 151 152 153 154 155 156 157
- (void)setProductTotalPrice:(NSNotification *)fication{
    //计算总金额
    //其他费用 + 商品总金额
    float otherPrice = [_purchaseView.otherPriceFiled.text floatValue];
    NSMutableArray *purchaseProduct = _pvc.productArr;
    float totalPrice = 0;
    for (PurchaseBillProduct *billProduct in purchaseProduct) {
        totalPrice += [billProduct.total floatValue];
    }
    _purchaseView.purchasePriceLabel.text = [NSString stringWithFormat:@"%.2f",totalPrice + otherPrice];
陈俊俊's avatar
陈俊俊 committed
158 159
    _purchaseView.chargePurchase = [NSNumber numberWithFloat:otherPrice];
    _purchaseView.total = [NSNumber numberWithFloat:totalPrice+otherPrice];
n22's avatar
n22 committed
160
}
陈俊俊's avatar
陈俊俊 committed
161
- (void)addPurchaseProduct:(NSNotification *)fication{
162 163 164 165 166 167
//    if (!self.popView) {
//        NSArray *arr = @[@"添加商品明细",@"选择发运单"];
//        self.popView = [[GXFPopView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height) withArr:arr];
//        [AppWindow addSubview:self.popView];
//    }
    
陈俊俊's avatar
陈俊俊 committed
168 169 170
    PurchaseBillProduct *billProduct = [[fication userInfo] objectForKey:@"purchaseBillProduct"];
    NSInteger indexTag = [[[fication userInfo] objectForKey:@"indexTag"] integerValue];
    
陈俊俊's avatar
陈俊俊 committed
171
    ShopDetaileViewController *svc = [[ShopDetaileViewController alloc] init];
陈俊俊's avatar
陈俊俊 committed
172
    if (billProduct) {
陈俊俊's avatar
陈俊俊 committed
173
        svc.navTitle = @"编辑商品";
陈俊俊's avatar
陈俊俊 committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        ShopDetail *shopDetail=[self coverShopDetail:billProduct];
        shopDetail.IsXiuGai=YES;
        svc.choseShopDetail = ^(ShopDetail *Detail){
            if (Detail.IsDeleted) {
                [_pvc.productArr removeObjectAtIndex:indexTag];
            }else{
                PurchaseBillProduct *billProduct = [self coverPurchaseProduct:Detail];
                [_pvc.productArr replaceObjectAtIndex:indexTag withObject:billProduct];
            }
            [_pvc.tableView reloadData];
            [[NSNotificationCenter defaultCenter] postNotificationName:SetProductTotalPrice object:nil];
        };
        svc.shopDetail=shopDetail;
    }else{
        // 回调
陈俊俊's avatar
陈俊俊 committed
189
        svc.navTitle = @"添加商品";
陈俊俊's avatar
陈俊俊 committed
190 191 192 193 194 195 196 197
        svc.choseShopDetail = ^(ShopDetail *shopDetail){
            PurchaseBillProduct *billProduct = [self coverPurchaseProduct:shopDetail];
            [_pvc.productArr addObject:billProduct];
            [_pvc.tableView reloadData];
            [[NSNotificationCenter defaultCenter] postNotificationName:SetProductTotalPrice object:nil];
        };
    }
   
陈俊俊's avatar
陈俊俊 committed
198 199 200
    svc.title = [IBTCommon localizableString:@"AddShopDetail"];
    [self PushViewController:svc animated:YES];
}
陈俊俊's avatar
陈俊俊 committed
201 202


n22's avatar
n22 committed
203
- (void)btnClick:(UIButton *)btn{
n22's avatar
n22 committed
204 205 206 207
    switch (btn.tag) {
        case SaveTag:
        {
            if ([self checkPurchase]) {
陈俊俊's avatar
陈俊俊 committed
208
                [self getDataFromServer:PURCHASE_STATE_INITIAL msg:@"正在保存..."];
n22's avatar
n22 committed
209 210 211 212
            }
        }
            break;
        case CommitTag:{
陈俊俊's avatar
陈俊俊 committed
213 214 215 216
            if ([self checkPurchase]) {
                [self getDataFromServer:PURCHASE_STATE_SUBMITTED msg:@"正在提交..."];
 
            }
n22's avatar
n22 committed
217 218 219 220 221 222 223
        }
            break;
        default:
            break;
    }
}

陈俊俊's avatar
陈俊俊 committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
- (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{//提交成功
                    PurchaseViewController *svc = [PurchaseViewController new];
                    [self PushViewController:svc animated:YES];
                }
            }else{
                [IBTLoadingView showTips:message];
            }
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    
    if(self.purchaseBill.uuid.length > 0 && uuidObject == [NSNull null]){
        uuidObject = self.purchaseBill.uuid;
    }
    if (self.purchaseBill.billNumber > 0 && billNumberObject == [NSNull null]) {
        billNumberObject = self.purchaseBill.billNumber;
    }
    if (self.purchaseBill && [versionObject integerValue] == 0) {
        versionObject = self.purchaseBill.version;
    }
    NSMutableArray *billProducts = [NSMutableArray array];
    for (PurchaseBillProduct *billProduct in _pvc.productArr) {
        [billProducts  addObject:[billProduct dictForCommit]];
    }
陈俊俊's avatar
陈俊俊 committed
268
    self.state = state;
陈俊俊's avatar
陈俊俊 committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    NSDictionary *dict = @{@"uuid":uuidObject,
                           @"version":versionObject,
                           @"billnumber":billNumberObject,
                           @"state":state,
                           @"type":[IBTCommon checkString:_purchaseView.type],
                           @"noticeUuid":[IBTCommon checkString:_purchaseView.noticeUuid],
                           @"noticeNumber":[IBTCommon checkString:_purchaseView.noticeNumber],
                           @"vendor_uuid":[IBTCommon checkString:_purchaseView.vendor_uuid],
                           @"vendor_code":[IBTCommon checkString:_purchaseView.vendor_code],
                           @"vendor_name":[IBTCommon checkString:_purchaseView.vendor_name],
                           @"receiveWrh_uuid":[IBTCommon checkString:_purchaseView.receiveWrh_uuid],
                           @"receiveWrh_code":[IBTCommon checkString:_purchaseView.receiveWrh_code],
                           @"receiveWrh_name":[IBTCommon checkString:_purchaseView.receiveWrh_name],
                           @"total":_purchaseView.total,
                           @"charge":_purchaseView.chargePurchase,
                           @"remark":_purchaseView.remark,
                           @"products":billProducts};
    [IBTLoadingView showProgressLabel:msg];
    [[ICRHTTPController sharedController] savePurchaseWithData:dict success:succ failure:fail];
}
n22's avatar
n22 committed
289 290
#pragma mark - checkNull
- (BOOL)checkPurchase{
陈俊俊's avatar
陈俊俊 committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    _purchaseView.remark = _purchaseView.remarkTextView.text;
    if (_purchaseView.type.length == 0 ) {
        ShowMessage(@"采购类型不能为空");
        return NO;
    }
    if (_purchaseView.vendor_uuid.length == 0 ) {
        ShowMessage(@"供应商不能为空");
        return NO;
    }
    if (_purchaseView.receiveWrh_uuid.length == 0 ) {
        ShowMessage(@"收货仓库不能为空");
        return NO;
    }
    if (_pvc.productArr.count == 0) {
        ShowMessage(@"采购商品不能为空");
        return NO;
    }
    if ([_purchaseView.total floatValue] < 0 || !_purchaseView.total) {
        ShowMessage(@"金额不能为空");
n22's avatar
n22 committed
310 311 312
        return NO;
    }
    return YES;
陈俊俊's avatar
陈俊俊 committed
313
}
n22's avatar
n22 committed
314 315


n22's avatar
n22 committed
316 317
#pragma mark - TopPurchaseViewDelegate

陈俊俊's avatar
陈俊俊 committed
318 319
- (void)pushNextViewController:(id)vc{
    [self PushViewController:vc animated:YES];
n22's avatar
n22 committed
320
}
陈俊俊's avatar
陈俊俊 committed
321 322 323
//两个类之间的转换
- (PurchaseBillProduct *)coverPurchaseProduct:(ShopDetail *)shopDetail{
    PurchaseBillProduct *billProduct = [[PurchaseBillProduct alloc]init];
陈俊俊's avatar
陈俊俊 committed
324
    billProduct.uuid = shopDetail.uuid;
陈俊俊's avatar
陈俊俊 committed
325 326 327 328 329 330 331 332 333 334 335
    billProduct.product_uuid = shopDetail.product_uuid;
    billProduct.product_code = shopDetail.product_code;
    billProduct.product_name = 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.basePrice = [NSNumber numberWithFloat:[shopDetail.foundationUnitPrice floatValue]];
    billProduct.total = [NSNumber numberWithFloat:[shopDetail.totalMoney floatValue]];
    billProduct.remark = shopDetail.remark;
陈俊俊's avatar
陈俊俊 committed
336
    billProduct.qpcStr = shopDetail.packageQpcStr ? shopDetail.packageQpcStr : @"无";
陈俊俊's avatar
陈俊俊 committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    return billProduct;
}
- (ShopDetail *)coverShopDetail:(PurchaseBillProduct *)billProduct{
    ShopDetail *shopDetail = [[ShopDetail alloc]init];
    shopDetail.uuid = billProduct.uuid;
    shopDetail.product_uuid = billProduct.product_uuid;
    shopDetail.product_code = billProduct.product_code;
    shopDetail.merchandise  = billProduct.product_name;
    shopDetail.packageSpecification  =  [billProduct.qpc stringValue];
    shopDetail.packageUnit = billProduct.unit;
    shopDetail.packageQuantity = [billProduct.qty stringValue];
    shopDetail.packageUnitPrice  = [billProduct.price stringValue];
    shopDetail.foundationQuantity= [billProduct.baseQty stringValue];
    shopDetail.foundationUnitPrice= [billProduct.basePrice  stringValue];
    shopDetail.totalMoney= [billProduct.total stringValue];
    shopDetail.remark  = billProduct.remark;
陈俊俊's avatar
陈俊俊 committed
353
    shopDetail.packageQpcStr = billProduct.qpcStr;
陈俊俊's avatar
陈俊俊 committed
354 355 356
    
    return shopDetail;
}
陈俊俊's avatar
陈俊俊 committed
357 358 359 360
- (NSMutableArray *)coverPurchaseBillProduct:(NSArray *)arr{
    NSMutableArray *billArr = [NSMutableArray array];
    for (NoticeProduct *product in arr) {
        PurchaseBillProduct *billProduct = [PurchaseBillProduct new];
陈俊俊's avatar
陈俊俊 committed
361
//        billProduct.uuid = product.uuid;
陈俊俊's avatar
陈俊俊 committed
362 363 364 365 366 367 368 369 370 371 372
        billProduct.product_name = product.productName;
        billProduct.product_uuid  = product.productUuid;
        billProduct.product_code = product.productCode;
        billProduct.qpc = product.qpc;
        billProduct.unit = product.packUnit;
        billProduct.qty = [NSNumber numberWithFloat:0];
        billProduct.price = [NSNumber numberWithFloat:0];
        billProduct.baseQty = [NSNumber numberWithFloat:0];
        billProduct.basePrice = [NSNumber numberWithFloat:0];
        billProduct.total = [NSNumber numberWithFloat:0];
        billProduct.remark = product.remark;
陈俊俊's avatar
陈俊俊 committed
373
        billProduct.qpcStr = @"无";
陈俊俊's avatar
陈俊俊 committed
374 375 376 377
        [billArr addObject:billProduct];
    }
    return billArr;
}
n22's avatar
n22 committed
378

n22's avatar
n22 committed
379 380 381 382 383
#pragma mark - 协议方法
- (void)hiddenKeyBoard{
    [self keyboardHidden];
}
- (void)keyboardHidden{
n22's avatar
n22 committed
384
    [_purchaseView.otherPriceFiled resignFirstResponder];
n22's avatar
n22 committed
385 386
    [_purchaseView.remarkTextView resignFirstResponder];
}
陈俊俊's avatar
陈俊俊 committed
387 388 389 390 391 392 393
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if(self.purchaseBill && [self.state isEqualToString:PURCHASE_STATE_INITIAL]){
        [ICRUserUtil sharedInstance].needFresh = YES;
    }
}

n22's avatar
n22 committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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