SelectUploadOrderViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // SelectUploadOrderViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 9/3/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SelectUploadOrderViewController.h"
  9. #import "iSalesDB.h"
  10. #import <sqlite3.h>
  11. #import "SelectOrderTableViewCell.h"
  12. #import "DefaultTableHeaderView.h"
  13. #import "DefaultAppearance.h"
  14. #import "iSalesNetwork.h"
  15. #import "RAUtils.h"
  16. @interface SelectUploadOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
  17. @property (nonatomic,strong) NSMutableArray<NSDictionary *> *sync_data;
  18. @property (strong, nonatomic) IBOutlet UITableView *syncDataTableView;
  19. @property (strong, nonatomic) IBOutlet UIButton *uploadButton;
  20. @property (strong, nonatomic) NSIndexPath *mergeTo;
  21. @end
  22. @implementation SelectUploadOrderViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  27. style:UIBarButtonItemStylePlain
  28. target:self
  29. action:@selector( onCloseClick:)];
  30. closeButton.width = -10;
  31. UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"clear all" style:UIBarButtonItemStylePlain target:self action:@selector(onClearClick:)];
  32. self.navigationItem.leftBarButtonItem = closeButton;
  33. self.navigationItem.rightBarButtonItem = clearButton;
  34. }
  35. - (void)didReceiveMemoryWarning {
  36. [super didReceiveMemoryWarning];
  37. // Dispose of any resources that can be recreated.
  38. }
  39. - (NSMutableArray<NSDictionary *> *)sync_data {
  40. if (!_sync_data) {
  41. _sync_data = [NSMutableArray array];
  42. NSString *sql = @"select decrypt(c.company_name),o.so_id,o.sync_data from (select so_id,sync_data,customer_cid from offline_order where sync_data not null) as o join offline_contact as c on c.contact_id = o.customer_cid ;";
  43. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  44. const char *cn = (char *)sqlite3_column_text(stmt,0);
  45. if (cn == NULL) {
  46. cn = "";
  47. }
  48. const char *_id = (char *)sqlite3_column_text(stmt,1);
  49. if (_id == NULL) {
  50. _id = "";
  51. }
  52. const char *data = (char *)sqlite3_column_text(stmt,2);
  53. if (data == NULL) {
  54. data = "";
  55. }
  56. NSString *company_name = [NSString stringWithUTF8String:cn];
  57. NSString *so_id = [NSString stringWithUTF8String:_id];
  58. NSString *sync_data_str = [NSString stringWithUTF8String:data];
  59. NSDictionary *dic = [@{
  60. @"check" : @(1),
  61. @"company_name" : company_name,
  62. @"so_id" : so_id,
  63. @"sync_data" : sync_data_str
  64. } mutableCopy];
  65. [_sync_data addObject:dic];
  66. }];
  67. }
  68. return _sync_data;
  69. }
  70. - (IBAction)uploadButtonClicked:(UIButton *)sender {
  71. // NSMutableArray *uploadArray = [NSMutableArray array];
  72. //
  73. // for (NSDictionary *dic in self.sync_data) {
  74. //
  75. // int check = [dic[@"check"] integerValue];
  76. // NSString *so_id = dic[@"so_id"];
  77. //
  78. // if (check == 1) {
  79. // [uploadArray addObject:so_id];
  80. // }
  81. //
  82. // }
  83. //
  84. //
  85. //
  86. //
  87. // [self dismissViewControllerAnimated:true completion:^{
  88. // if (self.returnValue ) {
  89. // self.returnValue(uploadArray);
  90. // } ;
  91. // }];
  92. // merge order
  93. if (self.mergeList.count < 2) {
  94. [RAUtils message_alert:@"Please Choose More Order" title:@"Warning" controller:self];
  95. return;
  96. }
  97. if (!self.mergeTo) {
  98. [RAUtils message_alert:@"Please choose an order to copy information from." title:@"Warning" controller:self];
  99. return;
  100. }
  101. __block NSMutableString *order_ids = [NSMutableString string];
  102. __block NSString *checked_id = nil;
  103. [self.mergeList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  104. NSDictionary *dic = (NSDictionary *)obj;
  105. // order_id
  106. NSString *order_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_id"]];
  107. int check = [[dic objectForKey:@"check"] integerValue];
  108. [order_ids appendString:[NSString stringWithFormat:@"%@,",order_id]];
  109. if (check) {
  110. checked_id = order_id;
  111. }
  112. }];
  113. [order_ids deleteCharactersInRange:NSMakeRange(order_ids.length - 1, 1)];
  114. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  115. [params setObject:order_ids forKey:@"order_ids"];
  116. [params setObject:checked_id forKey:@"targetOrderId"];
  117. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Merge Order"];
  118. NSDictionary *ret = [iSalesNetwork merge_order:params];
  119. [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  120. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  121. if (self.mergeBlock) {
  122. self.mergeBlock(ret);
  123. }
  124. }];
  125. }
  126. #pragma mark - dataSource
  127. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  128. return 1;
  129. }
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. return self.mergeList.count;
  132. }
  133. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath];
  135. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  136. int check = [dic[@"check"] integerValue];
  137. NSString *so_id = dic[@"so_id"];
  138. NSString *create_by = dic[@"create_by"];
  139. NSString *create_time = [dic[@"create_time"] stringByAppendingString:@" PST"];
  140. if (check) {
  141. cell.checkedButton.selected = YES;
  142. self.mergeTo = indexPath;
  143. } else {
  144. cell.checkedButton.selected = NO;
  145. }
  146. cell.labelsoid.text = so_id;
  147. cell.labelcompany.text = create_by;
  148. cell.labelCreateTime.text = create_time;
  149. __weak typeof(self) weakSelf = self;
  150. __weak typeof(tableView) weakTable = tableView;
  151. // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  152. cell.checkBlock = ^{
  153. [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath];
  154. };
  155. return cell;
  156. }
  157. #pragma mark - delegate
  158. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  159. {
  160. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  161. if(value==nil)
  162. value=@"";
  163. unsigned long color = strtoul([value UTF8String],0,16);
  164. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  165. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  166. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  167. myView.layer.shadowOffset = CGSizeMake(0, 0);
  168. myView.layer.shadowOpacity = 0.5;
  169. myView.layer.shadowRadius = 2.0;
  170. UILabel *mergeTo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 44)];
  171. mergeTo.textAlignment = NSTextAlignmentCenter;
  172. mergeTo.textColor=UIColorFromRGB(color);
  173. mergeTo.backgroundColor = [UIColor clearColor];
  174. mergeTo.text=NSLocalizedString(@"To", nil);
  175. [mergeTo sizeToFit];
  176. [myView addSubview:mergeTo];
  177. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 2, 100, 22)];
  178. solabel.textColor=UIColorFromRGB(color);
  179. solabel.backgroundColor = [UIColor clearColor];
  180. solabel.text=NSLocalizedString(@"SO#", nil);
  181. [solabel sizeToFit];
  182. [myView addSubview:solabel];
  183. UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 21, 100, 22)];
  184. userlabel.textColor=UIColorFromRGB(color);
  185. userlabel.backgroundColor = [UIColor clearColor];
  186. userlabel.text=NSLocalizedString(@"Create By", nil);
  187. [userlabel sizeToFit];
  188. [myView addSubview:userlabel];
  189. UILabel *createTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(tableView.bounds.size.width - 150, 21, 100, 22)];
  190. createTimeLabel.textColor=UIColorFromRGB(color);
  191. createTimeLabel.backgroundColor = [UIColor clearColor];
  192. createTimeLabel.text=NSLocalizedString(@"Create Time", nil);
  193. [createTimeLabel sizeToFit];
  194. [myView addSubview:createTimeLabel];
  195. myView.autoresizesSubviews=true;
  196. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  197. //
  198. return myView;
  199. }
  200. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  201. {
  202. return 44;
  203. }
  204. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  205. return 86;
  206. }
  207. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  208. SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  209. if (self.mergeTo && self.mergeTo.row != indexPath.row) {
  210. [self tableView:tableView didSelectRowAtIndexPath:self.mergeTo];
  211. }
  212. cell.selected = NO;
  213. cell.checkedButton.selected = !cell.checkedButton.selected;
  214. NSMutableDictionary *dic = (NSMutableDictionary *)[self.mergeList objectAtIndex:indexPath.row];
  215. int check = [dic[@"check"] integerValue];
  216. if (cell.checkedButton.selected) {
  217. check = 1;
  218. self.mergeTo = indexPath;
  219. } else {
  220. self.mergeTo = nil;
  221. check = 0;
  222. }
  223. [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"];
  224. }
  225. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  226. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  227. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  228. [self.mergeList removeObject:dic];
  229. self.mergeTo = nil;
  230. [tableView reloadData];
  231. }];
  232. return @[deleteAction];
  233. }
  234. //- (BOOL
  235. - (void)onCloseClick:(id)sender {
  236. [self dismissViewControllerAnimated:true completion:nil];
  237. }
  238. - (void)onClearClick:(id)sender {
  239. [self.mergeList removeAllObjects];
  240. [self.syncDataTableView reloadData];
  241. }
  242. @end