OrderListViewController.m 82 KB

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