public int Update(Phieu_Nhap_Kho nk)
        {
            DatabaseHelper help = new DatabaseHelper();
            help.ConnectDatabase();
            int temp = 0;
            using (var dbcxtransaction = help.ent.Database.BeginTransaction())
            {
                using (var context = help.ent)
                {
                    context.Phieu_Nhap_Kho.Attach(nk);
                    context.Entry(nk).State = EntityState.Modified;
                    temp = help.ent.SaveChanges();
                    dbcxtransaction.Commit();

                }

                // dbcxtransaction.Commit();
            }
            return temp;
        }
Пример #2
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaPhieuNhap.Text.Trim() == "")
            {
                MessageBox.Show("Mã phiếu bắt buộc nhập!");
                return;
            }
            if (cbKhoNhan.Text == "" )
            {
                MessageBox.Show("Chưa nhập kho hoặc mã phiếu");
                return;
            }

            DatabaseHelper help = new DatabaseHelper();
            help.ConnectDatabase();
            //switch (staTus)
            switch (PanelButton.getClickStatus())
            {

                #region "Thêm"
                case enumButton2.Them:
                    using (var dbcxtransaction = help.ent.Database.BeginTransaction())
                    {
                        try
                        {

                            clsPhieuNhapKho phieunhap = new clsPhieuNhapKho();
                            phieunhap.Ma_phieu_nhap = txtMaPhieuNhap.Text;
                            if (!phieunhap.CheckTonTaiSoDK(txtMaPhieuNhap.Text))
                            {

                                phieunhap.Kho_nhan = cbKhoNhan.Text;
                                phieunhap.Kho_xuat_ra = txtXuatTaiKho.Text;
                                phieunhap.Da_phan_kho = false;

                                  phieunhap.ID_Loai_Phieu_Nhap = null;
                                phieunhap.Ma_phieu_nhap = txtMaPhieuNhap.Text;
                                phieunhap.Dia_chi = txtDiaChi.Text;
                                phieunhap.Ly_do = txtLyDo.Text;
                                phieunhap.Ngay_lap = dtNgayNhap.Value;
                                phieunhap.Cong_trinh = txtCongTrinh.Text;
                                phieunhap.Da_phan_kho = false;
                                phieunhap.ID_khoNhan = (int)cbKhoNhan.SelectedValue;
                                if (rdoKNMN.Checked == true)
                                    phieunhap.isKNMN = true;
                                if (rdoKNTN.Checked == true)
                                    phieunhap.isKNTN = true;
                                if (rdoKCTN.Checked == true)
                                    phieunhap.isKCTN = true;
                                if (rdoKCMuonNo.Checked == true)
                                    phieunhap.isKCMN = true;

                                phieunhap.isNhapNgoai = true;
                                phieunhap.Ten_kho_muon = cbKhoNgoai.Text;
                                if (phieunhap.Insert(help) == 1)
                                {

                                    //DataTable chiTietPhieuNhap = new clsChi_Tiet_Phieu_Nhap_Vat_Tu().GetAll(phieuNhap.ID_phieu_nhap);
                                    for (int i = 0; i < dataTable1.Rows.Count; i++)
                                    {
                                        clsChi_Tiet_Phieu_Nhap_Vat_Tu chitiet = new clsChi_Tiet_Phieu_Nhap_Vat_Tu();
                                        //    chitiet.ID_chi_tiet_phieu_nhap = int.Parse(gridMaster.Rows[i].Cells["ID_chi_tiet_phieu_nhap"].ToString());
                                        chitiet.Ma_phieu_nhap = (txtMaPhieuNhap.Text);
                                        chitiet.ID_Don_vi_tinh = int.Parse(dataTable1.Rows[i]["ID_Don_vi_tinh"].ToString());
                                        chitiet.Ma_vat_tu = (dataTable1.Rows[i]["Ma_vat_tu"].ToString());
                                        chitiet.ID_Chat_luong = int.Parse(dataTable1.Rows[i]["ID_Chat_luong"].ToString());
                                        // chitiet.So_luong_yeu_cau = decimal.Parse(dataTable1.Rows[i]["So_luong_yeu_cau"].ToString());
                                        chitiet.So_luong_thuc_lanh = decimal.Parse(dataTable1.Rows[i]["so_luong_thuc_lanh"].ToString());
                                        chitiet.Don_gia = decimal.Parse(dataTable1.Rows[i]["Don_gia"].ToString());
                                        chitiet.Thanh_tien = decimal.Parse(dataTable1.Rows[i]["Thanh_tien"].ToString());
                                        chitiet.Da_duyet = false;
                                        if (chitiet.Insert(help) == 0)
                                            dbcxtransaction.Rollback();

                                    }

                                    dbcxtransaction.Commit();

                                    PanelButton.ResetClickStatus();

                                    //setInputComponentStatus(true);
                                    enableInputForm();

                                    PanelButton.ResetButton();
                                    frmDanhSachPhieuNhap dspn = new frmDanhSachPhieuNhap(txtMaPhieuNhap.Text);
                                    dspn.Show();

                                }
                                else
                                    dbcxtransaction.Rollback();

                            }
                            else
                            {
                                MessageBox.Show("mã phiếu nhập này đã tồn tại trong csdl!");
                                button2_Click(this, EventArgs.Empty);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(Utilities.clsThamSoUtilities.COException(ex));
                            dbcxtransaction.Rollback();
                        }
                    }
                    break;
                #endregion
                #region "Sua"
                case enumButton2.Sua:
                    {
                        using (var dbcxtransaction = help.ent.Database.BeginTransaction())
                        {
                            try
                            {
                                clsPhieuNhapKho phieunhap = new clsPhieuNhapKho();
                                phieunhap.Ma_phieu_nhap = txtMaPhieuNhap.Text;
                                {

                                    // phieunhap.

                                    //  phieunhap.ID_Loai_Phieu_Nhap = Int32.Parse(cbLoaiPhieuNhan.SelectedValue.ToString());
                                    phieunhap.Ma_phieu_nhap = txtMaPhieuNhap.Text;
                                    phieunhap.Dia_chi = txtDiaChi.Text;
                                    phieunhap.Ly_do = txtLyDo.Text;
                                    phieunhap.Ngay_lap = dtNgayNhap.Value;
                                    //     phieunhap.So_hoa_don = txtSoHD.Text;
                                    phieunhap.Cong_trinh = txtCongTrinh.Text;

                                    //if (rdoKNMN.Checked == true)
                                    //    phieunhap.isChoMuonNgoai = true;

                                    if (rdoKNMN.Checked == true)
                                        phieunhap.isKNMN = true;
                                    if (rdoKNTN.Checked == true)
                                        phieunhap.isKNTN = true;
                                    if (rdoKCTN.Checked == true)
                                        phieunhap.isKCTN = true;
                                    if (rdoKCMuonNo.Checked == true)
                                        phieunhap.isKCMN = true;

                                    phieunhap.isNhapNgoai = true;
                                    //if (isLoaiPhieu == "TN")
                                    //    phieunhap.isTraNo = true;
                                    //  phieunhap.isGoiDau = chbNGD.Checked;
                                    phieunhap.ID_khoNhan = (int)cbKhoNhan.SelectedValue;
                                    DataTable temp = phieunhap.GetThongTinPhieuMuonNo(phieunhap.Ma_phieu_nhap);
                                    Phieu_Nhap_Kho nk = new Phieu_Nhap_Kho();

                                    nk.Ma_phieu_nhap = phieunhap.Ma_phieu_nhap;
                                    nk.ID_phieu_nhap = int.Parse(temp.Rows[0]["ID_phieu_nhap"].ToString());

                                    nk.Cong_trinh = phieunhap.Cong_trinh;
                                    nk.Da_phan_kho = phieunhap.Da_phan_kho;
                                    nk.Dia_Chi = phieunhap.Dia_chi;
                                    nk.ID_Loai_Phieu_Nhap = phieunhap.ID_Loai_Phieu_Nhap;
                                    nk.Kho_nhan = phieunhap.Kho_nhan;
                                    nk.ID_kho = phieunhap.ID_khoNhan;
                                    nk.Kho_xuat_ra = phieunhap.Kho_xuat_ra;
                                  //  nk.isKNTN = phieunhap.isTraNo;
                                    nk.isToTrinh = phieunhap.isToTrinh;
                                    nk.Ly_do = phieunhap.Ly_do;
                                    nk.Ngay_lap = phieunhap.Ngay_lap;
                                    nk.So_hoa_don = phieunhap.So_hoa_don;
                                    nk.isKNMN = phieunhap.isKNMN;
                                    nk.isKNTN = phieunhap.isKNTN;
                                    nk.isKCTN = phieunhap.isKCTN;
                                    nk.isKCMN = phieunhap.isKCMN;
                                    nk.isNVMN = phieunhap.isNVMN;

                                    nk.isNhapNgoai = true;

                                    if (phieunhap.Update(nk) == 1)
                                    {

                                        clsChi_Tiet_Phieu_Nhap_Vat_Tu pn = new clsChi_Tiet_Phieu_Nhap_Vat_Tu();
                                        pn.Ma_phieu_nhap = phieunhap.Ma_phieu_nhap;

                                        pn.remove(pn.Ma_phieu_nhap);
                                        for (int i = 0; i < dataTable1.Rows.Count; i++)
                                        {

                                            clsChi_Tiet_Phieu_Nhap_Vat_Tu chitiet = new clsChi_Tiet_Phieu_Nhap_Vat_Tu();
                                            chitiet.Ma_phieu_nhap = (txtMaPhieuNhap.Text);
                                            chitiet.ID_Don_vi_tinh = int.Parse(dataTable1.Rows[i]["ID_Don_vi_tinh"].ToString());
                                            chitiet.Ma_vat_tu = (dataTable1.Rows[i]["Ma_vat_tu"].ToString());
                                            chitiet.ID_Chat_luong = int.Parse(dataTable1.Rows[i]["ID_Chat_luong"].ToString());
                                            chitiet.So_luong_thuc_lanh = decimal.Parse(dataTable1.Rows[i]["so_luong_thuc_lanh"].ToString());
                                            chitiet.Don_gia = decimal.Parse(dataTable1.Rows[i]["Don_gia"].ToString());
                                            chitiet.Thanh_tien = decimal.Parse(dataTable1.Rows[i]["Thanh_tien"].ToString());
                                            chitiet.Da_duyet = false;
                                            chitiet.ID_Chat_luong = int.Parse(cbChatLuong.SelectedValue.ToString());
                                            if (chitiet.Insert(help) == 0)
                                                dbcxtransaction.Rollback();

                                        }

                                        dbcxtransaction.Commit();

                                        PanelButton.ResetClickStatus();

                                        //setInputComponentStatus(true);
                                        enableInputForm();

                                        PanelButton.ResetButton();
                                    }
                                    else
                                        dbcxtransaction.Rollback();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(Utilities.clsThamSoUtilities.COException(ex));
                                dbcxtransaction.Rollback();
                            }
                        }
                        this.ds.LoadData();
                        break;
                    }
                #endregion

            }
        }
        //dal.BeginTransaction();
        //m_dbConnection = dal.m_conn;
        //if(m_dbConnection.State == ConnectionState.Closed)
        //    m_dbConnection.Open();
        //string sql = "";
        //sql += "INSERT INTO Phieu_Nhap_Kho (ma_phieu_nhap,ID_kho,Ngay_lap,ly_do,Dia_chi,Cong_trinh) ";
        //sql += "VALUES(@ma_phieu_nhap,@ID_kho,@Ngay_lap,@ly_do,@Dia_chi,@Cong_trinh)";
        //SqlCommand command = new SqlCommand(sql, m_dbConnection,dal.m_trans);
        //command.CommandType = CommandType.Text;
        ////command.Parameters.Add(new SQLiteParameter("@BangKe_Id", BangKe_Id));
        //command.Parameters.Add(new SqlParameter("@ma_phieu_nhap", Ma_phieu_nhap));
        //command.Parameters.Add(new SqlParameter("@ID_kho", ID_kho));
        //command.Parameters.Add(new SqlParameter("@Ngay_lap", Ngay_lap.ToString("yyyy-MM-dd")));
        //command.Parameters.Add(new SqlParameter("@Ly_do", Ly_do));
        ////  command.Parameters.Add(new SqlParameter("@So_hoa_don", So_hoa_don));
        //command.Parameters.Add(new SqlParameter("@Dia_chi", Dia_chi));
        //command.Parameters.Add(new SqlParameter("@Cong_trinh", Cong_trinh));
        ////command.Parameters.Add(new SqlParameter("@ma_phieu_nhap", Ma_phieu_nhap));
        //int result = command.ExecuteNonQuery();
        //dal.CommitTransaction();
        public int Update(  DatabaseHelper help,Phieu_Nhap_Kho nk)
        {
            int temp = 0;
            {

                {
                    help.ent.Phieu_Nhap_Kho.Attach(nk);
                    help.ent.Entry(nk).State = EntityState.Modified;
                    temp = help.ent.SaveChanges();

                }

                // dbcxtransaction.Commit();
            }
            return temp;
        }