Ejemplo n.º 1
0
        public bool SaveVoucher_Detail(List <VoucherDetail> saveData, Voucher voucher)
        {
            using (DbContextTransaction transaction = Context.Database.BeginTransaction())
            {
                try
                {
                    long seq       = VoucherDAO.GetVoucherSEQ();
                    long seqDetail = VoucherDetailDAO.GetVoucherDetailSEQ();
                    switch (voucher.Status)
                    {
                    case ModifyMode.Insert:
                        seq++;
                        voucher.VouchersID = GenerateID.GetVoucherID(seq);
                        this.VoucherDAO.InsertVouchers(voucher);
                        voucher.CreateUser = UserInfo.UserID;
                        voucher.CompanyID  = CommonInfo.CompanyInfo.CompanyID;
                        //Select voucher vừa insert để lấy voucherno
                        foreach (VoucherDetail data in saveData)
                        {
                            seqDetail++;
                            data.VouchersID       = voucher.VouchersID;
                            data.CreateUser       = UserInfo.UserID;
                            data.CompanyID        = CommonInfo.CompanyInfo.CompanyID;
                            data.VouchersDetailID = GenerateID.VoucherDetailID(seqDetail);
                            this.VoucherDetailDAO.InsertVouchersDetail(data);
                        }
                        break;

                    // Update
                    case ModifyMode.Update:
                        this.VoucherDAO.UpdateVoucher(voucher);
                        voucher.UpdateUser = UserInfo.UserID;
                        voucher.CompanyID  = CommonInfo.CompanyInfo.CompanyID;
                        if (saveData.Count > 0)
                        {
                            foreach (VoucherDetail voucherDetail in saveData)
                            {
                                //thực hiện nhiệm vụ update với các chi tiết
                                if (voucherDetail.Status == ModifyMode.Update)
                                {
                                    #region Update dataDetail
                                    if (!string.IsNullOrEmpty(voucherDetail.VouchersDetailID))
                                    {
                                        if (voucherDetail.Amount == 0)
                                        {
                                            this.VoucherDetailDAO.DeleteVoucherDetail(voucherDetail.VouchersDetailID, voucherDetail.CompanyID);
                                            voucherDetail.Status = ModifyMode.Delete;
                                        }
                                        else
                                        {
                                            voucherDetail.UpdateUser = UserInfo.UserID;
                                            voucherDetail.CompanyID  = CommonInfo.CompanyInfo.CompanyID;
                                            this.VoucherDetailDAO.UpdateVoucherDetail(voucherDetail);
                                        }
                                    }
                                    #endregion Update dataDetail
                                }
                                else if (voucherDetail.Status == ModifyMode.Insert)
                                {
                                    //thêm mới Detail
                                    #region insert Detail khi thêm detail mới cho S35 đã có sẵn
                                    if (string.IsNullOrEmpty(voucherDetail.VouchersDetailID))
                                    {
                                        seqDetail++;
                                        voucherDetail.VouchersDetailID = GenerateID.VoucherDetailID(seqDetail);    // GenerateID.InvoiceS35DetailID(seqDetail);
                                        voucherDetail.VouchersID       = voucher.VouchersID;
                                        voucherDetail.CompanyID        = voucher.CompanyID;
                                        voucherDetail.CreateUser       = UserInfo.UserID;
                                        this.VoucherDetailDAO.InsertVouchersDetail(voucherDetail);
                                    }
                                    #endregion insert Detail khi thêm detail mới cho S35 đã có sẵn
                                }
                            }
                        }
                        break;

                    // Delete
                    case ModifyMode.Delete:
                        this.VoucherDAO.DeleteVoucher(voucher.VouchersID, voucher.CompanyID);
                        break;
                    }
                    transaction.Commit();
                    return(true);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    Console.WriteLine("Update data fail.\r\n" + e.Message);
                    return(false);
                }
            }
        }