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

#import "NewReceiveProductViewController.h"
#import "SurveyCell.h"
#import "HPGrowingTextView.h"
#import "ChooseProductViewController.h"
#import "ChooseProductUnitViewController.h"
陈俊俊's avatar
陈俊俊 committed
14
#import "GeometryView.h"
陈俊俊's avatar
陈俊俊 committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#define LeftMargin 15
#define BtnHeight 44
#define TableRowHeight 46
#define CornerRadius 5
#define BtnSize 19
#define TotalHeight 230
#define KeyboardHeight 258

@interface NewReceiveProductViewController ()<UITableViewDataSource,UITableViewDelegate,HPGrowingTextViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    
    UILabel *_chooseProductLabel;
    UILabel *_choosePackUnitLabel;
    UILabel *_choosePackStandLabel;
    UILabel *_choosePackCountLabel;
    UILabel *_chooseBaseCountLabel;
    UILabel *_choosePackPriceLabel;
    UILabel *_chooseBasePriceLabel;
陈俊俊's avatar
陈俊俊 committed
35 36
    UILabel *_chooseTotalLabel;
    
陈俊俊's avatar
陈俊俊 committed
37 38 39
    
    
    UILabel *_chooseTotalPriceLabel;
陈俊俊's avatar
陈俊俊 committed
40 41 42 43 44 45
    UITextField *_shPackField;
    UITextField *_shBaseQuantityField;
    UILabel *_shjcLabel;
    UILabel *_shbzLabel;
    
    HPGrowingTextView *_noteTextView;
陈俊俊's avatar
陈俊俊 committed
46
    GeometryView *_geometryView;
陈俊俊's avatar
陈俊俊 committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}
@property (nonatomic,strong)NSString *productNameStr;
@property (nonatomic,strong)NSString *productCodeStr;
@property (nonatomic,strong)NSString *productUuidStr;
@property (nonatomic,strong)NSString *productQpcStr;//商品规格描述
@property (nonatomic,strong)NSMutableArray *selectProducts;
@property (nonatomic,strong)NSString *selectUnit;

@end

@implementation NewReceiveProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navTitle;
    [self initData];
    [self createTableView];
    
}
- (void)initData{
    
    _dataArr = [NSMutableArray array];
    self.selectProducts = [NSMutableArray array];
    [_dataArr addObject:@"商品"];
    [_dataArr addObject:@"包装单位"];
    [_dataArr addObject:@"包装规格"];
    [_dataArr addObject:@"包装单价"];
    [_dataArr addObject:@"基础单价"];
陈俊俊's avatar
陈俊俊 committed
75 76 77
    [_dataArr addObject:@"包装数量[实收]"];
    [_dataArr addObject:@"基础数量[实收]"];
    [_dataArr addObject:@"总金额[实收]"];
陈俊俊's avatar
陈俊俊 committed
78 79 80 81 82 83 84 85 86 87 88
    [_dataArr addObject:@"备注"];
}

- (void)createTableView
{
    self.view.backgroundColor  = XXFBgColor;

    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, LeftMargin,ScreenSize.width, ScreenSize.height - 64 - LeftMargin)) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
陈俊俊's avatar
陈俊俊 committed
89 90 91 92 93 94
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, 60+TableRowHeight* 3)];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:(CGRectMake(LeftMargin, 0, ScreenSize.width - LeftMargin * 2, 1))];
    lineLabel.backgroundColor = GXF_LINE_COLOR;
    lineLabel.font = GXF_FIFTEENTEN_SIZE;
    [footView addSubview:lineLabel];
陈俊俊's avatar
陈俊俊 committed
95
    
陈俊俊's avatar
陈俊俊 committed
96 97 98 99 100
    _geometryView = [GeometryView new];
    _geometryView.frame = CGRectMake(LeftMargin/2, 5, ScreenSize.width - LeftMargin, TableRowHeight* 3);
    [footView addSubview:_geometryView];
    [self createBottomField];
    UIButton *commitBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, _geometryView.bottom + 5, (ScreenSize.width - LeftMargin*2), BtnHeight) target:self sel:@selector(btnClick:) tag:0 image:nil title:@"保存" titleColor: [UIColor whiteColor] isCorner:YES corner:CornerRadius bgColor:GXF_COMMIT_COLOR];
陈俊俊's avatar
陈俊俊 committed
101 102 103 104 105 106
    [footView addSubview:commitBtn];
    
    _tableView.tableFooterView = footView;
    [self.view addSubview:_tableView];
}

