public OrderPTenantEmailRecipient DeletePropertyTenantEmailRecipient(OrderPTenantEmailRecipient notification, int userId)
 {
     notification.IsDeleted   = true;
     notification.UpdatedBy   = userId;
     notification.DateUpdated = DateTime.UtcNow;
     _applicationContext.Entry(notification).State = EntityState.Modified;
     _applicationContext.SaveChanges();
     return(notification);
 }
        public OrderPTenantEmailRecipient AddPropertyTenantRecipients(int orderId, int shipPropertyId, int propertyTenantId, TenantEmailNotificationQueue lastNotification)
        {
            var existingRecipient = _applicationContext.OrderPTenantEmailRecipients.FirstOrDefault(m => m.PTenantId == propertyTenantId && m.OrderId == orderId);

            if (existingRecipient == null)
            {
                var recipient = new OrderPTenantEmailRecipient()
                {
                    OrderId               = orderId,
                    DateUpdated           = DateTime.UtcNow,
                    PPropertyId           = shipPropertyId,
                    PTenantId             = propertyTenantId,
                    LastEmailNotification = lastNotification
                };
                _applicationContext.Entry(recipient).State = EntityState.Added;
                _applicationContext.SaveChanges();
                return(recipient);
            }
            return(existingRecipient);
        }
        public bool UpdateOrderPTenantEmailRecipients(int?[] accountContactId, int orderId, int UserId)
        {
            if (orderId > 0)
            {
                List <OrderPTenantEmailRecipient> accountids = _currentDbContext.OrderPTenantEmailRecipients.Where(a => a.OrderId == orderId && a.IsDeleted != false).ToList();

                if (accountids.Count > 0)
                {
                    accountids.ForEach(u => u.IsDeleted = true);
                }
                var order = _currentDbContext.Order.Find(orderId);
                if (accountContactId != null && accountContactId.Length > 0)
                {
                    foreach (var item in accountContactId)
                    {
                        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);
                    }
                }
                _currentDbContext.SaveChanges();
            }
            return(true);
        }
示例#4
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);
        }
