DetailHeaderCell.m 66 KB

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