PortfolioViewController.m 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. //
  2. // PortfolioViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 12/7/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PortfolioViewController.h"
  9. #import "iSalesNetwork.h"
  10. #import "ModelItemCell.h"
  11. #import "DetailViewController.h"
  12. #import "LoginViewController.h"
  13. #import "MainViewController.h"
  14. #import "TearSheetParamViewController.h"
  15. #import "EditModelPriceViewController.h"
  16. #import "ContactListViewController.h"
  17. #define ALERT_DEL 1025
  18. @interface PortfolioViewController ()
  19. @end
  20. @implementation PortfolioViewController
  21. -(void) logout
  22. {
  23. self.content_data = nil;
  24. }
  25. -(void) reload_container_getdata:(bool) update_data
  26. {
  27. [super reload_container_getdata:update_data];
  28. if(update_data)
  29. [self reload_data];
  30. else
  31. [self.itemListTable reloadData];
  32. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  33. if(appDelegate.can_create_portfolio)
  34. {
  35. [self.btnCreatePort setTitle:@"Create Portfolio"];
  36. self.btnCreatePort.enabled = true;
  37. }
  38. else
  39. {
  40. [self.btnCreatePort setTitle:@""];
  41. self.btnCreatePort.enabled = false;
  42. }
  43. }
  44. -(void) showHidePrice
  45. {
  46. [self reload_container_getdata:false];
  47. // [self refresh_total];
  48. }
  49. - (IBAction)onAddtoCart:(id)sender {
  50. UIApplication * app = [UIApplication sharedApplication];
  51. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  52. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  53. if(appDelegate.bLogin==false)
  54. {
  55. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  56. // loginvc.delegate = self;
  57. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  58. loginvc.returnValue = ^(bool blogin){
  59. // UIApplication * app = [UIApplication sharedApplication];
  60. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  61. if(blogin)
  62. {
  63. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  64. {
  65. [main_vc checklogin:false];
  66. if(appDelegate.can_create_order)
  67. {
  68. NSString* msg =@"";
  69. if(appDelegate.contact_id.length>0)
  70. {
  71. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  72. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  73. }
  74. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  75. // alert.
  76. [alert show];
  77. }
  78. else
  79. {
  80. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  81. // alert.
  82. [alert show];
  83. }
  84. }
  85. else
  86. {
  87. [main_vc checklogin:true];
  88. }
  89. }
  90. // self.offset = 0;
  91. // [self.content_data removeAllObjects];
  92. // [self loadpage];
  93. //
  94. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  95. };
  96. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  97. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  98. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  99. [self presentViewController:navi animated:YES completion:^{
  100. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  101. NSLog(@"LoginViewController present.........");
  102. // self.btop = false;
  103. // <#code#>
  104. }];
  105. }
  106. else
  107. {
  108. if(appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  109. {
  110. [main_vc checklogin:false];
  111. if(appDelegate.can_create_order)
  112. {
  113. NSString* msg =@"";
  114. if(appDelegate.contact_id.length>0)
  115. {
  116. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  117. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  118. }
  119. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  120. // alert.
  121. [alert show];
  122. }
  123. else
  124. {
  125. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  126. // alert.
  127. [alert show];
  128. }
  129. }
  130. else
  131. {
  132. if(appDelegate.order_code==nil)
  133. [ self neworder];
  134. else
  135. [self addtocart];
  136. }
  137. }
  138. }
  139. -(void) addtocart
  140. {
  141. NSMutableArray* checked = [[NSMutableArray alloc] init];
  142. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  143. int count = [[self.content_data valueForKey:@"count"] intValue];
  144. for(int i=0;i<count;i++)
  145. {
  146. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  147. bool check = [[item_json valueForKey:@"check"] boolValue];
  148. if(check)
  149. {
  150. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  151. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  152. }
  153. }
  154. if(checked.count==0)
  155. {
  156. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  157. return;
  158. }
  159. NSString * ids = [checked componentsJoinedByString:@","];
  160. self.btnCreatePort.enabled = false;
  161. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  162. NSDictionary* return_json = [iSalesNetwork add_toCart:ids count:-1];
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. self.btnCreatePort.enabled = true;
  165. if([[return_json valueForKey:@"result"] intValue]==2)
  166. {
  167. // int result=[[return_json valueForKey:@"result"] intValue];
  168. // if(result==2)
  169. // {
  170. // //successed.
  171. // NSString* order_code = [return_json valueForKey:@"order_code"];
  172. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  173. // appDelegate.order_code = order_code;
  174. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  175. // appDelegate.order_code = order_code;
  176. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false ];
  177. [RAUtils message_alert:nil title:@" Multiple items added to Cart" controller:self] ;
  178. // [self end_select];
  179. // }
  180. }
  181. else
  182. {
  183. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  184. }
  185. });
  186. });
  187. }
  188. -(void) neworder
  189. {
  190. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  191. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  192. NSDictionary* return_json = [iSalesNetwork new_Order];
  193. dispatch_async(dispatch_get_main_queue(), ^{
  194. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  195. if([[return_json valueForKey:@"result"] intValue]==2)
  196. {
  197. int result=[[return_json valueForKey:@"result"] intValue];
  198. if(result==2)
  199. {
  200. //successed.
  201. NSString* order_code = [return_json valueForKey:@"orderCode"];
  202. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  203. appDelegate.order_code = order_code;
  204. [self addtocart];
  205. // [self ReloadData];
  206. // if(self.shopCartBlock!=nil)
  207. // {
  208. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  209. //
  210. //
  211. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  212. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  213. // iv.image = img;
  214. // // [self.contentView addSubview:iv];
  215. // self.shopCartBlock(iv);
  216. //
  217. // }
  218. }
  219. }
  220. else
  221. {
  222. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  223. }
  224. });
  225. });
  226. }
  227. - (IBAction)onCreateTearSheet:(id)sender {
  228. self.pdf_url= nil;
  229. NSMutableArray* checked = [[NSMutableArray alloc] init];
  230. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  231. int count = [[self.content_data valueForKey:@"count"] intValue];
  232. for(int i=0;i<count;i++)
  233. {
  234. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  235. bool check = [[item_json valueForKey:@"check"] boolValue];
  236. if(check)
  237. {
  238. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  239. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  240. }
  241. }
  242. // disable for debug
  243. if(checked.count==0)
  244. {
  245. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Create Portfolio." controller:self] ;
  246. return;
  247. }
  248. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  249. NSString * string = [checked componentsJoinedByString:@","];
  250. NSString * string_iid = [checked_iid componentsJoinedByString:@","];
  251. [params setValue:string forKey:@"product_ids"];
  252. [params setValue:string_iid forKey:@"item_ids"];
  253. TearSheetParamViewController * VC =[ self.storyboard instantiateViewControllerWithIdentifier:@"TearSheetParamViewController"];
  254. VC.url_type = URL_REMOTE;
  255. VC.request_url=URL_DM_PARAMS;
  256. VC.params = params;
  257. // VC.delegate=self;
  258. VC.return_url=^(NSString* url,NSString* path, bool remove_item)
  259. {
  260. self.pdf_url = url;
  261. self.path = path;
  262. self.remove_item = remove_item;
  263. };
  264. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  265. VC.onDismiss=^(NSString* catelog_name)
  266. {
  267. };
  268. //
  269. // if(checked.count==count)
  270. // {
  271. // orderinfoVC.have_tail = true
  272. // }
  273. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:VC] ;
  274. navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  275. [self presentViewController:navi animated:YES completion:^{
  276. NSLog(@"TearSheetParamViewController present.........");
  277. // self.btop = false;
  278. // <#code#>
  279. }];
  280. [self.btnselect setTitle:@"Select all"];
  281. }
  282. - (void)viewDidLoad
  283. {
  284. [super viewDidLoad];
  285. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  286. ref.tag = 200 ;
  287. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  288. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  289. // ref.hidden = true;
  290. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  291. [self.itemListTable addSubview:ref];
  292. [[UIToolbar appearance] setBackgroundColor:UIColorFromRGB(0xF2EEEA)];
  293. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  294. self.label_net_err.layer.borderWidth = 2.0;
  295. self.label_net_err.layer.cornerRadius=15;
  296. self.label_net_err.layer.masksToBounds=true;
  297. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  298. // tap.minimumPressDuration = 0.8; //定义按的时间
  299. [self.label_net_err addGestureRecognizer:tap];
  300. // self.toolbar.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolbar.bounds].CGPath;
  301. // self.toolbar.layer.masksToBounds = false;
  302. //// //添加四个边阴影
  303. //
  304. // self.toolbar.layer.shadowColor = [UIColor blackColor].CGColor;
  305. // self.toolbar.layer.shadowOffset = CGSizeMake(0, 0);
  306. // self.toolbar.layer.shadowOpacity = 0.5;
  307. // self.toolbar.layer.shadowRadius = 2.0;
  308. //
  309. // self.toolbar.layer.borderWidth = 1;
  310. // self.toolbar.layer.borderColor = [[UIColor blackColor] CGColor];
  311. //
  312. // self.toolbar.clipsToBounds = YES;
  313. // [self reload_data];
  314. }
  315. -(void)manually_refresh
  316. {
  317. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  318. if(self.isrefreshing)
  319. {
  320. [reF endRefreshing];
  321. return;
  322. }
  323. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  324. if ([self respondsToSelector:@selector(reload_data)])
  325. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  326. // DebugLog(@"refresh!!!!!!!!");
  327. }
  328. - (IBAction)onSelectClick:(id)sender {
  329. // self.total=0;
  330. if([self.btnselect.title isEqualToString:@"Select all"])
  331. {
  332. int count =[[self.content_data valueForKey:@"count"] intValue];
  333. for(int i=0;i<count;i++)
  334. {
  335. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  336. [item_json setValue:@"1" forKey:@"check"];
  337. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  338. cell.img_checkmark.hidden=false;
  339. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  340. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  341. }
  342. [self.btnselect setTitle:@"Deselect all"];
  343. }
  344. else
  345. {
  346. int count =[[self.content_data valueForKey:@"count"] intValue];
  347. for(int i=0;i<count;i++)
  348. {
  349. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  350. [item_json setValue:@"0" forKey:@"check"];
  351. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  352. cell.img_checkmark.hidden=true;
  353. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  354. }
  355. [self.btnselect setTitle:@"Select all"];
  356. // self.btnselect.titleLabel.text=@"Select all";
  357. }
  358. }
  359. - (IBAction)onDeleteClick:(id)sender {
  360. NSMutableArray* checked = [[NSMutableArray alloc] init];
  361. int count = [[self.content_data valueForKey:@"count"] intValue];
  362. for(int i=0;i<count;i++)
  363. {
  364. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  365. int check = [[item_json valueForKey:@"check"] intValue];
  366. if(check==1)
  367. {
  368. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  369. }
  370. }
  371. // disable for debug
  372. if(checked.count==0)
  373. return;
  374. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  375. NSString * string = [checked componentsJoinedByString:@","];
  376. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  377. NSDictionary* cart_json = [iSalesNetwork portfolio_remove:string];
  378. dispatch_async(dispatch_get_main_queue(), ^{
  379. if([[cart_json valueForKey:@"result"] intValue]==2)
  380. {
  381. [self reload_data];
  382. }
  383. else
  384. {
  385. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Remove Model From Portfolio" controller:self] ;
  386. }
  387. });
  388. });
  389. }
  390. -(void) viewWillAppear:(BOOL)animated
  391. {
  392. [super viewWillAppear:animated];
  393. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  394. }
  395. -(void) reload_data
  396. {
  397. dispatch_async(dispatch_get_main_queue(), ^{
  398. if(self.isrefreshing)
  399. return;
  400. self.isrefreshing=true;
  401. self.label_net_err.hidden=true;
  402. self.itemListTable.hidden=true;
  403. [self.content_data removeAllObjects];
  404. [self.itemListTable reloadData];
  405. self.mum.center = self.view.center;
  406. self.mum.hidden = false;
  407. [self.mum startAnimating];
  408. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  409. [reF endRefreshing];
  410. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  411. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  412. NSDictionary* cart_json = [iSalesNetwork request_Portfolio];
  413. dispatch_async(dispatch_get_main_queue(), ^{
  414. [self.mum stopAnimating];
  415. int result=[[cart_json valueForKey:@"result"] intValue];
  416. if(result==2||result==1||result==0)
  417. {
  418. self.itemListTable.hidden=false;
  419. self.content_data = [cart_json mutableCopy];
  420. [self.itemListTable reloadData];
  421. UIApplication * app = [UIApplication sharedApplication];
  422. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  423. appDelegate.port_count =[[self.content_data valueForKey:@"count"] intValue];
  424. }
  425. else
  426. if(result==RESULT_NET_ERROR)
  427. {
  428. self.label_net_err.hidden=false;
  429. self.itemListTable.hidden=true;
  430. }
  431. else
  432. {
  433. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Watchlist" controller:self] ;
  434. }
  435. self.isrefreshing=false;
  436. });
  437. });
  438. });
  439. }
  440. - (void)didReceiveMemoryWarning
  441. {
  442. [super didReceiveMemoryWarning];
  443. // Dispose of any resources that can be recreated.
  444. }
  445. #pragma mark - Table view data source
  446. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  447. //{
  448. // return 0;
  449. //}
  450. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  451. //{
  452. // return 0;
  453. //}
  454. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  455. //{
  456. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  457. // return myView;
  458. //
  459. //}
  460. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  461. //{
  462. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  463. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  464. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  465. //// titleLabel.textColor=[UIColor whiteColor];
  466. //// titleLabel.backgroundColor = [UIColor clearColor];
  467. //// if(section==0)
  468. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  469. //// else
  470. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  471. //// [titleLabel sizeToFit];
  472. //// [myView addSubview:titleLabel];
  473. ////
  474. // return myView;
  475. //}
  476. //
  477. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  478. // if(section==0)
  479. // return nil;
  480. // else
  481. // return @"detail section";
  482. //}
  483. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  484. {
  485. return 1;
  486. }
  487. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  488. {
  489. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  490. int count =[[self.content_data valueForKey:@"count"] intValue];
  491. return count;
  492. }
  493. - (IBAction)imgbtnClicked:(UIButton *)sender {
  494. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  495. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  496. // NSLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  497. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  498. NSString* product_id = [item_json valueForKey:@"product_id"];
  499. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  500. // dvc
  501. dvc.product_id=product_id;
  502. dvc.category_id=nil;
  503. dvc.ispush=true;
  504. [dvc reload];
  505. [self.navigationController pushViewController:dvc animated:true];
  506. }
  507. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  508. {
  509. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  510. // if(tableView==self.itemListTable)
  511. // {
  512. NSString *CellIdentifier = @"WatchListItemCell";
  513. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  514. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  515. NSString* img_url = [item_json valueForKey:@"img"];
  516. NSString* description = [item_json valueForKey:@"description"];
  517. // NSString* identifier = [item_json valueForKey:@"identifier"];
  518. // NSString* attribute = [item_json valueForKey:@"attribute"];
  519. // NSString* currency = [item_json valueForKey:@"currency"];
  520. // int count =[[item_json valueForKey:@"count"] intValue];
  521. float discount = [[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  522. float price =[[item_json valueForKey:@"tear_sheet_price"] floatValue];
  523. // cell.labelAttribute.text = attribute;
  524. // cell.labelCurrency.text = currency;
  525. cell.labelDescription.text = description;
  526. // cell.labelIdentifier.text = identifier;
  527. NSString* str_price =[NSString stringWithFormat:@"%.2f",price * (1.0-discount/100)];
  528. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  529. {
  530. }
  531. else
  532. {
  533. str_price=nil;
  534. }
  535. cell.labelPrice.text = str_price;
  536. // cell.labelPrice.text = price;
  537. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  538. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  539. int check = [[item_json valueForKey:@"check"] intValue];
  540. if(check==1)
  541. cell.img_checkmark.hidden=false;
  542. else
  543. cell.img_checkmark.hidden=true;
  544. // UIImageView* iv =cell.btnImage.imageView;
  545. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  546. NSString* file_name=[img_url lastPathComponent];
  547. NSData* img_data=[iSalesDB load_cached_img:file_name];
  548. if(img_data!=nil)
  549. {
  550. UIImage * img =[UIImage imageWithData:img_data];
  551. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  552. // cell.btnImage.imageView.image = img;
  553. }
  554. else
  555. {
  556. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  557. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  558. dispatch_async(dispatch_get_main_queue(), ^{
  559. if(downloadimg_data!=nil)
  560. {
  561. [iSalesDB cache_img:downloadimg_data :file_name ];
  562. UIImage * img =[UIImage imageWithData:downloadimg_data];
  563. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  564. }
  565. else
  566. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  567. });
  568. });
  569. }
  570. cell.backgroundColor = [UIColor whiteColor];//UIColorFromRGB(0xF2EEEA);
  571. return cell;
  572. }
  573. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  574. {
  575. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  576. cell.selected = false;
  577. // self.dirty = true;
  578. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  579. int check = [[item_json valueForKey:@"check"] intValue];
  580. if(check==1)
  581. {
  582. [item_json setValue:@"0" forKey:@"check"];
  583. cell.img_checkmark.hidden=true;
  584. [self.btnselect setTitle: @"Select all"];
  585. }
  586. else
  587. {
  588. [item_json setValue:@"1" forKey:@"check"];
  589. cell.img_checkmark.hidden=false;
  590. }
  591. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  592. }
  593. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  594. return UITableViewCellEditingStyleDelete;
  595. }
  596. //- (void)tableView:(UITableView *)tableView
  597. //commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  598. //
  599. //
  600. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  601. //
  602. // NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  603. //
  604. //
  605. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  606. //
  607. // NSDictionary* cart_json = [iSalesNetwork watchlist_remove:ids];
  608. //
  609. // dispatch_async(dispatch_get_main_queue(), ^{
  610. //
  611. // if([[cart_json valueForKey:@"result"] intValue]==2)
  612. // {
  613. // [self reload_data];
  614. // }
  615. // else
  616. // {
  617. // [RAUtils error_alert:[cart_json valueForKey:@"err_msg"] title:@"Failed to remove model from wishlist."] ;
  618. // }
  619. //
  620. //
  621. //
  622. // });
  623. // });
  624. //}
  625. //#pragma mark - UIAlertViewDelegate
  626. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  627. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  628. //{
  629. //
  630. //
  631. //
  632. //}
  633. - (BOOL)isPureFloat:(NSString*)string{
  634. NSScanner* scan = [NSScanner scannerWithString:string];
  635. float val;
  636. return[scan scanFloat:&val] && [scan isAtEnd];
  637. }
  638. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  639. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  640. {
  641. // 添加一个删除按钮
  642. self.indexPath=indexPath;
  643. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  644. NSLog(@"delete click");
  645. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure remove model from portfolio?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  646. alert.tag = ALERT_DEL;
  647. // alert.
  648. [alert show];
  649. }];
  650. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  651. // 添加一个编辑按钮
  652. UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" Edit "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  653. NSLog(@"edit click");
  654. UIViewController* parentvc=self;
  655. EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  656. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  657. vc.set_watch_list=true;
  658. vc.discount=[[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  659. vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  660. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  661. // vc.delegate = parentvc.self;
  662. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  663. vc.onSetValue = ^(double price,double discount){
  664. [self reload_data];//[main_vc checklogin:true];
  665. };
  666. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  667. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  668. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  669. [parentvc presentViewController:navi animated:YES completion:^{
  670. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  671. NSLog(@"EditModelPriceViewController present.........");
  672. // self.btop = false;
  673. // <#code#>
  674. }];
  675. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  676. }];
  677. editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  678. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  679. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  680. {
  681. NSMutableArray* arr=[@[] mutableCopy];
  682. [arr addObject:deleteRowAction];
  683. if(appDelegate.can_set_tearsheet_price)
  684. {
  685. [arr addObject:editRowAction];
  686. }
  687. return arr;
  688. // return @[deleteRowAction, editRowAction];
  689. }
  690. else
  691. return @[deleteRowAction];
  692. }
  693. - (void)tableView:(UITableView *)tableView
  694. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  695. }
  696. #pragma mark - UIAlertViewDelegate
  697. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  698. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  699. {
  700. UIApplication * app = [UIApplication sharedApplication];
  701. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  702. // if(alertView.tag==1234)
  703. // {
  704. //
  705. // // availability warring;
  706. //
  707. //
  708. // if(buttonIndex!=alertView.cancelButtonIndex)
  709. // {
  710. // if(appDelegate.order_code==nil)
  711. // {
  712. // [self neworder];
  713. // }
  714. // else
  715. // {
  716. // [self addtocart];
  717. // }
  718. // }
  719. // return;
  720. // }
  721. // int count=[self.quantity_text.text intValue];
  722. if(alertView.tag==ALERT_DEL)
  723. {
  724. if(buttonIndex!=alertView.cancelButtonIndex)
  725. {
  726. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  727. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  728. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  729. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  730. NSDictionary* cart_json = [iSalesNetwork portfolio_remove:ids];
  731. dispatch_async(dispatch_get_main_queue(), ^{
  732. if([[cart_json valueForKey:@"result"] intValue]==2)
  733. {
  734. [self reload_data];
  735. }
  736. else
  737. {
  738. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  739. }
  740. });
  741. });
  742. }
  743. return;
  744. }
  745. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  746. if(buttonIndex==alertView.cancelButtonIndex)
  747. {
  748. }
  749. else if(buttonIndex==1)
  750. {
  751. //open exist
  752. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  753. ovc.showNavibar = true;
  754. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  755. if(appDelegate.order_code==nil)
  756. {
  757. [self neworder];
  758. }
  759. else
  760. {
  761. [self addtocart];
  762. [main_vc checklogin:true];
  763. }
  764. };
  765. ovc.init_style = OL_OPEN;
  766. ovc.onCancel = ^(){
  767. [main_vc checklogin:true];
  768. };
  769. [self.navigationController pushViewController:ovc animated:true];
  770. }
  771. else
  772. {
  773. //create new;
  774. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  775. {
  776. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  777. cvc.showNavibar = true;
  778. cvc.contact_type = @"Sales_Order_Customer";
  779. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  780. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  781. appDelegate.customerInfo = value;
  782. if(appDelegate.order_code==nil)
  783. [self neworder];
  784. // [main_vc checklogin:true];
  785. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  786. //
  787. // if(self.returnValue)
  788. // self.returnValue(value);
  789. };
  790. cvc.onCancel = ^(){
  791. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  792. };
  793. cvc.onReset = ^(){
  794. [main_vc checklogin:true];
  795. };
  796. [self.navigationController pushViewController:cvc animated:true];
  797. }
  798. else
  799. {
  800. [self neworder];
  801. }
  802. }
  803. }
  804. /*
  805. #pragma mark - Navigation
  806. // In a storyboard-based application, you will often want to do a little preparation before navigation
  807. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  808. // Get the new view controller using [segue destinationViewController].
  809. // Pass the selected object to the new view controller.
  810. }
  811. */
  812. @end