예제 #1
0
        public ActionResult Import_Hangtonkho(HttpPostedFileBase file)
        {
            try
            {
                if (Request != null)
                {
                    HttpPostedFileBase filetonkho = Request.Files["UploadedFile"];
                    if ((filetonkho != null) && (filetonkho.ContentLength > 0) && !string.IsNullOrEmpty(filetonkho.FileName))
                    {
                        string fileName        = filetonkho.FileName;
                        string fileContentType = filetonkho.ContentType;
                        byte[] fileBytes       = new byte[filetonkho.ContentLength];
                        var    data            = filetonkho.InputStream.Read(fileBytes, 0, Convert.ToInt32(filetonkho.ContentLength));
                        //var usersList = new List<Users>();
                        using (var package = new ExcelPackage(filetonkho.InputStream))
                        {
                            var currentSheet = package.Workbook.Worksheets;
                            var workSheet    = currentSheet.First();
                            var noOfCol      = workSheet.Dimension.End.Column;
                            var noOfRow      = workSheet.Dimension.End.Row;
                            for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                            {
                                TONKHO_HOPLONG tonkho = new TONKHO_HOPLONG();
                                tonkho.MA_HANG    = workSheet.Cells[rowIterator, 2].Value.ToString();
                                tonkho.MA_KHO_CON = workSheet.Cells[rowIterator, 3].Value.ToString();
                                tonkho.SL_HOPLONG = Convert.ToInt32(workSheet.Cells[rowIterator, 4].Value.ToString());

                                db.TONKHO_HOPLONG.Add(tonkho);


                                db.SaveChanges();
                                so_dong_thanh_cong++;
                                dong = rowIterator;
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                ViewBag.Error       = " Đã xảy ra lỗi, Liên hệ ngay với admin. " + Environment.NewLine + " Thông tin chi tiết về lỗi:" + Environment.NewLine + Ex;
                ViewBag.Information = "Lỗi tại dòng thứ: " + dong;
            }
            finally
            {
                ViewBag.Message = "Đã import thành công " + so_dong_thanh_cong + " dòng";
            }

            return(View("Import_Hanghoa"));
        }
        public IHttpActionResult PutKHO_XUAT_KHO(XuatKho kho_xuatkho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Lưu thông tin nhập kho
            var xk = db.KHO_XUAT_KHO.Where(x => x.SO_CHUNG_TU == kho_xuatkho.SO_CHUNG_TU).FirstOrDefault();

            xk.NGAY_CHUNG_TU   = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_CHUNG_TU);
            xk.NGAY_HACH_TOAN  = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_HACH_TOAN);
            xk.SO_CHUNG_TU     = kho_xuatkho.SO_CHUNG_TU;
            xk.NGUOI_NHAN      = kho_xuatkho.NGUOI_NHAN;
            xk.KHACH_HANG      = kho_xuatkho.KHACH_HANG;
            xk.NGUOI_LAP_PHIEU = kho_xuatkho.NGUOI_LAP_PHIEU;
            xk.TRUC_THUOC      = kho_xuatkho.TRUC_THUOC;
            xk.LOAI_XUAT_KHO   = kho_xuatkho.LOAI_XUAT_KHO;

            //Lưu thông tin tham chiếu
            if (kho_xuatkho.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_xuatkho.ThamChieu)
                {
                    var newItem = db.XL_THAM_CHIEU_CHUNG_TU.Where(x => x.SO_CHUNG_TU_GOC == xk.SO_CHUNG_TU).FirstOrDefault();
                    if (newItem != null)
                    {
                        //newItem.SO_CHUNG_TU_GOC = xk.SO_CHUNG_TU;
                        newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    }
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //TONKHO_HOPLONG HHTon = new TONKHO_HOPLONG();
            //HH_NHOM_VTHH NhomHang = new HH_NHOM_VTHH();
            if (kho_xuatkho.ChiTietPX != null && kho_xuatkho.ChiTietPX.Count > 0)
            {
                foreach (ChiTietPhieuXuatKho item in kho_xuatkho.ChiTietPX)
                {
                    var newItem = db.KHO_CT_XUAT_KHO.Where(x => x.SO_CHUNG_TU == xk.SO_CHUNG_TU).FirstOrDefault();
                    int sl_cu   = newItem.SO_LUONG;
                    if (newItem != null)
                    {
                        newItem.SO_CHUNG_TU = xk.SO_CHUNG_TU;
                        newItem.MA_HANG     = item.MA_HANG;
                        newItem.TK_CO       = item.TK_CO;
                        newItem.TK_NO       = item.TK_NO;
                        newItem.DVT         = item.DVT;
                        newItem.DON_GIA_BAN = Convert.ToDecimal(item.DON_GIA_BAN);
                        newItem.DON_GIA_VON = Convert.ToDecimal(item.DON_GIA_VON);
                        newItem.SO_LUONG    = Convert.ToInt32(item.SO_LUONG);
                        newItem.THANH_TIEN  = newItem.DON_GIA_BAN * newItem.SO_LUONG;
                        tongtien           += newItem.THANH_TIEN;
                        newItem.TK_KHO      = item.TK_KHO;
                    }

                    //Cập nhật hàng tồn
                    TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG).FirstOrDefault();
                    newHangTon.SL_HOPLONG = newHangTon.SL_HOPLONG + sl_cu;
                    if (newHangTon == null || newHangTon.SL_HOPLONG < item.SO_LUONG)
                    {
                        return(BadRequest("Hàng không có trong kho hoặc SL tồn không đủ"));
                    }
                    else
                    {
                        newHangTon.SL_HOPLONG -= Convert.ToInt32(item.SO_LUONG);
                    }
                    //if (newHangTon == null)
                    //{
                    //    db.TONKHO_HOPLONG.Add(newHangTon);
                    //}
                    ////Cập nhật nhóm hàng
                    //TONKHO_HANG hangton = NhomHang.GetNhomHang(item.MaHang);
                    //if (hangton != null)
                    //{
                    //    hangton.SL_HANG = Convert.ToInt32(item.SoLuong);
                    //}
                }
            }

            xk.TONG_TIEN = tongtien;


            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(xk.SO_CHUNG_TU));
        }
        public IHttpActionResult PostKHO_XUAT_KHO(XuatKho kho_xuatkho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //Lưu thông tin nhập kho
            KHO_XUAT_KHO xk = new KHO_XUAT_KHO();

            xk.NGAY_CHUNG_TU  = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_CHUNG_TU);
            xk.NGAY_HACH_TOAN = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_HACH_TOAN);
            xk.SO_CHUNG_TU    = GeneralChungTu();
            xk.NGUOI_NHAN     = kho_xuatkho.NGUOI_NHAN;
            xk.KHACH_HANG     = kho_xuatkho.KHACH_HANG;

            xk.NGUOI_LAP_PHIEU = kho_xuatkho.NGUOI_LAP_PHIEU;
            xk.TRUC_THUOC      = kho_xuatkho.TRUC_THUOC;

            xk.LOAI_XUAT_KHO = kho_xuatkho.LOAI_XUAT_KHO;
            db.KHO_XUAT_KHO.Add(xk);

            //Lưu thông tin tham chiếu
            if (kho_xuatkho.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_xuatkho.ThamChieu)
                {
                    XL_THAM_CHIEU_CHUNG_TU newItem = new XL_THAM_CHIEU_CHUNG_TU();
                    newItem.SO_CHUNG_TU_GOC        = xk.SO_CHUNG_TU;
                    newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    db.XL_THAM_CHIEU_CHUNG_TU.Add(newItem);
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //TONKHO_HOPLONG HHTon = new TONKHO_HOPLONG();
            //HH_NHOM_VTHH NhomHang = new HH_NHOM_VTHH();
            if (kho_xuatkho.ChiTiet != null && kho_xuatkho.ChiTiet.Count > 0)
            {
                foreach (ChiTietXuatKho item in kho_xuatkho.ChiTiet)
                {
                    KHO_CT_XUAT_KHO newItem = new KHO_CT_XUAT_KHO();
                    newItem.SO_CHUNG_TU = xk.SO_CHUNG_TU;
                    newItem.MA_HANG     = item.MaHang;
                    newItem.TK_CO       = item.TKCo;
                    newItem.TK_NO       = item.TKNo;
                    newItem.DVT         = item.DVT;
                    newItem.DON_GIA_BAN = Convert.ToDecimal(item.DonGia);
                    newItem.DON_GIA_VON = Convert.ToDecimal(item.DonGiaVon);
                    newItem.SO_LUONG    = Convert.ToInt32(item.SoLuong);
                    newItem.THANH_TIEN  = newItem.DON_GIA_BAN * newItem.SO_LUONG;
                    tongtien           += newItem.THANH_TIEN;
                    newItem.TK_KHO      = item.TKKho;
                    db.KHO_CT_XUAT_KHO.Add(newItem);
                    //Cập nhật hàng tồn
                    TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MaHang).FirstOrDefault();
                    if (newHangTon == null || newHangTon.SL_HOPLONG < item.SoLuong)
                    {
                        return(BadRequest("Hàng không có trong kho hoặc SL tồn không đủ"));
                    }
                    newHangTon.SL_HOPLONG -= Convert.ToInt32(item.SoLuong);
                    //if (newHangTon == null)
                    //{
                    //    db.TONKHO_HOPLONG.Add(newHangTon);
                    //}
                    ////Cập nhật nhóm hàng
                    //TONKHO_HANG hangton = NhomHang.GetNhomHang(item.MaHang);
                    //if (hangton != null)
                    //{
                    //    hangton.SL_HANG = Convert.ToInt32(item.SoLuong);
                    //}
                }
            }

            xk.TONG_TIEN = tongtien;
            db.SaveChanges();

            return(Ok(xk.SO_CHUNG_TU));
            //try
            //{
            //    db.SaveChanges();
            //}
            //catch (DbUpdateException)
            //{
            //    if (KHO_XUAT_KHOExists(kho_xuatkho.SO_CHUNG_TU))
            //    {
            //        return Conflict();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return Ok(xk.SO_CHUNG_TU);
        }
        public IHttpActionResult PostKHO_XUAT_KHO(XuatKhoBH kho_xuatkho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //Lưu thông tin nhập kho
            KHO_XUAT_KHO xk = new KHO_XUAT_KHO();

            xk.NGAY_CHUNG_TU      = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_CHUNG_TU);
            xk.NGAY_HACH_TOAN     = GeneralFunction.ConvertToTime(kho_xuatkho.NGAY_HACH_TOAN);
            xk.SO_CHUNG_TU        = GeneralChungTu();
            xk.NGUOI_NHAN         = kho_xuatkho.NGUOI_NHAN;
            xk.KHACH_HANG         = kho_xuatkho.KHACH_HANG;
            xk.LY_DO_XUAT         = kho_xuatkho.LY_DO_XUAT;
            xk.NHAN_VIEN_BAN_HANG = kho_xuatkho.NHAN_VIEN_BAN_HANG;
            xk.NGUOI_LAP_PHIEU    = kho_xuatkho.NGUOI_LAP_PHIEU;
            xk.TRUC_THUOC         = "HOPLONG";
            xk.LOAI_XUAT_KHO      = kho_xuatkho.LOAI_XUAT_KHO;
            xk.PHIEU_BAN_HANG     = kho_xuatkho.PHIEU_BAN_HANG;
            db.KHO_XUAT_KHO.Add(xk);

            // Lưu Đơn bán hàng
            BH_DON_BAN_HANG dbh = db.BH_DON_BAN_HANG.Where(x => x.MA_SO_BH == xk.PHIEU_BAN_HANG).FirstOrDefault();

            if (dbh.DA_XUAT_KHO == false)
            {
                dbh.DA_XUAT_KHO = true;
            }


            //Lưu thông tin tham chiếu
            if (kho_xuatkho.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_xuatkho.ThamChieu)
                {
                    XL_THAM_CHIEU_CHUNG_TU newItem = new XL_THAM_CHIEU_CHUNG_TU();
                    newItem.SO_CHUNG_TU_GOC        = xk.SO_CHUNG_TU;
                    newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    db.XL_THAM_CHIEU_CHUNG_TU.Add(newItem);
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //TONKHO_HOPLONG HHTon = new TONKHO_HOPLONG();
            //HH_NHOM_VTHH NhomHang = new HH_NHOM_VTHH();
            if (kho_xuatkho.ChiTiet != null && kho_xuatkho.ChiTiet.Count > 0)
            {
                foreach (ChiTietXuatKhoBH item in kho_xuatkho.ChiTiet)
                {
                    KHO_CT_XUAT_KHO newItem = new KHO_CT_XUAT_KHO();
                    newItem.SO_CHUNG_TU = xk.SO_CHUNG_TU;
                    newItem.MA_HANG     = item.MA_HANG;

                    newItem.MA_DIEU_CHINH = item.MA_DIEU_CHINH;

                    newItem.MA_KHO_CON  = item.MA_KHO_CON;
                    newItem.TK_CO       = item.TK_CO;
                    newItem.TK_NO       = item.TK_NO;
                    newItem.DVT         = item.DVT;
                    newItem.DON_GIA_BAN = Convert.ToDecimal(item.DON_GIA);
                    newItem.DON_GIA_VON = Convert.ToDecimal(item.DON_GIA_VON);
                    newItem.SO_LUONG    = Convert.ToInt32(item.SO_LUONG);
                    newItem.THANH_TIEN  = newItem.DON_GIA_BAN * newItem.SO_LUONG;
                    tongtien           += newItem.THANH_TIEN;
                    newItem.TK_KHO      = item.TK_HACH_TOAN_KHO;
                    db.KHO_CT_XUAT_KHO.Add(newItem);
                    //Cập nhật hàng tồn
                    TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();
                    if (newHangTon == null || newHangTon.SL_HOPLONG < item.SO_LUONG)
                    {
                        return(Ok("Hàng không có trong kho hoặc SL tồn không đủ"));
                    }
                    newHangTon.SL_HOPLONG -= Convert.ToInt32(item.SO_LUONG);
                    //if (newHangTon == null)
                    //{
                    //    db.TONKHO_HOPLONG.Add(newHangTon);
                    //}
                    ////Cập nhật nhóm hàng
                    //TONKHO_HANG hangton = NhomHang.GetNhomHang(item.MA_HANG);
                    //if (hangton != null)
                    //{
                    //    hangton.SL_HANG = Convert.ToInt32(item.SO_LUONG);
                    //}
                    // Lưu Nhật ký
                    KT_SO_NHAT_KY_CHUNG sonhatky = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky.NGAY_CHUNG_TU  = xk.NGAY_CHUNG_TU;
                    sonhatky.NGAY_HACH_TOAN = xk.NGAY_HACH_TOAN;
                    if (xk.NGUOI_NHAN == null)
                    {
                        sonhatky.DOI_TUONG = xk.KHACH_HANG;
                    }
                    else
                    {
                        sonhatky.DOI_TUONG = xk.NGUOI_NHAN;
                    }

                    sonhatky.TRUC_THUOC          = "HOPLONG";
                    sonhatky.DIEN_GIAI_CHUNG     = xk.LY_DO_XUAT;
                    sonhatky.DIEN_GIAI_CHI_TIET  = xk.LY_DO_XUAT;
                    sonhatky.TAI_KHOAN_HACH_TOAN = newItem.TK_NO;
                    sonhatky.TAI_KHOAN_DOI_UNG   = newItem.TK_CO;
                    sonhatky.PHAT_SINH_NO        = tongtien;
                    sonhatky.PHAT_SINH_CO        = 0;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky);
                    KT_SO_NHAT_KY_CHUNG sonhatky1 = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky1.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky1.NGAY_CHUNG_TU  = xk.NGAY_CHUNG_TU;
                    sonhatky1.NGAY_HACH_TOAN = xk.NGAY_HACH_TOAN;
                    if (xk.NGUOI_NHAN == null)
                    {
                        sonhatky1.DOI_TUONG = xk.KHACH_HANG;
                    }
                    else
                    {
                        sonhatky1.DOI_TUONG = xk.NGUOI_NHAN;
                    }
                    sonhatky1.TRUC_THUOC          = "HOPLONG";
                    sonhatky1.DIEN_GIAI_CHUNG     = xk.LY_DO_XUAT;
                    sonhatky1.DIEN_GIAI_CHI_TIET  = xk.LY_DO_XUAT;
                    sonhatky1.TAI_KHOAN_HACH_TOAN = newItem.TK_CO;
                    sonhatky1.TAI_KHOAN_DOI_UNG   = newItem.TK_NO;
                    sonhatky1.PHAT_SINH_NO        = 0;
                    sonhatky1.PHAT_SINH_CO        = tongtien;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky1);
                }
            }

            xk.TONG_TIEN = tongtien;



            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (KHO_XUAT_KHOExists(kho_xuatkho.SO_CHUNG_TU))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(xk.SO_CHUNG_TU));
        }
