CartViewController.m 133 KB

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