Exemplo n.º 1
0
        public IActionResult PostSalesInvoice([FromBody] Dto.Sales.SalesInvoice salesInvoiceDto)
        {
            string[] errors = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    errors = new string[ModelState.ErrorCount];
                    foreach (var val in ModelState.Values)
                    {
                        for (int i = 0; i < ModelState.ErrorCount; i++)
                        {
                            errors[i] = val.Errors[i].ErrorMessage;
                        }
                    }

                    return(new BadRequestObjectResult(errors));
                }

                _salesService.PostSalesInvoice(salesInvoiceDto.Id);

                return(new ObjectResult(Ok()));
            }
            catch (Exception ex)
            {
                errors = new string[1] {
                    ex.InnerException != null ? ex.InnerException.Message : ex.Message
                };
                return(new BadRequestObjectResult(errors));
            }
        }
Exemplo n.º 2
0
        public IActionResult CustomerInvoices(int id)
        {
            try
            {
                var invoices = _salesService.GetCustomerInvoices(id);

                var invoicesDto = new HashSet <Dto.Sales.SalesInvoice>();

                foreach (var invoice in invoices)
                {
                    var invoiceDto = new Dto.Sales.SalesInvoice()
                    {
                        Id                   = invoice.Id,
                        InvoiceDate          = invoice.Date,
                        CustomerId           = invoice.CustomerId,
                        TotalAllocatedAmount = (decimal)invoice.CustomerAllocations.Sum(i => i.Amount),
                        Posted               = invoice.GeneralLedgerHeaderId.HasValue
                    };

                    invoicesDto.Add(invoiceDto);
                }

                return(new ObjectResult(invoicesDto));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex));
            }
        }
Exemplo n.º 3
0
        public IActionResult SalesInvoice(int id)
        {
            try
            {
                var salesInvoice = _salesService.GetSalesInvoiceById(id);

                var salesOrderDto = new Dto.Sales.SalesInvoice()
                {
                    Id                = salesInvoice.Id,
                    CustomerId        = salesInvoice.CustomerId,
                    CustomerName      = salesInvoice.Customer.Party.Name,
                    InvoiceDate       = salesInvoice.Date,
                    SalesInvoiceLines = new List <Dto.Sales.SalesInvoiceLine>(),
                    PaymentTermId     = salesInvoice.PaymentTermId,
                    ReferenceNo       = salesInvoice.ReferenceNo,
                    Posted            = salesInvoice.GeneralLedgerHeaderId != null
                };

                foreach (var line in salesInvoice.SalesInvoiceLines)
                {
                    var lineDto = new Dto.Sales.SalesInvoiceLine();
                    lineDto.Id            = line.Id;
                    lineDto.Amount        = line.Amount;
                    lineDto.Discount      = line.Discount;
                    lineDto.Quantity      = line.Quantity;
                    lineDto.ItemId        = line.ItemId;
                    lineDto.MeasurementId = line.MeasurementId;

                    salesOrderDto.SalesInvoiceLines.Add(lineDto);
                }

                // is this journal entry ready for posting?
                if (!salesOrderDto.Posted && salesOrderDto.SalesInvoiceLines.Count >= 1)
                {
                    salesOrderDto.ReadyForPosting = true;
                }

                return(new ObjectResult(salesOrderDto));
            }
            catch (Exception ex)
            {
                return(new ObjectResult(ex));
            }
        }
Exemplo n.º 4
0
        public IActionResult SalesInvoices()
        {
            var salesInvoices = _salesService.GetSalesInvoices();
            IList <Dto.Sales.SalesInvoice> salesInvoicesDto = new List <Dto.Sales.SalesInvoice>();

            foreach (var salesInvoice in salesInvoices)
            {
                var salesInvoiceDto = new Dto.Sales.SalesInvoice()
                {
                    Id           = salesInvoice.Id,
                    No           = salesInvoice.No,
                    CustomerId   = salesInvoice.CustomerId,
                    CustomerName = salesInvoice.Customer.Party.Name,
                    InvoiceDate  = salesInvoice.Date,
                    ReferenceNo  = salesInvoice.ReferenceNo,
                    Posted       = salesInvoice.GeneralLedgerHeaderId != null
                };

                foreach (var line in salesInvoice.SalesInvoiceLines)
                {
                    var lineDto = new Dto.Sales.SalesInvoiceLine()
                    {
                        ItemId        = line.ItemId,
                        MeasurementId = line.MeasurementId,
                        Quantity      = line.Quantity,
                        Amount        = line.Amount,
                        Discount      = line.Discount
                    };
                    salesInvoiceDto.SalesInvoiceLines.Add(lineDto);
                }

                salesInvoicesDto.Add(salesInvoiceDto);
            }

            return(new ObjectResult(salesInvoicesDto));
        }
