Exemplo n.º 1
0
        public static SalesOrderViewModel CreateSalesOrderViewModelFromSalesOrder(SalesOrder salesOrder)
        {
            SalesOrderViewModel salesOrderViewModel = new SalesOrderViewModel()
                                                          {
                                                              CustomerName = salesOrder.CustomerName,
                                                              PoNumber = salesOrder.PoNumber,
                                                              SalesOrderId = salesOrder.SalesOrderId,
                                                              ObjectState = ObjectState.Unchanged
                                                          };

            foreach (SalesOrderItem salesOrderItem in salesOrder.SalesOrderItems)
            {
                SalesOrderItemViewModel salesOrderItemViewModel = new SalesOrderItemViewModel();
                salesOrderItemViewModel.SalesOrderItemId = salesOrderItem.SalesOrderItemId;
                salesOrderItemViewModel.ProductCode = salesOrderItem.ProductCode;
                salesOrderItemViewModel.Quantity = salesOrderItem.Quantity;
                salesOrderItemViewModel.UnitPrice = salesOrderItem.UnitPrice;
                salesOrderItemViewModel.ObjectState = ObjectState.Unchanged;
                salesOrderItemViewModel.SalesOrderId = salesOrderItem.SalesOrderId;

                salesOrderViewModel.SalesOrderItems.Add(salesOrderItemViewModel);
            }

            return salesOrderViewModel;
        }
Exemplo n.º 2
0
        public static SalesOrderViewModel CreateSalesOrderViewModelFromSalesOrder(SalesOrder salesOrder)
        {
            SalesOrderViewModel salesOrderViewModel = new SalesOrderViewModel()
            {
                CustomerName = salesOrder.CustomerName,
                PoNumber     = salesOrder.PoNumber,
                SalesOrderId = salesOrder.SalesOrderId,
                ObjectState  = ObjectState.Unchanged
            };


            foreach (SalesOrderItem salesOrderItem in salesOrder.SalesOrderItems)
            {
                SalesOrderItemViewModel salesOrderItemViewModel = new SalesOrderItemViewModel();
                salesOrderItemViewModel.SalesOrderItemId = salesOrderItem.SalesOrderItemId;
                salesOrderItemViewModel.ProductCode      = salesOrderItem.ProductCode;
                salesOrderItemViewModel.Quantity         = salesOrderItem.Quantity;
                salesOrderItemViewModel.UnitPrice        = salesOrderItem.UnitPrice;
                salesOrderItemViewModel.ObjectState      = ObjectState.Unchanged;
                salesOrderItemViewModel.SalesOrderId     = salesOrderItem.SalesOrderId;

                salesOrderViewModel.SalesOrderItems.Add(salesOrderItemViewModel);
            }


            return(salesOrderViewModel);
        }
Exemplo n.º 3
0
        public static SalesOrder CreateSalesOrderFromSalesOrderViewModel(SalesOrderViewModel salesOrderViewModel)
        {
            SalesOrder salesOrder = new SalesOrder()
            {
                CustomerName = salesOrderViewModel.CustomerName,
                PoNumber     = salesOrderViewModel.PoNumber,
                SalesOrderId = salesOrderViewModel.SalesOrderId,
                ObjectState  = salesOrderViewModel.ObjectState
            };

            int temporarySalesOrderItemId = -1;

            foreach (SalesOrderItemViewModel salesOrderItemViewModel in salesOrderViewModel.SalesOrderItems)
            {
                SalesOrderItem salesOrderItem = new SalesOrderItem();
                salesOrderItem.SalesOrderItemId = salesOrderItemViewModel.SalesOrderItemId;
                salesOrderItem.ProductCode      = salesOrderItemViewModel.ProductCode;
                salesOrderItem.Quantity         = salesOrderItemViewModel.Quantity;
                salesOrderItem.UnitPrice        = salesOrderItemViewModel.UnitPrice;
                salesOrderItem.ObjectState      = salesOrderItemViewModel.ObjectState;

                if (salesOrderItemViewModel.ObjectState != ObjectState.Added)
                {
                    salesOrderItem.SalesOrderItemId = salesOrderItemViewModel.SalesOrderItemId;
                }
                else
                {
                    salesOrderItem.SalesOrderItemId = temporarySalesOrderItemId;
                    temporarySalesOrderItemId--;
                }


                salesOrderItem.SalesOrderId = salesOrderViewModel.SalesOrderId;

                salesOrder.SalesOrderItems.Add(salesOrderItem);
            }

            return(salesOrder);
        }
