OrderListViewController.m 59 KB

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