public ProductSaleQueryResponse GetProductPriceThresholdByAccountId(int productId, int?accountId = null)
        {
            if (!accountId.HasValue || accountId == 0)
            {
                return(GetProductSalePriceById(productId));
            }

            var product = GetProductMasterWithSpecialPrice(productId, accountId ?? 0);

            var minSellPrice = GetPercentageMarginPrice(product.LandedCost, product.PercentMargin);

            var minThresholdPrice = product.MinThresholdPrice ?? (minSellPrice <= 0 ? product.SellPrice : minSellPrice) ?? 0;

            var account = _accountServices.GetAccountsById(accountId.Value);

            var minSellPriceForAccount = GetProductPriceByAccountId(productId, accountId);

            var finalThresholdPrice = new[] { minThresholdPrice, minSellPriceForAccount, minSellPrice }.Min();

            var thresholdInfo = new ProductSaleQueryResponse()
            {
                MinimumThresholdPrice = ConvertBaseRates(accountId ?? 0, finalThresholdPrice), SellPrice = ConvertBaseRates(accountId ?? 0, product.SellPrice ?? 0), LandingCost = ConvertBaseRates(accountId ?? 0, product.LandedCost ?? 0), LandingCostWithMargin = ConvertBaseRates(accountId ?? 0, minSellPrice), PriceGroupID = account.PriceGroupID, PriceGroupPercent = account.TenantPriceGroups.Percent, ProfitMargin = product.PercentMargin
            };

            return(LoadThresholdInfo(thresholdInfo, product));
        }
示例#2
0
        public Order CreateSalesOrder(Order order, OrderRecipientInfo shipmentAndRecipientInfo, int tenantId, int warehouseId,
                                      int userId, List <OrderDetail> orderDetails = null, List <OrderNotes> orderNotes = null)
        {
            order.OrderNumber = order.OrderNumber.Trim();
            var duplicateOrder = _currentDbContext.Order.FirstOrDefault(m => m.OrderNumber.Equals(order.OrderNumber, StringComparison.CurrentCultureIgnoreCase));

            if (duplicateOrder != null)
            {
                throw new Exception($"Order Number {order.OrderNumber} already associated with another Order. Please regenerate order number.", new Exception("Duplicate Order Number"));
            }

            order.IssueDate     = DateTime.UtcNow;
            order.OrderStatusID = (int)OrderStatusEnum.Active;
            order.DateCreated   = DateTime.UtcNow;
            order.DateUpdated   = DateTime.UtcNow;
            order.TenentId      = tenantId;
            order.CreatedBy     = userId;
            order.UpdatedBy     = userId;
            order.WarehouseId   = warehouseId;
            if (order.AccountID > 0)
            {
                var account = _currentDbContext.Account.Find(order.AccountID);
                if (account != null)
                {
                    order.AccountCurrencyID = account.CurrencyID;
                }
            }
            if (!caCurrent.CurrentWarehouse().AutoAllowProcess)
            {
                order.OrderStatusID = _currentDbContext.OrderStatus.First(a => a.OrderStatusID == (int)OrderStatusEnum.Hold).OrderStatusID;
            }
            else
            {
                order.OrderStatusID = _currentDbContext.OrderStatus.First(a => a.OrderStatusID == (int)OrderStatusEnum.Active).OrderStatusID;
            }
            _currentDbContext.Order.Add(order);


            if (orderDetails != null)
            {
                if (orderDetails.Any(m => m.OrderDetailStatusId == (int)OrderStatusEnum.AwaitingAuthorisation))
                {
                    order.OrderStatusID = (int)OrderStatusEnum.AwaitingAuthorisation;
                }

                decimal?ordTotal = 0;
                foreach (var item in orderDetails)
                {
                    item.DateCreated   = DateTime.UtcNow;
                    item.CreatedBy     = userId;
                    item.OrderID       = order.OrderID;
                    item.TenentId      = tenantId;
                    item.SortOrder     = item.ProductMaster?.ProductGroup?.SortOrder ?? 0;
                    item.ProductMaster = null;
                    item.TaxName       = null;
                    item.Warranty      = null;
                    item.WarehouseId   = warehouseId;
                    order.OrderDetails.Add(item);
                    ordTotal = ordTotal + ((item.Price * item.Qty) + item.TaxAmount);
                }

                order.OrderTotal = (decimal)ordTotal;
            }
            if (orderNotes != null)
            {
                foreach (var item in orderNotes)
                {
                    item.DateCreated = DateTime.UtcNow;
                    item.CreatedBy   = userId;
                    item.OrderID     = order.OrderID;
                    item.TenantId    = tenantId;
                    order.OrderNotes.Add(item);
                }
            }
            if (shipmentAndRecipientInfo.AddAddressToAccount == true && order.AccountID > 0 && (!order.ShipmentAccountAddressId.HasValue || order.ShipmentAccountAddressId < 1))
            {
                var account        = _accountServices.GetAccountsById(order.AccountID.Value);
                var accountAddress = new AccountAddresses()
                {
                    AccountID       = order.AccountID.Value,
                    AddressLine1    = shipmentAndRecipientInfo.ShipmentAddressLine1,
                    AddressLine2    = shipmentAndRecipientInfo.ShipmentAddressLine2,
                    AddressLine3    = shipmentAndRecipientInfo.ShipmentAddressLine3,
                    Town            = shipmentAndRecipientInfo.ShipmentAddressLine4,
                    PostCode        = shipmentAndRecipientInfo.ShipmentAddressPostcode,
                    AddTypeShipping = true,
                    CountryID       = account.CountryID,
                    CreatedBy       = userId,
                    DateCreated     = DateTime.UtcNow,
                    IsActive        = true,
                    Name            = account.CompanyName
                };
                _currentDbContext.AccountAddresses.Add(accountAddress);
            }
            if (order.ShipmentAccountAddressId < 1)
            {
                order.ShipmentAccountAddressId = (int?)null;
            }

            if (order.ConsignmentTypeId == 4)
            {
                order.ShipmentAddressLine1     = _currentDbContext.ConsignmentTypes.FirstOrDefault(u => u.ConsignmentTypeId == 4)?.ConsignmentType;
                order.ShipmentAddressLine2     = null;
                order.ShipmentAddressLine3     = null;
                order.ShipmentAddressLine4     = null;
                order.ShipmentAddressPostcode  = null;
                order.ShipmentAccountAddressId = (int?)null;
            }


            #region SendEmailWithAttachment
            if (shipmentAndRecipientInfo.SendEmailWithAttachment)
            {
                if (shipmentAndRecipientInfo.AccountEmailContacts != null && shipmentAndRecipientInfo.AccountEmailContacts.Length > 0)
                {
                    foreach (var item in shipmentAndRecipientInfo.AccountEmailContacts)
                    {
                        string email = "";

                        var secondryAddress = _currentDbContext.AccountContacts.Where(u => u.AccountContactId == item).AsNoTracking().FirstOrDefault();
                        if (secondryAddress != null)
                        {
                            email = secondryAddress.ContactEmail;
                        }

                        var recipient = new OrderPTenantEmailRecipient()
                        {
                            OrderId          = order.OrderID,
                            EmailAddress     = email,
                            AccountContactId = item,
                            UpdatedBy        = userId,
                            DateUpdated      = DateTime.UtcNow,
                        };

                        _currentDbContext.OrderPTenantEmailRecipients.Add(recipient);
                    }
                }
            }
            #endregion

            _currentDbContext.SaveChanges();
            Inventory.StockRecalculateByOrderId(order.OrderID, warehouseId, tenantId, caCurrent.CurrentUser().UserId);

            return(order);
        }