CustomerEditViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //
  122. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  123. // NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  124. // int count = [[cadejson valueForKey:@"count"] intValue];
  125. // // bool setvalue = false;
  126. // for(int cc=0;cc<count;cc++)
  127. // {
  128. // NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  129. // if([[valjson valueForKey:@"check"]intValue]==1)
  130. // {
  131. //
  132. // [checked addObject:[NSNumber numberWithInt:[[valjson valueForKey:@"value_id"] intValue]]];
  133. // // [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  134. // // setvalue=true;
  135. // // break;
  136. // }
  137. // }
  138. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"]&&checked.count==0)
  139. // {
  140. // self.cancommit = false;
  141. // }
  142. // else
  143. // {
  144. // NSString * string = [checked componentsJoinedByString:@","];
  145. // [upparams setValue:string forKey:[itemjson valueForKey:@"name"]];
  146. // }
  147. // }
  148. // /*
  149. // upparams setValue:itemjson forKey:<#(NSString *)#>
  150. //
  151. // */
  152. // }else
  153. // if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  154. // {
  155. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  156. // self.cancommit = false;
  157. // }
  158. // else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  159. // {
  160. // NSString* valuestr = [itemjson valueForKey:@"value"];
  161. // [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  162. //
  163. // NSString* value = [itemjson valueForKey:@"value"];
  164. // NSMutableDictionary* boolitem=nil;
  165. // NSMutableDictionary* subjson=nil;
  166. // if([value isEqualToString:@"true"])
  167. // {
  168. // boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  169. //
  170. // }
  171. // else
  172. // {
  173. // boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  174. //
  175. // }
  176. // subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  177. // if( subjson!=nil /*&& !active*/)
  178. // {
  179. // int sub_count = [[subjson valueForKey:@"count"] intValue];
  180. // for(int l=0;l<sub_count;l++)
  181. // {
  182. // NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  183. //
  184. //
  185. //
  186. //
  187. // if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  188. // [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  189. // else
  190. // {
  191. // if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  192. // self.cancommit = false;
  193. // }
  194. // }
  195. //
  196. //
  197. //
  198. // }
  199. // }
  200. // else
  201. // if([[itemjson valueForKey:@"control"] isEqualToString:@"img"])
  202. // {
  203. // if([itemjson valueForKey:@"avalue"]!=nil && ![[itemjson valueForKey:@"avalue"]isEqualToString:@""])
  204. // [upparams setValue:[itemjson valueForKey:@"avalue"] forKey:[itemjson valueForKey:@"name"]];
  205. // else
  206. // {
  207. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  208. // self.cancommit = false;
  209. // }
  210. // }
  211. // else
  212. // {
  213. // if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  214. // [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  215. // else
  216. // {
  217. // if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  218. // self.cancommit = false;
  219. // }
  220. // }
  221. // upparams= [self subitem_param:itemjson addto:upparams ];
  222. // }
  223. // }
  224. // if(self.cancommit==false)
  225. // {
  226. // [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  227. // self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  228. // //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  229. // //[self.editorTable reloadData ];
  230. // [self.editorTable reloadData];
  231. // return;
  232. // }
  233. NSMutableDictionary* upparams = [self check_cancommit:true];
  234. if(self.cancommit==false)
  235. return;
  236. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Saving Contact"];
  237. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  238. NSDictionary* editor_json = nil;
  239. // NSString* contactId=[upparams valueForKey:@"contactId"];
  240. if(self.bnewcustomer )
  241. editor_json=[iSalesNetwork save_newCustomer:upparams];
  242. else
  243. editor_json=[iSalesNetwork update_Customer:upparams];
  244. dispatch_async(dispatch_get_main_queue(), ^{
  245. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  246. if([[editor_json valueForKey:@"result"] intValue]==2)
  247. {
  248. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  249. //
  250. //
  251. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  252. // appDelegate.customerInfo = customerInfo;
  253. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  254. // // self.labelContact.text=appDelegate.contact_name;
  255. //
  256. // NSDictionary * customerinfo = appDelegate.customerInfo;
  257. [self prepareReturn:nil];
  258. if(self.bnewcustomer )
  259. [self.navigationController popViewControllerAnimated:false];
  260. else
  261. [self.navigationController popToRootViewControllerAnimated:false];
  262. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  263. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  264. }
  265. else
  266. {
  267. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:self] ;
  268. }
  269. });
  270. });
  271. }
  272. - (void)didReceiveMemoryWarning {
  273. [super didReceiveMemoryWarning];
  274. // Dispose of any resources that can be recreated.
  275. }
  276. /*
  277. #pragma mark - Navigation
  278. // In a storyboard-based application, you will often want to do a little preparation before navigation
  279. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  280. // Get the new view controller using [segue destinationViewController].
  281. // Pass the selected object to the new view controller.
  282. }
  283. */
  284. @end