OrderListViewController.m 61 KB

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