OrderListViewController.m 61 KB

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