DetailHeaderCell.m 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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 || appDelegate.offline_mode || buyQty < 1) {
  1096. return;
  1097. }
  1098. if (appDelegate.user_type == USER_ROLE_CUSTOMER) { // customer
  1099. if (!appDelegate.order_code) { // 未打开订单状态
  1100. if ((buyQty + cartQty) > availability) {
  1101. self.btnaddCart.enabled = NO;
  1102. self.btnNotifyMe.enabled = YES;
  1103. } else {
  1104. self.btnaddCart.enabled = YES;
  1105. self.btnNotifyMe.enabled = NO;
  1106. }
  1107. } else {
  1108. // 打开订单状态
  1109. if (![Singleton sharedInstance].currentOrderIsMerged) {
  1110. // Shop Order
  1111. self.btnaddCart.enabled = YES;
  1112. // 默认未缺货
  1113. self.btnNotifyMe.enabled = NO;
  1114. if ((buyQty + cartQty) > availability) { // 缺货
  1115. self.btnNotifyMe.enabled = YES;
  1116. }
  1117. } else {
  1118. // Purchase Order
  1119. if ((buyQty + cartQty) > availability) { // 缺货
  1120. self.btnaddCart.enabled = NO;
  1121. self.btnNotifyMe.enabled = YES;
  1122. } else { // 未缺货
  1123. self.btnNotifyMe.enabled = NO;
  1124. self.btnaddCart.enabled = YES;
  1125. }
  1126. }
  1127. }
  1128. } else { // empoyee
  1129. if ((buyQty + cartQty) > availability) { // 缺货
  1130. self.btnaddCart.enabled = NO;
  1131. self.btnNotifyMe.enabled = YES;
  1132. } else { // 未缺货
  1133. self.btnNotifyMe.enabled = NO;
  1134. self.btnaddCart.enabled = YES;
  1135. }
  1136. }
  1137. //#endif
  1138. }
  1139. - (IBAction)introduceBtnClick:(UIButton *)sender {
  1140. if (self.introduceBlock) {
  1141. self.introduceBlock(sender);
  1142. }
  1143. }
  1144. - (IBAction)descriptionBtnClick:(UIButton *)sender {
  1145. if (self.descriptionBlock) {
  1146. self.descriptionBlock(sender);
  1147. }
  1148. }
  1149. - (IBAction)OnClosePlayer:(id)sender {
  1150. [self.playerView stopVideo];
  1151. self.playerView.hidden=true;
  1152. }
  1153. @end