RAUtils.m 52 KB

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