CartViewController.m 110 KB

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