CustomerInfoViewController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. //
  2. // CustomerInfoViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 9/10/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CustomerInfoViewController.h"
  9. #import "RANetwork.h"
  10. #import "CustomerEditViewController.h"
  11. #import "MainViewController.h"
  12. #import "JKTimerManager.h"
  13. @interface CustomerInfoViewController ()
  14. @end
  15. @implementation CustomerInfoViewController
  16. - (void)onEditClick:(id)sender {
  17. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  18. [params setValue:self.contactId forKey:@"contact_id"];
  19. CustomerEditViewController * cuseditVC = [[CustomerEditViewController alloc]initWithNibName:@"CommonEditor.iPad" bundle:nil];
  20. cuseditVC.bnewcustomer=false;
  21. cuseditVC.url_type = URL_REMOTE;
  22. cuseditVC.request_url=URL_EDIT_CUSTOMER;
  23. cuseditVC.params = params;
  24. cuseditVC.delegate=self;
  25. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  26. [self.navigationController pushViewController:cuseditVC animated:false];
  27. }
  28. - (void)onAssignClick:(id)sender {
  29. [self.lastedit endEditing:true];
  30. [self.lasttextview endEditing:true];
  31. // [self.navigationController popViewControllerAnimated:(false)];
  32. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  33. // NSMutableDictionary* data = [[self.data_init objectForKey:@"customerInfo"] mutableCopy];
  34. for(int i=0;i<section_count;i++)
  35. {
  36. NSDictionary * sectionjson= [self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i]] ;
  37. int count = [[sectionjson valueForKey:@"count"] intValue];
  38. for(int j=0;j<count;j++)
  39. {
  40. NSDictionary * itemjson=[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j]];
  41. //NSObject* obj=[itemjson valueForKey:@"name"];
  42. NSString* name = itemjson[@"name"];//[[ valueForKey:@"name"] stringValue];
  43. // DebugLog(@"%@",name.class);
  44. // DebugLog(@"%@",self.data_init.class);
  45. // DebugLog(@"%@",NSDictionary.class);
  46. NSString* value = itemjson[@"value"];//[[itemjson valueForKey:@""] stringValue];
  47. //[self.data_init setValue:value forKey:name];
  48. self.data_init[name]=value;
  49. }
  50. }
  51. //---------------- construct customer_contact----------------------
  52. NSString* customer_first_name = [self.data_init valueForKey:@"customer_first_name"];
  53. if(customer_first_name==nil)
  54. customer_first_name=@"";
  55. NSString* customer_last_name= [self.data_init valueForKey:@"customer_last_name"];
  56. if(customer_last_name==nil)
  57. customer_last_name=@"";
  58. NSString* customer_contact = [NSString stringWithFormat:@"%@ %@",customer_first_name,customer_last_name];
  59. customer_contact=[customer_contact stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  60. [self.data_init setValue:customer_contact forKey:@"customer_contact"];
  61. //---------------- finish construct customer_contact----------------------
  62. #ifdef SCANNER_ORDER
  63. [self.navigationController popViewControllerAnimated:false];
  64. if(self.returnValue)
  65. {
  66. self.returnValue(self.data_init);
  67. }
  68. return;
  69. #else
  70. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  71. if(self.update_order && appDelegate.order_code!=nil)
  72. {
  73. [RANetwork request_update_ordercustomer:appDelegate.order_code customerinfo:self.data_init completionHandler:^(NSMutableDictionary *result) {
  74. NSDictionary* editor_json = result;
  75. if([[editor_json valueForKey:@"result"] intValue]==2)
  76. {
  77. [self.navigationController popViewControllerAnimated:false];
  78. if(self.returnValue)
  79. {
  80. self.returnValue(self.data_init);
  81. }
  82. }
  83. else
  84. {
  85. [RAUtils message_box:@"Update Order Customer Infomation" message:[editor_json valueForKey:@"err_msg"] completion:nil];
  86. }
  87. }];
  88. }
  89. else
  90. {
  91. [self.navigationController popViewControllerAnimated:false];
  92. if(self.returnValue)
  93. {
  94. self.returnValue(self.data_init);
  95. }
  96. }
  97. #endif
  98. }
  99. - (void)onResetClick:(id)sender {
  100. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  101. if(appDelegate.order_code.length>0)
  102. {
  103. //当前已打开订单则关闭订单。
  104. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure to release order and reset contact?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  105. //block代码块取代了delegate
  106. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  107. #ifdef SCANNER_ORDER
  108. [self.navigationController popViewControllerAnimated:false];
  109. if(self.returnValue)
  110. {
  111. self.returnValue(nil);
  112. }
  113. return;
  114. #else
  115. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Release Order" completion:^{
  116. PopWaitAlert* pop = [RAUtils waiting_pop:@"Release Order" completion:nil];
  117. [RANetwork request_release_order:appDelegate.order_code withScreen:ScreenCodeCustomerInfo completionHandler:^(NSMutableDictionary *result) {
  118. NSDictionary* order_json = result;
  119. // [waitalert dismissViewControllerAnimated:YES completion:^{
  120. [pop hide];
  121. if([[order_json valueForKey:@"result"] intValue]==2)
  122. {
  123. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  124. // appDelegate.order_code = nil;
  125. [appDelegate closeOrder];
  126. #ifdef RA_NOTIFICATION
  127. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  128. [ActiveViewController Notify:@"ContactListViewController" Message:RA_NOTIFICATION_REFRESH_UI];
  129. #else
  130. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  131. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  132. [((MainViewController*)appDelegate.main_vc) reloadContact:false immediately:false];
  133. #endif
  134. [self.navigationController popToRootViewControllerAnimated:false];
  135. }
  136. else
  137. {
  138. [RAUtils message_box:@"Release Order" message:[order_json valueForKey:@"err_msg"] completion:nil];
  139. }
  140. // }];
  141. }];
  142. // }];
  143. #endif
  144. }];
  145. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  146. DebugLog(@"Cancel");
  147. }];
  148. [alertControl addAction:actionOne];
  149. [alertControl addAction:alertthree];
  150. [self presentViewController:alertControl animated:YES completion:nil];
  151. }
  152. else
  153. {
  154. //询问并清空当前的全局contact_id、customer
  155. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure to reset contact?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  156. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  157. #ifdef SCANNER_ORDER
  158. [self.navigationController popViewControllerAnimated:false];
  159. if(self.returnValue)
  160. {
  161. self.returnValue(nil);
  162. }
  163. return;
  164. #else
  165. if(appDelegate.user_type!=USER_ROLE_CUSTOMER)
  166. {
  167. appDelegate.contact_id=nil;
  168. appDelegate.customerInfo = nil;
  169. }
  170. [self.navigationController popViewControllerAnimated:(false)];
  171. #endif
  172. }];
  173. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  174. DebugLog(@"Cancel");
  175. }];
  176. [alertControl addAction:actionOne];
  177. [alertControl addAction:alertthree];
  178. [self presentViewController:alertControl animated:YES completion:nil];
  179. }
  180. }
  181. - (void)viewDidLoad {
  182. [super viewDidLoad];
  183. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  184. if(self.rightbtn_title!=nil)
  185. [ self.right_btn setTitle:self.rightbtn_title];
  186. // UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onBackClick:)];
  187. // // closeButton.title = @"Close";
  188. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  189. style:UIBarButtonItemStylePlain
  190. target:self
  191. action:@selector( onBackClick:)];
  192. self.navigationItem.leftBarButtonItem = backButton;
  193. NSMutableArray * items = [[NSMutableArray alloc]init];
  194. self.btn_edit = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"edit"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  195. style:UIBarButtonItemStylePlain
  196. target:self
  197. action:@selector(onEditClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Download", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onDownloadOrderClick:)];
  198. // self.btn_edit.tintColor = UIColorFromRGB(0x996633);
  199. self.btn_assign = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"assign"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  200. style:UIBarButtonItemStylePlain
  201. target:self
  202. action:@selector(onAssignClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Download", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onDownloadOrderClick:)];
  203. // self.btn_assign.tintColor = UIColorFromRGB(0x996633);
  204. self.btn_reset = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"reset"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  205. style:UIBarButtonItemStylePlain
  206. target:self
  207. action:@selector(onResetClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Download", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onDownloadOrderClick:)];
  208. // self.btn_reset.tintColor = UIColorFromRGB(0x996633);
  209. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  210. if(self.edit_icon)
  211. [items addObject:self.btn_edit];
  212. if ((appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) && appDelegate.order_code.length > 0 && ![RASingleton sharedInstance].currentOrderIsMerged) {
  213. // shop order 隐藏assign
  214. } else {
  215. if(self.assig_icon)
  216. [items addObject:self.btn_assign];
  217. }
  218. if(self.reset_icon)
  219. [items addObject:self.btn_reset];
  220. self.navigationItem.rightBarButtonItems=items;
  221. if (self.data_init==nil) {
  222. [self request_fill];
  223. }
  224. else
  225. {
  226. [self fillData];
  227. }
  228. // Do any additional setup after loading the view.
  229. }
  230. - (void)onBackClick:(UIButton *)sender {
  231. [self.lastedit endEditing:true];
  232. [self.lasttextview endEditing:true];
  233. NSMutableDictionary* back_data = [self.data_init mutableCopy];
  234. // [self.navigationController popViewControllerAnimated:(false)];
  235. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  236. // NSMutableDictionary* data = [[self.data_init objectForKey:@"customerInfo"] mutableCopy];
  237. for(int i=0;i<section_count;i++)
  238. {
  239. NSDictionary * sectionjson= [self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i]] ;
  240. int count = [[sectionjson valueForKey:@"count"] intValue];
  241. for(int j=0;j<count;j++)
  242. {
  243. NSDictionary * itemjson=[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j]];
  244. NSString* name = [itemjson valueForKey:@"name"];
  245. NSString* value = [itemjson valueForKey:@"value"];
  246. [back_data setValue:value forKey:name];
  247. }
  248. }
  249. //---------------- construct customer_contact----------------------
  250. NSString* customer_first_name = [back_data valueForKey:@"customer_first_name"];
  251. if(customer_first_name==nil)
  252. customer_first_name=@"";
  253. NSString* customer_last_name= [back_data valueForKey:@"customer_last_name"];
  254. if(customer_last_name==nil)
  255. customer_last_name=@"";
  256. NSString* customer_contact = [NSString stringWithFormat:@"%@ %@",customer_first_name,customer_last_name];
  257. customer_contact=[customer_contact stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  258. [back_data setValue:customer_contact forKey:@"customer_contact"];
  259. if(self.onClose)
  260. {
  261. self.onClose(back_data);
  262. }
  263. [self.navigationController popViewControllerAnimated:(false)];
  264. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  265. // ;
  266. // }];
  267. }
  268. -(void) request_fill
  269. {
  270. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Loading Contact information" completion:^{
  271. PopWaitAlert* pop = [RAUtils waiting_pop:@"Loading Contact information" completion:nil];
  272. [RANetwork request_contactinfo:self.contactId completionHandler:^(NSMutableDictionary *result) {
  273. NSMutableDictionary* editor_json = result;
  274. [pop hide];
  275. // [waitalert dismissViewControllerAnimated:YES completion:^{
  276. if([[editor_json valueForKey:@"result"] intValue]==2)
  277. {
  278. self.data_init = [[editor_json objectForKey:@"customerInfo" ] mutableCopy];
  279. [self fillData];
  280. self.btn_assign.enabled=true;
  281. }
  282. else
  283. {
  284. [RAUtils message_box:@"Loading Contact Information" message:[editor_json valueForKey:@"err_msg"] completion:nil] ;
  285. self.btn_assign.enabled=false;
  286. }
  287. // }];
  288. }];
  289. return;
  290. // }];
  291. }
  292. - (void) fillData
  293. {
  294. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  295. // NSDictionary* data = [self.data_init objectForKey:@"customerInfo"];
  296. for(int i=0;i<section_count;i++)
  297. {
  298. NSMutableDictionary * sectionjson= [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i]] mutableCopy];
  299. int count = [[sectionjson valueForKey:@"count"] intValue];
  300. for(int j=0;j<count;j++)
  301. {
  302. NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  303. NSString* control=[itemjson valueForKey:@"control"];
  304. NSString* name = [itemjson valueForKey:@"name"];
  305. NSString* value = [self.data_init valueForKey:name];
  306. [itemjson setValue:value forKey:@"value"];
  307. if([control isEqualToString:@"img"])
  308. {
  309. {
  310. NSString* img_url = [itemjson valueForKey:@"img_url_0"];
  311. NSString* url_value = [self.data_init valueForKey:img_url];
  312. [itemjson setValue:url_value forKey:@"img_url_0"];
  313. }
  314. {
  315. NSString* img_url = [itemjson valueForKey:@"img_url_1"];
  316. NSString* url_value = [self.data_init valueForKey:img_url];
  317. [itemjson setValue:url_value forKey:@"img_url_1"];
  318. }
  319. {
  320. NSString* img_url = [itemjson valueForKey:@"img_url_2"];
  321. NSString* url_value = [self.data_init valueForKey:img_url];
  322. [itemjson setValue:url_value forKey:@"img_url_2"];
  323. }
  324. }
  325. [sectionjson setObject:itemjson forKey:[NSString stringWithFormat:@"item_%d",j]];
  326. }
  327. [self.content_data_download setObject:sectionjson forKey:[NSString stringWithFormat:@"section_%d",i]];
  328. }
  329. self.content_data_control=[self translate_json:self.content_data_download changed:self.changed_data];
  330. // self.navigationItem.title = [self.content_data_download valueForKey:@"title"] ;
  331. [self.editorTable reloadData];
  332. }
  333. - (void)didReceiveMemoryWarning {
  334. [super didReceiveMemoryWarning];
  335. // Dispose of any resources that can be recreated.
  336. }
  337. /*
  338. #pragma mark - Navigation
  339. // In a storyboard-based application, you will often want to do a little preparation before navigation
  340. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  341. // Get the new view controller using [segue destinationViewController].
  342. // Pass the selected object to the new view controller.
  343. }
  344. */
  345. #pragma mark - Urgency
  346. - (void)viewWillAppear:(BOOL)animated {
  347. [super viewWillAppear:animated];
  348. [self start_urgency_timer];
  349. }
  350. -(void) viewWillDisappear:(BOOL)animated
  351. {
  352. [super viewWillDisappear:animated];
  353. [self cancel_urgency_timer];
  354. }
  355. - (void)dealloc {
  356. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  357. [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]];
  358. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"];
  359. }
  360. - (void)start_urgency_timer {
  361. if (!self.editable) {
  362. return;
  363. }
  364. __weak typeof(self) weakSelf = self;
  365. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  366. [appDelegate.urgencyDic setValue:RASingleton.sharedInstance.user forKey:@"user"];
  367. #ifdef OFFLINE_MODE
  368. [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
  369. #endif
  370. [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{
  371. NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary];
  372. // DebugLog(@"timer start....");
  373. [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  374. [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"];
  375. [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]];
  376. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"];
  377. }];
  378. }
  379. - (void)cancel_urgency_timer {
  380. [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
  381. }
  382. @end