예제 #5
0
        public IHttpActionResult PutKHO_NHAP_KHO(NhapKho kho_NhapKho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Lưu thông tin nhập kho
            var nk = db.KHO_NHAP_KHO.Where(x => x.SO_CHUNG_TU == kho_NhapKho.SO_CHUNG_TU).FirstOrDefault();

            nk.NGAY_CHUNG_TU   = GeneralFunction.ConvertToTime(kho_NhapKho.NGAY_CHUNG_TU);
            nk.NGAY_HACH_TOAN  = GeneralFunction.ConvertToTime(kho_NhapKho.NGAY_HACH_TOAN);
            nk.SO_CHUNG_TU     = kho_NhapKho.SO_CHUNG_TU;
            nk.NGUOI_GIAO_HANG = kho_NhapKho.NGUOI_GIAO_HANG;
            nk.NGUOI_LAP_PHIEU = kho_NhapKho.NGUOI_LAP_PHIEU;
            nk.MA_DOI_TUONG    = kho_NhapKho.MA_DOI_TUONG;
            nk.DIEN_GIAI       = kho_NhapKho.DIEN_GIAI;
            nk.TRUC_THUOC      = "HOPLONG";
            nk.LOAI_NHAP_KHO   = kho_NhapKho.LOAI_NHAP_KHO;
            //Lưu thông tin tham chiếu
            if (kho_NhapKho.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_NhapKho.ThamChieu)
                {
                    var newItem = db.XL_THAM_CHIEU_CHUNG_TU.Where(x => x.SO_CHUNG_TU_GOC == nk.SO_CHUNG_TU).FirstOrDefault();
                    if (newItem != null)
                    {
                        //newItem.SO_CHUNG_TU_GOC = xk.SO_CHUNG_TU;
                        newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    }
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //TONKHO_HOPLONG HHTon = new TONKHO_HOPLONG();
            //HH_NHOM_VTHH NhomHang = new HH_NHOM_VTHH();
            if (kho_NhapKho.ChiTiet != null && kho_NhapKho.ChiTiet.Count > 0)
            {
                foreach (ChiTietNhapKho item in kho_NhapKho.ChiTiet)
                {
                    var newItem = db.KHO_CT_NHAP_KHO.Where(x => x.SO_CHUNG_TU == nk.SO_CHUNG_TU && x.MA_HANG == item.MA_HANG).FirstOrDefault();
                    int sl_cu   = newItem.SO_LUONG;
                    if (newItem != null)
                    {
                        newItem.SO_CHUNG_TU = nk.SO_CHUNG_TU;
                        newItem.MA_HANG     = item.MA_HANG;
                        newItem.TK_CO       = item.TK_CO;
                        newItem.TK_NO       = item.TK_NO;
                        newItem.DVT         = item.DVT;
                        newItem.DON_GIA     = Convert.ToDecimal(item.DON_GIA);
                        newItem.SO_LUONG    = Convert.ToInt32(item.SO_LUONG);
                        newItem.THANH_TIEN  = newItem.DON_GIA * newItem.SO_LUONG;
                        tongtien           += newItem.THANH_TIEN;
                        newItem.TK_KHO      = item.TK_KHO;
                    }

                    //Cập nhật hàng tồn
                    TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG).FirstOrDefault();
                    newHangTon.SL_HOPLONG  = newHangTon.SL_HOPLONG - sl_cu;
                    newHangTon.SL_HOPLONG += Convert.ToInt32(item.SO_LUONG);
                    //if (newHangTon == null)
                    //{
                    //    db.TONKHO_HOPLONG.Add(newHangTon);
                    //}
                    ////Cập nhật nhóm hàng
                    //TONKHO_HANG hangton = NhomHang.GetNhomHang(item.MA_HANG);
                    //if (hangton != null)
                    //{
                    //    hangton.SL_HANG = Convert.ToInt32(item.SO_LUONG);
                    //}
                }
            }

            nk.TONG_TIEN = tongtien;


            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(nk.SO_CHUNG_TU));
        }
