OrderListViewController.m 81 KB

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