CartViewController.m 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. //
  2. // CartViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-6-6.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAUtils.h"
  9. #import "CartViewController.h"
  10. #import "ModelItemCell.h"
  11. #import "iSalesNetwork.h"
  12. //#import "CommonGridViewController.h"
  13. #import "CreateOrderViewController.h"
  14. #import "DetailViewController.h"
  15. #import "EditModelPriceViewController.h"
  16. #import "MainViewController.h"
  17. #import "LoginViewController.h"
  18. #import "ItemNotesViewController.h"
  19. #import "CartGeneralNotesViewController.h"
  20. #define ALERT_FREE 1024
  21. #define ALERT_DEL 1025
  22. #define ALERT_SET_FREE 1026
  23. #define ALERT_RESTORE_FREE 1027
  24. @interface CartViewController ()
  25. @end
  26. @implementation CartViewController
  27. -(void) logout
  28. {
  29. [self.content_arr removeAllObjects];
  30. }
  31. - (IBAction)OnEditSelectClick:(id)sender {
  32. NSMutableArray* arr_ids = [[NSMutableArray alloc] init];
  33. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  34. for(int i=0;i<count;i++)
  35. {
  36. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  37. [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  38. }
  39. NSString * ids = [arr_ids componentsJoinedByString:@","];
  40. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select all model and" message:nil preferredStyle:UIAlertControllerStyleAlert];
  41. //block代码块取代了delegate
  42. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  43. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove all models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  44. //block代码块取代了delegate
  45. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  46. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  47. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  48. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  51. if([[cart_json valueForKey:@"result"] intValue]==2)
  52. {
  53. [self end_edit];
  54. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  55. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  56. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  57. }
  58. else
  59. {
  60. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  61. }
  62. });
  63. });
  64. }];
  65. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  66. NSLog(@"No");
  67. }];
  68. [alertControl addAction:actionOne];
  69. [alertControl addAction:alertthree];
  70. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  71. [self presentViewController:alertControl animated:YES completion:nil];
  72. NSLog(@"%@",self.edit_select_arr);
  73. }];
  74. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Add To Wishlist" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  75. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Add To Wishlist"];
  76. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  77. NSDictionary* cart_json = [iSalesNetwork move_cart2wish:ids];
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  80. if([[cart_json valueForKey:@"result"] intValue]==2)
  81. {
  82. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  83. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  84. [self end_edit];
  85. }
  86. else
  87. {
  88. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Add To Wishlist" controller:self] ;
  89. }
  90. });
  91. });
  92. }];
  93. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  94. NSLog(@"No");
  95. }];
  96. [alertControl addAction:actionOne];
  97. [alertControl addAction:actionTwo];
  98. [alertControl addAction:alertthree];
  99. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  100. [self presentViewController:alertControl animated:YES completion:nil];
  101. NSLog(@"%@",self.edit_select_arr);
  102. }
  103. - (IBAction)onEditAddWishClick:(id)sender {
  104. if(self.edit_select_arr.count==0)
  105. {
  106. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Move To Wishlist" controller:self] ;
  107. return;
  108. }
  109. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  110. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove selected models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  111. // //block代码块取代了delegate
  112. //
  113. //
  114. //
  115. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  116. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Move Models To Wishlist"];
  117. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  118. NSDictionary* cart_json = [iSalesNetwork move_cart2wish:ids];
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  121. if([[cart_json valueForKey:@"result"] intValue]==2)
  122. {
  123. [self end_edit];
  124. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  125. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  126. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  127. }
  128. else
  129. {
  130. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Move To Wishlist" controller:self] ;
  131. }
  132. });
  133. });
  134. // }];
  135. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  136. // NSLog(@"No");
  137. // }];
  138. // [alertControl addAction:actionOne];
  139. //
  140. // [alertControl addAction:alertthree];
  141. //
  142. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  143. //
  144. //
  145. // [self presentViewController:alertControl animated:YES completion:nil];
  146. //
  147. // NSLog(@"%@",self.edit_select_arr);
  148. }
  149. - (IBAction)onEditDelClick:(id)sender {
  150. if(self.edit_select_arr.count==0)
  151. {
  152. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Delete From Cart" controller:self] ;
  153. return;
  154. }
  155. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  156. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove selected models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  157. //block代码块取代了delegate
  158. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  159. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  160. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  161. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  164. if([[cart_json valueForKey:@"result"] intValue]==2)
  165. {
  166. [self end_edit];
  167. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  168. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  169. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  170. }
  171. else
  172. {
  173. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  174. }
  175. });
  176. });
  177. }];
  178. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  179. NSLog(@"No");
  180. }];
  181. [alertControl addAction:actionOne];
  182. [alertControl addAction:alertthree];
  183. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  184. [self presentViewController:alertControl animated:YES completion:nil];
  185. NSLog(@"%@",self.edit_select_arr);
  186. }
  187. - (IBAction)onEndEditClick:(id)sender {
  188. [self end_edit];
  189. }
  190. -(void)begin_edit
  191. {
  192. self.toolpanel.hidden = true;
  193. self.toolbar.hidden = false;
  194. // NSLog(@"begin edit %@",NSStringFromCGRect(self.cartItemView.frame));
  195. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  196. if ( UIInterfaceOrientationIsPortrait(orientation))
  197. self.cartItemView.frame = CGRectMake(0,0,768,880);
  198. else
  199. self.cartItemView.frame = CGRectMake(0,0,1024,624);
  200. [self.itemListTable setEditing:true animated:YES];
  201. [self.itemListTable reloadData];
  202. }
  203. -(void) end_edit
  204. {
  205. self.toolpanel.hidden=false;
  206. self.toolbar.hidden=true;
  207. // NSLog(@"end edit %@",NSStringFromCGRect(self.cartItemView.frame));
  208. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  209. if ( UIInterfaceOrientationIsPortrait(orientation))
  210. self.cartItemView.frame = CGRectMake(0,0,768,824);
  211. else
  212. self.cartItemView.frame = CGRectMake(0,0,1024,568);
  213. [self.itemListTable setEditing:false animated:YES];
  214. [self.edit_select_arr removeAllObjects];
  215. [self.itemListTable reloadData];
  216. // [self reload_data];
  217. }
  218. - (IBAction)onEditClick:(id)sender {
  219. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  220. //
  221. // [appDelegate test_onDecodedDataResult:@"4400022"];
  222. //
  223. //
  224. // return;
  225. //
  226. [self begin_edit];
  227. }
  228. -(void) reload_container_getdata:(bool) update_data
  229. {
  230. [super reload_container_getdata:update_data];
  231. if(update_data)
  232. [self reload_data];
  233. else
  234. {
  235. [self.itemListTable reloadData];
  236. [self refresh_total];
  237. }
  238. }
  239. -(void) showHidePrice
  240. {
  241. [self reload_container_getdata:false];
  242. // [self refresh_total];
  243. }
  244. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  245. {
  246. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  247. if (self) {
  248. // Custom initialization
  249. }
  250. return self;
  251. }
  252. - (IBAction)imgbtnClick:(UIButton *)sender {
  253. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  254. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  255. // NSLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  256. NSDictionary * item_json = self.content_arr[indexPath.row];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  257. NSString* product_id = [item_json valueForKey:@"product_id"];
  258. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  259. // dvc
  260. dvc.product_id=product_id;
  261. dvc.category_id=nil;
  262. dvc.ispush=true;
  263. [dvc reload];
  264. [self.navigationController pushViewController:dvc animated:true];
  265. }
  266. -(void)viewWillLayoutSubviews
  267. {
  268. // if(self.showDetail==true)
  269. // {
  270. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  271. // int width=175;
  272. // // int height;
  273. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  274. // {
  275. // width = 370;
  276. // // height = 400;
  277. // }
  278. // else
  279. // {
  280. // // height = 300;
  281. // width = 175;
  282. // }
  283. //
  284. //
  285. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  286. //
  287. // self.collectionview.frame=frame;
  288. //
  289. //
  290. //
  291. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  292. // self.detailView.frame = framedetail;
  293. // }
  294. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  295. // self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  296. }
  297. - (void)viewWillAppear:(BOOL)animated
  298. {
  299. [super viewWillAppear:animated];
  300. // UIApplication * app = [UIApplication sharedApplication];
  301. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  302. // if(appDelegate.bLogin==false)
  303. // {
  304. // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  305. // // loginvc.delegate = self;
  306. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  307. //
  308. // loginvc.returnValue = ^(bool blogin){
  309. //
  310. //
  311. // UIApplication * app = [UIApplication sharedApplication];
  312. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  313. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  314. //
  315. // [main_vc checklogin:true];
  316. //
  317. //// [self reload_data];
  318. //
  319. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  320. //
  321. // };
  322. //
  323. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  324. //
  325. //
  326. //
  327. //
  328. //
  329. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  330. //
  331. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  332. // [self presentViewController:navi animated:YES completion:^{
  333. //
  334. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  335. //
  336. // NSLog(@"CreateOrderViewController present.........");
  337. //
  338. // // self.btop = false;
  339. // // <#code#>
  340. // }];
  341. // }
  342. // else
  343. // {
  344. // [self reload_data];
  345. //// self.total = 0;
  346. // }
  347. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  348. }
  349. -(void) reload_data
  350. {
  351. dispatch_async(dispatch_get_main_queue(), ^{
  352. if(self.isrefreshing)
  353. return;
  354. self.itemListTable.hidden = true;
  355. self.label_net_err.hidden=true;
  356. self.isrefreshing=true;
  357. // [self.content_data removeAllObjects];
  358. // [self.itemListTable reloadData];
  359. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  360. //
  361. // [reF endRefreshing];
  362. //
  363. // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  364. self.mum.center = self.view.center;
  365. self.mum.hidden = false;
  366. [self.mum startAnimating];
  367. NSLog(@"reloading...");
  368. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  369. NSLog(@"BEGIN LOAD CART");
  370. NSDictionary* cart_json = [iSalesNetwork request_Cart];
  371. dispatch_async(dispatch_get_main_queue(), ^{
  372. //
  373. [self.mum stopAnimating];
  374. // self.content_data = [cart_json mutableCopy];
  375. self.freejson = [cart_json objectForKey:@"freeGiveaway"];
  376. self.notes = [cart_json valueForKey:@"general_note"];
  377. self.content_arr = [RAUtils dictionary2array:cart_json count_fields:@"count" item_mark:@"item_" items_mark:nil];
  378. [self.itemListTable reloadData];
  379. [self refresh_total];
  380. int result=[[cart_json valueForKey:@"result"] intValue];
  381. if(result==2||result==1||result==0)
  382. {
  383. self.itemListTable.hidden = false;
  384. UIApplication * app = [UIApplication sharedApplication];
  385. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  386. appDelegate.cart_count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  387. self.itemListTable.hidden=false;
  388. if(self.onFinishLoad)
  389. self.onFinishLoad();
  390. }
  391. else
  392. if(result==RESULT_NET_ERROR)
  393. {
  394. self.label_net_err.hidden=false;
  395. self.itemListTable.hidden=true;
  396. }
  397. else
  398. {
  399. if(result!=1)
  400. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Cart" controller:self] ;
  401. }
  402. self.isrefreshing=false;
  403. NSLog(@"FINISH LOAD CART");
  404. });
  405. });
  406. });
  407. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  408. }
  409. -(void)manually_refresh
  410. {
  411. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  412. if(self.isrefreshing)
  413. {
  414. [reF endRefreshing];
  415. return;
  416. }
  417. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  418. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  419. // DebugLog(@"refresh!!!!!!!!");
  420. }
  421. -(void) ReloadData
  422. {
  423. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  424. [reF endRefreshing];
  425. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  426. [self reload_data];
  427. }
  428. - (void)viewDidLoad
  429. {
  430. [super viewDidLoad];
  431. self.edit_select_arr= [[NSMutableArray alloc] init];
  432. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  433. self.label_net_err.layer.borderWidth = 2.0;
  434. self.label_net_err.layer.cornerRadius=15;
  435. self.label_net_err.layer.masksToBounds=true;
  436. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  437. // tap.minimumPressDuration = 0.8; //定义按的时间
  438. [self.label_net_err addGestureRecognizer:tap];
  439. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  440. ref.tag = 200 ;
  441. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  442. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  443. // ref.hidden = true;
  444. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  445. [self.itemListTable addSubview:ref];
  446. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  447. self.toolpanel.layer.masksToBounds = false;
  448. //添加四个边阴影
  449. self.toolpanel.layer.shadowColor = [UIColor blackColor].CGColor;
  450. self.toolpanel.layer.shadowOffset = CGSizeMake(0, 0);
  451. self.toolpanel.layer.shadowOpacity = 0.5;
  452. self.toolpanel.layer.shadowRadius = 2.0;
  453. // CGRect frame =self.orderInfoTableView.tableHeaderView.frame;
  454. // self.orderInfoTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.orderInfoTableView.bounds.size.width, 0.0001)];
  455. // self.orderInfoTableView.tableHeaderView.backgroundColor = [UIColor redColor];
  456. // // self.detailTable.sectionHeaderHeight = 0.0001;
  457. // self.orderInfoTableView.sectionFooterHeight = 0.0001;
  458. // Do any additional setup after loading the view.
  459. }
  460. //-(void)SelectOrder:(NSString *)orderid
  461. //{
  462. //// self.editOrderID.text=orderid;
  463. //// self.btnAddToOrder.enabled = true;
  464. //}
  465. - (void)didReceiveMemoryWarning
  466. {
  467. [super didReceiveMemoryWarning];
  468. // Dispose of any resources that can be recreated.
  469. }
  470. //- (IBAction)stepperAction:(UIStepper *)sender {
  471. //
  472. //
  473. //}
  474. - (IBAction)onContinueShoppingClick:(id)sender {
  475. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  476. [appDelegate switchToPreviousVC];
  477. }
  478. - (IBAction)onPlaceOrder:(UIButton *)sender {
  479. [self placeOrder];
  480. }
  481. -(void)placeOrder
  482. {
  483. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  484. bool have_free = false;
  485. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  486. if(count==0)
  487. {
  488. [RAUtils message_alert:@"Add some model first." title:@"Cart Is Empty." controller:self] ;
  489. return;
  490. }
  491. for(int i=0;i<count;i++)
  492. {
  493. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  494. // bool check = [[item_json valueForKey:@"check"] boolValue];
  495. NSString* product_id = [item_json valueForKey:@"product_id"];
  496. // if(check)
  497. // {
  498. // [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  499. //
  500. // }
  501. if(product_id.length==0)
  502. {
  503. [RAUtils message_alert:@"Some models in cart does not exist, you must remove them before place order." title:@"No Such Model" controller:self] ;
  504. return;
  505. }
  506. bool is_free = [[item_json valueForKey:@"is_free"]boolValue];
  507. if(is_free)
  508. have_free=true;
  509. }
  510. // disable for debug
  511. // if(checked.count==0)
  512. // {
  513. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Place Order" controller:self] ;
  514. // return;
  515. // }
  516. // NSDictionary * freejson = [self.content_data objectForKey:@"freeGiveaway"];
  517. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  518. /* FREE GIVE AWAY
  519. int free_count = [[self.freejson valueForKey:@"count"]intValue];
  520. for(int ic=free_count-1;ic>=0;ic--)
  521. {
  522. float compare_price = [[self.freejson valueForKey:[NSString stringWithFormat:@"item_%d",ic]] floatValue];
  523. if(self.total>=compare_price && !have_free&&appDelegate.can_set_cart_price)
  524. {
  525. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Free give away available.", nil) message:[NSString stringWithFormat:@"Order total price more than %.2f, do you want to add free give away item before place order?", compare_price] delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  526. alert.tag = ALERT_FREE;
  527. [alert show];
  528. return;
  529. }
  530. }
  531. */
  532. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  533. // NSString * string = [checked componentsJoinedByString:@","];
  534. // [params setValue:string forKey:@"cart2Checkbox"];
  535. CreateOrderViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CreateOrderViewController"];
  536. orderinfoVC.url_type = URL_REMOTE;
  537. orderinfoVC.request_url=URL_CARTDELIVERY;
  538. orderinfoVC.params = params;
  539. orderinfoVC.delegate=self;
  540. //
  541. // if(checked.count==count)
  542. // {
  543. // orderinfoVC.have_tail = true
  544. // }
  545. [self.navigationController pushViewController:orderinfoVC animated:true];
  546. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
  547. //
  548. //
  549. //
  550. //
  551. //
  552. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  553. // [self presentViewController:navi animated:YES completion:^{
  554. //
  555. // NSLog(@"CreateOrderViewController present.........");
  556. //
  557. // // self.btop = false;
  558. // // <#code#>
  559. // }];
  560. }
  561. //- (IBAction)onCommit:(UIButton *)sender {
  562. // ;
  563. //}
  564. //- (IBAction)CreateOrder:(id)sender {
  565. //}
  566. //- (IBAction)AddToOrder:(id)sender {
  567. //}
  568. //- (IBAction)ChooseOrder:(id)sender {
  569. //
  570. // OrderListViewController * orderlistVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  571. // // orderlistVC.delegate = self;
  572. // orderlistVC.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  573. // [self presentViewController:orderlistVC animated:YES completion:^{
  574. //
  575. // NSLog(@"login.........");
  576. // // <#code#>
  577. // }];
  578. //}
  579. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  580. {
  581. //
  582. // CommonGridViewController * orderlistVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  583. // orderlistVC.delegate = self;
  584. // orderlistVC.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  585. // [self presentViewController:orderlistVC animated:YES completion:^{
  586. //
  587. // NSLog(@"textFieldShouldBeginEditing.........");
  588. // // <#code#>
  589. // }];
  590. return NO;
  591. }
  592. - (IBAction)onDelete:(UIButton *)sender {
  593. return ;
  594. NSMutableArray* checked = [[NSMutableArray alloc] init];
  595. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  596. for(int i=0;i<count;i++)
  597. {
  598. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  599. bool check = [[item_json valueForKey:@"check"] boolValue];
  600. if(check)
  601. {
  602. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  603. }
  604. }
  605. // disable for debug
  606. if(checked.count==0)
  607. return;
  608. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  609. NSString * string = [checked componentsJoinedByString:@","];
  610. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  611. NSDictionary* cart_json = [iSalesNetwork cart_remove:string];
  612. dispatch_async(dispatch_get_main_queue(), ^{
  613. if([[cart_json valueForKey:@"result"] intValue]==2)
  614. {
  615. [self reload_data];
  616. }
  617. else
  618. {
  619. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  620. }
  621. });
  622. });
  623. }
  624. -(void)refresh_total
  625. {
  626. self.total=0;
  627. self.total_carton=0;
  628. self.total_cuft=0;
  629. self.total_weight=0;
  630. self.total_QTY=0;
  631. self.total_item=0;
  632. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  633. for(int i=0;i<count;i++)
  634. {
  635. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  636. bool bcheck = [[item_json valueForKey:@"check"]boolValue];
  637. if(bcheck)
  638. {
  639. double unitprice = [[item_json valueForKey:@"unit_price"] doubleValue];
  640. double discount = [[item_json valueForKey:@"discount"] doubleValue];
  641. int count=[[item_json valueForKey:@"count"] intValue];
  642. double totalprice =count*unitprice * (1.0-discount/100.0);
  643. double cuft=count*[[item_json valueForKey:@"cuft"] doubleValue];
  644. double weight=count*[[item_json valueForKey:@"weight"] doubleValue];
  645. int carton=/*count**/[[item_json valueForKey:@"carton"] intValue];
  646. int item_count=1;
  647. int item_qty=1;
  648. NSDictionary * bundle_item =[item_json objectForKey:@"combine"];
  649. double dprice=0;
  650. if(bundle_item!=nil)
  651. {
  652. // int citem=0;
  653. int bcount=[[bundle_item valueForKey:@"count"] intValue];
  654. for(int bc=0;bc<bcount;bc++)
  655. {
  656. NSDictionary * bitem = [bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  657. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  658. // citem+= modulus;
  659. item_count+=1;
  660. item_qty+=modulus;
  661. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  662. dprice+= uprice*modulus*count;
  663. }
  664. }
  665. totalprice+= dprice;
  666. self.total+=totalprice;
  667. self.total_cuft+=cuft;
  668. self.total_carton+=carton;
  669. self.total_weight+=weight;
  670. self.total_item+=item_count;
  671. self.total_QTY+=(item_qty)*count;
  672. }
  673. }
  674. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  675. NSString* subtotal = [NSString stringWithFormat:@"$%.2f",self.total];
  676. NSString* subcuft = [NSString stringWithFormat:@"%.2f ft³",self.total_cuft];
  677. NSString* subcarton = [NSString stringWithFormat:@"%d",self.total_carton];
  678. NSString* subweight = [NSString stringWithFormat:@"%.2f lbs",self.total_weight];
  679. NSString* subitems = [NSString stringWithFormat:@"%d",self.total_item];
  680. NSString* subqty = [NSString stringWithFormat:@"%d",self.total_QTY];
  681. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  682. {
  683. }
  684. else
  685. {
  686. subtotal=nil;
  687. }
  688. self.labelTotal.text=subtotal;
  689. self.label_cuft.text=subcuft;
  690. self.label_carton.text=subcarton;
  691. self.label_weight.text=subweight;
  692. self.label_items.text = subitems;
  693. self.label_qty.text = subqty;
  694. self.label_notes.text = [self.notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
  695. }
  696. - (IBAction)onEditGeneralNotes:(id)sender {
  697. UIViewController* parentvc=self;
  698. CartGeneralNotesViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"CartGeneralNotesViewController"];
  699. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  700. vc.notes=self.notes;
  701. // vc.delegate = parentvc.self;
  702. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  703. vc.onSetValue = ^(NSString* notes){
  704. self.notes = notes;
  705. [self refresh_total];
  706. };
  707. //
  708. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  709. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  710. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  711. [parentvc presentViewController:navi animated:YES completion:^{
  712. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  713. NSLog(@"EditModelPriceViewController present.........");
  714. // self.btop = false;
  715. // <#code#>
  716. }];
  717. }
  718. //- (IBAction)onSelectAll:(UIButton *)sender {
  719. //
  720. //
  721. // NSMutableArray* arr_ids = [[NSMutableArray alloc] init];
  722. //
  723. //
  724. //
  725. //
  726. //
  727. //
  728. // if([self.btnselect.titleLabel.text isEqualToString:@"Select all"])
  729. // {
  730. //
  731. // long count = self.content_arr.count;// [[self.content_data valueForKey:@"count"] intValue];
  732. // for(int i=0;i<count;i++)
  733. // {
  734. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy]; //[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  735. // if(![[item_json valueForKey:@"check"] boolValue])
  736. // [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  737. //
  738. // }
  739. // [self.btnselect setTitle: @"Deselect all" forState: UIControlStateNormal];
  740. // }
  741. // else
  742. // {
  743. // long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  744. // for(int i=0;i<count;i++)
  745. // {
  746. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy]; //[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  747. // if([[item_json valueForKey:@"check"] boolValue])
  748. // [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  749. //
  750. // }
  751. // [self.btnselect setTitle: @"Select all" forState: UIControlStateNormal];
  752. // }
  753. //
  754. // NSString * ids = [arr_ids componentsJoinedByString:@","];
  755. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  756. //
  757. //
  758. // NSDictionary* check_json = [iSalesNetwork cart_check:ids];
  759. //
  760. // dispatch_async(dispatch_get_main_queue(), ^{
  761. //
  762. //
  763. //
  764. // if([[check_json valueForKey:@"result"] intValue]==2)
  765. // {
  766. //
  767. // long count = self.content_arr.count;
  768. // for(int i=0;i<count;i++)
  769. // {
  770. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];
  771. // if([item_json[@"check"] boolValue])
  772. // item_json[@"check"]= @"false";
  773. // else
  774. // item_json[@"check"]= @"true";
  775. //
  776. // self.content_arr[i] = item_json;
  777. // //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  778. // }
  779. //
  780. // [self reload_container_getdata:false];
  781. //
  782. // // [self reload_data];
  783. // }
  784. // else
  785. // {
  786. // [RAUtils message_alert:[check_json valueForKey:@"err_msg"] title:nil controller:self] ;
  787. // }
  788. //
  789. //
  790. //
  791. // });
  792. // });
  793. //
  794. //
  795. //
  796. //
  797. //
  798. //
  799. // // [self refresh_total];
  800. // // self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  801. // // NSLog(self.btnselect.titleLabel.text);
  802. // // if(self.btnselect.titleLabel.text)
  803. //}
  804. //#pragma mark - MODEL cell delegate
  805. //-(void) returnQTY:(int)qty
  806. //{
  807. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  808. //
  809. // int count=[[item_json valueForKey:@"count"] intValue];
  810. //
  811. // if(count==qty)
  812. // return;
  813. // [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  814. // double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  815. // // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  816. // [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty] forKey:@"subtotal_price"];
  817. // self.content_arr[indexPath.row]= item_json;
  818. // // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  819. // [self refresh_total];
  820. // // self.total+=(unit_price*qty-sub_total);
  821. // // self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  822. //
  823. //
  824. //}
  825. #pragma mark - commoneditor controller delegate
  826. -(void) returnValue:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  827. {
  828. // [self reload_data];
  829. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  830. }
  831. #pragma mark - Table view data source
  832. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  833. {
  834. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  835. NSString* notes =[item_json valueForKey:@"note"];
  836. if(notes.length>0)
  837. {
  838. return 146;
  839. }
  840. else
  841. return 120;
  842. }
  843. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  844. //{
  845. // return 0;
  846. //}
  847. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  848. //{
  849. // return 0;
  850. //}
  851. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  852. //{
  853. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  854. // return myView;
  855. //
  856. //}
  857. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  858. //{
  859. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  860. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  861. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  862. //// titleLabel.textColor=[UIColor whiteColor];
  863. //// titleLabel.backgroundColor = [UIColor clearColor];
  864. //// if(section==0)
  865. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  866. //// else
  867. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  868. //// [titleLabel sizeToFit];
  869. //// [myView addSubview:titleLabel];
  870. ////
  871. // return myView;
  872. //}
  873. //
  874. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  875. // if(section==0)
  876. // return nil;
  877. // else
  878. // return @"detail section";
  879. //}
  880. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  881. {
  882. return 1;
  883. }
  884. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  885. {
  886. return 40;
  887. }
  888. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  889. {
  890. UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  891. myView.backgroundColor = UIColorFromRGB(0x996633);
  892. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  893. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  894. myView.layer.shadowOffset = CGSizeMake(0, 0);
  895. myView.layer.shadowOpacity = 0.5;
  896. myView.layer.shadowRadius = 2.0;
  897. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 5.5, 90, 22)];
  898. solabel.textColor=[UIColor whiteColor];
  899. solabel.backgroundColor = [UIColor clearColor];
  900. solabel.text=NSLocalizedString(@"Description", nil);
  901. [solabel sizeToFit];
  902. [myView addSubview:solabel];
  903. UILabel *contactlabel = [[UILabel alloc] initWithFrame:CGRectMake(320, 5.5, 90, 22)];
  904. contactlabel.textColor=[UIColor whiteColor];
  905. contactlabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  906. contactlabel.backgroundColor = [UIColor clearColor];
  907. contactlabel.text=NSLocalizedString(@"Unit price", nil);
  908. [contactlabel sizeToFit];
  909. [myView addSubview:contactlabel];
  910. UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(470, 5.5, 90, 22)];
  911. modellabel.textColor=[UIColor whiteColor];
  912. modellabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  913. modellabel.backgroundColor = [UIColor clearColor];
  914. modellabel.text=NSLocalizedString(@"Discount/QTY", nil);
  915. [modellabel sizeToFit];
  916. [myView addSubview:modellabel];
  917. UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(640, 5.5, 90, 22)];
  918. pricelabel.textColor=[UIColor whiteColor];
  919. pricelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  920. pricelabel.backgroundColor = [UIColor clearColor];
  921. pricelabel.text=NSLocalizedString(@"Set/Subtotal", nil);
  922. [pricelabel sizeToFit];
  923. [myView addSubview:pricelabel];
  924. //
  925. return myView;
  926. }
  927. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  928. {
  929. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  930. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  931. return count;
  932. }
  933. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  934. {
  935. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  936. // if(tableView==self.itemListTable)
  937. // {
  938. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  939. NSString *CellIdentifier = @"CartItemCell";
  940. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  941. NSDictionary * item_json = self.content_arr[indexPath.row];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  942. NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  943. bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  944. NSString* notes = [item_json valueForKey:@"note"];
  945. bool noprice =[[item_json valueForKey:@"is_no_price"]boolValue];
  946. cell.noprice = noprice;
  947. cell.enable_longpress = true;
  948. cell.bundle_item=combine_json;
  949. NSString* img_url = [item_json valueForKey:@"img_url"];
  950. NSString* description = [item_json valueForKey:@"description"];
  951. // NSString* identifier = [item_json valueForKey:@"identifier"];
  952. // NSString* attribute = [item_json valueForKey:@"attribute"];
  953. // NSString* currency = [item_json valueForKey:@"currency"];
  954. int count =[[item_json valueForKey:@"count"] intValue];
  955. int stockUom =[[item_json valueForKey:@"stockUom"] intValue];
  956. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  957. double unitprice=[[item_json valueForKey:@"unit_price"] doubleValue];
  958. double discount =[[item_json valueForKey:@"discount"] doubleValue];
  959. NSString* product_id = [item_json valueForKey:@"product_id"];
  960. // handle no such model
  961. if(product_id.length==0)
  962. cell.labelNoSuchModel.hidden=false;
  963. else
  964. cell.labelNoSuchModel.hidden=true;
  965. // discount = 5.0;
  966. cell.discount = discount;
  967. if(notes.length>0)
  968. {
  969. cell.labelNotes.text = [@"Note: " stringByAppendingString:[notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];
  970. }
  971. else
  972. cell.labelNotes.text = nil;
  973. NSString* discountstr=[NSString stringWithFormat:@"%@%% off", [RAUtils FloatFormat:discount]];
  974. if(isfree)
  975. {
  976. cell.labelOldPrice.hidden = false;
  977. cell.labelOldPrice.textColor = [UIColor redColor];
  978. cell.labelOldPrice.text=@"Free";
  979. cell.labelOldPrice.hideline = true;
  980. cell.labelDiscount.hidden = true;
  981. }
  982. else
  983. {
  984. cell.labelOldPrice.textColor = [UIColor blackColor];
  985. cell.labelOldPrice.hideline = false;
  986. if(discount==0)
  987. {
  988. cell.labelOldPrice.hidden = true;
  989. cell.labelDiscount.hidden = true;
  990. }
  991. else
  992. {
  993. cell.labelOldPrice.hidden = false;
  994. cell.labelDiscount.hidden = false;
  995. }
  996. }
  997. cell.onReturnQTY=^(int qty,NSDictionary* ext)
  998. {
  999. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1000. int count=[[item_json valueForKey:@"count"] intValue];
  1001. if(count==qty)
  1002. return;
  1003. [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  1004. [item_json setValue:[ext valueForKey:@"cuft"] forKey:@"cuft"];
  1005. [item_json setValue:[ext valueForKey:@"carton"] forKey:@"carton"];
  1006. [item_json setValue:[ext valueForKey:@"weight"] forKey:@"weight"];
  1007. double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  1008. // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1009. [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty] forKey:@"subtotal_price"];
  1010. self.content_arr[indexPath.row]= item_json;
  1011. // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1012. [self refresh_total];
  1013. };
  1014. // cell.delegate=self;
  1015. // cell.from=indexPath;
  1016. cell.unit_price = unitprice;
  1017. // cell.labelAttribute.text = attribute;
  1018. // cell.labelCurrency.text = currency;
  1019. NSString* oldprice =[NSString stringWithFormat:@"%.2f",unitprice];
  1020. NSString* newprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)*count];
  1021. NSString* newunitprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)];
  1022. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  1023. {
  1024. }
  1025. else
  1026. {
  1027. oldprice=nil;
  1028. newprice=nil;
  1029. newunitprice=nil;
  1030. }
  1031. cell.labelDiscount.text = discountstr;
  1032. cell.labelOldPrice.text = oldprice;
  1033. cell.labelMasterpack.text =[NSString stringWithFormat:@"Sold in QTY of:%d", stockUom];
  1034. cell.labelDescription.text = description;
  1035. // cell.labelIdentifier.text = identifier;
  1036. if(noprice&&unitprice==0.0)
  1037. {
  1038. cell.labelPrice.text = @"No Price";
  1039. cell.labelUnitPrice.text = @"No Price";
  1040. }
  1041. else
  1042. {
  1043. cell.labelPrice.text = newprice;
  1044. cell.labelUnitPrice.text = newunitprice;
  1045. }
  1046. bool check = [[item_json valueForKey:@"check"] boolValue];
  1047. if(check)
  1048. {
  1049. cell.img_checkmark.hidden=false;
  1050. // [item_json setValue:@"0" forKey:@"check"];
  1051. }
  1052. else
  1053. {
  1054. // [item_json setValue:@"1" forKey:@"check"];
  1055. cell.img_checkmark.hidden=true;
  1056. }
  1057. NSString* cart_item_id= [item_json valueForKey:@"cart_item_id"];
  1058. cell.cart_id = cart_item_id;
  1059. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/ true)
  1060. [cell init_Stepper:stockUom max:9999 min:stockUom value:count];
  1061. else
  1062. [cell init_Stepper:1 max:9999 min:1 value:count];
  1063. [cell set_Count:count];
  1064. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  1065. NSString* file_name=[img_url lastPathComponent];
  1066. NSData* img_data=[iSalesDB load_cached_img:file_name];
  1067. if(img_data!=nil)
  1068. {
  1069. UIImage * img =[UIImage imageWithData:img_data];
  1070. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  1071. }
  1072. else
  1073. {
  1074. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1075. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1076. dispatch_async(dispatch_get_main_queue(), ^{
  1077. if(downloadimg_data!=nil)
  1078. {
  1079. [iSalesDB cache_img:downloadimg_data :file_name ];
  1080. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1081. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  1082. }
  1083. else
  1084. {
  1085. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  1086. }
  1087. });
  1088. });
  1089. }
  1090. cell.backgroundColor = UIColorFromRGB(0xF2EEEA);
  1091. if(self.itemListTable.editing)
  1092. {
  1093. cell.editCount.enabled = false;
  1094. cell.stepper.hidden=true;
  1095. }
  1096. else
  1097. {
  1098. cell.editCount.enabled=true;
  1099. cell.stepper.hidden=false;
  1100. }
  1101. return cell;
  1102. // }
  1103. // else
  1104. // {
  1105. // NSString *CellIdentifier = @"OrderInfoListItem";
  1106. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1107. // return cell;
  1108. // }
  1109. }
  1110. -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  1111. {
  1112. if(!self.itemListTable.editing)
  1113. return;
  1114. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1115. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1116. if ([self.edit_select_arr containsObject:cart_item_id])
  1117. {
  1118. [self.edit_select_arr removeObject:cart_item_id];
  1119. }
  1120. }
  1121. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  1122. {
  1123. NSMutableDictionary * item_json =[self.content_arr[indexPath.row] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1124. if(self.itemListTable.editing)
  1125. {
  1126. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1127. if (![self.edit_select_arr containsObject:cart_item_id]) {
  1128. [self.edit_select_arr addObject:cart_item_id];
  1129. }
  1130. return;
  1131. }
  1132. // no checkmark support anymore for cart
  1133. return;
  1134. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  1135. cell.selected = false;
  1136. // self.dirty = true;
  1137. bool check = [[item_json valueForKey:@"check"] boolValue];
  1138. if(check)
  1139. {
  1140. [item_json setValue:@"false" forKey:@"check"];
  1141. cell.img_checkmark.hidden=true;
  1142. [self.btnselect setTitle: @"Select all" forState: UIControlStateNormal];
  1143. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1144. // self.total-=totalprice;
  1145. }
  1146. else
  1147. {
  1148. [item_json setValue:@"true" forKey:@"check"];
  1149. cell.img_checkmark.hidden=false;
  1150. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1151. // self.total+=totalprice;
  1152. }
  1153. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1154. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1155. NSDictionary* check_json = [iSalesNetwork cart_check:ids];
  1156. dispatch_async(dispatch_get_main_queue(), ^{
  1157. if([[check_json valueForKey:@"result"] intValue]==2)
  1158. {
  1159. if([item_json[@"check"] boolValue]==false)
  1160. item_json[@"check"]= @"false";
  1161. else
  1162. item_json[@"check"]= @"true";
  1163. self.content_arr[indexPath.row] = item_json;
  1164. //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1165. [self reload_container_getdata:false];
  1166. }
  1167. else
  1168. {
  1169. [RAUtils message_alert:[check_json valueForKey:@"err_msg"] title:nil controller:self] ;
  1170. }
  1171. });
  1172. });
  1173. //debug
  1174. //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1175. [self refresh_total];
  1176. //self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  1177. }
  1178. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  1179. if(self.itemListTable.editing)
  1180. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  1181. else
  1182. return UITableViewCellEditingStyleDelete;
  1183. }
  1184. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  1185. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  1186. {
  1187. // 添加一个删除按钮
  1188. self.indexPath=indexPath;
  1189. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" Delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1190. NSLog(@"delete click");
  1191. //[self.itemListTable setEditing:false animated:YES];
  1192. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure remove model from cart?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  1193. alert.tag = ALERT_DEL;
  1194. // alert.
  1195. [alert show];
  1196. }];
  1197. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  1198. UITableViewRowAction *addWatchAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Move To Wishlist" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1199. NSLog(@"Wishlist click");
  1200. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1201. NSString* cart_item_id = [item_json valueForKey:@"cart_item_id"];
  1202. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1203. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1204. NSDictionary* return_json = [iSalesNetwork move_cart2wish:cart_item_id ];
  1205. dispatch_async(dispatch_get_main_queue(), ^{
  1206. if([[return_json valueForKey:@"result"] intValue]==2)
  1207. {
  1208. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1209. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1210. for(int i=0;i<self.content_arr.count;i++)
  1211. {
  1212. NSDictionary* obj_json = self.content_arr[i];
  1213. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1214. if([_id isEqualToString:cart_item_id])
  1215. {
  1216. [self.content_arr removeObjectAtIndex:i];
  1217. [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1218. appDelegate.cart_count--;
  1219. [appDelegate update_count_mark];
  1220. [self refresh_total];
  1221. }
  1222. }
  1223. // if(self.WatchlistBlock!=nil)
  1224. // {
  1225. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  1226. //
  1227. //
  1228. // CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  1229. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  1230. // iv.image = img;
  1231. // // [self.contentView addSubview:iv];
  1232. // self.WatchlistBlock(iv);
  1233. //
  1234. // }
  1235. }
  1236. else
  1237. {
  1238. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Move To Wishlist" controller:self] ;
  1239. }
  1240. });
  1241. });
  1242. }];
  1243. addWatchAction.backgroundColor = UIColorFromRGB(0x339966);
  1244. // 添加一个编辑按钮
  1245. UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Edit Price"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1246. NSLog(@"edit click");
  1247. UIViewController* parentvc=self;
  1248. EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  1249. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1250. vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  1251. vc.price = [[item_json valueForKey:@"unit_price"] floatValue];
  1252. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1253. // vc.delegate = parentvc.self;
  1254. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1255. vc.onSetValue = ^(double price,double discount){
  1256. bool bmodify=false;
  1257. if(price!= [[item_json valueForKey:@"unit_price"] floatValue])
  1258. bmodify=true;
  1259. NSString* notes = [item_json valueForKey:@"note"];
  1260. if(notes.length==0)
  1261. notes=@"";
  1262. if(bmodify)
  1263. {
  1264. if([notes isEqualToString:@""])
  1265. notes=@"PRICE MODIFIED";
  1266. NSRange range;
  1267. range = [notes rangeOfString:@"PRICE MODIFIED"];
  1268. if (range.location != NSNotFound) {
  1269. }else{
  1270. notes= [notes stringByAppendingString:@"\nPRICE MODIFIED"];
  1271. }
  1272. }
  1273. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1274. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Update Price"];
  1275. NSDictionary* cart_json = nil;
  1276. cart_json = [iSalesNetwork cart_setPrice:[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]] price:price discount:discount notes:notes];
  1277. dispatch_async(dispatch_get_main_queue(), ^{
  1278. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1279. if([[cart_json valueForKey:@"result"] intValue]==2)
  1280. {
  1281. [item_json setValue:[NSNumber numberWithDouble:price ] forKey:@"unit_price"];
  1282. [item_json setValue:[NSNumber numberWithDouble:discount ] forKey:@"discount"];
  1283. [item_json setValue:notes forKey:@"note"];
  1284. self.content_arr[indexPath.row ] = item_json;
  1285. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1286. }
  1287. else
  1288. {
  1289. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Update Price" controller:self] ;
  1290. }
  1291. });
  1292. });
  1293. // [self reload_data];//[main_vc checklogin:true];
  1294. };
  1295. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1296. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1297. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  1298. [parentvc presentViewController:navi animated:YES completion:^{
  1299. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1300. NSLog(@"EditModelPriceViewController present.........");
  1301. // self.btop = false;
  1302. // <#code#>
  1303. }];
  1304. // [self.itemListTable setEditing:false animated:YES];
  1305. //[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1306. }];
  1307. editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  1308. // // 添加一个更多按钮
  1309. //
  1310. // UITableViewRowAction *moreRowAction = [UITableView RowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1311. //
  1312. // NSLog(@点击了更多);
  1313. //
  1314. //
  1315. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1316. //
  1317. // }];
  1318. //
  1319. // moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
  1320. //
  1321. // 将设置好的按钮放到数组中返回
  1322. // UITableViewRowAction *freeRowAction = nil;
  1323. //
  1324. //
  1325. // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1326. //
  1327. // bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  1328. //
  1329. // if(!isfree)
  1330. // {
  1331. //
  1332. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Set Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1333. //
  1334. // NSLog(@"free click");
  1335. //
  1336. //
  1337. //
  1338. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Free give away confirm", nil) message:NSLocalizedString(@"Are you sure make this model free?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  1339. // alert.tag = ALERT_SET_FREE;
  1340. // // alert.
  1341. // [alert show];
  1342. // }];
  1343. // }
  1344. // else
  1345. // {
  1346. //
  1347. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Not Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1348. //
  1349. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Restore free model confirm", nil) message:NSLocalizedString(@"Are you sure restore free model?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  1350. // alert.tag = ALERT_RESTORE_FREE;
  1351. // // alert.
  1352. // [alert show];
  1353. // }];
  1354. // }
  1355. //
  1356. //
  1357. //
  1358. // freeRowAction.backgroundColor = [UIColor lightGrayColor];
  1359. UITableViewRowAction *noteRowAction = nil;
  1360. // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1361. noteRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Edit note" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1362. NSLog(@"item note click");
  1363. UIViewController* parentvc=self;
  1364. ItemNotesViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"ItemNotesViewController"];
  1365. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1366. vc.onSaveNote=^(NSString* notes){
  1367. [item_json setValue:notes forKey:@"note"];
  1368. self.content_arr[indexPath.row ] = item_json;
  1369. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1370. };
  1371. vc.notes=[item_json valueForKey:@"note"];
  1372. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1373. // vc.delegate = parentvc.self;
  1374. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1375. // vc.onSetValue = ^(){
  1376. // [self reload_data];//[main_vc checklogin:true];
  1377. // };
  1378. //
  1379. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1380. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1381. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  1382. [parentvc presentViewController:navi animated:YES completion:^{
  1383. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1384. NSLog(@"EditModelPriceViewController present.........");
  1385. // self.btop = false;
  1386. // <#code#>
  1387. }];
  1388. // [self.itemListTable setEditing:false animated:YES];
  1389. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1390. }];
  1391. noteRowAction.backgroundColor = [UIColor lightGrayColor];
  1392. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1393. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  1394. {
  1395. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]
  1396. NSString* product_id = [item_json valueForKey:@"product_id"];
  1397. NSMutableArray* arr=[@[] mutableCopy];
  1398. if(product_id.length>0)
  1399. [arr addObject:noteRowAction];
  1400. [arr addObject:deleteRowAction];
  1401. if(product_id.length>0)
  1402. [arr addObject:addWatchAction];
  1403. if(appDelegate.can_set_cart_price && product_id.length>0)
  1404. {
  1405. [arr addObject:editRowAction];
  1406. // [arr addObject:freeRowAction];
  1407. }
  1408. return arr;
  1409. // return @[deleteRowAction,addWatchAction, editRowAction,freeRowAction];
  1410. }
  1411. else
  1412. return @[deleteRowAction,addWatchAction];
  1413. }
  1414. - (void)tableView:(UITableView *)tableView
  1415. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1416. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1417. //
  1418. // NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1419. //
  1420. //
  1421. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1422. //
  1423. // NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  1424. //
  1425. // dispatch_async(dispatch_get_main_queue(), ^{
  1426. //
  1427. //
  1428. //
  1429. // if(cart_json!=nil)
  1430. // {
  1431. //
  1432. // [self reload_data];
  1433. // }
  1434. //
  1435. // });
  1436. // });
  1437. // sqlite3* db = [ApexMobileDB get_db];
  1438. // NSString* sql = [NSString stringWithFormat:@"delete from history where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
  1439. // [ApexMobileDB execSql:sql db:db];
  1440. // sqlite3_close(db);
  1441. //
  1442. // [self.data.pagedata removeObjectAtIndex:indexPath.row];
  1443. //
  1444. // [tableView reloadData];
  1445. }
  1446. #pragma mark - UIAlertViewDelegate
  1447. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  1448. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  1449. {
  1450. if(alertView.tag==ALERT_DEL)
  1451. {
  1452. if(buttonIndex!=alertView.cancelButtonIndex)
  1453. {
  1454. // [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1455. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1456. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1457. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1458. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  1459. dispatch_async(dispatch_get_main_queue(), ^{
  1460. if([[cart_json valueForKey:@"result"] intValue]==2)
  1461. {
  1462. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1463. for(int i=0;i<self.content_arr.count;i++)
  1464. {
  1465. NSDictionary* obj_json = self.content_arr[i];
  1466. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1467. if([_id isEqualToString:ids])
  1468. {
  1469. [self.content_arr removeObjectAtIndex:i];
  1470. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1471. break;
  1472. // appDelegate.cart_count--;
  1473. // [appDelegate update_count_mark];
  1474. }
  1475. }
  1476. [self.itemListTable reloadData];
  1477. [self refresh_total];
  1478. // [self reload_data];
  1479. }
  1480. else
  1481. {
  1482. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  1483. }
  1484. });
  1485. });
  1486. }
  1487. }
  1488. else if(alertView.tag==ALERT_FREE)
  1489. {
  1490. if(buttonIndex==alertView.cancelButtonIndex)
  1491. {
  1492. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1493. // NSString * string = [checked componentsJoinedByString:@","];
  1494. // [params setValue:string forKey:@"cart2Checkbox"];
  1495. CreateOrderViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CreateOrderViewController"];
  1496. orderinfoVC.url_type = URL_REMOTE;
  1497. orderinfoVC.request_url=URL_CARTDELIVERY;
  1498. orderinfoVC.params = params;
  1499. orderinfoVC.delegate=self;
  1500. [self.navigationController pushViewController:orderinfoVC animated:true];
  1501. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
  1502. //
  1503. //
  1504. //
  1505. //
  1506. //
  1507. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  1508. // [self presentViewController:navi animated:YES completion:^{
  1509. //
  1510. // NSLog(@"CreateOrderViewController present.........");
  1511. //
  1512. // // self.btop = false;
  1513. // // <#code#>
  1514. // }];
  1515. }
  1516. }
  1517. else if(alertView.tag==ALERT_SET_FREE)
  1518. {
  1519. if(buttonIndex!=alertView.cancelButtonIndex)
  1520. {
  1521. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1522. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1523. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1524. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1525. NSDictionary* cart_json = [iSalesNetwork cart_setFree:ids isfree:true];
  1526. dispatch_async(dispatch_get_main_queue(), ^{
  1527. if([[cart_json valueForKey:@"result"] intValue]==2)
  1528. {
  1529. [self reload_data];
  1530. }
  1531. else
  1532. {
  1533. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Set Free Model" controller:self] ;
  1534. }
  1535. });
  1536. });
  1537. }
  1538. }
  1539. else if(alertView.tag==ALERT_RESTORE_FREE)
  1540. {
  1541. if(buttonIndex!=alertView.cancelButtonIndex)
  1542. {
  1543. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1544. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1545. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1546. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1547. NSDictionary* cart_json = [iSalesNetwork cart_setFree:ids isfree:false];
  1548. dispatch_async(dispatch_get_main_queue(), ^{
  1549. if([[cart_json valueForKey:@"result"] intValue]==2)
  1550. {
  1551. [self reload_data];
  1552. }
  1553. else
  1554. {
  1555. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Restore Model Price" controller:self] ;
  1556. }
  1557. });
  1558. });
  1559. }
  1560. }
  1561. }
  1562. #pragma mark - Support scanner
  1563. -(void) onDecodedData:(NSString*) value
  1564. {
  1565. // return;
  1566. [self addtocart:value];
  1567. }
  1568. //-(void) neworder
  1569. //{
  1570. //
  1571. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  1572. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1573. //
  1574. // NSDictionary* return_json = [iSalesNetwork new_Order];
  1575. //
  1576. // dispatch_async(dispatch_get_main_queue(), ^{
  1577. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1578. //
  1579. //
  1580. // if([[return_json valueForKey:@"result"] intValue]==2)
  1581. // {
  1582. // int result=[[return_json valueForKey:@"result"] intValue];
  1583. // if(result==2)
  1584. // {
  1585. // //successed.
  1586. //
  1587. // NSString* order_code = [return_json valueForKey:@"orderCode"];
  1588. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1589. // appDelegate.order_code = order_code;
  1590. //
  1591. //
  1592. // [self addtocart];
  1593. //
  1594. //
  1595. // // [self ReloadData];
  1596. //
  1597. //
  1598. // // if(self.shopCartBlock!=nil)
  1599. // // {
  1600. // // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  1601. // //
  1602. // //
  1603. // // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  1604. // // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  1605. // // iv.image = img;
  1606. // // // [self.contentView addSubview:iv];
  1607. // // self.shopCartBlock(iv);
  1608. // //
  1609. // // }
  1610. //
  1611. // }
  1612. // }
  1613. // else
  1614. // {
  1615. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  1616. // }
  1617. //
  1618. //
  1619. //
  1620. //
  1621. // });
  1622. // });
  1623. //}
  1624. -(void) addtocart:(NSString*) modelname
  1625. {
  1626. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1627. NSDictionary* return_json = [iSalesNetwork add_toCart_byName: modelname];
  1628. dispatch_async(dispatch_get_main_queue(), ^{
  1629. if([[return_json valueForKey:@"result"] intValue]==2)
  1630. {
  1631. NSDictionary* newitem = [return_json objectForKey:@"item_0"];
  1632. NSString* item_id = [newitem valueForKey:@"cart_item_id"];
  1633. // bool isnew=false;
  1634. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1635. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1636. for(int i=0;i<self.content_arr.count;i++)
  1637. {
  1638. NSDictionary* obj_json = self.content_arr[i];
  1639. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1640. if([_id isEqualToString:item_id])
  1641. {
  1642. // self.content_arr[i] = newitem;
  1643. [self.content_arr removeObjectAtIndex:i];
  1644. break;
  1645. // [self.content_arr removeObjectAtIndex:i];
  1646. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1647. // appDelegate.cart_count--;
  1648. // [appDelegate update_count_mark];
  1649. }
  1650. }
  1651. [self.content_arr insertObject:newitem atIndex:0];
  1652. [self.itemListTable reloadData];
  1653. [self refresh_total];
  1654. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1655. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1656. }
  1657. else
  1658. {
  1659. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  1660. }
  1661. });
  1662. });
  1663. }
  1664. @end