using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model.Account;
using Muchinfo.MTPClient.Data.Model.Delivery;
using System;
using Muchinfo.MTPClient.Delivery.Views;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.Infrastructure.Windows;
using Muchinfo.MTPClient.IService;
using Muchinfo.MTPClient.Resources;
using Muchinfo.MTPClient.Infrastructure.Cache;
namespace Muchinfo.MTPClient.Delivery.ViewModels
{
///
/// This class contains properties that a View can data bind to.
///
/// See http://www.galasoft.ch/mvvm
///
///
public class DeliveryWarehouseOrderViewModel : QueryPanelModelBase
{
#region "=========私有成员/Private Data Members"
protected IDeliveryService _deliveryService;
#endregion "Private Data Members"
#region "=========构造函数/Constructor/Initialization"
public DeliveryWarehouseOrderViewModel()
{
_deliveryService = SimpleIoc.Default.GetInstance();
}
#endregion "Constructor/Initialization"
#region "=========接口重写/Interface implementation Or override"
///
/// 初始化注册消息
///
public override void RegisterMessage()
{
EndTime = DateTime.Now;
StartTime = DateTime.Now.AddDays(-30);
RefreshCommand.Execute(null);
MessengerHelper.DefaultUnregister(this, MessengerTokens.OrderNotify);
MessengerHelper.DefaultRegister(this, MessengerTokens.OrderNotify, (e) =>
{
//接收通知时,防止时间设置错误,弹出错误提示
EndTime = ApplicationParameter.ServerTimeNow;
StartTime = ApplicationParameter.ServerTimeNow.AddDays(-30);
RefreshCommand.Execute(null);
});
}
public override void UnRegisterMessage()
{
MessengerHelper.DefaultUnregister(this);
}
///
/// 查询交收申报单
///
public override void RefreshContent()
{
if (!Vailed())
{
return;
}
IsBusy = true;
var accountid = UserManager.CurrentTradeAccount.AccountId;
//if (UserManager.CurrentTradeAccount.FundsAccounts != null && UserManager.CurrentTradeAccount.FundsAccounts.Any())
//{
// accountid = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
_deliveryService.QueryDeliveryWarehouseOrders(SearchWarehouseOrdersType.AccountId,string.Empty,accountid, StartTime.Date, EndTime.Date.AddHours(24), MarketOrderSuccess, QueryErrorCallback);
}
#endregion "Interface implementation Or override"
#region "=========公共属性/Public Properties To Get/Set "
#region 开始时间
private DateTime startTime;
///
/// 开始时间
///
public DateTime StartTime
{
get { return startTime; }
set { Set(() => StartTime, ref startTime, value); }
}
#endregion
#region 结束时间
private DateTime _endTime;
///
/// 结束时间
///
public DateTime EndTime
{
get { return _endTime; }
set { Set(() => EndTime, ref _endTime, value); }
}
#endregion
#region 所有ListSource
private List _allAduitOrders;
///
/// 所有ListSource
///
public List AllAduitOrders
{
get { return _allAduitOrders; }
set { Set(() => AllAduitOrders, ref _allAduitOrders, value); }
}
#endregion
#region 当前选择单据转换
private List _currentSelectedOutStore = null;
///
/// 当前勾选的提货单据LIST
///
public List CurrentSelectedOutStore
{
get
{
return _currentSelectedOutStore;
}
set
{
Set(() => CurrentSelectedOutStore, ref _currentSelectedOutStore, value);
}
}
#endregion
#region 选择当前行【实体】
private DeliveryWarehouse _selectEntrustOrder;
///
/// 选择 行
///
public DeliveryWarehouse SelectEntrustOrder
{
get { return _selectEntrustOrder; }
set { Set(() => SelectEntrustOrder, ref _selectEntrustOrder, value); }
}
#endregion
#endregion "Public Properties To Get/Set "
#region "=========公共命令/Public Commands"
#region 交割申报下单命令Command
private RelayCommand _deliveryCommand;
///
/// Gets the DeliveryCommand.
///
public RelayCommand DeliveryCommand
{
get
{
return _deliveryCommand
?? (_deliveryCommand = new RelayCommand(
() =>
{
MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.DeliveryDeclare);
}));
}
}
#endregion
#region 重置查询时间命令Command
private RelayCommand _resetCommand;
///
/// Gets the ResetCommand.
///
public RelayCommand ResetCommand
{
get
{
return _resetCommand
?? (_resetCommand = new RelayCommand(
() =>
{
EndTime = DateTime.Now;
StartTime = DateTime.Now.AddDays(-30);
}));
}
}
#endregion
#region 弹出窗口填写个人信息Command
private RelayCommand _popUpRecieveInfoCommand;
///
/// 弹出窗口填写个人信息Command
///
public RelayCommand PopUpRecieveInfoCommand
{
get
{
return _popUpRecieveInfoCommand
?? (_popUpRecieveInfoCommand = new RelayCommand(
() =>
{
MessengerHelper.DefaultSend(SelectEntrustOrder, MessengerTokens.WarehouseRecieveInfo);
}));
}
}
#endregion
#region 提货命令Command
public ICommand TakeGoodsCommand
{
get
{
return new RelayCommand(() =>
{
CurrentSelectedOutStore = new List();
CurrentSelectedOutStore.Add(new OutStoreReqDetailModel() { WRId = (SelectEntrustOrder.WRID), StoreOutQty = (SelectEntrustOrder.Qty) });
if (SelectEntrustOrder != null && CurrentSelectedOutStore.Count()>0)
{
var boxResult = MessageBoxHelper.ShowQuestion(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesureToTakeGoods,
Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips);
if (boxResult == MessageBoxResult.Yes)
{
IsBusy = true;
var newOrder = new OutStoreReqModel()
{
//申请
OperateType = (int)TakaGoodsOperateType.Apply,
OutStoreDetail = CurrentSelectedOutStore
};
if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
{
newOrder.AccountID = newOrder.OperatorID = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
}
else
{
newOrder.AccountID = newOrder.OperatorID = UserManager.CurrentTradeAccount.AccountId;
}
try
{
_deliveryService.SubmitOutStoreReq(newOrder, EntrurstSuccessCallBack, EntrurstErrorCallBack);
}
finally
{
}
}
}
});
}
}
#endregion
#region 仓单详情Command
private RelayCommand _popDetailCommand;
///
/// 信息Command
///
public RelayCommand PopDetailCommand
{
get
{
return _popDetailCommand
?? (_popDetailCommand = new RelayCommand(
(wareHouse) =>
{
var wareHouseView = new WareHouseOrderDetailView()
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
DataContext = wareHouse,
ShowInTaskbar = false
};
wareHouseView.ShowDialog();
}));
}
}
#endregion
#endregion "Public Commands"
#region "=========私有方法/Private Methods"
///
/// 提交提货成功返回
///
///
private void EntrurstSuccessCallBack(OutStoreRspModel order)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//OKButtonEnabled = true;
//提货成功
MessageBoxHelper.ShowSuccess(Client_Resource.Order_TakaDeliveryGoods_Success_Result,
Client_Resource.UI2014_Tips);
}));
//刷新正常单
MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
}
///
/// 提货委托失败返回
///
///
private void EntrurstErrorCallBack(ErrorEntity errorEntity)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//OKButtonEnabled = true;
ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
}));
}
///
/// 查询成功返回
///
///
protected void MarketOrderSuccess(List orders)
{
UpdateItemSource(orders);
IsBusy = false;
}
///
/// 查询失败返回
///
///
public override void QueryErrorCallback(ErrorEntity errorEntity)
{
IsBusy = false;
var errorMsg = string.Format("{0}:{1}", errorEntity.RequestFunc, ErrorManager.FormatErrorMsg(errorEntity)); //错误信息显示到状态
MessengerHelper.DefaultSend(errorMsg, MessengerTokens.ErrorMessage);
}
///
/// 更新数据源
///
///
private void UpdateItemSource(List orders)
{
if (orders != null)
{
foreach (var item in orders)
{
#region 合约单位转换
var goodsUnit = CacheManager.TradeGoodsUnit.FirstOrDefault((x) => x.UNITID == item.GoodsUnitId);
if (goodsUnit != null)
{
item.GoodsUnitIdDisplay = goodsUnit.GOODSUNITNAME;
}
#endregion
#region 是否可提货
if (item.Status == eWRStatus.Normal && (item.Qty - item.FreezeQty - item.PayQty) > 0)
{
item.IsVisibility = Visibility.Visible;
}
#endregion
try
{
var helper = new EncryptHelper();
item.WarehouseAddress = item.AreaAddress +
(string.IsNullOrWhiteSpace(item.DetailAddress)
? string.Empty
: helper.AesDecrypt(item.DetailAddress));
}
catch (Exception)
{
}
#region 存货货显本人
item.WRSAVER = UserManager.CurrentTradeAccount.CustomerName;
#endregion
}
AllAduitOrders = orders; //所有单据
}
}
#endregion "Private Methods"
#region "=========其它方法/Other Methods"
///
/// 数据验证
///
///
private bool Vailed()
{
if (StartTime > EndTime)
{
MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.DateTimeVaided_StartEnd, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
return false;
}
else if ((EndTime - StartTime).Days > 30)
{
MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.DateTimeVaided_Over30days, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
return false;
}
return true;
}
#endregion "Other Methods"
}
}