OrderListViewController.m 56 KB

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