CartViewController.m 124 KB

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