PortfolioViewController.m 62 KB

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