SelectUploadOrderViewController.m 17 KB

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