OrderListViewController.m 68 KB

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