CartViewController.m 134 KB

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