CartViewController.m 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  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. #import "DefaultAppearance.h"
  21. #import "DefaultTableHeaderView.h"
  22. #import "SortItemViewController.h"
  23. #import "SortButton.h"
  24. #import "NotificationNameCenter.h"
  25. #import "Singleton.h"
  26. #import "JKMessageBoxController.h"
  27. #import "PDFViewController.h"
  28. #define ALERT_FREE 1024
  29. #define ALERT_DEL 1025
  30. #define ALERT_SET_FREE 1026
  31. #define ALERT_RESTORE_FREE 1027
  32. @interface CartViewController ()
  33. {
  34. CGRect cancelButtonFrame;
  35. }
  36. @property (nonatomic,strong) SortItemViewController *sortItemController;
  37. @property (nonatomic,assign) int sortIndex;
  38. @property (nonatomic,strong) SortButton *sortButton;
  39. @property (strong, nonatomic) IBOutlet UIButton *placeOrderButton;
  40. @property (strong, nonatomic) IBOutlet UIButton *cancelOrderButton;
  41. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  42. @property (strong, nonatomic) IBOutlet UIButton *emailButton;
  43. @property (strong, nonatomic) IBOutlet UIButton *printButton;
  44. @property (nonatomic,assign) BOOL currentOrderIsMerged;
  45. @property (nonatomic,copy) NSString *print_url;
  46. @property (nonatomic,assign) BOOL available;///<所有Model均有库存,才能Place Order。
  47. @end
  48. @implementation CartViewController
  49. - (NSOperationQueue *)dataOperationQueue {
  50. if (!_dataOperationQueue) {
  51. _dataOperationQueue = [[NSOperationQueue alloc] init];
  52. _dataOperationQueue.maxConcurrentOperationCount = 1;
  53. }
  54. return _dataOperationQueue;
  55. }
  56. - (void)dealloc {
  57. [[NSNotificationCenter defaultCenter] removeObserver:self];
  58. }
  59. -(void) logout
  60. {
  61. [self.content_arr removeAllObjects];
  62. }
  63. - (IBAction)OnEditSelectClick:(id)sender {
  64. NSMutableArray* arr_ids = [[NSMutableArray alloc] init];
  65. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  66. if(count==0)
  67. {
  68. [RAUtils message_alert:@"No item in the cart" title:@"Cart" controller:self] ;
  69. return;
  70. }
  71. for(int i=0;i<count;i++)
  72. {
  73. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  74. [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  75. }
  76. NSString * ids = [arr_ids componentsJoinedByString:@","];
  77. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select all model and" message:nil preferredStyle:UIAlertControllerStyleAlert];
  78. //block代码块取代了delegate
  79. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  80. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove all models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  81. //block代码块取代了delegate
  82. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  83. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  84. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  85. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  88. if([[cart_json valueForKey:@"result"] intValue]==2)
  89. {
  90. [self end_edit];
  91. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  92. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  93. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  94. }
  95. else
  96. {
  97. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  98. }
  99. });
  100. });
  101. }];
  102. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  103. DebugLog(@"No");
  104. }];
  105. [alertControl addAction:actionOne];
  106. [alertControl addAction:alertthree];
  107. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  108. [self presentViewController:alertControl animated:YES completion:nil];
  109. DebugLog(@"%@",self.edit_select_arr);
  110. }];
  111. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Move To Wish List" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  112. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Move Models To Wish List"];
  113. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  114. NSDictionary* cart_json = [iSalesNetwork move_cart2wish:ids];
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  117. if([[cart_json valueForKey:@"result"] intValue]==2)
  118. {
  119. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  120. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  121. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  122. [self end_edit];
  123. }
  124. else
  125. {
  126. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Move Models To Wish List" controller:self] ;
  127. }
  128. });
  129. });
  130. }];
  131. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  132. DebugLog(@"No");
  133. }];
  134. [alertControl addAction:actionOne];
  135. [alertControl addAction:actionTwo];
  136. [alertControl addAction:alertthree];
  137. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  138. [self presentViewController:alertControl animated:YES completion:nil];
  139. DebugLog(@"%@",self.edit_select_arr);
  140. }
  141. - (IBAction)onEditAddWishClick:(id)sender {
  142. if(self.edit_select_arr.count==0)
  143. {
  144. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Move To Wish List" controller:self] ;
  145. return;
  146. }
  147. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  148. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove selected models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  149. // //block代码块取代了delegate
  150. //
  151. //
  152. //
  153. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  154. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Move Models To Wish List"];
  155. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  156. NSDictionary* cart_json = [iSalesNetwork move_cart2wish:ids];
  157. dispatch_async(dispatch_get_main_queue(), ^{
  158. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  159. if([[cart_json valueForKey:@"result"] intValue]==2)
  160. {
  161. [self end_edit];
  162. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  163. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  164. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  165. }
  166. else
  167. {
  168. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Move To Wish List" controller:self] ;
  169. }
  170. });
  171. });
  172. // }];
  173. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  174. // DebugLog(@"No");
  175. // }];
  176. // [alertControl addAction:actionOne];
  177. //
  178. // [alertControl addAction:alertthree];
  179. //
  180. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  181. //
  182. //
  183. // [self presentViewController:alertControl animated:YES completion:nil];
  184. //
  185. // DebugLog(@"%@",self.edit_select_arr);
  186. }
  187. - (IBAction)onEditDelClick:(id)sender {
  188. if(self.edit_select_arr.count==0)
  189. {
  190. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Delete From Cart" controller:self] ;
  191. return;
  192. }
  193. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  194. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove selected models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  195. //block代码块取代了delegate
  196. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  197. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  198. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  199. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  200. dispatch_async(dispatch_get_main_queue(), ^{
  201. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  202. if([[cart_json valueForKey:@"result"] intValue]==2)
  203. {
  204. [self end_edit];
  205. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  206. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  207. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  208. }
  209. else
  210. {
  211. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  212. }
  213. });
  214. });
  215. }];
  216. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  217. DebugLog(@"No");
  218. }];
  219. [alertControl addAction:actionOne];
  220. [alertControl addAction:alertthree];
  221. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  222. [self presentViewController:alertControl animated:YES completion:nil];
  223. DebugLog(@"%@",self.edit_select_arr);
  224. }
  225. - (IBAction)onEndEditClick:(id)sender {
  226. [self end_edit];
  227. }
  228. -(void)begin_edit
  229. {
  230. self.toolpanel.hidden = true;
  231. self.toolbar.hidden = false;
  232. // DebugLog(@"begin edit %@",NSStringFromCGRect(self.cartItemView.frame));
  233. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  234. if ( UIInterfaceOrientationIsPortrait(orientation))
  235. self.cartItemView.frame = CGRectMake(0,0,768,880);
  236. else
  237. self.cartItemView.frame = CGRectMake(0,0,1024,624);
  238. [self.itemListTable setEditing:true animated:YES];
  239. [self.itemListTable reloadData];
  240. }
  241. -(void) end_edit
  242. {
  243. self.toolpanel.hidden=false;
  244. self.toolbar.hidden=true;
  245. // DebugLog(@"end edit %@",NSStringFromCGRect(self.cartItemView.frame));
  246. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  247. if ( UIInterfaceOrientationIsPortrait(orientation))
  248. self.cartItemView.frame = CGRectMake(0,0,768,824);
  249. else
  250. self.cartItemView.frame = CGRectMake(0,0,1024,568);
  251. [self.itemListTable setEditing:false animated:YES];
  252. [self.edit_select_arr removeAllObjects];
  253. [self.itemListTable reloadData];
  254. // [self reload_data];
  255. }
  256. - (IBAction)onEditClick:(id)sender {
  257. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  258. //
  259. // [appDelegate test_onDecodedDataResult:@"4400022"];
  260. //
  261. //
  262. // return;
  263. [self begin_edit];
  264. }
  265. -(void) reload_container_getdata:(bool) update_data
  266. {
  267. [super reload_container_getdata:update_data];
  268. if(update_data)
  269. [self reload_data];
  270. else
  271. {
  272. [self.itemListTable reloadData];
  273. [self refresh_total];
  274. }
  275. }
  276. -(void) showHidePrice
  277. {
  278. [self reload_container_getdata:false];
  279. // [self refresh_total];
  280. }
  281. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  282. {
  283. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  284. if (self) {
  285. // Custom initialization
  286. }
  287. return self;
  288. }
  289. - (IBAction)imgbtnClick:(UIButton *)sender {
  290. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  291. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  292. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  293. NSDictionary * item_json = self.content_arr[indexPath.row];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  294. NSString* product_id = [item_json valueForKey:@"product_id"];
  295. int is_rate = [item_json[@"is_rate"] intValue];
  296. if(is_rate==1)
  297. return;
  298. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  299. // dvc
  300. dvc.product_id=product_id;
  301. dvc.category_id=nil;
  302. dvc.ispush=true;
  303. [dvc reload];
  304. [self.navigationController pushViewController:dvc animated:true];
  305. }
  306. -(void)viewWillLayoutSubviews
  307. {
  308. // if(self.showDetail==true)
  309. // {
  310. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  311. // int width=175;
  312. // // int height;
  313. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  314. // {
  315. // width = 370;
  316. // // height = 400;
  317. // }
  318. // else
  319. // {
  320. // // height = 300;
  321. // width = 175;
  322. // }
  323. //
  324. //
  325. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  326. //
  327. // self.collectionview.frame=frame;
  328. //
  329. //
  330. //
  331. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  332. // self.detailView.frame = framedetail;
  333. // }
  334. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  335. // self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  336. }
  337. - (void)viewWillAppear:(BOOL)animated
  338. {
  339. [super viewWillAppear:animated];
  340. // print 13 email 19 cancel 24 place order
  341. // Shop 权限检查
  342. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  343. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  344. if (![Singleton sharedInstance].permissions_edit_order) {
  345. CGRect frame0 = self.cancelOrderButton.frame;
  346. CGRect frame1 = self.placeOrderButton.frame;
  347. CGRect frame2 = self.emailButton.frame;
  348. CGRect frame3 = self.printButton.frame;
  349. CGFloat x = CGRectGetMaxX(frame1) - frame0.size.width;
  350. frame0.origin.x = x;
  351. self.cancelOrderButton.frame = frame0;
  352. x -= 19 + CGRectGetWidth(frame2);
  353. frame2.origin.x = x;
  354. self.emailButton.frame = frame2;
  355. x -= 13 + CGRectGetWidth(frame3);
  356. frame3.origin.x = x;
  357. self.printButton.frame = frame3;
  358. self.placeOrderButton.hidden = YES;
  359. } else {
  360. self.placeOrderButton.hidden = NO;
  361. CGSize size = cancelButtonFrame.size;
  362. CGFloat x = CGRectGetMinX(self.placeOrderButton.frame) - 24 - size.width;
  363. CGFloat y = CGRectGetMinY(self.placeOrderButton.frame);
  364. self.cancelOrderButton.frame = CGRectMake(x, y, size.width, size.height);
  365. CGRect frame2 = self.emailButton.frame;
  366. x -= 19 + CGRectGetWidth(frame2);
  367. frame2.origin.x = x;
  368. self.emailButton.frame = frame2;
  369. CGRect frame3 = self.printButton.frame;
  370. x -= 13 + CGRectGetWidth(frame3);
  371. frame3.origin.x = x;
  372. self.printButton.frame = frame3;
  373. }
  374. } else {
  375. // self.cancelOrderButton.frame = cancelButtonFrame;
  376. self.placeOrderButton.hidden = NO;
  377. CGSize size = cancelButtonFrame.size;
  378. CGFloat x = CGRectGetMinX(self.placeOrderButton.frame) - 24 - size.width;
  379. CGFloat y = CGRectGetMinY(self.placeOrderButton.frame);
  380. self.cancelOrderButton.frame = CGRectMake(x, y, size.width, size.height);
  381. CGRect frame2 = self.emailButton.frame;
  382. x -= 19 + CGRectGetWidth(frame2);
  383. frame2.origin.x = x;
  384. self.emailButton.frame = frame2;
  385. CGRect frame3 = self.printButton.frame;
  386. x -= 13 + CGRectGetWidth(frame3);
  387. frame3.origin.x = x;
  388. self.printButton.frame = frame3;
  389. }
  390. // 离线隐藏Email
  391. self.emailButton.hidden = appDelegate.offline_mode;
  392. // 离线隐藏Print
  393. self.printButton.hidden = appDelegate.offline_mode;
  394. // [self reload_data]; // 视图没及时刷新
  395. // UIApplication * app = [UIApplication sharedApplication];
  396. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  397. // if(appDelegate.bLogin==false)
  398. // {
  399. // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  400. // // loginvc.delegate = self;
  401. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  402. //
  403. // loginvc.returnValue = ^(bool blogin){
  404. //
  405. //
  406. // UIApplication * app = [UIApplication sharedApplication];
  407. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  408. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  409. //
  410. // [main_vc checklogin:true];
  411. //
  412. //// [self reload_data];
  413. //
  414. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  415. //
  416. // };
  417. //
  418. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  419. //
  420. //
  421. //
  422. //
  423. //
  424. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  425. //
  426. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  427. // [self presentViewController:navi animated:YES completion:^{
  428. //
  429. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  430. //
  431. // DebugLog(@"CreateOrderViewController present.........");
  432. //
  433. // // self.btop = false;
  434. // // <#code#>
  435. // }];
  436. // }
  437. // else
  438. // {
  439. // [self reload_data];
  440. //// self.total = 0;
  441. // }
  442. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  443. }
  444. - (void)checkProductAvailable:(NSArray *)contents {
  445. [contents enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  446. if ([obj isKindOfClass:[NSDictionary class]]) {
  447. NSDictionary *item_json = (NSDictionary *)obj;
  448. BOOL isAvailable = ![[item_json valueForKey:@"available"] isEqualToString:@"In Production"] || [[item_json valueForKey:@"available"] integerValue] != 0;
  449. self.available = self.available && isAvailable;
  450. }
  451. }];
  452. }
  453. -(void) operation_reload_data
  454. {
  455. if (self.dataOperationQueue.operationCount > 1) {
  456. return;
  457. }
  458. __weak typeof(self) weakself = self;
  459. dispatch_async(dispatch_get_main_queue(), ^{
  460. if(weakself.isrefreshing)
  461. return;
  462. weakself.available = YES;
  463. weakself.itemListTable.hidden = true;
  464. weakself.label_net_err.hidden=true;
  465. weakself.isrefreshing=true;
  466. // [self.content_data removeAllObjects];
  467. // [self.itemListTable reloadData];
  468. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  469. //
  470. // [reF endRefreshing];
  471. //
  472. // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  473. weakself.mum.center = weakself.view.center;
  474. weakself.mum.hidden = YES;
  475. // [self.mum startAnimating];
  476. DebugLog(@"reloading...");
  477. UIAlertView *loadingView = [RAUtils waiting_alert:@"Please Wait" title:@"Loading"];
  478. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  479. DebugLog(@"BEGIN LOAD CART");
  480. NSDictionary* cart_json = [iSalesNetwork request_Cart:weakself.sortIndex];
  481. dispatch_async(dispatch_get_main_queue(), ^{
  482. //
  483. // [self.mum stopAnimating];
  484. [loadingView dismissWithClickedButtonIndex:0 animated:YES];
  485. // self.content_data = [cart_json mutableCopy];
  486. weakself.freejson = [cart_json objectForKey:@"freeGiveaway"];
  487. weakself.notes = [cart_json valueForKey:@"general_note"];
  488. weakself.content_arr = [RAUtils dictionary2array:cart_json count_fields:@"count" item_mark:@"item_" items_mark:nil];
  489. weakself.currentOrderIsMerged = [[cart_json valueForKey:@"is_merged_order"] boolValue];
  490. [Singleton sharedInstance].currentOrderIsMerged = weakself.currentOrderIsMerged;
  491. [weakself.itemListTable reloadData];
  492. weakself.print_url = [cart_json valueForKey:@"cart_print_url"];
  493. [weakself checkProductAvailable:weakself.content_arr];
  494. [weakself refresh_total];
  495. int result=[[cart_json valueForKey:@"result"] intValue];
  496. if (weakself.dataOperationQueue.operationCount > 1) {
  497. weakself.isrefreshing=false;
  498. return;
  499. }
  500. if(result==2||result==1||result==0)
  501. {
  502. [weakself.edit_select_arr removeAllObjects];
  503. weakself.itemListTable.hidden = false;
  504. UIApplication * app = [UIApplication sharedApplication];
  505. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  506. appDelegate.cart_count =weakself.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  507. weakself.itemListTable.hidden=false;
  508. if(weakself.onFinishLoad)
  509. weakself.onFinishLoad();
  510. }
  511. else
  512. if(result==RESULT_NET_ERROR)
  513. {
  514. weakself.label_net_err.hidden=false;
  515. weakself.itemListTable.hidden=true;
  516. }
  517. else
  518. {
  519. if(result!=1)
  520. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Cart" controller:weakself] ;
  521. }
  522. weakself.isrefreshing=false;
  523. DebugLog(@"FINISH LOAD CART");
  524. });
  525. });
  526. });
  527. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  528. }
  529. - (void) reload_data {
  530. __weak typeof(self) weakself = self;
  531. self.currentOrderIsMerged = NO;
  532. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  533. if (weakself) {
  534. __strong typeof(weakself) strongself = weakself;
  535. [strongself operation_reload_data];
  536. }
  537. }];
  538. [self.dataOperationQueue addOperation:operation];
  539. }
  540. -(void)manually_refresh
  541. {
  542. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  543. if(self.isrefreshing)
  544. {
  545. [reF endRefreshing];
  546. return;
  547. }
  548. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  549. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  550. // DebugLog(@"refresh!!!!!!!!");
  551. }
  552. -(void) ReloadData
  553. {
  554. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  555. [reF endRefreshing];
  556. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  557. [self reload_data];
  558. }
  559. - (void)viewDidLoad
  560. {
  561. [super viewDidLoad];
  562. self.edit_select_arr= [[NSMutableArray alloc] init];
  563. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  564. self.label_net_err.layer.borderWidth = 2.0;
  565. self.label_net_err.layer.cornerRadius=15;
  566. self.label_net_err.layer.masksToBounds=true;
  567. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(priceTypeChanged:) name:Change_Price_Type_Notification object:nil];
  568. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  569. // tap.minimumPressDuration = 0.8; //定义按的时间
  570. [self.label_net_err addGestureRecognizer:tap];
  571. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  572. ref.tag = 200 ;
  573. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  574. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  575. // ref.hidden = true;
  576. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  577. [self.itemListTable addSubview:ref];
  578. self.sortIndex = 0;
  579. self.currentOrderIsMerged = NO;
  580. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  581. self.toolpanel.layer.masksToBounds = false;
  582. //添加四个边阴影
  583. self.toolpanel.layer.shadowColor = [UIColor blackColor].CGColor;
  584. self.toolpanel.layer.shadowOffset = CGSizeMake(0, 0);
  585. self.toolpanel.layer.shadowOpacity = 0.5;
  586. self.toolpanel.layer.shadowRadius = 2.0;
  587. cancelButtonFrame = self.cancelOrderButton.frame;
  588. // CGRect frame =self.orderInfoTableView.tableHeaderView.frame;
  589. // self.orderInfoTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.orderInfoTableView.bounds.size.width, 0.0001)];
  590. // self.orderInfoTableView.tableHeaderView.backgroundColor = [UIColor redColor];
  591. // // self.detailTable.sectionHeaderHeight = 0.0001;
  592. // self.orderInfoTableView.sectionFooterHeight = 0.0001;
  593. // Do any additional setup after loading the view.
  594. }
  595. //-(void)SelectOrder:(NSString *)orderid
  596. //{
  597. //// self.editOrderID.text=orderid;
  598. //// self.btnAddToOrder.enabled = true;
  599. //}
  600. - (void)didReceiveMemoryWarning
  601. {
  602. [super didReceiveMemoryWarning];
  603. // Dispose of any resources that can be recreated.
  604. }
  605. //- (IBAction)stepperAction:(UIStepper *)sender {
  606. //
  607. //
  608. //}
  609. - (IBAction)onContinueShoppingClick:(id)sender {
  610. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  611. [appDelegate switchToPreviousVC];
  612. }
  613. - (IBAction)onPlaceOrder:(UIButton *)sender {
  614. [self placeOrder];
  615. }
  616. -(void)placeOrder
  617. {
  618. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  619. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  620. if (![Singleton sharedInstance].permissions_edit_order) {
  621. return;
  622. }
  623. // if (!self.available) {
  624. //
  625. // [RAUtils message_alert:@"There are some products is not available" title:@"Warning" controller:self];
  626. // return;
  627. // }
  628. }
  629. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  630. bool have_free = false;
  631. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  632. if(count==0)
  633. {
  634. [RAUtils message_alert:@"Add some model first." title:@"Cart Is Empty." controller:self] ;
  635. return;
  636. }
  637. for(int i=0;i<count;i++)
  638. {
  639. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  640. // bool check = [[item_json valueForKey:@"check"] boolValue];
  641. NSString* product_id = [item_json valueForKey:@"product_id"];
  642. // if(check)
  643. // {
  644. // [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  645. //
  646. // }
  647. if(product_id.length==0)
  648. {
  649. [RAUtils message_alert:@"Some models in cart does not exist, you must remove them before place order." title:@"No Such Model" controller:self] ;
  650. return;
  651. }
  652. bool is_free = [[item_json valueForKey:@"is_free"]boolValue];
  653. if(is_free)
  654. have_free=true;
  655. }
  656. // disable for debug
  657. // if(checked.count==0)
  658. // {
  659. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Place Order" controller:self] ;
  660. // return;
  661. // }
  662. // NSDictionary * freejson = [self.content_data objectForKey:@"freeGiveaway"];
  663. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  664. /* FREE GIVE AWAY
  665. int free_count = [[self.freejson valueForKey:@"count"]intValue];
  666. for(int ic=free_count-1;ic>=0;ic--)
  667. {
  668. float compare_price = [[self.freejson valueForKey:[NSString stringWithFormat:@"item_%d",ic]] floatValue];
  669. if(self.total>=compare_price && !have_free&&appDelegate.can_set_cart_price)
  670. {
  671. 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];
  672. alert.tag = ALERT_FREE;
  673. [alert show];
  674. return;
  675. }
  676. }
  677. */
  678. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  679. // NSString * string = [checked componentsJoinedByString:@","];
  680. // [params setValue:string forKey:@"cart2Checkbox"];
  681. CreateOrderViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CreateOrderViewController"];
  682. orderinfoVC.disable_dropdown_refresh=true;
  683. orderinfoVC.url_type = URL_REMOTE;
  684. orderinfoVC.request_url=URL_CARTDELIVERY;
  685. orderinfoVC.params = params;
  686. orderinfoVC.delegate=self;
  687. //
  688. // if(checked.count==count)
  689. // {
  690. // orderinfoVC.have_tail = true
  691. // }
  692. [self.navigationController pushViewController:orderinfoVC animated:true];
  693. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
  694. //
  695. //
  696. //
  697. //
  698. //
  699. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  700. // [self presentViewController:navi animated:YES completion:^{
  701. //
  702. // DebugLog(@"CreateOrderViewController present.........");
  703. //
  704. // // self.btop = false;
  705. // // <#code#>
  706. // }];
  707. }
  708. //- (IBAction)onCommit:(UIButton *)sender {
  709. // ;
  710. //}
  711. //- (IBAction)CreateOrder:(id)sender {
  712. //}
  713. //- (IBAction)AddToOrder:(id)sender {
  714. //}
  715. //- (IBAction)ChooseOrder:(id)sender {
  716. //
  717. // OrderListViewController * orderlistVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  718. // // orderlistVC.delegate = self;
  719. // orderlistVC.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  720. // [self presentViewController:orderlistVC animated:YES completion:^{
  721. //
  722. // DebugLog(@"login.........");
  723. // // <#code#>
  724. // }];
  725. //}
  726. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  727. {
  728. //
  729. // CommonGridViewController * orderlistVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  730. // orderlistVC.delegate = self;
  731. // orderlistVC.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  732. // [self presentViewController:orderlistVC animated:YES completion:^{
  733. //
  734. // DebugLog(@"textFieldShouldBeginEditing.........");
  735. // // <#code#>
  736. // }];
  737. return NO;
  738. }
  739. - (IBAction)onDelete:(UIButton *)sender {
  740. return ;
  741. NSMutableArray* checked = [[NSMutableArray alloc] init];
  742. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  743. for(int i=0;i<count;i++)
  744. {
  745. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  746. bool check = [[item_json valueForKey:@"check"] boolValue];
  747. if(check)
  748. {
  749. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  750. }
  751. }
  752. // disable for debug
  753. if(checked.count==0)
  754. return;
  755. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  756. NSString * string = [checked componentsJoinedByString:@","];
  757. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  758. NSDictionary* cart_json = [iSalesNetwork cart_remove:string];
  759. dispatch_async(dispatch_get_main_queue(), ^{
  760. if([[cart_json valueForKey:@"result"] intValue]==2)
  761. {
  762. [self reload_data];
  763. }
  764. else
  765. {
  766. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  767. }
  768. });
  769. });
  770. }
  771. -(void)refresh_total
  772. {
  773. self.total=0;
  774. self.total_carton=0;
  775. self.total_cuft=0;
  776. self.total_weight=0;
  777. self.total_QTY=0;
  778. self.total_item=0;
  779. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  780. for(int i=0;i<count;i++)
  781. {
  782. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  783. bool bcheck = [[item_json valueForKey:@"check"]boolValue];
  784. if(bcheck)
  785. {
  786. double unitprice = [[item_json valueForKey:@"unit_price"] doubleValue];
  787. double discount = [[item_json valueForKey:@"discount"] doubleValue];
  788. int count=[[item_json valueForKey:@"count"] intValue];
  789. double totalprice =count*unitprice * (1.0-discount/100.0);
  790. double cuft=[[item_json valueForKey:@"cuft"] doubleValue];
  791. double weight=[[item_json valueForKey:@"weight"] doubleValue];
  792. int carton=/*count**/[[item_json valueForKey:@"carton"] intValue];
  793. int item_count=1;
  794. int item_qty=1;
  795. NSDictionary * bundle_item =[item_json objectForKey:@"combine"];
  796. double dprice=0;
  797. if(bundle_item!=nil)
  798. {
  799. // int citem=0;
  800. int bcount=[[bundle_item valueForKey:@"count"] intValue];
  801. for(int bc=0;bc<bcount;bc++)
  802. {
  803. NSDictionary * bitem = [bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  804. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  805. // citem+= modulus;
  806. item_count+=1;
  807. item_qty+=modulus;
  808. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  809. dprice+= uprice*modulus*count;
  810. }
  811. }
  812. totalprice+= dprice;
  813. self.total+=totalprice;
  814. self.total_cuft+=cuft;
  815. self.total_carton+=carton;
  816. self.total_weight+=weight;
  817. self.total_item+=item_count;
  818. self.total_QTY+=(item_qty)*count;
  819. }
  820. }
  821. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  822. NSString* subtotal = [NSString stringWithFormat:@"$%.2f",self.total];
  823. NSString* subcuft = [NSString stringWithFormat:@"%.2f ft³",self.total_cuft];
  824. NSString* subcarton = [NSString stringWithFormat:@"%d",self.total_carton];
  825. NSString* subweight = [NSString stringWithFormat:@"%.2f lbs",self.total_weight];
  826. NSString* subitems = [NSString stringWithFormat:@"%d",self.total_item];
  827. NSString* subqty = [NSString stringWithFormat:@"%d",self.total_QTY];
  828. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  829. {
  830. }
  831. else
  832. {
  833. subtotal=nil;
  834. }
  835. self.labelTotal.text=subtotal;
  836. self.label_cuft.text=subcuft;
  837. self.label_carton.text=subcarton;
  838. self.label_weight.text=subweight;
  839. self.label_items.text = subitems;
  840. self.label_qty.text = subqty;
  841. self.label_notes.text = [self.notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
  842. }
  843. - (IBAction)onEditGeneralNotes:(id)sender {
  844. UIViewController* parentvc=self;
  845. CartGeneralNotesViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"CartGeneralNotesViewController"];
  846. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  847. vc.notes=self.notes;
  848. // vc.delegate = parentvc.self;
  849. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  850. vc.onSetValue = ^(NSString* notes){
  851. self.notes = notes;
  852. [self refresh_total];
  853. };
  854. //
  855. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  856. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  857. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  858. [parentvc presentViewController:navi animated:YES completion:^{
  859. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  860. DebugLog(@"EditModelPriceViewController present.........");
  861. // self.btop = false;
  862. // <#code#>
  863. }];
  864. }
  865. //- (IBAction)onSelectAll:(UIButton *)sender {
  866. //
  867. //
  868. // NSMutableArray* arr_ids = [[NSMutableArray alloc] init];
  869. //
  870. //
  871. //
  872. //
  873. //
  874. //
  875. // if([self.btnselect.titleLabel.text isEqualToString:@"Select all"])
  876. // {
  877. //
  878. // long count = self.content_arr.count;// [[self.content_data valueForKey:@"count"] intValue];
  879. // for(int i=0;i<count;i++)
  880. // {
  881. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy]; //[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  882. // if(![[item_json valueForKey:@"check"] boolValue])
  883. // [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  884. //
  885. // }
  886. // [self.btnselect setTitle: @"Deselect all" forState: UIControlStateNormal];
  887. // }
  888. // else
  889. // {
  890. // long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  891. // for(int i=0;i<count;i++)
  892. // {
  893. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy]; //[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  894. // if([[item_json valueForKey:@"check"] boolValue])
  895. // [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  896. //
  897. // }
  898. // [self.btnselect setTitle: @"Select all" forState: UIControlStateNormal];
  899. // }
  900. //
  901. // NSString * ids = [arr_ids componentsJoinedByString:@","];
  902. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  903. //
  904. //
  905. // NSDictionary* check_json = [iSalesNetwork cart_check:ids];
  906. //
  907. // dispatch_async(dispatch_get_main_queue(), ^{
  908. //
  909. //
  910. //
  911. // if([[check_json valueForKey:@"result"] intValue]==2)
  912. // {
  913. //
  914. // long count = self.content_arr.count;
  915. // for(int i=0;i<count;i++)
  916. // {
  917. // NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];
  918. // if([item_json[@"check"] boolValue])
  919. // item_json[@"check"]= @"false";
  920. // else
  921. // item_json[@"check"]= @"true";
  922. //
  923. // self.content_arr[i] = item_json;
  924. // //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  925. // }
  926. //
  927. // [self reload_container_getdata:false];
  928. //
  929. // // [self reload_data];
  930. // }
  931. // else
  932. // {
  933. // [RAUtils message_alert:[check_json valueForKey:@"err_msg"] title:nil controller:self] ;
  934. // }
  935. //
  936. //
  937. //
  938. // });
  939. // });
  940. //
  941. //
  942. //
  943. //
  944. //
  945. //
  946. // // [self refresh_total];
  947. // // self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  948. // // DebugLog(self.btnselect.titleLabel.text);
  949. // // if(self.btnselect.titleLabel.text)
  950. //}
  951. //#pragma mark - MODEL cell delegate
  952. //-(void) returnQTY:(int)qty
  953. //{
  954. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  955. //
  956. // int count=[[item_json valueForKey:@"count"] intValue];
  957. //
  958. // if(count==qty)
  959. // return;
  960. // [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  961. // double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  962. // // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  963. // [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty] forKey:@"subtotal_price"];
  964. // self.content_arr[indexPath.row]= item_json;
  965. // // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  966. // [self refresh_total];
  967. // // self.total+=(unit_price*qty-sub_total);
  968. // // self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  969. //
  970. //
  971. //}
  972. - (IBAction)OnCancelOrderClicked:(id)sender {
  973. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Cancel Order" message:@"Are you sure to cancel the order in cart ?" preferredStyle:UIAlertControllerStyleAlert];
  974. //block代码块取代了delegate
  975. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  976. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  977. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"];
  978. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  979. NSDictionary* return_json = [iSalesNetwork cancel_Order:nil order_code:appDelegate.order_code];
  980. dispatch_async(dispatch_get_main_queue(), ^{
  981. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  982. if([[return_json valueForKey:@"result"] intValue]==2)
  983. {
  984. //if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]])
  985. {
  986. appDelegate.order_code= nil;
  987. //appDelegate.user_type = 0;
  988. appDelegate.customerInfo = nil;
  989. appDelegate.contact_id = nil;
  990. appDelegate.order_customer_id = nil;
  991. [appDelegate SetSo:nil];
  992. appDelegate.cart_count=0;
  993. [appDelegate update_count_mark];
  994. }
  995. //[RAUtils message_alert:nil title:@"Order Canceled" controller:self] ;
  996. //[self ReloadData];
  997. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  998. // // appDelegate.order_code = nil;
  999. //
  1000. // [appDelegate closeOrder];
  1001. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1002. [((MainViewController*)appDelegate.main_vc) switchToHome];
  1003. // if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.submit_order_logout)
  1004. // {
  1005. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  1006. // }
  1007. }
  1008. else
  1009. {
  1010. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ;
  1011. }
  1012. });
  1013. });
  1014. }];
  1015. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1016. DebugLog(@"No");
  1017. }];
  1018. [alertControl addAction:actionOne];
  1019. [alertControl addAction:alertthree];
  1020. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1021. [self presentViewController:alertControl animated:YES completion:nil];
  1022. DebugLog(@"%@",self.edit_select_arr);
  1023. }
  1024. #pragma mark - commoneditor controller delegate
  1025. -(void) returnValue:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  1026. {
  1027. // [self reload_data];
  1028. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  1029. }
  1030. #pragma mark - Table view data source
  1031. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  1032. {
  1033. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1034. NSString* notes =[item_json valueForKey:@"note"];
  1035. if(notes.length>0)
  1036. {
  1037. return 146;
  1038. }
  1039. else
  1040. return 120;
  1041. }
  1042. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  1043. //{
  1044. // return 0;
  1045. //}
  1046. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  1047. //{
  1048. // return 0;
  1049. //}
  1050. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  1051. //{
  1052. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  1053. // return myView;
  1054. //
  1055. //}
  1056. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  1057. //{
  1058. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  1059. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  1060. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  1061. //// titleLabel.textColor=[UIColor whiteColor];
  1062. //// titleLabel.backgroundColor = [UIColor clearColor];
  1063. //// if(section==0)
  1064. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  1065. //// else
  1066. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  1067. //// [titleLabel sizeToFit];
  1068. //// [myView addSubview:titleLabel];
  1069. ////
  1070. // return myView;
  1071. //}
  1072. //
  1073. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  1074. // if(section==0)
  1075. // return nil;
  1076. // else
  1077. // return @"detail section";
  1078. //}
  1079. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  1080. {
  1081. return 1;
  1082. }
  1083. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  1084. {
  1085. return 40;
  1086. }
  1087. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  1088. {
  1089. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  1090. if(value==nil)
  1091. value=@"";
  1092. unsigned long color = strtoul([value UTF8String],0,16);
  1093. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  1094. // myView.backgroundColor = UIColorFromRGB(0x996633);
  1095. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  1096. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  1097. myView.layer.shadowOffset = CGSizeMake(0, 0);
  1098. myView.layer.shadowOpacity = 0.5;
  1099. myView.layer.shadowRadius = 2.0;
  1100. // UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
  1101. // sortButton.frame = CGRectMake(10, 5.5, (40 - 5.5 * 2), (40 - 5.5 * 2));
  1102. // [sortButton setImage:[UIImage imageNamed:@"check_1_24"] forState:UIControlStateNormal];
  1103. // [sortButton addTarget:self action:@selector(cartSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  1104. [myView addSubview:self.sortButton];
  1105. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 5.5, 90, 22)];
  1106. solabel.textColor=UIColorFromRGB(color);
  1107. solabel.backgroundColor = [UIColor clearColor];
  1108. solabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  1109. solabel.text=NSLocalizedString(@"Description", nil);
  1110. [solabel sizeToFit];
  1111. [myView addSubview:solabel];
  1112. CGFloat x = tableView.bounds.size.width - (768 - 320);
  1113. UILabel *contactlabel = [[UILabel alloc] initWithFrame:CGRectMake(320, 5.5, 90, 22)];
  1114. contactlabel.textColor=UIColorFromRGB(color);
  1115. contactlabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  1116. contactlabel.backgroundColor = [UIColor clearColor];
  1117. contactlabel.text=NSLocalizedString(@"Unit price", nil);
  1118. [contactlabel sizeToFit];
  1119. [myView addSubview:contactlabel];
  1120. x = tableView.bounds.size.width - (768 - 470);
  1121. UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(470, 5.5, 90, 22)];
  1122. modellabel.textColor=UIColorFromRGB(color);
  1123. modellabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  1124. modellabel.backgroundColor = [UIColor clearColor];
  1125. modellabel.text=NSLocalizedString(@"Discount/QTY", nil);
  1126. [modellabel sizeToFit];
  1127. [myView addSubview:modellabel];
  1128. x = tableView.bounds.size.width - (768 - 640);
  1129. UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 5.5, 90, 22)];
  1130. pricelabel.textColor=UIColorFromRGB(color);
  1131. pricelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  1132. pricelabel.backgroundColor = [UIColor clearColor];
  1133. pricelabel.text=NSLocalizedString(@"Set/Subtotal", nil);
  1134. [pricelabel sizeToFit];
  1135. [myView addSubview:pricelabel];
  1136. //
  1137. return myView;
  1138. }
  1139. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  1140. {
  1141. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  1142. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  1143. return count;
  1144. }
  1145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  1146. {
  1147. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1148. // if(tableView==self.itemListTable)
  1149. // {
  1150. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1151. NSString *CellIdentifier = @"CartItemCell";
  1152. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1153. NSDictionary * item_json = self.content_arr[indexPath.row];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1154. NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  1155. bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  1156. NSString* notes = [item_json valueForKey:@"note"];
  1157. bool noprice =[[item_json valueForKey:@"is_no_price"]boolValue];
  1158. cell.noprice = noprice;
  1159. cell.enable_longpress = true;
  1160. cell.bundle_item=combine_json;
  1161. NSString* img_url = [item_json valueForKey:@"img_url"];
  1162. NSString* description = [item_json valueForKey:@"description"];
  1163. // NSString* identifier = [item_json valueForKey:@"identifier"];
  1164. // NSString* attribute = [item_json valueForKey:@"attribute"];
  1165. // NSString* currency = [item_json valueForKey:@"currency"];
  1166. int count =[[item_json valueForKey:@"count"] intValue];
  1167. int stockUom =[[item_json valueForKey:@"stockUom"] intValue];
  1168. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1169. double unitprice=[[item_json valueForKey:@"unit_price"] doubleValue];
  1170. double discount =[[item_json valueForKey:@"discount"] doubleValue];
  1171. NSString* product_id = [item_json valueForKey:@"product_id"];
  1172. // handle no such model
  1173. if(product_id.length==0)
  1174. cell.labelNoSuchModel.hidden=false;
  1175. else
  1176. cell.labelNoSuchModel.hidden=true;
  1177. // discount = 5.0;
  1178. cell.discount = discount;
  1179. CGRect frame = cell.frame;
  1180. if(notes.length>0)
  1181. {
  1182. cell.labelNotes.text = [@"Note: " stringByAppendingString:[notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];
  1183. frame.size.height = 146;
  1184. }
  1185. else
  1186. {
  1187. cell.labelNotes.text = nil;
  1188. frame.size.height = 120;
  1189. }
  1190. // cell.frame = frame;
  1191. NSString* discountstr=[NSString stringWithFormat:@"%@%% off", [RAUtils FloatFormat:discount]];
  1192. if(isfree)
  1193. {
  1194. cell.labelOldPrice.hidden = false;
  1195. cell.labelOldPrice.textColor = [UIColor redColor];
  1196. cell.labelOldPrice.text=@"Free";
  1197. cell.labelOldPrice.hideline = true;
  1198. cell.labelDiscount.hidden = true;
  1199. }
  1200. else
  1201. {
  1202. cell.labelOldPrice.textColor = [UIColor blackColor];
  1203. cell.labelOldPrice.hideline = false;
  1204. if(discount==0)
  1205. {
  1206. cell.labelOldPrice.hidden = true;
  1207. cell.labelDiscount.hidden = true;
  1208. }
  1209. else
  1210. {
  1211. cell.labelOldPrice.hidden = false;
  1212. cell.labelDiscount.hidden = false;
  1213. }
  1214. }
  1215. cell.onReturnQTY=^(int qty,NSDictionary* ext)
  1216. {
  1217. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1218. // int count=[[item_json valueForKey:@"count"] intValue];
  1219. // if(count==qty)
  1220. // return;
  1221. [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  1222. [item_json setValue:[ext valueForKey:@"cuft"] forKey:@"cuft"];
  1223. [item_json setValue:[ext valueForKey:@"carton"] forKey:@"carton"];
  1224. [item_json setValue:[ext valueForKey:@"weight"] forKey:@"weight"];
  1225. double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  1226. // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1227. [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty] forKey:@"subtotal_price"];
  1228. self.content_arr[indexPath.row]= item_json;
  1229. // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1230. [self refresh_total];
  1231. };
  1232. // cell.delegate=self;
  1233. // cell.from=indexPath;
  1234. cell.unit_price = unitprice;
  1235. // cell.labelAttribute.text = attribute;
  1236. // cell.labelCurrency.text = currency;
  1237. NSString* oldprice =[NSString stringWithFormat:@"%.2f",unitprice];
  1238. NSString* newprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)*count];
  1239. NSString* newunitprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)];
  1240. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  1241. {
  1242. }
  1243. else
  1244. {
  1245. oldprice=nil;
  1246. newprice=nil;
  1247. newunitprice=nil;
  1248. }
  1249. cell.labelDiscount.text = discountstr;
  1250. cell.labelOldPrice.text = oldprice;
  1251. cell.labelMasterpack.text =[NSString stringWithFormat:@"Sold in QTY of:%d", stockUom];
  1252. cell.labelDescription.text = description;
  1253. // cell.labelIdentifier.text = identifier;
  1254. if(noprice&&unitprice==0.0)
  1255. {
  1256. cell.labelPrice.text = @"No Price";
  1257. cell.labelUnitPrice.text = @"No Price";
  1258. }
  1259. else
  1260. {
  1261. cell.labelPrice.text = newprice;
  1262. cell.labelUnitPrice.text = newunitprice;
  1263. }
  1264. bool check = [[item_json valueForKey:@"check"] boolValue];
  1265. if(check)
  1266. {
  1267. cell.img_checkmark.hidden=false;
  1268. // [item_json setValue:@"0" forKey:@"check"];
  1269. }
  1270. else
  1271. {
  1272. // [item_json setValue:@"1" forKey:@"check"];
  1273. cell.img_checkmark.hidden=true;
  1274. }
  1275. NSString* cart_item_id= [item_json valueForKey:@"cart_item_id"];
  1276. cell.cart_id = cart_item_id;
  1277. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/ true)
  1278. [cell init_Stepper:stockUom max:9999 min:stockUom value:count];
  1279. else
  1280. [cell init_Stepper:1 max:9999 min:1 value:count];
  1281. [cell set_Count:count];
  1282. if (![cell.imageName isEqualToString:img_url]) {
  1283. cell.imageName = img_url;
  1284. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  1285. NSString* file_name=[img_url lastPathComponent];
  1286. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  1287. if(img_data!=nil)
  1288. {
  1289. UIImage * img =[UIImage imageWithData:img_data];
  1290. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  1291. }
  1292. else
  1293. {
  1294. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1295. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1296. dispatch_async(dispatch_get_main_queue(), ^{
  1297. if(downloadimg_data!=nil)
  1298. {
  1299. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  1300. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1301. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  1302. }
  1303. else
  1304. {
  1305. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  1306. }
  1307. });
  1308. });
  1309. }
  1310. }
  1311. cell.backgroundColor = [UIColor whiteColor];
  1312. if(self.itemListTable.editing)
  1313. {
  1314. cell.editCount.enabled = false;
  1315. cell.stepper.hidden=true;
  1316. }
  1317. else
  1318. {
  1319. cell.editCount.enabled=true;
  1320. cell.stepper.hidden=false;
  1321. }
  1322. return cell;
  1323. // }
  1324. // else
  1325. // {
  1326. // NSString *CellIdentifier = @"OrderInfoListItem";
  1327. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1328. // return cell;
  1329. // }
  1330. }
  1331. -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  1332. {
  1333. if(!self.itemListTable.editing)
  1334. return;
  1335. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1336. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1337. if ([self.edit_select_arr containsObject:cart_item_id])
  1338. {
  1339. [self.edit_select_arr removeObject:cart_item_id];
  1340. }
  1341. }
  1342. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  1343. {
  1344. NSMutableDictionary * item_json =[self.content_arr[indexPath.row] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1345. if(self.itemListTable.editing)
  1346. {
  1347. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1348. if (![self.edit_select_arr containsObject:cart_item_id]) {
  1349. [self.edit_select_arr addObject:cart_item_id];
  1350. }
  1351. return;
  1352. }
  1353. // no checkmark support anymore for cart
  1354. return;
  1355. // ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  1356. //
  1357. //
  1358. // cell.selected = false;
  1359. // // self.dirty = true;
  1360. //
  1361. //
  1362. // bool check = [[item_json valueForKey:@"check"] boolValue];
  1363. // if(check)
  1364. // {
  1365. // [item_json setValue:@"false" forKey:@"check"];
  1366. // cell.img_checkmark.hidden=true;
  1367. // [self.btnselect setTitle: @"Select all" forState: UIControlStateNormal];
  1368. // // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1369. // // self.total-=totalprice;
  1370. // }
  1371. // else
  1372. // {
  1373. // [item_json setValue:@"true" forKey:@"check"];
  1374. // cell.img_checkmark.hidden=false;
  1375. // // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  1376. // // self.total+=totalprice;
  1377. // }
  1378. //
  1379. //
  1380. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1381. //
  1382. //
  1383. // NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1384. //
  1385. // NSDictionary* check_json = [iSalesNetwork cart_check:ids];
  1386. //
  1387. // dispatch_async(dispatch_get_main_queue(), ^{
  1388. //
  1389. //
  1390. //
  1391. // if([[check_json valueForKey:@"result"] intValue]==2)
  1392. // {
  1393. //
  1394. //
  1395. // if([item_json[@"check"] boolValue]==false)
  1396. // item_json[@"check"]= @"false";
  1397. // else
  1398. // item_json[@"check"]= @"true";
  1399. //
  1400. // self.content_arr[indexPath.row] = item_json;
  1401. // //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1402. //
  1403. //
  1404. // [self reload_container_getdata:false];
  1405. //
  1406. //
  1407. // }
  1408. // else
  1409. // {
  1410. // [RAUtils message_alert:[check_json valueForKey:@"err_msg"] title:nil controller:self] ;
  1411. // }
  1412. //
  1413. //
  1414. //
  1415. // });
  1416. // });
  1417. //
  1418. // //debug
  1419. // //[self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1420. //
  1421. // [self refresh_total];
  1422. //self.labelTotal.text=[NSString stringWithFormat:@"$%.2f",self.total];
  1423. }
  1424. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  1425. if(self.itemListTable.editing)
  1426. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  1427. else
  1428. return UITableViewCellEditingStyleDelete;
  1429. }
  1430. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  1431. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  1432. {
  1433. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1434. // 添加一个删除按钮
  1435. self.indexPath=indexPath;
  1436. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" Delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1437. DebugLog(@"delete click");
  1438. //[self.itemListTable setEditing:false animated:YES];
  1439. 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];
  1440. alert.tag = ALERT_DEL;
  1441. // alert.
  1442. [alert show];
  1443. }];
  1444. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  1445. UITableViewRowAction *addWatchAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Move To Wish List" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1446. DebugLog(@"Wishlist click");
  1447. NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1448. NSString* cart_item_id = [item_json valueForKey:@"cart_item_id"];
  1449. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1450. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1451. // ModelItemCell* cell=[self.itemListTable cellForRowAtIndexPath:indexPath];
  1452. // [cell cancel_setQTY];
  1453. NSDictionary* return_json = [iSalesNetwork move_cart2wish:cart_item_id ];
  1454. dispatch_async(dispatch_get_main_queue(), ^{
  1455. if([[return_json valueForKey:@"result"] intValue]==2)
  1456. {
  1457. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1458. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1459. for(int i=0;i<self.content_arr.count;i++)
  1460. {
  1461. NSDictionary* obj_json = self.content_arr[i];
  1462. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1463. if([_id isEqualToString:cart_item_id])
  1464. {
  1465. [self.content_arr removeObjectAtIndex:i];
  1466. [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1467. appDelegate.cart_count--;
  1468. [appDelegate update_count_mark];
  1469. [self refresh_total];
  1470. }
  1471. }
  1472. [self.itemListTable reloadData];
  1473. }
  1474. else
  1475. {
  1476. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Move To Wish List" controller:self] ;
  1477. }
  1478. });
  1479. });
  1480. }];
  1481. addWatchAction.backgroundColor = UIColorFromRGB(0x339966);
  1482. // 添加一个编辑按钮
  1483. __weak typeof(self) weakself = self;
  1484. UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Edit Price"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1485. DebugLog(@"edit click");
  1486. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  1487. if (self.currentOrderIsMerged) { // 合并过的订单不能编辑价格
  1488. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Sales Order Can't Edit Price" preferredStyle:UIAlertControllerStyleAlert];
  1489. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  1490. [alert dismissViewControllerAnimated:YES completion:nil];
  1491. }];
  1492. [alert addAction:action];
  1493. if (weakself) {
  1494. __strong typeof(weakself) strongself = weakself;
  1495. [strongself presentViewController:alert animated:YES completion:nil];
  1496. }
  1497. return ;
  1498. }
  1499. if ([Singleton sharedInstance].npd_shop_price_type == 0) { // 提货价不能编辑价格
  1500. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Current Price Type is %@,You Can't Change it",[Singleton sharedInstance].deliveryString] preferredStyle:UIAlertControllerStyleAlert];
  1501. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  1502. [alert dismissViewControllerAnimated:YES completion:nil];
  1503. }];
  1504. [alert addAction:action];
  1505. if (weakself) {
  1506. __strong typeof(weakself) strongself = weakself;
  1507. [strongself presentViewController:alert animated:YES completion:nil];
  1508. }
  1509. return ;
  1510. }
  1511. }
  1512. UIViewController* parentvc=self;
  1513. EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  1514. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1515. vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  1516. vc.price = [[item_json valueForKey:@"unit_price"] floatValue];
  1517. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1518. // vc.delegate = parentvc.self;
  1519. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1520. vc.onSetValue = ^(double price,double discount){
  1521. bool bmodify=false;
  1522. if(price!= [[item_json valueForKey:@"unit_price"] floatValue])
  1523. bmodify=true;
  1524. NSString* notes = [item_json valueForKey:@"note"];
  1525. if(notes.length==0)
  1526. notes=@"";
  1527. if(bmodify)
  1528. {
  1529. if([notes isEqualToString:@""])
  1530. notes=@"PRICE MODIFIED";
  1531. NSRange range;
  1532. range = [notes rangeOfString:@"PRICE MODIFIED"];
  1533. if (range.location != NSNotFound) {
  1534. }else{
  1535. notes= [notes stringByAppendingString:@"\nPRICE MODIFIED"];
  1536. }
  1537. }
  1538. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Update Price"];
  1539. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1540. NSDictionary* cart_json = nil;
  1541. cart_json = [iSalesNetwork cart_setPrice:[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]] price:price discount:discount notes:notes];
  1542. dispatch_async(dispatch_get_main_queue(), ^{
  1543. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1544. if([[cart_json valueForKey:@"result"] intValue]==2)
  1545. {
  1546. [item_json setValue:[NSNumber numberWithDouble:price ] forKey:@"unit_price"];
  1547. [item_json setValue:[NSNumber numberWithDouble:discount ] forKey:@"discount"];
  1548. [item_json setValue:notes forKey:@"note"];
  1549. self.content_arr[indexPath.row ] = item_json;
  1550. tableView.editing = false;
  1551. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1552. [self refresh_total];
  1553. }
  1554. else
  1555. {
  1556. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Update Price" controller:self] ;
  1557. }
  1558. });
  1559. });
  1560. // [self reload_data];//[main_vc checklogin:true];
  1561. };
  1562. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1563. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1564. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  1565. [parentvc presentViewController:navi animated:YES completion:^{
  1566. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1567. DebugLog(@"EditModelPriceViewController present.........");
  1568. // self.btop = false;
  1569. // <#code#>
  1570. }];
  1571. // [self.itemListTable setEditing:false animated:YES];
  1572. //[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1573. }];
  1574. editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  1575. // // 添加一个更多按钮
  1576. //
  1577. // UITableViewRowAction *moreRowAction = [UITableView RowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1578. //
  1579. // DebugLog(@点击了更多);
  1580. //
  1581. //
  1582. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1583. //
  1584. // }];
  1585. //
  1586. // moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
  1587. //
  1588. // 将设置好的按钮放到数组中返回
  1589. // UITableViewRowAction *freeRowAction = nil;
  1590. //
  1591. //
  1592. // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1593. //
  1594. // bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  1595. //
  1596. // if(!isfree)
  1597. // {
  1598. //
  1599. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Set Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1600. //
  1601. // DebugLog(@"free click");
  1602. //
  1603. //
  1604. //
  1605. // 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];
  1606. // alert.tag = ALERT_SET_FREE;
  1607. // // alert.
  1608. // [alert show];
  1609. // }];
  1610. // }
  1611. // else
  1612. // {
  1613. //
  1614. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Not Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1615. //
  1616. // 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];
  1617. // alert.tag = ALERT_RESTORE_FREE;
  1618. // // alert.
  1619. // [alert show];
  1620. // }];
  1621. // }
  1622. //
  1623. //
  1624. //
  1625. // freeRowAction.backgroundColor = [UIColor lightGrayColor];
  1626. UITableViewRowAction *noteRowAction = nil;
  1627. // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1628. noteRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Edit note" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  1629. DebugLog(@"item note click");
  1630. UIViewController* parentvc=self;
  1631. ItemNotesViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"ItemNotesViewController"];
  1632. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1633. vc.onSaveNote=^(NSString* notes){
  1634. [item_json setValue:notes forKey:@"note"];
  1635. self.content_arr[indexPath.row ] = item_json;
  1636. tableView.editing = false;
  1637. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1638. };
  1639. vc.notes=[item_json valueForKey:@"note"];
  1640. vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1641. // vc.delegate = parentvc.self;
  1642. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1643. // vc.onSetValue = ^(){
  1644. // [self reload_data];//[main_vc checklogin:true];
  1645. // };
  1646. //
  1647. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1648. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1649. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  1650. [parentvc presentViewController:navi animated:YES completion:^{
  1651. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1652. DebugLog(@"EditModelPriceViewController present.........");
  1653. // self.btop = false;
  1654. // <#code#>
  1655. }];
  1656. // [self.itemListTable setEditing:false animated:YES];
  1657. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1658. }];
  1659. noteRowAction.backgroundColor = [UIColor lightGrayColor];
  1660. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  1661. {
  1662. NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]
  1663. NSString* product_id = [item_json valueForKey:@"product_id"];
  1664. int is_rate = [item_json[@"is_rate"] intValue];
  1665. NSMutableArray* arr=[@[] mutableCopy];
  1666. if(product_id.length>0)
  1667. [arr addObject:noteRowAction];
  1668. [arr addObject:deleteRowAction];
  1669. if(product_id.length>0&& is_rate==0)
  1670. [arr addObject:addWatchAction];
  1671. if(appDelegate.can_set_cart_price && product_id.length>0&& is_rate==0)
  1672. {
  1673. [arr addObject:editRowAction];
  1674. // [arr addObject:freeRowAction];
  1675. }
  1676. return arr;
  1677. // return @[deleteRowAction,addWatchAction, editRowAction,freeRowAction];
  1678. } else if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  1679. return @[deleteRowAction,addWatchAction,editRowAction];
  1680. }
  1681. else
  1682. return @[deleteRowAction,addWatchAction];
  1683. }
  1684. - (void)tableView:(UITableView *)tableView
  1685. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  1686. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1687. //
  1688. // NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1689. //
  1690. //
  1691. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1692. //
  1693. // NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  1694. //
  1695. // dispatch_async(dispatch_get_main_queue(), ^{
  1696. //
  1697. //
  1698. //
  1699. // if(cart_json!=nil)
  1700. // {
  1701. //
  1702. // [self reload_data];
  1703. // }
  1704. //
  1705. // });
  1706. // });
  1707. // sqlite3* db = [ApexMobileDB get_db];
  1708. // NSString* sql = [NSString stringWithFormat:@"delete from history where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
  1709. // [ApexMobileDB execSql:sql db:db];
  1710. // [iSalesDB close_db:db];
  1711. //
  1712. // [self.data.pagedata removeObjectAtIndex:indexPath.row];
  1713. //
  1714. // [tableView reloadData];
  1715. }
  1716. #pragma mark - UIAlertViewDelegate
  1717. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  1718. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  1719. {
  1720. if(alertView.tag==ALERT_DEL)
  1721. {
  1722. if(buttonIndex!=alertView.cancelButtonIndex)
  1723. {
  1724. // [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1725. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1726. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1727. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1728. NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  1729. dispatch_async(dispatch_get_main_queue(), ^{
  1730. if([[cart_json valueForKey:@"result"] intValue]==2)
  1731. {
  1732. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1733. for(int i=0;i<self.content_arr.count;i++)
  1734. {
  1735. NSDictionary* obj_json = self.content_arr[i];
  1736. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1737. if([_id isEqualToString:ids])
  1738. {
  1739. [self.content_arr removeObjectAtIndex:i];
  1740. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1741. break;
  1742. // appDelegate.cart_count--;
  1743. // [appDelegate update_count_mark];
  1744. }
  1745. }
  1746. [self.itemListTable reloadData];
  1747. [self refresh_total];
  1748. // [self reload_data];
  1749. }
  1750. else
  1751. {
  1752. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  1753. }
  1754. });
  1755. });
  1756. }
  1757. }
  1758. else if(alertView.tag==ALERT_FREE)
  1759. {
  1760. if(buttonIndex==alertView.cancelButtonIndex)
  1761. {
  1762. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1763. // NSString * string = [checked componentsJoinedByString:@","];
  1764. // [params setValue:string forKey:@"cart2Checkbox"];
  1765. CreateOrderViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CreateOrderViewController"];
  1766. orderinfoVC.disable_dropdown_refresh=true;
  1767. orderinfoVC.url_type = URL_REMOTE;
  1768. orderinfoVC.request_url=URL_CARTDELIVERY;
  1769. orderinfoVC.params = params;
  1770. orderinfoVC.delegate=self;
  1771. [self.navigationController pushViewController:orderinfoVC animated:true];
  1772. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
  1773. //
  1774. //
  1775. //
  1776. //
  1777. //
  1778. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  1779. // [self presentViewController:navi animated:YES completion:^{
  1780. //
  1781. // DebugLog(@"CreateOrderViewController present.........");
  1782. //
  1783. // // self.btop = false;
  1784. // // <#code#>
  1785. // }];
  1786. }
  1787. }
  1788. else if(alertView.tag==ALERT_SET_FREE)
  1789. {
  1790. if(buttonIndex!=alertView.cancelButtonIndex)
  1791. {
  1792. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1793. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1794. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1795. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1796. NSDictionary* cart_json = [iSalesNetwork cart_setFree:ids isfree:true];
  1797. dispatch_async(dispatch_get_main_queue(), ^{
  1798. if([[cart_json valueForKey:@"result"] intValue]==2)
  1799. {
  1800. [self reload_data];
  1801. }
  1802. else
  1803. {
  1804. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Set Free Model" controller:self] ;
  1805. }
  1806. });
  1807. });
  1808. }
  1809. }
  1810. else if(alertView.tag==ALERT_RESTORE_FREE)
  1811. {
  1812. if(buttonIndex!=alertView.cancelButtonIndex)
  1813. {
  1814. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1815. NSMutableDictionary * item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  1816. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1817. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1818. NSDictionary* cart_json = [iSalesNetwork cart_setFree:ids isfree:false];
  1819. dispatch_async(dispatch_get_main_queue(), ^{
  1820. if([[cart_json valueForKey:@"result"] intValue]==2)
  1821. {
  1822. [self reload_data];
  1823. }
  1824. else
  1825. {
  1826. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Restore Model Price" controller:self] ;
  1827. }
  1828. });
  1829. });
  1830. }
  1831. }
  1832. }
  1833. #pragma mark - Support scanner
  1834. -(void) onDecodedData:(NSString*) value
  1835. {
  1836. int cqty=0;
  1837. for(int i=0;i<self.content_arr.count;i++)
  1838. {
  1839. NSDictionary* item= self.content_arr[i];
  1840. if([item[@"model"] isEqualToString:value])
  1841. cqty=[item[@"count"]intValue];
  1842. }
  1843. if(cqty>0)
  1844. {
  1845. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"QTY: %d of this model already in cart. Continue ?",cqty] message:nil preferredStyle:UIAlertControllerStyleAlert];
  1846. //block代码块取代了delegate
  1847. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1848. [self addtocart:value];
  1849. }];
  1850. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1851. DebugLog(@"No");
  1852. }];
  1853. [alertControl addAction:actionOne];
  1854. [alertControl addAction:alertthree];
  1855. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1856. [self presentViewController:alertControl animated:YES completion:nil];
  1857. }
  1858. else
  1859. [self addtocart:value];
  1860. }
  1861. //-(void) neworder
  1862. //{
  1863. //
  1864. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  1865. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1866. //
  1867. // NSDictionary* return_json = [iSalesNetwork new_Order];
  1868. //
  1869. // dispatch_async(dispatch_get_main_queue(), ^{
  1870. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1871. //
  1872. //
  1873. // if([[return_json valueForKey:@"result"] intValue]==2)
  1874. // {
  1875. // int result=[[return_json valueForKey:@"result"] intValue];
  1876. // if(result==2)
  1877. // {
  1878. // //successed.
  1879. //
  1880. // NSString* order_code = [return_json valueForKey:@"orderCode"];
  1881. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1882. // appDelegate.order_code = order_code;
  1883. //
  1884. //
  1885. // [self addtocart];
  1886. //
  1887. //
  1888. // // [self ReloadData];
  1889. //
  1890. //
  1891. // // if(self.shopCartBlock!=nil)
  1892. // // {
  1893. // // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  1894. // //
  1895. // //
  1896. // // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  1897. // // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  1898. // // iv.image = img;
  1899. // // // [self.contentView addSubview:iv];
  1900. // // self.shopCartBlock(iv);
  1901. // //
  1902. // // }
  1903. //
  1904. // }
  1905. // }
  1906. // else
  1907. // {
  1908. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  1909. // }
  1910. //
  1911. //
  1912. //
  1913. //
  1914. // });
  1915. // });
  1916. //}
  1917. -(void) addtocart:(NSString*) modelname
  1918. {
  1919. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1920. NSDictionary* return_json = [iSalesNetwork add_toCart_byName: modelname];
  1921. dispatch_async(dispatch_get_main_queue(), ^{
  1922. if([[return_json valueForKey:@"result"] intValue]==2)
  1923. {
  1924. NSDictionary* newitem = [return_json objectForKey:@"item_0"];
  1925. NSString* item_id = [newitem valueForKey:@"cart_item_id"];
  1926. // bool isnew=false;
  1927. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1928. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1929. for(int i=0;i<self.content_arr.count;i++)
  1930. {
  1931. NSDictionary* obj_json = self.content_arr[i];
  1932. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  1933. if([_id isEqualToString:item_id])
  1934. {
  1935. // self.content_arr[i] = newitem;
  1936. [self.content_arr removeObjectAtIndex:i];
  1937. break;
  1938. // [self.content_arr removeObjectAtIndex:i];
  1939. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  1940. // appDelegate.cart_count--;
  1941. // [appDelegate update_count_mark];
  1942. }
  1943. }
  1944. [self.content_arr insertObject:newitem atIndex:0];
  1945. [self.itemListTable reloadData];
  1946. [self refresh_total];
  1947. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1948. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1949. }
  1950. else
  1951. {
  1952. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  1953. }
  1954. });
  1955. });
  1956. }
  1957. #pragma mark - sort button
  1958. - (void)cartSortButtonClicked:(UIButton *)sender {
  1959. DebugLog(@"cart sort button clicked");
  1960. [self.view addSubview:self.sortItemController.view];
  1961. }
  1962. - (SortItemViewController *)sortItemController {
  1963. if (!_sortItemController) {
  1964. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(20, 50)];
  1965. _sortItemController.sortIndex = self.sortIndex;
  1966. __weak typeof(self) weakSelf = self;
  1967. _sortItemController.sortBlock = ^(int sort){
  1968. weakSelf.sortIndex = sort;
  1969. [weakSelf reload_data];
  1970. };
  1971. }
  1972. // _sortItemController.sortIndex = self.sortIndex;
  1973. return _sortItemController;
  1974. }
  1975. - (SortButton *)sortButton {
  1976. if (!_sortButton) {
  1977. _sortButton = [SortButton sortButtonWithHeight:40];
  1978. _sortButton.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  1979. [_sortButton addTarget:self action:@selector(cartSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  1980. }
  1981. return _sortButton;
  1982. }
  1983. - (void)setSortIndex:(int)sortIndex {
  1984. _sortIndex = sortIndex;
  1985. NSString *selectedImageName = @"";
  1986. switch (sortIndex) {
  1987. case 0:{
  1988. selectedImageName = @"TX_22";
  1989. }
  1990. break;
  1991. case 1:{
  1992. selectedImageName = @"TS_22";
  1993. }
  1994. break;
  1995. case 2:{
  1996. selectedImageName = @"IX_22";
  1997. }
  1998. break;
  1999. case 3:{
  2000. selectedImageName = @"IS_22";
  2001. }
  2002. break;
  2003. case 4:{
  2004. selectedImageName = @"DX_22";
  2005. }
  2006. break;
  2007. default:
  2008. break;
  2009. }
  2010. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  2011. }
  2012. #pragma mark - rotation
  2013. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  2014. self.sortItemController.view.frame = self.view.bounds;
  2015. }
  2016. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  2017. return YES;
  2018. }
  2019. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  2020. self.sortItemController.view.frame = self.view.bounds;
  2021. }
  2022. #pragma mark - notification action
  2023. - (void)priceTypeChanged:(id)notification {
  2024. [self reload_data];
  2025. }
  2026. #pragma mark - button action
  2027. - (IBAction)emailButtonClick:(UIButton *)sender {
  2028. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2029. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter email address"];
  2030. emailAddrVC.textFiled.secureTextEntry = NO;
  2031. emailAddrVC.yesButtonTitle = @"send";
  2032. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  2033. __weak typeof(self) weakself = self;
  2034. emailAddrVC.textHandler = ^(NSString *text){
  2035. // 验证邮箱格式是否正确
  2036. NSString *match = @"^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$";
  2037. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  2038. BOOL isEmailAddr = [predicate evaluateWithObject:text];
  2039. if (isEmailAddr) {
  2040. // 验证是邮件地址,发送邮件
  2041. [weakVC dismissViewControllerAnimated:YES completion:^{
  2042. UIAlertView *waitting_alert = [RAUtils waiting_alert:@"Sending Email" title:@"Waitting"];
  2043. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  2044. NSDictionary *result = [iSalesNetwork quoteOrder:appDelegate.order_code emailAddr:text];
  2045. DebugLog(@"email cart result: %@",result);
  2046. dispatch_async(dispatch_get_main_queue(), ^{
  2047. [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  2048. if ([[result objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  2049. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",text] preferredStyle:UIAlertControllerStyleAlert];
  2050. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  2051. }];
  2052. [errorAlertVC addAction:action];
  2053. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  2054. }else {
  2055. [RAUtils message_alert:@"Send Email Success" title:@"Message" controller:weakself];
  2056. }
  2057. });
  2058. });
  2059. }];
  2060. } else {
  2061. // 非邮件地址,警告
  2062. [weakVC warning:@"Please enter right email address"];
  2063. }
  2064. };
  2065. [self presentViewController:emailAddrVC animated:YES completion:nil];
  2066. }
  2067. - (IBAction)printCartButtonClick:(UIButton *)sender {
  2068. PDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PDFViewController"];
  2069. ViewController.url = self.print_url;
  2070. ViewController.canSave = false;
  2071. ViewController.isLocalfile=NO;
  2072. NSString* subject;
  2073. NSString* cur_time =[RAUtils current_date];
  2074. subject =@"Cart Print";
  2075. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2076. NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  2077. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  2078. if(customer_email.length>0)
  2079. {
  2080. send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  2081. }
  2082. ViewController.mail_to = send_to;
  2083. ViewController.attachment_name = [NSString stringWithFormat:@"NPD_Product_List %@.pdf",cur_time];
  2084. ViewController.mail_subject = subject;
  2085. ViewController.hidenavi = false;
  2086. [self.navigationController pushViewController:ViewController animated:YES];
  2087. }
  2088. @end