SelectUploadOrderViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. [self->_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. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Release Order"];
  126. NSDictionary* order_json = [RANetwork release_Order:order_code withScreen:ScreenCodeOfflineSetting];
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. [waitalert dismissViewControllerAnimated:YES completion:nil];
  129. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  130. if([[order_json valueForKey:@"result"] intValue]==2)
  131. {
  132. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  133. [appDelegate closeOrder];
  134. closeOrder = YES;
  135. [weakself uploadButtonClicked:weakself.uploadButton];
  136. } else {
  137. // 释放失败
  138. closeOrder = NO;
  139. [RAUtils message_alert:@"Release Order Failed!" title:@"Warning" controller:weakself];
  140. }
  141. });
  142. }];
  143. [alertVC addAction:cancelAction];
  144. [alertVC addAction:closeOrderAction];
  145. [weakself presentViewController:alertVC animated:YES completion:nil];
  146. }
  147. }];
  148. if (closeOrder) {
  149. [order_ids deleteCharactersInRange:NSMakeRange(order_ids.length - 1, 1)];
  150. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  151. [params setObject:order_ids forKey:@"order_ids"];
  152. [params setObject:checked_id forKey:@"targetOrderId"];
  153. [params setObject:[Singleton sharedInstance].customerInfo forKey:@"customerInfo"];
  154. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Merge Order"];
  155. [RANetwork request_mergeorder:params completionHandler:^(NSMutableDictionary *result) {
  156. NSDictionary *ret = result;
  157. // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  158. [waitalert dismissViewControllerAnimated:YES completion:nil];
  159. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  160. if (weakself.mergeBlock) {
  161. weakself.mergeBlock(ret);
  162. }
  163. }];
  164. }];
  165. // NSDictionary *ret = [RANetwork merge_order:params];
  166. //
  167. // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  168. //
  169. // [self.navigationController dismissViewControllerAnimated:YES completion:^{
  170. //
  171. // if (weakself.mergeBlock) {
  172. // weakself.mergeBlock(ret);
  173. // }
  174. //
  175. // }];
  176. }
  177. }
  178. #pragma mark - dataSource
  179. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  180. return 1;
  181. }
  182. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  183. return self.mergeList.count;
  184. }
  185. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  186. SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath];
  187. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  188. int check = [dic[@"check"] intValue];
  189. NSString *so_id = dic[@"so_id"];
  190. NSString *create_by = dic[@"create_by"];
  191. NSString *create_time = [dic[@"create_time"] stringByAppendingString:@" PST"];
  192. if (check) {
  193. cell.checkedButton.selected = YES;
  194. self.mergeTo = indexPath;
  195. } else {
  196. cell.checkedButton.selected = NO;
  197. }
  198. cell.labelsoid.text = so_id;
  199. cell.labelcompany.text = create_by;
  200. cell.labelCreateTime.text = create_time;
  201. __weak typeof(self) weakSelf = self;
  202. __weak typeof(tableView) weakTable = tableView;
  203. // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  204. cell.checkBlock = ^{
  205. [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath];
  206. };
  207. return cell;
  208. }
  209. #pragma mark - delegate
  210. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  211. {
  212. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  213. if(value==nil)
  214. value=@"";
  215. unsigned long color = strtoul([value UTF8String],0,16);
  216. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  217. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  218. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  219. myView.layer.shadowOffset = CGSizeMake(0, 0);
  220. myView.layer.shadowOpacity = 0.5;
  221. myView.layer.shadowRadius = 2.0;
  222. // UILabel *mergeTo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 44)];
  223. // mergeTo.textAlignment = NSTextAlignmentCenter;
  224. // mergeTo.textColor=UIColorFromRGB(color);
  225. // mergeTo.backgroundColor = [UIColor clearColor];
  226. // mergeTo.text=NSLocalizedString(@"To", nil);
  227. // [mergeTo sizeToFit];
  228. // [myView addSubview:mergeTo];
  229. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 22)];// 48,2
  230. solabel.textColor=UIColorFromRGB(color);
  231. solabel.backgroundColor = [UIColor clearColor];
  232. solabel.text=NSLocalizedString(@"SO#", nil);
  233. [solabel sizeToFit];
  234. [myView addSubview:solabel];
  235. UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 21, 100, 22)];
  236. userlabel.textColor=UIColorFromRGB(color);
  237. userlabel.backgroundColor = [UIColor clearColor];
  238. userlabel.text=NSLocalizedString(@"Create By", nil);
  239. [userlabel sizeToFit];
  240. [myView addSubview:userlabel];
  241. UILabel *createTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(tableView.bounds.size.width - 150, 21, 100, 22)];
  242. createTimeLabel.textColor=UIColorFromRGB(color);
  243. createTimeLabel.backgroundColor = [UIColor clearColor];
  244. createTimeLabel.text=NSLocalizedString(@"Create Time", nil);
  245. [createTimeLabel sizeToFit];
  246. [myView addSubview:createTimeLabel];
  247. myView.autoresizesSubviews=true;
  248. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  249. //
  250. return myView;
  251. }
  252. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  253. {
  254. return 44;
  255. }
  256. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  257. return 86;
  258. }
  259. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  260. SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  261. if (self.mergeTo && self.mergeTo.row != indexPath.row) {
  262. [self tableView:tableView didSelectRowAtIndexPath:self.mergeTo];
  263. }
  264. cell.selected = NO;
  265. cell.checkedButton.selected = !cell.checkedButton.selected;
  266. NSMutableDictionary *dic = (NSMutableDictionary *)[self.mergeList objectAtIndex:indexPath.row];
  267. int check = [dic[@"check"] intValue];
  268. if (cell.checkedButton.selected) {
  269. check = 1;
  270. self.mergeTo = indexPath;
  271. } else {
  272. self.mergeTo = nil;
  273. check = 0;
  274. }
  275. [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"];
  276. }
  277. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  278. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  279. NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
  280. [self.mergeList removeObject:dic];
  281. // self.mergeTo = nil;
  282. NSString *order_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_id"]];
  283. if (self.deleteBlock) {
  284. self.deleteBlock(order_id);
  285. }
  286. [tableView reloadData];
  287. }];
  288. return @[deleteAction];
  289. }
  290. //- (BOOL
  291. - (void)onCloseClick:(id)sender {
  292. [self dismissViewControllerAnimated:true completion:nil];
  293. }
  294. - (void)onClearClick:(id)sender {
  295. if (self.clearBlock) {
  296. self.clearBlock();
  297. }
  298. [self.mergeList removeAllObjects];
  299. [self.syncDataTableView reloadData];
  300. }
  301. @end