DetailHeaderCell.m 64 KB

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