RAUtils.m 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. //
  2. // RAUtils.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 9/17/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAUtils.h"
  9. #import "LoginViewController.h"
  10. #import "MainViewController.h"
  11. @implementation RAUtils
  12. + (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
  13. // 创建一个bitmap的context
  14. // 并把它设置成为当前正在使用的context
  15. UIGraphicsBeginImageContext(size);
  16. // 绘制改变大小的图片
  17. [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  18. // 从当前context中创建一个改变大小后的图片
  19. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  20. // 使当前的context出堆栈
  21. UIGraphicsEndImageContext();
  22. // 返回新的改变大小后的图片
  23. // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  24. return scaledImage;
  25. }
  26. +(UIImage*)img_compress:(UIImage*)image kbsize:(float) size
  27. {
  28. //UIImage *image=[UIImage imageNamed:@"xxoo.jpeg"];
  29. NSData *imageData=UIImageJPEGRepresentation(image, 1.f);
  30. if(size>imageData.length/1024)
  31. return image;
  32. // CGFloat size=40.f;// kb
  33. CGFloat scale=size/(imageData.length/1024);
  34. scale = sqrt (scale);
  35. CGSize newsize=image.size;
  36. newsize.height = newsize.height*scale;
  37. newsize.width = newsize.width*scale;
  38. return [RAUtils scaleToSize:image size:newsize];
  39. // NSData *newData=UIImageJPEGRepresentation(image, scale);
  40. // UIImage* ret= [[UIImage alloc] initWithData:newData];
  41. //
  42. // return ret;
  43. }
  44. +(NSString*) FloatFormat:(float)value
  45. {
  46. if (fmodf(value, 1)==0)
  47. {
  48. return [NSString stringWithFormat:@"%.0f",value];
  49. } else if (fmodf(value*10, 1)==0)
  50. {
  51. return [NSString stringWithFormat:@"%.1f",value];
  52. }
  53. else if (fmodf(value*100, 1)==0)
  54. {
  55. return [NSString stringWithFormat:@"%.2f",value];
  56. }
  57. else if (fmodf(value*1000, 1)==0)
  58. {
  59. return [NSString stringWithFormat:@"%.3f",value];
  60. }
  61. else
  62. {
  63. return [NSString stringWithFormat:@"%.4f",value];
  64. }
  65. return nil;
  66. }
  67. +(UIViewController*) getViewController:(UIView*) view
  68. {
  69. for (UIView* next = [view superview]; next; next = next.superview) {
  70. UIResponder* nextResponder = [next nextResponder];
  71. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  72. return (UIViewController*)nextResponder;
  73. }
  74. }
  75. return nil;
  76. }
  77. + (float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小
  78. {
  79. NSFileManager *fileManager = [[NSFileManager alloc] init];
  80. float size =0;
  81. NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
  82. for(int i = 0; i<[array count]; i++)
  83. {
  84. NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
  85. BOOL isDir;
  86. if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
  87. {
  88. NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
  89. size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
  90. }
  91. else
  92. {
  93. size+=[self fileSizeForDir:fullPath];
  94. }
  95. }
  96. return size;
  97. }
  98. +(NSDictionary*) error_dict:(NSError*)error
  99. {
  100. if(error==nil)
  101. return nil;
  102. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  103. [ret setValue:[NSString stringWithFormat:@"%d",error.code] forKey:@"error_code"];
  104. [ret setValue:error.domain forKey:@"err_domain"];
  105. [ret setValue:[error localizedDescription] forKey:@"err_message"];
  106. // [ret setObject:error.userInfo forKey:@"user_info"];
  107. return ret;
  108. }
  109. +(NSString*) current_date
  110. {
  111. NSDate * date = [NSDate date];
  112. NSTimeInterval sec = [date timeIntervalSinceNow];
  113. NSDate * currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
  114. NSDateFormatter * df = [[NSDateFormatter alloc] init ];
  115. [df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  116. NSString * na = [df stringFromDate:currentDate];
  117. return na;
  118. }
  119. +(void) message_alert :(NSString*) msg title:(NSString*) title controller:(UIViewController*) vc
  120. {
  121. if(title==nil)
  122. title = @"Error.";
  123. return [self alert_view:msg title:title];
  124. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  125. //block代码块取代了delegate
  126. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  127. // textField.text = self.save_name;
  128. //
  129. //
  130. // }];
  131. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  132. //
  133. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"];
  134. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  135. //
  136. // NSDictionary* return_json = [iSalesNetwork delete_Order:orderid];
  137. //
  138. // dispatch_async(dispatch_get_main_queue(), ^{
  139. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  140. //
  141. //
  142. // if([[return_json valueForKey:@"result"] intValue]==2)
  143. // {
  144. //
  145. // [RAUtils error_alert:nil title:@"Order Delete"] ;
  146. // }
  147. // else
  148. // {
  149. // [RAUtils error_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order Failed."] ;
  150. // }
  151. //
  152. //
  153. //
  154. //
  155. // });
  156. // });
  157. //
  158. //
  159. // }];
  160. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  161. //NSLog(@"Cancel");
  162. }];
  163. // [alertControl addAction:actionOne];
  164. [alertControl addAction:alertthree];
  165. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  166. }];
  167. [alertControl addAction:alertcancel];
  168. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  169. [vc presentViewController:alertControl animated:YES completion:nil];
  170. return;
  171. }
  172. +(void) neworder:(UIViewController*) vc selector:(SEL)addtocart
  173. {
  174. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  175. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  176. NSDictionary* return_json = [iSalesNetwork new_Order];
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  179. if([[return_json valueForKey:@"result"] intValue]==2)
  180. {
  181. int result=[[return_json valueForKey:@"result"] intValue];
  182. if(result==2)
  183. {
  184. //successed.
  185. NSString* order_code = [return_json valueForKey:@"orderCode"];
  186. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  187. appDelegate.order_code = order_code;
  188. [vc performSelector:addtocart];
  189. // [self ReloadData];
  190. // if(self.shopCartBlock!=nil)
  191. // {
  192. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  193. //
  194. //
  195. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  196. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  197. // iv.image = img;
  198. // // [self.contentView addSubview:iv];
  199. // self.shopCartBlock(iv);
  200. //
  201. // }
  202. }
  203. }
  204. else
  205. {
  206. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:vc] ;
  207. }
  208. });
  209. });
  210. }
  211. +(void) add_to_cart:(UIViewController*) vc selector:(SEL)addtocart
  212. {
  213. UIApplication * app = [UIApplication sharedApplication];
  214. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  215. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  216. if(appDelegate.bLogin==false)
  217. {
  218. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  219. loginvc.returnValue = ^(bool blogin){
  220. if(blogin)
  221. {
  222. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  223. {
  224. [main_vc checklogin:false];
  225. NSString* msg =@"";
  226. // if(appDelegate.contact_id.length>0)
  227. // {
  228. // msg = [msg stringByAppendingString:@"Customer:"];
  229. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  230. //
  231. // }
  232. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  233. //block代码块取代了delegate
  234. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Add to pending order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  235. // vc.disable_refresh = true;
  236. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  237. ovc.showNavibar = true;
  238. //ovc.customer_id = appDelegate.contact_id;
  239. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  240. [vc performSelector:addtocart];
  241. };
  242. ovc.init_style = OL_OPEN;
  243. ovc.onCancel = ^(){
  244. // self.disable_refresh = false;
  245. };
  246. [vc.navigationController pushViewController:ovc animated:true];
  247. }];
  248. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Add to new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  249. {
  250. //create new;
  251. // self.disable_refresh = true;
  252. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  253. {
  254. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  255. cvc.showNavibar = true;
  256. cvc.contact_type = @"Sales_Order_Customer";
  257. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  258. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  259. appDelegate.customerInfo = value;
  260. if(appDelegate.order_code==nil)
  261. [self neworder:vc selector:addtocart];
  262. // neworder();
  263. // [main_vc checklogin:true];
  264. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  265. //
  266. // if(self.returnValue)
  267. // self.returnValue(value);
  268. };
  269. cvc.onCancel = ^(){
  270. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  271. // self.disable_refresh = false;
  272. };
  273. cvc.onReset = ^(){
  274. // [main_vc checklogin:true];
  275. };
  276. [vc.navigationController pushViewController:cvc animated:true];
  277. }
  278. else
  279. {
  280. [self neworder:vc selector:addtocart];
  281. }
  282. }
  283. NSLog(@"No");
  284. }];
  285. [alertControl addAction:actionOne];
  286. [alertControl addAction:alertthree];
  287. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  288. }];
  289. [alertControl addAction:alertcancel];
  290. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  291. [vc presentViewController:alertControl animated:YES completion:nil];
  292. }
  293. else
  294. {
  295. //customer login;
  296. [main_vc checklogin:false];
  297. //[self addtocart];
  298. [vc performSelector:addtocart];
  299. }
  300. }
  301. };
  302. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  303. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  304. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  305. [vc presentViewController:navi animated:YES completion:^{
  306. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  307. NSLog(@"LoginViewController present.........");
  308. // self.btop = false;
  309. // <#code#>
  310. }];
  311. }
  312. else
  313. {
  314. if(appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  315. {
  316. if(appDelegate.contact_id.length==0)
  317. {
  318. NSString* msg =@"";
  319. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  320. //block代码块取代了delegate
  321. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Add to pending order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  322. // vc.disable_refresh = true;
  323. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  324. ovc.showNavibar = true;
  325. //ovc.customer_id = appDelegate.contact_id;
  326. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  327. [vc performSelector:addtocart];
  328. };
  329. ovc.init_style = OL_OPEN;
  330. ovc.onCancel = ^(){
  331. // self.disable_refresh = false;
  332. };
  333. [vc.navigationController pushViewController:ovc animated:true];
  334. }];
  335. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Add to new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  336. {
  337. //create new;
  338. // self.disable_refresh = true;
  339. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  340. {
  341. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  342. cvc.showNavibar = true;
  343. cvc.contact_type = @"Sales_Order_Customer";
  344. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  345. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  346. appDelegate.customerInfo = value;
  347. if(appDelegate.order_code==nil)
  348. [self neworder:vc selector:addtocart];
  349. // neworder();
  350. // [main_vc checklogin:true];
  351. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  352. //
  353. // if(self.returnValue)
  354. // self.returnValue(value);
  355. };
  356. cvc.onCancel = ^(){
  357. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  358. // self.disable_refresh = false;
  359. };
  360. cvc.onReset = ^(){
  361. // [main_vc checklogin:true];
  362. };
  363. [vc.navigationController pushViewController:cvc animated:true];
  364. }
  365. else
  366. {
  367. [self neworder:vc selector:addtocart];
  368. }
  369. }
  370. NSLog(@"No");
  371. }];
  372. [alertControl addAction:actionOne];
  373. [alertControl addAction:alertthree];
  374. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  375. }];
  376. [alertControl addAction:alertcancel];
  377. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  378. [vc presentViewController:alertControl animated:YES completion:nil];
  379. }
  380. else
  381. {
  382. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking Pending Order"];
  383. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  384. NSDictionary* return_json = [iSalesNetwork request_PendingOrder:appDelegate.contact_id];
  385. dispatch_async(dispatch_get_main_queue(), ^{
  386. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  387. if([[return_json valueForKey:@"result"] intValue]==2)
  388. {
  389. bool openPendingOrder= [[return_json valueForKey:@"hasPending"] boolValue];
  390. bool createNewOrder=appDelegate.can_create_order;
  391. if(openPendingOrder&&createNewOrder)
  392. {
  393. NSString* msg =@"";
  394. if(appDelegate.contact_id.length>0)
  395. {
  396. msg = [msg stringByAppendingString:@"Customer:"];
  397. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  398. }
  399. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  400. //block代码块取代了delegate
  401. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Add to pending order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  402. // vc.disable_refresh = true;
  403. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  404. ovc.showNavibar = true;
  405. ovc.customer_id = appDelegate.contact_id;
  406. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  407. [vc performSelector:addtocart];
  408. };
  409. ovc.init_style = OL_OPEN;
  410. ovc.onCancel = ^(){
  411. // self.disable_refresh = false;
  412. };
  413. [vc.navigationController pushViewController:ovc animated:true];
  414. }];
  415. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Add to new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  416. {
  417. //create new;
  418. // self.disable_refresh = true;
  419. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  420. {
  421. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  422. cvc.showNavibar = true;
  423. cvc.contact_type = @"Sales_Order_Customer";
  424. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  425. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  426. appDelegate.customerInfo = value;
  427. if(appDelegate.order_code==nil)
  428. [self neworder:vc selector:addtocart];
  429. // neworder();
  430. // [main_vc checklogin:true];
  431. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  432. //
  433. // if(self.returnValue)
  434. // self.returnValue(value);
  435. };
  436. cvc.onCancel = ^(){
  437. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  438. // self.disable_refresh = false;
  439. };
  440. cvc.onReset = ^(){
  441. // [main_vc checklogin:true];
  442. };
  443. [vc.navigationController pushViewController:cvc animated:true];
  444. }
  445. else
  446. {
  447. [self neworder:vc selector:addtocart];
  448. }
  449. }
  450. NSLog(@"No");
  451. }];
  452. [alertControl addAction:actionOne];
  453. [alertControl addAction:alertthree];
  454. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  455. }];
  456. [alertControl addAction:alertcancel];
  457. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  458. [vc presentViewController:alertControl animated:YES completion:nil];
  459. }
  460. else
  461. {
  462. if(openPendingOrder)
  463. {
  464. NSString* msg =@"";
  465. if(appDelegate.contact_id.length>0)
  466. {
  467. msg = [msg stringByAppendingString:@"Customer:"];
  468. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  469. }
  470. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  471. //block代码块取代了delegate
  472. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Add to pending order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  473. // vc.disable_refresh = true;
  474. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  475. ovc.showNavibar = true;
  476. ovc.customer_id = appDelegate.contact_id;
  477. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  478. [vc performSelector:addtocart];
  479. };
  480. ovc.init_style = OL_OPEN;
  481. ovc.onCancel = ^(){
  482. // self.disable_refresh = false;
  483. };
  484. [vc.navigationController pushViewController:ovc animated:true];
  485. }];
  486. [alertControl addAction:actionOne];
  487. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  488. }];
  489. [alertControl addAction:alertcancel];
  490. // [alertControl addAction:alertthree];
  491. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  492. [vc presentViewController:alertControl animated:YES completion:nil];
  493. }
  494. else if(createNewOrder)
  495. {
  496. NSString* msg =@"";
  497. if(appDelegate.contact_id.length>0)
  498. {
  499. msg = [msg stringByAppendingString:@"Customer:"];
  500. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  501. }
  502. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  503. //block代码块取代了delegate
  504. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Add to new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  505. {
  506. //create new;
  507. // self.disable_refresh = true;
  508. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  509. {
  510. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  511. cvc.showNavibar = true;
  512. cvc.contact_type = @"Sales_Order_Customer";
  513. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  514. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  515. appDelegate.customerInfo = value;
  516. if(appDelegate.order_code==nil)
  517. [self neworder:vc selector:addtocart];
  518. // neworder();
  519. // [main_vc checklogin:true];
  520. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  521. //
  522. // if(self.returnValue)
  523. // self.returnValue(value);
  524. };
  525. cvc.onCancel = ^(){
  526. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  527. // self.disable_refresh = false;
  528. };
  529. cvc.onReset = ^(){
  530. // [main_vc checklogin:true];
  531. };
  532. [vc.navigationController pushViewController:cvc animated:true];
  533. }
  534. else
  535. {
  536. [self neworder:vc selector:addtocart];
  537. }
  538. }
  539. NSLog(@"No");
  540. }];
  541. // [alertControl addAction:actionOne];
  542. [alertControl addAction:alertthree];
  543. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  544. }];
  545. [alertControl addAction:alertcancel];
  546. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  547. [vc presentViewController:alertControl animated:YES completion:nil];
  548. }
  549. else
  550. {
  551. [RAUtils message_alert:[return_json valueForKey:@"You donot have permission to access order"] title:@"Add To Cart" controller:vc];
  552. }
  553. }
  554. }
  555. else
  556. {
  557. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Check Pending Order" controller:vc] ;
  558. }
  559. });
  560. });
  561. }
  562. // // [main_vc checklogin:false];
  563. //
  564. // if(appDelegate.can_create_order)
  565. // {
  566. // NSString* msg =@"";
  567. // if(appDelegate.contact_id.length>0)
  568. // {
  569. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  570. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  571. //
  572. // }
  573. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  574. //
  575. // // alert.
  576. // [alert show];
  577. // }
  578. // else
  579. // {
  580. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  581. //
  582. // // alert.
  583. // [alert show];
  584. // }
  585. }
  586. else
  587. {
  588. // if(appDelegate.order_code==nil)
  589. // [ self neworder];
  590. // else
  591. [vc performSelector:addtocart];
  592. }
  593. }
  594. }
  595. +(void) alert_view :(NSString*) msg title:(NSString*) title
  596. {
  597. if(title==nil)
  598. title = @"Error.";
  599. if(msg.length>0)
  600. {
  601. title=[NSString stringWithFormat:@"%@\n\n%@",title,msg];
  602. }
  603. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  604. [alert show];
  605. }
  606. +(UIAlertView * ) waiting_alert :(NSString*) msg title:(NSString*) title
  607. {
  608. if(title==nil)
  609. title = @"Please Wait";
  610. if(msg==nil)
  611. msg= @"Waiting...";
  612. NSAssert(msg!=nil, @"error message from json is nil");
  613. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  614. [alert show];
  615. //
  616. //
  617. // UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125.0, 80.0, 30.0, 30.0)];
  618. // aiView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  619. // // check if os version is 7 or above. ios7.0及以上UIAlertView弃用了addSubview方法
  620. //// if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending) {
  621. //// [alert setValue:aiView forKey:@"accessoryView"];
  622. //// }else{
  623. //// [alert addSubview:aiView];
  624. //// }
  625. //
  626. // aiView.hidden = false;
  627. // aiView.hidesWhenStopped = false;
  628. // [aiView startAnimating];
  629. //
  630. //[alert addSubview:aiView];
  631. return alert;
  632. //return nil;
  633. // return alert;
  634. // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  635. //[alert show];
  636. }
  637. +(NSDictionary*) device_info
  638. {
  639. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  640. NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
  641. NSString* version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  642. NSString* versionNum = [NSString stringWithFormat:@"Version: npd %@ Build %@",version,build];
  643. NSMutableDictionary * info = [[NSMutableDictionary alloc]init];
  644. [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  645. [info setValue:[[UIDevice currentDevice] systemVersion] forKey:@"systemVersion"];
  646. [info setValue:[[UIDevice currentDevice] model] forKey:@"model"];
  647. [info setValue:versionNum forKey:@"ver"];
  648. [info setValue:[[UIDevice currentDevice] localizedModel] forKey:@"localizedModel"];
  649. return info;
  650. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  651. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  652. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  653. }
  654. +(NSArray*) string2arr:(NSString*) string separator:(NSString*)separator
  655. {
  656. NSArray *stringArray = [string componentsSeparatedByString:separator];
  657. return stringArray;
  658. }
  659. +(NSString*) arr2string:(NSArray *) arr separator:(NSString*)separator trim:(bool) btrim
  660. {
  661. NSMutableArray * marr = [arr mutableCopy];
  662. for (NSString* item in marr) {
  663. if(item.length==0 )
  664. if( btrim)
  665. [marr removeObject:item];
  666. }
  667. NSString * ret = [marr componentsJoinedByString:separator];
  668. return ret;
  669. }
  670. +(NSString*) dict2string:(NSDictionary*) dict
  671. {
  672. if(dict==nil)
  673. return nil;
  674. // 将NSDictionary转化为NSData
  675. NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil];
  676. // 再将NSData转为字符串
  677. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  678. return jsonStr;
  679. }
  680. +(NSString*) base64en:(NSString*) string
  681. {
  682. if(string == nil)
  683. return nil;
  684. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  685. NSString *stringBase64 = [data base64EncodedStringWithOptions:0]; // base64格式的字符串
  686. return stringBase64;
  687. }
  688. +(NSString*) base64de:(NSString*) stringBase64
  689. {
  690. if(stringBase64==nil)
  691. return nil;
  692. NSData *data = [[NSData alloc] initWithBase64EncodedString:stringBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
  693. NSString *string =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  694. return string;
  695. }
  696. +(void) deletefiles :(NSString*) path
  697. {
  698. // NSString *extension = @"m4r";
  699. NSFileManager *fileManager = [NSFileManager defaultManager];
  700. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  701. // NSString *documentsDirectory = [paths objectAtIndex:0];
  702. NSArray *contents = [fileManager contentsOfDirectoryAtPath:path error:NULL];
  703. NSEnumerator *e = [contents objectEnumerator];
  704. NSString *filename;
  705. while ((filename = [e nextObject])) {
  706. bool result= [fileManager removeItemAtPath:[path stringByAppendingPathComponent:filename] error:NULL];
  707. if(!result)
  708. NSLog(@"delete file failed %@------%@",path,filename);
  709. }
  710. }
  711. +(NSMutableArray*)dictionary2array:(NSDictionary*)json count_fields:(NSString*) count_fields item_mark:(NSString*) item_mark items_mark:(NSString* )items_mark
  712. {
  713. if(json==nil)
  714. return nil;
  715. NSMutableArray* ret = [[NSMutableArray alloc] init];
  716. int count = [[json valueForKey:count_fields] intValue];
  717. NSDictionary* items = nil;
  718. if(items_mark==nil)
  719. items = json;
  720. else
  721. items = [json objectForKey:items_mark];
  722. for(int i=0;i<count;i++)
  723. {
  724. NSDictionary* obj = [items objectForKey:[NSString stringWithFormat:@"%@%d",item_mark,i]];
  725. [ret addObject:obj];
  726. }
  727. return ret;
  728. }
  729. +(NSDictionary*) error_json :(int)code err_msg:(NSString*)msg
  730. {
  731. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  732. //#define RESULT_FALSE 0
  733. //#define RESULT_TRUE 2
  734. //#define RESULT_NET_ERROR -3
  735. //#define RESULT_NET_NOTAVAILABLE -4
  736. //#define RESULT_ERROR -5
  737. //#define RESULT_LOCALFILE_ERROR -7
  738. //#define RESULT_USERAUTH_ERROR -9
  739. //#define RESULT_UPDATE_USERAUTH_ERROR -11
  740. //#define RESULT_SESSION_EXPIRED -13
  741. //#define RESULT_VER_LOW
  742. if(msg.length<=0)
  743. {
  744. switch (code) {
  745. case RESULT_NET_NOTAVAILABLE:
  746. msg= MSG_NET_NOTAVAILABLE;
  747. break;
  748. default:
  749. // assert(@"UNDEFINE ERROR CODE!");
  750. break;
  751. }
  752. }
  753. // if(code==RESULT_NET_NOTAVAILABLE)
  754. // [ret setValue:[NSString stringWithFormat:@"%d",RESULT_NET_ERROR] forKey:@"result"];
  755. // else
  756. [ret setValue:[NSString stringWithFormat:@"%d",code] forKey:@"result"];
  757. [ret setValue:msg forKey:@"err_msg"];
  758. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ret
  759. // options:0
  760. // error:nil];
  761. return ret;
  762. }
  763. + (CGRect)relativeFrameForScreenWithView:(UIView *)v
  764. {
  765. UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
  766. CGRect rect=[v convertRect: v.bounds toView:window];
  767. return rect;
  768. // BOOL iOS7 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 7;
  769. //
  770. // CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  771. // if (!iOS7) {
  772. // screenHeight -= 20;
  773. // }
  774. // UIView *view = v;
  775. // CGFloat x = .0;
  776. // CGFloat y = .0;
  777. // while (view.frame.size.width != 320 || view.frame.size.height != screenHeight) {
  778. // x += view.frame.origin.x;
  779. // y += view.frame.origin.y;
  780. // view = view.superview;
  781. // if ([view isKindOfClass:[UIScrollView class]]) {
  782. // x -= ((UIScrollView *) view).contentOffset.x;
  783. // y -= ((UIScrollView *) view).contentOffset.y;
  784. // }
  785. // }
  786. // return CGRectMake(x, y, v.frame.size.width, v.frame.size.height);
  787. }
  788. @end