CreateOrderViewController.m 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. //
  2. // CreateOrderViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 8/24/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CreateOrderViewController.h"
  9. #import "iSalesNetwork.h"
  10. #import "MainViewController.h"
  11. #define SUBMIT_CONFIRM 123
  12. #define KEEP_TAIL 456
  13. #define CLOSE_ACTION 567
  14. @interface CreateOrderViewController ()
  15. // static const char associatedkey;
  16. @end
  17. @implementation CreateOrderViewController
  18. -(void) viewWillAppear:(BOOL)animated
  19. {
  20. [super viewWillAppear:animated];
  21. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  22. if(appDelegate.can_submit_order)
  23. {
  24. [self.btnSubmitOrder setImage:[UIImage imageNamed:@"commit"]];
  25. self.btnSubmitOrder.enabled = true;
  26. }
  27. else
  28. {
  29. [self.btnSubmitOrder setImage:nil];
  30. self.btnSubmitOrder.enabled = false;
  31. }
  32. self.navigationController.navigationBarHidden=false;
  33. }
  34. -(void) download_success
  35. {
  36. // 用于处理不同child,在成功去的数据后,需要附加的操作。
  37. self.have_tail=[[self.content_data_download valueForKey:@"has_reItem"] boolValue];
  38. NSString* order_customerid=[self getValue:@"customer_cid"];
  39. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  40. appDelegate.order_customer_id = order_customerid;
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  45. style:UIBarButtonItemStylePlain
  46. target:self
  47. action:@selector(onCloseClick:)];
  48. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  49. // closeButton.title = @"Close";
  50. self.navigationItem.leftBarButtonItem = closeButton;
  51. NSMutableArray * items = [[NSMutableArray alloc]init];
  52. UIBarButtonItem *savebtn = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)];
  53. savebtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  54. style:UIBarButtonItemStylePlain
  55. target:self
  56. action:@selector(onSaveClick:)];
  57. self.btnSubmitOrder = nil;
  58. self.btnSubmitOrder =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"commit"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  59. style:UIBarButtonItemStylePlain
  60. target:self
  61. action:@selector(onCommitOrderClick:)];
  62. self.btnSubmitOrder.tintColor= UIColorFromRGB(0x996633);
  63. //savebtn setBackgroundImage:(nullable UIImage *) forState:<#(UIControlState)#> style:<#(UIBarButtonItemStyle)#> barMetrics:<#(UIBarMetrics)#>
  64. //[savebtn setImage:[UIImage imageNamed:@"save"] ];
  65. /*
  66. UIBarMetricsDefault,
  67. UIBarMetricsCompact,
  68. UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
  69. UIBarMetricsCompactPrompt
  70. */
  71. // [savebtn setBackgroundImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  72. // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  73. // item1.title = @"item1";
  74. //// item1.image = [UIImage imageNamed:@"rect_setting"];
  75. // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  76. //// item2.image = [UIImage imageNamed:@"rect_about"];
  77. // item2.title = @"item2";
  78. UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  79. fixedItem.width = 20.0f;
  80. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  81. if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  82. {
  83. [items addObject:savebtn];
  84. [items addObject:fixedItem];
  85. }
  86. [items addObject:self.btnSubmitOrder];
  87. // [items addObject:item2];
  88. self.navigationItem.rightBarButtonItems=items;
  89. // self.navigationItem.backBarButtonItem = backButton;
  90. // Do any additional setup after loading the view.
  91. }
  92. - (void)onCommitOrderClick:(id)sender {
  93. // get customer info
  94. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Submit Confirm.", nil) message:NSLocalizedString(@"Are you sure to submit order?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  95. alert.tag = SUBMIT_CONFIRM;
  96. [alert show];
  97. }
  98. - (void)onCloseClick:(UIButton *)sender {
  99. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Choose your action", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Switch to cart", nil),@"Switch to order history",NSLocalizedString(@"Close and release order", nil), nil];
  100. alert.tag = CLOSE_ACTION;
  101. // alert.
  102. [alert show];
  103. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  104. // appDelegate.order_code = nil;
  105. //
  106. // }
  107. }
  108. - (void)didReceiveMemoryWarning {
  109. [super didReceiveMemoryWarning];
  110. // Dispose of any resources that can be recreated.
  111. }
  112. - (IBAction)onSaveClick:(id)sender {
  113. // add params check here.
  114. [self.lastedit endEditing:true];
  115. [self.lasttextview endEditing:true];
  116. self.cancommit=true;
  117. NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
  118. [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
  119. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  120. for(int i=0;i<section_count;i++)
  121. {
  122. NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  123. int item_count = [[sectionjson valueForKey:@"count"] intValue];
  124. for(int j=0;j<item_count;j++)
  125. {
  126. NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  127. NSString* key = [itemjson valueForKey:@"name"];
  128. if(key==nil || key.length==0)
  129. continue;
  130. if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
  131. {
  132. NSString* single_select = [itemjson valueForKey:@"single_select"];
  133. if([single_select isEqualToString:@"true"])
  134. {
  135. NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  136. int count = [[cadejson valueForKey:@"count"] intValue];
  137. bool setvalue = false;
  138. for(int cc=0;cc<count;cc++)
  139. {
  140. NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  141. if([[valjson valueForKey:@"check"]intValue]==1)
  142. {
  143. [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  144. setvalue=true;
  145. break;
  146. }
  147. }
  148. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
  149. {
  150. self.cancommit = false;
  151. }
  152. }
  153. else
  154. {
  155. //not support multi select for now;
  156. }
  157. /*
  158. upparams setValue:itemjson forKey:<#(NSString *)#>
  159. */
  160. }else
  161. if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  162. {
  163. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  164. self.cancommit = false;
  165. }
  166. else if([[itemjson valueForKey:@"control"] isEqualToString:@"signature"])
  167. {
  168. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"value"]==nil)
  169. self.cancommit = false;
  170. NSString* valuestr = [itemjson valueForKey:@"value"];
  171. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  172. }
  173. else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  174. {
  175. NSString* valuestr = [itemjson valueForKey:@"value"];
  176. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  177. NSString* value = [itemjson valueForKey:@"value"];
  178. NSMutableDictionary* boolitem=nil;
  179. NSMutableDictionary* subjson=nil;
  180. if([value isEqualToString:@"true"])
  181. {
  182. boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  183. }
  184. else
  185. {
  186. boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  187. }
  188. subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  189. if( subjson!=nil /*&& !active*/)
  190. {
  191. int sub_count = [[subjson valueForKey:@"count"] intValue];
  192. for(int l=0;l<sub_count;l++)
  193. {
  194. NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  195. if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  196. [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  197. else
  198. {
  199. if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  200. self.cancommit = false;
  201. }
  202. }
  203. }
  204. }
  205. else
  206. {
  207. if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  208. [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  209. else
  210. {
  211. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  212. self.cancommit = false;
  213. }
  214. }
  215. upparams= [self subitem_param:itemjson addto:upparams ];
  216. }
  217. }
  218. // save order 不检查必填项;
  219. self.cancommit=true;
  220. if(self.cancommit==false)
  221. {
  222. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  223. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  224. //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  225. //[self.editorTable reloadData ];
  226. [self.editorTable reloadData];
  227. return;
  228. }
  229. if([upparams[@"shipping"] isEqualToString:@"Shipping To Be Quoted"])
  230. upparams[@"shipping"] = nil;
  231. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Saving Order"];
  232. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  233. NSDictionary* editor_json = [iSalesNetwork save_Order:upparams];
  234. dispatch_async(dispatch_get_main_queue(), ^{
  235. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  236. if([[editor_json valueForKey:@"result"] intValue]==2)
  237. {
  238. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  239. NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  240. appDelegate.customerInfo = customerInfo;
  241. appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  242. [RAUtils message_alert:nil title:@"Order Saved" controller:self] ;
  243. if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.save_order_logout && appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  244. {
  245. [((MainViewController*)appDelegate.main_vc) Loginout];
  246. [self prepareReturn:nil];
  247. [self.navigationController popViewControllerAnimated:true];
  248. }
  249. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  250. // appDelegate.order_code = nil;
  251. //
  252. // }
  253. // // self.labelContact.text=appDelegate.contact_name;
  254. //
  255. // NSDictionary * customerinfo = appDelegate.customerInfo;
  256. // [self prepareReturn:nil];
  257. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  258. //
  259. //
  260. // ;
  261. // }];
  262. }
  263. else
  264. {
  265. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:self] ;
  266. }
  267. });
  268. });
  269. }
  270. - (void)textFieldDidEndEditing:(UITextField *)textField
  271. {
  272. [super textFieldDidEndEditing:textField];
  273. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  274. NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  275. NSMutableDictionary* section_json=nil;
  276. // NSLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  277. NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  278. NSString* name = [item_json valueForKey:@"name"];
  279. if([name isEqualToString:@"shipping"]|| [name isEqualToString:@"handling_fee_value"]||[name isEqualToString:@"lift_gate_value"])
  280. {
  281. float shipping = [self getprice:@"shipping" section:indexPath.section];
  282. float paymentsAndCredits = [self getprice:@"paymentsAndCredits" section:indexPath.section];
  283. float handling_fee_value = [self getprice:@"handling_fee_value" section:indexPath.section];
  284. float lift_gate =[self getprice:@"lift_gate_value" section:indexPath.section];
  285. float totalprice=shipping+paymentsAndCredits+handling_fee_value+lift_gate;
  286. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  287. int count=0;
  288. count=[[section_json valueForKey:@"count"] intValue];
  289. int total_row_idx=-1;
  290. for(int i=0;i<count;i++)
  291. {
  292. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  293. if([[olditem valueForKey:@"name"] isEqualToString:@"totalPrice" ])
  294. {
  295. [olditem setValue:[NSString stringWithFormat:@"%.2f",totalprice] forKey:@"value"];
  296. [olditem setValue:@"true" forKey:@"dirty"];
  297. [section_json setObject:olditem forKey:[NSString stringWithFormat:@"item_%d",i ]];
  298. total_row_idx=i;
  299. }
  300. }
  301. [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  302. self.content_data_control = [self translate_json:self.content_data_download changed: self.changed_data];
  303. if(total_row_idx>=0)
  304. {
  305. NSUInteger newIndex[] = {indexPath.section, total_row_idx};
  306. NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
  307. // NSLog(@"before refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  308. [self.editorTable reloadRowsAtIndexPaths:@[newPath]withRowAnimation:UITableViewRowAnimationMiddle];
  309. // NSLog(@"after refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  310. // CGSize tablecontent =self.editorTable.contentSize;
  311. // tablecontent.height=tablecontent.height+self.keyboard_h;
  312. // self.editorTable.contentSize=tablecontent;
  313. //
  314. //
  315. // NSLog(@"resize to %@",NSStringFromCGSize(self.editorTable.contentSize));
  316. }
  317. // self.content_data = [editor_json mutableCopy];
  318. // self.content_data = [self translate_json:editor_json];
  319. // NSMutableDictionary* content_data1 = [self translate_json:self.content_data];
  320. // [self.editorTable reloadData];
  321. }
  322. //
  323. // [item_json setValue:@"true" forKey:@"dirty"];
  324. // [item_json setValue:textField.text forKey:@"value"];
  325. }
  326. -(float) getprice:(NSString*) name section :(long) index
  327. {
  328. NSMutableDictionary* section_json=nil;
  329. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",index ]] mutableCopy];
  330. int count=0;
  331. count=[[section_json valueForKey:@"count"] intValue];
  332. for(int i=0;i<count;i++)
  333. {
  334. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  335. if([[olditem valueForKey:@"name"] isEqualToString:name])
  336. {
  337. return [[olditem valueForKey:@"value"] floatValue];
  338. }
  339. }
  340. return 0;
  341. }
  342. /*
  343. #pragma mark - Navigation
  344. // In a storyboard-based application, you will often want to do a little preparation before navigation
  345. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  346. // Get the new view controller using [segue destinationViewController].
  347. // Pass the selected object to the new view controller.
  348. }
  349. */
  350. #pragma mark - UIAlertViewDelegate
  351. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  352. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  353. {
  354. if(buttonIndex!=alertView.cancelButtonIndex && alertView.tag == SUBMIT_CONFIRM)
  355. {
  356. // add params check here.
  357. [self.lastedit endEditing:true];
  358. [self.lasttextview endEditing:true];
  359. self.cancommit=true;
  360. NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
  361. [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
  362. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  363. for(int i=0;i<section_count;i++)
  364. {
  365. NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  366. int item_count = [[sectionjson valueForKey:@"count"] intValue];
  367. for(int j=0;j<item_count;j++)
  368. {
  369. NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  370. NSString* key = [itemjson valueForKey:@"name"];
  371. if(key==nil || key.length==0)
  372. continue;
  373. if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
  374. {
  375. NSString* single_select = [itemjson valueForKey:@"single_select"];
  376. if([single_select isEqualToString:@"true"])
  377. {
  378. NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  379. int count = [[cadejson valueForKey:@"count"] intValue];
  380. bool setvalue = false;
  381. for(int cc=0;cc<count;cc++)
  382. {
  383. NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  384. if([[valjson valueForKey:@"check"]intValue]==1)
  385. {
  386. [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  387. setvalue=true;
  388. break;
  389. }
  390. }
  391. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
  392. {
  393. self.cancommit = false;
  394. }
  395. }
  396. else
  397. {
  398. //not support multi select for now;
  399. }
  400. /*
  401. upparams setValue:itemjson forKey:<#(NSString *)#>
  402. */
  403. }else
  404. if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  405. {
  406. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  407. self.cancommit = false;
  408. }
  409. else if([[itemjson valueForKey:@"control"] isEqualToString:@"signature"])
  410. {
  411. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"value"] ==nil)
  412. self.cancommit = false;
  413. NSString* valuestr = [itemjson valueForKey:@"value"];
  414. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  415. }
  416. else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  417. {
  418. NSString* valuestr = [itemjson valueForKey:@"value"];
  419. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  420. NSString* value = [itemjson valueForKey:@"value"];
  421. NSMutableDictionary* boolitem=nil;
  422. NSMutableDictionary* subjson=nil;
  423. if([value isEqualToString:@"true"])
  424. {
  425. boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  426. }
  427. else
  428. {
  429. boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  430. }
  431. subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  432. if( subjson!=nil /*&& !active*/)
  433. {
  434. int sub_count = [[subjson valueForKey:@"count"] intValue];
  435. for(int l=0;l<sub_count;l++)
  436. {
  437. NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  438. if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  439. [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  440. else
  441. {
  442. if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  443. self.cancommit = false;
  444. }
  445. }
  446. }
  447. }
  448. else
  449. {
  450. if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  451. [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  452. else
  453. {
  454. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  455. self.cancommit = false;
  456. }
  457. }
  458. upparams= [self subitem_param:itemjson addto:upparams ];
  459. }
  460. }
  461. if(self.cancommit==false)
  462. {
  463. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  464. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  465. //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  466. //[self.editorTable reloadData ];
  467. [self.editorTable reloadData];
  468. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  469. return;
  470. }
  471. if(upparams[@"shipping"]==nil)
  472. goto commit_order;
  473. if([upparams[@"shipping"] isEqualToString:@"Shipping To Be Quoted"])
  474. {
  475. upparams[@"shipping"] = nil;
  476. goto commit_order;
  477. }
  478. else if([upparams[@"shipping"] doubleValue]==0)
  479. {
  480. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Shipping is $0.00, continue submit?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  481. //block代码块取代了delegate
  482. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
  483. {
  484. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  485. if(appDelegate.user_type ==USER_ROLE_EMPLOYEE )
  486. {
  487. if(/*!self.have_tail*/ true)
  488. {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  489. [upparams setValue:@"false" forKey:@"isHold"];
  490. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  491. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  492. dispatch_async(dispatch_get_main_queue(), ^{
  493. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  494. if([[editor_json valueForKey:@"result"] intValue]==2)
  495. {
  496. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  497. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  498. // appDelegate.customerInfo = customerInfo;
  499. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  500. // appDelegate.order_code = nil;
  501. [appDelegate closeOrder];
  502. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  503. if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  504. {
  505. [((MainViewController*)appDelegate.main_vc) Loginout];
  506. }
  507. // // self.labelContact.text=appDelegate.contact_name;
  508. //
  509. // NSDictionary * customerinfo = appDelegate.customerInfo;
  510. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  511. [alert show];
  512. [self prepareReturn:nil];
  513. [self.navigationController popViewControllerAnimated:true];
  514. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  515. //
  516. //
  517. // ;
  518. // }];
  519. }
  520. else
  521. {
  522. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  523. }
  524. });
  525. });
  526. }
  527. else
  528. {
  529. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Retain models", nil) message:NSLocalizedString(@"There are some models left in cart,do you want to keep them?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  530. alert.tag = KEEP_TAIL;
  531. self.upparams = upparams;
  532. [alert show];
  533. }
  534. }
  535. else
  536. { //USER_ROLE_CUSTOMER
  537. // if(self.have_tail)
  538. // [upparams setValue:@"true" forKey:@"isHold"];
  539. // else
  540. // [upparams setValue:@"false" forKey:@"isHold"];
  541. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  542. [upparams setValue:@"true" forKey:@"isHold"];
  543. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  544. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  545. dispatch_async(dispatch_get_main_queue(), ^{
  546. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  547. if([[editor_json valueForKey:@"result"] intValue]==2)
  548. {
  549. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  550. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  551. // appDelegate.customerInfo = customerInfo;
  552. NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  553. appDelegate.order_code = orderCode;
  554. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  555. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  556. [alert show];
  557. // // self.labelContact.text=appDelegate.contact_name;
  558. //
  559. // NSDictionary * customerinfo = appDelegate.customerInfo;
  560. [self prepareReturn:nil];
  561. [self.navigationController popViewControllerAnimated:true];
  562. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  563. //
  564. //
  565. // ;
  566. // }];
  567. }
  568. else
  569. {
  570. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  571. }
  572. });
  573. });
  574. }
  575. }];
  576. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  577. //NSLog(@"Cancel");
  578. }];
  579. [alertControl addAction:actionOne];
  580. [alertControl addAction:alertthree];
  581. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  582. [self presentViewController:alertControl animated:YES completion:nil];
  583. }
  584. else
  585. {
  586. commit_order:
  587. {
  588. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  589. if(appDelegate.user_type ==USER_ROLE_EMPLOYEE )
  590. {
  591. if(/*!self.have_tail*/ true)
  592. {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  593. [upparams setValue:@"false" forKey:@"isHold"];
  594. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  595. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  596. dispatch_async(dispatch_get_main_queue(), ^{
  597. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  598. if([[editor_json valueForKey:@"result"] intValue]==2)
  599. {
  600. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  601. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  602. // appDelegate.customerInfo = customerInfo;
  603. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  604. // appDelegate.order_code = nil;
  605. [appDelegate closeOrder];
  606. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  607. if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  608. {
  609. [((MainViewController*)appDelegate.main_vc) Loginout];
  610. }
  611. // // self.labelContact.text=appDelegate.contact_name;
  612. //
  613. // NSDictionary * customerinfo = appDelegate.customerInfo;
  614. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  615. [alert show];
  616. [self prepareReturn:nil];
  617. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  618. //
  619. //
  620. // ;
  621. // }];
  622. [self.navigationController popViewControllerAnimated:true];
  623. }
  624. else
  625. {
  626. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  627. }
  628. });
  629. });
  630. }
  631. else
  632. {
  633. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Retain models", nil) message:NSLocalizedString(@"There are some models left in cart,do you want to keep them?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  634. alert.tag = KEEP_TAIL;
  635. self.upparams = upparams;
  636. [alert show];
  637. }
  638. }
  639. else
  640. { //USER_ROLE_CUSTOMER
  641. // if(self.have_tail)
  642. // [upparams setValue:@"true" forKey:@"isHold"];
  643. // else
  644. // [upparams setValue:@"false" forKey:@"isHold"];
  645. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  646. [upparams setValue:@"true" forKey:@"isHold"];
  647. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  648. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  649. dispatch_async(dispatch_get_main_queue(), ^{
  650. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  651. if([[editor_json valueForKey:@"result"] intValue]==2)
  652. {
  653. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  654. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  655. // appDelegate.customerInfo = customerInfo;
  656. NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  657. appDelegate.order_code = orderCode;
  658. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  659. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  660. [alert show];
  661. // // self.labelContact.text=appDelegate.contact_name;
  662. //
  663. // NSDictionary * customerinfo = appDelegate.customerInfo;
  664. [self prepareReturn:nil];
  665. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  666. //
  667. //
  668. // ;
  669. // }];
  670. [self.navigationController popViewControllerAnimated:true];
  671. }
  672. else
  673. {
  674. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  675. }
  676. });
  677. });
  678. }
  679. }
  680. }
  681. }
  682. // else if( alertView.tag == KEEP_TAIL)
  683. // {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  684. // if(buttonIndex!=alertView.cancelButtonIndex)
  685. // {//keep tail;
  686. //
  687. // [self.upparams setValue:@"true" forKey:@"isHold"];
  688. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  689. //
  690. // NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:self.upparams];
  691. //
  692. // dispatch_async(dispatch_get_main_queue(), ^{
  693. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  694. //
  695. //
  696. // if([[editor_json valueForKey:@"result"] intValue]==2)
  697. // {
  698. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  699. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  700. //
  701. // [alert show];
  702. //
  703. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  704. // appDelegate.customerInfo = customerInfo;
  705. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  706. // NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  707. // appDelegate.order_code = orderCode;
  708. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  709. // // // self.labelContact.text=appDelegate.contact_name;
  710. // //
  711. // // NSDictionary * customerinfo = appDelegate.customerInfo;
  712. // [self prepareReturn:nil];
  713. //// [self.navigationController dismissViewControllerAnimated:true completion:^{
  714. ////
  715. ////
  716. //// ;
  717. //// }];
  718. // [self.navigationController popViewControllerAnimated:true];
  719. //
  720. // }
  721. // else
  722. // {
  723. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  724. // }
  725. //
  726. //
  727. //
  728. //
  729. // });
  730. // });
  731. // }
  732. // else
  733. // {// not keep tail;
  734. // [self.upparams setValue:@"false" forKey:@"isHold"];
  735. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  736. //
  737. // NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:self.upparams];
  738. //
  739. // dispatch_async(dispatch_get_main_queue(), ^{
  740. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  741. //
  742. //
  743. // if([[editor_json valueForKey:@"result"] intValue]==2)
  744. // {
  745. // // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  746. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  747. //
  748. // [alert show];
  749. //
  750. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  751. // // appDelegate.customerInfo = customerInfo;
  752. // // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  753. // // appDelegate.order_code = nil;
  754. // [appDelegate closeOrder];
  755. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  756. // // // self.labelContact.text=appDelegate.contact_name;
  757. // //
  758. // // NSDictionary * customerinfo = appDelegate.customerInfo;
  759. // [self prepareReturn:nil];
  760. //// [self.navigationController dismissViewControllerAnimated:true completion:^{
  761. ////
  762. ////
  763. //// ;
  764. //// }];
  765. // [self.navigationController popViewControllerAnimated:true];
  766. //
  767. // }
  768. // else
  769. // {
  770. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  771. // }
  772. //
  773. //
  774. //
  775. //
  776. // });
  777. // });
  778. // }
  779. // }
  780. else if(alertView.tag == CLOSE_ACTION)
  781. {
  782. if(buttonIndex==1)
  783. {
  784. [self.navigationController popViewControllerAnimated:true];
  785. // back to cart;
  786. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  787. // ;
  788. // }];
  789. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  790. // appDelegate.order_code = nil;
  791. //
  792. // }
  793. }
  794. if(buttonIndex==2)
  795. {
  796. [self.navigationController popViewControllerAnimated:true];
  797. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  798. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  799. [((MainViewController*)appDelegate.main_vc) switchToOrder];
  800. // back to cart;
  801. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  802. // ;
  803. // }];
  804. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  805. // appDelegate.order_code = nil;
  806. //
  807. // }
  808. }
  809. else if(buttonIndex==3)
  810. {
  811. // close order;
  812. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  813. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Release Order"];
  814. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  815. NSDictionary* order_json = [iSalesNetwork release_Order:appDelegate.order_code];
  816. dispatch_async(dispatch_get_main_queue(), ^{
  817. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  818. if([[order_json valueForKey:@"result"] intValue]==2)
  819. {
  820. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  821. // appDelegate.order_code = nil;
  822. [appDelegate closeOrder];
  823. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  824. [((MainViewController*)appDelegate.main_vc) switchToHome];
  825. if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  826. {
  827. [((MainViewController*)appDelegate.main_vc) Loginout];
  828. }
  829. [self prepareReturn:nil];
  830. [self.navigationController popViewControllerAnimated:true];
  831. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  832. // ;
  833. // }];
  834. }
  835. else
  836. {
  837. [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  838. }
  839. });
  840. });
  841. //
  842. // }
  843. }
  844. }
  845. }
  846. @end