OrderListViewController.m 85 KB

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