CartViewController.m 110 KB

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