Example #1
0
        public void AddReceipt(Invoice invoice, IEnumerable <InvoiceItem> items)
        {
            using (var transaction = new TransactionScope())
                using (var connection = new ConnectionScope())
                {
                    if (invoice.Id == Guid.Empty)
                    {
                        invoice.Id = Guid.NewGuid();
                    }

                    // Update Inventory.
                    foreach (var item in items)
                    {
                        var product = ProductManager.GetProduct(item.ProductId);
                        if (product == null)
                        {
                            throw new Exception("Product not found.");
                        }

                        // Stock Correction?
                        // TODO.

                        // Movement.
                        ProductManager.AddMovement(new ProductMovement
                        {
                            Id               = Guid.NewGuid(),
                            ProductId        = item.ProductId,
                            DateTime         = invoice.DateTime,
                            MovementType     = ProductMovementType.Receipt,
                            Quantity         = item.Quantity,
                            SourceId         = invoice.Id,
                            SourceItemNumber = item.ItemNumber
                        });
                    }

                    // Add Customer Transaction.
                    CustomerManager.AddTransaction(new CustomerTransaction()
                    {
                        Id         = Guid.NewGuid(),
                        CustomerId = invoice.CustomerId,
                        DateTime   = invoice.DateTime,
                        Type       = CustomerTransactionType.Payment,
                        Amount     = items.Aggregate(0.0m, (total, item) => total += item.Quantity * item.Price) * -1.0m,
                        SourceId   = invoice.Id
                    });

                    transaction.Complete();
                }
        }
Example #2
0
        public void AddInvoice(Invoice invoice, IEnumerable <InvoiceItem> items, decimal payment)
        {
            using (var transaction = new TransactionScope())
                using (var connection = new ConnectionScope())
                {
                    // Insert Invoice.
                    invoice.Insert();

                    foreach (var item in items)
                    {
                        item.InvoiceId = invoice.Id;
                    }

                    items.Insert();

                    // Update Inventory.
                    foreach (var item in items)
                    {
                        var product = ProductManager.GetProduct(item.ProductId);
                        if (product == null)
                        {
                            throw new Exception("Product not found.");
                        }

                        // Stock Correction?
                        if (product.QuantityOnHand < item.Quantity)
                        {
                            ProductManager.AddMovement(new ProductMovement
                            {
                                Id               = Guid.NewGuid(),
                                ProductId        = item.ProductId,
                                DateTime         = invoice.DateTime,
                                MovementType     = ProductMovementType.Correction,
                                Quantity         = item.Quantity - product.QuantityOnHand,
                                SourceId         = invoice.Id,
                                SourceItemNumber = item.ItemNumber
                            });
                        }

                        // Movement.
                        ProductManager.AddMovement(new ProductMovement
                        {
                            Id               = Guid.NewGuid(),
                            ProductId        = item.ProductId,
                            DateTime         = invoice.DateTime,
                            MovementType     = ProductMovementType.Invoice,
                            Quantity         = item.Quantity * -1,
                            SourceId         = invoice.Id,
                            SourceItemNumber = item.ItemNumber
                        });
                    }

                    // Add Customer Transaction.
                    CustomerManager.AddTransaction(new CustomerTransaction()
                    {
                        Id         = Guid.NewGuid(),
                        CustomerId = invoice.CustomerId,
                        DateTime   = invoice.DateTime,
                        Type       = CustomerTransactionType.Invoice,
                        Amount     = items.Aggregate(0.0m, (total, item) => total += item.Quantity * Math.Round(item.Price * (100 - item.Discount) / 100, 2, MidpointRounding.AwayFromZero)),
                        SourceId   = invoice.Id
                    });

                    // Payment?
                    if (payment > 0)
                    {
                        CustomerManager.AddTransaction(new CustomerTransaction()
                        {
                            Id         = Guid.NewGuid(),
                            CustomerId = invoice.CustomerId,
                            DateTime   = invoice.DateTime,
                            Type       = CustomerTransactionType.Payment,
                            Amount     = payment * -1,
                            SourceId   = invoice.Id
                        });
                    }

                    transaction.Complete();
                }
        }