CustomerEditViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // CustomerEditViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 11/4/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CustomerEditViewController.h"
  9. #import "RANetwork.h"
  10. #import "MainViewController.h"
  11. #import "JKTimerManager.h"
  12. @interface CustomerEditViewController ()
  13. @end
  14. @implementation CustomerEditViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.editable = YES;
  18. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  19. style:UIBarButtonItemStylePlain
  20. target:self
  21. action:@selector(onBackClick:)];
  22. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  23. // closeButton.title = @"Close";
  24. self.navigationItem.leftBarButtonItem = closeButton;
  25. // NSMutableArray * items = [[NSMutableArray alloc]init];
  26. UIBarButtonItem *savebtn = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)];
  27. savebtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  28. style:UIBarButtonItemStylePlain
  29. target:self
  30. action:@selector(onSaveClick:)];
  31. self.navigationItem.rightBarButtonItem = savebtn;
  32. // UIBarButtonItem *btnCommit = nil;
  33. // btnCommit =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"commit"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  34. // style:UIBarButtonItemStylePlain
  35. // target:self
  36. // action:@selector(onCommitOrderClick:)];
  37. //
  38. //
  39. //
  40. //
  41. // //savebtn setBackgroundImage:(nullable UIImage *) forState:<#(UIControlState)#> style:<#(UIBarButtonItemStyle)#> barMetrics:<#(UIBarMetrics)#>
  42. // //[savebtn setImage:[UIImage imageNamed:@"save"] ];
  43. //
  44. //
  45. // /*
  46. // UIBarMetricsDefault,
  47. // UIBarMetricsCompact,
  48. // UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
  49. // UIBarMetricsCompactPrompt
  50. //
  51. // */
  52. // // [savebtn setBackgroundImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  53. // // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  54. // // item1.title = @"item1";
  55. // //// item1.image = [UIImage imageNamed:@"rect_setting"];
  56. // // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  57. // //// item2.image = [UIImage imageNamed:@"rect_about"];
  58. // // item2.title = @"item2";
  59. //
  60. //
  61. // UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  62. // fixedItem.width = 20.0f;
  63. //
  64. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  65. //
  66. // if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  67. // {
  68. // [items addObject:savebtn];
  69. //
  70. // [items addObject:fixedItem];
  71. // }
  72. // [items addObject:btnCommit];
  73. // // [items addObject:item2];
  74. // self.navigationItem.rightBarButtonItems=items;
  75. // Do any additional setup after loading the view.
  76. }
  77. - (void)onBackClick:(UIButton *)sender {
  78. [self.navigationController popViewControllerAnimated:false];
  79. if(self.returnValue)
  80. self.returnValue(nil);
  81. }
  82. - (void)onSaveClick:(UIButton *)sender {
  83. // add params check here.
  84. [self.lastedit endEditing:true];
  85. [self.lasttextview endEditing:true];
  86. NSMutableDictionary* upparams = [self check_cancommit:true];
  87. if(self.cancommit==false)
  88. return;
  89. if(self.bolocustomer)
  90. {
  91. [self.navigationController popViewControllerAnimated:false];
  92. if(self.returnValue)
  93. self.returnValue(upparams);
  94. return;
  95. }
  96. __weak typeof(self) weakself = self;
  97. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Saving Contact" completion:^{
  98. PopWaitAlert* pop = [RAUtils waiting_pop:@"Saving Contact" completion:nil];
  99. resultHandler handler =^(NSMutableDictionary *result) {
  100. NSDictionary* editor_json = result;
  101. // [waitalert dismissViewControllerAnimated:YES completion:^{
  102. [pop hide];
  103. if([[editor_json valueForKey:@"result"] intValue]==2)
  104. {
  105. [weakself prepareReturn:nil];
  106. if(weakself.bnewcustomer )
  107. [weakself.navigationController popViewControllerAnimated:false];
  108. else
  109. [weakself.navigationController popToRootViewControllerAnimated:false];
  110. #ifdef RA_NOTIFICATION
  111. [ActiveViewController Notify:@"ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  112. #else
  113. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  114. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  115. #endif
  116. }
  117. else
  118. {
  119. [RAUtils message_box:@"Save Order" message:[editor_json valueForKey:@"err_msg"] completion:nil];
  120. }
  121. // }];
  122. } ;
  123. if(self.bnewcustomer )
  124. [RANetwork request_add_customer:upparams completionHandler:handler];
  125. else
  126. [RANetwork request_update_customer:upparams completionHandler:handler];
  127. // }];
  128. }
  129. - (void)didReceiveMemoryWarning {
  130. [super didReceiveMemoryWarning];
  131. // Dispose of any resources that can be recreated.
  132. }
  133. /*
  134. #pragma mark - Navigation
  135. // In a storyboard-based application, you will often want to do a little preparation before navigation
  136. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  137. // Get the new view controller using [segue destinationViewController].
  138. // Pass the selected object to the new view controller.
  139. }
  140. */
  141. #pragma mark - Urgency
  142. - (void)viewWillAppear:(BOOL)animated {
  143. [super viewWillAppear:animated];
  144. [self start_urgency_timer];
  145. }
  146. -(void) viewWillDisappear:(BOOL)animated
  147. {
  148. [super viewWillDisappear:animated];
  149. [self cancel_urgency_timer];
  150. }
  151. - (void)dealloc {
  152. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  153. [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]];
  154. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"];
  155. }
  156. - (void)start_urgency_timer {
  157. if (!self.editable) {
  158. return;
  159. }
  160. __weak typeof(self) weakSelf = self;
  161. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  162. [appDelegate.urgencyDic setValue:RASingleton.sharedInstance.user forKey:@"user"];
  163. #ifdef OFFLINE_MODE
  164. [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
  165. #endif
  166. [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{
  167. NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary];
  168. // DebugLog(@"timer start....");
  169. [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  170. [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"];
  171. [urgencyDic setValue:@(weakSelf.bnewcustomer) forKey:@"new_customer"];
  172. if ([weakSelf.class_name isEqualToString:@"CustomerEditViewController"]) {
  173. // CustomerEditViewController
  174. __block NSString *contact_id = @"";
  175. [weakSelf.content_data_download enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  176. // section_0
  177. if ([key isEqualToString:@"section_0"]) {
  178. NSDictionary *section_0 = (NSDictionary *)obj;
  179. [section_0 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  180. // Contact ID
  181. if ([obj isKindOfClass:[NSDictionary class]]) {
  182. if ([[obj objectForKey:@"aname"] isEqualToString:@"Contact ID"]) {
  183. contact_id = [obj objectForKey:@"value"];
  184. }
  185. }
  186. }];
  187. }
  188. }];
  189. [urgencyDic setValue:contact_id forKey:@"contact_id"];
  190. }
  191. [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]];
  192. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"];
  193. }];
  194. }
  195. - (void)cancel_urgency_timer {
  196. [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
  197. }
  198. @end