PortfolioViewController.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 =@"Do you want to switch to a pending order or a new order?";
  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(@"Do you want to add to a pending order?", 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 =@"Do you want to switch to a pending order or a new order?";
  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(@"Do you want to add to a pending order?", 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. // PDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PDFViewController"];
  268. // ViewController.url = self.pdf_url;
  269. // ViewController.mail_content = [self.content_data valueForKey:@"email_content"];
  270. //
  271. // ViewController.save_name =catelog_name;
  272. //
  273. //
  274. // NSString* company= [appDelegate.customerInfo valueForKey:@"customer_name"];
  275. //
  276. // if(company==nil)
  277. // company=@"";
  278. // NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  279. //
  280. //
  281. // NSMutableArray* send_to = [[NSMutableArray alloc]init];
  282. // if(customer_email.length>0)
  283. // [send_to addObject:customer_email];
  284. // ViewController.mail_to = send_to;
  285. //
  286. // NSString* subject;
  287. // if (company.length==0) {
  288. // subject = @"Tear sheet";
  289. //
  290. //
  291. //
  292. // }
  293. // else
  294. // {
  295. // subject = [NSString stringWithFormat:@"Tear sheet for %@",company ];
  296. // }
  297. // ViewController.mail_subject = subject;
  298. //
  299. // ViewController.onLoadSuccess =^(NSString* url)
  300. // {
  301. // if(self.remove_item)
  302. // {
  303. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  304. //
  305. // NSDictionary* cart_json = [iSalesNetwork watchlist_remove:string_iid];
  306. //
  307. // dispatch_async(dispatch_get_main_queue(), ^{
  308. //
  309. //
  310. //
  311. // if([[cart_json valueForKey:@"result"] intValue]==2)
  312. // {
  313. // [self reload_data];
  314. // }
  315. // else
  316. // {
  317. // [RAUtils error_alert:[cart_json valueForKey:@"err_msg"] title:@"Failed to remove model from wishlist."] ;
  318. // }
  319. //
  320. // });
  321. // });
  322. // }
  323. // };
  324. //
  325. //
  326. // ViewController.onSavePDF=^(NSString* name,NSString* file)
  327. // {
  328. //
  329. // NSString* cid= [appDelegate.customerInfo valueForKey:@"customer_cid"];
  330. // if(cid==nil)
  331. // cid=@"";
  332. //
  333. // NSString *path = NSTemporaryDirectory();
  334. // NSString *temp_path = [path stringByAppendingPathComponent:file];
  335. //
  336. // [iSalesDB save_pdf:temp_path filename:file];
  337. // NSString* str_sendto=[send_to componentsJoinedByString:@";"];
  338. // NSString *sql = [NSString stringWithFormat:
  339. // @"INSERT INTO pdf_cache (name,file_name,c_id,c_name,o_id,pdf_type,send_to) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@')",
  340. // name, file,cid, company, @"", @"Tear Sheet",str_sendto];
  341. //
  342. // [iSalesDB execSql:sql];
  343. //
  344. // };
  345. //
  346. // // ViewController. = self.function_name;
  347. // // ViewController.behavior =BEHAVIOR_SEARCH;
  348. //
  349. // [self.navigationController pushViewController:ViewController animated:YES];
  350. // [[self navigationController] setNavigationBarHidden:NO animated:NO];
  351. };
  352. //
  353. // if(checked.count==count)
  354. // {
  355. // orderinfoVC.have_tail = true
  356. // }
  357. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:VC] ;
  358. navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  359. [self presentViewController:navi animated:YES completion:^{
  360. NSLog(@"TearSheetParamViewController present.........");
  361. // self.btop = false;
  362. // <#code#>
  363. }];
  364. [self.btnselect setTitle:@"Select all"];
  365. }
  366. - (void)viewDidLoad
  367. {
  368. [super viewDidLoad];
  369. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  370. ref.tag = 200 ;
  371. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  372. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  373. // ref.hidden = true;
  374. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  375. [self.itemListTable addSubview:ref];
  376. [[UIToolbar appearance] setBackgroundColor:UIColorFromRGB(0xF2EEEA)];
  377. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  378. self.label_net_err.layer.borderWidth = 2.0;
  379. self.label_net_err.layer.cornerRadius=15;
  380. self.label_net_err.layer.masksToBounds=true;
  381. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  382. // tap.minimumPressDuration = 0.8; //定义按的时间
  383. [self.label_net_err addGestureRecognizer:tap];
  384. // self.toolbar.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolbar.bounds].CGPath;
  385. // self.toolbar.layer.masksToBounds = false;
  386. //// //添加四个边阴影
  387. //
  388. // self.toolbar.layer.shadowColor = [UIColor blackColor].CGColor;
  389. // self.toolbar.layer.shadowOffset = CGSizeMake(0, 0);
  390. // self.toolbar.layer.shadowOpacity = 0.5;
  391. // self.toolbar.layer.shadowRadius = 2.0;
  392. //
  393. // self.toolbar.layer.borderWidth = 1;
  394. // self.toolbar.layer.borderColor = [[UIColor blackColor] CGColor];
  395. //
  396. // self.toolbar.clipsToBounds = YES;
  397. // [self reload_data];
  398. }
  399. -(void)manually_refresh
  400. {
  401. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  402. if(self.isrefreshing)
  403. {
  404. [reF endRefreshing];
  405. return;
  406. }
  407. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  408. if ([self respondsToSelector:@selector(reload_data)])
  409. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  410. // DebugLog(@"refresh!!!!!!!!");
  411. }
  412. - (IBAction)onSelectClick:(id)sender {
  413. // self.total=0;
  414. if([self.btnselect.title isEqualToString:@"Select all"])
  415. {
  416. int count =[[self.content_data valueForKey:@"count"] intValue];
  417. for(int i=0;i<count;i++)
  418. {
  419. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  420. [item_json setValue:@"1" forKey:@"check"];
  421. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  422. cell.img_checkmark.hidden=false;
  423. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  424. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  425. }
  426. [self.btnselect setTitle:@"Deselect all"];
  427. }
  428. else
  429. {
  430. int count =[[self.content_data valueForKey:@"count"] intValue];
  431. for(int i=0;i<count;i++)
  432. {
  433. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  434. [item_json setValue:@"0" forKey:@"check"];
  435. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  436. cell.img_checkmark.hidden=true;
  437. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  438. }
  439. [self.btnselect setTitle:@"Select all"];
  440. // self.btnselect.titleLabel.text=@"Select all";
  441. }
  442. }
  443. - (IBAction)onDeleteClick:(id)sender {
  444. NSMutableArray* checked = [[NSMutableArray alloc] init];
  445. int count = [[self.content_data valueForKey:@"count"] intValue];
  446. for(int i=0;i<count;i++)
  447. {
  448. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  449. int check = [[item_json valueForKey:@"check"] intValue];
  450. if(check==1)
  451. {
  452. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  453. }
  454. }
  455. // disable for debug
  456. if(checked.count==0)
  457. return;
  458. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  459. NSString * string = [checked componentsJoinedByString:@","];
  460. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  461. NSDictionary* cart_json = [iSalesNetwork portfolio_remove:string];
  462. dispatch_async(dispatch_get_main_queue(), ^{
  463. if([[cart_json valueForKey:@"result"] intValue]==2)
  464. {
  465. [self reload_data];
  466. }
  467. else
  468. {
  469. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Remove Model From Portfolio" controller:self] ;
  470. }
  471. });
  472. });
  473. }
  474. -(void) viewWillAppear:(BOOL)animated
  475. {
  476. [super viewWillAppear:animated];
  477. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  478. }
  479. -(void) reload_data
  480. {
  481. dispatch_async(dispatch_get_main_queue(), ^{
  482. if(self.isrefreshing)
  483. return;
  484. self.isrefreshing=true;
  485. self.label_net_err.hidden=true;
  486. self.itemListTable.hidden=true;
  487. [self.content_data removeAllObjects];
  488. [self.itemListTable reloadData];
  489. self.mum.center = self.view.center;
  490. self.mum.hidden = false;
  491. [self.mum startAnimating];
  492. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  493. [reF endRefreshing];
  494. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  495. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  496. NSDictionary* cart_json = [iSalesNetwork request_Portfolio];
  497. dispatch_async(dispatch_get_main_queue(), ^{
  498. [self.mum stopAnimating];
  499. int result=[[cart_json valueForKey:@"result"] intValue];
  500. if(result==2||result==1||result==0)
  501. {
  502. self.itemListTable.hidden=false;
  503. self.content_data = [cart_json mutableCopy];
  504. [self.itemListTable reloadData];
  505. UIApplication * app = [UIApplication sharedApplication];
  506. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  507. appDelegate.port_count =[[self.content_data valueForKey:@"count"] intValue];
  508. }
  509. else
  510. if(result==RESULT_NET_ERROR)
  511. {
  512. self.label_net_err.hidden=false;
  513. self.itemListTable.hidden=true;
  514. }
  515. else
  516. {
  517. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Watchlist" controller:self] ;
  518. }
  519. self.isrefreshing=false;
  520. });
  521. });
  522. });
  523. }
  524. - (void)didReceiveMemoryWarning
  525. {
  526. [super didReceiveMemoryWarning];
  527. // Dispose of any resources that can be recreated.
  528. }
  529. #pragma mark - Table view data source
  530. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  531. //{
  532. // return 0;
  533. //}
  534. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  535. //{
  536. // return 0;
  537. //}
  538. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  539. //{
  540. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  541. // return myView;
  542. //
  543. //}
  544. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  545. //{
  546. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  547. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  548. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  549. //// titleLabel.textColor=[UIColor whiteColor];
  550. //// titleLabel.backgroundColor = [UIColor clearColor];
  551. //// if(section==0)
  552. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  553. //// else
  554. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  555. //// [titleLabel sizeToFit];
  556. //// [myView addSubview:titleLabel];
  557. ////
  558. // return myView;
  559. //}
  560. //
  561. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  562. // if(section==0)
  563. // return nil;
  564. // else
  565. // return @"detail section";
  566. //}
  567. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  568. {
  569. return 1;
  570. }
  571. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  572. {
  573. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  574. int count =[[self.content_data valueForKey:@"count"] intValue];
  575. return count;
  576. }
  577. - (IBAction)imgbtnClicked:(UIButton *)sender {
  578. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  579. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  580. // NSLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  581. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  582. NSString* product_id = [item_json valueForKey:@"product_id"];
  583. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  584. // dvc
  585. dvc.product_id=product_id;
  586. dvc.category_id=nil;
  587. dvc.ispush=true;
  588. [dvc reload];
  589. [self.navigationController pushViewController:dvc animated:true];
  590. }
  591. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  592. {
  593. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  594. // if(tableView==self.itemListTable)
  595. // {
  596. NSString *CellIdentifier = @"WatchListItemCell";
  597. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  598. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  599. NSString* img_url = [item_json valueForKey:@"img"];
  600. NSString* description = [item_json valueForKey:@"description"];
  601. // NSString* identifier = [item_json valueForKey:@"identifier"];
  602. // NSString* attribute = [item_json valueForKey:@"attribute"];
  603. // NSString* currency = [item_json valueForKey:@"currency"];
  604. // int count =[[item_json valueForKey:@"count"] intValue];
  605. float discount = [[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  606. float price =[[item_json valueForKey:@"tear_sheet_price"] floatValue];
  607. // cell.labelAttribute.text = attribute;
  608. // cell.labelCurrency.text = currency;
  609. cell.labelDescription.text = description;
  610. // cell.labelIdentifier.text = identifier;
  611. NSString* str_price =[NSString stringWithFormat:@"%.2f",price * (1.0-discount/100)];
  612. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  613. {
  614. }
  615. else
  616. {
  617. str_price=nil;
  618. }
  619. cell.labelPrice.text = str_price;
  620. // cell.labelPrice.text = price;
  621. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  622. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  623. int check = [[item_json valueForKey:@"check"] intValue];
  624. if(check==1)
  625. cell.img_checkmark.hidden=false;
  626. else
  627. cell.img_checkmark.hidden=true;
  628. // UIImageView* iv =cell.btnImage.imageView;
  629. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  630. NSString* file_name=[img_url lastPathComponent];
  631. NSData* img_data=[iSalesDB load_cached_img:file_name];
  632. if(img_data!=nil)
  633. {
  634. UIImage * img =[UIImage imageWithData:img_data];
  635. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  636. // cell.btnImage.imageView.image = img;
  637. }
  638. else
  639. {
  640. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  641. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  642. dispatch_async(dispatch_get_main_queue(), ^{
  643. if(downloadimg_data!=nil)
  644. {
  645. [iSalesDB cache_img:downloadimg_data :file_name ];
  646. UIImage * img =[UIImage imageWithData:downloadimg_data];
  647. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  648. }
  649. else
  650. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  651. });
  652. });
  653. }
  654. cell.backgroundColor = UIColorFromRGB(0xF2EEEA);
  655. return cell;
  656. }
  657. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  658. {
  659. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  660. cell.selected = false;
  661. // self.dirty = true;
  662. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  663. int check = [[item_json valueForKey:@"check"] intValue];
  664. if(check==1)
  665. {
  666. [item_json setValue:@"0" forKey:@"check"];
  667. cell.img_checkmark.hidden=true;
  668. [self.btnselect setTitle: @"Select all"];
  669. }
  670. else
  671. {
  672. [item_json setValue:@"1" forKey:@"check"];
  673. cell.img_checkmark.hidden=false;
  674. }
  675. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  676. }
  677. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  678. return UITableViewCellEditingStyleDelete;
  679. }
  680. //- (void)tableView:(UITableView *)tableView
  681. //commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  682. //
  683. //
  684. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  685. //
  686. // NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  687. //
  688. //
  689. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  690. //
  691. // NSDictionary* cart_json = [iSalesNetwork watchlist_remove:ids];
  692. //
  693. // dispatch_async(dispatch_get_main_queue(), ^{
  694. //
  695. // if([[cart_json valueForKey:@"result"] intValue]==2)
  696. // {
  697. // [self reload_data];
  698. // }
  699. // else
  700. // {
  701. // [RAUtils error_alert:[cart_json valueForKey:@"err_msg"] title:@"Failed to remove model from wishlist."] ;
  702. // }
  703. //
  704. //
  705. //
  706. // });
  707. // });
  708. //}
  709. //#pragma mark - UIAlertViewDelegate
  710. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  711. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  712. //{
  713. //
  714. //
  715. //
  716. //}
  717. - (BOOL)isPureFloat:(NSString*)string{
  718. NSScanner* scan = [NSScanner scannerWithString:string];
  719. float val;
  720. return[scan scanFloat:&val] && [scan isAtEnd];
  721. }
  722. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  723. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  724. {
  725. // 添加一个删除按钮
  726. self.indexPath=indexPath;
  727. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  728. NSLog(@"delete click");
  729. 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];
  730. alert.tag = ALERT_DEL;
  731. // alert.
  732. [alert show];
  733. }];
  734. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  735. // 添加一个编辑按钮
  736. UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" Edit "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  737. NSLog(@"edit click");
  738. UIViewController* parentvc=self;
  739. EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  740. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  741. vc.set_watch_list=true;
  742. vc.discount=[[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  743. vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  744. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  745. // vc.delegate = parentvc.self;
  746. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  747. vc.onSetValue = ^(double price,double discount){
  748. [self reload_data];//[main_vc checklogin:true];
  749. };
  750. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  751. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  752. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  753. [parentvc presentViewController:navi animated:YES completion:^{
  754. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  755. NSLog(@"EditModelPriceViewController present.........");
  756. // self.btop = false;
  757. // <#code#>
  758. }];
  759. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  760. }];
  761. editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  762. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  763. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  764. {
  765. NSMutableArray* arr=[@[] mutableCopy];
  766. [arr addObject:deleteRowAction];
  767. if(appDelegate.can_set_tearsheet_price)
  768. {
  769. [arr addObject:editRowAction];
  770. }
  771. return arr;
  772. // return @[deleteRowAction, editRowAction];
  773. }
  774. else
  775. return @[deleteRowAction];
  776. }
  777. - (void)tableView:(UITableView *)tableView
  778. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  779. }
  780. #pragma mark - UIAlertViewDelegate
  781. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  782. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  783. {
  784. UIApplication * app = [UIApplication sharedApplication];
  785. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  786. // if(alertView.tag==1234)
  787. // {
  788. //
  789. // // availability warring;
  790. //
  791. //
  792. // if(buttonIndex!=alertView.cancelButtonIndex)
  793. // {
  794. // if(appDelegate.order_code==nil)
  795. // {
  796. // [self neworder];
  797. // }
  798. // else
  799. // {
  800. // [self addtocart];
  801. // }
  802. // }
  803. // return;
  804. // }
  805. // int count=[self.quantity_text.text intValue];
  806. if(alertView.tag==ALERT_DEL)
  807. {
  808. if(buttonIndex!=alertView.cancelButtonIndex)
  809. {
  810. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  811. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  812. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  813. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  814. NSDictionary* cart_json = [iSalesNetwork portfolio_remove:ids];
  815. dispatch_async(dispatch_get_main_queue(), ^{
  816. if([[cart_json valueForKey:@"result"] intValue]==2)
  817. {
  818. [self reload_data];
  819. }
  820. else
  821. {
  822. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  823. }
  824. });
  825. });
  826. }
  827. return;
  828. }
  829. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  830. if(buttonIndex==alertView.cancelButtonIndex)
  831. {
  832. }
  833. else if(buttonIndex==1)
  834. {
  835. //open exist
  836. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  837. ovc.showNavibar = true;
  838. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  839. if(appDelegate.order_code==nil)
  840. {
  841. [self neworder];
  842. }
  843. else
  844. {
  845. [self addtocart];
  846. [main_vc checklogin:true];
  847. }
  848. };
  849. ovc.init_style = OL_OPEN;
  850. ovc.onCancel = ^(){
  851. [main_vc checklogin:true];
  852. };
  853. [self.navigationController pushViewController:ovc animated:true];
  854. }
  855. else
  856. {
  857. //create new;
  858. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  859. {
  860. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  861. cvc.showNavibar = true;
  862. cvc.contact_type = @"Sales_Order_Customer";
  863. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  864. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  865. appDelegate.customerInfo = value;
  866. if(appDelegate.order_code==nil)
  867. [self neworder];
  868. // [main_vc checklogin:true];
  869. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  870. //
  871. // if(self.returnValue)
  872. // self.returnValue(value);
  873. };
  874. cvc.onCancel = ^(){
  875. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  876. };
  877. cvc.onReset = ^(){
  878. [main_vc checklogin:true];
  879. };
  880. [self.navigationController pushViewController:cvc animated:true];
  881. }
  882. else
  883. {
  884. [self neworder];
  885. }
  886. }
  887. }
  888. /*
  889. #pragma mark - Navigation
  890. // In a storyboard-based application, you will often want to do a little preparation before navigation
  891. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  892. // Get the new view controller using [segue destinationViewController].
  893. // Pass the selected object to the new view controller.
  894. }
  895. */
  896. @end