SelectUploadOrderViewController.m 14 KB

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