RAOrderEditorViewController.m 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. //
  2. // RAOrderEditorViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2017/11/7.
  6. // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAOrderEditorViewController.h"
  9. #import "AppDelegate.h"
  10. #import "MainViewController.h"
  11. #import "AddressEditorViewController.h"
  12. #import "CreditCardEditorViewController.h"
  13. #import "JKTimerManager.h"
  14. #import "RAOrderPreviewController.h"
  15. #import "CommonEditorCellModel.h"
  16. //#define CLOSE_ACTION 567
  17. static const int totalPage = 3;
  18. @interface RAOrderEditorViewController ()
  19. @property (nonatomic,assign) BOOL isResume;///<恢复
  20. @property (nonatomic,assign) NSInteger userType;///<恢复使用
  21. @property (nonatomic,assign) NSInteger pageIndex;///<当前页码
  22. /**
  23. * 保存不同页码上行参数,最新的在最后。
  24. * 点击下一页时将当前页面上行参数写入数组尾
  25. * 退回到前一页后删除最后一个元素
  26. */
  27. @property (nonatomic,strong) NSMutableArray *pageUpParams;
  28. @property (nonatomic,strong) NSMutableArray *pageContentDownloadArray;///<保存不同页码content_data_download
  29. @property (nonatomic,strong) NSMutableArray *pageContentControlArray;///<保存不同页码content_data_control
  30. @property (nonatomic,strong) NSMutableArray *pageChangeDataArray;///<保存不同页码change_data
  31. @property (strong, nonatomic) IBOutlet UIToolbar *pageControlToolBar;
  32. @property (strong, nonatomic) IBOutlet UIBarButtonItem *frontBarItem;
  33. @property (strong, nonatomic) IBOutlet UIBarButtonItem *nextBarItem;
  34. @end
  35. @implementation RAOrderEditorViewController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39. self.editable = YES;
  40. self.navigationController.navigationBarHidden = NO;
  41. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  42. style:UIBarButtonItemStylePlain
  43. target:self
  44. action:@selector(onCloseClick:)];
  45. self.navigationItem.leftBarButtonItem = closeButton;
  46. // UIBarButtonItem *savebtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  47. // style:UIBarButtonItemStylePlain
  48. // target:self
  49. // action:@selector(onSaveClick:)];
  50. //
  51. // self.navigationItem.rightBarButtonItem = savebtn;
  52. [self setupPageControlBar];
  53. }
  54. -(void) viewWillAppear:(BOOL)animated
  55. {
  56. [super viewWillAppear:animated];
  57. [self start_urgency_timer];
  58. self.isResume = [[[NSUserDefaults standardUserDefaults] objectForKey:@"urgency_resume"] boolValue];
  59. self.userType = [[[NSUserDefaults standardUserDefaults] objectForKey:@"urgency_userType"] integerValue];
  60. if (!self.isResume) {
  61. // 正常情况
  62. } else {
  63. // 恢复
  64. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"urgency_resume"];
  65. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"urgency_userType"];
  66. }
  67. self.navigationController.navigationBarHidden=false;
  68. }
  69. - (void)didReceiveMemoryWarning {
  70. [super didReceiveMemoryWarning];
  71. // Dispose of any resources that can be recreated.
  72. }
  73. - (void)dealloc {
  74. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  75. [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]];
  76. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"];
  77. if (self.isResume && self.userType == USER_ROLE_CUSTOMER) {
  78. [self releaseOrderAfterSave];
  79. }
  80. }
  81. - (void)awakeFromNib {
  82. [super awakeFromNib];
  83. self.navigationItem.rightBarButtonItem = nil;
  84. }
  85. #pragma mark - Lazy Load
  86. - (NSMutableArray *)pageUpParams {
  87. if (!_pageUpParams) {
  88. _pageUpParams = [NSMutableArray array];
  89. }
  90. return _pageUpParams;
  91. }
  92. - (NSMutableArray *)pageContentDownloadArray {
  93. if (!_pageContentDownloadArray) {
  94. _pageContentDownloadArray = [NSMutableArray array];
  95. }
  96. return _pageContentDownloadArray;
  97. }
  98. - (NSMutableArray *)pageContentControlArray {
  99. if (!_pageContentControlArray) {
  100. _pageContentControlArray = [NSMutableArray array];
  101. }
  102. return _pageContentControlArray;
  103. }
  104. - (NSMutableArray *)pageChangeDataArray {
  105. if (!_pageChangeDataArray) {
  106. _pageChangeDataArray = [NSMutableArray array];
  107. }
  108. return _pageChangeDataArray;
  109. }
  110. #pragma mark - Private
  111. - (void)onCloseClick:(UIButton *)sender {
  112. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  113. if (self.resumeOrder) {
  114. [appDelegate.main_vc.navigationController popViewControllerAnimated:YES];
  115. return;
  116. }
  117. // UIAlertView * alert = nil;
  118. NSString* a2title = nil;
  119. if(appDelegate.order_status==0)
  120. a2title= @"Close and cancel order";
  121. else
  122. a2title=@"Close and release order";
  123. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Choose your action" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  124. UIAlertAction *action_0 = [UIAlertAction actionWithTitle:@"Switch to cart" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  125. [self.navigationController popViewControllerAnimated:true];
  126. }];
  127. UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"Switch to order history" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  128. [self.navigationController popViewControllerAnimated:false];
  129. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  130. //#ifdef RA_NOTIFICATION
  131. // [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  132. //#else
  133. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  134. //#endif
  135. [((MainViewController*)appDelegate.main_vc) switchToOrder];
  136. }];
  137. UIAlertAction *action_2 = [UIAlertAction actionWithTitle:a2title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  138. // close order;
  139. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  140. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Release Order"];
  141. [RANetwork request_release_order:appDelegate.order_code withScreen:nil completionHandler:^(NSMutableDictionary *result) {
  142. NSDictionary* order_json = result;
  143. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  144. [waitalert dismissViewControllerAnimated:YES completion:^{
  145. if([[order_json valueForKey:@"result"] intValue]==2)
  146. {
  147. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  148. [appDelegate closeOrder];
  149. //#ifdef RA_NOTIFICATION
  150. // [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  151. //#else
  152. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  153. //#endif
  154. [((MainViewController*)appDelegate.main_vc) switchToHome];
  155. if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.submit_order_logout)
  156. {
  157. [((MainViewController*)appDelegate.main_vc) Loginout:false];
  158. }
  159. [self prepareReturn:nil];
  160. [self.navigationController popViewControllerAnimated:true];
  161. }
  162. else
  163. {
  164. [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  165. }
  166. }];
  167. }];
  168. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  169. //
  170. // NSDictionary* order_json = [RANetwork release_Order:appDelegate.order_code withScreen:nil];
  171. //
  172. // dispatch_async(dispatch_get_main_queue(), ^{
  173. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  174. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  175. //
  176. // if([[order_json valueForKey:@"result"] intValue]==2)
  177. // {
  178. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  179. //
  180. // [appDelegate closeOrder];
  181. //
  182. //
  183. // //#ifdef RA_NOTIFICATION
  184. // // [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  185. // //#else
  186. // // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  187. // //#endif
  188. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  189. //
  190. // if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.submit_order_logout)
  191. // {
  192. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  193. // }
  194. //
  195. // [self prepareReturn:nil];
  196. //
  197. //
  198. // [self.navigationController popViewControllerAnimated:true];
  199. //
  200. //
  201. // }
  202. // else
  203. // {
  204. // [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  205. // }
  206. //
  207. //
  208. //
  209. // });
  210. // });
  211. }];
  212. UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  213. }];
  214. [alertController addAction:action_0];
  215. [alertController addAction:action_1];
  216. [alertController addAction:action_2];
  217. [alertController addAction:action_3];
  218. [self presentViewController:alertController animated:YES completion:nil];
  219. //
  220. // 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 cancel order", nil), nil];
  221. // else
  222. // {
  223. // 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];
  224. // }
  225. // alert.tag = CLOSE_ACTION;
  226. //
  227. // [alert show];
  228. }
  229. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  230. //
  231. // if(alertView.tag == CLOSE_ACTION)
  232. // {
  233. // if(buttonIndex==1)
  234. // {
  235. //
  236. // [self.navigationController popViewControllerAnimated:true];
  237. //
  238. // }
  239. // if(buttonIndex==2)
  240. // {
  241. //
  242. // [self.navigationController popViewControllerAnimated:false];
  243. //
  244. //
  245. //
  246. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  247. ////#ifdef RA_NOTIFICATION
  248. //// [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  249. ////#else
  250. //// [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  251. ////#endif
  252. // [((MainViewController*)appDelegate.main_vc) switchToOrder];
  253. //
  254. // }
  255. // else if(buttonIndex==3)
  256. // {
  257. // // close order;
  258. //
  259. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  260. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Release Order"];
  261. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  262. //
  263. // NSDictionary* order_json = [RANetwork release_Order:appDelegate.order_code withScreen:nil];
  264. //
  265. // dispatch_async(dispatch_get_main_queue(), ^{
  266. //// [waitalert dismissViewControllerAnimated:YES completion:nil];
  267. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  268. //
  269. // if([[order_json valueForKey:@"result"] intValue]==2)
  270. // {
  271. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  272. //
  273. // [appDelegate closeOrder];
  274. //
  275. //
  276. ////#ifdef RA_NOTIFICATION
  277. //// [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  278. ////#else
  279. //// [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  280. ////#endif
  281. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  282. //
  283. // if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.submit_order_logout)
  284. // {
  285. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  286. // }
  287. //
  288. // [self prepareReturn:nil];
  289. //
  290. //
  291. // [self.navigationController popViewControllerAnimated:true];
  292. //
  293. //
  294. // }
  295. // else
  296. // {
  297. // [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  298. // }
  299. //
  300. //
  301. //
  302. // });
  303. // });
  304. //
  305. //
  306. // }
  307. // }
  308. //
  309. //}
  310. - (void)setupPageControlBar {
  311. // 1. 控制ToolBar Item显隐
  312. NSMutableArray *items = [self.pageControlToolBar.items mutableCopy];
  313. if (self.pageIndex == 0) {
  314. // 首页不需要前一页
  315. if ([items containsObject:self.frontBarItem]) {
  316. [items removeObject:self.frontBarItem];
  317. }
  318. } else if (self.pageIndex == totalPage - 1) {
  319. // // 尾页不需要后一页
  320. // if ([items containsObject:self.nextBarItem]) {
  321. // [items removeObject:self.nextBarItem];
  322. // }
  323. // 尾页点击Next进入Preview
  324. if (![items containsObject:self.nextBarItem]) {
  325. [items addObject:self.nextBarItem];
  326. }
  327. } else {
  328. // 中间显示前一页、后一页按钮
  329. if (![items containsObject:self.frontBarItem]) {
  330. [items insertObject:self.frontBarItem atIndex:0];
  331. }
  332. if (![items containsObject:self.nextBarItem]) {
  333. [items addObject:self.nextBarItem];
  334. }
  335. }
  336. self.pageControlToolBar.items = items;
  337. }
  338. - (NSString *)getValue:(NSString *)key from:(NSArray *)control {
  339. NSString *ret = nil;
  340. for(int i = 0; i < control.count; i++)
  341. {
  342. NSMutableArray* items=(NSMutableArray*)control[i];
  343. for(int j=0;j<items.count;j++)
  344. {
  345. NSDictionary* item=items[j];
  346. NSString* item_name=[item valueForKey:@"name"];
  347. if([item_name isEqualToString:key])
  348. {
  349. NSString* control = [item valueForKey:@"control"];
  350. if([control isEqualToString:@"enum"])
  351. {
  352. NSDictionary* cadedate = [item objectForKey:@"cadedate"];
  353. int cc = [[cadedate valueForKey:@"count"] intValue];
  354. for(int l=0;l<cc;l++)
  355. {
  356. NSDictionary* val_json=[cadedate objectForKey:[NSString stringWithFormat:@"val_%d",l]];
  357. if([[val_json valueForKey:@"check"]intValue]==1)
  358. return [val_json valueForKey:@"value_id"];
  359. }
  360. }
  361. else
  362. return [item valueForKey:@"value"];
  363. }
  364. }
  365. }
  366. return ret;
  367. }
  368. #pragma mark - BarItem Action
  369. - (IBAction)frontBarItemClick:(UIBarButtonItem *)sender {
  370. if (self.pageIndex == 0) {
  371. return;
  372. }
  373. // 结束编辑防止TextFiled在编辑中时返回,Table会Reload,此时TextFiled会结束编辑,但是Data已经改变。
  374. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
  375. self.pageIndex--;
  376. // 1.恢复当前页面数据
  377. NSMutableDictionary* upparams = [self.pageUpParams lastObject]; // 前页上行参数
  378. __weak typeof(self) weakSelf = self;
  379. [upparams enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  380. [weakSelf.params removeObjectForKey:key];
  381. }];
  382. [self.pageUpParams removeLastObject];
  383. [self.params setObject:@(self.pageIndex) forKey:@"step"];
  384. self.content_data_control = [self.pageContentControlArray lastObject];
  385. [self.pageContentControlArray removeLastObject];
  386. self.content_data_download = [self.pageContentDownloadArray lastObject];
  387. [self.pageContentDownloadArray removeLastObject];
  388. self.changed_data = [self.pageChangeDataArray lastObject];
  389. [self.pageChangeDataArray removeLastObject];
  390. // 2.刷新页面
  391. [self.editorTable reloadData];
  392. self.navigationItem.title = [self.content_data_download valueForKey:@"title"] ;
  393. [self setupPageControlBar];
  394. }
  395. - (IBAction)nextBarItemClick:(UIBarButtonItem *)sender {
  396. if (self.pageIndex == totalPage) {
  397. return;
  398. }
  399. // 结束编辑防止TextFiled在编辑中时下一页,Table会Reload,此时TextFiled会结束编辑,但是Data已经改变。
  400. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
  401. NSMutableDictionary* upparams = [self check_cancommit:true]; // 当前页上行参数
  402. if(self.cancommit==false)
  403. return;
  404. __weak typeof(self) weakSelf = self;
  405. if (self.pageIndex == totalPage - 1) {
  406. // 在此设置Step 避免返回后index不正确。
  407. [upparams setObject:@(self.pageIndex + 1) forKey:@"step"];
  408. // 尾页 进入Preview
  409. [upparams enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  410. [weakSelf.params setObject:obj forKey:key];
  411. }];
  412. RAOrderPreviewController * orderinfoVC = [[RAOrderPreviewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  413. orderinfoVC.disable_dropdown_refresh=true;
  414. orderinfoVC.url_type = URL_REMOTE;
  415. orderinfoVC.request_url=URL_BACK_ORDER_PREVIEW;
  416. orderinfoVC.params = self.params;
  417. orderinfoVC.delegate=self.delegate;
  418. [self.navigationController pushViewController:orderinfoVC animated:true];
  419. return;
  420. }
  421. self.pageIndex++;
  422. // 请求页码
  423. [upparams setObject:@(self.pageIndex) forKey:@"step"];
  424. // 1.保存当前页面数据
  425. if (upparams == nil) {
  426. upparams = [NSMutableDictionary dictionary];
  427. }
  428. [self.pageUpParams addObject:upparams];
  429. if (self.content_data_control == nil) {
  430. self.content_data_control = [NSMutableArray array];
  431. }
  432. [self.pageContentControlArray addObject:self.content_data_control];
  433. if (self.content_data_download == nil) {
  434. self.content_data_download = [NSMutableDictionary dictionary];
  435. }
  436. [self.pageContentDownloadArray addObject:self.content_data_download];
  437. if (self.changed_data == nil) {
  438. self.changed_data = [NSMutableDictionary dictionary];
  439. }
  440. [self.pageChangeDataArray addObject:self.changed_data];
  441. // 2.请求下一页数据
  442. // 当前页及之前所有页参数均上传
  443. [upparams enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  444. [weakSelf.params setObject:obj forKey:key];
  445. }];
  446. [self setupPageControlBar];
  447. self.content_data_download = [NSMutableDictionary dictionary];
  448. [self refresh:nil];
  449. }
  450. #pragma mark - Override
  451. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  452. UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
  453. NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  454. NSString* control = [item_json valueForKey:@"control"];
  455. if([control isEqualToString:@"model"]) {
  456. ((CommonEditorCellModel *)cell).avalibilityLabel.hidden = YES;
  457. }
  458. return cell;
  459. }
  460. - (void)download_success {
  461. NSString *title = [self.content_data_download objectForKey:@"title"];
  462. self.title = title;
  463. [self setupPageControlBar];
  464. // 用于处理不同child,在成功去的数据后,需要附加的操作。
  465. self.have_tail=[[self.content_data_download valueForKey:@"has_reItem"] boolValue];
  466. NSString* order_customerid=[self getValue:@"customer_cid"];
  467. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  468. appDelegate.order_customer_id = order_customerid;
  469. }
  470. - (void)download_failed {
  471. // 恢复为前一页
  472. [self frontBarItemClick:nil];
  473. }
  474. -(NSString*) getValue:(NSString*)name
  475. {
  476. if (self.pageContentControlArray.count) {
  477. NSString* ret=nil;
  478. for (NSInteger i = self.pageContentControlArray.count - 1; i >= 0 ; i--) {
  479. NSArray *control = [self.pageContentControlArray objectAtIndex:i];
  480. ret = [self getValue:name from:control];
  481. if (ret != nil) {
  482. return ret;
  483. }
  484. }
  485. return ret;
  486. } else {
  487. return [self getValue:name from:self.content_data_control];
  488. }
  489. }
  490. #pragma mark - Place Order
  491. - (void)alertMessage:(NSString *)msg {
  492. if (notifyMe_switch) {
  493. if ([self.class_name isEqualToString:@"RAOrderEditorViewController"]) {
  494. NSString *err_msg = msg;
  495. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Attention!!" message:err_msg preferredStyle:UIAlertControllerStyleAlert];
  496. __weak typeof(self) weakself = self;
  497. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  498. [weakself.navigationController popViewControllerAnimated:YES];
  499. }];
  500. [alertVC addAction:action];
  501. [self presentViewController:alertVC animated:YES completion:nil];
  502. } else {
  503. [RAUtils message_alert:msg title:nil controller:self] ;
  504. }
  505. } else {
  506. [RAUtils message_alert:msg title:nil controller:self] ;
  507. }
  508. }
  509. -(void) commonAction:(NSString *)value index:(NSIndexPath*) indexPath data:(NSMutableDictionary*) item_json
  510. {
  511. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  512. if([value isEqualToString:@"new_addr"])
  513. {
  514. AddressEditorViewController * addressVC = [[AddressEditorViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  515. addressVC.url_type = URL_REMOTE;
  516. addressVC.request_url=URL_ADDRESS_EDOTOR;
  517. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  518. addressVC.params = params;
  519. addressVC.delegate = self;
  520. addressVC.from = indexPath;
  521. // orderinfoVC.params = params;
  522. [self.navigationController pushViewController:addressVC animated:true];
  523. }else if([value isEqualToString:@"credit_card"])
  524. {
  525. CreditCardEditorViewController * cardVC = [[CreditCardEditorViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  526. NSDictionary *data = [item_json objectForKey:@"data"];
  527. if(data!=nil)
  528. {
  529. cardVC.content_data_download = [data mutableCopy];
  530. cardVC.url_type = URL_NONE;
  531. cardVC.request_url=nil;
  532. }
  533. else
  534. {
  535. cardVC.url_type = URL_REMOTE;
  536. cardVC.request_url=URL_CREDITCARD_EDOTOR;
  537. }
  538. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  539. params[@"orderCode"]=appDelegate.order_code;
  540. cardVC.params = params;
  541. cardVC.delegate = self;
  542. cardVC.from = indexPath;
  543. [self.navigationController pushViewController:cardVC animated:true];
  544. }
  545. else if([value isEqualToString:@"Sales_Order_Freight_Bill_To"])
  546. {
  547. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  548. cvc.showNavibar = true;
  549. cvc.contact_type = value;
  550. __weak typeof(self) weakself = self;
  551. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  552. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  553. };
  554. [self.navigationController pushViewController:cvc animated:true];
  555. }
  556. else if([value isEqualToString:@"Sales_Order_Ship_From"])
  557. {
  558. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  559. cvc.showNavibar = true;
  560. cvc.contact_type = value;
  561. __weak typeof(self) weakself = self;
  562. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  563. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  564. };
  565. [self.navigationController pushViewController:cvc animated:true];
  566. }
  567. else if([value isEqualToString:@"Sales_Order_Customer"])
  568. {
  569. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  570. cvc.contact_type = value;
  571. cvc.showNavibar = true;
  572. __weak typeof(self) weakself = self;
  573. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  574. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  575. };
  576. [self.navigationController pushViewController:cvc animated:true];
  577. }
  578. else if([value isEqualToString:@"Sales_Order_Merchandise_Bill_To"])
  579. {
  580. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  581. cvc.showNavibar = true;
  582. cvc.contact_type = value;
  583. __weak typeof(self) weakself = self;
  584. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  585. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  586. };
  587. [self.navigationController pushViewController:cvc animated:true];
  588. }
  589. else if([value isEqualToString:@"Contact_Return_To"])
  590. {
  591. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  592. cvc.showNavibar = true;
  593. cvc.contact_type = value;
  594. __weak typeof(self) weakself = self;
  595. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  596. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  597. };
  598. [self.navigationController pushViewController:cvc animated:true];
  599. }
  600. else if([value isEqualToString:@"Sales_Order_Ship_To"])
  601. {
  602. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  603. cvc.showNavibar = true;
  604. cvc.contact_type = value;
  605. __weak typeof(self) weakself = self;
  606. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  607. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  608. };
  609. [self.navigationController pushViewController:cvc animated:true];
  610. }
  611. }
  612. - (UIView *)createDemoView:(NSString*) msg
  613. {
  614. UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];
  615. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
  616. // [imageView setImage:[UIImage imageNamed:@"demo"]];
  617. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 270, 10)];
  618. titleLabel.font = [UIFont boldSystemFontOfSize:20];
  619. titleLabel.text=@"Order Submission";
  620. titleLabel.numberOfLines = 0;
  621. titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
  622. // titleLabel.textAlignment = NSTextAlignmentRight;
  623. titleLabel.textAlignment = NSTextAlignmentCenter;
  624. CGSize constraintkey = CGSizeMake(270, 20000.0f);//key label width is 40% cell width;
  625. // CGSize sizetitle = [titleLabel.text=@"Order Submission" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  626. CGSize sizetitle = [RAUtils sizeWithFont:titleLabel.text=@"Order Submission" font:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  627. //[titleLabel sizeToFit];
  628. // titleLabel.center.x = demoView.center.x;
  629. titleLabel.frame = CGRectMake(10, 10, 270, sizetitle.height);
  630. [demoView addSubview:titleLabel];
  631. UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, 10)];
  632. lineLabel.font = [UIFont systemFontOfSize:17];
  633. lineLabel.text=msg;
  634. lineLabel.numberOfLines = 0;
  635. lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
  636. // CGSize sizemsg = [msg sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  637. CGSize sizemsg = [RAUtils sizeWithFont:msg font:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  638. lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, sizemsg.height);
  639. //[lineLabel sizeToFit];
  640. [demoView addSubview:lineLabel];
  641. demoView.frame = CGRectMake(0, 0, 290, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
  642. return demoView;
  643. }
  644. -(float) getprice:(NSString*) name section :(long) index
  645. {
  646. NSMutableDictionary* section_json=nil;
  647. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",index ]] mutableCopy];
  648. int count=0;
  649. count=[[section_json valueForKey:@"count"] intValue];
  650. for(int i=0;i<count;i++)
  651. {
  652. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  653. if([[olditem valueForKey:@"name"] isEqualToString:name])
  654. {
  655. return [[olditem valueForKey:@"value"] floatValue];
  656. }
  657. }
  658. return 0;
  659. }
  660. - (void)textFieldDidEndEditing:(UITextField *)textField
  661. {
  662. // [super textFieldDidEndEditing:textField];
  663. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  664. NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  665. // table滑动后,cell可能会滚出table visible范围,此时indexPath为nil
  666. if (indexPath == nil) {
  667. indexPath = self.lastedit_from;
  668. }
  669. // 避免提前将lastedit_from置空
  670. [super textFieldDidEndEditing:textField];
  671. NSMutableDictionary* section_json=nil;
  672. // DebugLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  673. NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  674. NSString* name = [item_json valueForKey:@"name"];
  675. if([name isEqualToString:@"shipping"]|| [name isEqualToString:@"handling_fee_value"]||[name isEqualToString:@"lift_gate_value"]||[name isEqualToString:@"tax_percentage"])
  676. {
  677. float shipping = [self getprice:@"shipping" section:indexPath.section];
  678. float paymentsAndCredits = [self getprice:@"paymentsAndCredits" section:indexPath.section];
  679. float handling_fee_value = [self getprice:@"handling_fee_value" section:indexPath.section];
  680. float lift_gate =[self getprice:@"lift_gate_value" section:indexPath.section];
  681. float tax = [self getprice:@"tax_percentage" section:indexPath.section]/100.0*paymentsAndCredits;
  682. float totalprice=shipping+paymentsAndCredits+handling_fee_value+lift_gate+tax;
  683. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  684. int count=0;
  685. count=[[section_json valueForKey:@"count"] intValue];
  686. int total_row_idx=-1;
  687. for(int i=0;i<count;i++)
  688. {
  689. NSDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]] mutableCopy];
  690. if([[olditem valueForKey:@"name"] isEqualToString:@"totalPrice" ] || [[olditem valueForKey:@"name"] isEqualToString:@"totalPrice_a" ])
  691. {
  692. [olditem setValue:[NSString stringWithFormat:@"%.2f",totalprice] forKey:@"value"];
  693. [olditem setValue:@"true" forKey:@"dirty"];
  694. [section_json setObject:olditem forKey:[NSString stringWithFormat:@"item_%d",i ]];
  695. total_row_idx=i;
  696. }
  697. }
  698. [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  699. self.content_data_control = [self translate_json:self.content_data_download changed: self.changed_data];
  700. if(total_row_idx>=0)
  701. {
  702. NSUInteger newIndex[] = {indexPath.section, total_row_idx};
  703. NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
  704. // DebugLog(@"before refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  705. [self.editorTable reloadRowsAtIndexPaths:@[newPath]withRowAnimation:UITableViewRowAnimationMiddle];
  706. }
  707. }
  708. }
  709. //
  710. //- (IBAction)onSaveClick:(id)sender {
  711. //
  712. // id a = nil;
  713. // NSMutableArray *arr = [NSMutableArray array];
  714. // [arr addObject:a];
  715. //
  716. // [self.lastedit endEditing:true];
  717. // [self.lasttextview endEditing:true];
  718. //
  719. // NSMutableDictionary* upparams = [self check_cancommit:false];
  720. //
  721. // if([upparams[@"shipping"] isEqualToString:@"Shipping To Be Quoted"]||[upparams[@"shipping"] isEqualToString:@"To Be Quoted"])
  722. // upparams[@"shipping"] = nil;
  723. //
  724. //
  725. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Saving Order"];
  726. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  727. //
  728. // NSDictionary* editor_json = [RANetwork save_Order:upparams];
  729. //
  730. // dispatch_async(dispatch_get_main_queue(), ^{
  731. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  732. //
  733. //
  734. // if([[editor_json valueForKey:@"result"] intValue]==2)
  735. // {
  736. //
  737. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  738. //
  739. // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table];
  740. //
  741. // if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  742. // {
  743. // appDelegate.customerInfo = customerInfo;
  744. // appDelegate.contact_id = [customerInfo valueForKey:@"customer_cid"];
  745. // }
  746. // NSString *msg = nil;
  747. // if ([editor_json objectForKey:@"so#"]) {
  748. // msg = [@"SO#:" stringByAppendingString:[editor_json objectForKey:@"so#"]];
  749. // }
  750. // [RAUtils message_alert:msg title:@"Order Saved" controller:self] ;
  751. //
  752. // if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.save_order_logout && appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  753. // {
  754. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  755. // [self prepareReturn:nil];
  756. // [self.navigationController popViewControllerAnimated:true];
  757. // }
  758. //
  759. // if (appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  760. //
  761. // if (![Singleton sharedInstance].currentOrderIsMerged) {
  762. // [self releaseOrderAfterSave];
  763. // }
  764. //
  765. //
  766. // }
  767. //
  768. // }
  769. // else
  770. // {
  771. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Save Order" controller:self] ;
  772. // }
  773. //
  774. // });
  775. // });
  776. //
  777. //}
  778. - (void)releaseOrderAfterSave {
  779. dispatch_async(dispatch_get_main_queue(), ^{
  780. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  781. [RANetwork request_release_order:appDelegate.order_code withScreen:nil completionHandler:nil];
  782. [appDelegate closeOrder];
  783. [appDelegate SetSo:nil];
  784. appDelegate.cart_count=0;
  785. [appDelegate update_count_mark];
  786. [((MainViewController*)appDelegate.main_vc) switchToOrder];
  787. [self prepareReturn:nil];
  788. [self.navigationController popToRootViewControllerAnimated:false];
  789. });
  790. //
  791. // [RANetwork release_Order:appDelegate.order_code withScreen:nil];
  792. //
  793. // [appDelegate closeOrder];
  794. // [appDelegate SetSo:nil];
  795. // appDelegate.cart_count=0;
  796. // [appDelegate update_count_mark];
  797. // [((MainViewController*)appDelegate.main_vc) switchToOrder];
  798. //
  799. // [self prepareReturn:nil];
  800. // [self.navigationController popToRootViewControllerAnimated:false];
  801. }
  802. -(NSString* )create_customer_ext:(NSDictionary*)customerinfo
  803. {
  804. NSString* ret=nil;
  805. NSString* customer_address1= [customerinfo valueForKey:@"customer_address1"];
  806. NSString* customer_address2= [customerinfo valueForKey:@"customer_address2"];
  807. NSString* customer_address3= [customerinfo valueForKey:@"customer_address3"];
  808. NSString* customer_address4= [customerinfo valueForKey:@"customer_address4"];
  809. NSMutableArray* arraddr = [[NSMutableArray alloc] init];
  810. if(customer_address1.length>0)
  811. [arraddr addObject:customer_address1];
  812. if(customer_address2.length>0)
  813. [arraddr addObject:customer_address2];
  814. if(customer_address3.length>0)
  815. [arraddr addObject:customer_address3];
  816. if(customer_address4.length>0)
  817. [arraddr addObject:customer_address4];
  818. NSString *addr_string = [arraddr componentsJoinedByString:@"\r\n"];
  819. NSString* customer_city= [customerinfo valueForKey:@"customer_city"];
  820. NSString* customer_country= [customerinfo valueForKey:@"customer_country"];
  821. NSString* customer_zipcode= [customerinfo valueForKey:@"customer_zipcode"];
  822. NSString* customer_state= [customerinfo valueForKey:@"customer_state"];
  823. NSMutableArray* arrcty = [[NSMutableArray alloc] init];
  824. if(customer_city.length>0)
  825. [arrcty addObject:customer_city];
  826. if(customer_state.length>0)
  827. [arrcty addObject:customer_state];
  828. if(customer_zipcode.length>0)
  829. [arrcty addObject:customer_zipcode];
  830. if(customer_country.length>0)
  831. [arrcty addObject:customer_country];
  832. NSString *cty_string = [arrcty componentsJoinedByString:@", "];
  833. NSMutableArray* arrext = [[NSMutableArray alloc] init];
  834. if(addr_string.length>0)
  835. [arrext addObject:addr_string];
  836. if(cty_string.length>0)
  837. [arrext addObject:cty_string];
  838. ret = [arrext componentsJoinedByString:@"\r\n"];
  839. return ret;
  840. }
  841. -(NSMutableDictionary*)create_cusromer_info_from_table
  842. {
  843. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  844. NSMutableDictionary* ret= [appDelegate.customerInfo mutableCopy] ;
  845. [ret setValue:[self getValue:@"customer_country"] forKey:@"customer_country"];
  846. [ret setValue:[self getValue:@"customer_phone"] forKey:@"customer_phone"];
  847. [ret setValue:[self getValue:@"customer_email"] forKey:@"customer_email"];
  848. [ret setValue:[self getValue:@"customer_name"] forKey:@"customer_name"];
  849. [ret setValue:[self getValue:@"customer_address4"] forKey:@"customer_address4"];
  850. [ret setValue:[self getValue:@"customer_address3"] forKey:@"customer_address3"];
  851. [ret setValue:[self getValue:@"customer_address2"] forKey:@"customer_address2"];
  852. [ret setValue:[self getValue:@"customer_address1"] forKey:@"customer_address1"];
  853. [ret setValue:[self getValue:@"customer_zipcode"] forKey:@"customer_zipcode"];
  854. [ret setValue:[self getValue:@"customer_city"] forKey:@"customer_city"];
  855. [ret setValue:[self getValue:@"customer_state"] forKey:@"customer_state"];
  856. [ret setValue:[self getValue:@"customer_cid"] forKey:@"customer_cid"];
  857. [ret setValue:[self getValue:@"customer_state"] forKey:@"customer_state"];
  858. [ret setValue:[self getValue:@"customer_contact"] forKey:@"customer_contact"];
  859. [ret setValue:[self getValue:@"customer_fax"] forKey:@"customer_fax"];
  860. NSString * firstname =nil;
  861. NSString* lastname = nil;
  862. NSString* contact_name = [self getValue:@"customer_contact"];
  863. if(contact_name!=nil)
  864. {
  865. NSArray *_arr = [contact_name componentsSeparatedByString:NSLocalizedString(@" ", nil)];
  866. if(_arr.count>=1)
  867. {
  868. firstname =_arr[0];
  869. if(_arr.count>1)
  870. {
  871. lastname=[[_arr subarrayWithRange:NSMakeRange(1, _arr.count-1)] componentsJoinedByString:@" "];
  872. }
  873. }
  874. }
  875. [ret setValue:firstname forKey:@"customer_first_name"];
  876. [ret setValue:lastname forKey:@"customer_last_name"];
  877. // [ret setValue:[self create_customer_ext:ret] forKey:@"customer_contact_ext"];
  878. return ret;
  879. }
  880. -(NSMutableDictionary*)create_value_map:(NSDictionary*)key_map
  881. {
  882. NSMutableDictionary* ret= [[NSMutableDictionary alloc] init];
  883. NSArray *keys= [key_map allKeys];
  884. //遍历keys
  885. for(int i=0;i<[keys count];i++)
  886. {
  887. //得到当前key
  888. NSString *key=[keys objectAtIndex:i];
  889. //如果key不是pic,说明value是字符类型,比如name:Boris
  890. NSString* valuefrom = [key_map valueForKey:key];
  891. if(valuefrom.length==0)
  892. continue;
  893. NSString* value=[self getValue:valuefrom];
  894. [ret setValue:value forKey:valuefrom];
  895. }
  896. NSDictionary * customerinfo =[self create_cusromer_info_from_table];
  897. [ret setValue:[self create_customer_ext:customerinfo] forKey:@"customer_contact_ext"];
  898. [ret setValue:customerinfo[@"customer_first_name"] forKey:@"customer_first_name"];
  899. [ret setValue:customerinfo[@"customer_last_name"] forKey:@"customer_last_name"];
  900. return ret;
  901. }
  902. #pragma mark - multi_action sub action clicked
  903. - (void)MActionClicked:(UIButton *)sender {
  904. UITableViewCell *cell = (UITableViewCell *) sender.superview.superview;
  905. NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  906. int refresh =0;
  907. NSMutableDictionary* section_json=nil;
  908. NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  909. NSString* subid=[item_json valueForKey:@"subid"];
  910. NSString* refresh_trigger = [item_json valueForKey:@"name"];
  911. NSDictionary* sub_action = [item_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)sender.tag]];
  912. NSString* type = [sub_action valueForKey:@"type"];
  913. if([type isEqualToString:@"pull"])
  914. {
  915. [self.lastedit endEditing:true];
  916. refresh = [[sub_action valueForKey:@"refresh"] intValue];
  917. NSDictionary* keymap= [sub_action objectForKey:@"key_map"];
  918. NSMutableDictionary* value=[self create_value_map:keymap];
  919. if(subid==nil)
  920. {
  921. NSDictionary* jsonmap=[sub_action valueForKey:@"key_map"];
  922. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  923. int icount=[[section_json valueForKey:@"count"] intValue];
  924. for (int ic=0;ic<icount;ic++)
  925. {
  926. NSMutableDictionary* modify_item= [[section_json objectForKey:[NSString stringWithFormat:@"item_%d",ic ]] mutableCopy];
  927. NSString* mapto = [modify_item valueForKey:@"name"];
  928. if(mapto.length==0)
  929. continue;
  930. NSString* valuefrom= [jsonmap valueForKey:[modify_item valueForKey:@"name"]];
  931. if(valuefrom==nil)
  932. continue;
  933. NSString* valuestr=[value valueForKey:valuefrom];
  934. // valuestr = [self getValue:valuefrom];
  935. if(true)
  936. {
  937. if(valuestr.length>0)
  938. [modify_item setValue:valuestr forKey:@"value"];
  939. else
  940. [modify_item setValue:@"" forKey:@"value"];
  941. [modify_item setValue:@"true" forKey:@"dirty"];
  942. [section_json setObject:modify_item forKey:[NSString stringWithFormat:@"item_%d",ic ]];
  943. }
  944. }
  945. }
  946. else
  947. {
  948. NSArray* idarr=[subid componentsSeparatedByString:@"_"];
  949. int section = [(NSString*)idarr[0] intValue];
  950. int item=[(NSString*)idarr[1] intValue];
  951. NSDictionary* jsonmap=[sub_action valueForKey:@"key_map"];
  952. section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d", section]] mutableCopy];
  953. NSMutableDictionary * olditem=[[section_json objectForKey:[NSString stringWithFormat:@"item_%d",item ]] mutableCopy];
  954. NSString* control_type = [olditem valueForKey:@"control"];
  955. if([control_type isEqualToString:@"switch"])
  956. olditem= [self fill_switch_subitem:olditem subid:nil source:value mapping:jsonmap ];
  957. else if([control_type isEqualToString:@"enum"])
  958. {
  959. olditem= [self fill_enum_subitem:olditem subid:nil source:value mapping:jsonmap ];
  960. }
  961. else
  962. {
  963. [RAUtils message_alert:@"not impl" title:@"Sorry" controller:self];
  964. // [RAUtils alert_view:@"" title:@"not impl"];
  965. }
  966. [olditem setValue:@"true" forKey:@"dirty"];
  967. [section_json setObject:olditem forKey:[NSString stringWithFormat:@"item_%d",item ]];
  968. }
  969. [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  970. self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
  971. if(refresh==1)
  972. {
  973. // if(restore_json!=nil)
  974. // {
  975. // int rc=[[restore_json valueForKey:@"count"] intValue];
  976. // for(int ir=0;ir<rc;ir++)
  977. // {
  978. // NSString* name = [restore_json valueForKey:[NSString stringWithFormat:@"item_%d",ir]];
  979. //
  980. //
  981. // int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
  982. // for(int i=0;i<section_count;i++)
  983. // {
  984. // NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  985. // // NSMutableDictionary * to_sectionjson = [[to objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
  986. // int item_count = [[sectionjson valueForKey:@"count"] intValue];
  987. // for(int j=0;j<item_count;j++)
  988. // {
  989. // NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  990. // // NSMutableDictionary * to_itemjson=[[to_sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
  991. //
  992. // if([[itemjson valueForKey:@"name"] isEqualToString:name])
  993. // {
  994. // [itemjson removeObjectForKey:@"dirty"];
  995. // [sectionjson setObject:itemjson forKey:[NSString stringWithFormat:@"item_%d",j ]];
  996. // }
  997. //
  998. // }
  999. // [self.content_data_download setObject:sectionjson forKey:[NSString stringWithFormat:@"section_%d",i ]];
  1000. // }
  1001. //
  1002. //
  1003. // }
  1004. //
  1005. // }
  1006. [self refresh:refresh_trigger] ;
  1007. return ;
  1008. }
  1009. // DebugLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
  1010. [self.editorTable reloadData ];
  1011. }
  1012. else
  1013. {
  1014. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1015. NSDictionary * item_json = sub_action;
  1016. NSString* value = [item_json valueForKey:@"value"];
  1017. if([value isEqualToString:@"new_addr"])
  1018. {
  1019. AddressEditorViewController * addressVC = [[AddressEditorViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1020. addressVC.url_type = URL_REMOTE;
  1021. addressVC.request_url=URL_ADDRESS_EDOTOR;
  1022. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1023. // NSString * string = [checked componentsJoinedByString:@","];
  1024. // [params setValue:string forKey:@"cart2Checkbox"];
  1025. params[@"is_subaction"]=@"true";
  1026. params[@"subaction_tag"]= [NSString stringWithFormat:@"%ld",(long)sender.tag];
  1027. addressVC.params = params;
  1028. addressVC.delegate = self;
  1029. addressVC.from = indexPath;
  1030. // orderinfoVC.params = params;
  1031. [self.navigationController pushViewController:addressVC animated:true];
  1032. }else if([value isEqualToString:@"credit_card"])
  1033. {
  1034. CreditCardEditorViewController * cardVC = [[CreditCardEditorViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1035. NSDictionary *data = [item_json objectForKey:@"data"];
  1036. if(data!=nil)
  1037. {
  1038. cardVC.content_data_download = [data mutableCopy];
  1039. cardVC.url_type = URL_NONE;
  1040. cardVC.request_url=nil;
  1041. }
  1042. else
  1043. {
  1044. cardVC.url_type = URL_REMOTE;
  1045. cardVC.request_url=URL_CREDITCARD_EDOTOR;
  1046. }
  1047. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1048. params[@"orderCode"]=appDelegate.order_code;
  1049. params[@"is_subaction"]=@"true";
  1050. params[@"subaction_tag"]= [NSString stringWithFormat:@"%ld",(long)sender.tag];
  1051. cardVC.params = params;
  1052. cardVC.delegate = self;
  1053. cardVC.from = indexPath;
  1054. // orderinfoVC.params = params;
  1055. [self.navigationController pushViewController:cardVC animated:true];
  1056. }
  1057. else if([value isEqualToString:@"Sales_Order_Freight_Bill_To"])
  1058. {
  1059. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1060. cvc.showNavibar = true;
  1061. cvc.contact_type = value;
  1062. cvc.is_subaction = @"true";
  1063. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1064. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1065. [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1066. //
  1067. // if(self.returnValue)
  1068. // self.returnValue(value);
  1069. };
  1070. [self.navigationController pushViewController:cvc animated:true];
  1071. }
  1072. else if([value isEqualToString:@"Sales_Order_Ship_From"])
  1073. {
  1074. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1075. cvc.showNavibar = true;
  1076. cvc.contact_type = value;
  1077. cvc.is_subaction = @"true";
  1078. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1079. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1080. [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1081. //
  1082. // if(self.returnValue)
  1083. // self.returnValue(value);
  1084. };
  1085. [self.navigationController pushViewController:cvc animated:true];
  1086. }
  1087. else if([value isEqualToString:@"Sales_Order_Customer"])
  1088. {
  1089. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1090. cvc.is_subaction = @"true";
  1091. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1092. cvc.showNavibar = true;
  1093. cvc.contact_type = value;
  1094. __weak typeof(self) weakself = self;
  1095. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1096. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1097. //
  1098. // if(self.returnValue)
  1099. // self.returnValue(value);
  1100. };
  1101. [self.navigationController pushViewController:cvc animated:true];
  1102. }
  1103. else if([value isEqualToString:@"Sales_Order_Merchandise_Bill_To"])
  1104. {
  1105. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1106. cvc.showNavibar = true;
  1107. cvc.contact_type = value;
  1108. cvc.is_subaction = @"true";
  1109. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1110. __weak typeof(self) weakself = self;
  1111. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1112. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1113. //
  1114. // if(self.returnValue)
  1115. // self.returnValue(value);
  1116. };
  1117. [self.navigationController pushViewController:cvc animated:true];
  1118. }
  1119. else if([value isEqualToString:@"Contact_Return_To"])
  1120. {
  1121. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1122. cvc.is_subaction = @"true";
  1123. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1124. cvc.showNavibar = true;
  1125. cvc.contact_type = value;
  1126. __weak typeof(self) weakself = self;
  1127. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1128. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1129. //
  1130. // if(self.returnValue)
  1131. // self.returnValue(value);
  1132. };
  1133. [self.navigationController pushViewController:cvc animated:true];
  1134. }
  1135. else if([value isEqualToString:@"Sales_Order_Ship_To"])
  1136. {
  1137. ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1138. cvc.is_subaction = @"true";
  1139. cvc.subaction_tag =[NSString stringWithFormat:@"%ld",(long)sender.tag];
  1140. cvc.showNavibar = true;
  1141. cvc.contact_type = value;
  1142. __weak typeof(self) weakself = self;
  1143. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1144. [weakself handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1145. //
  1146. // if(self.returnValue)
  1147. // self.returnValue(value);
  1148. };
  1149. [self.navigationController pushViewController:cvc animated:true];
  1150. }
  1151. }
  1152. }
  1153. #pragma mark - Urgency
  1154. -(void) viewWillDisappear:(BOOL)animated
  1155. {
  1156. [super viewWillDisappear:animated];
  1157. [self cancel_urgency_timer];
  1158. }
  1159. - (void)start_urgency_timer {
  1160. if (!self.editable) {
  1161. return;
  1162. }
  1163. __weak typeof(self) weakSelf = self;
  1164. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1165. MainViewController *mainVC = (MainViewController *)appDelegate.main_vc;
  1166. __block NSString* labelsotext= mainVC.labelSo.text;
  1167. [appDelegate.urgencyDic setValue:appDelegate.user forKey:@"user"];
  1168. #ifdef OFFLINE_MODE
  1169. [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"];
  1170. #endif
  1171. [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{
  1172. NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary];
  1173. // DebugLog(@"timer start....");
  1174. [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  1175. [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"];
  1176. if ([weakSelf.class_name isEqualToString:@"RAOrderEditorViewController"]) {
  1177. [urgencyDic setValue:appDelegate.order_code forKey:@"order_code"];
  1178. NSString *so = [labelsotext substringFromIndex:5];
  1179. [urgencyDic setValue:so forKey:@"order_so"];
  1180. [urgencyDic setValue:[NSNumber numberWithInt:appDelegate.order_status ] forKey:@"order_status"];
  1181. [urgencyDic setValue:appDelegate.order_customer_id forKey:@"order_customer_id"];
  1182. // page
  1183. [urgencyDic setObject:@(weakSelf.pageIndex) forKey:@"page_index"];
  1184. [urgencyDic setObject:weakSelf.pageContentDownloadArray forKey:@"download_content_array"];
  1185. [urgencyDic setObject:weakSelf.pageContentControlArray forKey:@"control_content_array"];
  1186. [urgencyDic setObject:weakSelf.pageChangeDataArray forKey:@"changed_data_array"];
  1187. [urgencyDic setObject:weakSelf.pageUpParams forKey:@"up_param_array"];
  1188. [urgencyDic setObject:weakSelf.request_url forKey:@"request_url"];
  1189. [urgencyDic setObject:weakSelf.params forKey:@"params"];
  1190. }
  1191. [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]];
  1192. [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"];
  1193. }];
  1194. }
  1195. - (void)cancel_urgency_timer {
  1196. [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name];
  1197. }
  1198. - (void)recoveryFromUrgencyDic:(NSDictionary *)urgencyDic {
  1199. self.disable_dropdown_refresh=true;
  1200. self.url_type = URL_RESTORE;
  1201. self.params = [NSMutableDictionary dictionary];
  1202. self.content_data_download = [urgencyDic objectForKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]];
  1203. self.resumeOrder = YES;
  1204. self.pageIndex = [[urgencyDic objectForKey:@"page_index"] integerValue];
  1205. self.pageContentDownloadArray = [urgencyDic objectForKey:@"download_content_array"];
  1206. self.pageContentControlArray = [urgencyDic objectForKey:@"control_content_array"];
  1207. self.pageChangeDataArray = [urgencyDic objectForKey:@"changed_data_array"];
  1208. self.pageUpParams = [urgencyDic objectForKey:@"up_param_array"];
  1209. self.request_url = [urgencyDic objectForKey:@"request_url"];
  1210. self.params = [urgencyDic objectForKey:@"params"];
  1211. }
  1212. @end