CreateOrderViewController.m 50 KB

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