OrderListViewController.m 80 KB

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