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