Пример #1
0
        public IActionResult AddSalesOrder([FromBody] Dto.Sales.SalesOrder salesorderDto)
        {
            try
            {
                var salesOrder = new Core.Domain.Sales.SalesOrderHeader()
                {
                    CustomerId = salesorderDto.CustomerId,
                    Date       = salesorderDto.OrderDate,
                };

                foreach (var line in salesorderDto.SalesOrderLines)
                {
                    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();

                    salesOrder.SalesOrderLines.Add(salesOrderLine);
                }

                _salesService.AddSalesOrder(salesOrder, true);

                salesorderDto.Id = salesOrder.Id;

                return(new ObjectResult(salesorderDto));
            }
            catch (Exception ex)
            {
                return(new ObjectResult(ex));
            }
        }
Пример #2
0
        public ActionResult SaveOrder(Models.ViewModels.Sales.SalesHeaderViewModel model)
        {
            SalesOrderHeader order = null;
            if (model.Id == 0)
            {
                order = new SalesOrderHeader();
                order.CreatedBy = User.Identity.Name;
                order.CreatedOn = DateTime.Now;
            }
            else
            {
                order = _salesService.GetSalesOrderById(model.Id);
            }

            order.ModifiedBy = User.Identity.Name;
            order.ModifiedOn = DateTime.Now;
            order.CustomerId = model.CustomerId.Value;
            order.PaymentTermId = model.PaymentTermId;
            order.Date = model.Date;

            foreach (var line in model.SalesLine.SalesLineItems)
            {
                SalesOrderLine lineItem = null;
                var item = _inventoryService.GetItemByNo(line.ItemNo);
                if (!line.Id.HasValue)
                {
                    lineItem = new SalesOrderLine();
                    lineItem.CreatedBy = User.Identity.Name;
                    lineItem.CreatedOn = DateTime.Now;
                    order.SalesOrderLines.Add(lineItem);
                }
                else
                {
                    lineItem = order.SalesOrderLines.Where(i => i.Id == line.Id).FirstOrDefault();
                }

                lineItem.ModifiedBy = User.Identity.Name;
                lineItem.ModifiedOn = DateTime.Now;
                lineItem.ItemId = line.ItemId;
                lineItem.MeasurementId = item.SellMeasurementId.Value;
                lineItem.Quantity = line.Quantity;
                lineItem.Discount = line.Discount;
                lineItem.Amount = line.Price;
            }

            if (model.Id == 0)
                _salesService.AddSalesOrder(order, true);
            else
                _salesService.UpdateSalesOrder(order);

            return RedirectToAction("SalesOrders");
        }
Пример #3
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));
            }
        }
Пример #4
0
        public IActionResult SaveSalesOrder([FromBody] Dto.Sales.SalesOrder salesOrderDto)
        {
            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 = salesOrderDto.Id == 0;
                Core.Domain.Sales.SalesOrderHeader salesOrder = null;

                if (isNew)
                {
                    salesOrder        = new Core.Domain.Sales.SalesOrderHeader();
                    salesOrder.Status = SalesOrderStatus.Open;
                }
                else
                {
                    salesOrder = _salesService.GetSalesOrderById(salesOrderDto.Id);
                }

                salesOrder.CustomerId    = salesOrderDto.CustomerId;
                salesOrder.Date          = salesOrderDto.OrderDate;
                salesOrder.PaymentTermId = salesOrderDto.PaymentTermId;
                salesOrder.ReferenceNo   = salesOrderDto.ReferenceNo;

                foreach (var line in salesOrderDto.SalesOrderLines)
                {
                    if (!isNew)
                    {
                        var existingLine = salesOrder.SalesOrderLines.Where(id => id.Id == line.Id).FirstOrDefault();
                        if (salesOrder.SalesOrderLines.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
                        {
                            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();

                            salesOrder.SalesOrderLines.Add(salesOrderLine);
                        }
                    }
                    else
                    {
                        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();

                        salesOrder.SalesOrderLines.Add(salesOrderLine);
                    }
                }

                if (isNew)
                {
                    _salesService.AddSalesOrder(salesOrder, true);
                }
                else
                {
                    var deleted = (from line in salesOrder.SalesOrderLines
                                   where !salesOrderDto.SalesOrderLines.Any(x => x.Id == line.Id)
                                   select line).ToList();

                    foreach (var line in deleted)
                    {
                        if (line.SalesInvoiceLines.Count() > 0)
                        {
                            throw new Exception("The line cannot be deleted. An invoice line is created from the item.");
                        }
                    }

                    foreach (var line in deleted)
                    {
                        salesOrder.SalesOrderLines.Remove(line);
                    }

                    _salesService.UpdateSalesOrder(salesOrder);
                }


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