CustomerEditViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "iSalesNetwork.h"
  10. #import "MainViewController.h"
  11. @interface CustomerEditViewController ()
  12. @end
  13. @implementation CustomerEditViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  17. style:UIBarButtonItemStylePlain
  18. target:self
  19. action:@selector(onBackClick:)];
  20. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  21. // closeButton.title = @"Close";
  22. self.navigationItem.leftBarButtonItem = closeButton;
  23. // NSMutableArray * items = [[NSMutableArray alloc]init];
  24. UIBarButtonItem *savebtn = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)];
  25. savebtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  26. style:UIBarButtonItemStylePlain
  27. target:self
  28. action:@selector(onSaveClick:)];
  29. self.navigationItem.rightBarButtonItem = savebtn;
  30. // UIBarButtonItem *btnCommit = nil;
  31. // btnCommit =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"commit"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  32. // style:UIBarButtonItemStylePlain
  33. // target:self
  34. // action:@selector(onCommitOrderClick:)];
  35. //
  36. //
  37. //
  38. //
  39. // //savebtn setBackgroundImage:(nullable UIImage *) forState:<#(UIControlState)#> style:<#(UIBarButtonItemStyle)#> barMetrics:<#(UIBarMetrics)#>
  40. // //[savebtn setImage:[UIImage imageNamed:@"save"] ];
  41. //
  42. //
  43. // /*
  44. // UIBarMetricsDefault,
  45. // UIBarMetricsCompact,
  46. // UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
  47. // UIBarMetricsCompactPrompt
  48. //
  49. // */
  50. // // [savebtn setBackgroundImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  51. // // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  52. // // item1.title = @"item1";
  53. // //// item1.image = [UIImage imageNamed:@"rect_setting"];
  54. // // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  55. // //// item2.image = [UIImage imageNamed:@"rect_about"];
  56. // // item2.title = @"item2";
  57. //
  58. //
  59. // UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  60. // fixedItem.width = 20.0f;
  61. //
  62. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  63. //
  64. // if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  65. // {
  66. // [items addObject:savebtn];
  67. //
  68. // [items addObject:fixedItem];
  69. // }
  70. // [items addObject:btnCommit];
  71. // // [items addObject:item2];
  72. // self.navigationItem.rightBarButtonItems=items;
  73. // Do any additional setup after loading the view.
  74. }
  75. - (void)onBackClick:(UIButton *)sender {
  76. [self.navigationController popViewControllerAnimated:false];
  77. }
  78. - (void)onSaveClick:(UIButton *)sender {
  79. // add params check here.
  80. [self.lastedit endEditing:true];
  81. [self.lasttextview endEditing:true];
  82. self.cancommit=true;
  83. NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
  84. // [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
  85. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  86. for(int i=0;i<section_count;i++)
  87. {
  88. NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  89. int item_count = [[sectionjson valueForKey:@"count"] intValue];
  90. for(int j=0;j<item_count;j++)
  91. {
  92. NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  93. NSString* key = [itemjson valueForKey:@"name"];
  94. if(key==nil || key.length==0)
  95. continue;
  96. if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
  97. {
  98. NSString* single_select = [itemjson valueForKey:@"single_select"];
  99. if([single_select isEqualToString:@"true"])
  100. {
  101. NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  102. int count = [[cadejson valueForKey:@"count"] intValue];
  103. bool setvalue = false;
  104. for(int cc=0;cc<count;cc++)
  105. {
  106. NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  107. if([[valjson valueForKey:@"check"]intValue]==1)
  108. {
  109. [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  110. setvalue=true;
  111. break;
  112. }
  113. }
  114. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
  115. {
  116. self.cancommit = false;
  117. }
  118. }
  119. else
  120. {
  121. NSMutableArray* checked = [[NSMutableArray alloc] init];
  122. NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  123. int count = [[cadejson valueForKey:@"count"] intValue];
  124. // bool setvalue = false;
  125. for(int cc=0;cc<count;cc++)
  126. {
  127. NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  128. if([[valjson valueForKey:@"check"]intValue]==1)
  129. {
  130. [checked addObject:[NSNumber numberWithInt:[[valjson valueForKey:@"value_id"] intValue]]];
  131. // [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  132. // setvalue=true;
  133. // break;
  134. }
  135. }
  136. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"]&&checked.count==0)
  137. {
  138. self.cancommit = false;
  139. }
  140. else
  141. {
  142. NSString * string = [checked componentsJoinedByString:@","];
  143. [upparams setValue:string forKey:[itemjson valueForKey:@"name"]];
  144. }
  145. }
  146. /*
  147. upparams setValue:itemjson forKey:<#(NSString *)#>
  148. */
  149. }else
  150. if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  151. {
  152. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  153. self.cancommit = false;
  154. }
  155. else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  156. {
  157. NSString* valuestr = [itemjson valueForKey:@"value"];
  158. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  159. NSString* value = [itemjson valueForKey:@"value"];
  160. NSMutableDictionary* boolitem=nil;
  161. NSMutableDictionary* subjson=nil;
  162. if([value isEqualToString:@"true"])
  163. {
  164. boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  165. }
  166. else
  167. {
  168. boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  169. }
  170. subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  171. if( subjson!=nil /*&& !active*/)
  172. {
  173. int sub_count = [[subjson valueForKey:@"count"] intValue];
  174. for(int l=0;l<sub_count;l++)
  175. {
  176. NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  177. if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  178. [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  179. else
  180. {
  181. if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  182. self.cancommit = false;
  183. }
  184. }
  185. }
  186. }
  187. else
  188. if([[itemjson valueForKey:@"control"] isEqualToString:@"img"])
  189. {
  190. if([itemjson valueForKey:@"avalue"]!=nil && ![[itemjson valueForKey:@"avalue"]isEqualToString:@""])
  191. [upparams setValue:[itemjson valueForKey:@"avalue"] forKey:[itemjson valueForKey:@"name"]];
  192. else
  193. {
  194. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  195. self.cancommit = false;
  196. }
  197. }
  198. else
  199. {
  200. if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  201. [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  202. else
  203. {
  204. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  205. self.cancommit = false;
  206. }
  207. }
  208. upparams= [self subitem_param:itemjson addto:upparams ];
  209. }
  210. }
  211. if(self.cancommit==false)
  212. {
  213. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  214. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  215. //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  216. //[self.editorTable reloadData ];
  217. [self.editorTable reloadData];
  218. return;
  219. }
  220. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Saving Contact"];
  221. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  222. NSDictionary* editor_json = nil;
  223. // NSString* contactId=[upparams valueForKey:@"contactId"];
  224. if(self.bnewcustomer )
  225. editor_json=[iSalesNetwork save_newCustomer:upparams];
  226. else
  227. editor_json=[iSalesNetwork update_Customer:upparams];
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  230. if([[editor_json valueForKey:@"result"] intValue]==2)
  231. {
  232. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  233. //
  234. //
  235. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  236. // appDelegate.customerInfo = customerInfo;
  237. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  238. // // self.labelContact.text=appDelegate.contact_name;
  239. //
  240. // NSDictionary * customerinfo = appDelegate.customerInfo;
  241. [self prepareReturn:nil];
  242. if(self.bnewcustomer )
  243. [self.navigationController popViewControllerAnimated:false];
  244. else
  245. [self.navigationController popToRootViewControllerAnimated:false];
  246. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  247. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  248. }
  249. else
  250. {
  251. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:self] ;
  252. }
  253. });
  254. });
  255. }
  256. - (void)didReceiveMemoryWarning {
  257. [super didReceiveMemoryWarning];
  258. // Dispose of any resources that can be recreated.
  259. }
  260. /*
  261. #pragma mark - Navigation
  262. // In a storyboard-based application, you will often want to do a little preparation before navigation
  263. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  264. // Get the new view controller using [segue destinationViewController].
  265. // Pass the selected object to the new view controller.
  266. }
  267. */
  268. @end