private void GridPurchaseOrders_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { _poItemDomainModels.Clear(); DataModel.Model.PurchaseOrder purchaseOrder = (DataModel.Model.PurchaseOrder)((DataGrid)sender).CurrentItem; if (purchaseOrder == null && ((DataGrid)sender).Items.Count > 0) { purchaseOrder = (DataModel.Model.PurchaseOrder)((DataGrid)sender).Items[0]; } if (purchaseOrder != null) { purchaseOrder.POItems.ForEach(x => _poItemDomainModels.Add(new POItemDomainModel { POItem = x })); if (purchaseOrder.POStatusCategory != (sbyte)DataType.POStatusCategory.Completed) { for (int i = 0; i < 5; i++) { _poItemDomainModels.Add(new POItemDomainModel { POItem = new POItem { PurchaseOrderId = purchaseOrder.PurchaseOrderId, CreatedBy = ResourcesHelper.CurrentUser.Name, CreatedOn = DateTime.Now, LastModifiedBy = ResourcesHelper.CurrentUser.Name, LastModifiedOn = DateTime.Now, } }); } } TxtDetailTotalInfo.Text = string.Format("共{0}种商品, 合计{1}件, 合计金额: {2}", purchaseOrder.POItems.Count.ToString("F2"), purchaseOrder.POItems.Sum(x => x.QuantityReceived ?? 0).ToString("F2"), (purchaseOrder.POItems.Sum(x => x.QuantityReceived * x.PriceReceived) ?? 0).ToString("F2")); } }
private void BtnSave_Click(object sender, RoutedEventArgs e) { try { DataModel.Model.PurchaseOrder purchaseOrder = (DataModel.Model.PurchaseOrder)GridPurchaseOrders.SelectedItem; if (purchaseOrder != null) { purchaseOrder.POContractNo = purchaseOrder.PurchaseOrderNo; purchaseOrder.POItems = _poItemDomainModels .Where(x => x.POItem.Produce != null && x.POItem.POStatusCategory != (sbyte)DataType.POStatusCategory.Completed) .Select(x => x.POItem).ToList(); if (purchaseOrder.POItems.Count == 0) { MessageBox.Show("没有订单条目需要保存!", SmallHoneybee.Wpf.Properties.Resources.SystemName, MessageBoxButton.OK, MessageBoxImage.Information); return; } purchaseOrder.POItems.ForEach(x => { x.CreatedBy = ResourcesHelper.CurrentUser.Name; x.CreatedOn = DateTime.Now; x.LastModifiedBy = ResourcesHelper.CurrentUser.Name; x.LastModifiedOn = DateTime.Now; if (purchaseOrder.POStatusCategory == (sbyte)DataType.POStatusCategory.Completed) { x.Produce.Quantity += x.QuantityReceived ?? 0; x.Produce.Producelogs.Add(new Producelog { ChangedBy = ResourcesHelper.CurrentUser.Name, DateChanged = DateTime.Now, NewValue = string.Format(ResourcesHelper.PurchaseOrderImporterFormat, purchaseOrder.PurchaseOrderNo, (x.QuantityReceived ?? 0).ToString("F2"), (x.PriceReceived ?? 0).ToString("F2"), x.Produce.Quantity.ToString("F2")) }); x.Produce.LastOrderDate = DateTime.Now; x.POStatusCategory = (sbyte)DataType.POStatusCategory.Completed; } }); purchaseOrder.TotalAmount = purchaseOrder.POItems.Sum(x => x.PriceReceived * x.QuantityReceived); purchaseOrder.GrandTotal = (purchaseOrder.TotalAmount ?? 0) + (purchaseOrder.TotalOther ?? 0) + (purchaseOrder.TotalShipping ?? 0) + (purchaseOrder.TotalTax ?? 0); if (purchaseOrder.PurchaseOrderId == 0) { _purchaseOrderRepository.Create(purchaseOrder); } else { _purchaseOrderRepository.Update(purchaseOrder); } _unitOfWork.Commit(); MessageBox.Show("保存成功!", SmallHoneybee.Wpf.Properties.Resources.SystemName, MessageBoxButton.OK, MessageBoxImage.Information); ExecuteSearchText(); } } catch (Exception ex) { Log4NetHelper.WriteLog(ex.ToString()); MessageBox.Show("保存失败!", SmallHoneybee.Wpf.Properties.Resources.SystemName, MessageBoxButton.OK, MessageBoxImage.Error); ExecuteSearchText(); } }
private void ImportPurchaseOrders() { DataModel.Model.PurchaseOrder purchaseOrder = _purchaseOrderRepository .Query() .OrderByDescending(x => x.DateOriginated) .FirstOrDefault(x => x.POStatusCategory == (sbyte)DataType.POStatusCategory.Ordered); if (purchaseOrder == null) { return; } IList <DataModel.Model.Produce> produces = _produceRepository.Query().ToList(); IEnumerable <IEnumerable <KeyValuePair <string, string> > > csvContents = CsvHepler.GetCsvData(tbPath.Text); IEnumerable <KeyValuePair <byte, string> > procedureUnits = CommonHelper.Enumerate <DataType.ProduceUnit>(); csvContents.ForEach(x => { DataModel.Model.POItem poItem = new DataModel.Model.POItem(); foreach (var y in x) { if (!string.IsNullOrEmpty(y.Value)) { DataType.PurchaseOrderField purchaseOrderField = (DataType.PurchaseOrderField)Enum.Parse(typeof(DataType.PurchaseOrderField), y.Key); switch (purchaseOrderField) { case DataType.PurchaseOrderField.ProduceNo: DataModel.Model.Produce produce = produces .Single(c => y.Value.Equals(c.ProduceNo, StringComparison.CurrentCultureIgnoreCase)); poItem.ProduceId = produce.ProduceId; poItem.Produce = produce; continue; case DataType.PurchaseOrderField.PriceOrdered: poItem.PriceOrdered = float.Parse(y.Value); continue; case DataType.PurchaseOrderField.PriceReceived: poItem.PriceReceived = float.Parse(y.Value); continue; case DataType.PurchaseOrderField.QuantityOrdered: poItem.QuantityOrdered = float.Parse(y.Value); continue; case DataType.PurchaseOrderField.QuantityReceived: poItem.QuantityReceived = float.Parse(y.Value); continue; default: continue; } } } poItem.POStatusCategory = (sbyte)DataType.POItemStatusCategory.Dispatched; poItem.CreatedBy = "admin"; poItem.CreatedOn = DateTime.Now; poItem.LastModifiedBy = "admin"; poItem.LastModifiedOn = DateTime.Now; poItem.RowVersion = DateTime.Now; poItem.Produce.Quantity += poItem.QuantityReceived ?? 0; poItem.Produce.Producelogs.Add(new Producelog { ChangedBy = ResourcesHelper.CurrentUser.Name, DateChanged = DateTime.Now, NewValue = string.Format(ResourcesHelper.PurchaseOrderImporterFormat, purchaseOrder.PurchaseOrderNo, (poItem.QuantityReceived ?? 0).ToString("F2"), (poItem.PriceReceived ?? 0).ToString("F2"), poItem.Produce.Quantity.ToString("F2")) }); poItem.Produce.LastOrderDate = DateTime.Now; purchaseOrder.POItems.Add(poItem); }); purchaseOrder.POStatusCategory = (sbyte)DataType.POStatusCategory.Completed; purchaseOrder.TotalAmount = purchaseOrder.POItems.Sum(x => x.PriceReceived * x.QuantityReceived); purchaseOrder.GrandTotal = (purchaseOrder.TotalAmount ?? 0) + (purchaseOrder.TotalOther ?? 0) + (purchaseOrder.TotalShipping ?? 0) + (purchaseOrder.TotalTax ?? 0); _purchaseOrderRepository.Update(purchaseOrder); _unitOfWork.Commit(); System.Windows.MessageBox.Show("订单导入成功!", Properties.Resources.SystemName, MessageBoxButton.OK, MessageBoxImage.Information); Close(); }