-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInvoiceVC.m
More file actions
478 lines (427 loc) · 18.8 KB
/
InvoiceVC.m
File metadata and controls
478 lines (427 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//
// InvoiceVC.m
// DuDu
//
// Created by 教路浩 on 15/12/2.
// Copyright © 2015年 i-chou. All rights reserved.
//
#import "InvoiceVC.h"
#define SIZE 10
@interface InvoiceVC ()
{
ZBCLodingFooter *_loadingFooter;
UITableView *_tableView;
UILabel *_totalLabel;
float _totalPrice;
int _totalRoute;
NSMutableArray *_bookList;
UIButton *_selectAllButton;
UIButton *_nextButton;
BOOL _loadingMore;
BOOL _isAll;
int _currentPage;
BOOL _isSelectAll;
}
@end
@implementation InvoiceVC
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = COLORRGB(0xffffff);
_loadingFooter =
[[ZBCLodingFooter alloc] initWithFrame:ccr(0, 0, SCREEN_WIDTH, 50)];
_loadingFooter.delegate = self;
_bookList = [NSMutableArray array];
[self createTableView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self getBooksForPage:0 isMore:NO];
}
- (void)getBooksForPage:(int)page isMore:(BOOL)isMore
{
if (![UICKeyChainStore stringForKey:KEY_STORE_ACCESS_TOKEN service:KEY_STORE_SERVICE]) {
[ZBCToast showMessage:@"请先登录"];
[self.navigationController popViewControllerAnimated:YES];
return;
}
_loadingMore = isMore;
[_loadingFooter loading];
_loadingFooter.isLoadMore = isMore;
[[DuDuAPIClient sharedClient] GET:BOOK_ORDER_LIST(page) parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
[self fetchDataSuccess:responseObject
isLoadMore:isMore];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[_loadingFooter loadedAll];
_loadingMore = NO;
[_bookList removeAllObjects];
[_tableView reloadData];
}];
}
- (void)updateFooter
{
_totalPrice = 0;
_totalRoute = 0;
for (OrderModel *order in _bookList) {
if (order.isSelected) {
_totalPrice += [order.order_allMoney floatValue];
_totalRoute ++;
}
}
if (_totalRoute == _bookList.count) {
_isSelectAll = YES;
[_selectAllButton setImage:IMG(@"checkbox_yes")
forState:UIControlStateNormal];
[_selectAllButton setImage:IMG(@"checkbox_yes")
forState:UIControlStateSelected];
} else {
_isSelectAll = NO;
[_selectAllButton setImage:IMG(@"checkbox_no")
forState:UIControlStateNormal];
[_selectAllButton setImage:IMG(@"checkbox_no")
forState:UIControlStateSelected];
}
_totalLabel.text = [NSString stringWithFormat:@"合计:%.1f元 共%d个行程",_totalPrice,_totalRoute];
NSMutableAttributedString *totalString = [[NSMutableAttributedString alloc] initWithString:_totalLabel.text];
NSString *priceText = [NSString stringWithFormat:@"%.1f",_totalPrice];
NSUInteger priceLength = priceText.length;
NSString *numberText = [NSString stringWithFormat:@"%d",_totalRoute];
NSUInteger numberLength = numberText.length;
NSUInteger numberLocation = _totalLabel.text.length-3-numberLength;
[totalString addAttributes:@{NSForegroundColorAttributeName:COLORRGB(0xedad49)}
range:NSMakeRange(3, priceLength)];
[totalString addAttributes:@{NSForegroundColorAttributeName:COLORRGB(0xedad49)}
range:NSMakeRange(numberLocation, numberLength)];
_totalLabel.attributedText = totalString;
[_nextButton setEnabled:_totalRoute >= 5];
}
- (void)selectAllBookDidTaped
{
if (!_isSelectAll) {
for (OrderModel *order in _bookList) {
order.isSelected = YES;
}
} else {
for (OrderModel *order in _bookList) {
order.isSelected = NO;
}
}
[_tableView reloadData];
[self updateFooter];
}
- (void)fetchDataSuccess:(id)responseObject isLoadMore:(BOOL)isMore
{
NSDictionary *dic = [DuDuAPIClient parseJSONFrom:responseObject];
if (!isMore) {
//接口返回数据类型不统一,无法通过MTLJSONAdapter映射到Model,导致这么一大顿体力代码
// _bookList = [[MTLJSONAdapter modelsOfClass:[BookModel class]
// fromJSONArray:dic[@"info"]
// error:nil] mutableCopy];
NSArray *info = [NSArray arrayWithArray:dic[@"info"]];
if (info.count) {
for (NSDictionary *item in info) {
OrderModel *order = [[OrderModel alloc] init];
order.car_style = item[@"car_style"];
order.coupon_id = item[@"coupon_id"];
order.dest_lat = item[@"dest_lat"];
order.dest_lng = item[@"dest_lng"];
order.dest_loc_str = item[@"dest_loc_str"];
order.driver_status = item[@"driver_status"];
order.isbook = item[@"isbook"];
order.order_allMoney = item[@"order_allMoney"];
order.order_allTime = item[@"order_allTime"];
order.order_duration_money = item[@"order_duration_money"];
order.order_id = item[@"order_id"];
order.order_initiate_rate = item[@"order_initiate_rate"];
order.order_mileage = item[@"order_mileage"];
order.order_mileage_money = item[@"order_mileage_money"];
order.order_payStatus = item[@"order_payStatus"];
order.order_status = item[@"order_status"];
order.order_time = item[@"order_time"];
order.relevance_id = item[@"relevance_id"];
order.star_loc_str = item[@"star_loc_str"];
order.startTimeStr = item[@"startTimeStr"];
order.startTimeType = item[@"startTimeType"];
order.start_lat = item[@"start_lat"];
order.start_lng = item[@"start_lng"];
order.user_id = item[@"user_id"];
[_bookList addObject:order];
}
}
_isAll = _bookList.count < SIZE;
if (_isAll) {
[_loadingFooter loadedAll];
}
[self updateFooter];
[_tableView reloadData];
_loadingMore = NO;
} else {
// NSArray *booklistTemp = [MTLJSONAdapter modelsOfClass:[BookModel class]
// fromJSONArray:dic[@"info"]
// error:nil];
NSMutableArray *booklistTemp = [NSMutableArray array];
NSArray *info = [NSArray arrayWithArray:dic[@"info"]];
if (info.count) {
for (NSDictionary *item in info) {
OrderModel *order = [[OrderModel alloc] init];
order.car_style = item[@"car_style"];
order.coupon_id = item[@"coupon_id"];
order.dest_lat = item[@"dest_lat"];
order.dest_lng = item[@"dest_lng"];
order.dest_loc_str = item[@"dest_loc_str"];
order.driver_status = item[@"driver_status"];
order.isbook = item[@"isbook"];
order.order_allMoney = item[@"order_allMoney"];
order.order_allTime = item[@"order_allTime"];
order.order_duration_money = item[@"order_duration_money"];
order.order_id = item[@"order_id"];
order.order_initiate_rate = item[@"order_initiate_rate"];
order.order_mileage = item[@"order_mileage"];
order.order_mileage_money = item[@"order_mileage_money"];
order.order_payStatus = item[@"order_payStatus"];
order.order_status = item[@"order_status"];
order.order_time = item[@"order_time"];
order.relevance_id = item[@"relevance_id"];
order.star_loc_str = item[@"star_loc_str"];
order.startTimeStr = item[@"startTimeStr"];
order.startTimeType = item[@"startTimeType"];
order.start_lat = item[@"start_lat"];
order.start_lng = item[@"start_lng"];
order.user_id = item[@"user_id"];
[booklistTemp addObject:order];
}
}
_isAll = booklistTemp.count < SIZE;
if (_isAll) {
[_loadingFooter loadedAll];
}
if (booklistTemp.count > 0) {
[_tableView beginUpdates];
NSMutableArray *indexs = [NSMutableArray array];
[booklistTemp enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[indexs addObject:[NSIndexPath indexPathForRow:_bookList.count + idx
inSection:0]];
}];
[_tableView insertRowsAtIndexPaths:indexs
withRowAnimation:UITableViewRowAnimationAutomatic];
[_bookList addObjectsFromArray:booklistTemp];
[_tableView endUpdates];
}
_loadingMore = NO;
[self updateFooter];
}
}
- (void)createTableView
{
_tableView = [[UITableView alloc] initWithFrame:ccr(0,
NAV_BAR_HEIGHT_IOS7,
SCREEN_WIDTH,
SCREEN_HEIGHT-NAV_BAR_HEIGHT_IOS7-[self loadFooterView].height)];
_tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = COLORRGB(0xffffff);
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
// _tableView.tableHeaderView = [self loadHeaderView];
_tableView.tableFooterView = _loadingFooter;
[self.view addSubview:_tableView];
[self.view addSubview:[self loadFooterView]];
}
- (UIView *)loadHeaderView
{
UIView *headerView = [[UIView alloc] initWithFrame:ccr(0, 0, SCREEN_WIDTH, 50)];
headerView.backgroundColor = COLORRGB(0xf0f0f0);
UILabel *introLabel = [UILabel labelWithFrame:CGRectZero
color:COLORRGB(0x000000)
font:HSFONT(12)
text:@"开票说明 | 开票历史"
alignment:NSTextAlignmentRight
numberOfLines:1];
CGSize introSize = [introLabel.text sizeWithAttributes:@{NSFontAttributeName:introLabel.font}];
[introLabel sizeToFit];
introLabel.frame = ccr(SCREEN_WIDTH-5-introSize.width,
(50-introSize.height)/2,
introSize.width,
introSize.height);
[headerView addSubview:introLabel];
UIImageView *lineImage = [[UIImageView alloc] initWithFrame:ccr(0, 50-0.5, SCREEN_WIDTH, 0.5)];
lineImage.backgroundColor = COLORRGB(0xd7d7d7);
[headerView addSubview:lineImage];
return headerView;
}
- (UIView *)loadFooterView
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];
footerView.backgroundColor = COLORRGB(0xf0f0f0);
_selectAllButton = [UIButton buttonWithImageName:@"checkbox_no"
hlImageName:@"checkbox_no"
onTapBlock:^(UIButton *btn) {
[self selectAllBookDidTaped];
}];
_selectAllButton.frame = ccr(20, 10, 16, 16);
[footerView addSubview:_selectAllButton];
UILabel *selectAllLabel = [UILabel labelWithFrame:CGRectZero
color:COLORRGB(0x000000)
font:HSFONT(12)
text:@"全选"
alignment:NSTextAlignmentLeft
numberOfLines:1];
CGSize allSize = [selectAllLabel.text sizeWithAttributes:@{NSFontAttributeName:selectAllLabel.font}];
[selectAllLabel sizeToFit];
selectAllLabel.frame = ccr(CGRectGetMaxX(_selectAllButton.frame)+10,
_selectAllButton.origin.y,
allSize.width,
allSize.height);
[footerView addSubview:selectAllLabel];
UILabel *introdceLabel = [UILabel labelWithFrame:CGRectZero
color:COLORRGB(0x000000)
font:HSFONT(11)
text:@"需要至少选择5条行程才能开发票"
alignment:NSTextAlignmentRight
numberOfLines:1];
CGSize introSize = [self getTextFromLabel:introdceLabel];
introdceLabel.frame = ccr(SCREEN_WIDTH-10-introSize.width,
selectAllLabel.origin.y,
introSize.width,
introSize.height);
[footerView addSubview:introdceLabel];
_nextButton = [UIButton buttonWithImageName:@"orgbtn"
hlImageName:@"orgbtn_pressed"
DisabledImageName:@"commbtn"
title:@"下一步"
titleColor:COLORRGB(0xffffff)
font:HSFONT(15)
disabledTitleColor:COLORRGB(0xffffff)
onTapBlock:^(UIButton *btn) {
[self didClickNextButtonAction];
}];
_nextButton.frame = ccr(SCREEN_WIDTH-10-120,
CGRectGetMaxY(introdceLabel.frame)+10,
120,
40);
[_nextButton setEnabled:NO];
[footerView addSubview:_nextButton];
_totalLabel = [UILabel labelWithFrame:ccr(_selectAllButton.origin.x,
_nextButton.origin.y+(_nextButton.height-20)/2,
SCREEN_WIDTH-_selectAllButton.origin.x-_nextButton.width-10-20,
20)
color:COLORRGB(0x000000)
font:HSFONT(12)
text:@""
alignment:NSTextAlignmentLeft
numberOfLines:1];
[footerView addSubview:_totalLabel];
CGFloat footHeight = CGRectGetMaxY(_nextButton.frame)+10;
footerView.frame = ccr(0,
SCREEN_HEIGHT-footHeight,
SCREEN_WIDTH,
footHeight
);
return footerView;
}
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _bookList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"invoiceCell";
InvoiceCell *invoiceCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!invoiceCell) {
invoiceCell = [[InvoiceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
invoiceCell.delegate = self;
}
[self configureCell:invoiceCell atIndexPath:indexPath];
CGRect frame = [invoiceCell calculateFrame];
return frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"invoiveCell";
InvoiceCell *invoiceCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!invoiceCell) {
invoiceCell = [[InvoiceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
invoiceCell.selectionStyle = UITableViewCellSelectionStyleNone;
invoiceCell.delegate = self;
}
[self configureCell:invoiceCell atIndexPath:indexPath];
return invoiceCell;
}
- (void)configureCell:(InvoiceCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
OrderModel *order = _bookList[indexPath.row];
NSDate *start = [NSDate dateWithTimeIntervalSince1970:[order.startTimeStr floatValue]];
cell.startTime = [start displayWithFormat:@"yyyy-MM-dd H:mm"];
cell.price = order.order_allMoney;
cell.startSite = order.star_loc_str;
cell.endSite = order.dest_loc_str;
cell.isSelected = order.isSelected;
}
#pragma mark - tableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InvoiceCell *invoiceCell = (InvoiceCell *)[tableView cellForRowAtIndexPath:indexPath];
OrderModel *order = _bookList[indexPath.row];
order.isSelected = !order.isSelected;
invoiceCell.isSelected = order.isSelected;
[_bookList setObject:order atIndexedSubscript:indexPath.row];
[invoiceCell calculateFrame];
[self updateFooter];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_bookList.count - indexPath.row < 2 && !_loadingMore) {
if(!_isAll)
{
[self getBooksForPage:_currentPage+1 isMore:YES];
} else {
[_loadingFooter loadedAll];
}
}
}
- (CGSize)getTextFromLabel:(UILabel *)label
{
CGSize textSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
[label sizeToFit];
return textSize;
}
- (void)didClickNextButtonAction
{
InvoiceDetailVC *detailVC = [[InvoiceDetailVC alloc] init];
detailVC.title = @"按行程开票";
detailVC.delegate = self;
BookModel *book = [[BookModel alloc] init];
book.book_price = [NSString stringWithFormat:@"%.1f元",_totalPrice];
NSString *ids = @"";
for (OrderModel *order in _bookList) {
if(order.isSelected){
ids = [ids stringByAppendingString:[order.order_id stringValue]];
ids = [ids stringByAppendingString:@","];
}
}
book.order_ids = ids;
detailVC.book = book;
[self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark - InvoiceCellDelegate
- (void)invoiceCell:(InvoiceCell *)cell didChecked:(BOOL)checked
{
NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
OrderModel *order = _bookList[indexPath.row];
order.isSelected = checked;
[_bookList setObject:order atIndexedSubscript:indexPath.row];
[cell calculateFrame];
[self updateFooter];
}
#pragma mark - InvoiceDetailVCDelegate
- (void)dealBookSuccess
{
[self getBooksForPage:0 isMore:NO];
}
@end