示例#5
0
        public Order SaveSalesOrder(Order order, OrderRecipientInfo shipmentAndRecipientInfo, int tenantId, int warehouseId,
                                    int userId, List <OrderDetail> orderDetails = null, List <OrderNotes> orderNotes = null)
        {
            decimal total = 0;

            if (orderDetails != null)
            {
                var toAdd  = orderDetails.Where(a => a.OrderDetailID < 0).ToList();
                var cItems = orderDetails.Where(a => a.OrderDetailID > 0).ToList();

                var toDelete = _currentDbContext.OrderDetail.Where(a => a.OrderID == order.OrderID && a.IsDeleted != true).Select(a => a.OrderDetailID).Except(cItems.Select(a => a.OrderDetailID).ToList());
                foreach (var item in toDelete)
                {
                    var dItem = _currentDbContext.OrderDetail.FirstOrDefault(a => a.OrderDetailID == item);
                    dItem.IsDeleted = true;
                }

                foreach (var item in toAdd)
                {
                    int?taxId      = item.TaxID;
                    int?warrantyId = item.WarrantyID;
                    int productId  = item.ProductId;
                    item.DateCreated = DateTime.UtcNow;
                    item.CreatedBy   = userId;
                    item.TenentId    = tenantId;
                    item.OrderID     = order.OrderID;
                    total            = total + item.TotalAmount;
                    item.TenentId    = tenantId;
                    item.WarehouseId = warehouseId;
                    item.IsDeleted   = null;

                    if (item.WarrantyID > 0)
                    {
                        bool warrantyTypePercent = false;
                        var  warranty            = _currentDbContext.TenantWarranty.AsNoTracking().FirstOrDefault(x => x.WarrantyID == item.WarrantyID);
                        if (warranty != null)
                        {
                            warrantyTypePercent = warranty.IsPercent;
                        }
                        if (warrantyTypePercent)
                        {
                            item.WarrantyAmount = (item.TotalAmount / 100) * item.Warranty.PercentageOfPrice;
                        }
                        else
                        {
                            item.WarrantyAmount = item.Warranty.FixedPrice;
                        }
                    }

                    // fix navigation issue
                    item.ProductMaster = null;
                    item.TaxName       = null;
                    item.Warranty      = null;

                    item.ProductId  = productId;
                    item.TaxID      = taxId;
                    item.WarrantyID = warrantyId;

                    if (item.OrderDetailID > 0)
                    {
                        _currentDbContext.Entry(item).State = EntityState.Modified;
                    }
                    else
                    {
                        _currentDbContext.OrderDetail.Add(item);
                    }

                    _currentDbContext.SaveChanges();
                }

                foreach (var item in cItems)
                {
                    var orderDetail = _currentDbContext.OrderDetail.Find(item.OrderDetailID);

                    orderDetail.ProductId      = item.ProductId;
                    orderDetail.ExpectedDate   = item.ExpectedDate;
                    orderDetail.Qty            = item.Qty;
                    orderDetail.Price          = item.Price;
                    orderDetail.WarrantyID     = item.WarrantyID;
                    orderDetail.TaxID          = item.TaxID;
                    orderDetail.Notes          = item.Notes;
                    orderDetail.TaxAmount      = item.TaxAmount;
                    orderDetail.TotalAmount    = item.TotalAmount;
                    orderDetail.WarrantyAmount = item.WarrantyAmount;

                    _currentDbContext.Entry(orderDetail).State = EntityState.Modified;
                    total = total + item.TotalAmount;
                }

                _currentDbContext.SaveChanges();
            }

            else
            {
                foreach (var item in _currentDbContext.OrderDetail.Where(a => a.OrderID == order.OrderID && a.IsDeleted != true))
                {
                    item.IsDeleted = true;
                }
            }
            if (orderNotes != null)
            {
                if (orderNotes.Count == 0)
                {
                    var cItems = _currentDbContext.OrderNotes.Where(a => a.OrderID == order.OrderID && a.IsDeleted != true && a.TenantId == tenantId).ToList();
                    cItems.ForEach(m =>
                    {
                        m.DateUpdated = DateTime.UtcNow;
                        m.UpdatedBy   = userId;
                        m.IsDeleted   = true;
                    });
                }
                else
                {
                    orderNotes.Where(a => a.OrderNoteId < 0).ToList().ForEach(m =>
                    {
                        m.DateCreated = DateTime.UtcNow;
                        m.CreatedBy   = userId;
                        m.TenantId    = tenantId;
                        m.OrderID     = order.OrderID;
                        _currentDbContext.OrderNotes.Add(m);
                        _currentDbContext.SaveChanges();
                    });
                }
            }
            order.OrderTotal = total;

            if (shipmentAndRecipientInfo.AddAddressToAccount == true && shipmentAndRecipientInfo.ShipmentAddressLine1 != null && 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);
            }

            var obj = _currentDbContext.Order.Find(order.OrderID);

            if (obj != null)
            {
                _currentDbContext.Entry(obj).State = System.Data.Entity.EntityState.Detached;
                order.CreatedBy   = obj.CreatedBy;
                order.DateCreated = obj.DateCreated;
                //order.OrderStatusID = obj.OrderStatusID;
                order.TenentId    = obj.TenentId;
                order.WarehouseId = obj.WarehouseId;
                if (order.ShipmentAccountAddressId < 1)
                {
                    order.ShipmentAccountAddressId = (int?)null;
                }
                order.InventoryTransactionTypeId = obj.InventoryTransactionTypeId;
                obj.OrderNumber = order.OrderNumber.Trim();
                obj.DateUpdated = DateTime.UtcNow;
                obj.UpdatedBy   = userId;
                obj.WarehouseId = warehouseId;
                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)
                {
                    List <OrderPTenantEmailRecipient> accountids = _currentDbContext.OrderPTenantEmailRecipients.Where(a => a.OrderId == order.OrderID && a.IsDeleted != false).ToList();

                    if (accountids.Count > 0)
                    {
                        accountids.ForEach(u => u.IsDeleted = true);
                    }

                    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);
                        }
                    }
                }
                else
                {
                    List <OrderPTenantEmailRecipient> accountids = _currentDbContext.OrderPTenantEmailRecipients.Where(a => a.OrderId == order.OrderID && a.IsDeleted != false).ToList();

                    if (accountids.Count > 0)
                    {
                        accountids.ForEach(u => u.IsDeleted = true);
                    }
                }

                #endregion

                _currentDbContext.Order.Attach(order);
                _currentDbContext.Entry(order).State = EntityState.Modified;
            }

            _currentDbContext.SaveChanges();
            Inventory.StockRecalculateByOrderId(order.OrderID, warehouseId, tenantId, caCurrent.CurrentUser().UserId);
            return(obj);
        }
        public Order SaveWorksOrder(Order order, OrderRecipientInfo shipmentAndRecipientInfo, int tenantId, int warehouseId, int userId, bool isOrderComplete, List <OrderDetail> orderDetails = null, IEnumerable <OrderNotes> orderNotes = null)
        {
            order.OrderNumber = order.OrderNumber.Trim();
            order.DateUpdated = DateTime.UtcNow;
            order.UpdatedBy   = userId;
            order.WarehouseId = warehouseId;

            decimal total = 0;

            if (orderDetails != null)
            {
                var toAdd  = orderDetails.Where(a => a.OrderDetailID < 0).ToList();
                var cItems = orderDetails.Where(a => a.OrderDetailID > 0).ToList();

                var recentlyUpdatedIds = new List <int>();
                foreach (var item in toAdd)
                {
                    int?taxId      = item.TaxID;
                    int?warrantyId = item.WarrantyID;
                    int productId  = item.ProductId;
                    item.DateCreated = DateTime.UtcNow;
                    item.CreatedBy   = userId;
                    item.TenentId    = tenantId;
                    item.OrderID     = order.OrderID;
                    total            = total + item.TotalAmount;
                    item.TenentId    = tenantId;
                    item.WarehouseId = warehouseId;
                    total            = total + item.TotalAmount;

                    if (item.WarrantyID > 0)
                    {
                        var warranty = _currentDbContext.TenantWarranty.Find(item.WarrantyID);
                        if (warranty.IsPercent)
                        {
                            item.WarrantyAmount = (item.TotalAmount / 100) * item.Warranty.PercentageOfPrice;
                        }
                        else
                        {
                            item.WarrantyAmount = item.Warranty.FixedPrice;
                        }
                    }

                    // fix navigation issue
                    item.ProductMaster = null;
                    item.TaxName       = null;
                    item.Warranty      = null;

                    item.ProductId  = productId;
                    item.TaxID      = taxId;
                    item.WarrantyID = warrantyId;

                    if (item.OrderDetailID > 0)
                    {
                        _currentDbContext.Entry(item).State = EntityState.Modified;
                    }
                    else
                    {
                        _currentDbContext.OrderDetail.Add(item);
                    }
                    _currentDbContext.SaveChanges();
                    recentlyUpdatedIds.Add(item.OrderDetailID);
                }

                var toDelete = _currentDbContext.OrderDetail.Where(a => a.OrderID == order.OrderID && a.IsDeleted != true)
                               .Select(a => a.OrderDetailID)
                               .Except(cItems.Select(a => a.OrderDetailID).ToList().Union(recentlyUpdatedIds));

                foreach (var item in toDelete)
                {
                    var dItem = _currentDbContext.OrderDetail.FirstOrDefault(a => a.OrderDetailID == item);
                    dItem.IsDeleted = true;
                }

                foreach (var item in cItems)
                {
                    var orderDetail = _currentDbContext.OrderDetail.Find(item.OrderDetailID);
                    orderDetail.ProductId      = item.ProductId;
                    orderDetail.ExpectedDate   = item.ExpectedDate;
                    orderDetail.Qty            = item.Qty;
                    orderDetail.Price          = item.Price;
                    orderDetail.WarrantyID     = item.WarrantyID;
                    orderDetail.TaxID          = item.TaxID;
                    orderDetail.Notes          = item.Notes;
                    orderDetail.TaxAmount      = item.TaxAmount;
                    orderDetail.TotalAmount    = item.TotalAmount;
                    orderDetail.WarrantyAmount = item.WarrantyAmount;

                    _currentDbContext.Entry(orderDetail).State = EntityState.Modified;
                    total = total + item.TotalAmount;
                }
            }
            else
            {
                foreach (var item in _currentDbContext.OrderDetail.Where(a => a.OrderID == order.OrderID && a.IsDeleted != true))
                {
                    item.IsDeleted = true;
                }
            }
            if (orderNotes != null)
            {
                if (orderNotes.Count() == 0)
                {
                    var cItems = _currentDbContext.OrderNotes
                                 .Where(
                        a => a.OrderID == order.OrderID && a.IsDeleted != true && a.TenantId == tenantId)
                                 .ToList();
                    cItems.ForEach(m =>
                    {
                        m.DateUpdated = DateTime.UtcNow;
                        m.UpdatedBy   = userId;
                        m.IsDeleted   = true;
                    });
                }
                else
                {
                    orderNotes.Where(a => a.OrderNoteId < 0)
                    .ToList()
                    .ForEach(m =>
                    {
                        m.DateCreated =
                            DateTime.UtcNow;
                        m.CreatedBy = userId;
                        m.TenantId  = tenantId;
                        m.OrderID   = order.OrderID;
                        _currentDbContext.OrderNotes.Add(m);
                    });
                }
            }

            order.OrderTotal = total;
            var obj = _currentDbContext.Order.Find(order.OrderID);

            if (obj != null)
            {
                _currentDbContext.Entry(obj).State = EntityState.Detached;
                order.CreatedBy   = obj.CreatedBy;
                order.DateCreated = obj.DateCreated;
                if (order.OrderStatusID == 0)
                {
                    order.OrderStatusID = obj.OrderStatusID;
                }
                order.TenentId    = obj.TenentId;
                order.WarehouseId = obj.WarehouseId;
                order.InventoryTransactionTypeId = obj.InventoryTransactionTypeId;

                obj.OrderNumber = order.OrderNumber.Trim();
                obj.DateUpdated = DateTime.UtcNow;
                obj.UpdatedBy   = userId;
                obj.WarehouseId = warehouseId;
                _currentDbContext.Order.Attach(order);
                var entry = _currentDbContext.Entry(order);
                entry.State = EntityState.Modified;
            }

            if (!isOrderComplete && shipmentAndRecipientInfo.PropertyTenantIds != null)
            {
                foreach (var item in shipmentAndRecipientInfo.PropertyTenantIds)
                {
                    var ptenantId = item.AsInt();
                    if (!_currentDbContext.OrderPTenantEmailRecipients.Any(m => m.PTenantId == ptenantId && m.OrderId == order.OrderID))
                    {
                        var recipient = new OrderPTenantEmailRecipient()
                        {
                            OrderId     = order.OrderID,
                            DateUpdated = DateTime.UtcNow,
                            PPropertyId = order.ShipmentPropertyId,
                            PTenantId   = item.AsInt()
                        };
                        _currentDbContext.Entry(recipient).State = EntityState.Added;
                        _currentDbContext.SaveChanges();
                    }
                }
            }

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