CartViewController.m 145 KB

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