private Error validateModel(SalesOrderDetailModel model)
        {
            var error = isValidRequiredInt(model.ProductId, "ProductName", EvolutionResources.errAValidItemMustBeSelected);

            if (!error.IsError)
            {
                error = isValidNonRequiredString(getFieldValue(model.ProductDescription), 255, "ProductDescription", EvolutionResources.errTextDataRequiredInField);
            }

            return(error);
        }
Example #2
0
        public AllocationListModel FindAllocationsForSale(SalesOrderDetailModel sod)
        {
            AllocationListModel model = new AllocationListModel();

            foreach (var item in db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
                     .OrderBy(a => a.SaleLineId)
                     .ThenBy(a => a.DateCreated))
            {
                var alloc = MapToModel(item);
                model.Items.Add(alloc);
            }
            return(model);
        }
        public Error InsertOrUpdateSalesOrderDetail(SalesOrderDetailModel sod, string lockGuid)
        {
            var error = validateModel(sod);

            if (!error.IsError)
            {
                // Check that the lock is still current
                if (!db.IsLockStillValid(typeof(SalesOrderDetailTemp).ToString(), sod.Id, lockGuid))
                {
                    error.SetError(EvolutionResources.errRecordChangedByAnotherUser, "ProductName");
                }
                else
                {
                    SalesOrderDetail temp = null;
                    if (sod.Id != 0)
                    {
                        // Existing record to be updated
                        temp = db.FindSalesOrderDetail(sod.Id);

                        // The following fields are not copied:
                        //      OriginalRowId
                        //      LineNumber
                        Mapper.Map <SalesOrderDetailModel, SalesOrderDetail>(sod, temp);
                        if (temp.LineNumber == null)
                        {
                            temp.LineNumber = db.GetNextSalesOrderDetailLineNumber(temp.SalesOrderHeaderId, true);
                        }
                    }
                    else
                    {
                        // New record, so copy values

                        // The following fields are not copied:
                        //      OriginalRowId
                        //      LineNumber
                        temp = Mapper.Map <SalesOrderDetailModel, SalesOrderDetail>(sod);

                        temp.OriginalRowId = sod.OriginalRowId;
                        temp.LineNumber    = db.GetNextSalesOrderDetailLineNumber(temp.SalesOrderHeaderId, true);
                    }

                    db.InsertOrUpdateSalesOrderDetail(temp);
                    sod.Id = temp.Id;
                }
            }
            return(error);
        }
Example #4
0
        public DateTimeOffset?CalculateExpectedCompletionDate(SalesOrderDetailModel sod)
        {
            DateTimeOffset?result   = null;
            DateTimeOffset nullDate = new DateTimeOffset(1899, 1, 1, 0, 0, 0, new TimeSpan());
            DateTimeOffset?dteNow   = null;
            DateTimeOffset?dteEver  = db.FindAllocationECD(sod.Id).FirstOrDefault();

            var cnt = db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
                      .Count();

            if (cnt > 0)
            {
                dteNow = DateTimeOffset.Now;
            }
            else
            {
                dteNow = nullDate;
            }

            if ((dteEver == null || dteEver == nullDate) &&
                (dteNow == null || dteNow == nullDate))
            {
                result = null;
            }
            else
            {
                if (dteEver > dteNow)
                {
                    result = dteEver;
                }
                else
                {
                    result = dteNow;
                }
            }
            if (result == nullDate)
            {
                result = null;
            }

            return(result);
        }
Example #5
0
 public string GetExpectedShipdate(SalesOrderDetailModel sod)
 {
     if (!IsAllocated(sod))
     {
         // Not allocated
         return("TBA");
     }
     else
     {
         // It is allocated, but is it to stock or a purchase ?
         var allocs = db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
                      .Where(a => a.PurchaseLineId == null)
                      .FirstOrDefault();
         if (allocs == null)
         {
             // Allocated to stock
             return("");
         }
         else
         {
             // Allocated to a purchase
             allocs = db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
                      .Where(a => a.PurchaseLineId != null &&
                             a.PurchaseOrderDetail.PurchaseOrderHeader.RequiredDate != null)
                      .FirstOrDefault();
             if (allocs != null)
             {
                 return(allocs.PurchaseOrderDetail.PurchaseOrderHeader.RequiredDate.ISODate());
             }
             else
             {
                 return("TBA");
             }
         }
     }
 }
