OrderListViewController.m 58 KB

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