OrderListViewController.m 85 KB

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