CartViewController.m 127 KB

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