CustomerEditViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. resultHandler handler =^(NSMutableDictionary *result) {
  99. NSDictionary* editor_json = result;
  100. [waitalert dismissViewControllerAnimated:YES completion:^{
  101. if([[editor_json valueForKey:@"result"] intValue]==2)
  102. {
  103. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  104. //
  105. //
  106. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  107. // appDelegate.customerInfo = customerInfo;
  108. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  109. // // self.labelContact.text=appDelegate.contact_name;
  110. //
  111. // NSDictionary * customerinfo = appDelegate.customerInfo;
  112. [weakself prepareReturn:nil];
  113. if(weakself.bnewcustomer )
  114. [weakself.navigationController popViewControllerAnimated:false];
  115. else
  116. [weakself.navigationController popToRootViewControllerAnimated:false];
  117. #ifdef RA_NOTIFICATION
  118. [ActiveViewController Notify:@"ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  119. #else
  120. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  121. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  122. #endif
  123. }
  124. else
  125. {
  126. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:weakself] ;
  127. }
  128. }];
  129. } ;
  130. if(self.bnewcustomer )
  131. [RANetwork request_add_customer:upparams completionHandler:handler];
  132. else
  133. [RANetwork request_update_customer:upparams completionHandler:handler];
  134. }];
  135. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  136. //
  137. // NSDictionary* editor_json = nil;
  138. // // NSString* contactId=[upparams valueForKey:@"contactId"];
  139. // if(self.bnewcustomer )
  140. // editor_json=[RANetwork save_newCustomer:upparams];
  141. // else
  142. // editor_json=[RANetwork update_Customer:upparams];
  143. //
  144. // dispatch_async(dispatch_get_main_queue(), ^{
  145. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  146. //
  147. //
  148. // if([[editor_json valueForKey:@"result"] intValue]==2)
  149. // {
  150. // // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  151. // //
  152. // //
  153. // // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  154. // // appDelegate.customerInfo = customerInfo;
  155. // // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  156. // // // self.labelContact.text=appDelegate.contact_name;
  157. // //
  158. // // NSDictionary * customerinfo = appDelegate.customerInfo;
  159. // [weakself prepareReturn:nil];
  160. // if(weakself.bnewcustomer )
  161. // [weakself.navigationController popViewControllerAnimated:false];
  162. // else
  163. // [weakself.navigationController popToRootViewControllerAnimated:false];
  164. //
  165. //
  166. //
  167. //
  168. //#ifdef RA_NOTIFICATION
  169. //
  170. // [ActiveViewController Notify:@"ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  171. //#else
  172. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  173. // [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  174. //#endif
  175. //
  176. // }
  177. // else
  178. // {
  179. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:weakself] ;
  180. // }
  181. //
  182. //
  183. //
  184. //
  185. // });
  186. // });
  187. }
  188. - (void)didReceiveMemoryWarning {
  189. [super didReceiveMemoryWarning];
  190. // Dispose of any resources that can be recreated.
  191. }
  192. /*
  193. #pragma mark - Navigation
  194. // In a storyboard-based application, you will often want to do a little preparation before navigation
  195. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  196. // Get the new view controller using [segue destinationViewController].
  197. // Pass the selected object to the new view controller.
  198. }
  199. */
  200. #pragma mark - Urgency
  201. - (void)viewWillAppear:(BOOL)animated {
  202. [super viewWillAppear:animated];
  203. [self start_urgency_timer];
  204. }
  205. -(void) viewWillDisappear:(BOOL)animated
  206. {
  207. [super viewWillDisappear:animated];
  208. [self cancel_urgency_timer];
  209. }
  210. - (void)dealloc {
  211. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  212. [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]];
  213. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"];
  214. }
  215. - (void)start_urgency_timer {
  216. if (!self.editable) {
  217. return;
  218. }
  219. __weak typeof(self) weakSelf = self;
  220. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  221. [appDelegate.urgencyDic setValue:appDelegate.user forKey:@"user"];
  222. #ifdef OFFLINE_MODE
  223. [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
  224. #endif
  225. [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{
  226. NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary];
  227. // DebugLog(@"timer start....");
  228. [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  229. [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"];
  230. [urgencyDic setValue:@(weakSelf.bnewcustomer) forKey:@"new_customer"];
  231. if ([weakSelf.class_name isEqualToString:@"CustomerEditViewController"]) {
  232. // CustomerEditViewController
  233. __block NSString *contact_id = @"";
  234. [weakSelf.content_data_download enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  235. // section_0
  236. if ([key isEqualToString:@"section_0"]) {
  237. NSDictionary *section_0 = (NSDictionary *)obj;
  238. [section_0 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  239. // Contact ID
  240. if ([obj isKindOfClass:[NSDictionary class]]) {
  241. if ([[obj objectForKey:@"aname"] isEqualToString:@"Contact ID"]) {
  242. contact_id = [obj objectForKey:@"value"];
  243. }
  244. }
  245. }];
  246. }
  247. }];
  248. [urgencyDic setValue:contact_id forKey:@"contact_id"];
  249. }
  250. [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]];
  251. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"];
  252. }];
  253. }
  254. - (void)cancel_urgency_timer {
  255. [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
  256. }
  257. @end