| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- //
- // CartItemCell.m
- // RedAnt ERP Mobile
- //
- // Created by Ray on 14-7-9.
- // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
- //
- #import "ContrastModelItemCell.h"
- #import "RANetwork.h"
- #import "EditModelPriceViewController.h"
- #import "RAUtils.h"
- #import "BundleModelViewController.h"
- #import "CartViewController.h"
- #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
- #import "MasterModelViewController.h"
- #endif
- #define NUMBERS @"0123456789\n"
- @implementation ContrastModelItemCell
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- // Initialization code
- }
- return self;
- }
- -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value
- {
-
- // [self.stepper becomeFirstResponder];
- if(self.stepper!=nil)
- {
- if(min<=0)
- min=1;
- if(step<=0)
- step=1;
- self.stepper.minimumValue= min;
-
-
-
- self.stepper.stepValue= step;
-
- self.stepper.value= value;
-
- self.pre_val = value;
-
- [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
- self.totalchange=0;
-
- }
- }
- - (IBAction)setpperChanged:(UIStepper *)sender {
-
- DebugLog(@"setpperChanged");
-
- int val_change=(int)sender.value-self.pre_val;
-
- _totalchange=_totalchange+val_change;
-
-
- self.pre_val = (int)sender.value;
-
-
- [self cancel_setQTY];
- [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8];
-
- }
- -(void) cancel_setQTY
- {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil];
- }
- -(void) delayRun
- {
- // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil];
- // // 启动
- // [thread start];
-
- [RANetwork request_update_cartqty:self.cart_id value:self.pre_val completionHandler:^(NSMutableDictionary *result) {
- NSDictionary* cart_json =result;
- self.totalchange=0;
- if([[cart_json valueForKey:@"result"] intValue]==2)
- {
- if(self.onReturnQTY)
- self.onReturnQTY(self.pre_val,cart_json);
- // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
- // [self.delegate returnQTY:sender.value ];
- // }
- }
- else
- {
- UIViewController* vc=[RAUtils getViewController :self];
- [((CartViewController *)vc).itemListTable reloadData];
- [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
-
- ;
- }
-
-
-
- }];
-
-
-
- }
- - (void)valueChanged:(UIStepper *)sender {
-
-
-
- self.editCount.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
-
- [self set_Count:(int)sender.value ];
-
-
- //
- // [celldelegate stepClicked:(int)sender.value];
- // -(void) stepClicked:(int) value;
- }
- -(void) set_Count:(int) count
- {
-
- self.stepper.value=count;
- if(self.enable_longpress)
- {
- // [self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
- }
-
- double dprice=0;
- if(self.bundle_item!=nil)
- {
- int citem=0;
-
- int bcount=[[self.bundle_item valueForKey:@"count"] intValue];
- for(int bc=0;bc<bcount;bc++)
- {
- NSDictionary * bitem = [self.bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
- int modulus= [[bitem valueForKey:@"modulus"] intValue];
- citem+= modulus;
- double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
- dprice+= uprice*modulus;
-
- }
- if(citem==1)
- self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle item with price of $ %.2f",citem,dprice ];
- else
- self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle items with price of $ %.2f",citem,dprice ];
- self.bundleButton.hidden = NO;
- self.bundleLabel.hidden = NO;
- }
- else
- {
- self.bundleButton.hidden = YES;
- self.bundleLabel.hidden = YES;
- }
-
- // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price];
-
-
-
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- NSString* price = [NSString stringWithFormat:@"%.2f",(self.unit_price* (1.0-self.discount/100.0)+dprice)*count ];
- if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
- {
-
- }
- else
- {
- price=nil;
- }
-
- if(self.noprice&&self.unit_price==0.0)
- self.labelPrice.text =@"No Price";
- else
- self.labelPrice.text =price;
-
- self.editCount.text =[NSString stringWithFormat:@"%d",count];
- }
- //- (void)awakeFromNib
- //{
- // // Initialization code
- //
- //
- //
- //}
- - (IBAction)masterBundleClick:(UIButton *)sender {
-
-
-
- #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
- UIViewController* vc=[RAUtils getViewController :self.bundleButton];
- MasterModelViewController *masterModelVC = [MasterModelViewController buildMasterModelViewController];
- masterModelVC.content_data = self.master_items;
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:masterModelVC];
- [vc presentViewController:nav animated:YES completion:nil];
-
- #endif
-
- }
- //-(UIViewController*) getViewController
- //{
- // for (UIView* next = [self superview]; next; next = next.superview) {
- // UIResponder* nextResponder = [next nextResponder];
- // if ([nextResponder isKindOfClass:[UIViewController class]]) {
- // return (UIViewController*)nextResponder;
- // }
- // }
- // return nil;
- //
- //}
- - (IBAction)OnClick_BundleDetail:(id)sender {
-
- UIViewController* vc=[RAUtils getViewController :self.bundleButton];
-
-
- BundleModelViewController * bundleVC =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"BundleModelViewController"];
- bundleVC.content_data = self.bundle_item;
-
- UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:bundleVC] ;
-
-
-
- navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
- [vc presentViewController:navi animated:YES completion:^{
-
- // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
-
- DebugLog(@"bundle present.........");
-
- // self.btop = false;
- // <#code#>
- }];
-
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated
- {
- [super setSelected:selected animated:animated];
-
- // Configure the view for the selected state
- }
- #pragma mark textField delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return NO;
- }
- -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
-
-
- DebugLog(@"text:%@",textField.text);
-
-
-
- NSCharacterSet *cs;
- cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
-
- NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
-
- BOOL canChange = [string isEqualToString:filtered];
-
-
-
-
- return canChange;
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
-
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- int qty = [textField.text intValue];
-
- if(qty==0)
- qty=self.stepper.minimumValue;
-
- #ifdef MPACK
- if ((int)qty % (int)self.stepper.stepValue != 0) {
- UIViewController* vc=[RAUtils getViewController :self];
- [((CartViewController *)vc).itemListTable reloadData];
- [RAUtils message_box:@"Change Model Count" message:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] completion:nil];
-
-
- return;
- }
- #endif
-
-
- int c=qty;
-
- int m=c%(int)(self.stepper.stepValue);
- if(m!=0&&appDelegate.alert_sold_in_quantities)
- {
-
-
-
- UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",(int)(self.stepper.stepValue)] message:nil preferredStyle:UIAlertControllerStyleAlert];
- //block代码块取代了delegate
-
-
-
- UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- [self set_Count:qty];
-
-
-
- [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
- NSDictionary* cart_json =result;
- if([[cart_json valueForKey:@"result"] intValue]==2)
- {
- // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
- // [self.delegate returnQTY:qty];
- // }
-
- if(self.onReturnQTY)
- self.onReturnQTY(qty,cart_json);
- }
- else
- {
- UIViewController* vc=[RAUtils getViewController :self];
- [((CartViewController *)vc).itemListTable reloadData];
- [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
- }
-
-
-
- }];
-
- }];
-
- UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- DebugLog(@"No");
- textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
- }];
- [alertControl addAction:actionOne];
-
- [alertControl addAction:alertthree];
-
- //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
-
- UIViewController *vc= [RAUtils getViewController:self];
- [vc presentViewController:alertControl animated:YES completion:nil];
-
-
-
- }
- else
- {
- [self set_Count:qty];
-
- [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
- NSDictionary* cart_json =result;
- if([[cart_json valueForKey:@"result"] intValue]==2)
- {
- // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
- // [self.delegate returnQTY:qty];
- // }
-
- if(self.onReturnQTY)
- self.onReturnQTY(qty,cart_json);
- }
- else
- {
- UIViewController* vc=[RAUtils getViewController :self];
- [((CartViewController *)vc).itemListTable reloadData];
- [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
- }
-
-
-
- }];
-
- // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- //
- // NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:qty];
- //
- // dispatch_async(dispatch_get_main_queue(), ^{
- //
- // if([[cart_json valueForKey:@"result"] intValue]==2)
- // {
- // // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
- // // [self.delegate returnQTY:qty];
- // // }
- //
- // if(self.onReturnQTY)
- // self.onReturnQTY(qty,cart_json);
- // }
- // else
- // {
- // UIViewController* vc=[RAUtils getViewController :self];
- // [((CartViewController *)vc).itemListTable reloadData];
- // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
- // }
- //
- //
- //
- // });
- // });
- }
-
-
-
-
-
-
-
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField
- {
- NSString* text = textField.text;
- if(text.length==0)
- text=@"0";
- self.pre_val = [textField.text intValue];
-
- // self.last_edit = textField.text;
- }
- @end
|