陈俊俊's avatar
陈俊俊 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
- (void)createBottomField{
    NSArray *arr = @[@"包装数量[待收]:",@"基础数量[待收]:",@"总金额[待收]:"];
    for (NSInteger i =0; i < arr.count; i++) {
        UILabel *label =[[UILabel alloc]initWithFrame:(CGRectMake(LeftMargin/2, i*TableRowHeight, 110, TableRowHeight))];
        label.textAlignment= NSTextAlignmentLeft;
        label.textColor = GXF_LEFTSIX_COLOR;
        label.font = GXF_FIFTEENTEN_SIZE;
        label.text = arr[i];
        [_geometryView addSubview:label];
        
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.right, i*TableRowHeight,  _geometryView.width - label.width -LeftMargin, TableRowHeight)];
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = GXF_CONTENT_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
        [_geometryView addSubview:contentLabel];
        if (i == 0) {
            _choosePackCountLabel = contentLabel;
        }else if(i == 1){
            _chooseBaseCountLabel = contentLabel;
        }else if(i == 2){
            _chooseTotalLabel = contentLabel;
        }
    }
}


陈俊俊's avatar
陈俊俊 committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#pragma mark - 按钮点击事件
- (void)btnClick:(UIButton *)btn{
    if ([self checkCost]) {
        self.editReceiveProduct(self.noticeProduct,self.indexTag);
        [self PopViewControllerAnimated:YES];
    }
}
- (BOOL)checkCost{
    
    if (_shPackField.text.length == 0) {
        ShowMessage(@"包装数量(收货)不能为空");
        return NO;
    }
    if (_shBaseQuantityField.text.length == 0) {
        ShowMessage(@"基础数量(收货)不能为空");
        return NO;
    }
Sandy's avatar
Sandy committed
151 152 153 154 155
    
    if (_shPackField.text.floatValue > self.noticeProduct.qty.floatValue) {
        ShowMessage(@"包装数量[实收]不能大于包装数量[待收]");
        return NO;
    }
陈俊俊's avatar
陈俊俊 committed
156 157 158 159 160
    if (!self.noticeProduct) {
        self.noticeProduct = [[TransferPdtDetail alloc]init];
    }
    
    self.noticeProduct.rctQty =[NSNumber numberWithFloat:[_shPackField.text floatValue]];
陈俊俊's avatar
陈俊俊 committed
161
    self.noticeProduct.rctBaseQty = [NSNumber numberWithFloat:[_shBaseQuantityField.text floatValue]];
陈俊俊's avatar
陈俊俊 committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    self.noticeProduct.note = _noteTextView.text;
    return YES;
}

#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    SurveyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[SurveyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
陈俊俊's avatar
陈俊俊 committed
180
        cell.titleLabel.width = 110;
陈俊俊's avatar
陈俊俊 committed
181 182 183 184 185 186 187 188
        [self createViewInCell:cell indexPath:indexPath];
    }
    [cell setTitleStr:_dataArr[indexPath.row]];
    return cell;
}

-  (void)createViewInCell:(SurveyCell *)cell indexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _dataArr.count -1){
陈俊俊's avatar
陈俊俊 committed
189
        _noteTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(cell.titleLabel.right, 0, ScreenSize.width - cell.titleLabel.width - LeftMargin*2-15, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203
        _noteTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
        _noteTextView.minNumberOfLines = 1;
        _noteTextView.maxNumberOfLines = 2;
        _noteTextView.isScrollable = YES;
        _noteTextView.font = GXF_FIFTEENTEN_SIZE;
        _noteTextView.textAlignment = NSTextAlignmentRight;
        _noteTextView.delegate = self;
        _noteTextView.returnKeyType = UIReturnKeyDone;
        _noteTextView.placeholder = @"输入备注内容";
        [cell.contentView addSubview:_noteTextView];
        
        CGRect lineFrame =  cell.lineLabel.frame;
        lineFrame.origin.y = TableRowHeight*2-1;
        cell.lineLabel.frame = lineFrame;
陈俊俊's avatar
陈俊俊 committed
204
    }else if(indexPath.row == 5 || indexPath.row == 6){
陈俊俊's avatar
陈俊俊 committed
205
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(cell.titleLabel.right, 0, ScreenSize.width - cell.titleLabel.width - LeftMargin*2-30, TableRowHeight)];
陈俊俊's avatar
陈俊俊 committed
206 207 208 209 210 211
        textField.textAlignment = NSTextAlignmentRight;
        textField.textColor = GXF_CONTENT_COLOR;
        textField.font = GXF_FIFTEENTEN_SIZE;
        textField.returnKeyType = UIReturnKeyDone;
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.delegate = self;
陈俊俊's avatar
陈俊俊 committed
212
        [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
陈俊俊's avatar
陈俊俊 committed
213 214 215 216 217 218
        [cell.contentView addSubview:textField];

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(textField.frame)+ 5, 0, 20, TableRowHeight)];
        label.textColor = GXF_CONTENT_COLOR;
        label.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:label];
