OrderListViewController.m 57 KB

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