OrderListViewController.m 85 KB

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