SelectUploadOrderViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. #import "AppDelegate.h"
  17. @interface SelectUploadOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
  18. @property (nonatomic,strong) NSMutableArray<NSDictionary *> *sync_data;
  19. @property (strong, nonatomic) IBOutlet UITableView *syncDataTableView;
  20. @property (strong, nonatomic) IBOutlet UIButton *uploadButton;
  21. @property (strong, nonatomic) NSIndexPath *mergeTo;
  22. @end
  23. @implementation SelectUploadOrderViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  28. style:UIBarButtonItemStylePlain
  29. target:self
  30. action:@selector( onCloseClick:)];
  31. closeButton.width = -10;
  32. UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"clear all" style:UIBarButtonItemStylePlain target:self action:@selector(onClearClick:)];
  33. self.navigationItem.leftBarButtonItem = closeButton;
  34. self.navigationItem.rightBarButtonItem = clearButton;
  35. }
  36. - (void)didReceiveMemoryWarning {
  37. [super didReceiveMemoryWarning];
  38. // Dispose of any resources that can be recreated.
  39. }
  40. - (NSMutableArray<NSDictionary *> *)sync_data {
  41. if (!_sync_data) {
  42. _sync_data = [NSMutableArray array];
  43. 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 ;";
  44. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  45. const char *cn = (char *)sqlite3_column_text(stmt,0);
  46. if (cn == NULL) {
  47. cn = "";
  48. }
  49. const char *_id = (char *)sqlite3_column_text(stmt,1);
  50. if (_id == NULL) {
  51. _id = "";
  52. }
  53. const char *data = (char *)sqlite3_column_text(stmt,2);
  54. if (data == NULL) {
  55. data = "";
  56. }
  57. NSString *company_name = [NSString stringWithUTF8String:cn];
  58. NSString *so_id = [NSString stringWithUTF8String:_id];
  59. NSString *sync_data_str = [NSString stringWithUTF8String:data];
  60. NSDictionary *dic = [@{
  61. @"check" : @(1),
  62. @"company_name" : company_name,
  63. @"so_id" : so_id,
  64. @"sync_data" : sync_data_str
  65. } mutableCopy];
  66. [_sync_data addObject:dic];
  67. }];
  68. }
  69. return _sync_data;
  70. }
  71. - (IBAction)uploadButtonClicked:(UIButton *)sender {
  72. // NSMutableArray *uploadArray = [NSMutableArray array];
  73. //
  74. // for (NSDictionary *dic in self.sync_data) {
  75. //
  76. // int check = [dic[@"check"] integerValue];
  77. // NSString *so_id = dic[@"so_id"];
  78. //
  79. // if (check == 1) {
  80. // [uploadArray addObject:so_id];
  81. // }
  82. //
  83. // }
  84. //
  85. //
  86. //
  87. //
  88. // [self dismissViewControllerAnimated:true completion:^{
  89. // if (self.returnValue ) {
  90. // self.returnValue(uploadArray);
  91. // } ;
  92. // }];
  93. // merge order,一个订单也可以合并(合并订单也就是将shop order转为sales order)
  94. if (self.mergeList.count < 1) {
  95. [RAUtils message_alert:@"Please add some order" title:@"Warning" controller:self];
  96. return;
  97. }
  98. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  99. __weak typeof(self) weakself = self;
  100. __block NSMutableString *order_ids = [NSMutableString string];
  101. __block NSString *checked_id = nil;
  102. __block BOOL closeOrder = YES;
  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. // order_code
  108. NSString *order_code = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_code"]];
  109. [order_ids appendString:[NSString stringWithFormat:@"%@,",order_id]];
  110. if (idx == 0) {
  111. checked_id = order_id;
  112. }
  113. // 检查是否被自己打开
  114. if ([order_code isEqualToString:appDelegate.order_code]) {
  115. closeOrder = NO;
  116. *stop = YES;
  117. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"can not add to combine list because current order is opened. Do you want to close order and add to combine list?" preferredStyle:UIAlertControllerStyleAlert];
  118. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  119. closeOrder = NO;
  120. }];
  121. UIAlertAction *closeOrderAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  122. [alertVC dismissViewControllerAnimated:YES completion:nil];
  123. // 关闭订单
  124. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Release Order"];
  125. NSDictionary* order_json = [iSalesNetwork release_Order:order_code];
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  128. if([[order_json valueForKey:@"result"] intValue]==2)
  129. {
  130. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  131. [appDelegate closeOrder];
  132. closeOrder = YES;
  133. [weakself uploadButtonClicked:weakself.uploadButton];
  134. } else {
  135. // 释放失败
  136. closeOrder = NO;
  137. [RAUtils message_alert:@"Release Order Failed!" title:@"Warning" controller:weakself];
  138. }
  139. });
  140. }];
  141. [alertVC addAction:cancelAction];
  142. [alertVC addAction:closeOrderAction];
  143. [weakself presentViewController:alertVC animated:YES completion:nil];
  144. }
  145. }];
  146. if (closeOrder) {
  147. [order_ids deleteCharactersInRange:NSMakeRange(order_ids.length - 1, 1)];
  148. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  149. [params setObject:order_ids forKey:@"order_ids"];
  150. [params setObject:checked_id forKey:@"targetOrderId"];
  151. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Merge Order"];
  152. NSDictionary *ret = [iSalesNetwork merge_order:params];
  153. [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  154. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  155. if (weakself.mergeBlock) {
  156. weakself.mergeBlock(ret);
  157. }
  158. }];
  159. }
  160. }
  161. #pragma mark - dataSource
  162. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  163. return 1;
  164. }
  165. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  166. return self.mergeList.count;
  167. }
  168. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  169. SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath];
  170. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  171. int check = [dic[@"check"] integerValue];
  172. NSString *so_id = dic[@"so_id"];
  173. NSString *create_by = dic[@"create_by"];
  174. NSString *create_time = [dic[@"create_time"] stringByAppendingString:@" PST"];
  175. if (check) {
  176. cell.checkedButton.selected = YES;
  177. self.mergeTo = indexPath;
  178. } else {
  179. cell.checkedButton.selected = NO;
  180. }
  181. cell.labelsoid.text = so_id;
  182. cell.labelcompany.text = create_by;
  183. cell.labelCreateTime.text = create_time;
  184. __weak typeof(self) weakSelf = self;
  185. __weak typeof(tableView) weakTable = tableView;
  186. // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  187. cell.checkBlock = ^{
  188. [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath];
  189. };
  190. return cell;
  191. }
  192. #pragma mark - delegate
  193. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  194. {
  195. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  196. if(value==nil)
  197. value=@"";
  198. unsigned long color = strtoul([value UTF8String],0,16);
  199. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  200. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  201. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  202. myView.layer.shadowOffset = CGSizeMake(0, 0);
  203. myView.layer.shadowOpacity = 0.5;
  204. myView.layer.shadowRadius = 2.0;
  205. // UILabel *mergeTo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 44)];
  206. // mergeTo.textAlignment = NSTextAlignmentCenter;
  207. // mergeTo.textColor=UIColorFromRGB(color);
  208. // mergeTo.backgroundColor = [UIColor clearColor];
  209. // mergeTo.text=NSLocalizedString(@"To", nil);
  210. // [mergeTo sizeToFit];
  211. // [myView addSubview:mergeTo];
  212. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 22)];// 48,2
  213. solabel.textColor=UIColorFromRGB(color);
  214. solabel.backgroundColor = [UIColor clearColor];
  215. solabel.text=NSLocalizedString(@"SO#", nil);
  216. [solabel sizeToFit];
  217. [myView addSubview:solabel];
  218. UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 21, 100, 22)];
  219. userlabel.textColor=UIColorFromRGB(color);
  220. userlabel.backgroundColor = [UIColor clearColor];
  221. userlabel.text=NSLocalizedString(@"Create By", nil);
  222. [userlabel sizeToFit];
  223. [myView addSubview:userlabel];
  224. UILabel *createTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(tableView.bounds.size.width - 150, 21, 100, 22)];
  225. createTimeLabel.textColor=UIColorFromRGB(color);
  226. createTimeLabel.backgroundColor = [UIColor clearColor];
  227. createTimeLabel.text=NSLocalizedString(@"Create Time", nil);
  228. [createTimeLabel sizeToFit];
  229. [myView addSubview:createTimeLabel];
  230. myView.autoresizesSubviews=true;
  231. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  232. //
  233. return myView;
  234. }
  235. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  236. {
  237. return 44;
  238. }
  239. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  240. return 86;
  241. }
  242. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  243. SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  244. if (self.mergeTo && self.mergeTo.row != indexPath.row) {
  245. [self tableView:tableView didSelectRowAtIndexPath:self.mergeTo];
  246. }
  247. cell.selected = NO;
  248. cell.checkedButton.selected = !cell.checkedButton.selected;
  249. NSMutableDictionary *dic = (NSMutableDictionary *)[self.mergeList objectAtIndex:indexPath.row];
  250. int check = [dic[@"check"] integerValue];
  251. if (cell.checkedButton.selected) {
  252. check = 1;
  253. self.mergeTo = indexPath;
  254. } else {
  255. self.mergeTo = nil;
  256. check = 0;
  257. }
  258. [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"];
  259. }
  260. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  261. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  262. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  263. [self.mergeList removeObject:dic];
  264. // self.mergeTo = nil;
  265. NSString *order_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_id"]];
  266. if (self.deleteBlock) {
  267. self.deleteBlock(order_id);
  268. }
  269. [tableView reloadData];
  270. }];
  271. return @[deleteAction];
  272. }
  273. //- (BOOL
  274. - (void)onCloseClick:(id)sender {
  275. [self dismissViewControllerAnimated:true completion:nil];
  276. }
  277. - (void)onClearClick:(id)sender {
  278. if (self.clearBlock) {
  279. self.clearBlock();
  280. }
  281. [self.mergeList removeAllObjects];
  282. [self.syncDataTableView reloadData];
  283. }
  284. @end