Translate

Thứ Hai, 22 tháng 3, 2021

Bài 3: Tạo một đơn hàng bán đơn giản

Bài 3: Tạo một đơn hàng bán đơn giản

Nội dung chính

Tạo module Sales: có các entity chính:
  1. Khách hàng: Mã, tên, địa chỉ
  2. Đơn hàng: Ngày bán, người bán, khách hàng mua, tổng tiền, thành tiền
  3. Chi tiết đơn hàng: Đơn hàng, món hàng, đơn giá, số lượng, thành tiền.
Khi tạo xong chúng ta sẽ có các entity sau:


Lớp khách hàng




using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using System.ComponentModel;
using YouSoft.Vn.XAFCustom.Core.Entities;
using YouSoft.Vn.XAFCustom.Core.Extensions;
using YouSoft.Vn.XAFCustom.Supports;
using YouSoft.Vn.XAFCustom.Supports.Attributes.Validation;
using YouSoft.Vn.XAFCustom.Supports.Interfaces;

namespace App.Module.Modules.Sales
{
    [XafDisplayName("Khách hàng")]
    [ImageName("BO_Customer")]
    [VisibleInDashboards]
    [VisibleInReports]
    [LookupEditorMode(LookupEditorMode.AllItemsWithSearch)]
    public partial class Customer : YsvObject, IHasCode
    {

        public Customer(Session session) : base(session)
        {

        }

        string _name;
        [Indexed(Name = @"Idx_Name")]
        [NonCloneable]
        [VisibleInLookupListView(true)]
        [RequiredOnSave]
        [XafDisplayName("Tên khách")]
        //[ToolTip("   ")]
        public string Name
        {
            get =>
                _name;
            set =>
                SetPropertyValue(nameof(Name), ref _name, value);
        }
        protected override string MakeDescription() =>
            Name;


        string _code;
        [Indexed(Name = @"Idx_Code")]
        [Size(30)]
        [DevExpress.Persistent.Base.NonCloneable]
        [XafDisplayName("Mã khách")]
        [ModelDefault(nameof(IModelMember.AllowEdit), CoreApp.Consts.False)]
        //[ToolTip("")]
        [VisibleInLookupListView(true)]
        [RuleUniqueValue("", DefaultContexts.Save,
   CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction)]
        public string Code
        {
            get { return _code; }
            set { SetPropertyValue(nameof(Code), ref _code, value); }
        }

        int _sequenceNumber;
        [NonCloneable]
        [Indexed]
        [Browsable(false)]
        public int SequenceNumber
        {
            get =>
                _sequenceNumber;
            set =>
                SetPropertyValue(nameof(SequenceNumber), ref _sequenceNumber, value);
        }
        protected override void OnSavingOnly()
        {
            base.OnSavingOnly();
            Code = this.GenerateCode("KH", 4);
        }
    }
}

Lớp đơn hàng

using App.Module.Modules.Sales;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using ModuleBase.Entities;
using System;
using System.Linq;
using YouSoft.Vn.Framework.Core.Utilities;
using YouSoft.Vn.XAFCustom.Core.Entities;
using YouSoft.Vn.XAFCustom.Supports;
using YouSoft.Vn.XAFCustom.Supports.Attributes.Validation;

namespace App.Module.Modules.Inventory
{
    [XafDisplayName("Đơn hàng")]
    [ImageName("BO_SalesOrder")]
    [VisibleInDashboards]
    [VisibleInReports]
    [LookupEditorMode(LookupEditorMode.AllItemsWithSearch)]
    public partial class SalesOrder : YsvObject
    {

        public SalesOrder(Session session) : base(session)
        {

        }
        public override void AfterConstruction()
        {
            base.AfterConstruction();
            Date = DateTime.Now;
            Employee = YsvUser.GetCurrentUser<Employee>(Session);
        }

        private double _total;
        private Customer _customer;
        private Employee _employee;
        private DateTime _date;

        [XafDisplayName("Ngày bán")]
        //[ToolTip("")]
        public DateTime Date
        {
            get => _date;
            set => SetPropertyValue(nameof(Date), ref _date, value);
        }

        [XafDisplayName("Nhân viên")]
        [ModelDefault(nameof(IModelMember.AllowEdit), CoreApp.Consts.False)]
        //[ToolTip("")]
        public Employee Employee
        {
            get => _employee;
            set => SetPropertyValue(nameof(Employee), ref _employee, value);
        }


