CartViewController.m 145 KB

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