Ejemplo n.º 1
0
        public string saveDONMH(DONMH_ViewModel donmh, List <D_DONMH_ViewModel> lstDdonmh, string nameLog)
        {
            try
            {
                using (var db = new RestaurantManagerDataEntities())
                {
                    //master
                    var check = db.DONMHs.FirstOrDefault(e => e.iddonmh == donmh.iddonmh);
                    if (check == null)
                    {
                        check                 = new DONMH();
                        check.CreateBy        = nameLog;
                        check.CreateDate      = DateTime.Now;
                        db.Entry(check).State = EntityState.Added;
                        db.DONMHs.Add(check);
                    }
                    else
                    {
                        check.ModifyBy        = nameLog;
                        check.ModifyDate      = DateTime.Now;
                        db.Entry(check).State = EntityState.Modified;
                    }
                    check.iddonmh   = donmh.iddonmh;
                    check.ngaydonmh = donmh.ngaydonmh;
                    check.idncc     = donmh.idncc;
                    db.SaveChanges();

                    //detail
                    var checkD = db.D_DONMH.Where(e => e.iddonmh == check.iddonmh).ToList();
                    if (checkD.Count() > 0)
                    {
                        db.D_DONMH.RemoveRange(checkD);
                    }
                    foreach (var item in lstDdonmh)
                    {
                        var detail = new D_DONMH
                        {
                            iddonmh    = check.iddonmh,
                            idhang     = item.idhang,
                            slmh       = item.slmh,
                            dongiamh   = int.Parse(item.dongiamh.ToString()), //đổi sang double
                            CreateBy   = nameLog,
                            CreateDate = DateTime.Now,
                        };
                        db.D_DONMH.Add(detail);
                    }
                    db.SaveChanges();
                    return("success");
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public string savePNHAP(PNHAP_ViewModel model, List <D_PNHAP_ViewModel> lstDetail, string nameLog)
        {
            using (var db = new RestaurantManagerDataEntities())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        //master
                        var check = db.PNHAPs.FirstOrDefault(e => e.idpnhap == model.idpnhap);
                        if (check == null)
                        {
                            check            = new PNHAP();
                            check.CreateBy   = nameLog;
                            check.CreateDate = DateTime.Now;
                            db.PNHAPs.Add(check);
                        }
                        else
                        {
                            check.ModifyBy        = nameLog;
                            check.ModifyDate      = DateTime.Now;
                            db.Entry(check).State = EntityState.Modified;
                        }
                        check.thukho        = model.thukho;
                        check.ngaynhap      = model.ngaynhap;
                        check.idpgiao       = model.idpgiao;
                        check.nguoigiao     = model.nguoigiao;
                        check.nguoilapphieu = nameLog;
                        db.SaveChanges();

                        //detail
                        var details = db.D_PNHAP.Where(p => p.idpnhap == check.idpnhap).ToList();
                        if (details.Count() > 0)
                        {
                            db.D_PNHAP.RemoveRange(details);
                        }
                        db.SaveChanges();
                        foreach (var item in lstDetail)
                        {
                            var detail = new D_PNHAP
                            {
                                idpnhap    = check.idpnhap,
                                idhang     = item.idhang,
                                slgiao     = item.slgiao,
                                slnhan     = item.slnhan,
                                CreateBy   = nameLog,
                                CreateDate = DateTime.Now,
                            };
                            db.D_PNHAP.Add(detail);
                        }
                        db.SaveChanges();
                        transaction.Commit();
                        return("success");
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return(ex.Message);
                    }
                }
            }
        }