SelectUploadOrderViewController.m 16 KB

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