CreateOrderViewController.m 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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:@"switch"])
  167. {
  168. NSString* valuestr = [itemjson valueForKey:@"value"];
  169. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  170. NSString* value = [itemjson valueForKey:@"value"];
  171. NSMutableDictionary* boolitem=nil;
  172. NSMutableDictionary* subjson=nil;
  173. if([value isEqualToString:@"true"])
  174. {
  175. boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  176. }
  177. else
  178. {
  179. boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  180. }
  181. subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  182. if( subjson!=nil /*&& !active*/)
  183. {
  184. int sub_count = [[subjson valueForKey:@"count"] intValue];
  185. for(int l=0;l<sub_count;l++)
  186. {
  187. NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  188. if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  189. [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  190. else
  191. {
  192. if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  193. self.cancommit = false;
  194. }
  195. }
  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. // save order 不检查必填项;
  212. self.cancommit=true;
  213. if(self.cancommit==false)
  214. {
  215. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  216. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  217. //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  218. //[self.editorTable reloadData ];
  219. [self.editorTable reloadData];
  220. return;
  221. }
  222. if([upparams[@"shipping"] isEqualToString:@"Shipping To Be Quoted"])
  223. upparams[@"shipping"] = nil;
  224. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Saving Order"];
  225. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  226. NSDictionary* editor_json = [iSalesNetwork save_Order:upparams];
  227. dispatch_async(dispatch_get_main_queue(), ^{
  228. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  229. if([[editor_json valueForKey:@"result"] intValue]==2)
  230. {
  231. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  232. NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  233. appDelegate.customerInfo = customerInfo;
  234. appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  235. [RAUtils message_alert:nil title:@"Order Saved" controller:self] ;
  236. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  237. // appDelegate.order_code = nil;
  238. //
  239. // }
  240. // // self.labelContact.text=appDelegate.contact_name;
  241. //
  242. // NSDictionary * customerinfo = appDelegate.customerInfo;
  243. // [self prepareReturn:nil];
  244. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  245. //
  246. //
  247. // ;
  248. // }];
  249. }
  250. else
  251. {
  252. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:self] ;
  253. }
  254. });
  255. });
  256. }
  257. - (void)textFieldDidEndEditing:(UITextField *)textField
  258. {
  259. [super textFieldDidEndEditing:textField];
  260. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  261. NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  262. NSMutableDictionary* section_json=nil;
  263. // NSLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  264. NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  265. NSString* name = [item_json valueForKey:@"name"];
  266. if([name isEqualToString:@"shipping"]|| [name isEqualToString:@"handling_fee_value"]||[name isEqualToString:@"lift_gate_value"])
  267. {
  268. float shipping = [self getprice:@"shipping" section:indexPath.section];
  269. float paymentsAndCredits = [self getprice:@"paymentsAndCredits" section:indexPath.section];
  270. float handling_fee_value = [self getprice:@"handling_fee_value" section:indexPath.section];
  271. float lift_gate =[self getprice:@"lift_gate_value" section:indexPath.section];
  272. float totalprice=shipping+paymentsAndCredits+handling_fee_value+lift_gate;
  273. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  274. int count=0;
  275. count=[[section_json valueForKey:@"count"] intValue];
  276. int total_row_idx=-1;
  277. for(int i=0;i<count;i++)
  278. {
  279. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  280. if([[olditem valueForKey:@"name"] isEqualToString:@"totalPrice" ])
  281. {
  282. [olditem setValue:[NSString stringWithFormat:@"%.2f",totalprice] forKey:@"value"];
  283. [olditem setValue:@"true" forKey:@"dirty"];
  284. [section_json setObject:olditem forKey:[NSString stringWithFormat:@"item_%d",i ]];
  285. total_row_idx=i;
  286. }
  287. }
  288. [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  289. self.content_data_control = [self translate_json:self.content_data_download changed: self.changed_data];
  290. if(total_row_idx>=0)
  291. {
  292. NSUInteger newIndex[] = {indexPath.section, total_row_idx};
  293. NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
  294. // NSLog(@"before refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  295. [self.editorTable reloadRowsAtIndexPaths:@[newPath]withRowAnimation:UITableViewRowAnimationMiddle];
  296. // NSLog(@"after refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  297. // CGSize tablecontent =self.editorTable.contentSize;
  298. // tablecontent.height=tablecontent.height+self.keyboard_h;
  299. // self.editorTable.contentSize=tablecontent;
  300. //
  301. //
  302. // NSLog(@"resize to %@",NSStringFromCGSize(self.editorTable.contentSize));
  303. }
  304. // self.content_data = [editor_json mutableCopy];
  305. // self.content_data = [self translate_json:editor_json];
  306. // NSMutableDictionary* content_data1 = [self translate_json:self.content_data];
  307. // [self.editorTable reloadData];
  308. }
  309. //
  310. // [item_json setValue:@"true" forKey:@"dirty"];
  311. // [item_json setValue:textField.text forKey:@"value"];
  312. }
  313. -(float) getprice:(NSString*) name section :(long) index
  314. {
  315. NSMutableDictionary* section_json=nil;
  316. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",index ]] mutableCopy];
  317. int count=0;
  318. count=[[section_json valueForKey:@"count"] intValue];
  319. for(int i=0;i<count;i++)
  320. {
  321. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  322. if([[olditem valueForKey:@"name"] isEqualToString:name])
  323. {
  324. return [[olditem valueForKey:@"value"] floatValue];
  325. }
  326. }
  327. return 0;
  328. }
  329. /*
  330. #pragma mark - Navigation
  331. // In a storyboard-based application, you will often want to do a little preparation before navigation
  332. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  333. // Get the new view controller using [segue destinationViewController].
  334. // Pass the selected object to the new view controller.
  335. }
  336. */
  337. #pragma mark - UIAlertViewDelegate
  338. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  339. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  340. {
  341. if(buttonIndex!=alertView.cancelButtonIndex && alertView.tag == SUBMIT_CONFIRM)
  342. {
  343. // add params check here.
  344. [self.lastedit endEditing:true];
  345. [self.lasttextview endEditing:true];
  346. self.cancommit=true;
  347. NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
  348. [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
  349. int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  350. for(int i=0;i<section_count;i++)
  351. {
  352. NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  353. int item_count = [[sectionjson valueForKey:@"count"] intValue];
  354. for(int j=0;j<item_count;j++)
  355. {
  356. NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  357. NSString* key = [itemjson valueForKey:@"name"];
  358. if(key==nil || key.length==0)
  359. continue;
  360. if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
  361. {
  362. NSString* single_select = [itemjson valueForKey:@"single_select"];
  363. if([single_select isEqualToString:@"true"])
  364. {
  365. NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
  366. int count = [[cadejson valueForKey:@"count"] intValue];
  367. bool setvalue = false;
  368. for(int cc=0;cc<count;cc++)
  369. {
  370. NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
  371. if([[valjson valueForKey:@"check"]intValue]==1)
  372. {
  373. [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
  374. setvalue=true;
  375. break;
  376. }
  377. }
  378. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
  379. {
  380. self.cancommit = false;
  381. }
  382. }
  383. else
  384. {
  385. //not support multi select for now;
  386. }
  387. /*
  388. upparams setValue:itemjson forKey:<#(NSString *)#>
  389. */
  390. }else
  391. if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
  392. {
  393. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
  394. self.cancommit = false;
  395. }
  396. else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
  397. {
  398. NSString* valuestr = [itemjson valueForKey:@"value"];
  399. [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
  400. NSString* value = [itemjson valueForKey:@"value"];
  401. NSMutableDictionary* boolitem=nil;
  402. NSMutableDictionary* subjson=nil;
  403. if([value isEqualToString:@"true"])
  404. {
  405. boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
  406. }
  407. else
  408. {
  409. boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
  410. }
  411. subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
  412. if( subjson!=nil /*&& !active*/)
  413. {
  414. int sub_count = [[subjson valueForKey:@"count"] intValue];
  415. for(int l=0;l<sub_count;l++)
  416. {
  417. NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
  418. if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
  419. [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
  420. else
  421. {
  422. if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
  423. self.cancommit = false;
  424. }
  425. }
  426. }
  427. }
  428. else
  429. {
  430. if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
  431. [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
  432. else
  433. {
  434. if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
  435. self.cancommit = false;
  436. }
  437. }
  438. upparams= [self subitem_param:itemjson addto:upparams ];
  439. }
  440. }
  441. if(self.cancommit==false)
  442. {
  443. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  444. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  445. //NSLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  446. //[self.editorTable reloadData ];
  447. [self.editorTable reloadData];
  448. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  449. return;
  450. }
  451. if(upparams[@"shipping"]==nil)
  452. goto commit_order;
  453. if([upparams[@"shipping"] isEqualToString:@"Shipping To Be Quoted"])
  454. {
  455. upparams[@"shipping"] = nil;
  456. goto commit_order;
  457. }
  458. else if([upparams[@"shipping"] doubleValue]==0)
  459. {
  460. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Shipping is $0.00, continue submit?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  461. //block代码块取代了delegate
  462. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
  463. {
  464. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  465. if(appDelegate.user_type ==USER_ROLE_EMPLOYEE )
  466. {
  467. if(/*!self.have_tail*/ true)
  468. {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  469. [upparams setValue:@"false" forKey:@"isHold"];
  470. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  471. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  472. dispatch_async(dispatch_get_main_queue(), ^{
  473. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  474. if([[editor_json valueForKey:@"result"] intValue]==2)
  475. {
  476. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  477. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  478. // appDelegate.customerInfo = customerInfo;
  479. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  480. // appDelegate.order_code = nil;
  481. [appDelegate closeOrder];
  482. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  483. // // self.labelContact.text=appDelegate.contact_name;
  484. //
  485. // NSDictionary * customerinfo = appDelegate.customerInfo;
  486. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  487. [alert show];
  488. [self prepareReturn:nil];
  489. [self.navigationController popViewControllerAnimated:true];
  490. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  491. //
  492. //
  493. // ;
  494. // }];
  495. }
  496. else
  497. {
  498. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  499. }
  500. });
  501. });
  502. }
  503. else
  504. {
  505. 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];
  506. alert.tag = KEEP_TAIL;
  507. self.upparams = upparams;
  508. [alert show];
  509. }
  510. }
  511. else
  512. { //USER_ROLE_CUSTOMER
  513. // if(self.have_tail)
  514. // [upparams setValue:@"true" forKey:@"isHold"];
  515. // else
  516. // [upparams setValue:@"false" forKey:@"isHold"];
  517. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  518. [upparams setValue:@"true" forKey:@"isHold"];
  519. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  520. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  521. dispatch_async(dispatch_get_main_queue(), ^{
  522. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  523. if([[editor_json valueForKey:@"result"] intValue]==2)
  524. {
  525. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  526. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  527. // appDelegate.customerInfo = customerInfo;
  528. NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  529. appDelegate.order_code = orderCode;
  530. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  531. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  532. [alert show];
  533. // // self.labelContact.text=appDelegate.contact_name;
  534. //
  535. // NSDictionary * customerinfo = appDelegate.customerInfo;
  536. [self prepareReturn:nil];
  537. [self.navigationController popViewControllerAnimated:true];
  538. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  539. //
  540. //
  541. // ;
  542. // }];
  543. }
  544. else
  545. {
  546. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  547. }
  548. });
  549. });
  550. }
  551. }];
  552. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  553. //NSLog(@"Cancel");
  554. }];
  555. [alertControl addAction:actionOne];
  556. [alertControl addAction:alertthree];
  557. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  558. [self presentViewController:alertControl animated:YES completion:nil];
  559. }
  560. else
  561. {
  562. commit_order:
  563. {
  564. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  565. if(appDelegate.user_type ==USER_ROLE_EMPLOYEE )
  566. {
  567. if(/*!self.have_tail*/ true)
  568. {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  569. [upparams setValue:@"false" forKey:@"isHold"];
  570. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  571. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  572. dispatch_async(dispatch_get_main_queue(), ^{
  573. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  574. if([[editor_json valueForKey:@"result"] intValue]==2)
  575. {
  576. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  577. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  578. // appDelegate.customerInfo = customerInfo;
  579. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  580. // appDelegate.order_code = nil;
  581. [appDelegate closeOrder];
  582. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  583. // // self.labelContact.text=appDelegate.contact_name;
  584. //
  585. // NSDictionary * customerinfo = appDelegate.customerInfo;
  586. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  587. [alert show];
  588. [self prepareReturn:nil];
  589. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  590. //
  591. //
  592. // ;
  593. // }];
  594. [self.navigationController popViewControllerAnimated:true];
  595. }
  596. else
  597. {
  598. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  599. }
  600. });
  601. });
  602. }
  603. else
  604. {
  605. 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];
  606. alert.tag = KEEP_TAIL;
  607. self.upparams = upparams;
  608. [alert show];
  609. }
  610. }
  611. else
  612. { //USER_ROLE_CUSTOMER
  613. // if(self.have_tail)
  614. // [upparams setValue:@"true" forKey:@"isHold"];
  615. // else
  616. // [upparams setValue:@"false" forKey:@"isHold"];
  617. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  618. [upparams setValue:@"true" forKey:@"isHold"];
  619. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  620. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:upparams];
  621. dispatch_async(dispatch_get_main_queue(), ^{
  622. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  623. if([[editor_json valueForKey:@"result"] intValue]==2)
  624. {
  625. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  626. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  627. // appDelegate.customerInfo = customerInfo;
  628. NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  629. appDelegate.order_code = orderCode;
  630. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  631. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  632. [alert show];
  633. // // self.labelContact.text=appDelegate.contact_name;
  634. //
  635. // NSDictionary * customerinfo = appDelegate.customerInfo;
  636. [self prepareReturn:nil];
  637. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  638. //
  639. //
  640. // ;
  641. // }];
  642. [self.navigationController popViewControllerAnimated:true];
  643. }
  644. else
  645. {
  646. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  647. }
  648. });
  649. });
  650. }
  651. }
  652. }
  653. }
  654. else if( alertView.tag == KEEP_TAIL)
  655. {UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Submit Order"];
  656. if(buttonIndex!=alertView.cancelButtonIndex)
  657. {//keep tail;
  658. [self.upparams setValue:@"true" forKey:@"isHold"];
  659. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  660. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:self.upparams];
  661. dispatch_async(dispatch_get_main_queue(), ^{
  662. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  663. if([[editor_json valueForKey:@"result"] intValue]==2)
  664. {
  665. NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  666. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  667. [alert show];
  668. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  669. appDelegate.customerInfo = customerInfo;
  670. appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  671. NSString* orderCode = [editor_json valueForKey:@"orderCode"];
  672. appDelegate.order_code = orderCode;
  673. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  674. // // self.labelContact.text=appDelegate.contact_name;
  675. //
  676. // NSDictionary * customerinfo = appDelegate.customerInfo;
  677. [self prepareReturn:nil];
  678. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  679. //
  680. //
  681. // ;
  682. // }];
  683. [self.navigationController popViewControllerAnimated:true];
  684. }
  685. else
  686. {
  687. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  688. }
  689. });
  690. });
  691. }
  692. else
  693. {// not keep tail;
  694. [self.upparams setValue:@"false" forKey:@"isHold"];
  695. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  696. NSDictionary* editor_json = [iSalesNetwork saveandcommit_Order:self.upparams];
  697. dispatch_async(dispatch_get_main_queue(), ^{
  698. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  699. if([[editor_json valueForKey:@"result"] intValue]==2)
  700. {
  701. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  702. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Submit successful." message:[NSString stringWithFormat:@"SO#: %@",[editor_json valueForKey:@"so#"]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  703. [alert show];
  704. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  705. // appDelegate.customerInfo = customerInfo;
  706. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  707. // appDelegate.order_code = nil;
  708. [appDelegate closeOrder];
  709. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  710. // // self.labelContact.text=appDelegate.contact_name;
  711. //
  712. // NSDictionary * customerinfo = appDelegate.customerInfo;
  713. [self prepareReturn:nil];
  714. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  715. //
  716. //
  717. // ;
  718. // }];
  719. [self.navigationController popViewControllerAnimated:true];
  720. }
  721. else
  722. {
  723. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Submit Order" controller:self] ;
  724. }
  725. });
  726. });
  727. }
  728. }
  729. else if(alertView.tag == CLOSE_ACTION)
  730. {
  731. if(buttonIndex==1)
  732. {
  733. [self.navigationController popViewControllerAnimated:true];
  734. // back to cart;
  735. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  736. // ;
  737. // }];
  738. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  739. // appDelegate.order_code = nil;
  740. //
  741. // }
  742. }
  743. if(buttonIndex==2)
  744. {
  745. [self.navigationController popViewControllerAnimated:true];
  746. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  747. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  748. [((MainViewController*)appDelegate.main_vc) switchToOrder];
  749. // back to cart;
  750. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  751. // ;
  752. // }];
  753. // if ([[upparams valueForKey:@"close_on_save"] boolValue]) {
  754. // appDelegate.order_code = nil;
  755. //
  756. // }
  757. }
  758. else if(buttonIndex==3)
  759. {
  760. // close order;
  761. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  762. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Release Order"];
  763. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  764. NSDictionary* order_json = [iSalesNetwork release_Order:appDelegate.order_code];
  765. dispatch_async(dispatch_get_main_queue(), ^{
  766. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  767. if([[order_json valueForKey:@"result"] intValue]==2)
  768. {
  769. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  770. // appDelegate.order_code = nil;
  771. [appDelegate closeOrder];
  772. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  773. [self prepareReturn:nil];
  774. [self.navigationController popViewControllerAnimated:true];
  775. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  776. // ;
  777. // }];
  778. }
  779. else
  780. {
  781. [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  782. }
  783. });
  784. });
  785. //
  786. // }
  787. }
  788. }
  789. }
  790. @end