CartViewController.m 130 KB

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