        [XafDisplayName("Khách hàng")]
        [RequiredOnSave]
        //[ToolTip("")]
        public Customer Customer
        {
            get => _customer;
            set => SetPropertyValue(nameof(Customer), ref _customer, value);
        }

        
        [XafDisplayName("Tiền hàng")]
        [ModelDefault(nameof(IModelMember.DisplayFormat), CoreApp.Consts.N0)]
        [ModelDefault(nameof(IModelMember.EditMask), CoreApp.Consts.N0)]
        [ModelDefault(nameof(IModelMember.AllowEdit), CoreApp.Consts.False)]
        //[ToolTip("")]
        public double Total
        {
            get => _total;
            set => SetPropertyValue(nameof(Total), ref _total, value);
        }

        public string MoneyInWords => CurrencyUtil.ToString(Total);

        [XafDisplayName("Chi tiết hàng")]
        //[ToolTip("")]
        [Association("SalesOrder-OrderItems")]
        public XPCollection<OrderItem> OrderItems
        {
            get
            {
                return GetCollection<OrderItem>(nameof(OrderItems));
            }
        }

        protected override void OnSavingOnly()
        {
            base.OnSavingOnly();
            Total = OrderItems.Sum(i=>i.SubTotal);
        }
    }
}

Lớp đơn hàng chi tiết

using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using YouSoft.Vn.XAFCustom.Core.Entities;
using YouSoft.Vn.XAFCustom.Supports;
using YouSoft.Vn.XAFCustom.Supports.Attributes.Validation;

namespace App.Module.Modules.Inventory
{
    [XafDisplayName("Chi tiết hàng")]
    [ImageName("BO_OrderItem")]
    [VisibleInDashboards]
    [VisibleInReports]
    [LookupEditorMode(LookupEditorMode.AllItemsWithSearch)]
    public partial class OrderItem : YsvObject
    {

        public OrderItem(Session session) : base(session)
        {

        }
        
        [XafDisplayName("")]
        //[ToolTip("")]
        [RequiredOnSave]
        [ModelDefault(nameof(IModelMember.AllowEdit), CoreApp.Consts.False)]
        [Association("SalesOrder-OrderItems")]
        public SalesOrder SalesOrder
        {
            get => _salesOrder;
            set => SetPropertyValue(nameof(SalesOrder), ref _salesOrder, value);
        }

        private SalesOrder _salesOrder;
        private double _subTotal;
        private double _quantity;
        private double _unitPrice;
        private Product _product;

        [XafDisplayName("Món hàng")]
        [RequiredOnSave]
        //[ToolTip("")]
        public Product Product
        {
            get => _product;
            set => SetPropertyValue(nameof(Product), ref _product, value);
        }


        [XafDisplayName("Đơn giá")]
        [ModelDefault(nameof(IModelMember.DisplayFormat), CoreApp.Consts.N0)]
        [ModelDefault(nameof(IModelMember.EditMask), CoreApp.Consts.N0)]
        //[ToolTip("")]
        public double UnitPrice
        {
            get => _unitPrice;
            set => SetPropertyValue(nameof(UnitPrice), ref _unitPrice, value);
        }

        [XafDisplayName("Số lượng")]
        [ModelDefault(nameof(IModelMember.DisplayFormat), CoreApp.Consts.N1)]
        [ModelDefault(nameof(IModelMember.EditMask), CoreApp.Consts.N1)]
        //[ToolTip("")]
        public double Quantity
        {
            get => _quantity;
            set => SetPropertyValue(nameof(Quantity), ref _quantity, value);
        }


        [XafDisplayName("Thành tiền")]
        [ModelDefault(nameof(IModelMember.DisplayFormat), CoreApp.Consts.N0)]
        [ModelDefault(nameof(IModelMember.EditMask), CoreApp.Consts.N0)]
        [ModelDefault(nameof(IModelMember.AllowEdit), CoreApp.Consts.False)]
        //[ToolTip("")]
        public double SubTotal
        {
            get => _subTotal;
            set => SetPropertyValue(nameof(SubTotal), ref _subTotal, value);
        }

        protected override void OnPropertyValueChanged(string propertyName, object oldValue, object newValue)
        {
            base.OnPropertyValueChanged(propertyName, oldValue, newValue);
            switch (propertyName)
            {
                case nameof(Product):
                    UnitPrice = Product?.Price ?? 0;
                    break;
                case nameof(UnitPrice):
                case nameof(Quantity):
                    SubTotal = UnitPrice * Quantity;
                    break;
            }
        }
    }
}

Không có nhận xét nào:

Đăng nhận xét