陈俊俊's avatar
陈俊俊 committed
219
        if (indexPath.row == 5) {
陈俊俊's avatar
陈俊俊 committed
220
            _shbzLabel = label;
陈俊俊's avatar
陈俊俊 committed
221
            textField.placeholder = @"输入包装数量(实收)";
陈俊俊's avatar
陈俊俊 committed
222 223 224
            _shPackField = textField;
        }else if(indexPath.row == 6){
            _shjcLabel = label;
陈俊俊's avatar
陈俊俊 committed
225
            textField.placeholder = @"输入基础数量(实收)";
陈俊俊's avatar
陈俊俊 committed
226 227 228
            _shBaseQuantityField = textField;
        }
    }else{
陈俊俊's avatar
陈俊俊 committed
229
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(cell.titleLabel.right, 0, ScreenSize.width - cell.titleLabel.width - LeftMargin*2-15, TableRowHeight))];
陈俊俊's avatar
陈俊俊 committed
230 231 232 233 234 235 236 237 238 239 240 241 242
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = GXF_CONTENT_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:contentLabel];
        
        if (indexPath.row == 0) {
            _chooseProductLabel = contentLabel;
        }else if(indexPath.row == 1) {
            _choosePackUnitLabel = contentLabel;
        }else if(indexPath.row == 2) {
            _choosePackStandLabel = contentLabel;
        }else if(indexPath.row == 3) {
            _choosePackPriceLabel = contentLabel;
陈俊俊's avatar
陈俊俊 committed
243
        }else if(indexPath.row == 4) {
陈俊俊's avatar
陈俊俊 committed
244
            _chooseBasePriceLabel = contentLabel;
陈俊俊's avatar
陈俊俊 committed
245 246
        }else if(indexPath.row == 7) {
             _chooseTotalPriceLabel= contentLabel;
陈俊俊's avatar
陈俊俊 committed
247
            _chooseTotalPriceLabel.textColor = GXF_NAVIGAYION_COLOR;
陈俊俊's avatar
陈俊俊 committed
248 249 250 251
        }else if(indexPath.row == 8) {
            _choosePackCountLabel = contentLabel;
        }else if(indexPath.row == 9) {
            _chooseBaseCountLabel= contentLabel;
陈俊俊's avatar
陈俊俊 committed
252 253 254 255 256 257 258
        }
    }
    if (self.noticeProduct) {
        [self prepareDataIncell];
    }
}
- (void)prepareDataIncell{
陈俊俊's avatar
陈俊俊 committed
259
    //商品
陈俊俊's avatar
陈俊俊 committed
260
    _chooseProductLabel.text = [NSString stringWithFormat:@"%@[%@]",self.noticeProduct.productName,self.noticeProduct.productCode];
陈俊俊's avatar
陈俊俊 committed
261
    //包装单位
陈俊俊's avatar
陈俊俊 committed
262
    _choosePackUnitLabel.text = self.noticeProduct.unit;
陈俊俊's avatar
陈俊俊 committed
263
    //包装规格
陈俊俊's avatar
陈俊俊 committed
264
    _choosePackStandLabel.text = [NSString stringWithFormat:@"1*%@ %@",[self.noticeProduct.qpc stringValue],self.noticeProduct.baseUnit];
陈俊俊's avatar
陈俊俊 committed
265
    //待收包装数量
陈俊俊's avatar
陈俊俊 committed
266
    _choosePackCountLabel.text = [NSString stringWithFormat:@"%@ %@",[self.noticeProduct.qty stringValue],self.noticeProduct.unit];
陈俊俊's avatar
陈俊俊 committed
267
    //待收基础数量
陈俊俊's avatar
陈俊俊 committed
268
    _chooseBaseCountLabel.text = [NSString stringWithFormat:@"%@ %@",[self.noticeProduct.baseQty stringValue],self.noticeProduct.baseUnit];
陈俊俊's avatar
陈俊俊 committed
269 270 271 272 273 274 275
    //待收总金额
    _chooseTotalLabel.text = [NSString stringWithFormat:@"%.2f元",[self.noticeProduct.total floatValue]];
    
    //包装单价
    _choosePackPriceLabel.text = [NSString stringWithFormat:@"%.2f 元",[self.noticeProduct.packprice floatValue]];
    //基础单价
     _chooseBasePriceLabel.text = [NSString stringWithFormat:@"%.2f 元",[self.noticeProduct.price floatValue]];
陈俊俊's avatar
陈俊俊 committed
276 277 278
    
    _shbzLabel.text = self.noticeProduct.unit;
    _shjcLabel.text = self.noticeProduct.baseUnit;
陈俊俊's avatar
陈俊俊 committed
279
    //实收基础数量
陈俊俊's avatar
陈俊俊 committed
280
    if (self.noticeProduct.rctBaseQty) {
陈俊俊's avatar
陈俊俊 committed
281
        _shBaseQuantityField.text =[NSString stringWithFormat:@"%@",[self.noticeProduct.rctBaseQty stringValue]];
陈俊俊's avatar
陈俊俊 committed
282
    }
陈俊俊's avatar
陈俊俊 committed
283
    //实收包装数量
陈俊俊's avatar
陈俊俊 committed
284
    if (self.noticeProduct.rctQty) {
陈俊俊's avatar
陈俊俊 committed
285
        _shPackField.text =[NSString stringWithFormat:@"%@",[self.noticeProduct.rctQty stringValue]];
陈俊俊's avatar
陈俊俊 committed
286
    }
陈俊俊's avatar
陈俊俊 committed
287
    //实收总金额
陈俊俊's avatar
陈俊俊 committed
288
    if (self.noticeProduct.rctTotal) {
陈俊俊's avatar
陈俊俊 committed
289
        _chooseTotalPriceLabel.text = [NSString stringWithFormat:@"%.2f 元",[self.noticeProduct.rctTotal floatValue]];
陈俊俊's avatar
陈俊俊 committed
290 291

    }else {
陈俊俊's avatar
陈俊俊 committed
292
        _chooseTotalPriceLabel.text = @"0.00元";
陈俊俊's avatar
陈俊俊 committed
293 294 295 296 297 298 299 300
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TableRowHeight;
}


- (void)textChange:(UITextField *)textField{
陈俊俊's avatar
陈俊俊 committed
301 302
    if (textField == _shPackField) {
        if (_shPackField.text.length > 0) {
陈俊俊's avatar
陈俊俊 committed
303 304 305 306 307 308 309 310 311
            //基础数量【实收】= 包装数量【实收】* 规格
            float baseCount = [_shPackField.text floatValue] * [self.noticeProduct.qpc floatValue];
             _shBaseQuantityField.text = [NSString stringWithFormat:@"%.2f",baseCount];
            //总金额【实收】= 基础数量【实收】* 基础单价
            float total = 0;
            if ([_shPackField.text floatValue] == [self.noticeProduct.qty floatValue]) {
                total = [self.noticeProduct.total floatValue];
            }else{
               total = baseCount * [self.noticeProduct.price floatValue];
陈俊俊's avatar
陈俊俊 committed
312
            }
陈俊俊's avatar
陈俊俊 committed
313 314 315
            _chooseTotalPriceLabel.text = [NSString stringWithFormat:@"%.2f元",total];
           
             self.noticeProduct.rctTotal = [NSNumber numberWithFloat:total];
陈俊俊's avatar
陈俊俊 committed
316 317 318
        }
    }else if (textField == _shBaseQuantityField) {
        if (_shBaseQuantityField.text.length > 0) {
陈俊俊's avatar
陈俊俊 committed
319 320
            
//            包装数量【实收】= 基础数量【实收】/ 规格
陈俊俊's avatar
陈俊俊 committed
321
            float baseCount = 0;
陈俊俊's avatar
陈俊俊 committed
322 323
            if ([self.noticeProduct.qpc floatValue] > 0) {
                baseCount = [_shBaseQuantityField.text floatValue]/ [self.noticeProduct.qpc floatValue];
陈俊俊's avatar
陈俊俊 committed
324
            }
陈俊俊's avatar
陈俊俊 committed
325
            _shPackField.text = [NSString stringWithFormat:@"%.2f",baseCount];
陈俊俊's avatar
陈俊俊 committed
326 327 328 329 330 331 332 333
            float total = 0;
            if ([_shPackField.text floatValue] == [self.noticeProduct.qty floatValue]) {
                total = [self.noticeProduct.total floatValue];
            }else{
                total = baseCount * [self.noticeProduct.price floatValue];
            }
            _chooseTotalPriceLabel.text = [NSString stringWithFormat:@"%.2f元",total];
            self.noticeProduct.rctTotal = [NSNumber numberWithFloat:total];
陈俊俊's avatar
陈俊俊 committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
        }
    }
}


#pragma mark delegate
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [_noteTextView resignFirstResponder];
    return YES;
}

- (void)keyboardHidden{
    [_shPackField resignFirstResponder];
    [_shBaseQuantityField resignFirstResponder];
    [_noteTextView resignFirstResponder];
}


@end