public InvoiceDto GetSale1()
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Sale, InvoiceLayout.Item);

            dto.Date = DateTime.Today;
            dto.ContactUid = this.MrSmith.Uid;
            dto.Summary = "Test Insert Item Sale";
            dto.Notes = "From REST";
            dto.DueOrExpiryDate = dto.Date.AddMonths(1);
            dto.Layout = InvoiceLayout.Item;
            dto.Status = InvoiceStatus.Invoice;
            dto.InvoiceNumber = "<Auto Number>";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity = 1;
            item.InventoryItemUid = this.Item1.Uid;
            item.Description = this.Item1.Description;
            item.TaxCode = TaxCode.SaleInclGst;
            item.UnitPriceInclTax = this.Item1.RrpInclTax;
            dto.Items.Add(item);

            dto.Tags = "CC";

            return dto;
        }
        public InvoiceDto GetItemSale()
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Sale, InvoiceLayout.Item);

            dto.Date = DateTime.Parse("6-Oct-05");
            dto.ContactUid = this.MrSmith.Uid;
            dto.Summary = "Test Insert Item Sale";
            dto.Notes = "From REST";
            dto.DueOrExpiryDate = DateTime.Parse("6-Nov-05");
            dto.Layout = InvoiceLayout.Item;
            dto.Status = InvoiceStatus.Invoice;
            dto.InvoiceNumber = "<Auto Number>";
            dto.PurchaseOrderNumber = "PO333";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity = 2;
            item.InventoryItemUid = this.AsusLaptop.Uid;
            item.Description = "Asus Laptop";
            item.TaxCode = TaxCode.SaleInclGst;
            item.UnitPriceInclTax = 1200.75M;
            item.PercentageDiscount = 12.50M;

            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity = 5.125M;
            item.InventoryItemUid = this.Cat5Cable.Uid;
            item.Description = "Cat 5 Cable (in meter)";
            item.TaxCode = TaxCode.SaleGstFree;
            item.UnitPriceInclTax = 2.1234M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity = 3;
            item.InventoryItemUid = this.Cat5Cable.Uid;
            item.Description = "Cat 5 Cable (in meter)";
            item.TaxCode = TaxCode.SaleExports;
            item.UnitPriceInclTax = 5.125M;
            dto.Items.Add(item);

            QuickPaymentDto payment = new QuickPaymentDto();
            payment.DatePaid = dto.Date;
            payment.BankedToAccountUid = this.StGeorge.Uid;
            payment.Reference = "C-001-023";
            payment.Amount = 222.22M;

            dto.QuickPayment = payment;

            return dto;
        }
        public virtual void CreatePurchaseForInventoryItemsUsed()
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Purchase, InvoiceLayout.Item);

            dto.Date				= DateTime.Today.Date;
            dto.ContactUid			= this.MrSmith.Uid;
            dto.Summary				= "Invoice Tests - CreatePurchaseForInventoryItemsUsed()";
            dto.Notes				= "From REST";
            dto.DueOrExpiryDate		= dto.Date.AddMonths(1);
            dto.Status				= InvoiceStatus.Invoice;
            dto.PurchaseOrderNumber = "<Auto Number>";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity			= 10000;
            item.InventoryItemUid	= this.AsusLaptop.Uid;
            item.Description		= this.AsusLaptop.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 1000M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity			= 10000;
            item.InventoryItemUid	= this.HardDisk.Uid;
            item.Description		= this.HardDisk.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 99.95M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity			= 500000;
            item.InventoryItemUid	= this.Cat5Cable.Uid;
            item.Description		= this.Cat5Cable.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 0.95M;
            dto.Items.Add(item);

            new InvoiceProxy().Insert(dto);
        }
        public InvoiceDto GetUnpaidItemSale()
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Sale, InvoiceLayout.Item);

            dto.Date = DateTime.Parse("6-Oct-05");
            dto.ContactUid = this.MrsSmith.Uid;
            dto.Summary = "Test Insert Item Sale";
            dto.Notes = "From REST";
            dto.DueOrExpiryDate = DateTime.Parse("6-Nov-05");
            dto.Status = InvoiceStatus.Invoice;
            dto.InvoiceNumber = "<Auto Number>";
            dto.PurchaseOrderNumber = "PO333";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity = 2;
            item.InventoryItemUid = this.HardDisk.Uid;
            item.Description = this.HardDisk.Description;
            item.TaxCode = TaxCode.SaleInclGst;
            item.UnitPriceInclTax = 120.75M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity = 5.125M;
            item.InventoryItemUid = this.Cat5Cable.Uid;
            item.Description = this.Cat5Cable.Description;
            item.TaxCode = TaxCode.SaleInclGst;
            item.UnitPriceInclTax = 2.555M;
            dto.Items.Add(item);

            return dto;
        }
        private InvoiceDto GetUnpaidItemPurchase()
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Purchase, InvoiceLayout.Item);

            dto.Date				= DateTime.Today.Date;
            dto.ContactUid			= this.MrSmith.Uid;
            dto.Summary				= "Test Insert Item Purchase";
            dto.Notes				= "From REST";
            dto.DueOrExpiryDate		= dto.Date.AddMonths(1);
            dto.Status				= InvoiceStatus.Invoice;
            dto.InvoiceNumber		= "I123";
            dto.PurchaseOrderNumber = "<Auto Number>";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity			= 100;
            item.InventoryItemUid	= this.HardDisk.Uid;
            item.Description		= this.HardDisk.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 99.95M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity			= 5000;
            item.InventoryItemUid	= this.Cat5Cable.Uid;
            item.Description		= this.Cat5Cable.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 0.95M;
            dto.Items.Add(item);

            item = new ItemInvoiceItemDto();
            item.Quantity			= 1;
            item.InventoryItemUid	= this.Shipping1.Uid;
            item.Description		= this.Shipping1.Description;
            item.TaxCode			= TaxCode.ExpInclGst;
            item.UnitPriceInclTax	= 111.11M;
            dto.Items.Add(item);

            return dto;
        }
        public void TestInsertAndGetItemPurchase_Invoice_Totals_Test()
        {
            //Arange
            var proxy = new InvoiceProxy();
            var input = GetUnpaidItemPurchase();
            input.Items.Clear();

            var item = new ItemInvoiceItemDto
                           {
                               Quantity = 100,
                               InventoryItemUid = this.HardDisk.Uid,
                               Description = this.HardDisk.Description,
                               TaxCode = TaxCode.SaleInclGst,
                               UnitPriceInclTax = 99.95M
                           };

            input.Items.Add(item);

            item = new ItemInvoiceItemDto
                       {
                           Quantity = 5000,
                           InventoryItemUid = this.Cat5Cable.Uid,
                           Description = this.Cat5Cable.Description,
                           TaxCode = TaxCode.SaleInclGst,
                           UnitPriceInclTax = 0.95M
                       };

            input.Items.Add(item);

            //act
            var item1 = ((ItemInvoiceItemDto)input.Items[0]);
            var item2 = ((ItemInvoiceItemDto)input.Items[1]);

            var totalItem1 = (item1.UnitPriceInclTax*item1.Quantity);
            var totalItem2 = (item2.UnitPriceInclTax*item2.Quantity);

            var totalTax1 = totalItem1 - (Math.Round(totalItem1 / (1 + 0.100000m), 2, MidpointRounding.AwayFromZero));
            var totalTax2 = totalItem2 - (Math.Round(totalItem2 / (1 + 0.100000m), 2, MidpointRounding.AwayFromZero));

            proxy.Insert(input);

            var output = (InvoiceDto)proxy.GetByUid(input.Uid);
            var outputLineItem1 = ((ItemInvoiceItemDto)output.Items[0]);
            var outputLineItem2 = ((ItemInvoiceItemDto)output.Items[1]);

            //assert

            //Test Line Item Totals.
            Assert.AreEqual(outputLineItem1.TotalAmountInclTax, totalItem1);
            Assert.AreEqual(outputLineItem1.TotalTaxAmount, totalTax1);
            Assert.AreEqual(outputLineItem1.TotalAmountExclTax, outputLineItem1.TotalAmountInclTax - totalTax1);

            Assert.AreEqual(outputLineItem2.TotalAmountInclTax, totalItem2);
            Assert.AreEqual(outputLineItem2.TotalTaxAmount, totalTax2);
            Assert.AreEqual(outputLineItem2.TotalAmountExclTax, outputLineItem2.TotalAmountInclTax - totalTax2);

            //Test Invoice Totals.
            Assert.AreEqual(output.TotalAmountInclTax, outputLineItem1.TotalAmountInclTax + outputLineItem2.TotalAmountInclTax);
            Assert.AreEqual(output.TotalTaxAmount, outputLineItem1.TotalTaxAmount + outputLineItem2.TotalTaxAmount);
            Assert.AreEqual(output.TotalAmountExclTax, outputLineItem1.TotalAmountExclTax + outputLineItem2.TotalAmountExclTax);
        }
        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 DeleteReferenced()
        {
            ComboItemProxy proxy = new ComboItemProxy();
            ComboItemDto comboItem = this.GetComboItem01();
            proxy.Insert(comboItem);

            // 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 = "Using a combo item.";
            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;

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

            try
            {
                proxy.DeleteByUid(comboItem.Uid);
                Assert.Fail("Expected exception was not thrown.");
            }
            catch (RestException ex)
            {
                Assert.AreEqual("The specified inventory item is referenced by some Transactions and/or Combo Items  and therefore cannot be deleted.", ex.Message);
            }
        }
        private InvoiceDto GetSaleOrder(InventoryItemDto itemDto)
        {
            InvoiceDto dto = new InvoiceDto(TransactionType.Sale, InvoiceLayout.Item);

            dto.Date = DateTime.Parse("6-Oct-05");
            dto.ContactUid = this.MrSmith.Uid;
            dto.Summary = "Test Insert Item Sale";
            dto.Notes = "From REST";
            dto.DueOrExpiryDate = DateTime.Parse("6-Nov-05");
            dto.Layout = InvoiceLayout.Item;
            dto.Status = InvoiceStatus.Order;
            dto.InvoiceNumber = "<Auto Number>";
            dto.PurchaseOrderNumber = "PO333";

            ItemInvoiceItemDto item = null;

            item = new ItemInvoiceItemDto();
            item.Quantity = 5;
            item.InventoryItemUid = itemDto.Uid;
            item.Description = "Asus Laptop";
            item.TaxCode = TaxCode.SaleInclGst;
            item.UnitPriceInclTax = 1200.75M;
            item.PercentageDiscount = 12.50M;

            dto.Items.Add(item);

            return dto;
        }
 protected static void AssertEqual(ItemInvoiceItemDto expected, ItemInvoiceItemDto actual, int index)
 {
     string msg = "Item index: " + index.ToString() + ". Different ";
     Assert.AreEqual(expected.Quantity, actual.Quantity, msg + "quantity.");
     Assert.AreEqual(expected.InventoryItemUid, actual.InventoryItemUid, msg + "inventory item uid.");
     Assert.AreEqual(expected.Description, actual.Description, msg + "description.");
     Assert.AreEqual(expected.TaxCode, actual.TaxCode, msg + "tax code.");
     Assert.AreEqual(expected.UnitPriceInclTax, actual.UnitPriceInclTax, msg + "unit price incl tax.");
     Assert.AreEqual(expected.PercentageDiscount, actual.PercentageDiscount, msg + "percentage discount.");
 }