Exemplo n.º 4
0
        public static SalesOrder CreateSalesOrderFromSalesOrderViewModel(SalesOrderViewModel orderViewModel)
        {
            var salesOrder = new SalesOrder();

            salesOrder.Id           = orderViewModel.Id;
            salesOrder.CustomerName = orderViewModel.CustomerName;
            salesOrder.PONumber     = orderViewModel.PONumber;
            salesOrder.ObjectState  = orderViewModel.ObjectState;
            salesOrder.RowVersion   = orderViewModel.RowVersion;

            //assign negative temporary PK so EF can distinguish entities (DB will disregard negatives)
            int tempItemId = -1;

            foreach (var itemViewModel in orderViewModel.Items)
            {
                var item = new SalesOrderItem();
                item.ProductCode = itemViewModel.ProductCode;
                item.Quantity    = itemViewModel.Quantity;
                item.UnitPrice   = itemViewModel.UnitPrice;
                item.ObjectState = itemViewModel.ObjectState;
                item.RowVersion  = itemViewModel.RowVersion;

                if (itemViewModel.ObjectState != ObjectState.Added)
                {
                    item.Id = itemViewModel.Id;
                }
                else
                {
                    item.Id = tempItemId;
                    tempItemId--;
                }
                item.SalesOrderId = itemViewModel.SalesOrderId;
                salesOrder.Items.Add(item);
            }

            return(salesOrder);
        }
Exemplo n.º 5
0
        public static SalesOrder CreateSalesOrderFromSalesOrderViewModel(SalesOrderViewModel salesOrderViewModel)
        {
            SalesOrder salesOrder = new SalesOrder()
            {
                CustomerName = salesOrderViewModel.CustomerName,
                PoNumber = salesOrderViewModel.PoNumber,
                SalesOrderId = salesOrderViewModel.SalesOrderId,
                ObjectState = salesOrderViewModel.ObjectState
            };

            int temporarySalesOrderItemId = -1;

            foreach (SalesOrderItemViewModel salesOrderItemViewModel in salesOrderViewModel.SalesOrderItems)
            {
                SalesOrderItem salesOrderItem = new SalesOrderItem();
                salesOrderItem.SalesOrderItemId = salesOrderItemViewModel.SalesOrderItemId;
                salesOrderItem.ProductCode = salesOrderItemViewModel.ProductCode;
                salesOrderItem.Quantity = salesOrderItemViewModel.Quantity;
                salesOrderItem.UnitPrice = salesOrderItemViewModel.UnitPrice;
                salesOrderItem.ObjectState = salesOrderItemViewModel.ObjectState;

                if (salesOrderItemViewModel.ObjectState != ObjectState.Added)
                {
                    salesOrderItem.SalesOrderItemId = salesOrderItemViewModel.SalesOrderItemId;
                } else {
                    salesOrderItem.SalesOrderItemId = temporarySalesOrderItemId;
                    temporarySalesOrderItemId--;
                }

                salesOrderItem.SalesOrderId = salesOrderViewModel.SalesOrderId;

                salesOrder.SalesOrderItems.Add(salesOrderItem);
            }

            return salesOrder;
        }
Exemplo n.º 6
0
        public static SalesOrderViewModel CreateSalesOrderViewModelFromSalesOrder(SalesOrder salesOrder)
        {
            var salesOrderViewModel = new SalesOrderViewModel();

            salesOrderViewModel.Id           = salesOrder.Id;
            salesOrderViewModel.CustomerName = salesOrder.CustomerName;
            salesOrderViewModel.PONumber     = salesOrder.PONumber;
            salesOrderViewModel.ObjectState  = ObjectState.Unchanged;
            salesOrderViewModel.RowVersion   = salesOrder.RowVersion;

            foreach (var item in salesOrder.Items)
            {
                var itemViewModel = new SalesOrderItemViewModel();
                itemViewModel.Id           = item.Id;
                itemViewModel.ProductCode  = item.ProductCode;
                itemViewModel.Quantity     = item.Quantity;
                itemViewModel.UnitPrice    = item.UnitPrice;
                itemViewModel.ObjectState  = ObjectState.Unchanged;
                itemViewModel.SalesOrderId = item.SalesOrderId;
                itemViewModel.RowVersion   = item.RowVersion;
                salesOrderViewModel.Items.Add(itemViewModel);
            }
            return(salesOrderViewModel);
        }
Exemplo n.º 7
0
 // GET: Sales/Create
 public ActionResult Create()
 {
     SalesOrderViewModel salesOrderViewModel = new SalesOrderViewModel();
     salesOrderViewModel.ObjectState = ObjectState.Added;
     return View(salesOrderViewModel);
 }
Exemplo n.º 8
0
        public JsonResult Save(SalesOrderViewModel salesOrderViewModel)
        {
            SalesOrder salesOrder = ViewModels.Helpers.CreateSalesOrderFromSalesOrderViewModel(salesOrderViewModel);

            _salesContext.SalesOrders.Attach(salesOrder);
            _salesContext.ApplyStateChanges();
            _salesContext.SaveChanges();

            if (salesOrderViewModel.ObjectState == ObjectState.Deleted)
            {
                return Json(new { newLocation = "/Sales/Index/" });
            }

            string messageToClient = ViewModels.Helpers.GetMessageToClient(salesOrderViewModel.ObjectState, salesOrder.CustomerName);

            salesOrderViewModel = ViewModels.Helpers.CreateSalesOrderViewModelFromSalesOrder(salesOrder);

            salesOrderViewModel.MessageToClient = messageToClient;

            return Json(new { salesOrderViewModel });
        }