| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- //
- // SelectUploadOrderViewController.m
- // iSales-NPD
- //
- // Created by Ray on 9/3/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "SelectUploadOrderViewController.h"
- #import "iSalesDB.h"
- #import <sqlite3.h>
- #import "SelectOrderTableViewCell.h"
- #import "DefaultTableHeaderView.h"
- #import "DefaultAppearance.h"
- #import "RANetwork.h"
- #import "RAUtils.h"
- #import "AppDelegate.h"
- #import "Singleton.h"
- @interface SelectUploadOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
- @property (nonatomic,strong) NSMutableArray<NSDictionary *> *sync_data;
- @property (strong, nonatomic) IBOutlet UITableView *syncDataTableView;
- @property (strong, nonatomic) IBOutlet UIButton *uploadButton;
- @property (strong, nonatomic) NSIndexPath *mergeTo;
- @end
- @implementation SelectUploadOrderViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( onCloseClick:)];
- closeButton.width = -10;
-
-
- UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"clear all" style:UIBarButtonItemStylePlain target:self action:@selector(onClearClick:)];
-
-
- self.navigationItem.leftBarButtonItem = closeButton;
- self.navigationItem.rightBarButtonItem = clearButton;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (NSMutableArray<NSDictionary *> *)sync_data {
- if (!_sync_data) {
- _sync_data = [NSMutableArray array];
-
- NSString *sql = @"select decrypt(c.company_name),o.so_id,o.sync_data from (select so_id,sync_data,customer_cid from offline_order where sync_data not null) as o join offline_contact as c on c.contact_id = o.customer_cid ;";
- [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
-
- const char *cn = (char *)sqlite3_column_text(stmt,0);
- if (cn == NULL) {
- cn = "";
- }
- const char *_id = (char *)sqlite3_column_text(stmt,1);
- if (_id == NULL) {
- _id = "";
- }
- const char *data = (char *)sqlite3_column_text(stmt,2);
- if (data == NULL) {
- data = "";
- }
-
- NSString *company_name = [NSString stringWithUTF8String:cn];
- NSString *so_id = [NSString stringWithUTF8String:_id];
- NSString *sync_data_str = [NSString stringWithUTF8String:data];
-
- NSDictionary *dic = [@{
- @"check" : @(1),
- @"company_name" : company_name,
- @"so_id" : so_id,
- @"sync_data" : sync_data_str
- } mutableCopy];
- [self->_sync_data addObject:dic];
-
-
- }];
-
- }
- return _sync_data;
- }
- - (IBAction)uploadButtonClicked:(UIButton *)sender {
-
- // NSMutableArray *uploadArray = [NSMutableArray array];
- //
- // for (NSDictionary *dic in self.sync_data) {
- //
- // int check = [dic[@"check"] integerValue];
- // NSString *so_id = dic[@"so_id"];
- //
- // if (check == 1) {
- // [uploadArray addObject:so_id];
- // }
- //
- // }
- //
- //
- //
- //
- // [self dismissViewControllerAnimated:true completion:^{
- // if (self.returnValue ) {
- // self.returnValue(uploadArray);
- // } ;
- // }];
-
-
- // merge order,一个订单也可以合并(合并订单也就是将shop order转为sales order)
- if (self.mergeList.count < 1) {
- [RAUtils message_alert:@"Please add some order" title:@"Warning" controller:self];
- return;
- }
-
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- __weak typeof(self) weakself = self;
-
- __block NSMutableString *order_ids = [NSMutableString string];
- __block NSString *checked_id = nil;
-
- __block BOOL closeOrder = YES;
- [self.mergeList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-
- NSDictionary *dic = (NSDictionary *)obj;
- // order_id
- NSString *order_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_id"]];
- // order_code
- NSString *order_code = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_code"]];
-
- [order_ids appendString:[NSString stringWithFormat:@"%@,",order_id]];
- if (idx == 0) {
- checked_id = order_id;
- }
-
- // 检查是否被自己打开
- if ([order_code isEqualToString:appDelegate.order_code]) {
- closeOrder = NO;
- *stop = YES;
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"can not add to combine list because current order is opened. Do you want to close order and add to combine list?" preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- closeOrder = NO;
-
- }];
-
- UIAlertAction *closeOrderAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
-
- [alertVC dismissViewControllerAnimated:YES completion:^{
- // 关闭订单
- __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Release Order" completion:^{
- [RANetwork request_release_order:order_code withScreen:ScreenCodeOfflineSetting completionHandler:^(NSMutableDictionary *result) {
- NSDictionary* order_json =result;
-
- [waitalert dismissViewControllerAnimated:YES completion:^{
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- if([[order_json valueForKey:@"result"] intValue]==2)
- {
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- [appDelegate closeOrder];
- closeOrder = YES;
-
- [weakself uploadButtonClicked:weakself.uploadButton];
-
- } else {
- // 释放失败
- closeOrder = NO;
- [RAUtils message_alert:@"Release Order Failed!" title:@"Warning" controller:weakself];
- }
- }];
-
- }];
- }];
-
- }];
-
-
- // NSDictionary* order_json = [RANetwork release_Order:order_code withScreen:ScreenCodeOfflineSetting];
-
- // dispatch_async(dispatch_get_main_queue(), ^{
- //
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- //// [waitalert dismissViewControllerAnimated:YES completion:nil];
- // if([[order_json valueForKey:@"result"] intValue]==2)
- // {
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //
- // [appDelegate closeOrder];
- // closeOrder = YES;
- //
- // [weakself uploadButtonClicked:weakself.uploadButton];
- //
- // } else {
- // // 释放失败
- // closeOrder = NO;
- // [RAUtils message_alert:@"Release Order Failed!" title:@"Warning" controller:weakself];
- // }
- // });
- }];
-
- [alertVC addAction:cancelAction];
- [alertVC addAction:closeOrderAction];
-
- [weakself presentViewController:alertVC animated:YES completion:nil];
- }
- }];
-
- if (closeOrder) {
- [order_ids deleteCharactersInRange:NSMakeRange(order_ids.length - 1, 1)];
-
-
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- [params setObject:order_ids forKey:@"order_ids"];
- [params setObject:checked_id forKey:@"targetOrderId"];
- [params setObject:[Singleton sharedInstance].customerInfo forKey:@"customerInfo"];
-
- __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Merge Order" completion:^{
- [RANetwork request_mergeorder:params completionHandler:^(NSMutableDictionary *result) {
- NSDictionary *ret = result;
- // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
- [waitalert dismissViewControllerAnimated:YES completion:^{
- [self.navigationController dismissViewControllerAnimated:YES completion:^{
-
- if (weakself.mergeBlock) {
- weakself.mergeBlock(ret);
- }
-
- }];
-
- }];
-
- }];
- }];
-
-
-
- // NSDictionary *ret = [RANetwork merge_order:params];
- //
- // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
- //
- // [self.navigationController dismissViewControllerAnimated:YES completion:^{
- //
- // if (weakself.mergeBlock) {
- // weakself.mergeBlock(ret);
- // }
- //
- // }];
- }
-
-
- }
- #pragma mark - dataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.mergeList.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath];
-
- NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
- int check = [dic[@"check"] intValue];
- NSString *so_id = dic[@"so_id"];
- NSString *create_by = dic[@"create_by"];
- NSString *create_time = [dic[@"create_time"] stringByAppendingString:@" PST"];
-
- if (check) {
- cell.checkedButton.selected = YES;
- self.mergeTo = indexPath;
- } else {
- cell.checkedButton.selected = NO;
- }
-
- cell.labelsoid.text = so_id;
- cell.labelcompany.text = create_by;
- cell.labelCreateTime.text = create_time;
-
- __weak typeof(self) weakSelf = self;
- __weak typeof(tableView) weakTable = tableView;
- // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
- cell.checkBlock = ^{
- [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath];
-
- };
-
- return cell;
- }
- #pragma mark - delegate
- - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
-
- NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
-
- if(value==nil)
- value=@"";
- unsigned long color = strtoul([value UTF8String],0,16);
-
- DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
- myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
- myView.layer.shadowColor = [UIColor blackColor].CGColor;
- myView.layer.shadowOffset = CGSizeMake(0, 0);
- myView.layer.shadowOpacity = 0.5;
- myView.layer.shadowRadius = 2.0;
-
- // UILabel *mergeTo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 44)];
- // mergeTo.textAlignment = NSTextAlignmentCenter;
- // mergeTo.textColor=UIColorFromRGB(color);
- // mergeTo.backgroundColor = [UIColor clearColor];
- // mergeTo.text=NSLocalizedString(@"To", nil);
- // [mergeTo sizeToFit];
- // [myView addSubview:mergeTo];
-
- UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 22)];// 48,2
- solabel.textColor=UIColorFromRGB(color);
- solabel.backgroundColor = [UIColor clearColor];
- solabel.text=NSLocalizedString(@"SO#", nil);
- [solabel sizeToFit];
- [myView addSubview:solabel];
-
- UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 21, 100, 22)];
- userlabel.textColor=UIColorFromRGB(color);
- userlabel.backgroundColor = [UIColor clearColor];
- userlabel.text=NSLocalizedString(@"Create By", nil);
- [userlabel sizeToFit];
- [myView addSubview:userlabel];
-
-
- UILabel *createTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(tableView.bounds.size.width - 150, 21, 100, 22)];
- createTimeLabel.textColor=UIColorFromRGB(color);
- createTimeLabel.backgroundColor = [UIColor clearColor];
- createTimeLabel.text=NSLocalizedString(@"Create Time", nil);
- [createTimeLabel sizeToFit];
- [myView addSubview:createTimeLabel];
-
-
- myView.autoresizesSubviews=true;
- myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- //
- return myView;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- return 44;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 86;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
- SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
-
- if (self.mergeTo && self.mergeTo.row != indexPath.row) {
- [self tableView:tableView didSelectRowAtIndexPath:self.mergeTo];
- }
-
- cell.selected = NO;
- cell.checkedButton.selected = !cell.checkedButton.selected;
-
- NSMutableDictionary *dic = (NSMutableDictionary *)[self.mergeList objectAtIndex:indexPath.row];
- int check = [dic[@"check"] intValue];
-
- if (cell.checkedButton.selected) {
- check = 1;
- self.mergeTo = indexPath;
- } else {
- self.mergeTo = nil;
- check = 0;
- }
- [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"];
-
- }
- - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
-
- NSDictionary *dic = [self.mergeList objectAtIndex:indexPath.row];
- [self.mergeList removeObject:dic];
- // self.mergeTo = nil;
- NSString *order_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order_id"]];
- if (self.deleteBlock) {
- self.deleteBlock(order_id);
- }
-
-
- [tableView reloadData];
- }];
-
- return @[deleteAction];
- }
- //- (BOOL
- - (void)onCloseClick:(id)sender {
- [self dismissViewControllerAnimated:true completion:nil];
- }
- - (void)onClearClick:(id)sender {
- if (self.clearBlock) {
- self.clearBlock();
- }
- [self.mergeList removeAllObjects];
- [self.syncDataTableView reloadData];
- }
- @end
|