DurnyklyYol/DurnyklyYol.Module/BusinessObjects/Cargo.cs

208 lines
7.3 KiB
C#
Raw Normal View History

2024-09-02 10:07:25 +00:00
using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using System.ComponentModel;
namespace DurnyklyYol.Module.BusinessObjects
{
[DefaultClassOptions, NavigationItem("Transportation")]
[ImageName("Shipment")]
[DefaultProperty(nameof(No))]
[Appearance("CargoRedBackground", AppearanceItemType = "ViewItem", TargetItems = "Credit", Criteria = "TotalСredit > 0", BackColor = "Red", FontColor = "White")]
[Appearance("CargoStrikeBackground", AppearanceItemType = "ViewItem", TargetItems = "*", Criteria = "TotalСredit = 0 And TotalPriceAmount >0 ", FontStyle = DevExpress.Drawing.DXFontStyle.Strikeout)]
public class Cargo : BaseObject
{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument113146.aspx).
// Use CodeRush to create XPO classes and properties with a few keystrokes.
// https://docs.devexpress.com/CodeRushForRoslyn/118557
public Cargo(Session session)
: base(session)
{
}
protected override void OnSaving()
{
base.OnSaving();
if (Session is not NestedUnitOfWork
&& (Session.DataLayer != null)
&& Session.IsNewObject(this)
&& (Session.ObjectLayer is SimpleObjectLayer)
//OR
//&& !(Session.ObjectLayer is DevExpress.ExpressApp.Security.ClientServer.SecuredSessionObjectLayer)
&& string.IsNullOrEmpty(No))
{
int nextSequence = DistributedIdGeneratorHelper.Generate(Session.DataLayer, this.GetType().FullName, "ERPrefix");
No = string.Format("KA-{0:D6}", nextSequence);
}
}
double totalWeight;
string no;
//CargoState state;
Point currentPoint;
DateTime? arrivedAt;
DateTime? startedAt;
Carrier carrier;
Route route;
[ReadOnly(true), VisibleInDetailView(false), Index(0)]
[Size(SizeAttribute.DefaultStringMappingFieldSize), Indexed, RuleUniqueValue]
public string No
{
get => no;
set => SetPropertyValue(nameof(No), ref no, value);
}
//TODO current root bilen deneshdirip al
public CargoState State
{
get
{
if (CurrentPoint == null || CurrentPoint.Oid == Route.StartPoint.Point.Oid)
{
return CargoState.Warehouse;
}
else if (CurrentPoint != null && CurrentPoint.Oid == Route.DestinationPoint.Point.Oid)
{
return CargoState.Arrived;
}
else
{
return CargoState.InTransit;
}
}
//set => SetPropertyValue(nameof(State), ref state, value);
}
[ModelDefault("DisplayFormat", "{0:g}"), ModelDefault("EditMask", "g")]
public DateTime? StartedAt
{
get => startedAt;
set => SetPropertyValue(nameof(StartedAt), ref startedAt, value);
}
[ModelDefault("DisplayFormat", "{0:g}"), ModelDefault("EditMask", "g")]
public DateTime? ArrivedAt
{
get => arrivedAt;
set => SetPropertyValue(nameof(ArrivedAt), ref arrivedAt, value);
}
public double TotalWeight
{
get => totalWeight;
set => SetPropertyValue(nameof(TotalWeight), ref totalWeight, value);
}
//[Persistent(nameof(TotalPriceAmount))]
//private decimal totalPriceAmount;
//[PersistentAlias(nameof(totalPriceAmount))]
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal TotalPriceAmount => Goods.Sum(gs => gs.Price);
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal TotalExpenseAmount => Expenses.Sum(es => es.Amount);
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal PayedTotal => Goods.Sum(gs => gs.TotalPayment);
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal TotalСredit => Goods.Sum(gs => gs.Credit);
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal ExpectedProfit => TotalPriceAmount - TotalExpenseAmount;
[ModelDefault("DisplayFormat", "${0:#,##0.00}")]
public decimal CurrentProfit => PayedTotal - TotalExpenseAmount;
[Browsable(false)]
public string StartsFrom => Route?.StartPoint?.Name;
[Browsable(false)]
public string DestinationTo => Route?.DestinationPoint?.Name;
[RuleRequiredField(DefaultContexts.Save), ImmediatePostData]
public Route Route
{
get => route;
set {
if (SetPropertyValue(nameof(Route), ref route, value) && !IsLoading && !IsSaving && value is not null)
{
CurrentPoint = null;
OnChanged(nameof(CurrentPoint));
}
}
}
private void populateCargoPoints()
{
//todo initialize cargo route points
//current point is taken from cargo route points where date is not null
}
[Browsable(false)]
public XPCollection<Point> AvailablePoints
{
get
{
if (Route is null) return new XPCollection<Point>(Session);
var startPoint = Route.StartPoint.Point;
var endPoint = Route.DestinationPoint.Point;
var points = Route.Points
.OrderBy(or => or.Order)
.Select(sl => sl.Point)
.ToList();
points.Insert(0, startPoint);
points.Add(endPoint);
return new XPCollection<Point>(Session, points);
}
}
[RuleRequiredField(DefaultContexts.Save), ImmediatePostData, DataSourceProperty(nameof(AvailablePoints))]
public Point CurrentPoint
{
get => currentPoint;
set => SetPropertyValue(nameof(CurrentPoint), ref currentPoint, value);
}
[RuleRequiredField(DefaultContexts.Save)]
public Carrier Carrier
{
get => carrier;
set => SetPropertyValue(nameof(Carrier), ref carrier, value);
}
[Association("Cargo-Goods")]
public XPCollection<Goods> Goods
{
get
{
return GetCollection<Goods>(nameof(Goods));
}
}
[Association("Cargo-Expenses"), Aggregated]
public XPCollection<Expense> Expenses
{
get
{
return GetCollection<Expense>(nameof(Expenses));
}
}
[Association, Aggregated]
public XPCollection<CargoRoutePoints> CargoPoints
{
get
{
return GetCollection<CargoRoutePoints>(nameof(CargoPoints));
}
}
}
public enum CargoState
{
Warehouse,
InTransit,
Arrived
}
}