OrderListViewController.m 82 KB

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