PortfolioViewController.m 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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 if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  1062. if (appDelegate.customer_type == CustomerTypeStore) {
  1063. return @[deleteRowAction];
  1064. } else {
  1065. if(appDelegate.can_set_tearsheet_price) {
  1066. return @[deleteRowAction,EditQTYAction];
  1067. } else {
  1068. return @[deleteRowAction];
  1069. }
  1070. }
  1071. }
  1072. else
  1073. return @[deleteRowAction];
  1074. }
  1075. - (void)tableView:(UITableView *)tableView
  1076. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1077. }
  1078. #pragma mark - UIAlertViewDelegate
  1079. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  1080. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  1081. {
  1082. UIApplication * app = [UIApplication sharedApplication];
  1083. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1084. // if(alertView.tag==1234)
  1085. // {
  1086. //
  1087. // // availability warring;
  1088. //
  1089. //
  1090. // if(buttonIndex!=alertView.cancelButtonIndex)
  1091. // {
  1092. // if(appDelegate.order_code==nil)
  1093. // {
  1094. // [self neworder];
  1095. // }
  1096. // else
  1097. // {
  1098. // [self addtocart];
  1099. // }
  1100. // }
  1101. // return;
  1102. // }
  1103. // int count=[self.quantity_text.text intValue];
  1104. if(alertView.tag==ALERT_DEL)
  1105. {
  1106. if(buttonIndex!=alertView.cancelButtonIndex)
  1107. {
  1108. self.indexPath = self.deleteIndexPath;
  1109. int count =[[self.content_data valueForKey:@"count"] intValue];
  1110. BOOL delete_last = self.indexPath.row == count - 1 ? YES : NO;
  1111. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1112. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1113. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  1114. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1115. NSDictionary* cart_json = [RANetwork portfolio_remove:ids];
  1116. dispatch_async(dispatch_get_main_queue(), ^{
  1117. if([[cart_json valueForKey:@"result"] intValue]==2)
  1118. {
  1119. self.deletedRow = delete_last ? -1 : self.indexPath.row;
  1120. if (delete_last) {
  1121. self.indexPath = [NSIndexPath indexPathForRow:count - 2 inSection:0];
  1122. }
  1123. [self reload_data];
  1124. }
  1125. else
  1126. {
  1127. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  1128. }
  1129. });
  1130. });
  1131. }
  1132. self.deleteIndexPath = nil;
  1133. return;
  1134. }
  1135. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1136. if(buttonIndex==alertView.cancelButtonIndex)
  1137. {
  1138. }
  1139. else if(buttonIndex==1)
  1140. {
  1141. //open exist
  1142. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  1143. ovc.showNavibar = true;
  1144. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  1145. if(appDelegate.order_code==nil)
  1146. {
  1147. [self neworder];
  1148. }
  1149. else
  1150. {
  1151. [self addtocart];
  1152. [main_vc checklogin:true];
  1153. }
  1154. };
  1155. ovc.init_style = OL_OPEN;
  1156. ovc.onCancel = ^(){
  1157. [main_vc checklogin:true];
  1158. };
  1159. [self.navigationController pushViewController:ovc animated:true];
  1160. }
  1161. else
  1162. {
  1163. //create new;
  1164. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  1165. {
  1166. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1167. cvc.showNavibar = true;
  1168. cvc.contact_type = @"Sales_Order_Customer";
  1169. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1170. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  1171. appDelegate.customerInfo = value;
  1172. if(appDelegate.order_code==nil)
  1173. [self neworder];
  1174. // [main_vc checklogin:true];
  1175. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1176. //
  1177. // if(self.returnValue)
  1178. // self.returnValue(value);
  1179. };
  1180. cvc.onCancel = ^(){
  1181. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  1182. };
  1183. cvc.onReset = ^(){
  1184. [main_vc checklogin:true];
  1185. };
  1186. [self.navigationController pushViewController:cvc animated:true];
  1187. }
  1188. else
  1189. {
  1190. [self neworder];
  1191. }
  1192. }
  1193. }
  1194. /*
  1195. #pragma mark - Navigation
  1196. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1197. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1198. // Get the new view controller using [segue destinationViewController].
  1199. // Pass the selected object to the new view controller.
  1200. }
  1201. */
  1202. #pragma mark - Jack
  1203. - (BOOL)allChecked {
  1204. int checkedCount = [[self.content_data objectForKey:checked_Count] integerValue];
  1205. int count = [[self.content_data objectForKey:@"count"] integerValue];
  1206. return checkedCount == count;
  1207. }
  1208. - (void)initCheckedCount {
  1209. NSNumber *count = [self.content_data objectForKey:@"count"];
  1210. if (!count) {
  1211. count = [NSNumber numberWithInteger:0];
  1212. }
  1213. [self.content_data setObject:count forKey:checked_Count];
  1214. }
  1215. - (void)clearCheckedCount {
  1216. [self.content_data setObject:[NSNumber numberWithInteger:0] forKey:checked_Count];
  1217. }
  1218. - (void)checkedCountRise:(BOOL)rise {
  1219. int checkedCount = [[self.content_data objectForKey:checked_Count] integerValue];
  1220. if (rise) {
  1221. checkedCount++;
  1222. } else {
  1223. checkedCount--;
  1224. }
  1225. [self.content_data setObject:[NSNumber numberWithInteger:checkedCount] forKey:checked_Count];
  1226. }
  1227. - (SortItemViewController *)sortItemController {
  1228. if (!_sortItemController) {
  1229. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(40, 40)];
  1230. _sortItemController.sortIndex = self.sortIndex;
  1231. _sortItemController.sortData = @[
  1232. @{@"title":@"Last",@"icon":@"TX_18"},
  1233. @{@"title":@"First",@"icon":@"TS_18"},
  1234. @{@"title":@"Item number a-z",@"icon":@"IX_18"},
  1235. @{@"title":@"Item number z-a",@"icon":@"IS_18"},
  1236. @{@"title":@"Description",@"icon":@"DX_18"},
  1237. @{@"title":@"Category",@"icon":@"cs_18"}
  1238. ];
  1239. __weak typeof(self) weakSelf = self;
  1240. _sortItemController.sortBlock = ^(int sort){
  1241. weakSelf.sortIndex = sort;
  1242. [weakSelf reload_data];
  1243. };
  1244. }
  1245. // _sortItemController.sortIndex = self.sortIndex;
  1246. return _sortItemController;
  1247. }
  1248. - (void)portfolioSortButtonClicked:(UIButton *)sender {
  1249. [self.view addSubview:self.sortItemController.view];
  1250. }
  1251. - (SortButton *)sortButton {
  1252. if (!_sortButton) {
  1253. _sortButton = [SortButton sortButtonWithHeight:33];
  1254. [_sortButton addTarget:self action:@selector(portfolioSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  1255. }
  1256. return _sortButton;
  1257. }
  1258. - (void)setSortIndex:(int)sortIndex {
  1259. _sortIndex = sortIndex;
  1260. NSString *selectedImageName = @"";
  1261. switch (sortIndex) {
  1262. case 0:{
  1263. selectedImageName = @"TX_22";
  1264. }
  1265. break;
  1266. case 1:{
  1267. selectedImageName = @"TS_22";
  1268. }
  1269. break;
  1270. case 2:{
  1271. selectedImageName = @"IX_22";
  1272. }
  1273. break;
  1274. case 3:{
  1275. selectedImageName = @"IS_22";
  1276. }
  1277. break;
  1278. case 4:{
  1279. selectedImageName = @"DX_22";
  1280. }
  1281. break;
  1282. case 5: {
  1283. selectedImageName = @"cs_22";
  1284. }
  1285. default:
  1286. break;
  1287. }
  1288. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  1289. }
  1290. #pragma mark - rotation
  1291. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  1292. self.sortItemController.view.frame = self.view.bounds;
  1293. }
  1294. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  1295. return YES;
  1296. }
  1297. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  1298. self.sortItemController.view.frame = self.view.bounds;
  1299. }
  1300. - (void)reRefreshView {
  1301. [self.itemListTable reloadData];
  1302. }
  1303. @end