CartUtils.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. //
  2. // CartUtils.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 12/26/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CartUtils.h"
  9. #import "RAUtils.h"
  10. #import "AppDelegate.h"
  11. #import "iSalesNetwork.h"
  12. #import "LoginViewController.h"
  13. #import "MainViewController.h"
  14. @implementation CartUtils
  15. +(void) neworder:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart
  16. {
  17. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  18. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  19. NSDictionary* return_json = [iSalesNetwork new_Order];
  20. dispatch_async(dispatch_get_main_queue(), ^{
  21. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  22. if([[return_json valueForKey:@"result"] intValue]==2)
  23. {
  24. int result=[[return_json valueForKey:@"result"] intValue];
  25. if(result==2)
  26. {
  27. //successed.
  28. NSString* order_code = [return_json valueForKey:@"orderCode"];
  29. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  30. appDelegate.order_code = order_code;
  31. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  32. [holder performSelector:addtocart];
  33. }
  34. }
  35. else
  36. {
  37. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:vc] ;
  38. }
  39. });
  40. });
  41. }
  42. +(void) add_to_cart:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart
  43. {
  44. UIApplication * app = [UIApplication sharedApplication];
  45. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  46. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  47. if(appDelegate.bLogin==false)
  48. {
  49. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  50. loginvc.returnValue = ^(bool blogin){
  51. if(blogin)
  52. {
  53. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  54. {
  55. [main_vc checklogin:YES];
  56. NSString* msg =@"";
  57. // if(appDelegate.contact_id.length>0)
  58. // {
  59. // msg = [msg stringByAppendingString:@"Customer:"];
  60. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  61. //
  62. // }
  63. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  64. //block代码块取代了delegate
  65. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  66. // vc.disable_refresh = true;
  67. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  68. ovc.showNavibar = true;
  69. //ovc.customer_id = appDelegate.contact_id;
  70. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  71. [holder performSelector:addtocart];
  72. };
  73. ovc.init_style = OL_OPEN;
  74. ovc.onCancel = ^(){
  75. // self.disable_refresh = false;
  76. };
  77. [vc.navigationController pushViewController:ovc animated:true];
  78. }];
  79. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  80. {
  81. //create new;
  82. // self.disable_refresh = true;
  83. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  84. {
  85. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  86. cvc.showNavibar = true;
  87. cvc.contact_type = @"Sales_Order_Customer";
  88. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  89. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  90. appDelegate.customerInfo = value;
  91. if(appDelegate.order_code==nil)
  92. [self neworder:vc selectorholder:holder selector:addtocart];
  93. // neworder();
  94. // [main_vc checklogin:true];
  95. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  96. //
  97. // if(self.returnValue)
  98. // self.returnValue(value);
  99. };
  100. cvc.onCancel = ^(){
  101. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  102. // self.disable_refresh = false;
  103. };
  104. cvc.onReset = ^(){
  105. // [main_vc checklogin:true];
  106. };
  107. [vc.navigationController pushViewController:cvc animated:true];
  108. }
  109. else
  110. {
  111. [self neworder:vc selectorholder:holder selector:addtocart];
  112. }
  113. }
  114. DebugLog(@"No");
  115. }];
  116. [alertControl addAction:actionOne];
  117. [alertControl addAction:alertthree];
  118. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  119. }];
  120. [alertControl addAction:alertcancel];
  121. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  122. [vc presentViewController:alertControl animated:YES completion:nil];
  123. }
  124. else
  125. {
  126. //customer login;
  127. [main_vc checklogin:false];
  128. //[self addtocart];
  129. [holder performSelector:addtocart];
  130. }
  131. }
  132. };
  133. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  134. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  135. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  136. [vc presentViewController:navi animated:YES completion:^{
  137. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  138. DebugLog(@"LoginViewController present.........");
  139. // self.btop = false;
  140. // <#code#>
  141. }];
  142. }
  143. else
  144. {
  145. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  146. {
  147. if(appDelegate.contact_id.length==0)
  148. {
  149. NSString* msg =@"";
  150. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  151. //block代码块取代了delegate
  152. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  153. // vc.disable_refresh = true;
  154. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  155. ovc.showNavibar = true;
  156. //ovc.customer_id = appDelegate.contact_id;
  157. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  158. [holder performSelector:addtocart];
  159. };
  160. ovc.init_style = OL_OPEN;
  161. ovc.onCancel = ^(){
  162. // self.disable_refresh = false;
  163. };
  164. [vc.navigationController pushViewController:ovc animated:true];
  165. }];
  166. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  167. {
  168. //create new;
  169. // self.disable_refresh = true;
  170. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  171. {
  172. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  173. cvc.showNavibar = true;
  174. cvc.contact_type = @"Sales_Order_Customer";
  175. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  176. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  177. appDelegate.customerInfo = value;
  178. if(appDelegate.order_code==nil)
  179. [self neworder:vc selectorholder:holder selector:addtocart];
  180. // neworder();
  181. // [main_vc checklogin:true];
  182. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  183. //
  184. // if(self.returnValue)
  185. // self.returnValue(value);
  186. };
  187. cvc.onCancel = ^(){
  188. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  189. // self.disable_refresh = false;
  190. };
  191. cvc.onReset = ^(){
  192. // [main_vc checklogin:true];
  193. };
  194. [vc.navigationController pushViewController:cvc animated:true];
  195. }
  196. else
  197. {
  198. [self neworder:vc selectorholder:holder selector:addtocart];
  199. }
  200. }
  201. DebugLog(@"No");
  202. }];
  203. [alertControl addAction:actionOne];
  204. [alertControl addAction:alertthree];
  205. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  206. }];
  207. [alertControl addAction:alertcancel];
  208. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  209. [vc presentViewController:alertControl animated:YES completion:nil];
  210. }
  211. else
  212. {
  213. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking Pending Order"];
  214. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  215. NSDictionary* return_json = [iSalesNetwork request_PendingOrder:appDelegate.contact_id];
  216. dispatch_async(dispatch_get_main_queue(), ^{
  217. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  218. if([[return_json valueForKey:@"result"] intValue]==2)
  219. {
  220. bool openPendingOrder= [[return_json valueForKey:@"hasPending"] boolValue];
  221. bool createNewOrder=appDelegate.can_create_order;
  222. if(openPendingOrder&&createNewOrder)
  223. {
  224. NSString* msg =@"";
  225. if(appDelegate.contact_id.length>0)
  226. {
  227. msg = [msg stringByAppendingString:@"Customer:"];
  228. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  229. }
  230. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  231. //block代码块取代了delegate
  232. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  233. // vc.disable_refresh = true;
  234. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  235. ovc.showNavibar = true;
  236. ovc.customer_id = appDelegate.contact_id;
  237. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  238. [holder performSelector:addtocart];
  239. };
  240. ovc.init_style = OL_OPEN;
  241. ovc.onCancel = ^(){
  242. // self.disable_refresh = false;
  243. };
  244. [vc.navigationController pushViewController:ovc animated:true];
  245. }];
  246. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  247. {
  248. //create new;
  249. // self.disable_refresh = true;
  250. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  251. {
  252. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  253. cvc.showNavibar = true;
  254. cvc.contact_type = @"Sales_Order_Customer";
  255. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  256. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  257. appDelegate.customerInfo = value;
  258. if(appDelegate.order_code==nil)
  259. [self neworder:vc selectorholder:holder selector:addtocart];
  260. // neworder();
  261. // [main_vc checklogin:true];
  262. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  263. //
  264. // if(self.returnValue)
  265. // self.returnValue(value);
  266. };
  267. cvc.onCancel = ^(){
  268. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  269. // self.disable_refresh = false;
  270. };
  271. cvc.onReset = ^(){
  272. // [main_vc checklogin:true];
  273. };
  274. [vc.navigationController pushViewController:cvc animated:true];
  275. }
  276. else
  277. {
  278. [self neworder:vc selectorholder:holder selector:addtocart];
  279. }
  280. }
  281. DebugLog(@"No");
  282. }];
  283. [alertControl addAction:actionOne];
  284. [alertControl addAction:alertthree];
  285. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  286. }];
  287. [alertControl addAction:alertcancel];
  288. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  289. [vc presentViewController:alertControl animated:YES completion:nil];
  290. }
  291. else
  292. {
  293. if(openPendingOrder)
  294. {
  295. NSString* msg =@"";
  296. if(appDelegate.contact_id.length>0)
  297. {
  298. msg = [msg stringByAppendingString:@"Customer:"];
  299. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  300. }
  301. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  302. //block代码块取代了delegate
  303. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  304. // vc.disable_refresh = true;
  305. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  306. ovc.showNavibar = true;
  307. ovc.customer_id = appDelegate.contact_id;
  308. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  309. [holder performSelector:addtocart];
  310. };
  311. ovc.init_style = OL_OPEN;
  312. ovc.onCancel = ^(){
  313. // self.disable_refresh = false;
  314. };
  315. [vc.navigationController pushViewController:ovc animated:true];
  316. }];
  317. [alertControl addAction:actionOne];
  318. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  319. }];
  320. [alertControl addAction:alertcancel];
  321. // [alertControl addAction:alertthree];
  322. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  323. [vc presentViewController:alertControl animated:YES completion:nil];
  324. }
  325. else if(createNewOrder)
  326. {
  327. NSString* msg =@"";
  328. if(appDelegate.contact_id.length>0)
  329. {
  330. msg = [msg stringByAppendingString:@"Customer:"];
  331. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  332. }
  333. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  334. //block代码块取代了delegate
  335. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create 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 selectorholder:holder 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 selectorholder:holder selector:addtocart];
  368. }
  369. }
  370. DebugLog(@"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. [RAUtils message_alert:[return_json valueForKey:@"You donot have permission to access order"] title:@"Add To Cart" controller:vc];
  383. }
  384. }
  385. }
  386. else
  387. {
  388. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Check Pending Order" controller:vc] ;
  389. }
  390. });
  391. });
  392. }
  393. // // [main_vc checklogin:false];
  394. //
  395. // if(appDelegate.can_create_order)
  396. // {
  397. // NSString* msg =@"";
  398. // if(appDelegate.contact_id.length>0)
  399. // {
  400. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  401. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  402. //
  403. // }
  404. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil),NSLocalizedString(@"Create new order", nil), nil];
  405. //
  406. // // alert.
  407. // [alert show];
  408. // }
  409. // else
  410. // {
  411. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil), nil];
  412. //
  413. // // alert.
  414. // [alert show];
  415. // }
  416. }
  417. else
  418. {
  419. // if(appDelegate.order_code==nil)
  420. // [ self neworder];
  421. // else
  422. [holder performSelector:addtocart];
  423. }
  424. }
  425. }
  426. +(void) add_recent_model:(NSDictionary*) model
  427. {
  428. NSMutableDictionary * newdict = [[NSMutableDictionary alloc]init];
  429. [newdict setObject:model forKey:@"item_0"];
  430. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  431. int count=[appDelegate.recent_model[@"count"] intValue];
  432. for(int i=0;i<count;i++)
  433. {
  434. NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
  435. NSString *mitemProductID = [NSString stringWithFormat:@"%@",mitem[@"product_id"]];
  436. NSString *modelProductID = [NSString stringWithFormat:@"%@",model[@"product_id"]];
  437. if([mitemProductID isEqualToString:modelProductID])
  438. continue;
  439. [newdict setObject:mitem forKey:[NSString stringWithFormat:@"item_%lu",(unsigned long)newdict.allKeys.count]];
  440. }
  441. newdict[@"count"]=[NSString stringWithFormat:@"%lu",(unsigned long)newdict.allKeys.count];
  442. appDelegate.recent_model = newdict;
  443. //
  444. //
  445. // if(!bexist)
  446. // {
  447. // [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
  448. // appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
  449. // }
  450. }
  451. @end