PortfolioViewController.m 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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 "RANetwork.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. #import "PortfolioEditQTYViewController.h"
  18. #import "RAPDFViewController.h"
  19. #import "DefaultTableHeaderView.h"
  20. #import "DefaultAppearance.h"
  21. #import "SortItemViewController.h"
  22. #import "SortButton.h"
  23. #import "TextUtils.h"
  24. #define ALERT_DEL 1025
  25. #define checked_Count @"checkedCount"
  26. @interface PortfolioViewController ()
  27. @property (nonatomic,strong) SortItemViewController *sortItemController;
  28. @property (nonatomic,assign) int sortIndex;
  29. @property (nonatomic,strong) SortButton *sortButton;
  30. @property (nonatomic,assign) NSInteger editedRow;
  31. @property (nonatomic,assign) NSInteger deletedRow;
  32. @property (strong, nonatomic) NSIndexPath *deleteIndexPath;// 点击delete action
  33. @end
  34. @implementation PortfolioViewController
  35. -(void) logout
  36. {
  37. self.content_data = nil;
  38. }
  39. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  40. {
  41. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  42. if (self) {
  43. // Custom initialization
  44. }
  45. return self;
  46. }
  47. -(void) reload_container_getdata:(bool) update_data
  48. {
  49. [super reload_container_getdata:update_data];
  50. if(update_data)
  51. [self reload_data];
  52. else
  53. [self.itemListTable reloadData];
  54. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  55. if(appDelegate.can_create_portfolio)
  56. {
  57. [self.btnCreatePort setTitle:@"Create Portfolio"];
  58. self.btnCreatePort.enabled = true;
  59. }
  60. else
  61. {
  62. [self.btnCreatePort setTitle:@""];
  63. self.btnCreatePort.enabled = false;
  64. }
  65. }
  66. -(void) showHidePrice
  67. {
  68. [self reload_container_getdata:false];
  69. // [self refresh_total];
  70. }
  71. - (IBAction)onAddtoCart:(id)sender {
  72. UIApplication * app = [UIApplication sharedApplication];
  73. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  74. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  75. if(appDelegate.bLogin==false)
  76. {
  77. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  78. // loginvc.delegate = self;
  79. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  80. loginvc.returnValue = ^(bool blogin){
  81. // UIApplication * app = [UIApplication sharedApplication];
  82. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  83. if(blogin)
  84. {
  85. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  86. {
  87. [main_vc checklogin:false];
  88. if(appDelegate.can_create_order)
  89. {
  90. NSString* msg =@"";
  91. if(appDelegate.contact_id.length>0)
  92. {
  93. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  94. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  95. }
  96. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil),NSLocalizedString(@"Create new order", nil), nil];
  97. // alert.
  98. [alert show];
  99. }
  100. else
  101. {
  102. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil), nil];
  103. // alert.
  104. [alert show];
  105. }
  106. }
  107. else
  108. {
  109. [main_vc checklogin:true];
  110. }
  111. }
  112. // self.offset = 0;
  113. // [self.content_data removeAllObjects];
  114. // [self loadpage];
  115. //
  116. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  117. };
  118. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  119. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  120. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  121. [self presentViewController:navi animated:YES completion:^{
  122. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  123. DebugLog(@"LoginViewController present.........");
  124. // self.btop = false;
  125. // <#code#>
  126. }];
  127. }
  128. else
  129. {
  130. if(appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  131. {
  132. [main_vc checklogin:false];
  133. if(appDelegate.can_create_order)
  134. {
  135. NSString* msg =@"";
  136. if(appDelegate.contact_id.length>0)
  137. {
  138. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  139. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  140. }
  141. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil),NSLocalizedString(@"Create new order", nil), nil];
  142. // alert.
  143. [alert show];
  144. }
  145. else
  146. {
  147. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil), nil];
  148. // alert.
  149. [alert show];
  150. }
  151. }
  152. else
  153. {
  154. if(appDelegate.order_code==nil)
  155. [ self neworder];
  156. else
  157. [self addtocart];
  158. }
  159. }
  160. }
  161. -(void) addtocart
  162. {
  163. NSMutableArray* checked = [[NSMutableArray alloc] init];
  164. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  165. int count = [[self.content_data valueForKey:@"count"] intValue];
  166. for(int i=0;i<count;i++)
  167. {
  168. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  169. bool check = [[item_json valueForKey:@"check"] boolValue];
  170. if(check)
  171. {
  172. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  173. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  174. }
  175. }
  176. if(checked.count==0)
  177. {
  178. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  179. return;
  180. }
  181. NSString * ids = [checked componentsJoinedByString:@","];
  182. self.btnCreatePort.enabled = false;
  183. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  184. NSDictionary* return_json = [RANetwork add_toCart:ids count:-1 name:nil];
  185. dispatch_async(dispatch_get_main_queue(), ^{
  186. self.btnCreatePort.enabled = true;
  187. if([[return_json valueForKey:@"result"] intValue]==2)
  188. {
  189. // int result=[[return_json valueForKey:@"result"] intValue];
  190. // if(result==2)
  191. // {
  192. // //successed.
  193. // NSString* order_code = [return_json valueForKey:@"order_code"];
  194. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  195. // appDelegate.order_code = order_code;
  196. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  197. // appDelegate.order_code = order_code;
  198. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false ];
  199. NSString * msg = nil;
  200. if(checked.count==1)
  201. {
  202. msg=@"1 item added to Cart";
  203. }
  204. else
  205. {
  206. msg=[NSString stringWithFormat:@"%lu items added to Cart",(unsigned long)checked.count];
  207. }
  208. [RAUtils message_alert:nil title:msg controller:self] ;
  209. // [self end_select];
  210. // }
  211. }
  212. else
  213. {
  214. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  215. }
  216. });
  217. });
  218. }
  219. -(void) neworder
  220. {
  221. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  222. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  223. NSDictionary* return_json = [RANetwork new_Order];
  224. dispatch_async(dispatch_get_main_queue(), ^{
  225. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  226. if([[return_json valueForKey:@"result"] intValue]==2)
  227. {
  228. int result=[[return_json valueForKey:@"result"] intValue];
  229. if(result==2)
  230. {
  231. //successed.
  232. NSString* order_code = [return_json valueForKey:@"orderCode"];
  233. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  234. appDelegate.order_code = order_code;
  235. [self addtocart];
  236. // [self ReloadData];
  237. // if(self.shopCartBlock!=nil)
  238. // {
  239. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  240. //
  241. //
  242. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  243. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  244. // iv.image = img;
  245. // // [self.contentView addSubview:iv];
  246. // self.shopCartBlock(iv);
  247. //
  248. // }
  249. }
  250. }
  251. else
  252. {
  253. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  254. }
  255. });
  256. });
  257. }
  258. - (IBAction)onCreateTearSheet:(id)sender {
  259. self.pdf_url= nil;
  260. NSMutableArray* checked = [[NSMutableArray alloc] init];
  261. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  262. int count = [[self.content_data valueForKey:@"count"] intValue];
  263. for(int i=0;i<count;i++)
  264. {
  265. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  266. bool check = [[item_json valueForKey:@"check"] boolValue];
  267. if(check)
  268. {
  269. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  270. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  271. }
  272. }
  273. // disable for debug
  274. if(checked.count==0)
  275. {
  276. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Create Portfolio." controller:self] ;
  277. return;
  278. }
  279. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  280. NSString * string = [checked componentsJoinedByString:@","];
  281. NSString * string_iid = [checked_iid componentsJoinedByString:@","];
  282. [params setValue:string forKey:@"product_ids"];
  283. [params setValue:string_iid forKey:@"item_ids"];
  284. [params setObject:@(self.sortIndex) forKey:@"sort"];
  285. TearSheetParamViewController * VC =[ self.storyboard instantiateViewControllerWithIdentifier:@"TearSheetParamViewController"];
  286. VC.url_type = URL_REMOTE;
  287. VC.request_url=URL_DM_PARAMS;
  288. VC.params = params;
  289. VC.product_ids=string;
  290. VC.item_ids=string_iid;
  291. // VC.delegate=self;
  292. VC.return_url=^(NSString* url,NSString* path, bool remove_item)
  293. {
  294. self.pdf_url = url;
  295. self.path = path;
  296. self.remove_item = remove_item;
  297. };
  298. VC.onSaved=^(NSString* url,NSString* name,bool isLocalFile)
  299. {
  300. // NSString* name=[self.content_data[indexPath.row] valueForKey:@"tear_name"];
  301. // NSString* url= [self.content_data[indexPath.row] valueForKey:@"pdf_path"];
  302. RAPDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  303. ViewController.url = url;
  304. ViewController.canSave = false;
  305. ViewController.isLocalfile=isLocalFile;
  306. NSString* subject;
  307. // if (company.length==0) {
  308. NSString* cur_time =[RAUtils current_date];
  309. subject =name;//[NSString stringWithFormat:@"NPD Product List %@",cur_time];
  310. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  311. NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  312. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  313. if(customer_email.length>0)
  314. {
  315. send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  316. // customer_email compo
  317. // [send_to addObject:customer_email];
  318. }
  319. ViewController.mail_to = send_to;
  320. // }
  321. // else
  322. // {
  323. // subject = [NSString stringWithFormat:@"Tear sheet for %@",company ];
  324. // }
  325. // ViewController.attachment_name = [NSString stringWithFormat:@"NPD_Product_List %@.pdf",cur_time];
  326. ViewController.filename = [NSString stringWithFormat:@"%@.pdf",[TextUtils legalFilename:name]];
  327. ViewController.mail_subject = subject;
  328. ViewController.hidenavi = false;
  329. [self.navigationController pushViewController:ViewController animated:YES];
  330. };
  331. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  332. VC.onDismiss=^(NSString* catelog_name)
  333. {
  334. };
  335. //
  336. // if(checked.count==count)
  337. // {
  338. // orderinfoVC.have_tail = true
  339. // }
  340. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:VC] ;
  341. navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  342. [self presentViewController:navi animated:YES completion:^{
  343. DebugLog(@"TearSheetParamViewController present.........");
  344. // self.btop = false;
  345. // <#code#>
  346. }];
  347. [self.btnselect setTitle:@"Select all"];
  348. }
  349. - (void)viewDidLoad
  350. {
  351. [super viewDidLoad];
  352. // UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.000001)];
  353. // [line setBackgroundColor:[UIColor clearColor]];
  354. // self.itemListTable.tableHeaderView = line;
  355. //
  356. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  357. ref.tag = 200 ;
  358. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  359. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  360. // ref.hidden = true;
  361. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  362. [self.itemListTable addSubview:ref];
  363. // [[UIToolbar appearance] setBackgroundColor: [UIColor whiteColor]];
  364. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  365. self.label_net_err.layer.borderWidth = 2.0;
  366. self.label_net_err.layer.cornerRadius=15;
  367. self.label_net_err.layer.masksToBounds=true;
  368. self.sortIndex = 0;
  369. self.editedRow = -1;
  370. self.deletedRow = -1;
  371. self.indexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
  372. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  373. // tap.minimumPressDuration = 0.8; //定义按的时间
  374. [self.label_net_err addGestureRecognizer:tap];
  375. [self initCheckedCount];
  376. }
  377. -(void)manually_refresh
  378. {
  379. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  380. if(self.isrefreshing)
  381. {
  382. [reF endRefreshing];
  383. return;
  384. }
  385. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  386. if ([self respondsToSelector:@selector(reload_data)])
  387. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  388. // DebugLog(@"refresh!!!!!!!!");
  389. }
  390. - (IBAction)onSelectClick:(id)sender {
  391. // self.total=0;
  392. int count =[[self.content_data valueForKey:@"count"] intValue];
  393. if (count) {
  394. if([self.btnselect.title isEqualToString:@"Select all"])
  395. {
  396. for(int i=0;i<count;i++)
  397. {
  398. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  399. [item_json setValue:@"1" forKey:@"check"];
  400. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  401. cell.img_checkmark.hidden=false;
  402. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  403. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  404. }
  405. [self initCheckedCount];
  406. [self.btnselect setTitle:@"Deselect all"];
  407. }
  408. else
  409. {
  410. // int count =[[self.content_data valueForKey:@"count"] intValue];
  411. for(int i=0;i<count;i++)
  412. {
  413. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  414. [item_json setValue:@"0" forKey:@"check"];
  415. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  416. cell.img_checkmark.hidden=true;
  417. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  418. }
  419. [self clearCheckedCount];
  420. [self.btnselect setTitle:@"Select all"];
  421. // self.btnselect.titleLabel.text=@"Select all";
  422. }
  423. }
  424. }
  425. - (IBAction)onEditClick:(id)sender {
  426. NSMutableArray* checked = [[NSMutableArray alloc] init];
  427. NSMutableArray* fashion_ids = [[NSMutableArray alloc] init];
  428. int count = [[self.content_data valueForKey:@"count"] intValue];
  429. int single_idx=0;
  430. for(int i=0;i<count;i++)
  431. {
  432. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  433. int check = [[item_json valueForKey:@"check"] intValue];
  434. if(check==1)
  435. {
  436. single_idx = i;
  437. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  438. [fashion_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"fashion_id"] intValue]]];
  439. }
  440. }
  441. // disable for debug
  442. if(checked.count==0)
  443. return;
  444. if(checked.count==1)
  445. {
  446. //只选中一个和右滑一样处理。
  447. {
  448. DebugLog(@"edit click");
  449. UIViewController* parentvc=self;
  450. PortfolioEditQTYViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"PortfolioEditQTYViewController"];
  451. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",single_idx]] mutableCopy];
  452. vc.arr_fashion_ids = fashion_ids;
  453. // vc.fashionid = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"fashion_id"] intValue]];
  454. if([[item_json allKeys] containsObject:@"available_qty"])
  455. vc.qty = [[item_json valueForKey:@"available_qty"] intValue];
  456. else
  457. vc.qty=-1;
  458. if([[item_json allKeys] containsObject:@"available_percent"])
  459. vc.qty_p = [[item_json valueForKey:@"available_percent"] intValue];
  460. else
  461. vc.qty_p=-1;
  462. vc.linenotes=[item_json valueForKey:@"linenotes"] ;
  463. vc.discount=[[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  464. vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  465. vc.title=@"Edit Portfolio";
  466. // vc.delegate = parentvc.self;
  467. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  468. vc.onSetValue = ^(int qty,double qty_p, double price, double discount , NSString* notes){
  469. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Updating Quantity"];
  470. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  471. NSDictionary* cart_json = nil;
  472. cart_json= [RANetwork portfolio_setPrice:[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]] price:price discount:discount QTY:qty qty_p:qty_p notes:notes];
  473. dispatch_async(dispatch_get_main_queue(), ^{
  474. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  475. if([[cart_json valueForKey:@"result"] intValue]==2)
  476. {
  477. // if(qty>=0)
  478. // item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  479. // else
  480. // [item_json removeObjectForKey:@"available_qty"];
  481. // if(qty_p>=0)
  482. // item_json[@"available_percent"]=[NSNumber numberWithInt:qty_p];
  483. // else
  484. // [item_json removeObjectForKey:@"available_percent"];
  485. // item_json[@"linenotes"]=notes;
  486. // item_json[@"tear_sheet_discount"]=[NSString stringWithFormat:@"%.2f",discount];
  487. // item_json[@"tear_sheet_price"]=[NSString stringWithFormat:@"%.2f",price];
  488. // self.content_data[[NSString stringWithFormat:@"item_%d",single_idx]]=item_json;
  489. // // item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  490. // [self.itemListTable reloadData];
  491. if (!(qty == -INT32_MAX && qty_p == -MAXFLOAT)) {
  492. // qty
  493. if (qty != -INT32_MAX)
  494. item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  495. else
  496. [item_json removeObjectForKey:@"available_qty"];
  497. // qty_p
  498. if (qty_p != -MAXFLOAT)
  499. item_json[@"available_percent"]=[NSNumber numberWithInt:qty_p];
  500. else
  501. [item_json removeObjectForKey:@"available_percent"];
  502. }
  503. if (notes) {
  504. item_json[@"linenotes"]=notes;
  505. }
  506. if (discount != -MAXFLOAT) {
  507. item_json[@"tear_sheet_discount"]=[NSString stringWithFormat:@"%.2f",discount];
  508. }
  509. if (price != -MAXFLOAT) {
  510. item_json[@"tear_sheet_price"]=[NSString stringWithFormat:@"%.2f",price];
  511. }
  512. self.content_data[[NSString stringWithFormat:@"item_%d",single_idx]]=item_json;
  513. // item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  514. [self.itemListTable reloadData];
  515. }
  516. else
  517. {
  518. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Edit Portfolio" controller:self] ;
  519. }
  520. });
  521. });
  522. //
  523. //
  524. //
  525. // [self reload_data];//[main_vc checklogin:true];
  526. };
  527. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  528. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  529. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  530. [parentvc presentViewController:navi animated:YES completion:^{
  531. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  532. DebugLog(@"EditModelPriceViewController present.........");
  533. // self.btop = false;
  534. // <#code#>
  535. }];
  536. // [//tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  537. }
  538. return;
  539. }
  540. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  541. NSString * ids = [checked componentsJoinedByString:@","];
  542. {
  543. DebugLog(@"edit click");
  544. UIViewController* parentvc=self;
  545. PortfolioEditQTYViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"PortfolioEditQTYViewController"];
  546. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  547. vc.arr_fashion_ids = nil;
  548. vc.ids_count=checked.count;
  549. // vc.fashionid = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"fashion_id"] intValue]];
  550. // vc.qty = [[item_json valueForKey:@"available_qty"] intValue];
  551. //
  552. // vc.discount=[[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  553. // vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  554. // vc.delegate = parentvc.self;
  555. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  556. vc.onSetValue = ^(int qty,double qty_p,double price, double discount , NSString* notes){
  557. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Updating Quantity"];
  558. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  559. NSDictionary* cart_json = nil;
  560. cart_json= [RANetwork portfolio_setPrice:ids price:price discount:discount QTY:qty qty_p:qty_p notes:notes];
  561. dispatch_async(dispatch_get_main_queue(), ^{
  562. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  563. if([[cart_json valueForKey:@"result"] intValue]==2)
  564. {
  565. [RAUtils message_alert:[NSString stringWithFormat:@"%d items saved.",checked.count] title:@"Edit Portfolio" controller:self] ;
  566. [self reload_data];
  567. }
  568. else
  569. {
  570. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Edit Portfolio" controller:self] ;
  571. }
  572. });
  573. });
  574. //
  575. //
  576. //
  577. // [self reload_data];//[main_vc checklogin:true];
  578. };
  579. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  580. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  581. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  582. [parentvc presentViewController:navi animated:YES completion:^{
  583. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  584. DebugLog(@"EditModelPriceViewController present.........");
  585. // self.btop = false;
  586. // <#code#>
  587. }];
  588. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  589. }
  590. }
  591. - (IBAction)onDeleteClick:(id)sender {
  592. NSMutableArray* checked = [[NSMutableArray alloc] init];
  593. int count = [[self.content_data valueForKey:@"count"] intValue];
  594. for(int i=0;i<count;i++)
  595. {
  596. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  597. int check = [[item_json valueForKey:@"check"] intValue];
  598. if(check==1)
  599. {
  600. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  601. }
  602. }
  603. // disable for debug
  604. if(checked.count==0)
  605. return;
  606. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  607. NSString * string = [checked componentsJoinedByString:@","];
  608. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  609. NSDictionary* cart_json = [RANetwork portfolio_remove:string];
  610. dispatch_async(dispatch_get_main_queue(), ^{
  611. if([[cart_json valueForKey:@"result"] intValue]==2)
  612. {
  613. [self reload_data];
  614. }
  615. else
  616. {
  617. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Remove Model From Portfolio" controller:self] ;
  618. }
  619. });
  620. });
  621. }
  622. -(void) viewWillAppear:(BOOL)animated
  623. {
  624. [super viewWillAppear:animated];
  625. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  626. }
  627. -(void) reload_data
  628. {
  629. dispatch_async(dispatch_get_main_queue(), ^{
  630. if(self.isrefreshing)
  631. return;
  632. self.isrefreshing=true;
  633. self.label_net_err.hidden=true;
  634. self.itemListTable.hidden=true;
  635. [self.content_data removeAllObjects];
  636. [self.itemListTable reloadData];
  637. self.mum.center = self.view.center;
  638. self.mum.hidden = false;
  639. [self.mum startAnimating];
  640. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  641. [reF endRefreshing];
  642. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  643. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  644. NSDictionary* cart_json = [RANetwork request_Portfolio:self.sortIndex];
  645. dispatch_async(dispatch_get_main_queue(), ^{
  646. [self.mum stopAnimating];
  647. int result=[[cart_json valueForKey:@"result"] intValue];
  648. if(result==2||result==1||result==0)
  649. {
  650. self.itemListTable.hidden=false;
  651. self.content_data = [cart_json mutableCopy];
  652. [self.itemListTable reloadData];
  653. [self initCheckedCount];
  654. [self.btnselect setTitle: @"Deselect all"];
  655. UIApplication * app = [UIApplication sharedApplication];
  656. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  657. appDelegate.port_count =[[self.content_data valueForKey:@"count"] intValue];
  658. }
  659. else
  660. if(result==RESULT_NET_ERROR)
  661. {
  662. self.label_net_err.hidden=false;
  663. self.itemListTable.hidden=true;
  664. }
  665. else
  666. {
  667. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Portfolio" controller:self] ;
  668. }
  669. self.isrefreshing=false;
  670. });
  671. });
  672. });
  673. }
  674. - (void)didReceiveMemoryWarning
  675. {
  676. [super didReceiveMemoryWarning];
  677. // Dispose of any resources that can be recreated.
  678. }
  679. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  680. //{
  681. //
  682. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  683. //
  684. //
  685. //
  686. // [self.itemListTable reloadData];
  687. //
  688. //
  689. //
  690. //}
  691. #pragma mark - Table view data source
  692. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  693. //{
  694. // return 0;
  695. //}
  696. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  697. //{
  698. // return 0;
  699. //}
  700. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  701. //{
  702. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  703. // return myView;
  704. //
  705. //}
  706. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  707. //{
  708. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  709. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  710. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  711. //// titleLabel.textColor=[UIColor whiteColor];
  712. //// titleLabel.backgroundColor = [UIColor clearColor];
  713. //// if(section==0)
  714. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  715. //// else
  716. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  717. //// [titleLabel sizeToFit];
  718. //// [myView addSubview:titleLabel];
  719. ////
  720. // return myView;
  721. //}
  722. //
  723. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  724. // if(section==0)
  725. // return nil;
  726. // else
  727. // return @"detail section";
  728. //}
  729. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  730. {
  731. return 33;
  732. }
  733. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  734. {
  735. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  736. if(value==nil)
  737. value=@"";
  738. unsigned long color = strtoul([value UTF8String],0,16);
  739. NSString *sectionTitle = @"Portfolio";
  740. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 33)];
  741. // UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
  742. // sortButton.frame = CGRectMake(20, 5.5, (33 - 5.5 * 2), (33 - 5.5 * 2));
  743. // [sortButton setImage:[UIImage imageNamed:@"check_1_24"] forState:UIControlStateNormal];
  744. // [sortButton addTarget:self action:@selector(portfolioSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  745. [myView addSubview:self.sortButton];
  746. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5.5, tableView.bounds.size.width, 22)];
  747. titlelabel.textColor=UIColorFromRGB(color);
  748. titlelabel.textAlignment=NSTextAlignmentCenter;
  749. titlelabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
  750. titlelabel.text=sectionTitle;
  751. titlelabel.center = myView.center;
  752. myView.autoresizesSubviews=true;
  753. [myView addSubview:titlelabel];
  754. return myView;
  755. }
  756. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  757. {
  758. return 1;
  759. }
  760. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  761. {
  762. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  763. int count =[[self.content_data valueForKey:@"count"] intValue];
  764. return count;
  765. }
  766. - (IBAction)imgbtnClicked:(UIButton *)sender {
  767. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  768. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  769. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  770. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  771. NSString *name = [item_json objectForKey:@"description"];
  772. NSRange subRange = [name rangeOfString:@"\n"];
  773. name = [name substringToIndex:subRange.location];
  774. NSString* product_id = [item_json valueForKey:@"product_id"];
  775. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  776. // dvc
  777. dvc.product_id=product_id;
  778. dvc.category_id=nil;
  779. dvc.ispush=true;
  780. dvc.model_name = name;
  781. [dvc reload];
  782. [self.navigationController pushViewController:dvc animated:true];
  783. }
  784. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  785. {
  786. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  787. // if(tableView==self.itemListTable)
  788. // {
  789. NSString *CellIdentifier = @"WatchListItemCell";
  790. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  791. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  792. // Button 设置了大小约束,Size就不会随内容变化
  793. [cell.btnImage setContentMode:UIViewContentModeScaleAspectFit];
  794. [cell.btnImage setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  795. [cell.btnImage setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];
  796. [cell.btnImage setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
  797. cell.btnImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
  798. NSString* img_url = [item_json valueForKey:@"img"];
  799. NSString* description = [item_json valueForKey:@"description"];
  800. // NSString* identifier = [item_json valueForKey:@"identifier"];
  801. // NSString* attribute = [item_json valueForKey:@"attribute"];
  802. // NSString* currency = [item_json valueForKey:@"currency"];
  803. // int count =[[item_json valueForKey:@"count"] intValue];
  804. float discount = [[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  805. float price =[[item_json valueForKey:@"tear_sheet_price"] floatValue];
  806. // cell.labelAttribute.text = attribute;
  807. // cell.labelCurrency.text = currency;
  808. cell.labelDescription.text = description;
  809. // cell.labelIdentifier.text = identifier;
  810. int qty;
  811. if([[item_json allKeys] containsObject:@"available_qty"])
  812. qty= [[item_json valueForKey:@"available_qty"] intValue];
  813. else
  814. qty=-1;
  815. float qty_p;
  816. if([[item_json allKeys] containsObject:@"available_percent"])
  817. qty_p= [[item_json valueForKey:@"available_percent"] floatValue];
  818. else
  819. qty_p=-1;
  820. // if(![[item_json allKeys] containsObject:@"available_qty"])
  821. // {
  822. // cell.available_qty_label.hidden=true;
  823. // cell.available_qty_value_label.hidden=true;
  824. // }
  825. // else
  826. // {
  827. // cell.available_qty_label.hidden=false;
  828. // cell.available_qty_value_label.hidden=false;
  829. // cell.available_qty_value_label.text = [NSString stringWithFormat:@"%d",qty];
  830. //
  831. // }
  832. if(qty>=0)
  833. {
  834. cell.available_qty_label.hidden=false;
  835. cell.available_qty_value_label.hidden=false;
  836. cell.available_qty_value_label.text = [NSString stringWithFormat:@"%d",qty];
  837. }
  838. else if(qty_p>=0)
  839. {
  840. cell.available_qty_label.hidden=false;
  841. cell.available_qty_value_label.hidden=false;
  842. cell.available_qty_value_label.text = [NSString stringWithFormat:@"%.2f %%",qty_p];
  843. }
  844. else
  845. {
  846. cell.available_qty_label.hidden=true;
  847. cell.available_qty_value_label.hidden=true;
  848. }
  849. NSString* str_price =[NSString stringWithFormat:@"%.2f",price * (1.0-discount/100)];
  850. bool noprice =[[item_json valueForKey:@"is_no_price"]boolValue];
  851. if(noprice)
  852. str_price=@"No Price";
  853. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  854. {
  855. }
  856. else
  857. {
  858. str_price=nil;
  859. }
  860. cell.labelPrice.text = str_price;
  861. // cell.labelPrice.text = price;
  862. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  863. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  864. int check = [[item_json valueForKey:@"check"] intValue];
  865. if(check==1)
  866. cell.img_checkmark.hidden=false;
  867. else
  868. cell.img_checkmark.hidden=true;
  869. // UIImageView* iv =cell.btnImage.imageView;
  870. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  871. [cell.btnImage setImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  872. NSString* file_name=[img_url lastPathComponent];
  873. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  874. if(img_data!=nil)
  875. {
  876. UIImage * img =[UIImage imageWithData:img_data];
  877. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  878. [cell.btnImage setImage:img forState:UIControlStateNormal];
  879. // cell.btnImage.imageView.image = img;
  880. }
  881. else
  882. {
  883. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  884. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  885. dispatch_async(dispatch_get_main_queue(), ^{
  886. if(downloadimg_data!=nil)
  887. {
  888. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  889. UIImage * img =[UIImage imageWithData:downloadimg_data];
  890. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  891. [cell.btnImage setImage:img forState:UIControlStateNormal];
  892. }
  893. else
  894. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  895. [cell.btnImage setImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  896. });
  897. });
  898. }
  899. cell.backgroundColor = [UIColor whiteColor];// [UIColor whiteColor];;
  900. // if (indexPath.row == self.editedRow) {
  901. // cell.backgroundColor = UIColorFromRGB(0xEAEBED);
  902. // }
  903. // if (indexPath.row == self.deletedRow) {
  904. // cell.backgroundColor = UIColorFromRGB(0xEAEBED);
  905. // }
  906. if (indexPath.row == self.indexPath.row) {
  907. cell.backgroundColor = UIColorFromRGB(0xEAEBED);
  908. }
  909. return cell;
  910. }
  911. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  912. {
  913. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  914. cell.selected = false;
  915. // self.dirty = true;
  916. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  917. int check = [[item_json valueForKey:@"check"] intValue];
  918. if(check==1)
  919. {
  920. [item_json setValue:@"0" forKey:@"check"];
  921. cell.img_checkmark.hidden=true;
  922. [self.btnselect setTitle: @"Select all"];
  923. [self checkedCountRise:NO];
  924. }
  925. else
  926. {
  927. [item_json setValue:@"1" forKey:@"check"];
  928. cell.img_checkmark.hidden=false;
  929. [self checkedCountRise:YES];
  930. if ([self allChecked]) {
  931. [self.btnselect setTitle: @"DeSelect all"];
  932. }
  933. }
  934. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  935. }
  936. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  937. return UITableViewCellEditingStyleDelete;
  938. }
  939. //#pragma mark - UIAlertViewDelegate
  940. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  941. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  942. //{
  943. //
  944. //
  945. //
  946. //}
  947. - (BOOL)isPureFloat:(NSString*)string{
  948. NSScanner* scan = [NSScanner scannerWithString:string];
  949. float val;
  950. return[scan scanFloat:&val] && [scan isAtEnd];
  951. }
  952. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  953. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  954. {
  955. // 添加一个删除按钮
  956. // self.indexPath=indexPath;
  957. ModelItemCell * cell = [tableView cellForRowAtIndexPath:indexPath];
  958. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  959. DebugLog(@"delete click");
  960. self.deleteIndexPath = indexPath;
  961. 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];
  962. alert.tag = ALERT_DEL;
  963. // alert.
  964. [alert show];
  965. }];
  966. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  967. // edit qty
  968. UITableViewRowAction *EditQTYAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" Edit " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  969. DebugLog(@"edit click");
  970. UIViewController* parentvc=self;
  971. PortfolioEditQTYViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"PortfolioEditQTYViewController"];
  972. __block NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  973. vc.arr_fashion_ids = [[NSArray arrayWithObject:[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"fashion_id"] intValue]]] mutableCopy];
  974. // vc.fashionid = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"fashion_id"] intValue]];
  975. if([[item_json allKeys] containsObject:@"available_qty"])
  976. vc.qty = [[item_json valueForKey:@"available_qty"] intValue];
  977. else
  978. vc.qty=-1;
  979. if([[item_json allKeys] containsObject:@"available_percent"])
  980. vc.qty_p = [[item_json valueForKey:@"available_percent"] intValue];
  981. else
  982. vc.qty_p=-1;
  983. vc.linenotes=[item_json valueForKey:@"linenotes"] ;
  984. vc.discount=[[item_json valueForKey:@"tear_sheet_discount"] floatValue];
  985. vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  986. vc.title=@"Edit Portfolio";
  987. // vc.delegate = parentvc.self;
  988. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  989. vc.onSetValue = ^(int qty,double qty_p, double price, double discount , NSString* notes){
  990. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Updating Quantity"];
  991. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  992. NSDictionary* cart_json = nil;
  993. cart_json= [RANetwork portfolio_setPrice:[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]] price:price discount:discount QTY:qty qty_p:qty_p notes:notes];
  994. dispatch_async(dispatch_get_main_queue(), ^{
  995. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  996. if([[cart_json valueForKey:@"result"] intValue]==2)
  997. {
  998. self.editedRow = indexPath.row;
  999. if (!(qty == -INT32_MAX && qty_p == -MAXFLOAT)) {
  1000. // qty
  1001. if (qty != -INT32_MAX)
  1002. item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  1003. else
  1004. [item_json removeObjectForKey:@"available_qty"];
  1005. // qty_p
  1006. if (qty_p != -MAXFLOAT)
  1007. item_json[@"available_percent"]=[NSNumber numberWithInt:qty_p];
  1008. else
  1009. [item_json removeObjectForKey:@"available_percent"];
  1010. }
  1011. if (notes) {
  1012. item_json[@"linenotes"]=notes;
  1013. }
  1014. if (discount != -MAXFLOAT) {
  1015. item_json[@"tear_sheet_discount"]=[NSString stringWithFormat:@"%.2f",discount];
  1016. }
  1017. if (price != -MAXFLOAT) {
  1018. item_json[@"tear_sheet_price"]=[NSString stringWithFormat:@"%.2f",price];
  1019. }
  1020. self.content_data[[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]=item_json;
  1021. // item_json[@"available_qty"]=[NSNumber numberWithInt:qty];
  1022. self.indexPath=indexPath;
  1023. [self.itemListTable reloadData];
  1024. }
  1025. else
  1026. {
  1027. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Edit Portfolio" controller:self] ;
  1028. }
  1029. });
  1030. });
  1031. //
  1032. //
  1033. //
  1034. // [self reload_data];//[main_vc checklogin:true];
  1035. };
  1036. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1037. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1038. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  1039. [parentvc presentViewController:navi animated:YES completion:^{
  1040. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1041. DebugLog(@"EditModelPriceViewController present.........");
  1042. // self.btop = false;
  1043. // <#code#>
  1044. }];
  1045. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1046. }];
  1047. EditQTYAction.backgroundColor = UIColorFromRGB(0xff9933);
  1048. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1049. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  1050. {
  1051. NSMutableArray* arr=[@[] mutableCopy];
  1052. [arr addObject:deleteRowAction];
  1053. if(appDelegate.can_set_tearsheet_price)
  1054. {
  1055. //[arr addObject:editRowAction];
  1056. [arr addObject:EditQTYAction];
  1057. }
  1058. return arr;
  1059. // return @[deleteRowAction, editRowAction];
  1060. }
  1061. else
  1062. return @[deleteRowAction];
  1063. }
  1064. - (void)tableView:(UITableView *)tableView
  1065. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1066. }
  1067. #pragma mark - UIAlertViewDelegate
  1068. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  1069. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  1070. {
  1071. UIApplication * app = [UIApplication sharedApplication];
  1072. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1073. // if(alertView.tag==1234)
  1074. // {
  1075. //
  1076. // // availability warring;
  1077. //
  1078. //
  1079. // if(buttonIndex!=alertView.cancelButtonIndex)
  1080. // {
  1081. // if(appDelegate.order_code==nil)
  1082. // {
  1083. // [self neworder];
  1084. // }
  1085. // else
  1086. // {
  1087. // [self addtocart];
  1088. // }
  1089. // }
  1090. // return;
  1091. // }
  1092. // int count=[self.quantity_text.text intValue];
  1093. if(alertView.tag==ALERT_DEL)
  1094. {
  1095. if(buttonIndex!=alertView.cancelButtonIndex)
  1096. {
  1097. self.indexPath = self.deleteIndexPath;
  1098. int count =[[self.content_data valueForKey:@"count"] intValue];
  1099. BOOL delete_last = self.indexPath.row == count - 1 ? YES : NO;
  1100. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1101. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1102. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  1103. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1104. NSDictionary* cart_json = [RANetwork portfolio_remove:ids];
  1105. dispatch_async(dispatch_get_main_queue(), ^{
  1106. if([[cart_json valueForKey:@"result"] intValue]==2)
  1107. {
  1108. self.deletedRow = delete_last ? -1 : self.indexPath.row;
  1109. if (delete_last) {
  1110. self.indexPath = [NSIndexPath indexPathForRow:count - 2 inSection:0];
  1111. }
  1112. [self reload_data];
  1113. }
  1114. else
  1115. {
  1116. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  1117. }
  1118. });
  1119. });
  1120. }
  1121. self.deleteIndexPath = nil;
  1122. return;
  1123. }
  1124. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1125. if(buttonIndex==alertView.cancelButtonIndex)
  1126. {
  1127. }
  1128. else if(buttonIndex==1)
  1129. {
  1130. //open exist
  1131. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  1132. ovc.showNavibar = true;
  1133. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  1134. if(appDelegate.order_code==nil)
  1135. {
  1136. [self neworder];
  1137. }
  1138. else
  1139. {
  1140. [self addtocart];
  1141. [main_vc checklogin:true];
  1142. }
  1143. };
  1144. ovc.init_style = OL_OPEN;
  1145. ovc.onCancel = ^(){
  1146. [main_vc checklogin:true];
  1147. };
  1148. [self.navigationController pushViewController:ovc animated:true];
  1149. }
  1150. else
  1151. {
  1152. //create new;
  1153. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  1154. {
  1155. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1156. cvc.showNavibar = true;
  1157. cvc.contact_type = @"Sales_Order_Customer";
  1158. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1159. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  1160. appDelegate.customerInfo = value;
  1161. if(appDelegate.order_code==nil)
  1162. [self neworder];
  1163. // [main_vc checklogin:true];
  1164. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1165. //
  1166. // if(self.returnValue)
  1167. // self.returnValue(value);
  1168. };
  1169. cvc.onCancel = ^(){
  1170. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  1171. };
  1172. cvc.onReset = ^(){
  1173. [main_vc checklogin:true];
  1174. };
  1175. [self.navigationController pushViewController:cvc animated:true];
  1176. }
  1177. else
  1178. {
  1179. [self neworder];
  1180. }
  1181. }
  1182. }
  1183. /*
  1184. #pragma mark - Navigation
  1185. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1186. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1187. // Get the new view controller using [segue destinationViewController].
  1188. // Pass the selected object to the new view controller.
  1189. }
  1190. */
  1191. #pragma mark - Jack
  1192. - (BOOL)allChecked {
  1193. int checkedCount = [[self.content_data objectForKey:checked_Count] integerValue];
  1194. int count = [[self.content_data objectForKey:@"count"] integerValue];
  1195. return checkedCount == count;
  1196. }
  1197. - (void)initCheckedCount {
  1198. NSNumber *count = [self.content_data objectForKey:@"count"];
  1199. if (!count) {
  1200. count = [NSNumber numberWithInteger:0];
  1201. }
  1202. [self.content_data setObject:count forKey:checked_Count];
  1203. }
  1204. - (void)clearCheckedCount {
  1205. [self.content_data setObject:[NSNumber numberWithInteger:0] forKey:checked_Count];
  1206. }
  1207. - (void)checkedCountRise:(BOOL)rise {
  1208. int checkedCount = [[self.content_data objectForKey:checked_Count] integerValue];
  1209. if (rise) {
  1210. checkedCount++;
  1211. } else {
  1212. checkedCount--;
  1213. }
  1214. [self.content_data setObject:[NSNumber numberWithInteger:checkedCount] forKey:checked_Count];
  1215. }
  1216. - (SortItemViewController *)sortItemController {
  1217. if (!_sortItemController) {
  1218. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(40, 40)];
  1219. _sortItemController.sortIndex = self.sortIndex;
  1220. _sortItemController.sortData = @[
  1221. @{@"title":@"Last",@"icon":@"TX_18"},
  1222. @{@"title":@"First",@"icon":@"TS_18"},
  1223. @{@"title":@"Item number a-z",@"icon":@"IX_18"},
  1224. @{@"title":@"Item number z-a",@"icon":@"IS_18"},
  1225. @{@"title":@"Description",@"icon":@"DX_18"},
  1226. @{@"title":@"Category",@"icon":@"cs_18"}
  1227. ];
  1228. __weak typeof(self) weakSelf = self;
  1229. _sortItemController.sortBlock = ^(int sort){
  1230. weakSelf.sortIndex = sort;
  1231. [weakSelf reload_data];
  1232. };
  1233. }
  1234. // _sortItemController.sortIndex = self.sortIndex;
  1235. return _sortItemController;
  1236. }
  1237. - (void)portfolioSortButtonClicked:(UIButton *)sender {
  1238. [self.view addSubview:self.sortItemController.view];
  1239. }
  1240. - (SortButton *)sortButton {
  1241. if (!_sortButton) {
  1242. _sortButton = [SortButton sortButtonWithHeight:33];
  1243. [_sortButton addTarget:self action:@selector(portfolioSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  1244. }
  1245. return _sortButton;
  1246. }
  1247. - (void)setSortIndex:(int)sortIndex {
  1248. _sortIndex = sortIndex;
  1249. NSString *selectedImageName = @"";
  1250. switch (sortIndex) {
  1251. case 0:{
  1252. selectedImageName = @"TX_22";
  1253. }
  1254. break;
  1255. case 1:{
  1256. selectedImageName = @"TS_22";
  1257. }
  1258. break;
  1259. case 2:{
  1260. selectedImageName = @"IX_22";
  1261. }
  1262. break;
  1263. case 3:{
  1264. selectedImageName = @"IS_22";
  1265. }
  1266. break;
  1267. case 4:{
  1268. selectedImageName = @"DX_22";
  1269. }
  1270. break;
  1271. case 5: {
  1272. selectedImageName = @"cs_22";
  1273. }
  1274. default:
  1275. break;
  1276. }
  1277. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  1278. }
  1279. #pragma mark - rotation
  1280. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  1281. self.sortItemController.view.frame = self.view.bounds;
  1282. }
  1283. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  1284. return YES;
  1285. }
  1286. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  1287. self.sortItemController.view.frame = self.view.bounds;
  1288. }
  1289. @end