DetailHeaderCell.m 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. //
  2. // DetailHeaderCell.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-6-5.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "DetailHeaderCell.h"
  9. #import "iSalesNetwork.h"
  10. #import "MainViewController.h"
  11. #import "LoginViewController.h"
  12. #import "ContactListViewController.h"
  13. #import "RAUtils.h"
  14. #import "OrderListViewController.h"
  15. #import "ImageScrollerViewController.h"
  16. #import "CartUtils.h"
  17. #import "CustomIOSAlertView.h"
  18. #import "TextUtils.h"
  19. #import "ContentPreviewController.h"
  20. @implementation DetailHeaderCell
  21. @synthesize celldelegate;
  22. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. // Initialization code
  27. }
  28. // self.selector_imageView.layer.borderColor = [UIColorFromRGB(0x996633) CGColor];
  29. // self.selector_imageView.layer.borderWidth = 1;
  30. return self;
  31. }
  32. - (void)awakeFromNib
  33. {
  34. // self.selector_imageView.layer.borderColor = [UIColorFromRGB(0x996633) CGColor];
  35. // self.selector_imageView.layer.borderWidth = 1;
  36. self.quantity_text.delegate = self;
  37. self.selector_Button.layer.borderWidth = 0.5;
  38. self.selector_Button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  39. self.selector_Button.layer.cornerRadius = 0;
  40. [self.descriptionRTLabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  41. [self.descriptionRTLabel setTextAlignment:RTTextAlignmentCenter];
  42. [self.descriptionRTLabel setFont:[UIFont systemFontOfSize:22]];
  43. // self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  44. // self.label_net_err.layer.borderWidth = 2.0;
  45. // self.label_net_err.layer.cornerRadius=15;
  46. // self.label_net_err.layer.masksToBounds=true;
  47. // Initialization code
  48. [self addObserver:self
  49. forKeyPath:@"stack_contents"
  50. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  51. context:@"content changed"];
  52. }
  53. -(void) dealloc
  54. {
  55. [self removeObserver:self forKeyPath:@"stack_contents"];
  56. }
  57. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  58. {
  59. if([keyPath isEqualToString:@"stack_contents"])
  60. {
  61. [self.playerView stopVideo];
  62. self.playerView.hidden=true;
  63. }
  64. }
  65. - (IBAction)add_toPortfolio:(id)sender {
  66. // int count=[self.quantity_text.text intValue];
  67. UIApplication * app = [UIApplication sharedApplication];
  68. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  69. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  70. [[NSNotificationCenter defaultCenter] postNotificationName:No_Rights_Notification object:nil];
  71. return;
  72. }
  73. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  74. if(appDelegate.bLogin==false)
  75. {
  76. UIViewController *vc= [RAUtils getViewController:self];
  77. LoginViewController * loginvc =[ vc.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(self.set_update_data)
  86. self.set_update_data(true);
  87. [main_vc checklogin:true];
  88. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  89. NSDictionary* return_json = [iSalesNetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. if([[return_json valueForKey:@"result"] intValue]==2)
  92. {
  93. // DebugLog(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  94. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  95. if(self.WatchlistBlock!=nil)
  96. {
  97. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  98. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  99. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  100. iv.image = img;
  101. // [self.contentView addSubview:iv];
  102. self.PortfolioBlock(iv);
  103. }
  104. }
  105. else
  106. {
  107. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  108. }
  109. });
  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. [vc 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. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  131. NSDictionary* return_json = [iSalesNetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. if([[return_json valueForKey:@"result"] intValue]==2)
  134. {
  135. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  136. if(self.WatchlistBlock!=nil)
  137. {
  138. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  139. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  140. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  141. iv.image = img;
  142. // [self.contentView addSubview:iv];
  143. self.PortfolioBlock(iv);
  144. }
  145. }
  146. else
  147. {
  148. UIViewController *vc= [RAUtils getViewController:self];
  149. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  150. }
  151. });
  152. });
  153. }
  154. }
  155. - (IBAction)add_toWatchList:(id)sender {
  156. // int count=[self.quantity_text.text intValue];
  157. UIApplication * app = [UIApplication sharedApplication];
  158. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  159. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  160. if(appDelegate.bLogin==false)
  161. {
  162. UIViewController *vc= [RAUtils getViewController:self];
  163. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  164. // loginvc.delegate = self;
  165. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  166. loginvc.returnValue = ^(bool blogin){
  167. // UIApplication * app = [UIApplication sharedApplication];
  168. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  169. if(blogin)
  170. {
  171. if(self.set_update_data)
  172. self.set_update_data(true);
  173. [main_vc checklogin:true];
  174. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  175. NSDictionary* return_json = [iSalesNetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. if([[return_json valueForKey:@"result"] intValue]==2)
  178. {
  179. // DebugLog(@"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  180. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  181. if(self.WatchlistBlock!=nil)
  182. {
  183. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  184. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  185. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  186. iv.image = img;
  187. // [self.contentView addSubview:iv];
  188. self.WatchlistBlock(iv);
  189. }
  190. }
  191. else
  192. {
  193. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  194. }
  195. });
  196. });
  197. }
  198. // self.offset = 0;
  199. // [self.content_data removeAllObjects];
  200. // [self loadpage];
  201. //
  202. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  203. };
  204. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  205. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  206. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  207. [vc presentViewController:navi animated:YES completion:^{
  208. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  209. DebugLog(@"LoginViewController present.........");
  210. // self.btop = false;
  211. // <#code#>
  212. }];
  213. }
  214. else
  215. {
  216. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  217. NSDictionary* return_json = [iSalesNetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  218. dispatch_async(dispatch_get_main_queue(), ^{
  219. if([[return_json valueForKey:@"result"] intValue]==2)
  220. {
  221. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  222. if(self.WatchlistBlock!=nil)
  223. {
  224. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  225. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  226. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  227. iv.image = img;
  228. // [self.contentView addSubview:iv];
  229. self.WatchlistBlock(iv);
  230. }
  231. }
  232. else
  233. {
  234. UIViewController *vc= [RAUtils getViewController:self];
  235. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  236. }
  237. });
  238. });
  239. }
  240. }
  241. - (IBAction)add_toCart:(id)sender {
  242. UIViewController *vc= [RAUtils getViewController:self];
  243. [CartUtils add_to_cart:vc selectorholder:self selector:@selector(addtocart)];
  244. return;
  245. int count=[self.quantity_text.text intValue];
  246. UIApplication * app = [UIApplication sharedApplication];
  247. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  248. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  249. if(appDelegate.bLogin==false)
  250. {
  251. UIViewController *vc= [RAUtils getViewController:self];
  252. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  253. // loginvc.delegate = self;
  254. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  255. loginvc.returnValue = ^(bool blogin){
  256. // UIApplication * app = [UIApplication sharedApplication];
  257. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  258. if(blogin)
  259. {
  260. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  261. {
  262. if(self.set_update_data)
  263. self.set_update_data(true);
  264. [main_vc checklogin:false];
  265. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"Do you want to add to pending a order or a new order", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  266. // alert.
  267. [alert show];
  268. }
  269. else
  270. {
  271. [main_vc checklogin:true];
  272. int c=[self.count_Label.text intValue];
  273. DebugLog(@"count label : %@ , convert:%d", self.count_Label.text ,c);
  274. NSString* ct=[self.count_Label.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  275. if(!appDelegate.can_create_backorder && c<count)
  276. {
  277. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Availability %@",ct ] message:NSLocalizedString(@"Item is OUT OF STOCK and not available to order from NPD.\nAdd to cart anyway?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"No", nil) otherButtonTitles:NSLocalizedString(@"Yes", nil), nil];
  278. // alert.
  279. alert.tag = 1234;
  280. [alert show];
  281. }
  282. else
  283. {
  284. if(appDelegate.order_code==nil)
  285. [ self neworder];
  286. else
  287. [self addtocart:count];
  288. }
  289. }
  290. }
  291. // self.offset = 0;
  292. // [self.content_data removeAllObjects];
  293. // [self loadpage];
  294. //
  295. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  296. };
  297. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  298. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  299. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  300. [vc presentViewController:navi animated:YES completion:^{
  301. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  302. DebugLog(@"LoginViewController present.........");
  303. // self.btop = false;
  304. // <#code#>
  305. }];
  306. }
  307. else
  308. {
  309. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  310. {
  311. [main_vc checklogin:false];
  312. if(appDelegate.can_create_order)
  313. {
  314. NSString* msg =@"";
  315. if(appDelegate.contact_id.length>0)
  316. {
  317. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  318. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  319. }
  320. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  321. // alert.
  322. [alert show];
  323. }
  324. else
  325. {
  326. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"Do you want to add to a pending order?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  327. // alert.
  328. [alert show];
  329. }
  330. }
  331. else
  332. {
  333. // if ([self.priceLabel.text isEqualToString:@"No Price."])
  334. // {
  335. //// UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Model No Price" message:@"Cannot add model without price." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  336. ////
  337. ////
  338. ////
  339. //// [alert show];
  340. //
  341. //
  342. //
  343. // [RAUtils alert_view:@"Cannot add model without price." title:@"Model No Price."];
  344. // }
  345. // else
  346. {
  347. int c=[self.count_Label.text intValue];
  348. DebugLog(@"count label : %@ , convert:%d", self.count_Label.text ,c);
  349. NSString* ct=[self.count_Label.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  350. if(c<count)
  351. {
  352. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Availability %@",ct ] message:NSLocalizedString(@"Confirm add to cart. ", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Confirm", nil), nil];
  353. // alert.
  354. alert.tag = 1234;
  355. [alert show];
  356. }
  357. else
  358. {
  359. if(appDelegate.order_code==nil)
  360. [ self neworder];
  361. else
  362. [self addtocart:count];
  363. }
  364. }
  365. }
  366. }
  367. }
  368. -(void) addtocart
  369. {
  370. int count=[self.quantity_text.text intValue];
  371. int c=[self.count_Label.text intValue];
  372. DebugLog(@"count label : %@ , convert:%d", self.count_Label.text ,c);
  373. NSString* ct=[self.count_Label.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  374. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  375. if(/*c<count*/!appDelegate.can_create_backorder && c == 0)
  376. {
  377. NSString *msg = nil;
  378. BOOL continueAdd = YES;
  379. #ifdef BUILD_NPD
  380. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  381. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  382. if ([Singleton sharedInstance].currentOrderIsMerged) { // Purchase Order不允许购买数量大于库存
  383. msg = @"Item is OUT OF STOCK and not available to order from NPD.";
  384. continueAdd = NO;
  385. } else { // Shop Order购买数量大于库存需要确认
  386. msg = @"Item is OUT OF STOCK and not available to order from NPD.\nAdd to cart anyway?";
  387. continueAdd = YES;
  388. }
  389. }
  390. if (appDelegate.user_type == USER_ROLE_EMPLOYEE) { // Employee不允许购买数量大于库存
  391. msg = @"Item is OUT OF STOCK and not available to order.";
  392. continueAdd = NO;
  393. }
  394. #endif
  395. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Availability %@",ct ] message:msg preferredStyle:UIAlertControllerStyleAlert];
  396. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  397. if (continueAdd) {
  398. [self addtocart:count];
  399. }
  400. }];
  401. [alertControl addAction:alertthree];
  402. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  403. }];
  404. if (continueAdd) {
  405. [alertControl addAction:alertcancel];
  406. }
  407. UIViewController *vc= [RAUtils getViewController:self];
  408. [vc presentViewController:alertControl animated:YES completion:nil];
  409. }
  410. else
  411. {
  412. [self addtocart:count];
  413. }
  414. }
  415. -(void)Hide_selector:(bool) bhide
  416. {
  417. self.selector_Button.hidden=bhide;
  418. self.selector_dorpdown.hidden=bhide;
  419. self.selector_imageView.hidden=bhide;
  420. }
  421. -(void) addtocart:(int) count
  422. {
  423. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  424. if(self.cqty>0)
  425. {
  426. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"QTY: %d of this model already in cart. Continue ?",self.cqty] message:nil preferredStyle:UIAlertControllerStyleAlert];
  427. //block代码块取代了delegate
  428. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  429. int c=self.cqty+count;
  430. int m=c%self.step;
  431. if(m!=0&&appDelegate.alert_sold_in_quantities)
  432. {
  433. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  434. //block代码块取代了delegate
  435. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  436. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  437. NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count name:self.model_label.text];
  438. dispatch_async(dispatch_get_main_queue(), ^{
  439. if([[return_json valueForKey:@"result"] intValue]==2)
  440. {
  441. // NSString* order_code = [return_json valueForKey:@"order_code"];
  442. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  443. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  444. if(self.shopCartBlock!=nil)
  445. {
  446. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  447. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  448. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  449. iv.image = img;
  450. // [self.contentView addSubview:iv];
  451. self.shopCartBlock(iv);
  452. self.cqty +=count;
  453. if(self.set_cqty)
  454. self.set_cqty(self.cqty);
  455. }
  456. }
  457. else
  458. {
  459. UIViewController *vc= [RAUtils getViewController:self];
  460. NSString *msg = [return_json valueForKey:@"err_msg"];
  461. NSString *title = @"Add To Cart";
  462. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  463. title = @"Add To Cart: Out of Stock";
  464. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  465. self.btnNotifyMe.enabled = YES;
  466. self.btnaddCart.enabled = NO;
  467. }
  468. [RAUtils message_alert:msg title:title controller:vc] ;
  469. }
  470. });
  471. });
  472. }];
  473. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  474. DebugLog(@"No");
  475. }];
  476. [alertControl addAction:actionOne];
  477. [alertControl addAction:alertthree];
  478. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  479. UIViewController *vc= [RAUtils getViewController:self];
  480. [vc presentViewController:alertControl animated:YES completion:nil];
  481. }
  482. else
  483. {
  484. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  485. NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count name:self.model_label.text];
  486. dispatch_async(dispatch_get_main_queue(), ^{
  487. if([[return_json valueForKey:@"result"] intValue]==2)
  488. {
  489. // NSString* order_code = [return_json valueForKey:@"order_code"];
  490. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  491. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  492. if(self.shopCartBlock!=nil)
  493. {
  494. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  495. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  496. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  497. iv.image = img;
  498. // [self.contentView addSubview:iv];
  499. self.shopCartBlock(iv);
  500. self.cqty +=count;
  501. if(self.set_cqty)
  502. self.set_cqty(self.cqty);
  503. }
  504. }
  505. else
  506. {
  507. UIViewController *vc= [RAUtils getViewController:self];
  508. NSString *msg = [return_json valueForKey:@"err_msg"];
  509. NSString *title = @"Add To Cart";
  510. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  511. title = @"Add To Cart: Out of Stock";
  512. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  513. self.btnNotifyMe.enabled = YES;
  514. self.btnaddCart.enabled = NO;
  515. }
  516. [RAUtils message_alert:msg title:title controller:vc] ;
  517. }
  518. });
  519. });
  520. }
  521. //
  522. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  523. //
  524. // NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count];
  525. //
  526. // dispatch_async(dispatch_get_main_queue(), ^{
  527. //
  528. //
  529. //
  530. // if([[return_json valueForKey:@"result"] intValue]==2)
  531. // {
  532. //
  533. // // NSString* order_code = [return_json valueForKey:@"order_code"];
  534. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  535. //// appDelegate.order_code = order_code;
  536. ////
  537. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  538. //// appDelegate.order_code = order_code;
  539. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  540. //
  541. //
  542. // if(self.shopCartBlock!=nil)
  543. // {
  544. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  545. //
  546. //
  547. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  548. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  549. // iv.image = img;
  550. // // [self.contentView addSubview:iv];
  551. // self.shopCartBlock(iv);
  552. //
  553. // self.cqty +=count;
  554. // if(self.set_cqty)
  555. // self.set_cqty(self.cqty);
  556. //
  557. // }
  558. //
  559. //
  560. // }
  561. // else
  562. // {
  563. // UIViewController *vc= [RAUtils getViewController:self];
  564. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:vc] ;
  565. // }
  566. //
  567. //
  568. //
  569. //
  570. // });
  571. // });
  572. //
  573. }];
  574. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  575. DebugLog(@"No");
  576. }];
  577. [alertControl addAction:actionOne];
  578. [alertControl addAction:alertthree];
  579. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  580. UIViewController *vc= [RAUtils getViewController:self];
  581. [vc presentViewController:alertControl animated:YES completion:nil];
  582. }
  583. else
  584. {
  585. int c=self.cqty+count;
  586. int m=c%self.step;
  587. if(m!=0&&appDelegate.alert_sold_in_quantities)
  588. {
  589. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  590. //block代码块取代了delegate
  591. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  592. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  593. NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count name:self.model_label.text];
  594. dispatch_async(dispatch_get_main_queue(), ^{
  595. if([[return_json valueForKey:@"result"] intValue]==2)
  596. {
  597. // NSString* order_code = [return_json valueForKey:@"order_code"];
  598. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  599. // appDelegate.order_code = order_code;
  600. //
  601. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  602. // appDelegate.order_code = order_code;
  603. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  604. if(self.shopCartBlock!=nil)
  605. {
  606. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  607. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  608. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  609. iv.image = img;
  610. // [self.contentView addSubview:iv];
  611. self.shopCartBlock(iv);
  612. self.cqty +=count;
  613. if(self.set_cqty)
  614. self.set_cqty(self.cqty);
  615. }
  616. }
  617. else
  618. {
  619. UIViewController *vc= [RAUtils getViewController:self];
  620. NSString *msg = [return_json valueForKey:@"err_msg"];
  621. NSString *title = @"Add To Cart";
  622. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  623. title = @"Add To Cart: Out of Stock";
  624. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  625. self.btnNotifyMe.enabled = YES;
  626. self.btnaddCart.enabled = NO;
  627. }
  628. [RAUtils message_alert:msg title:title controller:vc] ;
  629. }
  630. });
  631. });
  632. }];
  633. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  634. DebugLog(@"No");
  635. }];
  636. [alertControl addAction:actionOne];
  637. [alertControl addAction:alertthree];
  638. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  639. UIViewController *vc= [RAUtils getViewController:self];
  640. [vc presentViewController:alertControl animated:YES completion:nil];
  641. }
  642. else
  643. {
  644. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  645. NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count name:self.model_label.text];
  646. dispatch_async(dispatch_get_main_queue(), ^{
  647. if([[return_json valueForKey:@"result"] intValue]==2)
  648. {
  649. // NSString* order_code = [return_json valueForKey:@"order_code"];
  650. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  651. // appDelegate.order_code = order_code;
  652. //
  653. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  654. // appDelegate.order_code = order_code;
  655. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  656. if(self.shopCartBlock!=nil)
  657. {
  658. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  659. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  660. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  661. iv.image = img;
  662. // [self.contentView addSubview:iv];
  663. self.shopCartBlock(iv);
  664. self.cqty +=count;
  665. if(self.set_cqty)
  666. self.set_cqty(self.cqty);
  667. }
  668. }
  669. else
  670. {
  671. UIViewController *vc= [RAUtils getViewController:self];
  672. NSString *msg = [return_json valueForKey:@"err_msg"];
  673. NSString *title = @"Add To Cart";
  674. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  675. title = @"Add To Cart: Out of Stock";
  676. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  677. self.btnNotifyMe.enabled = YES;
  678. self.btnaddCart.enabled = NO;
  679. }
  680. [RAUtils message_alert:msg title:title controller:vc] ;
  681. }
  682. });
  683. });
  684. }
  685. //
  686. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  687. //
  688. // NSDictionary* return_json = [iSalesNetwork add_toCart:self.product_id count:count];
  689. //
  690. // dispatch_async(dispatch_get_main_queue(), ^{
  691. //
  692. //
  693. //
  694. // if([[return_json valueForKey:@"result"] intValue]==2)
  695. // {
  696. //
  697. // // NSString* order_code = [return_json valueForKey:@"order_code"];
  698. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  699. //// appDelegate.order_code = order_code;
  700. ////
  701. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  702. //// appDelegate.order_code = order_code;
  703. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  704. //
  705. //
  706. // if(self.shopCartBlock!=nil)
  707. // {
  708. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  709. //
  710. //
  711. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  712. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  713. // iv.image = img;
  714. // // [self.contentView addSubview:iv];
  715. // self.shopCartBlock(iv);
  716. //
  717. // self.cqty +=count;
  718. // if(self.set_cqty)
  719. // self.set_cqty(self.cqty);
  720. //
  721. // }
  722. //
  723. //
  724. // }
  725. // else
  726. // {
  727. // UIViewController *vc= [RAUtils getViewController:self];
  728. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:vc] ;
  729. // }
  730. //
  731. //
  732. //
  733. //
  734. // });
  735. // });
  736. //
  737. }
  738. }
  739. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  740. {
  741. [super setSelected:selected animated:animated];
  742. // Configure the view for the selected state
  743. }
  744. - (IBAction)selectorOnClick:(id)sender {
  745. DebugLog(@"select click");
  746. [celldelegate SelectorClicked:(UIButton*) sender];
  747. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  748. }
  749. #pragma mark -
  750. #pragma mark Deck DataSource Protocol Methods
  751. -(NSUInteger)numberOfPhotosInPhotoStackView:(PhotoStackView *)photoStack {
  752. return [self.photos count];
  753. }
  754. -(UIImage *)photoStackView:(PhotoStackView *)photoStack photoForIndex:(NSUInteger)index {
  755. if(self.photos.count==0)
  756. return nil;
  757. return [self.photos objectAtIndex:index];
  758. }
  759. #pragma mark -
  760. #pragma mark Deck Delegate Protocol Methods
  761. -(void)photoStackView:(PhotoStackView *)photoStackView willStartMovingPhotoAtIndex:(NSUInteger)index {
  762. // User started moving a photo
  763. }
  764. -(void)photoStackView:(PhotoStackView *)photoStackView willFlickAwayPhotoFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
  765. // User flicked the photo away, revealing the next one in the stack
  766. }
  767. -(void)photoStackView:(PhotoStackView *)photoStackView didRevealPhotoAtIndex:(NSUInteger)index {
  768. self.pageControl.currentPage = index;
  769. }
  770. - (UIView *)createVideoView:(NSString*) videoID
  771. {
  772. UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 360)];
  773. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
  774. // [imageView setImage:[UIImage imageNamed:@"demo"]];
  775. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 620, 10)];
  776. titleLabel.font = [UIFont boldSystemFontOfSize:20];
  777. titleLabel.text=@"a separated player will show here:";
  778. titleLabel.numberOfLines = 0;
  779. titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
  780. // titleLabel.textAlignment = NSTextAlignmentRight;
  781. titleLabel.textAlignment = NSTextAlignmentCenter;
  782. CGSize constraintkey = CGSizeMake(620, 20000.0f);//key label width is 40% cell width;
  783. CGSize sizetitle = [@"a separated player will show here:" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  784. //[titleLabel sizeToFit];
  785. // titleLabel.center.x = demoView.center.x;
  786. titleLabel.frame = CGRectMake(10, 10, 620, sizetitle.height);
  787. [demoView addSubview:titleLabel];
  788. UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 610, 10)];
  789. lineLabel.font = [UIFont boldSystemFontOfSize:17];
  790. lineLabel.text=videoID;
  791. lineLabel.numberOfLines = 0;
  792. lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
  793. CGSize sizemsg = [videoID sizeWithFont:[UIFont boldSystemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  794. lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 630, sizemsg.height);
  795. //[lineLabel sizeToFit];
  796. [demoView addSubview:lineLabel];
  797. demoView.frame = CGRectMake(0, 0, 640, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
  798. return demoView;
  799. }
  800. -(void)photoStackView:(PhotoStackView *)photoStackView didSelectPhotoAtIndex:(NSUInteger)index {
  801. DebugLog(@"selected %lu", (unsigned long)index);
  802. NSDictionary* item= self.stack_contents[[NSString stringWithFormat:@"item_%ld",index]];
  803. if([item[@"type"] isEqualToString:@"video"])
  804. {
  805. NSString* video_code=item[@"code"];
  806. // [TextUtils expression_varable:video regex:<#(NSString *)#>]
  807. NSString* video_id=[self.playerView Embed2VID:video_code];
  808. NSDictionary *playerVars = @{
  809. @"playsinline" : @1,
  810. @"autoplay" : @(1),
  811. @"rel":@0,
  812. @"showinfo": @0,
  813. @"modestbranding":@0,
  814. @"enablejsapi":@1
  815. };
  816. [self.playerView loadWithVideoId:video_id playerVars:playerVars];
  817. for(UIView *v in self.playerView.subviews)
  818. {
  819. if([v isKindOfClass:[UIButton class] ])
  820. [self.playerView bringSubviewToFront:v];
  821. }
  822. self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
  823. self.playerView.hidden=false;
  824. return;
  825. } else {
  826. ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
  827. preVC.content = self.stack_contents;
  828. // preVC.currentIndex = index;
  829. [preVC setOffset:index];
  830. UIViewController *superVC= [RAUtils getViewController:self];
  831. [superVC.navigationController pushViewController:preVC animated:true];
  832. return;
  833. }
  834. // NSMutableArray *imgList = [NSMutableArray arrayWithCapacity:_data.count];
  835. // for (int i = 0; i < _data.count; i++) {
  836. // UIImage *imgMod = _data[i];
  837. // [imgList addObject:imgMod];
  838. // }
  839. // 调用展示窗口
  840. ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
  841. UIViewController *vc= [RAUtils getViewController:self];
  842. [vc.navigationController pushViewController:imgShow animated:true];
  843. // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:imgShow];
  844. // [self presentViewController:nav animated:YES completion:nil];
  845. // UIDocumentInteractionController
  846. // UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  847. // preview.delegate = self;
  848. // [preview presentPreviewAnimated:YES];
  849. }
  850. -(void) neworder
  851. {
  852. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  853. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  854. NSDictionary* return_json = [iSalesNetwork new_Order];
  855. dispatch_async(dispatch_get_main_queue(), ^{
  856. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  857. if([[return_json valueForKey:@"result"] intValue]==2)
  858. {
  859. int result=[[return_json valueForKey:@"result"] intValue];
  860. if(result==2)
  861. {
  862. //successed.
  863. NSString* order_code = [return_json valueForKey:@"orderCode"];
  864. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  865. appDelegate.order_code = order_code;
  866. int count=[self.quantity_text.text intValue];
  867. [self addtocart:count];
  868. // [self ReloadData];
  869. // if(self.shopCartBlock!=nil)
  870. // {
  871. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  872. //
  873. //
  874. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  875. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  876. // iv.image = img;
  877. // // [self.contentView addSubview:iv];
  878. // self.shopCartBlock(iv);
  879. //
  880. // }
  881. }
  882. }
  883. else
  884. {
  885. UIViewController *vc= [RAUtils getViewController:self];
  886. NSString *msg = [return_json valueForKey:@"err_msg"];
  887. NSString *title = @"Add To Cart";
  888. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  889. title = @"Add To Cart: Out of Stock";
  890. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  891. self.btnNotifyMe.enabled = YES;
  892. self.btnaddCart.enabled = NO;
  893. }
  894. [RAUtils message_alert:msg title:title controller:vc] ;
  895. }
  896. });
  897. });
  898. }
  899. #pragma mark - UIAlertViewDelegate
  900. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  901. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  902. {
  903. UIApplication * app = [UIApplication sharedApplication];
  904. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  905. if(alertView.tag==1234)//库存小于0
  906. {
  907. // availability warring;
  908. int count=[self.quantity_text.text intValue];
  909. if(buttonIndex!=alertView.cancelButtonIndex)
  910. {
  911. if(appDelegate.order_code==nil)
  912. {
  913. [self neworder];
  914. }
  915. else
  916. {
  917. [self addtocart:count];
  918. }
  919. }
  920. return;
  921. }
  922. // int count=[self.quantity_text.text intValue];
  923. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  924. if(buttonIndex==alertView.cancelButtonIndex)
  925. {
  926. }
  927. else if(buttonIndex==1)
  928. {
  929. //open exist
  930. UIViewController *vc= [RAUtils getViewController:self];
  931. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  932. ovc.showNavibar = true;
  933. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  934. if(appDelegate.order_code==nil)
  935. {
  936. [self neworder];
  937. }
  938. else
  939. {
  940. if(self.set_update_data)
  941. self.set_update_data(true);
  942. int model_count = [[order_detail valueForKey:@"model_count"] intValue];
  943. for(int i=0;i<model_count;i++)
  944. {
  945. NSDictionary* item = [order_detail objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  946. NSString* product_id = [item valueForKey:@"product_id"];
  947. if([product_id isEqualToString:self.product_id])
  948. {
  949. self.cqty = [[item valueForKey:@"QTY"]intValue];
  950. if(self.set_cqty)
  951. self.set_cqty(self.cqty);
  952. break;
  953. }
  954. }
  955. int count=[self.quantity_text.text intValue];
  956. [self addtocart:count];
  957. [main_vc checklogin:true];
  958. }
  959. };
  960. ovc.init_style = OL_OPEN;
  961. ovc.onCancel = ^(){
  962. [main_vc checklogin:true];
  963. };
  964. [vc.navigationController pushViewController:ovc animated:true];
  965. }
  966. else
  967. {
  968. //create new;
  969. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  970. {
  971. UIViewController *vc= [RAUtils getViewController:self];
  972. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  973. cvc.showNavibar = true;
  974. cvc.contact_type = @"Sales_Order_Customer";
  975. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  976. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  977. appDelegate.customerInfo = value;
  978. if(appDelegate.order_code==nil)
  979. [self neworder];
  980. [main_vc checklogin:true];
  981. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  982. //
  983. // if(self.returnValue)
  984. // self.returnValue(value);
  985. };
  986. cvc.onCancel = ^(){
  987. UIViewController *vc= [RAUtils getViewController:self];
  988. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order" controller:vc];
  989. };
  990. cvc.onReset = ^(){
  991. [main_vc checklogin:true];
  992. };
  993. [vc.navigationController pushViewController:cvc animated:true];
  994. }
  995. else
  996. {
  997. [self neworder];
  998. }
  999. }
  1000. }
  1001. - (void)valueChanged:(UIStepper *)sender {
  1002. self.quantity_text.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  1003. [celldelegate stepClicked:(int)sender.value];
  1004. // -(void) stepClicked:(int) value;
  1005. // 检查缺货
  1006. // NSString *cartQtyString = nil;
  1007. // if (self.cqyt_label.text) {
  1008. // cartQtyString = [self.cqyt_label.text substringFromIndex:[@"Model QTY in Cart:" length]];
  1009. // }
  1010. //
  1011. // int availability = [self.count_Label.text intValue];
  1012. // int buyQTY = [self.quantity_text.text intValue];
  1013. // int cartQTY = [cartQtyString intValue];
  1014. //
  1015. // [self checkAvailability:availability buyQTY:buyQTY cartQTY:cartQTY];
  1016. }
  1017. #pragma mark -
  1018. #pragma mark Actions
  1019. -(void)ClearPhotos
  1020. {
  1021. NSMutableArray *photosMutable = [self.photos mutableCopy];
  1022. [photosMutable removeAllObjects];
  1023. self.photos = photosMutable;
  1024. [self.photoStack reloadData];
  1025. }
  1026. - (void)AddPhoto :(UIImage*)photo {
  1027. NSMutableArray *photosMutable = [self.photos mutableCopy];
  1028. [photosMutable addObject:photo];
  1029. self.photos = photosMutable;
  1030. [self.photoStack reloadData];
  1031. self.pageControl.numberOfPages = [self.photos count];
  1032. if(self.photos.count<2-1)//1 for qrcode;
  1033. self.photoStack.showBorder=false;
  1034. else
  1035. self.photoStack.showBorder=true;
  1036. }
  1037. #pragma mark -
  1038. #pragma mark Setup
  1039. -(void)setup {
  1040. // int availability = [self.count_Label.text intValue];
  1041. self.stepper.minimumValue= self.step;
  1042. self.stepper.maximumValue = INTMAX_MAX;
  1043. self.stepper.stepValue= self.step;
  1044. self.stepper.value= self.quantity;
  1045. [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  1046. if(true)//(self.photos==nil)
  1047. {
  1048. self.photos = [NSArray array];
  1049. }
  1050. if(true)//(self.photoStack==nil)
  1051. {
  1052. // if(self.photoStack!=nil)
  1053. // [self.photoStack removeFromSuperview];
  1054. // self.photoStack = [[PhotoStackView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
  1055. // self.photoStack.backgroundColor =[UIColor redColor];
  1056. // self.photoStack.center = CGPointMake(self.center.x, 220);
  1057. self.photoStack.dataSource = self;
  1058. self.photoStack.delegate = self;
  1059. // [self addSubview:self.photoStack];
  1060. self.pageControl.numberOfPages = [self.photos count];
  1061. }
  1062. }
  1063. - (IBAction)notifyMeButtonClicked:(UIButton *)sender {
  1064. if (self.notifyMeBlock) {
  1065. self.notifyMeBlock();
  1066. }
  1067. }
  1068. #pragma mark - TextField Delegate
  1069. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  1070. NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  1071. // NSString *cartQtyString = nil;
  1072. // if (self.cqyt_label.text) {
  1073. // cartQtyString = [self.cqyt_label.text substringFromIndex:[@"Model QTY in Cart:" length]];
  1074. // }
  1075. // int availability = [self.count_Label.text intValue];
  1076. int buyQTY = [newString intValue];
  1077. // int cartQTY = [cartQtyString intValue];
  1078. // [self checkAvailability:availability buyQTY:buyQTY cartQTY:cartQTY];
  1079. self.stepper.value = buyQTY;
  1080. return YES;
  1081. }
  1082. #pragma mark - Private
  1083. /**检查是否缺货*/
  1084. - (void)checkAvailability:(int)availability buyQTY:(int)buyQty cartQTY:(int)cartQty { // availability不可以通过textField.text取得
  1085. #ifdef BUILD_NPD
  1086. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1087. if (!appDelegate.bLogin || appDelegate.offline_mode || buyQty < 1) {
  1088. return;
  1089. }
  1090. if (appDelegate.user_type == USER_ROLE_CUSTOMER) { // customer
  1091. if (!appDelegate.order_code) { // 未打开订单状态
  1092. if ((buyQty + cartQty) > availability) {
  1093. self.btnaddCart.enabled = NO;
  1094. self.btnNotifyMe.enabled = YES;
  1095. } else {
  1096. self.btnaddCart.enabled = YES;
  1097. self.btnNotifyMe.enabled = NO;
  1098. }
  1099. } else {
  1100. // 打开订单状态
  1101. if (![Singleton sharedInstance].currentOrderIsMerged) {
  1102. // Shop Order
  1103. self.btnaddCart.enabled = YES;
  1104. // 默认未缺货
  1105. self.btnNotifyMe.enabled = NO;
  1106. if ((buyQty + cartQty) > availability) { // 缺货
  1107. self.btnNotifyMe.enabled = YES;
  1108. }
  1109. } else {
  1110. // Purchase Order
  1111. if ((buyQty + cartQty) > availability) { // 缺货
  1112. self.btnaddCart.enabled = NO;
  1113. self.btnNotifyMe.enabled = YES;
  1114. } else { // 未缺货
  1115. self.btnNotifyMe.enabled = NO;
  1116. self.btnaddCart.enabled = YES;
  1117. }
  1118. }
  1119. }
  1120. } else { // empoyee
  1121. if ((buyQty + cartQty) > availability) { // 缺货
  1122. self.btnaddCart.enabled = NO;
  1123. self.btnNotifyMe.enabled = YES;
  1124. } else { // 未缺货
  1125. self.btnNotifyMe.enabled = NO;
  1126. self.btnaddCart.enabled = YES;
  1127. }
  1128. }
  1129. #endif
  1130. }
  1131. - (IBAction)introduceBtnClick:(UIButton *)sender {
  1132. if (self.introduceBlock) {
  1133. self.introduceBlock(sender);
  1134. }
  1135. }
  1136. - (IBAction)descriptionBtnClick:(UIButton *)sender {
  1137. if (self.descriptionBlock) {
  1138. self.descriptionBlock(sender);
  1139. }
  1140. }
  1141. - (IBAction)OnClosePlayer:(id)sender {
  1142. [self.playerView stopVideo];
  1143. self.playerView.hidden=true;
  1144. }
  1145. @end