OrderListViewController.m 67 KB

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