CartViewController.m 110 KB

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