OrderListViewController.m 83 KB

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