예제 #6
0
        public IHttpActionResult PostKHO_NHAP_KHOMH(NhapKhoMH kho_NhapKhoMH)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //Lưu thông tin nhập kho
            KHO_NHAP_KHO nk = new KHO_NHAP_KHO();

            nk.NGAY_CHUNG_TU      = GeneralFunction.ConvertToTime(kho_NhapKhoMH.NGAY_CHUNG_TU);
            nk.NGAY_HACH_TOAN     = GeneralFunction.ConvertToTime(kho_NhapKhoMH.NGAY_HACH_TOAN);
            nk.SO_CHUNG_TU        = GeneralChungTu();
            nk.NGUOI_LAP_PHIEU    = kho_NhapKhoMH.NGUOI_LAP_PHIEU;
            nk.MA_DOI_TUONG       = kho_NhapKhoMH.MA_DOI_TUONG;
            nk.NHAN_VIEN_MUA_HANG = kho_NhapKhoMH.NHAN_VIEN_MUA_HANG;
            nk.DIEN_GIAI          = kho_NhapKhoMH.DIEN_GIAI;
            nk.TRUC_THUOC         = "HOPLONG";
            nk.LOAI_NHAP_KHO      = kho_NhapKhoMH.LOAI_NHAP_KHO;
            db.KHO_NHAP_KHO.Add(nk);

            //Lưu thông tin tham chiếu
            if (kho_NhapKhoMH.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_NhapKhoMH.ThamChieu)
                {
                    XL_THAM_CHIEU_CHUNG_TU newItem = new XL_THAM_CHIEU_CHUNG_TU();
                    newItem.SO_CHUNG_TU_GOC        = nk.SO_CHUNG_TU;
                    newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    db.XL_THAM_CHIEU_CHUNG_TU.Add(newItem);
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //HHTONKHOViewModels HHTon = new HHTONKHOViewModels();
            //NhomHangViewModels NhomHang = new NhomHangViewModels();
            if (kho_NhapKhoMH.ChiTiet != null && kho_NhapKhoMH.ChiTiet.Count > 0)
            {
                foreach (ChiTietNhapKhoMH item in kho_NhapKhoMH.ChiTiet)
                {
                    KHO_CT_NHAP_KHO newItem = new KHO_CT_NHAP_KHO();
                    newItem.SO_CHUNG_TU = nk.SO_CHUNG_TU;
                    newItem.MA_HANG     = item.MA_HANG;
                    newItem.MA_KHO_CON  = item.MA_KHO_CON;
                    newItem.TK_CO       = item.TK_CO;
                    newItem.TK_NO       = item.TK_NO;
                    newItem.DVT         = item.DVT;
                    newItem.DON_GIA     = Convert.ToDecimal(item.DON_GIA_CHUA_VAT);
                    newItem.SO_LUONG    = Convert.ToInt32(item.SL);
                    newItem.THANH_TIEN  = Convert.ToDecimal(item.THANH_TIEN_CHUA_VAT);
                    tongtien           += newItem.THANH_TIEN;
                    newItem.TK_KHO      = item.TK_KHO;
                    db.KHO_CT_NHAP_KHO.Add(newItem);


                    //Cập nhật hàng tồn
                    var newkhogiu = db.KHO_HANG_CAN_GIU.Where(x => x.MA_HANG == item.MA_HANG).ToList();
                    soluongcon = newItem.SO_LUONG;
                    foreach (var abc in newkhogiu)
                    {
                        if (newItem.SO_LUONG > abc.SL_CAN_GIU)
                        {
                            TONKHO_HOPLONG newhanggiu = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == "IVHOPLONG05").FirstOrDefault();
                            if (newhanggiu != null)
                            {
                                newhanggiu.SL_HOPLONG += abc.SL_CAN_GIU;
                            }
                            var quezzzry = db.KHO_GIU_HANG.Where(x => x.ID_CT_PO == abc.ID_PO_BAN).FirstOrDefault();
                            if (quezzzry != null)
                            {
                                quezzzry.SL_GIU = abc.SL_CAN_GIU + quezzzry.SL_GIU;
                            }
                            abc.SL_CAN_GIU   = 0;
                            newItem.SO_LUONG = newItem.SO_LUONG - abc.SL_CAN_GIU;

                            TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();
                            if (newHangTon == null)
                            {
                                newHangTon            = new TONKHO_HOPLONG();
                                newHangTon.MA_HANG    = item.MA_HANG;
                                newHangTon.MA_KHO_CON = item.MA_KHO_CON;
                                newHangTon.SL_HOPLONG = newItem.SO_LUONG;
                                db.TONKHO_HOPLONG.Add(newHangTon);
                            }
                            else
                            {
                                newHangTon.SL_HOPLONG += newItem.SO_LUONG;
                            }
                        }
                        if (newItem.SO_LUONG < abc.SL_CAN_GIU)
                        {
                            TONKHO_HOPLONG newhanggiu = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == "IVHOPLONG05").FirstOrDefault();
                            if (newhanggiu != null)
                            {
                                newhanggiu.SL_HOPLONG += abc.SL_CAN_GIU;
                            }
                            var quezzzry = db.KHO_GIU_HANG.Where(x => x.ID_CT_PO == abc.ID_PO_BAN).FirstOrDefault();
                            if (quezzzry != null)
                            {
                                quezzzry.SL_GIU = abc.SL_CAN_GIU + quezzzry.SL_GIU;
                            }
                            abc.SL_CAN_GIU   = abc.SL_CAN_GIU - newItem.SO_LUONG;
                            newItem.SO_LUONG = 0;
                            TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();
                            if (newHangTon == null)
                            {
                                newHangTon            = new TONKHO_HOPLONG();
                                newHangTon.MA_HANG    = item.MA_HANG;
                                newHangTon.MA_KHO_CON = item.MA_KHO_CON;
                                newHangTon.SL_HOPLONG = newItem.SO_LUONG;
                                db.TONKHO_HOPLONG.Add(newHangTon);
                            }
                            else
                            {
                                newHangTon.SL_HOPLONG += newItem.SO_LUONG;
                            }
                        }
                        if (newItem.SO_LUONG == abc.SL_CAN_GIU)
                        {
                            TONKHO_HOPLONG newhanggiu = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == "IVHOPLONG05").FirstOrDefault();
                            if (newhanggiu != null)
                            {
                                newhanggiu.SL_HOPLONG += abc.SL_CAN_GIU;
                            }
                            var quezzzry = db.KHO_GIU_HANG.Where(x => x.ID_CT_PO == abc.ID_PO_BAN).FirstOrDefault();
                            if (quezzzry != null)
                            {
                                quezzzry.SL_GIU = abc.SL_CAN_GIU + quezzzry.SL_GIU;
                            }
                            abc.SL_CAN_GIU   = 0;
                            newItem.SO_LUONG = 0;
                            TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();
                            if (newHangTon == null)
                            {
                                newHangTon            = new TONKHO_HOPLONG();
                                newHangTon.MA_HANG    = item.MA_HANG;
                                newHangTon.MA_KHO_CON = item.MA_KHO_CON;
                                newHangTon.SL_HOPLONG = newItem.SO_LUONG;
                                db.TONKHO_HOPLONG.Add(newHangTon);
                            }
                            else
                            {
                                newHangTon.SL_HOPLONG += newItem.SO_LUONG;
                            }
                        }
                    }



                    var query = db.MH_CT_DE_NGHI_NHAP_KHO.Where(x => x.MA_HANG == newItem.MA_HANG && x.MA_SO_DN == kho_NhapKhoMH.MA_SO_DN).FirstOrDefault();
                    if (query != null)
                    {
                        IDDeNghi = query.ID;
                    }
                    ;
                    MH_DE_NGHI_JOIN_PO_MH dnmh = db.MH_DE_NGHI_JOIN_PO_MH.Where(x => x.ID_DE_NGHI == IDDeNghi).FirstOrDefault();
                    MH_PO_CT_MUA_HANG     pomh = db.MH_PO_CT_MUA_HANG.Where(x => x.ID == dnmh.ID_PO_DAT_HANG).FirstOrDefault();
                    if ((pomh.SL - pomh.SL_DA_VE) >= newItem.SO_LUONG)
                    {
                        pomh.SL_DA_VE += Convert.ToInt32(newItem.SO_LUONG);
                    }
                    else
                    {
                        return(Ok("Hàng đã được nhập vào kho rồi!"));
                    }
                    var query1 = db.MH_DE_NGHI_NHAP_KHO.Where(x => x.MA_SO_DN == kho_NhapKhoMH.MA_SO_DN).FirstOrDefault();
                    if (query1 != null)
                    {
                        query1.DA_NHAP_KHO    = true;
                        query1.PHIEU_NHAP_KHO = newItem.SO_CHUNG_TU;
                    }


                    // Lưu Nhật ký
                    KT_SO_NHAT_KY_CHUNG sonhatky = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky.NGAY_CHUNG_TU  = nk.NGAY_CHUNG_TU;
                    sonhatky.NGAY_HACH_TOAN = nk.NGAY_HACH_TOAN;
                    if (nk.NHAN_VIEN_MUA_HANG == null)
                    {
                        sonhatky.DOI_TUONG = nk.NHAN_VIEN_MUA_HANG;
                    }
                    else
                    {
                        sonhatky.DOI_TUONG = nk.NHAN_VIEN_MUA_HANG;
                    }

                    sonhatky.TRUC_THUOC          = "HOPLONG";
                    sonhatky.DIEN_GIAI_CHUNG     = nk.DIEN_GIAI;
                    sonhatky.DIEN_GIAI_CHI_TIET  = nk.DIEN_GIAI;
                    sonhatky.TAI_KHOAN_HACH_TOAN = newItem.TK_NO;
                    sonhatky.TAI_KHOAN_DOI_UNG   = newItem.TK_CO;
                    sonhatky.PHAT_SINH_NO        = tongtien;
                    sonhatky.PHAT_SINH_CO        = 0;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky);
                    KT_SO_NHAT_KY_CHUNG sonhatky1 = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky1.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky1.NGAY_CHUNG_TU  = nk.NGAY_CHUNG_TU;
                    sonhatky1.NGAY_HACH_TOAN = nk.NGAY_HACH_TOAN;
                    if (nk.NHAN_VIEN_MUA_HANG == null)
                    {
                        sonhatky1.DOI_TUONG = nk.NHAN_VIEN_MUA_HANG;
                    }
                    else
                    {
                        sonhatky1.DOI_TUONG = nk.NHAN_VIEN_MUA_HANG;
                    }
                    sonhatky1.TRUC_THUOC          = "HOPLONG";
                    sonhatky1.DIEN_GIAI_CHUNG     = nk.DIEN_GIAI;
                    sonhatky1.DIEN_GIAI_CHI_TIET  = nk.DIEN_GIAI;
                    sonhatky1.TAI_KHOAN_HACH_TOAN = newItem.TK_CO;
                    sonhatky1.TAI_KHOAN_DOI_UNG   = newItem.TK_NO;
                    sonhatky1.PHAT_SINH_NO        = 0;
                    sonhatky1.PHAT_SINH_CO        = tongtien;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky1);
                }
            }


            nk.TONG_TIEN = tongtien;
            //Cập nhật Hàng tồn kho
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (KHO_NHAP_KHOExists(kho_NhapKhoMH.SO_CHUNG_TU))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(nk.SO_CHUNG_TU));
        }
        public IHttpActionResult PostKHO_GIU_HANG1(KhoGiu khogiuhang)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            KHO_GIU_HANG kg = new KHO_GIU_HANG();

            kg.SALES_GIU     = khogiuhang.SALES_GIU;
            kg.MA_KHACH_HANG = khogiuhang.MA_KHACH_HANG;
            kg.NGAY_GIU      = DateTime.Today.Date;
            kg.MA_HANG       = khogiuhang.MA_HANG;
            kg.SL_GIU        = khogiuhang.SL_GIU;
            kg.GIU_PO        = Convert.ToBoolean(khogiuhang.GIU_PO);
            kg.TRUC_THUOC    = khogiuhang.TRUC_THUOC;
            kg.ID_CT_PO      = khogiuhang.ID_CT_PO;
            db.KHO_GIU_HANG.Add(kg);


            TONKHO_HOPLONG newhanggiu = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == khogiuhang.MA_HANG && x.MA_KHO_CON == "IVHOPLONG05").FirstOrDefault();

            if (newhanggiu == null)
            {
                TONKHO_HOPLONG soluonggiumoi = new TONKHO_HOPLONG();
                soluonggiumoi.MA_HANG    = khogiuhang.MA_HANG;
                soluonggiumoi.MA_KHO_CON = "IVHOPLONG05";
                soluonggiumoi.SL_HOPLONG = Convert.ToInt32(khogiuhang.SL_GIU);
                db.TONKHO_HOPLONG.Add(soluonggiumoi);
                db.SaveChanges();
            }
            else
            {
                newhanggiu.SL_HOPLONG += Convert.ToInt32(khogiuhang.SL_GIU);
            }

            foreach (TonKho item in khogiuhang.TonKho)
            {
                //Cập nhật hàng tồn
                TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == khogiuhang.MA_HANG && x.MA_KHO_CON == item.MA_KHO).FirstOrDefault();

                if (newHangTon != null)
                {
                    newHangTon.SL_HOPLONG = newHangTon.SL_HOPLONG - Convert.ToInt32(item.TON_TANG_2) - Convert.ToInt32(item.TON_TANG_3) - Convert.ToInt32(item.TON_TANG_4);
                }
                //if (newHangTon == null || newHangTon.SL_HOPLONG < khogiuhang.SL_GIU)
                //{
                //    return Ok("Hàng không có trong kho hoặc SL tồn không đủ");
                //}

                BH_CT_DON_HANG_PO trangthai = db.BH_CT_DON_HANG_PO.Where(x => x.ID == khogiuhang.ID_CT_PO).FirstOrDefault();
                if (trangthai != null)
                {
                    trangthai.CAN_GIU_HANG = true;
                }
                db.SaveChanges();
                var dagiu = db.BH_CT_DON_HANG_PO.Where(x => x.MA_SO_PO == khogiuhang.MA_SO_PO && x.CAN_GIU_HANG == false).ToList().Count();
                if (dagiu == 0)
                {
                    var dagiuhang = db.BH_DON_HANG_PO.Where(x => x.MA_SO_PO == khogiuhang.MA_SO_PO).FirstOrDefault();
                    if (dagiuhang != null)
                    {
                        dagiuhang.DA_GIU = true;
                    }
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw;
            }

            return(Ok());
        }
