OrderListViewController.m 61 KB

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