OrderListViewController.m 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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. // DebugLog(@"文件已经存在了");
  114. //
  115. // }
  116. // else
  117. // {
  118. //
  119. //
  120. //
  121. // // DebugLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  122. //
  123. // NSData *mainBundleFile = [NSData dataWithContentsOfFile:order_filter_source_path];
  124. //
  125. // // DebugLog(@"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. // DebugLog(@"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. // DebugLog(@"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. // DebugLog(@"更多...");
  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. DebugLog(@"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. DebugLog(@"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. DebugLog(@"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. DebugLog(@"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. DebugLog(@"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. // Cancel Order Action
  1096. UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel Order"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1097. DebugLog(@"edit click");
  1098. NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so];
  1099. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  1100. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1101. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  1102. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1103. NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid order_code:nil];
  1104. dispatch_async(dispatch_get_main_queue(), ^{
  1105. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1106. if([[return_json valueForKey:@"result"] intValue]==2)
  1107. {
  1108. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  1109. {
  1110. appDelegate.order_code= nil;
  1111. //appDelegate.user_type = 0;
  1112. appDelegate.customerInfo = nil;
  1113. appDelegate.contact_id = nil;
  1114. appDelegate.order_customer_id = nil;
  1115. [appDelegate SetSo:nil];
  1116. appDelegate.cart_count=0;
  1117. [appDelegate update_count_mark];
  1118. }
  1119. [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  1120. [self ReloadData];
  1121. }
  1122. else
  1123. {
  1124. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  1125. }
  1126. });
  1127. });
  1128. }];
  1129. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1130. DebugLog(@"Cancel");
  1131. }];
  1132. [alertControl addAction:actionOne];
  1133. [alertControl addAction:alertthree];
  1134. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1135. [self presentViewController:alertControl animated:YES completion:nil];
  1136. //
  1137. }];
  1138. cancelAction.backgroundColor = UIColorFromRGB(0xff9933);
  1139. NSMutableArray *actionArr = [NSMutableArray array];
  1140. // 取消订单
  1141. if([order_status isEqualToString:@"Saved Order"]&&appDelegate.can_cancel_order)
  1142. [actionArr addObject:cancelAction];
  1143. if ([Singleton sharedInstance].permissions_merge_order) {// 有合并订单的权限
  1144. if (self.orderType == 0) {// Sales Order不能合并
  1145. [actionArr addObject:add2MergeListAction];
  1146. }
  1147. }
  1148. return actionArr;
  1149. }
  1150. return @[];
  1151. }
  1152. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  1153. if(indexPath.row==self.content_data.count)
  1154. return NO;
  1155. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1156. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  1157. if(appDelegate.user_type==USER_ROLE_CUSTOMER) {
  1158. // saved && ((type==0 && merge_permission) || type == 1 || can_cancel)
  1159. // if ([order_status isEqualToString:@"Saved Order"] && (([Singleton sharedInstance].permissions_merge_order && self.orderType == 0) || self.orderType == 1 || appDelegate.can_cancel_order)) {
  1160. // DebugLog(@"%d Yes",indexPath.row);
  1161. // return YES;
  1162. // } else {
  1163. // return NO;
  1164. // }
  1165. BOOL isSavedOrder = [order_status isEqualToString:@"Saved Order"];
  1166. switch (self.orderType) {
  1167. case 0:{ // Shop Order
  1168. if (!isSavedOrder) {
  1169. return NO;
  1170. } else {
  1171. if ([Singleton sharedInstance].permissions_merge_order || appDelegate.can_cancel_order) {
  1172. return YES;
  1173. } else {
  1174. return NO;
  1175. }
  1176. }
  1177. }
  1178. break;
  1179. case 1:{ // Sales Order
  1180. if (isSavedOrder && appDelegate.can_cancel_order) {
  1181. return YES;
  1182. }
  1183. return NO;
  1184. }
  1185. break;
  1186. default:
  1187. break;
  1188. }
  1189. } else {
  1190. return YES;
  1191. }
  1192. return YES;
  1193. }
  1194. - (void)tableView:(UITableView *)tableView
  1195. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1196. }
  1197. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  1198. if(indexPath.row==self.content_data.count)
  1199. return UITableViewCellEditingStyleNone;
  1200. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1201. bool offline_edit =[[self.content_data[indexPath.row] valueForKey:@"offline_edit"] boolValue];
  1202. NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  1203. if(![order_status isEqualToString:@"Saved Order"]&&! [order_status isEqualToString:@"Quote Saved"])
  1204. return UITableViewCellEditingStyleNone;
  1205. else
  1206. {
  1207. if(appDelegate.offline_mode==false||offline_edit)
  1208. return UITableViewCellEditingStyleDelete;
  1209. else
  1210. return UITableViewCellEditingStyleNone;
  1211. }
  1212. }
  1213. /*
  1214. #pragma mark - Navigation
  1215. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1216. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1217. // Get the new view controller using [segue destinationViewController].
  1218. // Pass the selected object to the new view controller.
  1219. }
  1220. */
  1221. #pragma mark EnumSelectViewControllerDelegate
  1222. -(void) EnumValueChanged:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  1223. {
  1224. self.status_cadedate = value;
  1225. UIApplication * app = [UIApplication sharedApplication];
  1226. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1227. appDelegate.OrderFilter = value;
  1228. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1229. // NSString *documents = [paths objectAtIndex:0];
  1230. // NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  1231. //
  1232. //
  1233. // // if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  1234. // //
  1235. // // DebugLog(@"文件已经存在了");
  1236. // //
  1237. // // }
  1238. // // else
  1239. // // {
  1240. // //
  1241. // //
  1242. // //
  1243. // // DebugLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  1244. //
  1245. //NSData *data = [NSJSONSerialization dataWithJSONObject:self.status_cadedate options:kNilOptions error:nil];
  1246. //
  1247. // // DebugLog(@"mainBundleFile==%@",mainBundleFile);
  1248. //
  1249. // [[NSFileManager defaultManager] createFileAtPath:dist_path
  1250. //
  1251. // contents:data
  1252. //
  1253. // attributes:nil];
  1254. //
  1255. // }
  1256. // self.keywords=searchBar.text;
  1257. // self.offset = 0;
  1258. // [self.content_data removeAllObjects];
  1259. self.reset_result=true;
  1260. [self loadpage];
  1261. //[self.btnStatusFilter setTitle:setTitle:alert ];
  1262. }
  1263. #pragma mark - button action
  1264. - (IBAction)mergeListButtonClick:(UIBarButtonItem *)sender {
  1265. if (!self.mergeList.count) {
  1266. [RAUtils message_alert:@"Combine Order List is Empty" title:nil controller:self];
  1267. return;
  1268. }
  1269. SelectUploadOrderViewController *mergeVC = [[UIStoryboard storyboardWithName:@"OLM" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SelectUploadOrderViewController"];
  1270. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mergeVC];
  1271. mergeVC.mergeList = self.mergeList;
  1272. mergeVC.title = @"Combine Order List";
  1273. __weak typeof(self) weakself = self;
  1274. mergeVC.mergeBlock = ^(NSDictionary *ret) {
  1275. if (weakself) {
  1276. __strong typeof(weakself) strongself = weakself;
  1277. if([[ret objectForKey:@"result"]integerValue] == RESULT_TRUE) {
  1278. [weakself.mergeList removeAllObjects];
  1279. [strongself ReloadData];
  1280. } else {
  1281. NSString *msg = [ret objectForKey:@"err_msg"];
  1282. [RAUtils message_alert:msg title:nil controller:strongself];
  1283. }
  1284. }
  1285. };
  1286. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  1287. [self presentViewController:nav animated:YES completion:nil];
  1288. }
  1289. - (IBAction)orderTypeChanged:(UISegmentedControl *)sender {
  1290. self.orderType = sender.selectedSegmentIndex;
  1291. [Singleton sharedInstance].customer_order_type = self.orderType;
  1292. [self ReloadData];
  1293. }
  1294. #pragma mark - notification action
  1295. - (void)changePriceType:(id)notification {
  1296. [self ReloadData];
  1297. }
  1298. @end