예제 #8
0
        public IHttpActionResult PostKHO_NHAP_KHO(NhapKho kho_NhapKho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //Lưu thông tin nhập kho
            KHO_NHAP_KHO nk = new KHO_NHAP_KHO();

            nk.NGAY_CHUNG_TU   = GeneralFunction.ConvertToTime(kho_NhapKho.NGAY_CHUNG_TU);
            nk.NGAY_HACH_TOAN  = GeneralFunction.ConvertToTime(kho_NhapKho.NGAY_HACH_TOAN);
            nk.SO_CHUNG_TU     = GeneralChungTu();
            nk.NGUOI_GIAO_HANG = kho_NhapKho.NGUOI_GIAO_HANG;
            nk.NGUOI_LAP_PHIEU = kho_NhapKho.NGUOI_LAP_PHIEU;
            nk.MA_DOI_TUONG    = kho_NhapKho.MA_DOI_TUONG;
            nk.DIEN_GIAI       = kho_NhapKho.DIEN_GIAI;
            nk.TRUC_THUOC      = "HOPLONG";
            nk.LOAI_NHAP_KHO   = kho_NhapKho.LOAI_NHAP_KHO;
            db.KHO_NHAP_KHO.Add(nk);

            //Lưu thông tin tham chiếu
            if (kho_NhapKho.ThamChieu.Count > 0)
            {
                foreach (ThamChieu item in kho_NhapKho.ThamChieu)
                {
                    XL_THAM_CHIEU_CHUNG_TU newItem = new XL_THAM_CHIEU_CHUNG_TU();
                    newItem.SO_CHUNG_TU_GOC        = nk.SO_CHUNG_TU;
                    newItem.SO_CHUNG_TU_THAM_CHIEU = item.SO_CHUNG_TU;
                    db.XL_THAM_CHIEU_CHUNG_TU.Add(newItem);
                }
            }
            //Lưu chi tiết
            decimal tongtien = 0;

            //HHTONKHOViewModels HHTon = new HHTONKHOViewModels();
            //NhomHangViewModels NhomHang = new NhomHangViewModels();
            if (kho_NhapKho.ChiTiet != null && kho_NhapKho.ChiTiet.Count > 0)
            {
                foreach (ChiTietNhapKho item in kho_NhapKho.ChiTiet)
                {
                    KHO_CT_NHAP_KHO newItem = new KHO_CT_NHAP_KHO();
                    newItem.SO_CHUNG_TU = nk.SO_CHUNG_TU;
                    newItem.MA_HANG     = item.MA_HANG;
                    newItem.MA_KHO_CON  = item.MA_KHO;
                    newItem.TK_CO       = item.TK_CO;
                    newItem.TK_NO       = item.TK_NO;
                    newItem.DVT         = item.DVT;
                    newItem.DON_GIA     = Convert.ToDecimal(item.DON_GIA);
                    newItem.SO_LUONG    = Convert.ToInt32(item.SO_LUONG);
                    newItem.THANH_TIEN  = newItem.DON_GIA * newItem.SO_LUONG;
                    tongtien           += newItem.THANH_TIEN;
                    newItem.TK_KHO      = item.TK_KHO;
                    db.KHO_CT_NHAP_KHO.Add(newItem);
                    //Cập nhật hàng tồn
                    TONKHO_HOPLONG newHangTon = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO).FirstOrDefault();
                    if (newHangTon == null)
                    {
                        newHangTon            = new TONKHO_HOPLONG();
                        newHangTon.MA_HANG    = item.MA_HANG;
                        newHangTon.MA_KHO_CON = item.MA_KHO;
                        newHangTon.SL_HOPLONG = Convert.ToInt32(item.SO_LUONG);
                        db.TONKHO_HOPLONG.Add(newHangTon);
                    }
                    else
                    {
                        newHangTon.SL_HOPLONG += Convert.ToInt32(item.SO_LUONG);
                    }

                    // Lưu Nhật ký
                    KT_SO_NHAT_KY_CHUNG sonhatky = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky.NGAY_CHUNG_TU  = nk.NGAY_CHUNG_TU;
                    sonhatky.NGAY_HACH_TOAN = nk.NGAY_HACH_TOAN;
                    if (nk.MA_DOI_TUONG == null)
                    {
                        sonhatky.DOI_TUONG = nk.NGUOI_GIAO_HANG;
                    }
                    else
                    {
                        sonhatky.DOI_TUONG = nk.MA_DOI_TUONG;
                    }

                    sonhatky.TRUC_THUOC          = "HOPLONG";
                    sonhatky.DIEN_GIAI_CHUNG     = nk.DIEN_GIAI;
                    sonhatky.DIEN_GIAI_CHI_TIET  = nk.DIEN_GIAI;
                    sonhatky.TAI_KHOAN_HACH_TOAN = newItem.TK_NO;
                    sonhatky.TAI_KHOAN_DOI_UNG   = newItem.TK_CO;
                    sonhatky.PHAT_SINH_NO        = tongtien;
                    sonhatky.PHAT_SINH_CO        = 0;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky);
                    KT_SO_NHAT_KY_CHUNG sonhatky1 = new KT_SO_NHAT_KY_CHUNG();
                    sonhatky1.SO_CHUNG_TU    = newItem.SO_CHUNG_TU;
                    sonhatky1.NGAY_CHUNG_TU  = nk.NGAY_CHUNG_TU;
                    sonhatky1.NGAY_HACH_TOAN = nk.NGAY_HACH_TOAN;
                    if (nk.MA_DOI_TUONG == null)
                    {
                        sonhatky1.DOI_TUONG = nk.NGUOI_GIAO_HANG;
                    }
                    else
                    {
                        sonhatky1.DOI_TUONG = nk.MA_DOI_TUONG;
                    }
                    sonhatky1.TRUC_THUOC          = "HOPLONG";
                    sonhatky1.DIEN_GIAI_CHUNG     = nk.DIEN_GIAI;
                    sonhatky1.DIEN_GIAI_CHI_TIET  = nk.DIEN_GIAI;
                    sonhatky1.TAI_KHOAN_HACH_TOAN = newItem.TK_CO;
                    sonhatky1.TAI_KHOAN_DOI_UNG   = newItem.TK_NO;
                    sonhatky1.PHAT_SINH_NO        = 0;
                    sonhatky1.PHAT_SINH_CO        = tongtien;
                    db.KT_SO_NHAT_KY_CHUNG.Add(sonhatky1);
                }
            }


            nk.TONG_TIEN = tongtien;
            //Cập nhật Hàng tồn kho
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (KHO_NHAP_KHOExists(kho_NhapKho.SO_CHUNG_TU))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(nk.SO_CHUNG_TU));
        }
        public IHttpActionResult PutKHO_CHUYEN_KHO(ChuyenKho chuyenkho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var kho = db.KHO_CHUYEN_KHO.Where(x => x.SO_CHUNG_TU == chuyenkho.SO_CHUNG_TU).FirstOrDefault();

            kho.SO_CHUNG_TU     = chuyenkho.SO_CHUNG_TU;
            kho.NGAY_CHUNG_TU   = GeneralFunction.ConvertToTime(chuyenkho.NGAY_CHUNG_TU);
            kho.NGAY_HACH_TOAN  = GeneralFunction.ConvertToTime(chuyenkho.NGAY_HACH_TOAN);
            kho.NGUOI_LAP_PHIEU = chuyenkho.NGUOI_LAP_PHIEU;
            kho.TRUC_THUOC      = chuyenkho.TRUC_THUOC;
            kho.DIEN_GIAI       = chuyenkho.DIEN_GIAI;
            // Chi tiết chuyển kho
            foreach (ChiTietChuyenKho item in chuyenkho.ChiTiet)
            {
                var newItem = db.KHO_CT_CHUYEN_KHO.Where(x => x.SO_CHUNG_TU == chuyenkho.SO_CHUNG_TU).FirstOrDefault();
                int sl_cu   = newItem.SO_LUONG;
                newItem.SO_CHUNG_TU  = kho.SO_CHUNG_TU;
                newItem.MA_HANG      = item.MA_HANG;
                newItem.XUAT_TAI_KHO = item.MA_KHO_CON;
                newItem.NHAP_TAI_KHO = item.NHAP_TAI_KHO;
                newItem.SO_LUONG     = item.SO_LUONG;
                newItem.DVT          = item.DVT;

                //Chuyển hàng vào kho
                TONKHO_HOPLONG newkhoxuat = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();
                newkhoxuat.SL_HOPLONG = newkhoxuat.SL_HOPLONG + sl_cu;
                TONKHO_HOPLONG newkhonhap = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.NHAP_TAI_KHO).FirstOrDefault();
                newkhonhap.SL_HOPLONG = newkhonhap.SL_HOPLONG - sl_cu;
                if (newkhoxuat == null || newkhoxuat.SL_HOPLONG < item.SO_LUONG)
                {
                    return(Ok("Hàng không có trong kho hoặc SL tồn không đủ"));
                }
                newkhoxuat.SL_HOPLONG -= Convert.ToInt32(item.SO_LUONG);


                if (newkhonhap == null)
                {
                    newkhonhap            = new TONKHO_HOPLONG();
                    newkhonhap.MA_HANG    = item.MA_HANG;
                    newkhonhap.MA_KHO_CON = item.NHAP_TAI_KHO;
                    newkhonhap.SL_HOPLONG = Convert.ToInt32(item.SO_LUONG);
                }
                else
                {
                    newkhonhap.MA_HANG     = item.MA_HANG;
                    newkhonhap.SL_HOPLONG += Convert.ToInt32(item.SO_LUONG);
                }
            }



            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (KHO_CHUYEN_KHOExists(chuyenkho.SO_CHUNG_TU))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(kho.SO_CHUNG_TU));
        }
        public IHttpActionResult ChuyenKhoGiuHang(int id, ChuyenKho chuyenkho)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            KHO_CHUYEN_KHO kho = new KHO_CHUYEN_KHO();

            kho.SO_CHUNG_TU     = GeneralChungTu();
            kho.NGAY_CHUNG_TU   = DateTime.Today.Date;
            kho.NGAY_HACH_TOAN  = DateTime.Today.Date;
            kho.NGUOI_LAP_PHIEU = chuyenkho.NGUOI_LAP_PHIEU;
            kho.TRUC_THUOC      = chuyenkho.TRUC_THUOC;
            kho.DIEN_GIAI       = chuyenkho.DIEN_GIAI;
            db.KHO_CHUYEN_KHO.Add(kho);
            // Chi tiết chuyển kho
            foreach (ChiTietChuyenKho item in chuyenkho.ChiTiet)
            {
                KHO_CT_CHUYEN_KHO newItem = new KHO_CT_CHUYEN_KHO();
                newItem.SO_CHUNG_TU  = kho.SO_CHUNG_TU;
                newItem.MA_HANG      = item.MA_HANG;
                newItem.XUAT_TAI_KHO = item.MA_KHO_CON;
                newItem.NHAP_TAI_KHO = item.NHAP_TAI_KHO;
                newItem.SO_LUONG     = item.SO_LUONG;
                newItem.DVT          = item.DVT;
                db.KHO_CT_CHUYEN_KHO.Add(newItem);
                //Chuyển hàng vào kho
                TONKHO_HOPLONG newkhoxuat = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.MA_KHO_CON).FirstOrDefault();

                if (newkhoxuat == null || newkhoxuat.SL_HOPLONG < item.SO_LUONG)
                {
                    return(Ok("Hàng không có trong kho hoặc SL tồn không đủ"));
                }
                newkhoxuat.SL_HOPLONG -= Convert.ToInt32(item.SO_LUONG);

                TONKHO_HOPLONG newkhonhap = db.TONKHO_HOPLONG.Where(x => x.MA_HANG == item.MA_HANG && x.MA_KHO_CON == item.NHAP_TAI_KHO).FirstOrDefault();
                if (newkhonhap == null)
                {
                    newkhonhap            = new TONKHO_HOPLONG();
                    newkhonhap.MA_HANG    = item.MA_HANG;
                    newkhonhap.MA_KHO_CON = item.NHAP_TAI_KHO;
                    newkhonhap.SL_HOPLONG = Convert.ToInt32(item.SO_LUONG);
                    db.TONKHO_HOPLONG.Add(newkhonhap);
                }
                else
                {
                    newkhonhap.MA_HANG     = item.MA_HANG;
                    newkhonhap.SL_HOPLONG += Convert.ToInt32(item.SO_LUONG);
                }
            }


            var query = db.BH_CT_DON_HANG_PO.Where(x => x.ID == id).FirstOrDefault();

            query.CAN_GIU_HANG = true;



            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (KHO_CHUYEN_KHOExists(chuyenkho.SO_CHUNG_TU))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(kho.SO_CHUNG_TU));
        }