OrderListViewController.m 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  371. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  372. [main_vc reloadCart:true immediately:false];
  373. [self ReloadData];
  374. // [self performSelector:@selector(loadpage) withObject:nil afterDelay:1];
  375. // [self loadpage];
  376. // if(self.shopCartBlock!=nil)
  377. // {
  378. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  379. //
  380. //
  381. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  382. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  383. // iv.image = img;
  384. // // [self.contentView addSubview:iv];
  385. // self.shopCartBlock(iv);
  386. //
  387. // }
  388. }
  389. }
  390. else
  391. {
  392. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:self] ;
  393. }
  394. });
  395. });
  396. }
  397. #pragma mark - UIAlertViewDelegate
  398. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  399. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  400. {
  401. if(buttonIndex!=alertView.cancelButtonIndex)
  402. {
  403. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  404. if(appDelegate.order_code.length>0)
  405. {
  406. appDelegate.order_code= nil;
  407. [appDelegate SetSo:nil];
  408. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  409. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  410. cvc.showNavibar = true;
  411. cvc.contact_type = @"Sales_Order_Customer";
  412. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  413. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  414. appDelegate.customerInfo = value;
  415. self.norefresh = true;
  416. [self neworder];
  417. // [main_vc checklogin:true];
  418. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  419. //
  420. // if(self.returnValue)
  421. // self.returnValue(value);
  422. };
  423. cvc.onCancel = ^(){
  424. self.norefresh = true;
  425. if(appDelegate.customerInfo==nil)
  426. {
  427. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  428. }
  429. else
  430. {
  431. [self neworder];
  432. }
  433. // [main_vc checklogin:true];
  434. };
  435. cvc.onReset = ^(){
  436. // [main_vc checklogin:true];
  437. };
  438. [self.navigationController pushViewController:cvc animated:true];
  439. }
  440. else
  441. {
  442. //create new;
  443. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  444. {
  445. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  446. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  447. cvc.showNavibar = true;
  448. cvc.contact_type = @"Sales_Order_Customer";
  449. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  450. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  451. appDelegate.customerInfo = value;
  452. if(appDelegate.order_code==nil)
  453. [self neworder];
  454. // [main_vc checklogin:true];
  455. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  456. //
  457. // if(self.returnValue)
  458. // self.returnValue(value);
  459. };
  460. cvc.onCancel = ^(){
  461. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  462. };
  463. cvc.onReset = ^(){
  464. [main_vc checklogin:true];
  465. };
  466. [self.navigationController pushViewController:cvc animated:true];
  467. }
  468. else
  469. {
  470. [self neworder];
  471. }
  472. }
  473. }
  474. }
  475. #pragma mark - Table view data source
  476. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  477. {
  478. return 66;
  479. }
  480. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  481. //{
  482. // return 0;
  483. //}
  484. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  485. //{
  486. // return 0;
  487. //}
  488. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  489. //{
  490. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  491. // return myView;
  492. //
  493. //}
  494. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  495. {
  496. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  497. myView.backgroundColor = UIColorFromRGB(0x996633);
  498. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  499. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  500. myView.layer.shadowOffset = CGSizeMake(0, 0);
  501. myView.layer.shadowOpacity = 0.5;
  502. myView.layer.shadowRadius = 2.0;
  503. UILabel *statuslabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 2, 130, 22)];
  504. statuslabel.textColor=[UIColor whiteColor];
  505. statuslabel.backgroundColor = [UIColor clearColor];
  506. statuslabel.text=NSLocalizedString(@"Status", nil);
  507. [statuslabel sizeToFit];
  508. [myView addSubview:statuslabel];
  509. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 21, 130, 22)];
  510. solabel.textColor=[UIColor whiteColor];
  511. solabel.backgroundColor = [UIColor clearColor];
  512. solabel.text=NSLocalizedString(@"SO#", nil);
  513. [solabel sizeToFit];
  514. [myView addSubview:solabel];
  515. UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 2, 135, 22)];
  516. userlabel.textColor=[UIColor whiteColor];
  517. userlabel.backgroundColor = [UIColor clearColor];
  518. userlabel.text=NSLocalizedString(@"Sales rep", nil);
  519. [userlabel sizeToFit];
  520. [myView addSubview:userlabel];
  521. UILabel *creatorlabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 21, 135, 22)];
  522. creatorlabel.textColor=[UIColor whiteColor];
  523. creatorlabel.backgroundColor = [UIColor clearColor];
  524. creatorlabel.text=NSLocalizedString(@"Create by", nil);
  525. [creatorlabel sizeToFit];
  526. [myView addSubview:creatorlabel];
  527. UILabel *contactlabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 11, 150, 22)];
  528. contactlabel.textColor=[UIColor whiteColor];
  529. contactlabel.backgroundColor = [UIColor clearColor];
  530. contactlabel.text=NSLocalizedString(@"Contact", nil);
  531. [contactlabel sizeToFit];
  532. [myView addSubview:contactlabel];
  533. //
  534. // UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(424, 11, 52, 22)];
  535. // modellabel.textColor=[UIColor whiteColor];
  536. // modellabel.backgroundColor = [UIColor clearColor];
  537. // modellabel.text=NSLocalizedString(@"Model", nil);
  538. // [modellabel sizeToFit];
  539. // [myView addSubview:modellabel];
  540. // UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(self.table_order.frame.size.width-276, 2, 100, 22)];
  541. // modellabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  542. // modellabel.textColor=[UIColor whiteColor];
  543. // modellabel.backgroundColor = [UIColor clearColor];
  544. // modellabel.text=NSLocalizedString(@"Items/QTY", nil);
  545. // [modellabel sizeToFit];
  546. // [myView addSubview:modellabel];
  547. UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(/*492*/self.table_order.frame.size.width-276, 11, 100, 22)];
  548. pricelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  549. pricelabel.textColor=[UIColor whiteColor];
  550. pricelabel.backgroundColor = [UIColor clearColor];
  551. pricelabel.text=NSLocalizedString(@"Order Total", nil);
  552. [pricelabel sizeToFit];
  553. [myView addSubview:pricelabel];
  554. UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(self.table_order.frame.size.width-163/*605*/,11, 148, 22)];
  555. timelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  556. timelabel.textColor=[UIColor whiteColor];
  557. timelabel.backgroundColor = [UIColor clearColor];
  558. if(self.time_zone==nil)
  559. timelabel.text=@"Create time";
  560. else
  561. timelabel.text=[NSString stringWithFormat:@"Create time (%@)",self.time_zone];
  562. [timelabel sizeToFit];
  563. [myView addSubview:timelabel];
  564. myView.autoresizesSubviews=true;
  565. // [myView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
  566. // modellabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  567. // pricelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  568. // timelabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  569. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  570. //
  571. return myView;
  572. }
  573. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  574. {
  575. return 44;
  576. }
  577. //
  578. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  579. // if(section==0)
  580. // return nil;
  581. // else
  582. // return @"detail section";
  583. //}
  584. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  585. {
  586. return 1;
  587. }
  588. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  589. {
  590. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  591. if( self.content_data.count==0)
  592. return 0;
  593. return self.content_data.count+1;
  594. }
  595. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  596. {
  597. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  598. // if(tableView==self.itemListTable)
  599. // {
  600. // if(indexPath.row>self.content_data.count)
  601. // {
  602. // int debug=true;
  603. // }
  604. // NSLog(@"row %ld",(long)indexPath.row);
  605. if(indexPath.row==self.content_data.count)
  606. {
  607. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  608. UILabel *moreDataLabel=[[UILabel alloc] init];
  609. moreDataLabel.tag=200;
  610. moreDataLabel.text=self.load_more_hint;
  611. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  612. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  613. moreDataLabel.frame=CGRectMake(0, 10, self.table_order.bounds.size.width, 20);
  614. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  615. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  616. [moreCell.contentView addSubview:moreDataLabel];
  617. if([self.load_more_hint isEqualToString:@"Load more..."])
  618. [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  619. // NSLog(@"更多...");
  620. moreCell.backgroundColor = [UIColor whiteColor];
  621. return moreCell;
  622. }
  623. else
  624. {
  625. NSString *CellIdentifier = @"OrderListTableViewCell";
  626. OrderListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  627. NSString* po= [self.content_data[indexPath.row] valueForKey:@"so#"];
  628. NSString* price= [self.content_data[indexPath.row] valueForKey:@"price"];
  629. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  630. {
  631. }
  632. else
  633. {
  634. price=nil;
  635. }
  636. NSString* purchase_time= [self.content_data[indexPath.row] valueForKey:@"purchase_time"];
  637. NSString* status = [self.content_data[indexPath.row] valueForKey:@"order_status"] ;
  638. NSString* customer_name = [self.content_data[indexPath.row] valueForKey:@"customer_name"];
  639. NSString* model_count = [self.content_data[indexPath.row] valueForKey:@"model_count"];
  640. NSString* sales = [self.content_data[indexPath.row] valueForKey:@"sales_rep"];
  641. NSString* creator = [self.content_data[indexPath.row] valueForKey:@"create_by"];
  642. cell.label_so.text=po;
  643. cell.labelprice.text=price;
  644. cell.label_date.text=purchase_time;
  645. cell.label_contact.text=customer_name;
  646. cell.label_model.text = model_count;
  647. cell.label_sales.text = sales;
  648. cell.label_creator.text = creator;
  649. cell.label_status.text=status;
  650. cell.backgroundColor = [UIColor whiteColor];
  651. return cell;
  652. }
  653. // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  654. // NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  655. // cell.bundle_item=combine_json;
  656. // NSString* img_url = [item_json valueForKey:@"img_url"];
  657. // NSString* description = [item_json valueForKey:@"description"];
  658. // // NSString* identifier = [item_json valueForKey:@"identifier"];
  659. // // NSString* attribute = [item_json valueForKey:@"attribute"];
  660. // NSString* currency = [item_json valueForKey:@"currency"];
  661. // int count =[[item_json valueForKey:@"count"] intValue];
  662. // double price =[[item_json valueForKey:@"price"] doubleValue];
  663. // // cell.labelAttribute.text = attribute;
  664. // cell.labelCurrency.text = currency;
  665. // cell.labelDescription.text = description;
  666. // // cell.labelIdentifier.text = identifier;
  667. // cell.labelPrice.text = [NSString stringWithFormat:@"%.2f",price];
  668. //
  669. //
  670. //
  671. // [cell set_Count:count];
  672. // NSString* file_name=[img_url lastPathComponent];
  673. // NSData* img_data=[iSalesDB load_cached_img:file_name];
  674. // if(img_data!=nil)
  675. // {
  676. //
  677. // UIImage * img =[UIImage imageWithData:img_data];
  678. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  679. // }
  680. // else
  681. // {
  682. //
  683. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  684. //
  685. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  686. //
  687. // dispatch_async(dispatch_get_main_queue(), ^{
  688. //
  689. //
  690. //
  691. // if(downloadimg_data!=nil)
  692. // {
  693. // [iSalesDB cache_img:downloadimg_data :file_name ];
  694. //
  695. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  696. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  697. // }
  698. //
  699. // });
  700. // });
  701. //
  702. //
  703. // }
  704. // }
  705. // else
  706. // {
  707. // NSString *CellIdentifier = @"OrderInfoListItem";
  708. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  709. // return cell;
  710. // }
  711. }
  712. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  713. {
  714. //
  715. if(indexPath.row==self.content_data.count)
  716. return;
  717. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  718. OrderDetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController" ];
  719. // int i =[[self.content_data[indexPath.row] valueForKey:@"order_code"];
  720. dvc.selectOrder=^(NSMutableDictionary* order_detail){
  721. [self.navigationController popViewControllerAnimated:false];
  722. if(self.selectOrder)
  723. self.selectOrder(order_detail);
  724. };
  725. NSString* order_code =[self.content_data[indexPath.row] valueForKey:@"order_code"];
  726. dvc.order_code = order_code;
  727. dvc.order_status =[[self.content_data[indexPath.row] valueForKey:@"orderStatus"] intValue];
  728. // bool temp_order = false ;
  729. // NSString* order_status=[self.content_data[indexPath.row] valueForKey:@"order_status"];
  730. // if([order_status isEqualToString:@"Saved"]||[order_status isEqualToString:@"Quoted"])
  731. // temp_order=true;
  732. //
  733. // dvc.isTempOrder = temp_order;
  734. // dvc
  735. dvc.orderid=orderid;
  736. // dvc.category_id=nil;
  737. // dvc.ispush=true;
  738. // [dvc reload];
  739. [self.navigationController pushViewController:dvc animated:true];
  740. }
  741. #pragma mark - searchBar delegate;
  742. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  743. {
  744. self.keywords=searchBar.text;
  745. self.offset = 0;
  746. [self.content_data removeAllObjects];
  747. [self loadpage];
  748. }
  749. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  750. {
  751. if([searchText isEqualToString:@""] && !self.reset)
  752. {
  753. NSLog(@"clear");
  754. self.reset=true;
  755. self.keywords=nil;
  756. self.offset = 0;
  757. [self.content_data removeAllObjects];
  758. [self loadpage];
  759. }
  760. else
  761. self.reset = false;
  762. }
  763. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  764. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  765. {
  766. // 添加一个删除按钮
  767. // self.indexPath=indexPath;
  768. if(indexPath.row==self.content_data.count)
  769. return @[];
  770. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  771. if(appDelegate.user_type!=USER_ROLE_EMPLOYEE)
  772. return @[];
  773. NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"];
  774. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  775. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  776. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  777. NSLog(@"delete click");
  778. NSString * title =[NSString stringWithFormat:@"Are you sure to delete order SO#: %@",so];
  779. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  780. //block代码块取代了delegate
  781. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  782. // textField.text = self.save_name;
  783. //
  784. //
  785. // }];
  786. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  787. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"];
  788. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  789. NSDictionary* return_json = [iSalesNetwork delete_Order:orderid];
  790. dispatch_async(dispatch_get_main_queue(), ^{
  791. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  792. if([[return_json valueForKey:@"result"] intValue]==2)
  793. {
  794. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  795. {
  796. appDelegate.order_code= nil;
  797. [appDelegate SetSo:nil];
  798. }
  799. [RAUtils message_alert:nil title:@"Order Delete" controller:self] ;
  800. [self ReloadData];
  801. }
  802. else
  803. {
  804. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order" controller:self] ;
  805. }
  806. });
  807. });
  808. }];
  809. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  810. NSLog(@"Cancel");
  811. }];
  812. [alertControl addAction:actionOne];
  813. [alertControl addAction:alertthree];
  814. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  815. [self presentViewController:alertControl animated:YES completion:nil];
  816. }];
  817. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  818. // 添加一个编辑按钮
  819. UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  820. NSLog(@"edit click");
  821. NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so];
  822. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  823. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  824. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  825. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  826. NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid];
  827. dispatch_async(dispatch_get_main_queue(), ^{
  828. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  829. if([[return_json valueForKey:@"result"] intValue]==2)
  830. {
  831. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  832. {
  833. appDelegate.order_code= nil;
  834. [appDelegate SetSo:nil];
  835. }
  836. [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  837. [self ReloadData];
  838. }
  839. else
  840. {
  841. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  842. }
  843. });
  844. });
  845. }];
  846. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  847. NSLog(@"Cancel");
  848. }];
  849. [alertControl addAction:actionOne];
  850. [alertControl addAction:alertthree];
  851. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  852. [self presentViewController:alertControl animated:YES completion:nil];
  853. //
  854. }];
  855. cancelAction.backgroundColor = UIColorFromRGB(0xff9933);
  856. if([order_status isEqualToString:@"Saved Order"]&&appDelegate.can_cancel_order)
  857. return @[cancelAction];
  858. else
  859. {
  860. if([order_status isEqualToString:@"Quote Saved"])
  861. {
  862. NSMutableArray* arr=[@[] mutableCopy];
  863. if(appDelegate.can_delete_order)
  864. [arr addObject:deleteRowAction];
  865. if(appDelegate.can_cancel_order)
  866. [arr addObject:cancelAction];
  867. return arr;
  868. }
  869. }
  870. return @[];
  871. }
  872. - (void)tableView:(UITableView *)tableView
  873. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  874. }
  875. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  876. if(indexPath.row==self.content_data.count)
  877. return UITableViewCellEditingStyleNone;
  878. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  879. if(![order_status isEqualToString:@"Saved Order"]&&! [order_status isEqualToString:@"Quote Saved"])
  880. return UITableViewCellEditingStyleNone;
  881. else
  882. return UITableViewCellEditingStyleDelete;
  883. }
  884. /*
  885. #pragma mark - Navigation
  886. // In a storyboard-based application, you will often want to do a little preparation before navigation
  887. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  888. // Get the new view controller using [segue destinationViewController].
  889. // Pass the selected object to the new view controller.
  890. }
  891. */
  892. #pragma mark EnumSelectViewControllerDelegate
  893. -(void) EnumValueChanged:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  894. {
  895. self.status_cadedate = value;
  896. UIApplication * app = [UIApplication sharedApplication];
  897. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  898. appDelegate.OrderFilter = value;
  899. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  900. // NSString *documents = [paths objectAtIndex:0];
  901. // NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  902. //
  903. //
  904. // // if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  905. // //
  906. // // NSLog(@"文件已经存在了");
  907. // //
  908. // // }
  909. // // else
  910. // // {
  911. // //
  912. // //
  913. // //
  914. // // NSLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  915. //
  916. //NSData *data = [NSJSONSerialization dataWithJSONObject:self.status_cadedate options:kNilOptions error:nil];
  917. //
  918. // // NSLog(@"mainBundleFile==%@",mainBundleFile);
  919. //
  920. // [[NSFileManager defaultManager] createFileAtPath:dist_path
  921. //
  922. // contents:data
  923. //
  924. // attributes:nil];
  925. //
  926. // }
  927. // self.keywords=searchBar.text;
  928. self.offset = 0;
  929. [self.content_data removeAllObjects];
  930. [self loadpage];
  931. //[self.btnStatusFilter setTitle:setTitle:alert ];
  932. }
  933. @end