public async Task <IActionResult> ThemPhieuNhapAD(PhieuNhapInsertVM phieuNhap)
        {
            dynamic result = await _phieuNhapRepository.themPhieuNhapAD(phieuNhap);

            return(Ok(result));
        }
Beispiel #2
0
        public async Task <dynamic> themPhieuNhapAD(PhieuNhapInsertVM phieuNhap)
        {
            int?maPN = null;

            using (var connection = new SqlConnection(connectionstr))
            {
                await connection.OpenAsync();

                var transaction = connection.BeginTransaction();
                try
                {
                    var insertPN = @" INSERT INTO DBO.PHIEUNHAP(MaCode, NgayTao, MaNV, MaNCC)
	                                         VALUES (@MaCode,@NgayTao, @MaNV ,@MaNCC ); SELECT CAST(SCOPE_IDENTITY() as int)"    ;
                    var p        = new DynamicParameters();
                    p.Add("@NgayTao", phieuNhap.NgayTao);
                    p.Add("@MaCode", phieuNhap.MaCode);
                    p.Add("@MaNV", phieuNhap.MaNV);
                    p.Add("@MaNCC", phieuNhap.MaNCC);

                    maPN = await connection.ExecuteScalarAsync <int>(insertPN, p, transaction);

                    foreach (var item in phieuNhap.dsSanPham)
                    {
                        foreach (var itemSP in item.dsSeriSanPham)
                        {
                            var p2 = new DynamicParameters();
                            p2.Add("@MaSeRi", itemSP);
                            p2.Add("MaSP", item.MaSP);
                            var insertSeriSP = @"INSERT INTO DBO.SANPHAM(MaSeri, MaSP, TrangThai)
	                                                VALUES ( @MaSeRi, @MaSP, 1) "    ;
                            await connection.ExecuteScalarAsync <string>(insertSeriSP, p2, transaction);

                            var p3 = new DynamicParameters();
                            p3.Add("@MaSeri", itemSP);
                            p3.Add("@MaPN", maPN);
                            p3.Add("@DonGia", item.DonGia);
                            var insertCTPN = @"INSERT INTO DBO.CHITIETPHIEUNHAP(MaPN, MaSeri, DonGia)
	                                             VALUES (@MaPN, @MaSeri, @DonGia)"    ;
                            await connection.ExecuteAsync(insertCTPN, p3, transaction);
                        }
                    }
                    transaction.Commit();
                    return("success");
                }
                catch (Exception ex)
                {
                    //Log the exception (ex)
                    //System.Diagnostics.Debug.WriteLine(ex);
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        // Handle any errors that may have occurred
                        // on the server that would cause the rollback to fail, such as
                        // a closed connection.
                        // Log the exception ex2
                        var response = await tbl.TBLoi(ThongBaoLoi.Loi500, "Can not Rollback!");

                        return(response.Content);
                    }
                    var response2 = await tbl.TBLoi(ThongBaoLoi.Loi500, "Value is wrong! Insert order fail");

                    return(ex);
                }
            }
        }