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