using Bp.Sync.Domain.Elastics.BaseEntities;
using System;

namespace Bp.Sync.Domain.Elastics.Inventories
{
    public class ProductInvoiceElastic : ElasticBaseEntityWithKey<long>
    {
        /// <summary>
        /// Mã hóa đơn
        /// </summary>
        public long InvoiceId { get; set; }
        /// <summary>
        /// Id sản phẩm
        /// </summary>
        public long ProductId { get; set; }
        /// <summary>
        /// Id đơn vị tính
        /// </summary>
        public int UnitId { get; set; }
        /// <summary>
        /// Mã lưu kho
        /// </summary>
        public string InventoryCode { get; set; }
        /// <summary>
        /// Số lượng
        /// </summary>
        public decimal Quantity { get; set; }
        /// <summary>
        /// Giá bán
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// Chiết khấu 
        /// </summary>
        public decimal Discount { get; set; }
        /// <summary>
        /// Thuế VAT
        /// </summary>
        public decimal Tax { get; set; }
        public long RelateId { get; set; }

        public decimal Amount
        {
            get
            {
                var a = Price * Quantity;
                if (Discount > 0) a *= 1 - Discount / 100m;
                if (Tax > 0) a *= 1 + Tax / 100m;
                a = Math.Round(a, 4);
                return a;
            }
        }


        public decimal? ProductInventoryCostPrice { get; set; }

        /// <summary>
        /// Giá sản phẩm khi nhập kho
        /// </summary>
        public decimal? TotalProductInventoryCostPrice
        {
            get
            {
                if (ProductInventoryCostPrice.HasValue)
                    return ProductInventoryCostPrice.Value * Quantity;
                else return null;
            }
        }
        public decimal? Revenue
        {
            get
            {
                if (ProductInventoryCostPrice.HasValue)
                    return Amount - ProductInventoryCostPrice.Value * Quantity;
                else return null;
            }
        }


        /// <summary>
        /// thoong tin invoice
        /// </summary>
        public Entities.Inventories.Invoices.InvoiceType InvoiceType { get; set; }
        public int InvoiceBranchId { get; set; }
        /// <summary>
        /// Mã khách hàng
        /// </summary>
        public int InvoiceCustomerId { get; set; }
        public int? InvoiceCreatedBy { get; set; }

        public string InvoiceCreatedByUserName { get; set; }

        public int? InvoiceUpdatedBy { get; set; }

        public string InvoiceUpdatedByUserName { get; set; }
        public DateTimeOffset? InvoiceCreatedTime { get; set; }

        public DateTimeOffset? InvoiceUpdatedTime { get; set; }

        public bool InvoiceIsDeleted { get; set; }

        //sanr phamr
        public string ProductName { get; set; }
        public string ProductCode { get; set; }

        public string UnitName { get; set; }
        public long? ProductUnitId { get; set; }
        public string ProductUnitName { get; set; }


    }
}
