public int SaveAdjustCreditNote(AdjustCreditNoteForm psForm)
        {
            int autoId = 0;

            try
            {
                using (SDNSalesDBEntities entities = new SDNSalesDBEntities())
                {
                    foreach (var item in psForm.AdjustCreditNoteDetails)
                    {
                        CashAndBankTransaction obj = new CashAndBankTransaction();
                        obj.Cus_Sup_Id = psForm.AdjustCreditNote.CustomerID;
                        obj.Acc_Id     = psForm.AdjustCreditNote.AccountId;
                        obj.Amount     = psForm.AdjustCreditNote.Amount;
                        //obj.Cash_Cheque_No = psForm.AdjustCreditNote.CashChequeNo;
                        obj.Cash_Cheque_Date = psForm.AdjustCreditNote.Date;
                        obj.Remarks          = psForm.AdjustCreditNote.Remarks;
                        obj.Type             = "S";
                        //obj.Is_Cheque = psForm.AdjustCreditNote.IsCheque;

                        //saving details entity
                        obj.SO_CN_PO_DN_No   = item.SalesNo;
                        obj.SO_CN_PO_DN_Date = item.SalesDate;
                        obj.SO_CN_PO_DN_Amt  = item.SalesAmount;
                        obj.Amt_Due          = item.AmountDue;
                        obj.Amt_Refunded     = item.AmountAdjusted;
                        obj.Discount         = item.Discount;

                        obj.UpdatedBy   = 0;
                        obj.UpdatedDate = DateTime.Now.Date;

                        entities.CashAndBankTransactions.Add(obj);
                        entities.SaveChanges();

                        if (item.AmountDue == 0 && item.AmountAdjusted == item.SalesAmount)
                        {
                            SalesInvoice invoice = entities.SalesInvoices.Where(e => e.SI_No == item.SalesNo).SingleOrDefault();
                            SalesOrder   order   = entities.SalesOrders.Where(e => e.SO_No == item.SalesNo).SingleOrDefault();
                            if (invoice != null)
                            {
                                invoice.SI_Status = Convert.ToByte(SI_Status.Paid);
                                entities.SaveChanges();
                            }
                            else if (order != null)
                            {
                            }
                        }

                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(autoId);
        }
示例#2
0
        public int UpdateRefundToCustomer(RefundToCustomerForm psForm)
        {
            try
            {
                using (SDNSalesDBEntities entities = new SDNSalesDBEntities())
                {
                    foreach (var item in psForm.RefundToCustomerDetails)
                    {
                        CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.Cash_Cheque_No == psForm.RefundToCustomer.CashChequeNo &&
                                                                                            e.SO_CN_PO_DN_No == item.SalesNo).SingleOrDefault();
                        if (obj != null)
                        {
                            obj.Acc_Id           = psForm.RefundToCustomer.AccountId;
                            obj.Amount           = psForm.RefundToCustomer.Amount;
                            obj.Cash_Cheque_No   = psForm.RefundToCustomer.CashChequeNo;
                            obj.Cash_Cheque_Date = psForm.RefundToCustomer.Date;
                            obj.Remarks          = psForm.RefundToCustomer.Remarks;

                            obj.Is_Cheque = psForm.RefundToCustomer.IsCheque;

                            //saving details entity
                            obj.SO_CN_PO_DN_No   = item.SalesNo;
                            obj.SO_CN_PO_DN_Date = item.SalesDate;
                            obj.SO_CN_PO_DN_Amt  = item.SalesAmount;
                            obj.Amt_Due          = obj.Amt_Due = item.AmountDue - item.AmountAdjusted;
                            obj.Amt_Refunded     = item.AmountAdjusted;
                            obj.Discount         = item.Discount;

                            obj.UpdatedBy   = 0;
                            obj.UpdatedDate = DateTime.Now;
                            entities.SaveChanges();

                            if (item.AmountDue == 0 && item.AmountAdjusted == item.SalesAmount)
                            {
                                CreditNote dn    = entities.CreditNotes.Where(e => e.CN_No == item.SalesNo).SingleOrDefault();
                                SalesOrder order = entities.SalesOrders.Where(e => e.SO_No == item.SalesNo).SingleOrDefault();
                                if (dn != null)
                                {
                                    dn.CN_Status = Convert.ToByte(DN_Status.Refunded);
                                    entities.SaveChanges();
                                }
                                else if (order != null)
                                {
                                    order.SO_Status = Convert.ToByte(PO_Status.Refunded);
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(psForm.RefundToCustomer.ID);
        }
        public int SaveRefundFromSupplier(RefundFromSupplierForm psForm)
        {
            int autoId = 0;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    foreach (var item in psForm.RefundFromSupplierDetails)
                    {
                        CashAndBankTransaction obj = new CashAndBankTransaction();
                        obj.Cus_Sup_Id       = psForm.RefundFromSupplier.SupplierID;
                        obj.Acc_Id           = psForm.RefundFromSupplier.AccountId;
                        obj.Amount           = psForm.RefundFromSupplier.Amount;
                        obj.Cash_Cheque_No   = psForm.RefundFromSupplier.CashChequeNo;
                        obj.Cash_Cheque_Date = psForm.RefundFromSupplier.Date;
                        obj.Remarks          = psForm.RefundFromSupplier.Remarks;
                        obj.Type             = Convert.ToString(Convert.ToByte(CashBankTransactionType.RefundFromSupplier));
                        obj.Is_Cheque        = psForm.RefundFromSupplier.IsCheque;

                        //saving details entity
                        obj.SO_CN_PO_DN_No   = item.PurchaseNo;
                        obj.SO_CN_PO_DN_Date = item.PurchaseDate;
                        obj.SO_CN_PO_DN_Amt  = item.PurchaseAmount;
                        obj.Amt_Due          = item.AmountDue - item.AmountAdjusted;
                        obj.Amt_Due          = item.AmountDue;
                        obj.Amt_Refunded     = item.AmountAdjusted;
                        obj.Discount         = item.Discount;

                        obj.UpdatedBy   = 0;
                        obj.UpdatedDate = DateTime.Now;

                        entities.CashAndBankTransactions.Add(obj);
                        entities.SaveChanges();

                        if (item.AmountDue == 0 && item.AmountAdjusted == item.PurchaseAmount)
                        {
                            DebitNote     dn    = entities.DebitNotes.Where(e => e.DN_No == item.PurchaseNo).SingleOrDefault();
                            PurchaseOrder order = entities.PurchaseOrders.Where(e => e.PO_No == item.PurchaseNo).SingleOrDefault();
                            if (dn != null)
                            {
                                dn.DN_Status = Convert.ToByte(DN_Status.Refunded);
                                entities.SaveChanges();
                            }
                            else if (order != null)
                            {
                                order.PO_Status = Convert.ToByte(PO_Status.Refunded);
                                entities.SaveChanges();
                            }
                        }

                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(autoId);
        }
        public int SavePaymentFromCustomer(PaymentFromCustomerForm psForm)
        {
            int autoId = 0;

            try
            {
                using (SASEntitiesEDM entities = new SASEntitiesEDM())
                {
                    foreach (var item in psForm.PaymentFromCustomerDetails)
                    {
                        //CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.SO_CN_PO_DN_No == item.SalesNo).SingleOrDefault();
                        //if(obj!=null)
                        //{
                        //    // obj.Acc_Id = psForm.PaymentToSupplier.AccountId;
                        //    obj.Amount = psForm.PaymentToSupplier.Amount;

                        //    //saving details entity
                        //    obj.SO_CN_PO_DN_No = item.SalesNo;
                        //    obj.SO_CN_PO_DN_Date = item.SalesDate;
                        //    obj.SO_CN_PO_DN_Amt = item.SalesAmount;
                        //    obj.Amt_Due = item.AmountDue;
                        //    obj.Amt_Refunded = item.AmountAdjusted;
                        //    obj.Discount = item.Discount;
                        //    obj.UpdatedBy = 0;
                        //    obj.UpdatedDate = DateTime.Now.Date;
                        //    entities.SaveChanges();
                        //}
                        //else
                        //{
                        CashAndBankTransaction obj = new CashAndBankTransaction();
                        obj.Cus_Sup_Id       = psForm.PaymentFromCustomer.CustomerID;
                        obj.Acc_Id           = psForm.PaymentFromCustomer.AccountId;
                        obj.Amount           = psForm.PaymentFromCustomer.Amount;
                        obj.Cash_Cheque_No   = psForm.PaymentFromCustomer.CashChequeNo;
                        obj.Cash_Cheque_Date = psForm.PaymentFromCustomer.Date;
                        obj.Remarks          = psForm.PaymentFromCustomer.Remarks;
                        obj.Type             = "C";
                        obj.Is_Cheque        = psForm.PaymentFromCustomer.IsCheque;

                        //saving details entity
                        obj.SO_CN_PO_DN_No   = item.SalesNo;
                        obj.SO_CN_PO_DN_Date = item.SalesDate;
                        obj.SO_CN_PO_DN_Amt  = item.SalesAmount;
                        obj.Amt_Due          = item.AmountDue - item.AmountAdjusted;
                        obj.Amt_Refunded     = item.AmountAdjusted;
                        obj.Discount         = item.Discount;

                        obj.UpdatedBy   = 0;
                        obj.UpdatedDate = DateTime.Now.Date;

                        entities.CashAndBankTransactions.Add(obj);
                        entities.SaveChanges();

                        SalesInvoice invoice = entities.SalesInvoices.Where(e => e.SI_No == item.SalesNo).FirstOrDefault();
                        SalesOrder   order   = entities.SalesOrders.Where(e => e.SO_No == item.SalesNo).FirstOrDefault();
                        if (invoice != null)
                        {
                            if (item.AmountAdjusted == item.SalesAmount)
                            {
                                invoice.SI_Status = Convert.ToByte(PI_Status.Paid);
                                entities.SaveChanges();
                            }
                            else
                            {
                                invoice.SI_Status = Convert.ToByte(PI_Status.UnPaid);
                                entities.SaveChanges();
                            }
                        }
                        else if (order != null)
                        {
                            order.SO_Status = Convert.ToByte(PO_Status.Collected);
                            entities.SaveChanges();
                        }



                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(autoId);
        }
        public int UpdateAdjustCreditNote(AdjustCreditNoteForm psForm)
        {
            try
            {
                using (SDNSalesDBEntities entities = new SDNSalesDBEntities())
                {
                    foreach (var item in psForm.AdjustCreditNoteDetails)
                    {
                        if (item.CheckAmountAdjusted == true)
                        {
                            CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.SO_CN_PO_DN_No == item.SalesNo).SingleOrDefault();
                            if (obj != null)
                            {
                                obj.Acc_Id = psForm.AdjustCreditNote.AccountId;
                                obj.Amount = psForm.AdjustCreditNote.Amount;
                                //obj.Cash_Cheque_No = psForm.AdjustCreditNote.CashChequeNo;
                                obj.Cash_Cheque_Date = psForm.AdjustCreditNote.Date;
                                obj.Remarks          = psForm.AdjustCreditNote.Remarks;
                                obj.Is_Cheque        = psForm.AdjustCreditNote.IsCheque;
                                //saving details entity
                                obj.SO_CN_PO_DN_No   = item.SalesNo;
                                obj.SO_CN_PO_DN_Date = item.SalesDate;
                                obj.SO_CN_PO_DN_Amt  = item.SalesAmount;
                                obj.Amt_Due          = item.AmountDue;
                                obj.Amt_Refunded     = item.AmountAdjusted;
                                obj.Discount         = item.Discount;
                                obj.DN_CN_No         = psForm.AdjustCreditNote.CreditNoteNo;

                                obj.UpdatedBy   = 0;
                                obj.UpdatedDate = DateTime.Now.Date;
                                entities.SaveChanges();

                                if (item.AmountDue == 0 && item.AmountAdjusted == item.SalesAmount)
                                {
                                    SalesInvoice invoice = entities.SalesInvoices.Where(e => e.SI_No == item.SalesNo).SingleOrDefault();
                                    //SalesOrder order = entities.SalesOrders.Where(e => e.PO_No == item.SalesNo).SingleOrDefault();
                                    if (invoice != null)
                                    {
                                        invoice.SI_Status = Convert.ToByte(SI_Status.Paid);
                                        entities.SaveChanges();
                                    }
                                    //else if (order != null)
                                    //{
                                    //}
                                    CreditNote debit = entities.CreditNotes.Where(e => e.CN_No == psForm.AdjustCreditNote.CreditNoteNo).SingleOrDefault();
                                    if (debit != null)
                                    {
                                        debit.CN_Status = Convert.ToByte(CN_Status.Adjusted);
                                        entities.SaveChanges();
                                    }
                                }
                            }
                            else
                            {
                                CashAndBankTransaction cashbankobj = new CashAndBankTransaction()
                                {
                                    Acc_Id           = psForm.AdjustCreditNote.AccountId,
                                    Amount           = psForm.AdjustCreditNote.Amount,
                                    Cash_Cheque_Date = psForm.AdjustCreditNote.Date,
                                    Remarks          = psForm.AdjustCreditNote.Remarks,
                                    Is_Cheque        = psForm.AdjustCreditNote.IsCheque,
                                    SO_CN_PO_DN_No   = item.SalesNo,
                                    SO_CN_PO_DN_Date = item.SalesDate,
                                    SO_CN_PO_DN_Amt  = item.SalesAmount,
                                    Amt_Due          = item.AmountDue,
                                    Amt_Refunded     = item.AmountAdjusted,
                                    Discount         = item.Discount,
                                    UpdatedBy        = 0,
                                    UpdatedDate      = DateTime.Now.Date,
                                    Cus_Sup_Id       = psForm.AdjustCreditNote.CustomerID,
                                    DN_CN_No         = psForm.AdjustCreditNote.CreditNoteNo,
                                    Type             = "S"
                                };
                                entities.CashAndBankTransactions.Add(cashbankobj);
                                entities.SaveChanges();
                                if (item.AmountDue == 0 && item.AmountAdjusted == item.SalesAmount)
                                {
                                    SalesInvoice invoice = entities.SalesInvoices.Where(e => e.SI_No == item.SalesNo).SingleOrDefault();
                                    //SalesOrder order = entities.SalesOrders.Where(e => e.PO_No == item.SalesNo).SingleOrDefault();
                                    if (invoice != null)
                                    {
                                        invoice.SI_Status = Convert.ToByte(SI_Status.Paid);
                                        entities.SaveChanges();
                                    }
                                    //else if (order != null)
                                    //{
                                    //}
                                }
                                CreditNote debit = entities.CreditNotes.Where(e => e.CN_No == psForm.AdjustCreditNote.CreditNoteNo).SingleOrDefault();
                                if (debit != null)
                                {
                                    debit.CN_Status = Convert.ToByte(CN_Status.Adjusted);
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(psForm.AdjustCreditNote.ID);
        }
        public int SaveAdjustDebitNote(AdjustDebitNoteForm psForm, int Type)
        {
            int autoId = 0;

            try
            {
                using (SASEntitiesEDM entities = new SASEntitiesEDM())
                {
                    foreach (var item in psForm.AdjustDebitNoteDetails)
                    {
                        CashAndBankTransaction obj = new CashAndBankTransaction();
                        obj.Cus_Sup_Id = psForm.AdjustDebitNote.SupplierID;
                        obj.Acc_Id     = psForm.AdjustDebitNote.AccountId;
                        obj.Amount     = psForm.AdjustDebitNote.Amount;
                        //obj.Cash_Cheque_No = psForm.AdjustDebitNote.CashChequeNo;
                        obj.Cash_Cheque_Date = psForm.AdjustDebitNote.Date;
                        obj.Remarks          = psForm.AdjustDebitNote.Remarks;
                        obj.Type             = Type.ToString();
                        //obj.Is_Cheque = psForm.AdjustDebitNote.IsCheque;

                        //saving details entity
                        obj.SO_CN_PO_DN_No   = item.PurchaseNo;
                        obj.SO_CN_PO_DN_Date = item.PurchaseDate;
                        obj.SO_CN_PO_DN_Amt  = item.PurchaseAmount;
                        obj.Amt_Due          = item.AmountDue - item.AmountAdjusted;
                        obj.Amt_Refunded     = item.AmountAdjusted;
                        obj.Discount         = item.Discount;

                        obj.UpdatedBy   = 0;
                        obj.UpdatedDate = DateTime.Now.Date;

                        entities.CashAndBankTransactions.Add(obj);
                        entities.SaveChanges();

                        if (item.AmountAdjusted == item.PurchaseAmount)
                        {
                            PurchaseInvoice invoice = entities.PurchaseInvoices.Where(e => e.PI_No == item.PurchaseNo).SingleOrDefault();
                            PurchaseOrder   order   = entities.PurchaseOrders.Where(e => e.PO_No == item.PurchaseNo).SingleOrDefault();
                            if (invoice != null)
                            {
                                invoice.PI_Status = Convert.ToByte(PI_Status.Paid);
                                entities.SaveChanges();
                            }
                        }
                        DebitNote debit = entities.DebitNotes.Where(e => e.DN_No == psForm.AdjustDebitNote.DebitNoteNo).SingleOrDefault();
                        if (debit != null)
                        {
                            debit.DN_Status = Convert.ToByte(DN_Status.Adjusted);
                            entities.SaveChanges();
                        }

                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(autoId);
        }
        public int UpdateAdjustDebitNote(AdjustDebitNoteForm psForm, int Type)
        {
            int    autoID     = 0;
            string AdjustedNo = string.Empty;

            try
            {
                using (SASEntitiesEDM entities = new SASEntitiesEDM())
                {
                    //adjusted table entity
                    //adjusted debit note
                    AdjustedDebitNote Adn = new AdjustedDebitNote();
                    Adn.SupplierID            = psForm.AdjustDebitNote.SupplierID;
                    Adn.AdjustDebitNoteNumber = psForm.AdjustDebitNote.AdjustDebitNoteNumber;
                    Adn.AdjustDebitNoteDate   = Convert.ToDateTime(psForm.AdjustDebitNote.AdjustDebitNoteDateStr);
                    Adn.DebitNoteNumber       = psForm.AdjustDebitNote.DebitNoteNo;
                    Adn.DebitNoteDate         = psForm.AdjustDebitNote.Date;
                    Adn.DebitNoteAmount       = psForm.AdjustDebitNote.Amount;
                    Adn.CreatedBy             = 0;
                    if (entities.AdjustedDebitNotes.AsNoTracking().FirstOrDefault(x => x.ID == psForm.AdjustDebitNote.ID) == null)
                    {
                        //obj.CreatedBy = invoiceData.SIModel.CreatedBy;
                        Adn.CreatedDate = DateTime.Now;
                        entities.AdjustedDebitNotes.Add(Adn);
                        entities.SaveChanges();
                        autoID     = Adn.ID;
                        AdjustedNo = Adn.AdjustDebitNoteNumber;
                    }
                    else
                    {
                        // obj.ModifiedBy = invoiceData.SIModel.ModifiedBy;
                        Adn.ModifiedDate          = DateTime.Now;
                        entities.Entry(Adn).State = EntityState.Modified;
                        autoID = entities.SaveChanges();
                    }
                    foreach (var item in psForm.AdjustDebitNoteDetails)
                    {
                        if (item.CheckAmountAdjusted == true)
                        {
                            AdjustedDebitNoteDetail ADN = new AdjustedDebitNoteDetail();
                            if (autoID > 0)
                            {
                                ADN.ADN_ID                = autoID;
                                ADN.PurchaseInvoiceNo     = item.PurchaseNo;
                                ADN.PurchaseInvoiceDate   = item.PurchaseDate;
                                ADN.PaymentDueDate        = item.PaymentDueDate;
                                ADN.PurchaseInvoiceAmount = item.PurchaseAmount;
                                ADN.AmountDue             = item.AmountDue;
                                ADN.AmountAdjusted        = item.AmountAdjusted;
                                ADN.CreatedBy             = 0;
                                ADN.CreatedDate           = DateTime.Now;
                                if (entities.AdjustedDebitNoteDetails.AsNoTracking().FirstOrDefault(x => x.ID == psForm.AdjustDebitNote.ID) == null)
                                {
                                    entities.AdjustedDebitNoteDetails.Add(ADN);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(ADN).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                    //adjusted table entity
                    foreach (var item in psForm.AdjustDebitNoteDetails)
                    {
                        if (item.CheckAmountAdjusted == true)
                        {
                            CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.SO_CN_PO_DN_No == item.PurchaseNo).OrderByDescending(x => x.ID).ToList().FirstOrDefault();
                            if (obj != null)
                            {
                                obj.Acc_Id = psForm.AdjustDebitNote.AccountId;
                                obj.Amount = psForm.AdjustDebitNote.Amount;
                                //obj.Cash_Cheque_No = psForm.AdjustDebitNote.CashChequeNo;
                                obj.Cash_Cheque_Date = psForm.AdjustDebitNote.Date;
                                obj.Remarks          = psForm.AdjustDebitNote.Remarks;
                                obj.Is_Cheque        = psForm.AdjustDebitNote.IsCheque;
                                //saving details entity
                                obj.SO_CN_PO_DN_No   = item.PurchaseNo;
                                obj.SO_CN_PO_DN_Date = item.PurchaseDate;
                                obj.SO_CN_PO_DN_Amt  = item.PurchaseAmount;
                                obj.Amt_Due          = item.AmountDue - item.AmountAdjusted;
                                obj.Amt_Refunded     = item.AmountAdjusted;
                                obj.Discount         = item.Discount;
                                obj.DN_CN_No         = psForm.AdjustDebitNote.DebitNoteNo;
                                obj.Type             = Type.ToString();
                                obj.UpdatedBy        = 0;
                                obj.UpdatedDate      = DateTime.Now.Date;
                                obj.AD_AC_NO         = AdjustedNo;
                                entities.SaveChanges();

                                if (item.AmountAdjusted == item.PurchaseAmount)
                                {
                                    PurchaseInvoice invoice = entities.PurchaseInvoices.Where(e => e.PI_No == item.PurchaseNo).SingleOrDefault();
                                    //PurchaseOrder order = entities.PurchaseOrders.Where(e => e.PO_No == item.PurchaseNo).SingleOrDefault();
                                    if (invoice != null)
                                    {
                                        invoice.PI_Status = Convert.ToByte(PI_Status.Paid);
                                        entities.SaveChanges();
                                    }
                                    //else if (order != null)
                                    //{
                                    //}
                                }
                                DebitNote debit = entities.DebitNotes.Where(e => e.DN_No == psForm.AdjustDebitNote.DebitNoteNo).SingleOrDefault();
                                if (debit != null)
                                {
                                    debit.DN_Status = Convert.ToByte(DN_Status.Adjusted);
                                    entities.SaveChanges();
                                }
                            }
                            else
                            {
                                CashAndBankTransaction cashbankobj = new CashAndBankTransaction()
                                {
                                    Acc_Id           = psForm.AdjustDebitNote.AccountId,
                                    Amount           = psForm.AdjustDebitNote.Amount,
                                    Cash_Cheque_Date = psForm.AdjustDebitNote.Date,
                                    Remarks          = psForm.AdjustDebitNote.Remarks,
                                    Is_Cheque        = psForm.AdjustDebitNote.IsCheque,
                                    SO_CN_PO_DN_No   = item.PurchaseNo,
                                    SO_CN_PO_DN_Date = item.PurchaseDate,
                                    SO_CN_PO_DN_Amt  = item.PurchaseAmount,
                                    Amt_Due          = item.AmountDue - item.AmountAdjusted,
                                    Amt_Refunded     = item.AmountAdjusted,
                                    Discount         = item.Discount,
                                    UpdatedBy        = 0,
                                    UpdatedDate      = DateTime.Now.Date,
                                    Cus_Sup_Id       = psForm.AdjustDebitNote.SupplierID,
                                    DN_CN_No         = psForm.AdjustDebitNote.DebitNoteNo,
                                    Type             = Type.ToString(),
                                    AD_AC_NO         = AdjustedNo
                                };
                                entities.CashAndBankTransactions.Add(cashbankobj);
                                entities.SaveChanges();
                                if (item.AmountAdjusted == item.PurchaseAmount)
                                {
                                    PurchaseInvoice invoice = entities.PurchaseInvoices.Where(e => e.PI_No == item.PurchaseNo).SingleOrDefault();
                                    //PurchaseOrder order = entities.PurchaseOrders.Where(e => e.PO_No == item.PurchaseNo).SingleOrDefault();
                                    if (invoice != null)
                                    {
                                        invoice.PI_Status = Convert.ToByte(PI_Status.Paid);
                                        entities.SaveChanges();
                                    }
                                    //else if (order != null)
                                    //{
                                    //}
                                }
                                DebitNote debit = entities.DebitNotes.Where(e => e.DN_No == psForm.AdjustDebitNote.DebitNoteNo).SingleOrDefault();
                                if (debit != null)
                                {
                                    debit.DN_Status = Convert.ToByte(DN_Status.Adjusted);
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(psForm.AdjustDebitNote.ID);
        }
        public int UpdatePaymentToSupplier(PaymentToSupplierForm psForm)
        {
            try
            {
                using (SASEntitiesEDM entities = new SASEntitiesEDM())
                {
                    foreach (var item in psForm.PaymentToSupplierDetails)
                    {
                        CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.Cash_Cheque_No == psForm.PaymentToSupplier.CashChequeNo &&
                                                                                            e.SO_CN_PO_DN_No == item.PurchaseNo).SingleOrDefault();
                        if (obj != null)
                        {
                            obj.Acc_Id           = psForm.PaymentToSupplier.AccountId;
                            obj.Amount           = psForm.PaymentToSupplier.Amount;
                            obj.Cash_Cheque_No   = psForm.PaymentToSupplier.CashChequeNo;
                            obj.Cash_Cheque_Date = psForm.PaymentToSupplier.Date;
                            obj.Remarks          = psForm.PaymentToSupplier.Remarks;

                            obj.Is_Cheque = psForm.PaymentToSupplier.IsCheque;

                            //saving details entity
                            obj.SO_CN_PO_DN_No   = item.PurchaseNo;
                            obj.SO_CN_PO_DN_Date = item.PurchaseDate;
                            obj.SO_CN_PO_DN_Amt  = item.PurchaseAmount;
                            obj.Amt_Due          = obj.Amt_Due = item.AmountDue - item.AmountAdjusted;
                            obj.Amt_Refunded     = item.AmountAdjusted;
                            obj.Discount         = item.Discount;

                            obj.UpdatedBy   = 0;
                            obj.UpdatedDate = DateTime.Now;
                            entities.SaveChanges();

                            PurchaseInvoice invoice = entities.PurchaseInvoices.Where(e => e.PI_No == item.PurchaseNo).SingleOrDefault();
                            PurchaseOrder   order   = entities.PurchaseOrders.Where(e => e.PO_No == item.PurchaseNo).SingleOrDefault();
                            if (invoice != null)
                            {
                                if (item.AmountAdjusted == item.PurchaseAmount)
                                {
                                    invoice.PI_Status = Convert.ToByte(PI_Status.Paid);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    invoice.PI_Status = Convert.ToByte(PI_Status.UnPaid);
                                    entities.SaveChanges();
                                }
                            }
                            else if (order != null)
                            {
                                order.PO_Status = Convert.ToByte(PO_Status.Collected);
                                entities.SaveChanges();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(psForm.PaymentToSupplier.ID);
        }