RAOrderEditorViewController.m 64 KB

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