public void CheckQuantityOnOrderAndQuantityCommitted()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.RrpInclTax = 7.75M;
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InvoiceDto saleOrder = this.GetSaleOrder(dto1);
            CrudProxy invoice = new InvoiceProxy();
            invoice.Insert(saleOrder);

            dto1 = (InventoryItemDto) proxy.GetByUid(dto1.Uid);
            Assert.AreEqual(5, dto1.QuantityCommitted, "Incorrect # of stocks committed (used by sale orders)");

            InvoiceDto po = this.GetPurchaseOrder(dto1);
            po.PurchaseOrderNumber = "<Auto Number>";
            invoice.Insert(po);

            dto1 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);
            Assert.AreEqual(5, dto1.QuantityCommitted, "Incorrect # of stocks committed (used by sale orders)");

            dto1 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);
            Assert.AreEqual(20, dto1.QuantityOnOrder, "Incorrect # of stocks on order(used by purchase orders)");
        }
        public void Delete()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            proxy.Insert(dto1);

            proxy.DeleteByUid(dto1.Uid);

            try
            {
                proxy.GetByUid(dto1.Uid);
            }
            catch(RestException ex)
            {
                Assert.AreEqual("RecordNotFoundException", ex.Type);
            }
        }
        public void GetByCode()
        {
            string code1 =  Guid.NewGuid().ToString().Substring(0, 10);
            string code2 = code1 + "foo";
            InventoryItemProxy proxy = new InventoryItemProxy();

            InventoryItemDto dto = new InventoryItemDto();
            dto.Code = code1;
            dto.Description = "New Inventory Item";
            proxy.Insert(dto);

            dto = new InventoryItemDto();
            dto.Code = code2;
            dto.Description = "New Inventory Item";
            proxy.Insert(dto);

            List<InventoryItemDto> list = proxy.FindList<InventoryItemDto>(InventoryItemProxy.ResponseXPath,
                                                                          "CodeBeginsWith", code1);
            Assert.AreEqual(2, list.Count, string.Format("Incorrect number of items returned for code {0}",code1));
        }
        public void GetByUtcLastModified()
        {
            string code1 = Guid.NewGuid().ToString().Substring(0, 10);
            string code2 = code1 + "foo";
            InventoryItemProxy proxy = new InventoryItemProxy();

            Thread.Sleep(10000); //Wait for ten seconds. Avoid retreiving items from text fixture setup.

            InventoryItemDto dto = new InventoryItemDto();
            dto.Code = code1;
            dto.Description = "New Inventory Item";
            proxy.Insert(dto);

            DateTime lastModifiedFrom = ((InventoryItemDto)proxy.GetByUid(dto.Uid)).UtcLastModified;
            Thread.Sleep(10000); //Wait for ten seconds.

            dto = new InventoryItemDto();
            dto.Code = code2;
            dto.Description = "New Inventory Item";
            proxy.Insert(dto);

            DateTime lastModifiedTo = ((InventoryItemDto)proxy.GetByUid(dto.Uid)).UtcLastModified.AddSeconds(1);

            System.Console.WriteLine("Utc last modified: {0}", lastModifiedFrom.ToString());

            string utcFrom = SqlStrPrep.ToDateTime(lastModifiedFrom).ToString();
            string utcTo   = SqlStrPrep.ToDateTime(lastModifiedTo).ToString();

            utcFrom = utcFrom.Replace('/', '-');
            utcFrom = utcFrom.Replace(' ', 'T');

            utcTo = utcTo.Replace('/', '-');
            utcTo = utcTo.Replace(' ', 'T');

            List<InventoryItemDto> list = proxy.FindList<InventoryItemDto>(InventoryItemProxy.ResponseXPath,
                                                                           "UtcLastModifiedFrom", utcFrom, "UtcLastModifiedTo", utcTo);
            Assert.AreEqual(2, list.Count, string.Format("Incorrect number of items returned for UtcLastModifiedFrom {0} and UtcLastModifiedTo{1}", utcFrom, utcTo));
        }
        private void CreateTestInventoryItems()
        {
            var proxy = new InventoryItemProxy();

            //sale items.
            if (_InventorySaleItemId == 0)
            {
                var dto = new InventoryItemDto
                {
                    AssetAccountUid = _AssetAccountId,
                    AverageCost = new decimal(5.00),
                    BuyingPrice = new decimal(4.00),
                    Code = string.Format("Inv{0}", Guid.NewGuid().ToString().Substring(0, 20)),
                    CurrentValue = new decimal(6.00),
                    DefaultReOrderQuantity = 2,
                    Description = "Test inventory sale item",
                    IsActive = true,
                    IsBought = false,
                    IsBuyingPriceIncTax = false,
                    IsSold = true,
                    SaleIncomeAccountUid = _IncomeAccountId
                };

                proxy.Insert(dto);
                _InventorySaleItemId = dto.Uid;
            }

            if (_InventorySaleItemId2 == 0)
            {
                var dto = new InventoryItemDto
                {
                    AssetAccountUid = _AssetAccountId,
                    AverageCost = new decimal(25.00),
                    BuyingPrice = new decimal(40.00),
                    Code = string.Format("Inv{0}", Guid.NewGuid().ToString().Substring(0, 20)),
                    CurrentValue = new decimal(16.00),
                    DefaultReOrderQuantity = 20,
                    Description = "updated Test inventory sale item",
                    IsActive = true,
                    IsBought = false,
                    IsBuyingPriceIncTax = false,
                    IsSold = true,
                    SaleIncomeAccountUid = _IncomeAccountId2
                };

                proxy.Insert(dto);
                _InventorySaleItemId2 = dto.Uid;
            }

            //purchase items.
            if (_InventoryPurchaseItemId == 0)
            {
                var dto = new InventoryItemDto
                {
                    AssetAccountUid = _AssetAccountId,
                    AverageCost = new decimal(5.00),
                    BuyingPrice = new decimal(4.00),
                    Code = string.Format("Inv{0}", Guid.NewGuid().ToString().Substring(0, 20)),
                    CurrentValue = new decimal(6.00),
                    DefaultReOrderQuantity = 2,
                    Description = "Test inventory purchase item",
                    IsActive = true,
                    IsBought = true,
                    IsBuyingPriceIncTax = true,
                    PurchaseExpenseAccountUid = _ExpenseAccountId
                };

                proxy.Insert(dto);
                _InventoryPurchaseItemId = dto.Uid;
            }

            if (_InventoryPurchaseItemId2 == 0)
            {
                var dto = new InventoryItemDto
                {
                    AssetAccountUid = _AssetAccountId,
                    AverageCost = new decimal(50.00),
                    BuyingPrice = new decimal(40.00),
                    Code = string.Format("Inv{0}", Guid.NewGuid().ToString().Substring(0, 20)),
                    CurrentValue = new decimal(60.00),
                    DefaultReOrderQuantity = 20,
                    Description = "Updated test inventory purchase item",
                    IsActive = true,
                    IsBought = true,
                    IsBuyingPriceIncTax = true,
                    PurchaseExpenseAccountUid = _ExpenseAccountId2
                };

                proxy.Insert(dto);
                _InventoryPurchaseItemId2 = dto.Uid;
            }
        }
        public void UpdateAsInventoryItem()
        {
            ComboItemProxy proxy = new ComboItemProxy();
            ComboItemDto comboItem = this.GetComboItem01();
            proxy.Insert(comboItem);

            ComboItemDto fromDB = (ComboItemDto)proxy.GetByUid(comboItem.Uid);
            fromDB.Description = fromDB.Description + " - Update as Inventory Item";
            fromDB.Items.Clear();

            InventoryItemProxy itemProxy = new InventoryItemProxy();
            itemProxy.Update(fromDB);
        }
        public void BuildComboItem()
        {
            ComboItemProxy proxy = new ComboItemProxy();

            ComboItemDto comboItemDto = this.GetComboItem01();
            proxy.Insert(comboItemDto);
            ComboItemDto comboItem = (ComboItemDto)proxy.GetByUid(comboItemDto.Uid);

            // We need to insert stock first using a purchase
            CrudProxy invoiceProxy = new InvoiceProxy();
            InvoiceDto dto = new InvoiceDto(TransactionType.Purchase, InvoiceLayout.Item);
            dto.Date = DateTime.Today.Date;
            dto.ContactUid = this.MrSmith.Uid;
            dto.Summary = "Add stock do we can build ComboItems";
            dto.Notes = "From REST";
            dto.DueOrExpiryDate = dto.Date.AddMonths(1);
            dto.Status = InvoiceStatus.Invoice;
            dto.InvoiceNumber = "I123";
            dto.PurchaseOrderNumber = "<Auto Number>";

            decimal unitsToBuild = 12.25M;

            foreach(ComboItemLineItemDto itemInCombo in comboItem.Items)
            {   // "purchase" all the items that are part of this combo item so we have valid stock
                ItemInvoiceItemDto item = new ItemInvoiceItemDto();
                item.Quantity = itemInCombo.Quantity * unitsToBuild;
                item.InventoryItemUid = itemInCombo.Uid;
                item.Description = "Purchasing: " + itemInCombo.Code;
                item.TaxCode = TaxCode.ExpInclGst;
                item.UnitPriceInclTax = 99.95M;
                dto.Items.Add(item);
            }
            invoiceProxy.Insert(dto);

            // Download stock info before
            InventoryItemProxy inventoryItemProxy = new InventoryItemProxy();
            InventoryItemDto inventoryItem = (InventoryItemDto)inventoryItemProxy.GetByUid(comboItemDto.Uid);
            decimal stockOnHand = inventoryItem.StockOnHand;

            // Build the item!
            BuildComboItemResult result = proxy.Build(comboItemDto.Uid, unitsToBuild);
            Assert.AreNotEqual(0, result.Uid);
            Assert.IsNotNull(result.LastUpdatedUid);

            inventoryItem = (InventoryItemDto)inventoryItemProxy.GetByUid(comboItemDto.Uid);
            decimal newStockOnHand = inventoryItem.StockOnHand;
            Assert.AreEqual(stockOnHand + unitsToBuild, newStockOnHand, "We have one extra item in stock");

            // Read Inventory Transfer details
            InventoryTransferProxy transferProxy = new InventoryTransferProxy();
            InventoryTransferDto transfer = (InventoryTransferDto)transferProxy.GetByUid(result.Uid);
            Assert.AreEqual(comboItem.Items.Count+1, transfer.Items.Count); // +1 as we have the combo item the first one

            // confirm first item is the combo with +1
            InventoryTransferItemDto comboItemTransfer = (InventoryTransferItemDto)transfer.Items[0];
            Assert.AreEqual(comboItemDto.Uid, comboItemTransfer.InventoryItemUid);
            Assert.AreEqual(unitsToBuild, comboItemTransfer.Quantity);

            for (int i = 0; i < comboItem.Items.Count; i++)
            {
                ComboItemLineItemDto line = comboItem.Items[i];
                InventoryTransferItemDto item = (InventoryTransferItemDto)transfer.Items[i+1];
                Assert.AreEqual(line.Uid, item.InventoryItemUid);
                Assert.AreEqual(line.Quantity * unitsToBuild, -item.Quantity);
            }
        }
        public void InsertAndGet()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto) proxy.GetByUid(dto1.Uid);

            AssertEqual(dto1, dto2);
        }
        public void InsertSellPriceIncTax()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.SellingPrice = 7.75M;
            dto1.IsSellingPriceIncTax = true;
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);

            Assert.AreEqual(7.75M, dto1.SellingPrice, "Diff. selling price");
            Assert.AreEqual(true, dto1.IsSellingPriceIncTax, "Is selling price exc tax should be true.");
        }
        public void UpdateDeleted()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            proxy.Insert(dto1);

            proxy.DeleteByUid(dto1.Uid);

            dto1.Description = "Try updating deleted item";

            try
            {
                proxy.Update(dto1);
                throw new Exception("Expected exception not thrown.");
            }
            catch (RestException rex)
            {
                if (!rex.Message.Contains("Unable to find inventory item uid"))
                {
                    throw new Exception("Possibly incorrect error thrown.");
                }
            }
        }
        public void Update()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            proxy.Insert(dto1);

            string lastUpdatedUid = dto1.LastUpdatedUid;

            dto1.Description = this.TestUid + "For Test Update - Updated";
            dto1.Notes = "Updated ....";
            dto1.AssetAccountUid = this.AssetInventory2.Uid;
            dto1.SaleTaxCode = TaxCode.SaleExports;

            proxy.Update(dto1);

            Assert.IsTrue(dto1.LastUpdatedUid != lastUpdatedUid);

            InventoryItemDto dto2 = (InventoryItemDto) proxy.GetByUid(dto1.Uid);

            AssertEqual(dto1, dto2);
        }
        public void TestAverageCostIsReturnedForInventoryItem()
        {
            CrudProxy proxy = new InventoryAdjustmentProxy();
            InventoryAdjustmentDto dto1 = this.GetInventoryAdjustment();
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            proxy = new InventoryItemProxy();
            InventoryItemDto itemDto  = (InventoryItemDto)proxy.GetByUid(HardDisk.Uid);

            Assert.IsTrue(itemDto.AverageCost == 200.50M);
        }
        public void InsertWithDefaultBuyingPrice()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.BuyingPrice = 250M;
            dto1.IsBuyingPriceIncTax = true;
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);

            AssertEqual(dto1, dto2);
        }
        public void InsertVoucherItem()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.IsVoucher = true;
            dto1.ValidFrom = DateTime.Now.Date;
            dto1.ValidTo = dto1.ValidFrom.AddMonths(3);

            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);

            AssertEqual(dto1, dto2);
        }
        public void InsertVirtualItem()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.IsVirtual = true;
            dto1.VType = "Plan";

            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);

            AssertEqual(dto1, dto2);
        }
        public void InsertUsingDeprecatedRrpInclTax()
        {
            CrudProxy proxy = new InventoryItemProxy();
            InventoryItemDto dto1 = this.GetInventoryItem();
            dto1.RrpInclTax = 7.75M;
            proxy.Insert(dto1);

            Assert.IsTrue(dto1.Uid > 0, "Uid must be > 0 after save.");

            InventoryItemDto dto2 = (InventoryItemDto)proxy.GetByUid(dto1.Uid);

            Assert.AreEqual(7.75M, dto2.RrpInclTax, "Diff. rrp. incl. tax.");
            Assert.AreEqual(7.75M, dto2.SellingPrice, "Diff. selling price.");
            Assert.AreEqual(true, dto2.IsSellingPriceIncTax, "Is selling price exc tax should be true.");
        }
        private void SetupInventoryItems()
        {
            CrudProxy inventoryItem = new InventoryItemProxy();

            string guid = Guid.NewGuid().ToString().Remove(0, 20);

            this.AsusLaptop = new InventoryItemDto();
            this.AsusLaptop.Code = "ASUS-" + guid;
            this.AsusLaptop.Description = "Asus Laptop";
            this.AsusLaptop.IsInventoried = true;
            this.AsusLaptop.AssetAccountUid = this.AssetInventory.Uid;
            this.AsusLaptop.IsBought = true;
            this.AsusLaptop.PurchaseTaxCode = TaxCode.ExpInclGst;
            this.AsusLaptop.IsSold = true;
            this.AsusLaptop.SaleIncomeAccountUid = this.IncomeHardwareSales.Uid;
            this.AsusLaptop.SaleTaxCode = TaxCode.SaleInclGst;
            this.AsusLaptop.RrpInclTax = 1111.11M;
            this.AsusLaptop.SaleCoSAccountUid = this.CoSHardware.Uid;
            inventoryItem.Insert(this.AsusLaptop);

            this.Cat5Cable = new InventoryItemDto();
            this.Cat5Cable.Code = "Cat5-" + guid;
            this.Cat5Cable.Description = "Cat 5 Cable (in meter)";
            this.Cat5Cable.IsInventoried = true;
            this.Cat5Cable.AssetAccountUid = this.AssetInventory.Uid;
            this.Cat5Cable.IsBought = true;
            this.Cat5Cable.PurchaseTaxCode = TaxCode.ExpInclGst;
            this.Cat5Cable.IsSold = true;
            this.Cat5Cable.SaleIncomeAccountUid = this.IncomeHardwareSales.Uid;
            this.Cat5Cable.SaleTaxCode = TaxCode.SaleInclGst;
            this.Cat5Cable.RrpInclTax = 2.55M;
            this.Cat5Cable.SaleCoSAccountUid = this.CoSHardware.Uid;
            inventoryItem.Insert(this.Cat5Cable);

            this.HardDisk = new InventoryItemDto();
            this.HardDisk.Code = "Seagate-" + guid;
            this.HardDisk.Description = "Seagate HDD - 300G";
            this.HardDisk.IsInventoried = true;
            this.HardDisk.AssetAccountUid = this.AssetInventory.Uid;
            this.HardDisk.IsBought = true;
            this.HardDisk.PurchaseTaxCode = TaxCode.ExpInclGst;
            this.HardDisk.IsSold = true;
            this.HardDisk.SaleIncomeAccountUid = this.IncomeHardwareSales.Uid;
            this.HardDisk.SaleTaxCode = TaxCode.SaleInclGst;
            this.HardDisk.RrpInclTax = 110.95M;
            this.HardDisk.SaleCoSAccountUid = this.CoSHardware.Uid;
            inventoryItem.Insert(this.HardDisk);

            this.Shipping1 = new InventoryItemDto();
            this.Shipping1.Code = "Shipping1-" + guid;
            this.Shipping1.Description = "Shipping - 1";
            this.Shipping1.IsBought = true;
            this.Shipping1.PurchaseExpenseAccountUid = this.ExpenseMisc.Uid;
            this.Shipping1.PurchaseTaxCode = TaxCode.ExpInclGst;
            this.Shipping1.IsSold = true;
            this.Shipping1.SaleIncomeAccountUid = this.IncomeShipping.Uid;
            this.Shipping1.SaleTaxCode = TaxCode.SaleInclGst;
            this.Shipping1.RrpInclTax = 59.95M;
            inventoryItem.Insert(this.Shipping1);
        }
 private void SetupInventoryItems()
 {
     this.Item1 = this.GetItem1();
     CrudProxy proxy = new InventoryItemProxy();
     proxy.Insert(this.Item1);
     Assert.IsTrue(this.Item1.Uid > 0, "Uid must be > 0 after save.");
 }