Exemplo n.º 5
0
        public IActionResult SaveSalesInvoice([FromBody] Dto.Sales.SalesInvoice salesInvoiceDto)
        {
            string[] errors = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    errors = new string[ModelState.ErrorCount];
                    foreach (var val in ModelState.Values)
                    {
                        for (int i = 0; i < ModelState.ErrorCount; i++)
                        {
                            errors[i] = val.Errors[i].ErrorMessage;
                        }
                    }

                    return(new BadRequestObjectResult(errors));
                }

                bool isNew = salesInvoiceDto.Id == 0;
                Core.Domain.Sales.SalesInvoiceHeader salesInvoice = null;
                Core.Domain.Sales.SalesOrderHeader   salesOrder   = null;

                // Creating a new invoice
                if (isNew)
                {
                    // if fromsalesorderid has NO value, then create automatically a new sales order.
                    if (!salesInvoiceDto.FromSalesOrderId.HasValue)
                    {
                        salesOrder               = new Core.Domain.Sales.SalesOrderHeader();
                        salesOrder.Date          = salesInvoiceDto.InvoiceDate;
                        salesOrder.PaymentTermId = salesInvoiceDto.PaymentTermId;
                        salesOrder.CustomerId    = salesInvoiceDto.CustomerId;
                        salesOrder.ReferenceNo   = salesInvoiceDto.ReferenceNo;
                        salesOrder.Status        = SalesOrderStatus.FullyInvoiced;
                    }
                    else
                    {
                        // else,  your invoice is created from existing (open) sales order.
                        salesOrder = _salesService.GetSalesOrderById(salesInvoiceDto.FromSalesOrderId.GetValueOrDefault());
                    }

                    // populate invoice header
                    salesInvoice               = new Core.Domain.Sales.SalesInvoiceHeader();
                    salesInvoice.CustomerId    = salesInvoiceDto.CustomerId.GetValueOrDefault();
                    salesInvoice.Date          = salesInvoiceDto.InvoiceDate;
                    salesInvoice.PaymentTermId = salesInvoiceDto.PaymentTermId;
                    salesInvoice.ReferenceNo   = salesInvoiceDto.ReferenceNo;

                    foreach (var line in salesInvoiceDto.SalesInvoiceLines)
                    {
                        var salesInvoiceLine = new Core.Domain.Sales.SalesInvoiceLine();

                        salesInvoiceLine.Amount        = line.Amount.GetValueOrDefault();
                        salesInvoiceLine.Discount      = line.Discount.GetValueOrDefault();
                        salesInvoiceLine.Quantity      = line.Quantity.GetValueOrDefault();
                        salesInvoiceLine.ItemId        = line.ItemId.GetValueOrDefault();
                        salesInvoiceLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                        salesInvoice.SalesInvoiceLines.Add(salesInvoiceLine);

                        // line.Id here is referring to SalesOrderLineId. It is pre-populated when you create a new sales invoice from sales order.
                        if (line.Id != 0)
                        {
                            salesInvoiceLine.SalesOrderLineId = line.Id;
                        }
                        else
                        {
                            // if you reach here, this line item is newly added to invoice which is not originally in sales order. create correspondin orderline and add to sales order.
                            var salesOrderLine = new Core.Domain.Sales.SalesOrderLine();
                            salesOrderLine.Amount           = line.Amount.GetValueOrDefault();
                            salesOrderLine.Discount         = line.Discount.GetValueOrDefault();
                            salesOrderLine.Quantity         = line.Quantity.GetValueOrDefault();
                            salesOrderLine.ItemId           = line.ItemId.GetValueOrDefault();
                            salesOrderLine.MeasurementId    = line.MeasurementId.GetValueOrDefault();
                            salesInvoiceLine.SalesOrderLine = salesOrderLine;
                            salesOrder.SalesOrderLines.Add(salesOrderLine);

                            salesInvoiceLine.SalesOrderLine = salesOrderLine; // map invoice line to newly added orderline
                        }
                    }
                }
                else
                {
                    // if you reach here, you are updating existing invoice.
                    salesInvoice = _salesService.GetSalesInvoiceById(salesInvoiceDto.Id);

                    if (salesInvoice.GeneralLedgerHeaderId.HasValue)
                    {
                        throw new Exception("Invoice is already posted. Update is not allowed.");
                    }

                    salesInvoice.Date          = salesInvoiceDto.InvoiceDate;
                    salesInvoice.PaymentTermId = salesInvoiceDto.PaymentTermId;
                    salesInvoice.ReferenceNo   = salesInvoiceDto.ReferenceNo;

                    foreach (var line in salesInvoiceDto.SalesInvoiceLines)
                    {
                        var existingLine = salesInvoice.SalesInvoiceLines.Where(id => id.Id == line.Id).FirstOrDefault();
                        if (salesInvoice.SalesInvoiceLines.Where(id => id.Id == line.Id).FirstOrDefault() != null)
                        {
                            existingLine.Amount        = line.Amount.GetValueOrDefault();
                            existingLine.Discount      = line.Discount.GetValueOrDefault();
                            existingLine.Quantity      = line.Quantity.GetValueOrDefault();
                            existingLine.ItemId        = line.ItemId.GetValueOrDefault();
                            existingLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                        }
                        else
                        {
                            //if you reach here, this line item is newly added to invoice. also, it has no SalesOrderLineId.
                            var salesInvoiceLine = new Core.Domain.Sales.SalesInvoiceLine();
                            salesInvoiceLine.Amount        = line.Amount.GetValueOrDefault();
                            salesInvoiceLine.Discount      = line.Discount.GetValueOrDefault();
                            salesInvoiceLine.Quantity      = line.Quantity.GetValueOrDefault();
                            salesInvoiceLine.ItemId        = line.ItemId.GetValueOrDefault();
                            salesInvoiceLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                            salesInvoice.SalesInvoiceLines.Add(salesInvoiceLine);

                            // add a new order line.
                            var salesOrderLine = new Core.Domain.Sales.SalesOrderLine();
                            salesOrderLine.Amount        = line.Amount.GetValueOrDefault();
                            salesOrderLine.Discount      = line.Discount.GetValueOrDefault();
                            salesOrderLine.Quantity      = line.Quantity.GetValueOrDefault();
                            salesOrderLine.ItemId        = line.ItemId.GetValueOrDefault();
                            salesOrderLine.MeasurementId = line.MeasurementId.GetValueOrDefault();

                            // but on what order should the new orderline be added?
                            // note: each invoice is map to one and only one sales order. it can't be done that invoice lines came from multiple sales orders.
                            // with this rule, we are sure that all invoice lines are contained in the same sales order.
                            // therefore, we could just pick the first line, get the salesorderlineid, then get the salesorderheader.

                            // you will retrieve salesorder one time.
                            if (salesOrder == null)
                            {
                                // use the last value of existingLine
                                salesOrder = _salesService.GetSalesOrderLineById(existingLine.SalesOrderLine.SalesOrderHeaderId).SalesOrderHeader;
                                salesOrder.SalesOrderLines.Add(salesOrderLine);
                            }

                            salesInvoiceLine.SalesOrderLine = salesOrderLine; // map invoice line to newly added orderline
                        }
                    }
                }

                if (!isNew)
                {
                    var deleted = (from line in salesInvoice.SalesInvoiceLines
                                   where !salesInvoiceDto.SalesInvoiceLines.Any(x => x.Id == line.Id)
                                   select line).ToList();

                    foreach (var line in deleted)
                    {
                        salesInvoice.SalesInvoiceLines.Remove(line);
                    }
                }

                _salesService.SaveSalesInvoice(salesInvoice, salesOrder);

                return(new OkObjectResult(Ok()));
            }
            catch (Exception ex)
            {
                errors = new string[1] {
                    ex.InnerException != null ? ex.InnerException.Message : ex.Message
                };
                return(new BadRequestObjectResult(errors));
            }
        }