Example #6
0
        public void IntegrationTest()
        {
            var connection = TestSession.GetConnection();

            connection.Open();
            #region good insertion and select by id test
            SalesOrderDetailModel inserted = new SalesOrderDetailModel();
            inserted.SalesOrderID          = TestSession.Random.Next();
            inserted.CarrierTrackingNumber = TestSession.Random.RandomString(25);
            inserted.OrderQty          = TestSession.Random.RandomShort();
            inserted.ProductID         = TestSession.Random.Next();
            inserted.SpecialOfferID    = TestSession.Random.Next();
            inserted.UnitPrice         = TestSession.Random.RandomDecimal();
            inserted.UnitPriceDiscount = TestSession.Random.RandomDecimal();
            inserted.LineTotal         = TestSession.Random.RandomDecimal();
            inserted.rowguid           = Guid.NewGuid();
            inserted.ModifiedDate      = TestSession.Random.RandomDateTime();

            _tested.Insert(connection, new[] { inserted });

            var selectedAfterInsertion = _tested.GetByPrimaryKey(connection, new SalesOrderDetailModelPrimaryKey()
            {
                SalesOrderID       = inserted.SalesOrderID,
                SalesOrderDetailID = inserted.SalesOrderDetailID,
            });

            CollectionAssert.IsNotEmpty(selectedAfterInsertion);
            var selectedAfterInsert = selectedAfterInsertion.Single();
            Assert.AreEqual(inserted.SalesOrderID, selectedAfterInsert.SalesOrderID);
            Assert.AreEqual(inserted.SalesOrderDetailID, selectedAfterInsert.SalesOrderDetailID);
            Assert.AreEqual(inserted.CarrierTrackingNumber, selectedAfterInsert.CarrierTrackingNumber);
            Assert.AreEqual(inserted.OrderQty, selectedAfterInsert.OrderQty);
            Assert.AreEqual(inserted.ProductID, selectedAfterInsert.ProductID);
            Assert.AreEqual(inserted.SpecialOfferID, selectedAfterInsert.SpecialOfferID);
            Assert.AreEqual(inserted.UnitPrice, selectedAfterInsert.UnitPrice);
            Assert.AreEqual(inserted.UnitPriceDiscount, selectedAfterInsert.UnitPriceDiscount);
            Assert.AreEqual(inserted.LineTotal, selectedAfterInsert.LineTotal);
            Assert.AreEqual(inserted.rowguid, selectedAfterInsert.rowguid);
            Assert.AreEqual(inserted.ModifiedDate, selectedAfterInsert.ModifiedDate);

            #endregion

            #region update and select by id test
            inserted.CarrierTrackingNumber = TestSession.Random.RandomString(25);
            inserted.OrderQty          = TestSession.Random.RandomShort();
            inserted.ProductID         = TestSession.Random.Next();
            inserted.SpecialOfferID    = TestSession.Random.Next();
            inserted.UnitPrice         = TestSession.Random.RandomDecimal();
            inserted.UnitPriceDiscount = TestSession.Random.RandomDecimal();
            inserted.LineTotal         = TestSession.Random.RandomDecimal();
            inserted.rowguid           = Guid.NewGuid();
            inserted.ModifiedDate      = TestSession.Random.RandomDateTime();

            _tested.Update(connection, new[] { inserted });

            var selectedAfterUpdateAddresss = _tested.GetByPrimaryKey(connection, new SalesOrderDetailModelPrimaryKey()
            {
                SalesOrderID       = inserted.SalesOrderID,
                SalesOrderDetailID = inserted.SalesOrderDetailID,
            });

            CollectionAssert.IsNotEmpty(selectedAfterUpdateAddresss);
            var selectedAfterUpdate = selectedAfterUpdateAddresss.Single();
            Assert.AreEqual(inserted.SalesOrderID, selectedAfterUpdate.SalesOrderID);
            Assert.AreEqual(inserted.SalesOrderDetailID, selectedAfterUpdate.SalesOrderDetailID);
            Assert.AreEqual(inserted.CarrierTrackingNumber, selectedAfterUpdate.CarrierTrackingNumber);
            Assert.AreEqual(inserted.OrderQty, selectedAfterUpdate.OrderQty);
            Assert.AreEqual(inserted.ProductID, selectedAfterUpdate.ProductID);
            Assert.AreEqual(inserted.SpecialOfferID, selectedAfterUpdate.SpecialOfferID);
            Assert.AreEqual(inserted.UnitPrice, selectedAfterUpdate.UnitPrice);
            Assert.AreEqual(inserted.UnitPriceDiscount, selectedAfterUpdate.UnitPriceDiscount);
            Assert.AreEqual(inserted.LineTotal, selectedAfterUpdate.LineTotal);
            Assert.AreEqual(inserted.rowguid, selectedAfterUpdate.rowguid);
            Assert.AreEqual(inserted.ModifiedDate, selectedAfterUpdate.ModifiedDate);

            #endregion

            #region delete test
            _tested.Delete(connection, new[] { inserted });
            var selectedAfterDeleteAddresss = _tested.GetByPrimaryKey(connection, new SalesOrderDetailModelPrimaryKey()
            {
                SalesOrderID       = inserted.SalesOrderID,
                SalesOrderDetailID = inserted.SalesOrderDetailID,
            });
            CollectionAssert.IsEmpty(selectedAfterDeleteAddresss);
            #endregion
            connection.Close();
        }
        public SalesOrderDetailModel MapToModel(SalesOrderDetailModel item)
        {
            var newItem = Mapper.Map <SalesOrderDetailModel, SalesOrderDetailModel>(item);

            return(newItem);
        }
