SelectUploadOrderViewController.m 16 KB

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