OrderListViewController.m 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. //
  2. // OrderListViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 8/18/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OrderListViewController.h"
  9. #import "OrderListTableViewCell.h"
  10. #import "iSalesNetwork.h"
  11. #import "OrderDetailViewController.h"
  12. #import "MainViewController.h"
  13. #import "ContactListViewController.h"
  14. @interface OrderListViewController ()
  15. @end
  16. @implementation OrderListViewController
  17. -(void) logout
  18. {
  19. self.offset = 0;
  20. [self.content_data removeAllObjects];
  21. self.status_cadedate = nil;
  22. }
  23. -(void) reload_container_getdata:(bool) update_data
  24. {
  25. [super reload_container_getdata:update_data];
  26. if(update_data)
  27. {
  28. self.offset = 0;
  29. [self.content_data removeAllObjects];
  30. self.norefresh=true;
  31. [self loadpage];
  32. }
  33. else
  34. {
  35. [self.table_order reloadData];
  36. }
  37. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  38. if(appDelegate.can_create_order)
  39. {
  40. [self.btn_newOrder setImage:[UIImage imageNamed:@"neworder"]];
  41. self.btn_newOrder.enabled = true;
  42. }
  43. else
  44. {
  45. [self.btn_newOrder setImage:nil];
  46. self.btn_newOrder.enabled = false;
  47. }
  48. }
  49. -(void) showHidePrice
  50. {
  51. [self reload_container_getdata:false];
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. self.edgesForExtendedLayout = UIRectEdgeNone;
  56. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  57. self.label_net_err.layer.borderWidth = 2.0;
  58. self.label_net_err.layer.cornerRadius=15;
  59. self.label_net_err.layer.masksToBounds=true;
  60. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  61. // tap.minimumPressDuration = 0.8; //定义按的时间
  62. [self.label_net_err addGestureRecognizer:tap];
  63. // Do any additional setup after loading the view.
  64. self.offset = 0;
  65. self.limit = 25;
  66. self.content_data = [[NSMutableArray alloc]init];
  67. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  68. ref.tag = 201 ;
  69. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  70. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  71. // ref.hidden = true;
  72. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  73. [self.table_order addSubview:ref];
  74. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  75. NSString *documents = [paths objectAtIndex:0];
  76. NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  77. // if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  78. //
  79. // NSLog(@"文件已经存在了");
  80. //
  81. // }
  82. // else
  83. // {
  84. //
  85. //
  86. //
  87. // // NSLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  88. //
  89. // NSData *mainBundleFile = [NSData dataWithContentsOfFile:order_filter_source_path];
  90. //
  91. // // NSLog(@"mainBundleFile==%@",mainBundleFile);
  92. //
  93. // [[NSFileManager defaultManager] createFileAtPath:dist_path
  94. //
  95. // contents:mainBundleFile
  96. //
  97. // attributes:nil];
  98. //
  99. // }
  100. [self loadfilter];
  101. }
  102. -(void) loadfilter
  103. {
  104. if(self.status_cadedate!=nil)
  105. return;
  106. UIApplication * app = [UIApplication sharedApplication];
  107. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  108. NSData* json =nil;
  109. if(self.init_style==OL_OPEN)
  110. {
  111. json=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"status_filter_cadedate_open" ofType:@"json" ]];
  112. NSError *error=nil;
  113. self.status_cadedate = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  114. }
  115. else
  116. {
  117. // json=//[NSData dataWithContentsOfFile:dist_path];
  118. // NSError *error=nil;
  119. self.status_cadedate = appDelegate.OrderFilter;//[[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  120. }
  121. }
  122. - (IBAction)onCancel:(id)sender {
  123. if(self.onCancel)
  124. self.onCancel();
  125. [self.navigationController popViewControllerAnimated:false];
  126. }
  127. - (IBAction)onStatusFilterClick:(id)sender {
  128. EnumSelectViewController* enumvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];
  129. enumvc.delegate = self;
  130. enumvc.max_select = 0;
  131. //enumvc.updatePosition = indexPath;
  132. // if([[required lowercaseString] isEqualToString:@"true"])
  133. // enumvc.canbeEmpty = false;
  134. // else
  135. // enumvc.canbeEmpty =true;
  136. enumvc.cadedate = self.status_cadedate ;
  137. enumvc.title = @"Order Status Selection";
  138. // enumvc.value = [value_json mutableCopy];
  139. enumvc.single_select =false;
  140. [self.navigationController pushViewController:enumvc animated:true];
  141. }
  142. - (void)viewWillAppear:(BOOL)animated
  143. {
  144. [super viewWillAppear:animated];
  145. [self loadfilter];
  146. // UIApplication * app = [UIApplication sharedApplication];
  147. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  148. // if(appDelegate.bLogin==false)
  149. // {
  150. // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  151. //// loginvc.delegate = self;
  152. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  153. //
  154. // loginvc.returnValue = ^(bool blogin){
  155. //
  156. //
  157. // UIApplication * app = [UIApplication sharedApplication];
  158. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  159. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  160. //
  161. // [main_vc checklogin:true];
  162. //
  163. //// self.offset = 0;
  164. //// [self.content_data removeAllObjects];
  165. //// [self loadpage];
  166. //
  167. // [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO];
  168. //
  169. // };
  170. //
  171. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  172. //
  173. //
  174. //
  175. //
  176. //
  177. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  178. //
  179. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  180. // [self presentViewController:navi animated:YES completion:^{
  181. //
  182. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  183. //
  184. // NSLog(@"LoginViewController present.........");
  185. //
  186. // // self.btop = false;
  187. // // <#code#>
  188. // }];
  189. // }
  190. // else
  191. // {
  192. //
  193. // if(!self.norefresh)//避免 多次刷新
  194. // {
  195. // self.norefresh = true;
  196. // self.offset = 0;
  197. // [self.content_data removeAllObjects];
  198. // [self loadpage];
  199. //
  200. // }
  201. //
  202. // }
  203. [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO];
  204. }
  205. - (IBAction)onNewOrderClicked:(id)sender {
  206. NSString* msg=@"Do you want to create a new order?";
  207. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  208. if(appDelegate.order_code.length==0)
  209. {
  210. if(appDelegate.contact_id.length>0)
  211. {
  212. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  213. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  214. }
  215. }
  216. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Attention", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  217. [alert show];
  218. }
  219. - (void)didReceiveMemoryWarning {
  220. [super didReceiveMemoryWarning];
  221. // Dispose of any resources that can be recreated.
  222. }
  223. -(void)manually_refresh
  224. {
  225. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  226. if(self.isrefreshing)
  227. {
  228. [reF endRefreshing];
  229. return;
  230. }
  231. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  232. if ([self respondsToSelector:@selector(ReloadData)])
  233. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  234. }
  235. -(void)ReloadData
  236. {
  237. [self.content_data removeAllObjects];
  238. [self.table_order reloadData];
  239. // self.table_order.hidden = true;
  240. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  241. [reF endRefreshing];
  242. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  243. self.keywords=self.searchbar.text;
  244. self.offset = 0;
  245. // [self.content_data removeAllObjects];
  246. // [self.table_order reloadData];
  247. [self loadpage];
  248. // [self loadpage];
  249. }
  250. -(void) loadpage
  251. {
  252. dispatch_async(dispatch_get_main_queue(), ^{
  253. if(self.isrefreshing)
  254. return;
  255. self.isrefreshing=true;
  256. if(self.offset==0)
  257. {
  258. self.table_order.hidden = true;
  259. self.mum.center = self.view.center;
  260. self.mum.hidden = false;
  261. [self.mum startAnimating];
  262. }
  263. self.label_net_err.hidden=true;
  264. NSMutableArray* arr_status = [[NSMutableArray alloc] init];
  265. NSMutableArray* arr_statusname = [[NSMutableArray alloc] init];
  266. int count = [[self.status_cadedate valueForKey:@"count"] intValue];
  267. for(int i=0;i<count;i++)
  268. {
  269. NSDictionary* val_json =[self.status_cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  270. int check = [[val_json valueForKey:@"check"] intValue];
  271. if(check==1)
  272. {
  273. [arr_status addObject:[NSString stringWithFormat:@"%@",[val_json valueForKey:@"value_id"]]];
  274. [arr_statusname addObject:[NSString stringWithFormat:@"%@",[val_json valueForKey:@"value"]]];
  275. // break;
  276. }
  277. }
  278. NSString * str_status = [arr_status componentsJoinedByString:@","];
  279. NSString * str_statusname = [arr_statusname componentsJoinedByString:@","];
  280. if(str_statusname.length==0)
  281. str_statusname = @"All";
  282. self.label_filter.text = str_statusname;
  283. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  284. NSMutableDictionary* content=nil;
  285. content=[[iSalesNetwork request_OrderList:self.offset limit : self.limit keywords:self.keywords status:str_status customer:self.customer_id] mutableCopy];
  286. dispatch_async(dispatch_get_main_queue(), ^{
  287. [self.mum stopAnimating];
  288. int result=[[content valueForKey:@"result"] intValue];
  289. if(result==2||result==0)
  290. {
  291. int count = [[content valueForKey:@"count" ] intValue] ;
  292. self.time_zone = [content valueForKey:@"time_zone" ];
  293. self.offset += count;
  294. for(int i=0;i<count;i++)
  295. {
  296. NSDictionary* objmsg = [content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  297. [self.content_data addObject:[NSMutableDictionary dictionaryWithDictionary:objmsg]];
  298. }
  299. if(count<self.limit)
  300. {
  301. // int i =self.btnrefresh.state;
  302. self.load_more_hint=@"All loaded";
  303. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  304. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  305. }
  306. else
  307. {
  308. self.load_more_hint=@"Load more...";
  309. // self.btnrefresh.enabled = true;
  310. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  311. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  312. }
  313. self.table_order.hidden = false;
  314. [self.table_order reloadData ];
  315. }
  316. else if(result==RESULT_NET_ERROR &&self.offset==0)
  317. {
  318. self.label_net_err.hidden=false;
  319. self.table_order.hidden = true;
  320. }
  321. else
  322. {
  323. [RAUtils message_alert:[content valueForKey:@"err_msg"] title:@"Loading Order List" controller:self] ;
  324. }
  325. self.isrefreshing=false;
  326. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  327. // bool autologin =[defaults boolForKey:@"autologin"];
  328. // if(!autologin)
  329. // {
  330. // TabBarController * viewController =[self.storyboard instantiateViewControllerWithIdentifier:@"LoginTabBar"];
  331. // [self presentViewController:viewController animated:YES completion:^{
  332. //
  333. // }];
  334. // }
  335. });
  336. });
  337. });
  338. }
  339. - (void)loadmore
  340. {
  341. // self.btnrefresh.enabled = NO;
  342. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"loading",nil);
  343. // [self.btnrefresh setTitle:@"Loading..." forState:UIControlStateNormal];
  344. [self loadpage];
  345. }
  346. //-(void)changeCell
  347. //{
  348. //// UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  349. //// reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  350. // [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  351. // DebugLog(@"refresh!!!!!!!!");
  352. //
  353. //}
  354. -(void) neworder
  355. {
  356. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  357. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  358. NSDictionary* return_json = [iSalesNetwork new_Order];
  359. dispatch_async(dispatch_get_main_queue(), ^{
  360. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  361. if([[return_json valueForKey:@"result"] intValue]==2)
  362. {
  363. int result=[[return_json valueForKey:@"result"] intValue];
  364. if(result==2)
  365. {
  366. //successed.
  367. NSString* order_code = [return_json valueForKey:@"orderCode"];
  368. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  369. appDelegate.order_code = order_code;
  370. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  371. [main_vc reloadCart:true immediately:false];
  372. [self ReloadData];
  373. // [self performSelector:@selector(loadpage) withObject:nil afterDelay:1];
  374. // [self loadpage];
  375. // if(self.shopCartBlock!=nil)
  376. // {
  377. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  378. //
  379. //
  380. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  381. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  382. // iv.image = img;
  383. // // [self.contentView addSubview:iv];
  384. // self.shopCartBlock(iv);
  385. //
  386. // }
  387. }
  388. }
  389. else
  390. {
  391. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:self] ;
  392. }
  393. });
  394. });
  395. }
  396. #pragma mark - UIAlertViewDelegate
  397. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  398. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  399. {
  400. if(buttonIndex!=alertView.cancelButtonIndex)
  401. {
  402. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  403. if(appDelegate.order_code.length>0)
  404. {
  405. appDelegate.order_code= nil;
  406. [appDelegate SetSo:nil];
  407. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  408. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  409. cvc.showNavibar = true;
  410. cvc.contact_type = @"Sales_Order_Customer";
  411. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  412. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  413. appDelegate.customerInfo = value;
  414. self.norefresh = true;
  415. [self neworder];
  416. // [main_vc checklogin:true];
  417. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  418. //
  419. // if(self.returnValue)
  420. // self.returnValue(value);
  421. };
  422. cvc.onCancel = ^(){
  423. self.norefresh = true;
  424. if(appDelegate.customerInfo==nil)
  425. {
  426. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  427. }
  428. else
  429. {
  430. [self neworder];
  431. }
  432. // [main_vc checklogin:true];
  433. };
  434. cvc.onReset = ^(){
  435. // [main_vc checklogin:true];
  436. };
  437. [self.navigationController pushViewController:cvc animated:true];
  438. }
  439. else
  440. {
  441. //create new;
  442. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  443. {
  444. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  445. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  446. cvc.showNavibar = true;
  447. cvc.contact_type = @"Sales_Order_Customer";
  448. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  449. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  450. appDelegate.customerInfo = value;
  451. if(appDelegate.order_code==nil)
  452. [self neworder];
  453. // [main_vc checklogin:true];
  454. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  455. //
  456. // if(self.returnValue)
  457. // self.returnValue(value);
  458. };
  459. cvc.onCancel = ^(){
  460. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  461. };
  462. cvc.onReset = ^(){
  463. [main_vc checklogin:true];
  464. };
  465. [self.navigationController pushViewController:cvc animated:true];
  466. }
  467. else
  468. {
  469. [self neworder];
  470. }
  471. }
  472. }
  473. }
  474. #pragma mark - Table view data source
  475. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  476. {
  477. return 66;
  478. }
  479. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  480. //{
  481. // return 0;
  482. //}
  483. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  484. //{
  485. // return 0;
  486. //}
  487. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  488. //{
  489. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  490. // return myView;
  491. //
  492. //}
  493. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  494. {
  495. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  496. myView.backgroundColor = UIColorFromRGB(0x996633);
  497. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  498. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  499. myView.layer.shadowOffset = CGSizeMake(0, 0);
  500. myView.layer.shadowOpacity = 0.5;
  501. myView.layer.shadowRadius = 2.0;
  502. UILabel *statuslabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 2, 130, 22)];
  503. statuslabel.textColor=[UIColor whiteColor];
  504. statuslabel.backgroundColor = [UIColor clearColor];
  505. statuslabel.text=NSLocalizedString(@"Status", nil);
  506. [statuslabel sizeToFit];
  507. [myView addSubview:statuslabel];
  508. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 21, 130, 22)];
  509. solabel.textColor=[UIColor whiteColor];
  510. solabel.backgroundColor = [UIColor clearColor];
  511. solabel.text=NSLocalizedString(@"SO#", nil);
  512. [solabel sizeToFit];
  513. [myView addSubview:solabel];
  514. UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 2, 135, 22)];
  515. userlabel.textColor=[UIColor whiteColor];
  516. userlabel.backgroundColor = [UIColor clearColor];
  517. userlabel.text=NSLocalizedString(@"Sales rep", nil);
  518. [userlabel sizeToFit];
  519. [myView addSubview:userlabel];
  520. UILabel *creatorlabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 21, 135, 22)];
  521. creatorlabel.textColor=[UIColor whiteColor];
  522. creatorlabel.backgroundColor = [UIColor clearColor];
  523. creatorlabel.text=NSLocalizedString(@"Create by", nil);
  524. [creatorlabel sizeToFit];
  525. [myView addSubview:creatorlabel];
  526. UILabel *contactlabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 11, 150, 22)];
  527. contactlabel.textColor=[UIColor whiteColor];
  528. contactlabel.backgroundColor = [UIColor clearColor];
  529. contactlabel.text=NSLocalizedString(@"Contact", nil);
  530. [contactlabel sizeToFit];
  531. [myView addSubview:contactlabel];
  532. //
  533. // UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(424, 11, 52, 22)];
  534. // modellabel.textColor=[UIColor whiteColor];
  535. // modellabel.backgroundColor = [UIColor clearColor];
  536. // modellabel.text=NSLocalizedString(@"Model", nil);
  537. // [modellabel sizeToFit];
  538. // [myView addSubview:modellabel];
  539. // UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(self.table_order.frame.size.width-276, 2, 100, 22)];
  540. // modellabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  541. // modellabel.textColor=[UIColor whiteColor];
  542. // modellabel.backgroundColor = [UIColor clearColor];
  543. // modellabel.text=NSLocalizedString(@"Items/QTY", nil);
  544. // [modellabel sizeToFit];
  545. // [myView addSubview:modellabel];
  546. UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(/*492*/self.table_order.frame.size.width-276, 11, 100, 22)];
  547. pricelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  548. pricelabel.textColor=[UIColor whiteColor];
  549. pricelabel.backgroundColor = [UIColor clearColor];
  550. pricelabel.text=NSLocalizedString(@"Order Total", nil);
  551. [pricelabel sizeToFit];
  552. [myView addSubview:pricelabel];
  553. UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(self.table_order.frame.size.width-163/*605*/,11, 148, 22)];
  554. timelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  555. timelabel.textColor=[UIColor whiteColor];
  556. timelabel.backgroundColor = [UIColor clearColor];
  557. if(self.time_zone==nil)
  558. timelabel.text=@"Create time";
  559. else
  560. timelabel.text=[NSString stringWithFormat:@"Create time (%@)",self.time_zone];
  561. [timelabel sizeToFit];
  562. [myView addSubview:timelabel];
  563. myView.autoresizesSubviews=true;
  564. // [myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
  565. // modellabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  566. // pricelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  567. // timelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  568. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  569. //
  570. return myView;
  571. }
  572. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  573. {
  574. return 44;
  575. }
  576. //
  577. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  578. // if(section==0)
  579. // return nil;
  580. // else
  581. // return @"detail section";
  582. //}
  583. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  584. {
  585. return 1;
  586. }
  587. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  588. {
  589. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  590. if( self.content_data.count==0)
  591. return 0;
  592. return self.content_data.count+1;
  593. }
  594. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  595. {
  596. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  597. // if(tableView==self.itemListTable)
  598. // {
  599. // if(indexPath.row>self.content_data.count)
  600. // {
  601. // int debug=true;
  602. // }
  603. // NSLog(@"row %ld",(long)indexPath.row);
  604. if(indexPath.row==self.content_data.count)
  605. {
  606. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  607. UILabel *moreDataLabel=[[UILabel alloc] init];
  608. moreDataLabel.tag=200;
  609. moreDataLabel.text=self.load_more_hint;
  610. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  611. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  612. moreDataLabel.frame=CGRectMake(0, 10, self.table_order.bounds.size.width, 20);
  613. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  614. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  615. [moreCell.contentView addSubview:moreDataLabel];
  616. if([self.load_more_hint isEqualToString:@"Load more..."])
  617. [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  618. // NSLog(@"更多...");
  619. moreCell.backgroundColor = [UIColor whiteColor];
  620. return moreCell;
  621. }
  622. else
  623. {
  624. NSString *CellIdentifier = @"OrderListTableViewCell";
  625. OrderListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  626. NSString* po= [self.content_data[indexPath.row] valueForKey:@"so#"];
  627. NSString* price= [self.content_data[indexPath.row] valueForKey:@"price"];
  628. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  629. {
  630. }
  631. else
  632. {
  633. price=nil;
  634. }
  635. NSString* purchase_time= [self.content_data[indexPath.row] valueForKey:@"purchase_time"];
  636. NSString* status = [self.content_data[indexPath.row] valueForKey:@"order_status"] ;
  637. NSString* customer_name = [self.content_data[indexPath.row] valueForKey:@"customer_name"];
  638. NSString* model_count = [self.content_data[indexPath.row] valueForKey:@"model_count"];
  639. NSString* sales = [self.content_data[indexPath.row] valueForKey:@"sales_rep"];
  640. NSString* creator = [self.content_data[indexPath.row] valueForKey:@"create_by"];
  641. cell.label_so.text=po;
  642. cell.labelprice.text=price;
  643. cell.label_date.text=purchase_time;
  644. cell.label_contact.text=customer_name;
  645. cell.label_model.text = model_count;
  646. cell.label_sales.text = sales;
  647. cell.label_creator.text = creator;
  648. cell.label_status.text=status;
  649. cell.backgroundColor = [UIColor whiteColor];
  650. return cell;
  651. }
  652. // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  653. // NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  654. // cell.bundle_item=combine_json;
  655. // NSString* img_url = [item_json valueForKey:@"img_url"];
  656. // NSString* description = [item_json valueForKey:@"description"];
  657. // // NSString* identifier = [item_json valueForKey:@"identifier"];
  658. // // NSString* attribute = [item_json valueForKey:@"attribute"];
  659. // NSString* currency = [item_json valueForKey:@"currency"];
  660. // int count =[[item_json valueForKey:@"count"] intValue];
  661. // double price =[[item_json valueForKey:@"price"] doubleValue];
  662. // // cell.labelAttribute.text = attribute;
  663. // cell.labelCurrency.text = currency;
  664. // cell.labelDescription.text = description;
  665. // // cell.labelIdentifier.text = identifier;
  666. // cell.labelPrice.text = [NSString stringWithFormat:@"%.2f",price];
  667. //
  668. //
  669. //
  670. // [cell set_Count:count];
  671. // NSString* file_name=[img_url lastPathComponent];
  672. // NSData* img_data=[iSalesDB load_cached_img:file_name];
  673. // if(img_data!=nil)
  674. // {
  675. //
  676. // UIImage * img =[UIImage imageWithData:img_data];
  677. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  678. // }
  679. // else
  680. // {
  681. //
  682. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  683. //
  684. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  685. //
  686. // dispatch_async(dispatch_get_main_queue(), ^{
  687. //
  688. //
  689. //
  690. // if(downloadimg_data!=nil)
  691. // {
  692. // [iSalesDB cache_img:downloadimg_data :file_name ];
  693. //
  694. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  695. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  696. // }
  697. //
  698. // });
  699. // });
  700. //
  701. //
  702. // }
  703. // }
  704. // else
  705. // {
  706. // NSString *CellIdentifier = @"OrderInfoListItem";
  707. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  708. // return cell;
  709. // }
  710. }
  711. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  712. {
  713. //
  714. if(indexPath.row==self.content_data.count)
  715. return;
  716. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  717. OrderDetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController" ];
  718. // int i =[[self.content_data[indexPath.row] valueForKey:@"order_code"];
  719. dvc.selectOrder=^(NSMutableDictionary* order_detail){
  720. [self.navigationController popViewControllerAnimated:false];
  721. if(self.selectOrder)
  722. self.selectOrder(order_detail);
  723. };
  724. NSString* order_code =[self.content_data[indexPath.row] valueForKey:@"order_code"];
  725. dvc.order_code = order_code;
  726. // bool temp_order = false ;
  727. // NSString* order_status=[self.content_data[indexPath.row] valueForKey:@"order_status"];
  728. // if([order_status isEqualToString:@"Saved"]||[order_status isEqualToString:@"Quoted"])
  729. // temp_order=true;
  730. //
  731. // dvc.isTempOrder = temp_order;
  732. // dvc
  733. dvc.orderid=orderid;
  734. // dvc.category_id=nil;
  735. // dvc.ispush=true;
  736. // [dvc reload];
  737. [self.navigationController pushViewController:dvc animated:true];
  738. }
  739. #pragma mark - searchBar delegate;
  740. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  741. {
  742. self.keywords=searchBar.text;
  743. self.offset = 0;
  744. [self.content_data removeAllObjects];
  745. [self loadpage];
  746. }
  747. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  748. {
  749. if([searchText isEqualToString:@""] && !self.reset)
  750. {
  751. NSLog(@"clear");
  752. self.reset=true;
  753. self.keywords=nil;
  754. self.offset = 0;
  755. [self.content_data removeAllObjects];
  756. [self loadpage];
  757. }
  758. else
  759. self.reset = false;
  760. }
  761. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  762. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  763. {
  764. // 添加一个删除按钮
  765. // self.indexPath=indexPath;
  766. if(indexPath.row==self.content_data.count)
  767. return @[];
  768. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  769. if(appDelegate.user_type!=USER_ROLE_EMPLOYEE)
  770. return @[];
  771. NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"];
  772. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  773. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  774. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  775. NSLog(@"delete click");
  776. NSString * title =[NSString stringWithFormat:@"Are you sure to delete order SO#: %@",so];
  777. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  778. //block代码块取代了delegate
  779. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  780. // textField.text = self.save_name;
  781. //
  782. //
  783. // }];
  784. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  785. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"];
  786. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  787. NSDictionary* return_json = [iSalesNetwork delete_Order:orderid];
  788. dispatch_async(dispatch_get_main_queue(), ^{
  789. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  790. if([[return_json valueForKey:@"result"] intValue]==2)
  791. {
  792. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  793. {
  794. appDelegate.order_code= nil;
  795. [appDelegate SetSo:nil];
  796. }
  797. [RAUtils message_alert:nil title:@"Order Delete" controller:self] ;
  798. [self ReloadData];
  799. }
  800. else
  801. {
  802. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order" controller:self] ;
  803. }
  804. });
  805. });
  806. }];
  807. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  808. NSLog(@"Cancel");
  809. }];
  810. [alertControl addAction:actionOne];
  811. [alertControl addAction:alertthree];
  812. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  813. [self presentViewController:alertControl animated:YES completion:nil];
  814. }];
  815. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  816. // 添加一个编辑按钮
  817. UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  818. NSLog(@"edit click");
  819. NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so];
  820. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  821. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  822. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  823. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  824. NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid];
  825. dispatch_async(dispatch_get_main_queue(), ^{
  826. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  827. if([[return_json valueForKey:@"result"] intValue]==2)
  828. {
  829. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  830. {
  831. appDelegate.order_code= nil;
  832. [appDelegate SetSo:nil];
  833. }
  834. [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  835. [self ReloadData];
  836. }
  837. else
  838. {
  839. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  840. }
  841. });
  842. });
  843. }];
  844. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  845. NSLog(@"Cancel");
  846. }];
  847. [alertControl addAction:actionOne];
  848. [alertControl addAction:alertthree];
  849. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  850. [self presentViewController:alertControl animated:YES completion:nil];
  851. //
  852. }];
  853. cancelAction.backgroundColor = UIColorFromRGB(0xff9933);
  854. if([order_status isEqualToString:@"Saved Order"]&&appDelegate.can_cancel_order)
  855. return @[cancelAction];
  856. else
  857. {
  858. if([order_status isEqualToString:@"Quote Saved"])
  859. {
  860. NSMutableArray* arr=[@[] mutableCopy];
  861. if(appDelegate.can_delete_order)
  862. [arr addObject:deleteRowAction];
  863. if(appDelegate.can_cancel_order)
  864. [arr addObject:cancelAction];
  865. return arr;
  866. }
  867. }
  868. return @[];
  869. }
  870. - (void)tableView:(UITableView *)tableView
  871. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  872. }
  873. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  874. if(indexPath.row==self.content_data.count)
  875. return UITableViewCellEditingStyleNone;
  876. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  877. if(![order_status isEqualToString:@"Saved Order"]&&! [order_status isEqualToString:@"Quote Saved"])
  878. return UITableViewCellEditingStyleNone;
  879. else
  880. return UITableViewCellEditingStyleDelete;
  881. }
  882. /*
  883. #pragma mark - Navigation
  884. // In a storyboard-based application, you will often want to do a little preparation before navigation
  885. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  886. // Get the new view controller using [segue destinationViewController].
  887. // Pass the selected object to the new view controller.
  888. }
  889. */
  890. #pragma mark EnumSelectViewControllerDelegate
  891. -(void) EnumValueChanged:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  892. {
  893. self.status_cadedate = value;
  894. UIApplication * app = [UIApplication sharedApplication];
  895. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  896. appDelegate.OrderFilter = value;
  897. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  898. // NSString *documents = [paths objectAtIndex:0];
  899. // NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  900. //
  901. //
  902. // // if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  903. // //
  904. // // NSLog(@"文件已经存在了");
  905. // //
  906. // // }
  907. // // else
  908. // // {
  909. // //
  910. // //
  911. // //
  912. // // NSLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  913. //
  914. //NSData *data = [NSJSONSerialization dataWithJSONObject:self.status_cadedate options:kNilOptions error:nil];
  915. //
  916. // // NSLog(@"mainBundleFile==%@",mainBundleFile);
  917. //
  918. // [[NSFileManager defaultManager] createFileAtPath:dist_path
  919. //
  920. // contents:data
  921. //
  922. // attributes:nil];
  923. //
  924. // }
  925. // self.keywords=searchBar.text;
  926. self.offset = 0;
  927. [self.content_data removeAllObjects];
  928. [self loadpage];
  929. //[self.btnStatusFilter setTitle:setTitle:alert ];
  930. }
  931. @end