CustomerEditViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. }
  80. - (void)onSaveClick:(UIButton *)sender {
  81. // add params check here.
  82. [self.lastedit endEditing:true];
  83. [self.lasttextview endEditing:true];
  84. // self.cancommit=true;
  85. // NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
  86. // // [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
  87. // int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  88. // for(int i=0;i<section_count;i++)
  89. // {
  90. // NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  91. // int item_count = [[sectionjson valueForKey:@"count"] intValue];
  92. // for(int j=0;j<item_count;j++)
  93. // {
  94. // NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  95. // NSString* key = [itemjson valueForKey:@"name"];
  96. // if(key==nil || key.length==0)
  97. // continue;
  98. // if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
  99. // {
  100. // NSString* single_select = [itemjson valueForKey:@"single_select"];
  101. // if([single_select isEqualToString:@"true"])
  102. // {
  103. // NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  104. // int count = [[cadejson valueForKey:@"count"] intValue];
  105. // bool setvalue = false;
  106. // for(int cc=0;cc<count;cc++)
  107. // {
  108. // NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  109. // if([[valjson valueForKey:@"check"]intValue]==1)
  110. // {
  111. // [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  112. // setvalue=true;
  113. // break;
  114. // }
  115. // }
  116. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
  117. // {
  118. // self.cancommit = false;
  119. // }
  120. // }
  121. // else
  122. // {
  123. //
  124. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  125. // NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  126. // int count = [[cadejson valueForKey:@"count"] intValue];
  127. // // bool setvalue = false;
  128. // for(int cc=0;cc<count;cc++)
  129. // {
  130. // NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  131. // if([[valjson valueForKey:@"check"]intValue]==1)
  132. // {
  133. //
  134. // [checked addObject:[NSNumber numberWithInt:[[valjson valueForKey:@"value_id"] intValue]]];
  135. // // [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  136. // // setvalue=true;
  137. // // break;
  138. // }
  139. // }
  140. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"]&&checked.count==0)
  141. // {
  142. // self.cancommit = false;
  143. // }
  144. // else
  145. // {
  146. // NSString * string = [checked componentsJoinedByString:@","];
  147. // [upparams setValue:string forKey:[itemjson valueForKey:@"name"]];
  148. // }
  149. // }
  150. // /*
  151. // upparams setValue:itemjson forKey:<#(NSString *)#>
  152. //
  153. // */
  154. // }else
  155. // if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  156. // {
  157. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  158. // self.cancommit = false;
  159. // }
  160. // else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  161. // {
  162. // NSString* valuestr = [itemjson valueForKey:@"value"];
  163. // [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  164. //
  165. // NSString* value = [itemjson valueForKey:@"value"];
  166. // NSMutableDictionary* boolitem=nil;
  167. // NSMutableDictionary* subjson=nil;
  168. // if([value isEqualToString:@"true"])
  169. // {
  170. // boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  171. //
  172. // }
  173. // else
  174. // {
  175. // boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  176. //
  177. // }
  178. // subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  179. // if( subjson!=nil /*&& !active*/)
  180. // {
  181. // int sub_count = [[subjson valueForKey:@"count"] intValue];
  182. // for(int l=0;l<sub_count;l++)
  183. // {
  184. // NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  185. //
  186. //
  187. //
  188. //
  189. // if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  190. // [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  191. // else
  192. // {
  193. // if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  194. // self.cancommit = false;
  195. // }
  196. // }
  197. //
  198. //
  199. //
  200. // }
  201. // }
  202. // else
  203. // if([[itemjson valueForKey:@"control"] isEqualToString:@"img"])
  204. // {
  205. // if([itemjson valueForKey:@"avalue"]!=nil && ![[itemjson valueForKey:@"avalue"]isEqualToString:@""])
  206. // [upparams setValue:[itemjson valueForKey:@"avalue"] forKey:[itemjson valueForKey:@"name"]];
  207. // else
  208. // {
  209. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  210. // self.cancommit = false;
  211. // }
  212. // }
  213. // else
  214. // {
  215. // if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  216. // [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  217. // else
  218. // {
  219. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  220. // self.cancommit = false;
  221. // }
  222. // }
  223. // upparams= [self subitem_param:itemjson addto:upparams ];
  224. // }
  225. // }
  226. // if(self.cancommit==false)
  227. // {
  228. // [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  229. // self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  230. // //DebugLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  231. // //[self.editorTable reloadData ];
  232. // [self.editorTable reloadData];
  233. // return;
  234. // }
  235. NSMutableDictionary* upparams = [self check_cancommit:true];
  236. if(self.cancommit==false)
  237. return;
  238. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Saving Contact"];
  239. __weak typeof(self) weakself = self;
  240. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  241. NSDictionary* editor_json = nil;
  242. // NSString* contactId=[upparams valueForKey:@"contactId"];
  243. if(self.bnewcustomer )
  244. editor_json=[RANetwork save_newCustomer:upparams];
  245. else
  246. editor_json=[RANetwork update_Customer:upparams];
  247. dispatch_async(dispatch_get_main_queue(), ^{
  248. [waitalert dismissViewControllerAnimated:YES completion:nil];
  249. if([[editor_json valueForKey:@"result"] intValue]==2)
  250. {
  251. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  252. //
  253. //
  254. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  255. // appDelegate.customerInfo = customerInfo;
  256. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  257. // // self.labelContact.text=appDelegate.contact_name;
  258. //
  259. // NSDictionary * customerinfo = appDelegate.customerInfo;
  260. [weakself prepareReturn:nil];
  261. if(weakself.bnewcustomer )
  262. [weakself.navigationController popViewControllerAnimated:false];
  263. else
  264. [weakself.navigationController popToRootViewControllerAnimated:false];
  265. #ifdef RA_NOTIFICATION
  266. [ActiveViewController Notify:@"ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  267. #else
  268. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  269. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  270. #endif
  271. }
  272. else
  273. {
  274. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:weakself] ;
  275. }
  276. });
  277. });
  278. }
  279. - (void)didReceiveMemoryWarning {
  280. [super didReceiveMemoryWarning];
  281. // Dispose of any resources that can be recreated.
  282. }
  283. /*
  284. #pragma mark - Navigation
  285. // In a storyboard-based application, you will often want to do a little preparation before navigation
  286. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  287. // Get the new view controller using [segue destinationViewController].
  288. // Pass the selected object to the new view controller.
  289. }
  290. */
  291. #pragma mark - Urgency
  292. - (void)viewWillAppear:(BOOL)animated {
  293. [super viewWillAppear:animated];
  294. [self start_urgency_timer];
  295. }
  296. -(void) viewWillDisappear:(BOOL)animated
  297. {
  298. [super viewWillDisappear:animated];
  299. [self cancel_urgency_timer];
  300. }
  301. - (void)dealloc {
  302. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  303. [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]];
  304. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"];
  305. }
  306. - (void)start_urgency_timer {
  307. if (!self.editable) {
  308. return;
  309. }
  310. __weak typeof(self) weakSelf = self;
  311. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  312. [appDelegate.urgencyDic setValue:appDelegate.user forKey:@"user"];
  313. #ifdef OFFLINE_MODE
  314. [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
  315. #endif
  316. [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{
  317. NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary];
  318. // DebugLog(@"timer start....");
  319. [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  320. [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"];
  321. [urgencyDic setValue:@(weakSelf.bnewcustomer) forKey:@"new_customer"];
  322. if ([weakSelf.class_name isEqualToString:@"CustomerEditViewController"]) {
  323. // CustomerEditViewController
  324. __block NSString *contact_id = @"";
  325. [weakSelf.content_data_download enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  326. // section_0
  327. if ([key isEqualToString:@"section_0"]) {
  328. NSDictionary *section_0 = (NSDictionary *)obj;
  329. [section_0 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  330. // Contact ID
  331. if ([obj isKindOfClass:[NSDictionary class]]) {
  332. if ([[obj objectForKey:@"aname"] isEqualToString:@"Contact ID"]) {
  333. contact_id = [obj objectForKey:@"value"];
  334. }
  335. }
  336. }];
  337. }
  338. }];
  339. [urgencyDic setValue:contact_id forKey:@"contact_id"];
  340. }
  341. [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]];
  342. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"];
  343. }];
  344. }
  345. - (void)cancel_urgency_timer {
  346. [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
  347. }
  348. @end