OrderListViewController.m 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  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.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  822. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  823. [moreCell.contentView addSubview:moreDataLabel];
  824. if([self.load_more_hint isEqualToString:@"Load more..."])
  825. [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  826. // DebugLog(@"更多...");
  827. moreCell.backgroundColor = [UIColor whiteColor];
  828. return moreCell;
  829. }
  830. else
  831. {
  832. NSString *CellIdentifier = @"OrderListTableViewCell";
  833. OrderListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  834. NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"];
  835. NSString* price= [self.content_data[indexPath.row] valueForKey:@"price"];
  836. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  837. {
  838. }
  839. else
  840. {
  841. price=nil;
  842. }
  843. NSInteger statusCode = [[self.content_data[indexPath.row] valueForKey:@"orderStatus"] integerValue];
  844. NSString* purchase_time= [self.content_data[indexPath.row] valueForKey:@"purchase_time"];
  845. NSString* status = [self.content_data[indexPath.row] valueForKey:@"order_status"] ;
  846. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  847. status = [ERPUtils orderStatus:statusCode];
  848. }
  849. NSString* customer_name = [self.content_data[indexPath.row] valueForKey:@"customer_name"];
  850. NSString* model_count = [self.content_data[indexPath.row] valueForKey:@"model_count"];
  851. NSString* sales = [self.content_data[indexPath.row] valueForKey:@"sales_rep"];
  852. NSString* creator = [self.content_data[indexPath.row] valueForKey:@"create_by"];
  853. NSString *po = [self.content_data[indexPath.row] valueForKey:@"po_id"];
  854. CGRect price_frame = cell.labelprice.frame;
  855. if (appDelegate.user_type == USER_ROLE_CUSTOMER && self.orderType == 1) {
  856. cell.label_po.hidden = NO;
  857. price_frame.origin.y = 5;
  858. cell.label_po.text = po;
  859. } else {
  860. cell.label_po.hidden = YES;
  861. price_frame.origin.y = 21;
  862. }
  863. cell.labelprice.frame = price_frame;
  864. cell.label_so.text=so;
  865. cell.labelprice.text=price;
  866. cell.label_date.text=purchase_time;
  867. cell.label_contact.text=customer_name;
  868. cell.label_model.text = model_count;
  869. cell.label_sales.text = sales;
  870. cell.label_creator.text = creator;
  871. cell.label_status.text=status;
  872. cell.backgroundColor = [UIColor whiteColor];
  873. return cell;
  874. }
  875. // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  876. // NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  877. // cell.bundle_item=combine_json;
  878. // NSString* img_url = [item_json valueForKey:@"img_url"];
  879. // NSString* description = [item_json valueForKey:@"description"];
  880. // // NSString* identifier = [item_json valueForKey:@"identifier"];
  881. // // NSString* attribute = [item_json valueForKey:@"attribute"];
  882. // NSString* currency = [item_json valueForKey:@"currency"];
  883. // int count =[[item_json valueForKey:@"count"] intValue];
  884. // double price =[[item_json valueForKey:@"price"] doubleValue];
  885. // // cell.labelAttribute.text = attribute;
  886. // cell.labelCurrency.text = currency;
  887. // cell.labelDescription.text = description;
  888. // // cell.labelIdentifier.text = identifier;
  889. // cell.labelPrice.text = [NSString stringWithFormat:@"%.2f",price];
  890. //
  891. //
  892. //
  893. // [cell set_Count:count];
  894. // NSString* file_name=[img_url lastPathComponent];
  895. // NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  896. // if(img_data!=nil)
  897. // {
  898. //
  899. // UIImage * img =[UIImage imageWithData:img_data];
  900. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  901. // }
  902. // else
  903. // {
  904. //
  905. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  906. //
  907. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  908. //
  909. // dispatch_async(dispatch_get_main_queue(), ^{
  910. //
  911. //
  912. //
  913. // if(downloadimg_data!=nil)
  914. // {
  915. // [iSalesDB cache_img:downloadimg_data :file_name ];
  916. //
  917. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  918. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  919. // }
  920. //
  921. // });
  922. // });
  923. //
  924. //
  925. // }
  926. // }
  927. // else
  928. // {
  929. // NSString *CellIdentifier = @"OrderInfoListItem";
  930. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  931. // return cell;
  932. // }
  933. }
  934. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  935. {
  936. if (tableView.isEditing) { // 编辑模式下不响应
  937. return;
  938. }
  939. //
  940. if(indexPath.row==self.content_data.count)
  941. return;
  942. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  943. if(appDelegate.user_type==USER_ROLE_CUSTOMER) { // Customer
  944. if ([Singleton sharedInstance].global_lock) { // Order list 被锁
  945. [RAUtils message_alert:@"Permission Needed.\nGoto Menu -> Unlock" title:@"Warning" controller:self];
  946. return;
  947. }
  948. }
  949. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  950. OrderDetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController" ];
  951. // int i =[[self.content_data[indexPath.row] valueForKey:@"order_code"];
  952. dvc.selectOrder=^(NSMutableDictionary* order_detail){
  953. [self.navigationController popViewControllerAnimated:false];
  954. if(self.selectOrder)
  955. self.selectOrder(order_detail);
  956. };
  957. NSString* order_code =[self.content_data[indexPath.row] valueForKey:@"order_code"];
  958. dvc.is_shoporder=(self.orderType == 0);
  959. dvc.order_code = order_code;
  960. dvc.order_status =[[self.content_data[indexPath.row] valueForKey:@"orderStatus"] intValue];
  961. // bool temp_order = false ;
  962. // NSString* order_status=[self.content_data[indexPath.row] valueForKey:@"order_status"];
  963. // if([order_status isEqualToString:@"Saved"]||[order_status isEqualToString:@"Quoted"])
  964. // temp_order=true;
  965. //
  966. // dvc.isTempOrder = temp_order;
  967. // dvc
  968. dvc.orderid=orderid;
  969. // dvc.category_id=nil;
  970. // dvc.ispush=true;
  971. // [dvc reload];
  972. [self.navigationController pushViewController:dvc animated:true];
  973. }
  974. #pragma mark - searchBar delegate;
  975. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  976. {
  977. self.keywords=searchBar.text;
  978. // self.offset = 0;
  979. // [self.content_data removeAllObjects];
  980. self.reset_result=true;
  981. [self loadpage];
  982. }
  983. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  984. {
  985. if([searchText isEqualToString:@""] && !self.reset)
  986. {
  987. DebugLog(@"clear");
  988. self.reset=true;
  989. self.keywords=nil;
  990. // self.offset = 0;
  991. // [self.content_data removeAllObjects];
  992. self.reset_result=true;
  993. [self loadpage];
  994. }
  995. else
  996. self.reset = false;
  997. }
  998. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  999. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  1000. {
  1001. // 添加一个删除按钮
  1002. // self.indexPath=indexPath;
  1003. if(indexPath.row==self.content_data.count)
  1004. return @[];
  1005. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1006. NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"];
  1007. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  1008. // NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  1009. NSInteger statusCode = [[self.content_data[indexPath.row] valueForKey:@"orderStatus"] integerValue];
  1010. if(appDelegate.user_type==USER_ROLE_EMPLOYEE) {
  1011. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1012. DebugLog(@"delete click");
  1013. NSString * title =[NSString stringWithFormat:@"Are you sure to delete order SO#: %@",so];
  1014. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  1015. //block代码块取代了delegate
  1016. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  1017. // textField.text = self.save_name;
  1018. //
  1019. //
  1020. // }];
  1021. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1022. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"];
  1023. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1024. NSDictionary* return_json = [iSalesNetwork delete_Order:orderid];
  1025. dispatch_async(dispatch_get_main_queue(), ^{
  1026. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1027. if([[return_json valueForKey:@"result"] intValue]==2)
  1028. {
  1029. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  1030. {
  1031. appDelegate.order_code= nil;
  1032. [appDelegate SetSo:nil];
  1033. }
  1034. [RAUtils message_alert:nil title:@"Order Delete" controller:self] ;
  1035. [self ReloadData];
  1036. }
  1037. else
  1038. {
  1039. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order" controller:self] ;
  1040. }
  1041. });
  1042. });
  1043. }];
  1044. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1045. DebugLog(@"Cancel");
  1046. }];
  1047. [alertControl addAction:actionOne];
  1048. [alertControl addAction:alertthree];
  1049. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1050. [self presentViewController:alertControl animated:YES completion:nil];
  1051. }];
  1052. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  1053. // 添加一个编辑按钮
  1054. UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel Order"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1055. DebugLog(@"edit click");
  1056. NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so];
  1057. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  1058. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1059. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  1060. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1061. NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid order_code:nil];
  1062. dispatch_async(dispatch_get_main_queue(), ^{
  1063. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1064. if([[return_json valueForKey:@"result"] intValue]==2)
  1065. {
  1066. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  1067. {
  1068. appDelegate.order_code= nil;
  1069. //appDelegate.user_type = 0;
  1070. appDelegate.customerInfo = nil;
  1071. appDelegate.contact_id = nil;
  1072. appDelegate.order_customer_id = nil;
  1073. [appDelegate SetSo:nil];
  1074. appDelegate.cart_count=0;
  1075. [appDelegate update_count_mark];
  1076. }
  1077. [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  1078. [self ReloadData];
  1079. }
  1080. else
  1081. {
  1082. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  1083. }
  1084. });
  1085. });
  1086. }];
  1087. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1088. DebugLog(@"Cancel");
  1089. }];
  1090. [alertControl addAction:actionOne];
  1091. [alertControl addAction:alertthree];
  1092. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1093. [self presentViewController:alertControl animated:YES completion:nil];
  1094. //
  1095. }];
  1096. cancelAction.backgroundColor = UIColorFromRGB(0xff9933);
  1097. if(/*[order_status isEqualToString:@"Saved Order"]*/statusCode == 1&&appDelegate.can_cancel_order)
  1098. return @[cancelAction];
  1099. else
  1100. {
  1101. if(/*[order_status isEqualToString:@"Quote Saved"]*/statusCode == 0)
  1102. {
  1103. NSMutableArray* arr=[@[] mutableCopy];
  1104. if(appDelegate.can_delete_order)
  1105. [arr addObject:deleteRowAction];
  1106. if(appDelegate.can_cancel_order)
  1107. [arr addObject:cancelAction];
  1108. return arr;
  1109. }
  1110. }
  1111. } else if (appDelegate.user_type==USER_ROLE_CUSTOMER) {
  1112. // UITableViewRowAction *add2MergeListAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Add To Combine List" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  1113. //
  1114. // NSString *orderCode = [NSString stringWithFormat:@"%@",[self.content_data[indexPath.row] valueForKey:@"order_code"]];
  1115. //
  1116. //
  1117. // NSString *create_by = [self.content_data[indexPath.row] valueForKey:@"create_by"];
  1118. // NSString *create_time = [self.content_data[indexPath.row] valueForKey:@"purchase_time"];
  1119. //
  1120. // __block BOOL contain = NO;
  1121. // [self.mergeList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  1122. //
  1123. // NSString *so_id = [(NSDictionary *)obj objectForKey:@"so_id"];
  1124. // if ([so_id isEqualToString:so]) {
  1125. //
  1126. // contain = YES;
  1127. //
  1128. // *stop = YES;
  1129. // }
  1130. //
  1131. // }]; // 避免重复添加
  1132. //
  1133. //
  1134. // if (!contain) {
  1135. //
  1136. // __block BOOL closeOrder = NO;
  1137. // // 检查是否被自己打开
  1138. // if ([orderCode isEqualToString:appDelegate.order_code]) {
  1139. //
  1140. // 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];
  1141. //
  1142. // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1143. //
  1144. // closeOrder = NO;
  1145. // [alertVC dismissViewControllerAnimated:YES completion:nil];
  1146. //
  1147. // }];
  1148. //
  1149. // UIAlertAction *closeOrderAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1150. //
  1151. //
  1152. // [alertVC dismissViewControllerAnimated:YES completion:nil];
  1153. // // 关闭订单
  1154. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Release Order"];
  1155. // NSDictionary* order_json = [iSalesNetwork release_Order:orderCode];
  1156. //
  1157. // dispatch_async(dispatch_get_main_queue(), ^{
  1158. //
  1159. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1160. // if([[order_json valueForKey:@"result"] intValue]==2)
  1161. // {
  1162. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1163. //
  1164. // [appDelegate closeOrder];
  1165. // closeOrder = YES;
  1166. //
  1167. // // 锁订单/检查是否被别人锁定
  1168. // NSDictionary *lock_dic = [iSalesNetwork lock_order:orderCode]; // 和open一样,只不过不用更新car count
  1169. //
  1170. //
  1171. // if ([[lock_dic objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  1172. //
  1173. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1174. //
  1175. // [dic setValue:orderid forKey:@"order_id"];
  1176. // [dic setValue:so forKey:@"so_id"];
  1177. // [dic setValue:create_by forKey:@"create_by"];
  1178. // [dic setValue:create_time forKey:@"create_time"];
  1179. // [dic setValue:[NSNumber numberWithInteger:0] forKey:@"check"]; // 合并到
  1180. //
  1181. // [self.mergeList addObject:dic];
  1182. //
  1183. // } else {
  1184. //
  1185. // [RAUtils message_alert:[lock_dic objectForKey:@"err_msg"] title:@"Warning" controller:self];
  1186. //
  1187. // }
  1188. //
  1189. //
  1190. // }
  1191. // else
  1192. // {
  1193. // closeOrder = NO;
  1194. // [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Release Order" controller:self] ;
  1195. // }
  1196. //
  1197. //
  1198. //
  1199. // });
  1200. //
  1201. //
  1202. //
  1203. // }];
  1204. //
  1205. // [alertVC addAction:cancelAction];
  1206. // [alertVC addAction:closeOrderAction];
  1207. //
  1208. // [self presentViewController:alertVC animated:YES completion:nil];
  1209. //
  1210. // } else {
  1211. //
  1212. // closeOrder = YES;
  1213. // // 锁订单/检查是否被别人锁定
  1214. // NSDictionary *lock_dic = [iSalesNetwork lock_order:orderCode]; // 和open一样,只不过不用更新car count
  1215. //
  1216. // if ([[lock_dic objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  1217. //
  1218. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1219. //
  1220. // [dic setValue:orderid forKey:@"order_id"];
  1221. // [dic setValue:so forKey:@"so_id"];
  1222. // [dic setValue:create_by forKey:@"create_by"];
  1223. // [dic setValue:create_time forKey:@"create_time"];
  1224. // [dic setValue:[NSNumber numberWithInteger:0] forKey:@"check"]; // 合并到
  1225. //
  1226. // [self.mergeList addObject:dic];
  1227. //
  1228. // } else {
  1229. //
  1230. // [RAUtils message_alert:[lock_dic objectForKey:@"err_msg"] title:@"Warning" controller:self];
  1231. //
  1232. // }
  1233. // }
  1234. //
  1235. // }
  1236. //
  1237. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
  1238. //
  1239. // DebugLog(@"%@",self.mergeList);
  1240. //
  1241. // }];
  1242. // add2MergeListAction.backgroundColor = UIColorFromRGB(0x336699);
  1243. // Cancel Order Action
  1244. UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel Order"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1245. DebugLog(@"edit click");
  1246. NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so];
  1247. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  1248. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1249. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  1250. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1251. NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid order_code:nil];
  1252. dispatch_async(dispatch_get_main_queue(), ^{
  1253. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1254. if([[return_json valueForKey:@"result"] intValue]==2)
  1255. {
  1256. if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  1257. {
  1258. appDelegate.order_code= nil;
  1259. //appDelegate.user_type = 0;
  1260. appDelegate.customerInfo = nil;
  1261. appDelegate.contact_id = nil;
  1262. appDelegate.order_customer_id = nil;
  1263. [appDelegate SetSo:nil];
  1264. appDelegate.cart_count=0;
  1265. [appDelegate update_count_mark];
  1266. }
  1267. [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  1268. [self ReloadData];
  1269. }
  1270. else
  1271. {
  1272. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  1273. }
  1274. });
  1275. });
  1276. }];
  1277. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1278. DebugLog(@"Cancel");
  1279. }];
  1280. [alertControl addAction:actionOne];
  1281. [alertControl addAction:alertthree];
  1282. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1283. [self presentViewController:alertControl animated:YES completion:nil];
  1284. //
  1285. }];
  1286. cancelAction.backgroundColor = UIColorFromRGB(0xff9933);
  1287. NSMutableArray *actionArr = [NSMutableArray array];
  1288. // 取消订单
  1289. if(/*[order_status isEqualToString:@"Saved Order"]*/statusCode == 1&&appDelegate.can_cancel_order)
  1290. [actionArr addObject:cancelAction];
  1291. return actionArr;
  1292. }
  1293. return @[];
  1294. }
  1295. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  1296. if(indexPath.row==self.content_data.count)
  1297. return NO;
  1298. if(self.init_style==OL_OPEN)
  1299. return NO;
  1300. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1301. // NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  1302. NSInteger statusCode = [[self.content_data[indexPath.row] valueForKey:@"orderStatus"] integerValue];
  1303. if(appDelegate.user_type==USER_ROLE_CUSTOMER) {
  1304. // saved && ((type==0 && merge_permission) || type == 1 || can_cancel)
  1305. BOOL isSavedOrder = /*[order_status isEqualToString:@"Saved Order"]*/statusCode == 1;
  1306. switch (self.orderType) {
  1307. case 0:{ // Shop Order
  1308. if (!isSavedOrder) {
  1309. return NO;
  1310. } else {
  1311. if ([Singleton sharedInstance].permissions_merge_order || appDelegate.can_cancel_order) {
  1312. return YES;
  1313. } else {
  1314. return NO;
  1315. }
  1316. }
  1317. }
  1318. break;
  1319. case 1:{ // Sales Order
  1320. if (isSavedOrder && appDelegate.can_cancel_order) {
  1321. return YES;
  1322. }
  1323. return NO;
  1324. }
  1325. break;
  1326. default:
  1327. break;
  1328. }
  1329. } else {
  1330. return YES;
  1331. }
  1332. return YES;
  1333. }
  1334. - (void)tableView:(UITableView *)tableView
  1335. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1336. }
  1337. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  1338. if(indexPath.row==self.content_data.count)
  1339. return UITableViewCellEditingStyleNone;
  1340. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1341. bool offline_edit =[[self.content_data[indexPath.row] valueForKey:@"offline_edit"] boolValue];
  1342. // NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"];
  1343. NSInteger statusCode = [[self.content_data[indexPath.row] valueForKey:@"orderStatus"] integerValue];
  1344. if(/*![order_status isEqualToString:@"Saved Order"]&&! [order_status isEqualToString:@"Quote Saved"]*/statusCode != 1 && statusCode != 0)
  1345. return UITableViewCellEditingStyleNone;
  1346. else
  1347. {
  1348. if(appDelegate.offline_mode==false||offline_edit)
  1349. return UITableViewCellEditingStyleDelete;
  1350. else
  1351. return UITableViewCellEditingStyleNone;
  1352. }
  1353. }
  1354. /*
  1355. #pragma mark - Navigation
  1356. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1357. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1358. // Get the new view controller using [segue destinationViewController].
  1359. // Pass the selected object to the new view controller.
  1360. }
  1361. */
  1362. #pragma mark EnumSelectViewControllerDelegate
  1363. -(void) EnumValueChanged:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  1364. {
  1365. self.status_cadedate = value;
  1366. UIApplication * app = [UIApplication sharedApplication];
  1367. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1368. appDelegate.OrderFilter = value;
  1369. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1370. // NSString *documents = [paths objectAtIndex:0];
  1371. // NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  1372. //
  1373. //
  1374. // // if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  1375. // //
  1376. // // DebugLog(@"文件已经存在了");
  1377. // //
  1378. // // }
  1379. // // else
  1380. // // {
  1381. // //
  1382. // //
  1383. // //
  1384. // // DebugLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  1385. //
  1386. //NSData *data = [NSJSONSerialization dataWithJSONObject:self.status_cadedate options:kNilOptions error:nil];
  1387. //
  1388. // // DebugLog(@"mainBundleFile==%@",mainBundleFile);
  1389. //
  1390. // [[NSFileManager defaultManager] createFileAtPath:dist_path
  1391. //
  1392. // contents:data
  1393. //
  1394. // attributes:nil];
  1395. //
  1396. // }
  1397. // self.keywords=searchBar.text;
  1398. // self.offset = 0;
  1399. // [self.content_data removeAllObjects];
  1400. self.reset_result=true;
  1401. [self loadpage];
  1402. //[self.btnStatusFilter setTitle:setTitle:alert ];
  1403. }
  1404. #pragma mark - button action
  1405. - (NSIndexPath *)indexPathOfDeleteOrder:(NSString *)orderID {
  1406. for (NSIndexPath *indexPath in self.table_order.indexPathsForSelectedRows) {
  1407. NSString* orderid= [NSString stringWithFormat:@"%@",[self.content_data[indexPath.row] valueForKey:@"order_id"]];
  1408. if ([orderid isEqualToString:orderID]) {
  1409. return indexPath;
  1410. }
  1411. }
  1412. return nil;
  1413. }
  1414. - (IBAction)mergeListButtonClick:(UIBarButtonItem *)sender {
  1415. if (!self.table_order.isEditing) {
  1416. [RAUtils message_alert:@"You have to click select firstly then select some saved order" title:@"Warning" controller:self];
  1417. return;
  1418. }
  1419. [self handleSelectedOrders];
  1420. if (!self.mergeList.count) {
  1421. // [RAUtils message_alert:@"Combine List is Empty" title:@"Warning" controller:self];
  1422. return;
  1423. }
  1424. SelectUploadOrderViewController *mergeVC = [[UIStoryboard storyboardWithName:@"OLM" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SelectUploadOrderViewController"];
  1425. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mergeVC];
  1426. mergeVC.mergeList = self.mergeList;
  1427. mergeVC.title = @"Combine List";
  1428. __weak typeof(self) weakself = self;
  1429. mergeVC.mergeBlock = ^(NSDictionary *ret) {
  1430. if (weakself) {
  1431. __strong typeof(weakself) strongself = weakself;
  1432. if([[ret objectForKey:@"result"]integerValue] == RESULT_TRUE) {
  1433. [weakself.mergeList removeAllObjects];
  1434. [strongself.table_order endEditing:YES];
  1435. [strongself ReloadData];
  1436. if ([Singleton sharedInstance].customer_can_see_sales_Order) {
  1437. [strongself openOrder:ret];
  1438. }
  1439. } else {
  1440. NSString *msg = [ret objectForKey:@"err_msg"];
  1441. [RAUtils message_alert:msg title:nil controller:strongself];
  1442. }
  1443. }
  1444. };
  1445. mergeVC.deleteBlock = ^(NSString *orderID){
  1446. NSIndexPath *indexPath = [weakself indexPathOfDeleteOrder:orderID];
  1447. [weakself.table_order deselectRowAtIndexPath:indexPath animated:YES];
  1448. };
  1449. mergeVC.clearBlock = ^{
  1450. for (NSDictionary *order in self.mergeList) {
  1451. NSString *order_id = [NSString stringWithFormat:@"%@",[order objectForKey:@"order_id"]];
  1452. NSIndexPath *indexPath = [weakself indexPathOfDeleteOrder:order_id];
  1453. [weakself.table_order deselectRowAtIndexPath:indexPath animated:YES];
  1454. }
  1455. };
  1456. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  1457. [self presentViewController:nav animated:YES completion:nil];
  1458. }
  1459. - (void)handleSelectedOrders {
  1460. [self.mergeList removeAllObjects];
  1461. if (!self.table_order.isEditing) {
  1462. return;
  1463. }
  1464. for (NSIndexPath *indexPath in self.table_order.indexPathsForSelectedRows) {
  1465. if(indexPath.row==self.content_data.count)
  1466. continue;
  1467. NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"];
  1468. NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"];
  1469. NSString *create_by = [self.content_data[indexPath.row] valueForKey:@"create_by"];
  1470. NSString *create_time = [self.content_data[indexPath.row] valueForKey:@"purchase_time"];
  1471. NSString *orderCode = [NSString stringWithFormat:@"%@",[self.content_data[indexPath.row] valueForKey:@"order_code"]];
  1472. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1473. [dic setValue:orderid forKey:@"order_id"];
  1474. [dic setValue:orderCode forKey:@"order_code"];
  1475. [dic setValue:so forKey:@"so_id"];
  1476. [dic setValue:create_by forKey:@"create_by"];
  1477. [dic setValue:create_time forKey:@"create_time"];
  1478. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"check"]; // 合并到
  1479. [self.mergeList addObject:dic];
  1480. }
  1481. }
  1482. - (IBAction)editOrderListButtonClick:(UIBarButtonItem *)sender {
  1483. if ([Singleton sharedInstance].global_lock) { // Order list 被锁
  1484. [RAUtils message_alert:@"Permission Needed.\nGoto Menu -> Unlock" title:@"Warning" controller:self];
  1485. return;
  1486. }
  1487. UIImage *gray = [UIImage imageNamed:@"Combinegray"];
  1488. UIImage *green = [[UIImage imageNamed:@"converttopo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//converttopo
  1489. if (self.table_order.isEditing) {
  1490. [self mergeListButtonClick:nil];
  1491. // 结束选择时恢复
  1492. if ([Singleton sharedInstance].customer_can_see_sales_Order && ![Singleton sharedInstance].global_lock) { // 有查看Sales order 权限,并且app没有锁
  1493. if (self.orderTypeSegmentControl.numberOfSegments == 1) {
  1494. [self.orderTypeSegmentControl insertSegmentWithTitle:@"Purchase Order to NPD" atIndex:1 animated:YES];
  1495. }
  1496. }
  1497. self.status_cadedate = self.shop_order_filter;
  1498. [self ReloadData];
  1499. [self.table_order setEditing:NO animated:YES];
  1500. // sender.title = @"Select";
  1501. // [self.mergeList removeAllObjects];
  1502. [sender setImage:gray];
  1503. } else {
  1504. // 选择时不能选择Sales Order
  1505. if(self.orderTypeSegmentControl.numberOfSegments > 1) {
  1506. [self.orderTypeSegmentControl removeSegmentAtIndex:1 animated:YES];
  1507. [self changeOrderType:0];
  1508. }
  1509. // 切换filter
  1510. NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"status_filter_cadedate_open" ofType:@"json" ]];
  1511. NSError *error=nil;
  1512. self.status_cadedate = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1513. // 刷新,取Saved Order
  1514. [self ReloadData];
  1515. [self.table_order setEditing:YES animated:YES];
  1516. // sender.title = @"Exit";
  1517. [sender setImage:green];
  1518. }
  1519. }
  1520. - (void)changeOrderType:(NSInteger)ordertype {
  1521. self.orderType = ordertype;
  1522. [Singleton sharedInstance].customer_order_type = ordertype;
  1523. if(self.init_style != OL_OPEN) {
  1524. switch (ordertype) {
  1525. case 0:{
  1526. self.status_cadedate = self.shop_order_filter;
  1527. }
  1528. break;
  1529. case 1:{
  1530. self.status_cadedate = self.sales_order_filter;
  1531. }
  1532. break;
  1533. default:
  1534. break;
  1535. }
  1536. NSMutableArray *items = [self.toolbarView.items mutableCopy];
  1537. if (ordertype == 1) {
  1538. // 不能合并
  1539. // if ([items containsObject:self.mergeListButton]) { // 显示button
  1540. // [items removeObject:self.mergeListButton];
  1541. // }
  1542. if ([items containsObject:self.editOrderListButton]) { // 显示button
  1543. [items removeObject:self.editOrderListButton];
  1544. }
  1545. self.toolbarView.items = items;
  1546. } else {
  1547. // 合并订单的权限
  1548. if ([Singleton sharedInstance].permissions_merge_order) { // 能够合并
  1549. // 4 6 顺序不能换
  1550. if (![items containsObject:self.editOrderListButton]) { // 没显示button
  1551. [items insertObject:self.editOrderListButton atIndex:4];
  1552. }
  1553. // if (![items containsObject:self.mergeListButton]) { // 没显示button
  1554. // [items insertObject:self.mergeListButton atIndex:6];
  1555. // }
  1556. self.toolbarView.items = items;
  1557. } else { // 不能合并
  1558. // if ([items containsObject:self.mergeListButton]) { // 显示button
  1559. // [items removeObject:self.mergeListButton];
  1560. // }
  1561. if ([items containsObject:self.editOrderListButton]) { // 显示button
  1562. [items removeObject:self.editOrderListButton];
  1563. }
  1564. self.toolbarView.items = items;
  1565. }
  1566. }
  1567. } else {
  1568. NSString *resource = @"status_filter_cadedate_open";
  1569. if (ordertype == 1) {
  1570. resource = @"status_filter_cadedate_po";
  1571. }
  1572. NSData *json=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:resource ofType:@"json" ]];
  1573. NSError *error=nil;
  1574. self.status_cadedate = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1575. }
  1576. [self ReloadData];
  1577. }
  1578. - (IBAction)orderTypeChanged:(UISegmentedControl *)sender {
  1579. [self changeOrderType:sender.selectedSegmentIndex];
  1580. }
  1581. - (void)openOrder:(NSDictionary *)order {
  1582. if (!order) {
  1583. return;
  1584. }
  1585. NSString *orderCode = [order objectForKey:@"orderCode"];
  1586. NSInteger orderStatus = [[order objectForKey:@"orderStatus"] integerValue];
  1587. NSString *so = [order objectForKey:@"so#"];
  1588. __weak typeof(self) weakself = self;
  1589. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1590. NSDictionary* order_json = [iSalesNetwork open_Order:orderCode];
  1591. dispatch_async(dispatch_get_main_queue(), ^{
  1592. if([[order_json valueForKey:@"result"] intValue]==2)
  1593. {
  1594. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1595. appDelegate.order_code = orderCode;
  1596. appDelegate.order_status = orderStatus;
  1597. [appDelegate SetSo:so];
  1598. __weak MainViewController *mainVC = (MainViewController*)appDelegate.main_vc;
  1599. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  1600. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1601. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  1602. [((MainViewController*)appDelegate.main_vc) switchToCart];
  1603. mainVC.cartViewController.onFinishLoad=^{
  1604. [mainVC.cartViewController placeOrder];
  1605. mainVC.cartViewController.onFinishLoad=nil;
  1606. };
  1607. }
  1608. else
  1609. {
  1610. [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order" controller:weakself] ;
  1611. }
  1612. });
  1613. });
  1614. }
  1615. #pragma mark - notification action
  1616. - (void)changePriceType:(id)notification {
  1617. [self ReloadData];
  1618. }
  1619. - (void)lockOrderListNotification:(NSNotification *)notification {
  1620. if ([notification.name isEqual:Lock_Permission_Notification]) {
  1621. if (self.orderType == 1) {
  1622. self.orderTypeSegmentControl.selectedSegmentIndex = 0;
  1623. // [self changeOrderType:0];
  1624. [self.orderTypeSegmentControl sendActionsForControlEvents:UIControlEventValueChanged];
  1625. }
  1626. if(self.orderTypeSegmentControl.numberOfSegments > 1) {
  1627. [self.orderTypeSegmentControl removeSegmentAtIndex:1 animated:YES];
  1628. // [self changeOrderType:0];
  1629. }
  1630. } else { // 解锁
  1631. if ([Singleton sharedInstance].customer_can_see_sales_Order) { // 有查看Sales order 权限
  1632. if (self.orderTypeSegmentControl.numberOfSegments == 1 && !self.table_order.isEditing) {
  1633. [self.orderTypeSegmentControl insertSegmentWithTitle:@"Purchase Order to NPD" atIndex:1 animated:YES];
  1634. }
  1635. }
  1636. }
  1637. }
  1638. - (void)handleUserLogin:(NSNotification *)notification {
  1639. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1640. self.orderType = 0;
  1641. if (self.table_order.isEditing) {
  1642. [self.table_order setEditing:NO animated:YES];
  1643. self.editOrderListButton.image = [UIImage imageNamed:@"Combinegray"];
  1644. }
  1645. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  1646. self.orderTypeSegmentControl.selectedSegmentIndex = 0;
  1647. [Singleton sharedInstance].customer_order_type = 0;
  1648. }
  1649. }
  1650. @end