Пример #1
0
        public ActionResult Add(PurchaseOrderViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (POSContext context = new POSContext())
                {
                    using (var dbTransaction = context.Database.BeginTransaction())
                    {
                        int           UserId  = User.Identity.GetUserId <int>();
                        string        OrderNo = PurchaseOrderDAL.OrderNo();
                        PurchaseOrder order   = new PurchaseOrder()
                        {
                            OutletID   = model.OutletID,
                            OrderNo    = OrderNo,
                            StatusID   = 1,
                            SupplierID = model.SupplierID,
                            Notes      = model.Notes,
                            Total      = model.Total,
                            CreatedBy  = UserId,
                            CreatedOn  = DateTime.Now,
                            ModifiedBy = UserId,
                            ModifiedOn = DateTime.Now
                        };
                        context.TPurchaseOrder.Add(order);
                        context.SaveChanges();


                        PurchaseOrderHistory orderhis = new PurchaseOrderHistory()
                        {
                            HeaderID   = order.ID,
                            StatusID   = 1,
                            CreatedBy  = UserId,
                            CreatedOn  = DateTime.Now,
                            ModifiedBy = UserId,
                            ModifiedOn = DateTime.Now
                        };
                        context.TPurchaseOrderHistory.Add(orderhis);
                        context.SaveChanges();
                        foreach (var item in model.VariantID)
                        {
                            PurchaseOrderDetail orderdetail = new PurchaseOrderDetail()
                            {
                                HeaderID   = order.ID,
                                Quantity   = model.Quantity,
                                SubTotal   = model.SubTotal,
                                VariantID  = item,
                                UnitCost   = model.UnitCost,
                                CreatedBy  = UserId,
                                CreatedOn  = DateTime.Now,
                                ModifiedBy = UserId,
                                ModifiedOn = DateTime.Now
                            };
                            context.TPurchaseOrderDetail.Add(orderdetail);
                        }
                        context.SaveChanges();


                        try
                        {
                            dbTransaction.Commit();
                            return(RedirectToAction("Index"));
                        }
                        catch (Exception)
                        {
                            dbTransaction.Rollback();
                        }
                    }
                }
            }
            return(PartialView("Add", model));
        }
Пример #2
0
        public ActionResult Edit(ListPurchaseOrderViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (POSContext context = new POSContext())
                {
                    using (var dbTransaction = context.Database.BeginTransaction())
                    {
                        int           UserId  = User.Identity.GetUserId <int>();
                        string        OrderNo = PurchaseOrderDAL.OrderNo();
                        PurchaseOrder order   = context.TPurchaseOrder.Where(m => m.ID == model.ID).FirstOrDefault();
                        if (order != null)
                        {
                            order.OutletID   = model.OutletID;
                            order.SupplierID = model.SupplierID;
                            order.Notes      = model.Notes;
                            order.Total      = model.Total;
                            order.ModifiedBy = UserId;
                            order.ModifiedOn = DateTime.Now;
                        }

                        context.SaveChanges();

                        int i = 0;
                        foreach (var item in model.VariantID)
                        {
                            int POid = model.PODetailID[i];
                            PurchaseOrderDetail orderdetail = context.TPurchaseOrderDetail.Where(m => m.ID == POid).FirstOrDefault();
                            if (orderdetail != null)
                            {
                                orderdetail.HeaderID   = order.ID;
                                orderdetail.Quantity   = model.Quantity[i];
                                orderdetail.SubTotal   = model.SubTotal[i];
                                orderdetail.VariantID  = item;
                                orderdetail.UnitCost   = model.UnitCost[i];
                                orderdetail.ModifiedBy = UserId;
                                orderdetail.ModifiedOn = DateTime.Now;
                            }
                            else
                            {
                                PurchaseOrderDetail orderdetail2 = new PurchaseOrderDetail()
                                {
                                    HeaderID   = order.ID,
                                    Quantity   = model.Quantity[i],
                                    SubTotal   = model.SubTotal[i],
                                    VariantID  = item,
                                    UnitCost   = model.UnitCost[i],
                                    CreatedBy  = UserId,
                                    CreatedOn  = DateTime.Now,
                                    ModifiedBy = UserId,
                                    ModifiedOn = DateTime.Now
                                };
                                context.TPurchaseOrderDetail.Add(orderdetail2);
                            }
                            i++;
                        }
                        context.SaveChanges();


                        try
                        {
                            dbTransaction.Commit();
                            return(RedirectToAction("Index"));
                        }
                        catch (Exception)
                        {
                            dbTransaction.Rollback();
                        }
                    }
                }
            }
            return(PartialView("Edit", model));
        }