Mini Kabibi Habibi
using System;
using System.Windows;
using System.Windows.Data;
using GridDemo;
using DevExpress.Xpf.Editors.Settings;
using DevExpress.Data.Mask;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.DemoBase.DataClasses;
using DevExpress.Xpf.Grid;
using System.Windows.Controls;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using DevExpress.Xpf.DemoBase.NWind;
using System.Windows.Input;
using DevExpress.Xpf.DemoBase;
using DevExpress.Xpf.Utils;
using System.Data;
namespace GridDemo {
public class OrdersWithDetail : Orders {
public IList<Invoices> Invoices { get; private set; }
public static IList<OrdersWithDetail> CreateOrdersForMasterDetailView(string customerID, int employeeID) {
DataView orders = NWindData.Orders;
IList<OrdersWithDetail> res = new List<OrdersWithDetail>();
Dictionary<int, int> dict = EmployeesWithPhotoData.OrdersRelationsDictionary;
foreach(DataRowView order in orders) {
if((customerID == "" || (string)order["CustomerID"] == customerID) && (dict[(int)order["OrderID"]]) == employeeID)
res.Add(new OrdersWithDetail(order));
}
return res;
}
public static IList<OrdersWithDetail> CreateOrders(string customerID, int employeeID) {
DataView orders = NWindData.Orders;
IList<OrdersWithDetail> res = new List<OrdersWithDetail>();
int i = 0;
foreach(DataRowView order in orders) {
if(i > 10) break;
if((customerID == "" || (string)order["CustomerID"] == customerID) && (int)order["EmployeeID"] == employeeID) {
res.Add(new OrdersWithDetail(order));
i++;
}
}
return res;
}
IList<Invoices> CreateInvoices(int orderID, string customerID) {
DataView invoices = NWindData.Invoices;
IList<Invoices> res = new List<Invoices>();
foreach(DataRowView invoice in invoices) {
if((customerID == "" || (string)invoice["CustomerID"] == customerID) && (int)invoice["OrderID"] == orderID)
res.Add(new Invoices() {
Address = invoice["Address"] as string,
City = invoice["City"] as string,
Country = invoice["Country"] as string,
CustomerID = invoice["CustomerID"] as string,
Discount = Convert.ToDecimal((float)invoice["Discount"]),
ExtendedPrice = (decimal)invoice["ExtendedPrice"],
Freight = (decimal)invoice["Freight"],
OrderDate = (DateTime)invoice["OrderDate"],
OrderID = (int)invoice["OrderID"],
PostalCode = invoice["PostalCode"] as string,
ProductID = (int)invoice["ProductID"],
ProductName = invoice["ProductName"] as string,
Quantity = (short)invoice["Quantity"],
Region = invoice["Region"] as string,
RequiredDate = (DateTime)invoice["RequiredDate"],
Salesperson = invoice["Salesperson"] as string,
ShipAddress = invoice["ShipAddress"] as string,
ShipCity = invoice["ShipCity"] as string,
ShipCountry = CountryNameResolver.Resolve(invoice["ShipCountry"] as string),
ShipName = invoice["ShipName"] as string,
ShipPostalCode = invoice["ShipPostalCode"] as string,
ShipRegion = invoice["ShipRegion"] as string,
UnitPrice = (decimal)invoice["UnitPrice"]
});
}
return res;
}
public OrdersWithDetail(DataRowView o) {
this.CustomerID = o["CustomerID"] as string;
this.EmployeeID = (int)o["EmployeeID"];
this.Freight = (decimal)o["Freight"];
this.OrderDate = (DateTime)o["OrderDate"];
this.OrderID = (int)o["OrderID"];
this.RequiredDate = (DateTime)o["RequiredDate"];
this.ShipAddress = o["ShipAddress"] as string;
this.ShipCity = o["ShipCity"] as string;
this.ShipCountry = CountryNameResolver.Resolve(o["ShipCountry"] as string);
this.ShipName = o["ShipName"] as string;
this.ShipPostalCode = o["ShipPostalCode"] as string;
this.ShipRegion = o["ShipRegion"] as string;
this.ShipVia = (int)o["ShipVia"];
Invoices = CreateInvoices(OrderID, CustomerID);
}
}
public class CustomersWithDetail : Customers {
public IList<OrdersWithDetail> Orders { get; private set; }
public static bool Exists(DataView view, Predicate<DataRowView> condition) {
foreach(DataRowView row in view)
if(condition(row))
return true;
return false;
}
public static IList<CustomersWithDetail> CreateCustomersForMaterDetailView(int employeeID) {
DataView orders = NWindData.Orders;
DataView customers = NWindData.Customers;
Dictionary<int, int> dict = EmployeesWithPhotoData.OrdersRelationsDictionary;
IList<CustomersWithDetail> res = new List<CustomersWithDetail>();
foreach(DataRowView c in customers) {
DataRowView row = c;
if(Exists(orders, (order) => (string)order["CustomerID"] == (string)row["CustomerID"] && dict[(int)order["OrderId"]] == employeeID))
res.Add(new CustomersWithDetail(c, employeeID, true));
}
return res;
}
public static IList<CustomersWithDetail> CreateCustomers(int employeeID) {
DataView orders = NWindData.Orders;
DataView customers = NWindData.Customers;
IList<CustomersWithDetail> res = new List<CustomersWithDetail>();
foreach(DataRowView c in customers) {
DataRowView row = c;
if(Exists(orders, (order) => (string)order["CustomerID"] == (string)row["CustomerID"] && (int)order["EmployeeID"] == employeeID))
res.Add(new CustomersWithDetail(c, employeeID));
}
return res;
}
public CustomersWithDetail(DataRowView c, int employeeID, bool newOrders = false) {
this.Address = c["Address"] as string;
this.City = c["City"] as string;
this.CompanyName = c["CompanyName"] as string;
this.ContactName = c["ContactName"] as string;
this.ContactTitle = c["ContactTitle"] as string;
this.Country = CountryNameResolver.Resolve(c["Country"] as string);
this.CustomerID = c["CustomerID"] as string;
this.Fax = c["Fax"] as string;
this.Phone = c["Phone"] as string;
this.PostalCode = c["PostalCode"] as string;
this.Region = c["Region"] as string;
Orders = newOrders ? OrdersWithDetail.CreateOrdersForMasterDetailView(CustomerID, employeeID) : OrdersWithDetail.CreateOrders(CustomerID, employeeID);
}
}
public class EmployeesWithDetails : List<EmployeeWithDetails> {
public EmployeesWithDetails() {
AddRange(EmployeeWithDetails.CreateMasterDetailSource());
}
}
public class EmployeesWithDetailsForEmbeddedView : List<EmployeeWithDetails> {
public EmployeesWithDetailsForEmbeddedView() {
AddRange(EmployeeWithDetails.CreateEmbeddedViewSource());
}
}
public class EmployeeWithDetails : Employees {
IList<CustomersWithDetail> customersCore;
public IList<CustomersWithDetail> Customers {
get {
if(customersCore == null)
customersCore = CustomersWithDetail.CreateCustomers(EmployeeID);
return customersCore;
}
}
IList<OrdersWithDetail> ordersCore;
public IList<OrdersWithDetail> Orders {
get {
if(ordersCore == null)
ordersCore = OrdersWithDetail.CreateOrders("", EmployeeID);
return ordersCore;
}
}
IList<CustomersWithDetail> mdcustomersCore;
public IList<CustomersWithDetail> MDCustomers {
get {
if(mdcustomersCore == null)
mdcustomersCore = CustomersWithDetail.CreateCustomersForMaterDetailView(EmployeeID);
return mdcustomersCore;
}
}
IList<OrdersWithDetail> mdordersCore;
public IList<OrdersWithDetail> MDOrders {
get {
if(mdordersCore == null)
mdordersCore = OrdersWithDetail.CreateOrdersForMasterDetailView("", EmployeeID);
return mdordersCore;
}
}
IEnumerable<ChartPoint> chartSourceCore;
public IEnumerable<ChartPoint> ChartSource {
get {
if(chartSourceCore == null)
chartSourceCore = CreateChartSource();
return chartSourceCore;
}
}
IEnumerable<ChartPoint> CreateChartSource() {
IList<ChartPoint> list = (from o in MDOrders
group o by o.OrderDate into cp
select new ChartPoint() {
ArgumentMember = cp.Key,
Orders = cp.ToList() }).ToList();
foreach(ChartPoint cp in list) {
decimal value = 0;
foreach(OrdersWithDetail order in cp.Orders)
foreach(Invoices inv in order.Invoices)
value += inv.Quantity * inv.UnitPrice;
cp.ValueMember = (int)value;
}
return list;
}
public string EMail { get; set; }
public static IList<EmployeeWithDetails> CreateMasterDetailSource() {
List<Employee> empls = EmployeesWithPhotoData.DataSource;
List<EmployeeWithDetails> res = new List<EmployeeWithDetails>();
foreach(Employee employee in empls)
res.Add(new EmployeeWithDetails(employee));
InitSubordinateEmployee(res);
return res;
}
public List<EmployeeWithDetails> SubEmployees { get; set; }
static void InitSubordinateEmployee(List<EmployeeWithDetails> res) {
foreach(EmployeeWithDetails empl in res) {
int emplId = empl.EmployeeID;
empl.SubEmployees = res.Where(e => e.ParentId == emplId).ToList();
}
}
public static IList<EmployeeWithDetails> CreateEmbeddedViewSource() {
DataView employees = NWindData.Employees;
List<EmployeeWithDetails> res = new List<EmployeeWithDetails>();
foreach(DataRowView employee in employees) {
res.Add(new EmployeeWithDetails(employee));
}
return res;
}
public EmployeeWithDetails(DataRowView e) {
Address = e["Address"] as string;
BirthDate = (DateTime)e["BirthDate"];
City = e["City"] as string;
Country = e["Country"] as string;
EmployeeID = (int)e["EmployeeID"];
Extension = e["Extension"] as string;
FirstName = e["FirstName"] as string;
HireDate = (DateTime)e["HireDate"];
HomePhone = e["HomePhone"] as string;
LastName = e["LastName"] as string;
Notes = e["Notes"] as string;
Photo = (byte[])e["Photo"];
PostalCode = (string)e["PostalCode"] as string;
Region = e["Region"] as string;
Title = e["Title"] as string;
TitleOfCourtesy = e["TitleOfCourtesy"] as string;
}
public EmployeeWithDetails(Employee e) {
Address = e.AddressLine1;
BirthDate = e.BirthDate;
City = e.City;
Country = CountryNameResolver.Resolve(e.CountryRegionName);
EmployeeID = e.Id;
FirstName = e.FirstName;
HireDate = e.HireDate;
HomePhone = e.Phone;
LastName = e.LastName;
Photo = e.ImageData;
PostalCode = e.PostalCode;
Region = e.CountryRegionName;
EMail = e.EmailAddress;
Title = e.JobTitle;
this.ParentId = e.ParentId;
}
internal int ParentId { get; private set; }
}
public class ChartPoint {
public DateTime ArgumentMember { get; internal set; }
public int ValueMember { get; set; }
internal IList<OrdersWithDetail> Orders { get; set; }
}
internal static class CountryNameResolver {
internal static string Resolve(string countryName) {
switch(countryName) {
case "USA":
return "United States";
case "UK":
return "United Kingdom";
default: return countryName;
}
}
}
public class EmployeeToOrdersConverter : IValueConverter {
Dictionary<Employee, IEnumerable<Orders>> employeeOrders = new Dictionary<Employee, IEnumerable<Orders>>();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
Employee empl = value as Employee;
if(empl == null) return null;
IEnumerable<Orders> orders = null;
if(!employeeOrders.TryGetValue(empl, out orders)) {
orders = OrdersWithDetail.CreateOrdersForMasterDetailView("", empl.Id);
employeeOrders.Add(empl, orders);
}
return orders;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}
}