Example #8
0
 public bool IsAllocated(SalesOrderDetailModel sod)
 {
     return(db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
            .Count() > 0);
 }
Example #9
0
 public bool IsAllocatedToPurchaseOrder(SalesOrderDetailModel sod)
 {
     return(db.FindAllocationsForSalesOrderDetail(sod.CompanyId, sod.Id)
            .Where(a => a.PurchaseLineId != null)
            .Count() > 0);
 }
Example #10
0
        public Error ImportSales(CompanyModel company,
                                 UserModel user,
                                 int soStatusId,
                                 int sourceId,
                                 List <string> headings,
                                 string dateFormat,
                                 int tz)
        {
            var error = new Error();

            int lastCustomerId        = -1,
                lineNo                = 0;
            SalesOrderHeaderModel soh = null;

            var lineStatus = db.FindSalesOrderLineStatuses()
                             .Where(s => s.StatusName.ToLower() == "unpicked")
                             .FirstOrDefault();

            var nextAction = db.FindSaleNextActions()
                             .Where(na => na.Id == (int)Enumerations.SaleNextAction.None)
                             .FirstOrDefault();

            // Create an order for each supplier
            foreach (var row in db.FindFileImportRows(company.Id, user.Id)
                     .Skip(1)                      // Skip first record (headers)
                     .OrderBy(r => r.CustomerId)
                     .ToList())
            {
                if (row.CustomerId != lastCustomerId)
                {
                    // Found another supplier, so start a new order
                    soh = new SalesOrderHeaderModel {
                        CompanyId          = company.Id,
                        SourceId           = sourceId,
                        CustomerId         = row.CustomerId,
                        OrderNumber        = (int)LookupService.GetNextSequenceNumber(company, SequenceNumberType.SalesOrderNumber),
                        OrderDate          = DateTimeOffset.Now,
                        SOStatus           = soStatusId,
                        SalespersonId      = user.Id,
                        IsConfirmedAddress = false,
                        IsManualFreight    = false,
                        DateCreated        = DateTimeOffset.Now,
                    };

                    soh.EndUserName = getFieldValue(row, "EndUserName");

                    soh.ShipAddress1  = getFieldValue(row, "ShipAddress1");
                    soh.ShipSuburb    = getFieldValue(row, "ShipSuburb");
                    soh.ShipState     = getFieldValue(row, "ShipState");
                    soh.ShipPostcode  = getFieldValue(row, "ShipPostcode");
                    soh.ShipCountryId = db.FindCountry(getFieldValue(row, "ShipCountry")).Id;
                    soh.LocationId    = db.FindLocation(company.Id, getFieldValue(row, "LocationId")).Id;

                    soh.WarehouseInstructions = getFieldValue(row, "WarehouseInstructions");

                    soh.CustPO = getFieldValue(row, "CustPO");

                    soh.DeliveryWindowOpen  = getFieldValue(row, "DeliveryWindowOpen").PadLeft(10, '0').ParseDateTime(dateFormat, tz);
                    soh.DeliveryWindowClose = getFieldValue(row, "DeliveryWindowClose").PadLeft(10, '0').ParseDateTime(dateFormat, tz);

                    soh.ManualDWSet  = getFieldValue(row, "ManualDWSet").ParseBool();
                    soh.NextActionId = nextAction.Id;

                    soh.DeliveryInstructions = getFieldValue(row, "DeliveryInstructions");
                    soh.DeliveryContact      = getFieldValue(row, "DeliveryContact");

                    soh.BrandCategoryId = db.FindBrandCategory(company.Id, getFieldValue(row, "BrandCategory")).Id;

                    InsertOrUpdateSalesOrderHeader(soh, user, "");

                    lastCustomerId = row.CustomerId.Value;
                    lineNo         = 1000;
                }

                // Add items to the new order
                var sod = new SalesOrderDetailModel {
                    CompanyId          = company.Id,
                    SalesOrderHeaderId = soh.Id,
                    LineNumber         = lineNo,
                    ProductId          = row.ProductId,
                    ProductDescription = row.Product.ItemName,
                    UnitPriceExTax     = Convert.ToDecimal(getField(row, "UnitPriceExTax").Value),
                    DiscountPercent    = Convert.ToDecimal(getField(row, "DiscountPercent").Value),
                    TaxCodeId          = null,
                    OrderQty           = Convert.ToInt32(getField(row, "Quantity").Value),
                    LineStatusId       = null
                };
                if (row.Supplier != null)
                {
                    sod.TaxCodeId = row.Supplier.TaxCodeId.Value;
                }
                if (lineStatus != null)
                {
                    sod.LineStatusId = lineStatus.Id;
                }

                InsertOrUpdateSalesOrderDetail(sod, "");

                lineNo += 1000;
            }

            return(error);
        }