public bool SaveSearchJson(string jsonSearch, int ScreenId, string ScreenName)
 {
     try
     {
         var result = entities.LastSelectionHistories.Where(x => x.Screen_Id == ScreenId).FirstOrDefault();
         if (result != null)
         {
             result.Last_Selection = jsonSearch;
             result.Last_Updated   = DateTime.Now;
             entities.SaveChanges();
             return(true);
         }
         else
         {
             LastSelectionHistory lastSelection = new LastSelectionHistory()
             {
                 Screen_Id      = ScreenId,
                 Screen_Name    = ScreenName,
                 Last_Selection = jsonSearch,
                 Last_Updated   = DateTime.Now
             };
             entities.LastSelectionHistories.Add(lastSelection);
             entities.SaveChanges();
             return(true);
         }
     }
     catch
     {
         return(false);
     }
 }
예제 #2
0
        public int SaveAdjustDebitNote(AdjustDebitNoteForm psForm)
        {
            int autoId = 0;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    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             = "S";
                        //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;
                        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.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)
                            {
                            }
                        }

                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(autoId);
        }
        public int UpdateRefundFromSupplier(RefundFromSupplierForm psForm)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    foreach (var item in psForm.RefundFromSupplierDetails)
                    {
                        CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.Cash_Cheque_No == psForm.RefundFromSupplier.CashChequeNo &&
                                                                                            e.SO_CN_PO_DN_No == item.PurchaseNo).SingleOrDefault();
                        if (obj != null)
                        {
                            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.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          = 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.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(psForm.RefundFromSupplier.ID);
        }
        public bool DeleteQuotatoin(int pqID)
        {
            bool result = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    var detailList = entities.PurchaseOrderDetails.Where(x => x.PO_ID == pqID).ToList();
                    if (detailList != null)
                    {
                        foreach (PurchaseOrderDetail pqdetail in detailList)
                        {
                            int PSId = Convert.ToInt32(pqdetail.PO_No);
                            if (PSId != 0)
                            {
                                ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                if (ps != null)
                                {
                                    ps.PandS_Qty_on_PO = ps.PandS_Qty_on_PO - pqdetail.PO_Qty;
                                    entities.SaveChanges();
                                }
                            }

                            pqdetail.PO_Qty      = 0;
                            pqdetail.PO_Price    = 0;
                            pqdetail.PO_Discount = 0;
                            pqdetail.PO_Amount   = 0;
                            pqdetail.GST_Rate    = 0;
                            entities.SaveChanges();
                        }
                    }
                    var obj = entities.PurchaseOrders.Where(x => x.ID == pqID).FirstOrDefault();
                    //entities.PurchaseOrders.Remove(obj);
                    if (obj != null)
                    {
                        obj.PO_Status      = Convert.ToByte(PO_Status.Cancelled);
                        obj.PO_Tot_bef_Tax = 0;
                        obj.PO_Tot_aft_Tax = 0;
                        obj.PO_GST_Amt     = 0;
                        //obj.IsDeleted = true;
                        obj.ModifiedDate = DateTime.Now;
                        entities.SaveChanges();
                    }
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
 private void CreateDebitNote(PurchaseInvoiceForm invoiceData)
 {
     try
     {
         using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
         {
             DebitNote deb = entities.DebitNotes.Where(e => e.PI_Id == invoiceData.Invoice.ID).SingleOrDefault();
             if (Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax) < 0)
             {
                 if (deb != null)
                 {
                     deb.DN_Date = DateTime.Now.Date;
                     // deb.DN_No = "DN-" + (GetLastDebitNoteNo() + 1);
                     //deb.PI_Id = obj.ID;
                     deb.Sup_Id = invoiceData.Invoice.SupplierID;
                     // deb.Sup_CN_Amount = Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax);
                     //deb.ModifiedDate = DateTime.Now;
                     entities.SaveChanges();
                 }
                 else
                 {
                     deb           = new DebitNote();
                     deb.DN_Date   = DateTime.Now.Date;
                     deb.DN_No     = "DN-" + (GetLastDebitNoteNo() + 1);
                     deb.PI_Id     = invoiceData.Invoice.ID;
                     deb.Sup_Id    = invoiceData.Invoice.SupplierID;
                     deb.DN_Status = Convert.ToByte(DN_Status.UnAdjusted);
                     // deb.Sup_CN_Amount = Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax);
                     deb.CreatedDate = DateTime.Now;
                     entities.DebitNotes.Add(deb);
                     entities.SaveChanges();
                 }
             }
             else
             {
                 if (deb != null)
                 {
                     entities.DebitNotes.Remove(deb);
                     entities.SaveChanges();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public bool DeleteQuotatoin(int pqID)
        {
            bool result = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    //var detailList = entities.PurchaseQuotationDetails.Where(x => x.PQ_ID == pqID).ToList();
                    //if (detailList != null)
                    //{
                    //    //foreach (PurchaseQuotationDetail pqdetail in detailList)
                    //    //{
                    //    //    entities.PurchaseQuotationDetails.Remove(pqdetail);
                    //    //}

                    //}
                    var obj = entities.PurchaseQuotations.Where(x => x.ID == pqID).FirstOrDefault();
                    //entities.PurchaseQuotations.Remove(obj);
                    if (obj != null)
                    {
                        obj.IsDeleted    = true;
                        obj.ModifiedDate = DateTime.Now;
                        entities.SaveChanges();
                    }
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public void UpdateDebitNote(DebitNoteForm debitNote)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    DebitNote obj = entities.DebitNotes.Where(e => e.DN_No == debitNote.DebitNote.DebitNo
                                                              ).SingleOrDefault();
                    if (obj != null)
                    {
                        if (!string.IsNullOrEmpty(debitNote.DebitNote.SupplierCreditNoteAmount))
                        {
                            debitNote.DebitNote.SCNAmount = Convert.ToDecimal(debitNote.DebitNote.SupplierCreditNoteAmount);
                        }

                        obj.Sup_CN_No     = debitNote.DebitNote.SupplierCreditNoteNo;
                        obj.Sup_CN_Date   = debitNote.DebitNote.SupplierCreditNoteDate;
                        obj.Sup_CN_Amount = debitNote.DebitNote.SCNAmount;
                        obj.ModifiedDate  = DateTime.Now;
                        entities.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to add or edit purchase quotation
        /// </summary>
        /// <param name="quotationData"></param>
        /// <returns></returns>
        public int AddUpdateQuotation(PurchaseQuotationForm quotationData)
        {
            int autoId = 0;
            //Add purchase quotation
            PurchaseQuotation obj = new PurchaseQuotation();

            obj.ID             = quotationData.Quotation.ID;
            obj.Sup_Id         = quotationData.Quotation.SupplierID;
            obj.PQ_Conv_to_PO  = quotationData.Quotation.PQ_Conv_to_PO;
            obj.PQ_Conv_to_PI  = quotationData.Quotation.PQ_Conv_to_PI;
            obj.PQ_Date        = quotationData.Quotation.QuotationDate;
            obj.PQ_GST_Amt     = Convert.ToDecimal(quotationData.Quotation.TotalTax);
            obj.PQ_No          = quotationData.Quotation.QuotationNo;
            obj.PQ_TandC       = quotationData.Quotation.TermsAndConditions;
            obj.PQ_Tot_aft_Tax = Convert.ToDecimal(quotationData.Quotation.TotalAfterTax);
            obj.PQ_Tot_bef_Tax = Convert.ToDecimal(quotationData.Quotation.TotalBeforeTax);
            obj.PQ_Valid_for   = quotationData.Quotation.ValidForDays;
            obj.Exc_Inc_GST    = quotationData.Quotation.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseQuotations.AsNoTracking().FirstOrDefault(x => x.ID == quotationData.Quotation.ID) == null)
                    {
                        //obj.CreatedBy = quotationData.PQModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseQuotations.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = quotationData.PQModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseQuotationDetail PQDetails;
                        if (quotationData.QuotationDetails != null)
                        {
                            foreach (PurchaseQuotationDetailEntity PQDetailEntity in quotationData.QuotationDetails)
                            {
                                PQDetails             = new PurchaseQuotationDetail();
                                PQDetails.PQ_ID       = autoId;
                                PQDetails.PQ_No       = PQDetailEntity.PQNo;
                                PQDetails.PandS_Code  = PQDetailEntity.PandSCode;
                                PQDetails.PandS_Name  = PQDetailEntity.PandSName;
                                PQDetails.PQ_Amount   = PQDetailEntity.PQAmount;
                                PQDetails.PQ_Discount = PQDetailEntity.PQDiscount;
                                PQDetails.PQ_No       = PQDetailEntity.PQNo;
                                PQDetails.PQ_Price    = Convert.ToDecimal(PQDetailEntity.PQPrice);
                                PQDetails.PQ_Qty      = PQDetailEntity.PQQty;
                                PQDetails.GST_Code    = PQDetailEntity.GSTCode;
                                PQDetails.GST_Rate    = PQDetailEntity.GSTRate;

                                if (entities.PurchaseQuotationDetails.AsNoTracking().FirstOrDefault(x => x.ID == PQDetailEntity.ID) == null)
                                {
                                    entities.PurchaseQuotationDetails.Add(PQDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PQDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int UpdationOrder(PurchaseOrderForm orderData)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    PurchaseOrder obj = entities.PurchaseOrders.Where(e => e.PO_No == orderData.Order.OrderNo
                                                                      ).SingleOrDefault();
                    if (obj != null)
                    {
                        //obj.ID = orderData.Order.ID;
                        obj.Sup_Id = orderData.Order.SupplierID;
                        // obj.PO_Conv_to_PO = orderData.Order.PO_Conv_to_PO;
                        // obj.PO_Conv_to_PI = orderData.Order.PO_Conv_to_PI;
                        obj.PO_Date        = orderData.Order.OrderDate;
                        obj.PO_GST_Amt     = Convert.ToDecimal(orderData.Order.TotalTax);
                        obj.PO_No          = orderData.Order.OrderNo;
                        obj.PO_TandC       = orderData.Order.TermsAndConditions;
                        obj.PO_Tot_aft_Tax = Convert.ToDecimal(orderData.Order.TotalAfterTax);
                        obj.PO_Tot_bef_Tax = Convert.ToDecimal(orderData.Order.TotalBeforeTax);
                        //obj.PO_Valid_for = orderData.Order.ValidForDays;
                        obj.PO_Del_Date  = orderData.Order.DeliveryDate;
                        obj.Exc_Inc_GST  = orderData.Order.ExcIncGST;
                        obj.ModifiedDate = DateTime.Now;
                        entities.SaveChanges();
                    }

                    var objPurchase = entities.PurchaseOrderDetails.Where
                                          (e => e.PO_ID == obj.ID).ToList();
                    if (objPurchase != null)
                    {
                        foreach (var item in objPurchase)
                        {
                            int PSId = Convert.ToInt32(item.PO_No);
                            if (PSId != 0)
                            {
                                ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                if (ps != null)
                                {
                                    ps.PandS_Qty_on_PO = ps.PandS_Qty_on_PO - item.PO_Qty;
                                    entities.SaveChanges();
                                }
                            }
                            entities.PurchaseOrderDetails.Remove(item);
                            entities.SaveChanges();
                        }
                    }
                    PurchaseOrderDetail PODetails;
                    if (orderData.OrderDetails != null)
                    {
                        foreach (PurchaseOrderDetailEntity PODetailEntity in orderData.OrderDetails)
                        {
                            PODetails             = new PurchaseOrderDetail();
                            PODetails.PO_ID       = obj.ID;
                            PODetails.PO_No       = PODetailEntity.PONo;
                            PODetails.PandS_Code  = PODetailEntity.PandSCode;
                            PODetails.PandS_Name  = PODetailEntity.PandSName;
                            PODetails.PO_Amount   = PODetailEntity.POAmount;
                            PODetails.PO_Discount = PODetailEntity.PODiscount;
                            PODetails.PO_No       = PODetailEntity.PONo;
                            PODetails.PO_Price    = Convert.ToDecimal(PODetailEntity.POPrice);
                            PODetails.PO_Qty      = PODetailEntity.POQty;
                            PODetails.GST_Code    = PODetailEntity.GSTCode;
                            PODetails.GST_Rate    = PODetailEntity.GSTRate;

                            entities.PurchaseOrderDetails.Add(PODetails);
                            entities.SaveChanges();

                            int PSId = Convert.ToInt32(PODetailEntity.PONo);
                            if (PSId != 0)
                            {
                                ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                if (ps != null)
                                {
                                    ps.PandS_Qty_on_PO = ps.PandS_Qty_on_PO + PODetailEntity.POQty;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                    return(obj.ID);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to convert the Purchase quotation to order
        /// </summary>
        /// <param name="quotationData"></param>
        /// <returns></returns>
        public int ConvertToPurchaseOrder(PurchaseQuotationForm quotationData)
        {
            int autoId = 0;

            PurchaseOrder obj = new PurchaseOrder();

            //obj.ID = quotationData.Quotation.ID;
            obj.Sup_Id         = quotationData.Quotation.SupplierID;
            obj.PO_Date        = quotationData.Quotation.QuotationDate;
            obj.PO_Del_Date    = DateTime.Now;
            obj.PO_GST_Amt     = Convert.ToDecimal(quotationData.Quotation.TotalTax);
            obj.PO_No          = "PO-" + (GetLastOrderNo() + 1);
            obj.PO_TandC       = quotationData.Quotation.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(quotationData.Quotation.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(quotationData.Quotation.TotalBeforeTax);
            obj.PO_Status      = Convert.ToByte(PO_Status.unDeposited);
            obj.Exc_Inc_GST    = quotationData.Quotation.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == quotationData.Quotation.ID) == null)
                    {
                        //obj.CreatedBy = quotationData.PQModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = quotationData.PQModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PQDetails;
                        if (quotationData.QuotationDetails != null)
                        {
                            foreach (PurchaseQuotationDetailEntity PQDetailEntity in quotationData.QuotationDetails)
                            {
                                PQDetails             = new PurchaseOrderDetail();
                                PQDetails.PO_ID       = autoId;
                                PQDetails.PO_No       = PQDetailEntity.PQNo;
                                PQDetails.PandS_Code  = PQDetailEntity.PandSCode;
                                PQDetails.PandS_Name  = PQDetailEntity.PandSName;
                                PQDetails.PO_Amount   = PQDetailEntity.PQAmount;
                                PQDetails.PO_Discount = PQDetailEntity.PQDiscount;
                                PQDetails.PO_No       = PQDetailEntity.PQNo;
                                PQDetails.PO_Price    = Convert.ToDecimal(PQDetailEntity.PQPrice);
                                PQDetails.PO_Qty      = PQDetailEntity.PQQty;
                                PQDetails.GST_Code    = PQDetailEntity.GSTCode;
                                PQDetails.GST_Rate    = PQDetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PQDetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PQDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PQDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }

                        PurchaseQuotation objQ = entities.PurchaseQuotations.Where(e => e.PQ_No == quotationData.Quotation.QuotationNo
                                                                                   ).SingleOrDefault();
                        if (objQ != null)
                        {
                            objQ.PQ_Conv_to_PO = true;
                            objQ.Conv_to_No    = obj.PO_No;
                            objQ.ModifiedDate  = DateTime.Now;
                            entities.SaveChanges();
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public bool CreateSupplier(SupplierDetailEntity entity)
 {
     try
     {
         var supplier = entities.Suppliers.FirstOrDefault(x => x.ID == entity.ID);
         if (supplier != null)
         {
             supplier.Sup_Name                = entity.SupplierName;
             supplier.Sup_Bill_to_city        = entity.Sup_Bill_to_city;
             supplier.Sup_Bill_to_country     = entity.Sup_Bill_to_country;
             supplier.Sup_Bill_to_line1       = entity.Sup_Bill_to_line1;
             supplier.Sup_Bill_to_line2       = entity.Sup_Bill_to_line2;
             supplier.Sup_Bill_to_post_code   = entity.Sup_Bill_to_post_code;
             supplier.Sup_Bill_to_state       = entity.Sup_Bill_to_state;
             supplier.Sup_Company_Reg_no      = entity.Supp_Reg_No;
             supplier.Sup_Contact_Person      = entity.ContactPerson;
             supplier.Sup_Credit_Limit_Amount = Convert.ToDecimal(entity.CreditLimitAmount);
             supplier.Sup_Credit_Limit_Days   = entity.CreditLimitDays == string.Empty ? null as Int32? : Convert.ToInt32(entity.CreditLimitDays);
             supplier.Sup_Email               = entity.Email;
             supplier.Sup_Fax               = entity.Fax;
             supplier.Sup_GST_Reg_No        = entity.GstRegistrationNo;
             supplier.Sup_Remarks           = entity.Remarks;
             supplier.Sup_Ship_to_city      = entity.ShipCity;
             supplier.Sup_Ship_to_country   = entity.ShipCountry;
             supplier.Sup_Ship_to_line1     = entity.ShipAddressLine1;
             supplier.Sup_Ship_to_line2     = entity.ShipAddressLine2;
             supplier.Sup_Ship_to_post_code = entity.ShipPostalCode;
             supplier.Sup_Ship_to_state     = entity.ShipState;
             supplier.Sup_Telephone         = entity.Telephone;
             supplier.Sup_Charge_GST        = entity.ChangeSupplierGST;
             supplier.Sup_Inactive          = entity.IsInActive;
             supplier.TaxId = entity.TaxId;
             entities.SaveChanges();
             return(true);
         }
         else
         {
             Supplier supplierdetail = new Supplier()
             {
                 Sup_Name                = entity.SupplierName,
                 Sup_Bill_to_city        = entity.Sup_Bill_to_city,
                 Sup_Bill_to_country     = entity.Sup_Bill_to_country,
                 Sup_Bill_to_line1       = entity.Sup_Bill_to_line1,
                 Sup_Bill_to_line2       = entity.Sup_Bill_to_line2,
                 Sup_Bill_to_post_code   = entity.Sup_Bill_to_post_code,
                 Sup_Bill_to_state       = entity.Sup_Bill_to_state,
                 Sup_Company_Reg_no      = entity.Supp_Reg_No,
                 Sup_Contact_Person      = entity.ContactPerson,
                 Sup_Credit_Limit_Amount = Convert.ToDecimal(entity.CreditLimitAmount),
                 Sup_Credit_Limit_Days   = entity.CreditLimitDays == string.Empty ? null as Int32? : Convert.ToInt32(entity.CreditLimitDays),
                 Sup_Email               = entity.Email,
                 Sup_Fax               = entity.Fax,
                 Sup_GST_Reg_No        = entity.GstRegistrationNo,
                 Sup_Remarks           = entity.Remarks,
                 Sup_Ship_to_city      = entity.ShipCity,
                 Sup_Ship_to_country   = entity.ShipCountry,
                 Sup_Ship_to_line1     = entity.ShipAddressLine1,
                 Sup_Ship_to_line2     = entity.ShipAddressLine2,
                 Sup_Ship_to_post_code = entity.ShipPostalCode,
                 Sup_Ship_to_state     = entity.ShipState,
                 Sup_Telephone         = entity.Telephone,
                 Sup_Charge_GST        = entity.ChangeSupplierGST,
                 Sup_Inactive          = entity.IsInActive,
                 TaxId = entity.SelectedTaxId
             };
             entities.Suppliers.Add(supplierdetail);
             entities.SaveChanges();
             return(true);
         }
     }
     catch
     {
         return(false);
     }
     //if (entities.SupplierDetails.AsNoTracking().FirstOrDefault(x => x.ID == entity.ID) == null)
     //{
     //    entities.SupplierDetails.Add(entity);
     //}
     //else
     //{
     //    entities.Entry(entity).State = EntityState.Modified;
     //}
 }
        /// <summary>
        /// This method is used to convert the Purchase order to order
        /// </summary>
        /// <param name="orderData"></param>
        /// <returns></returns>
        public int ConvertToPurchaseOrder(PurchaseOrderForm orderData)
        {
            int autoId = 0;
            //Add purchase order
            PurchaseOrder obj = new PurchaseOrder();

            //obj.ID = orderData.Order.ID;
            obj.Sup_Id         = orderData.Order.SupplierID;
            obj.PO_Date        = orderData.Order.OrderDate;
            obj.PO_Del_Date    = DateTime.Now;
            obj.PO_GST_Amt     = Convert.ToDecimal(orderData.Order.TotalTax);
            obj.PO_No          = "PO-" + GetLastOrderNo();
            obj.PO_TandC       = orderData.Order.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(orderData.Order.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(orderData.Order.TotalBeforeTax);

            obj.Exc_Inc_GST = orderData.Order.ExcIncGST;
            obj.IsDeleted   = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == orderData.Order.ID) == null)
                    {
                        //obj.CreatedBy = orderData.POModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = orderData.POModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PODetails;
                        if (orderData.OrderDetails != null)
                        {
                            foreach (PurchaseOrderDetailEntity PODetailEntity in orderData.OrderDetails)
                            {
                                PODetails             = new PurchaseOrderDetail();
                                PODetails.PO_ID       = autoId;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PandS_Code  = PODetailEntity.PandSCode;
                                PODetails.PandS_Name  = PODetailEntity.PandSName;
                                PODetails.PO_Amount   = PODetailEntity.POAmount;
                                PODetails.PO_Discount = PODetailEntity.PODiscount;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PO_Price    = Convert.ToDecimal(PODetailEntity.POPrice);
                                PODetails.PO_Qty      = PODetailEntity.POQty;
                                PODetails.GST_Code    = PODetailEntity.GSTCode;
                                PODetails.GST_Rate    = PODetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PODetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PODetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PODetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                            }
                        }

                        PurchaseOrder objQ = entities.PurchaseOrders.Where(e => e.PO_No == orderData.Order.OrderNo
                                                                           ).SingleOrDefault();
                        if (objQ != null)
                        {
                            //objQ.PO_Conv_to_PO = true;
                            objQ.ModifiedDate = DateTime.Now;
                            entities.SaveChanges();
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #13
0
        public int UpdateAdjustDebitNote(AdjustDebitNoteForm psForm)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    foreach (var item in psForm.AdjustDebitNoteDetails)
                    {
                        if (item.CheckAmountAdjusted == true)
                        {
                            CashAndBankTransaction obj = entities.CashAndBankTransactions.Where(e => e.SO_CN_PO_DN_No == item.PurchaseNo).SingleOrDefault();
                            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;
                                obj.Amt_Refunded     = item.AmountAdjusted;
                                obj.Discount         = item.Discount;
                                obj.DN_CN_No         = psForm.AdjustDebitNote.DebitNoteNo;

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

                                if (item.AmountDue == 0 && 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,
                                    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             = "S"
                                };
                                entities.CashAndBankTransactions.Add(cashbankobj);
                                entities.SaveChanges();
                                if (item.AmountDue == 0 && 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 UpdationInvoice(PurchaseInvoiceForm invoiceData)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    PurchaseInvoice obj = entities.PurchaseInvoices.Where(e => e.PI_No == invoiceData.Invoice.InvoiceNo
                                                                          ).SingleOrDefault();
                    if (obj != null)
                    {
                        //obj.ID = invoiceData.Invoice.ID;
                        obj.Sup_Id = invoiceData.Invoice.SupplierID;

                        obj.PI_Date         = invoiceData.Invoice.InvoiceDate;
                        obj.PI_GST_Amt      = Convert.ToDecimal(invoiceData.Invoice.TotalTax);
                        obj.PI_No           = invoiceData.Invoice.InvoiceNo;
                        obj.PI_TandC        = invoiceData.Invoice.TermsAndConditions;
                        obj.PI_Tot_aft_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalAfterTax);
                        obj.PI_Tot_bef_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax);
                        obj.PI_Pmt_Due_Date = invoiceData.Invoice.PaymentDueDate;
                        obj.Our_PO_No       = invoiceData.Invoice.OurPONo;
                        obj.Exc_Inc_GST     = invoiceData.Invoice.ExcIncGST;
                        obj.ModifiedDate    = DateTime.Now;
                        entities.SaveChanges();
                    }

                    invoiceData.Invoice.ID = obj.ID;
                    CreateDebitNote(invoiceData);

                    var objPurchase = entities.PurchaseInvoiceDetails.Where
                                          (e => e.PI_ID == obj.ID).ToList();
                    if (objPurchase != null)
                    {
                        foreach (var item in objPurchase)
                        {
                            int PSId = Convert.ToInt32(item.PI_No);
                            if (PSId != 0)
                            {
                                ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                if (ps != null)
                                {
                                    ps.PandS_Qty_in_stock = ps.PandS_Qty_in_stock - item.PI_Qty;
                                    entities.SaveChanges();
                                }
                            }

                            entities.PurchaseInvoiceDetails.Remove(item);
                            entities.SaveChanges();
                        }
                    }
                    PurchaseInvoiceDetail PIDetails;

                    if (invoiceData.InvoiceDetails != null)
                    {
                        foreach (PurchaseInvoiceDetailEntity PIDetailEntity in invoiceData.InvoiceDetails)
                        {
                            PIDetails             = new PurchaseInvoiceDetail();
                            PIDetails.PI_ID       = obj.ID;
                            PIDetails.PI_No       = PIDetailEntity.PINo;
                            PIDetails.PandS_Code  = PIDetailEntity.PandSCode;
                            PIDetails.PandS_Name  = PIDetailEntity.PandSName;
                            PIDetails.PI_Amount   = PIDetailEntity.PIAmount;
                            PIDetails.PI_Discount = PIDetailEntity.PIDiscount;
                            PIDetails.PI_No       = PIDetailEntity.PINo;
                            PIDetails.PI_Price    = Convert.ToDecimal(PIDetailEntity.PIPrice);
                            PIDetails.PI_Qty      = PIDetailEntity.PIQty;
                            PIDetails.GST_Code    = PIDetailEntity.GSTCode;
                            PIDetails.GST_Rate    = PIDetailEntity.GSTRate;

                            entities.PurchaseInvoiceDetails.Add(PIDetails);
                            entities.SaveChanges();

                            int PSId = Convert.ToInt32(PIDetailEntity.PINo);
                            if (PSId != 0)
                            {
                                ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                if (ps != null)
                                {
                                    ps.PandS_Qty_in_stock = ps.PandS_Qty_in_stock + PIDetailEntity.PIQty;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                    int i = entities.Database.ExecuteSqlCommand("exec PRC_Update_PurchaseInvoiceData {0}", obj.ID);
                    return(obj.ID);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// This method is used to add or edit purchase order
        /// </summary>
        /// <param name="orderData"></param>
        /// <returns></returns>
        public int AddUpdateOrder(PurchaseOrderForm orderData)
        {
            int autoId = 0;
            //Add purchase order
            PurchaseOrder obj = new PurchaseOrder();

            obj.ID     = orderData.Order.ID;
            obj.Sup_Id = orderData.Order.SupplierID;
            //obj.PO_Conv_to_PO = orderData.Order.PO_Conv_to_PO;
            obj.PO_Conv_to_PI  = orderData.Order.PO_Conv_to_PI;
            obj.PO_Date        = orderData.Order.OrderDate;
            obj.PO_GST_Amt     = Convert.ToDecimal(orderData.Order.TotalTax);
            obj.PO_No          = orderData.Order.OrderNo;
            obj.PO_TandC       = orderData.Order.TermsAndConditions;
            obj.PO_Tot_aft_Tax = Convert.ToDecimal(orderData.Order.TotalAfterTax);
            obj.PO_Tot_bef_Tax = Convert.ToDecimal(orderData.Order.TotalBeforeTax);
            obj.PO_Status      = Convert.ToByte(PO_Status.unDeposited);
            obj.PO_Del_Date    = orderData.Order.DeliveryDate;
            obj.Exc_Inc_GST    = orderData.Order.ExcIncGST;
            obj.IsDeleted      = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseOrders.AsNoTracking().FirstOrDefault(x => x.ID == orderData.Order.ID) == null)
                    {
                        //obj.CreatedBy = orderData.POModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseOrders.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = orderData.POModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }
                    if (autoId > 0)
                    {
                        PurchaseOrderDetail PODetails;
                        if (orderData.OrderDetails != null)
                        {
                            foreach (PurchaseOrderDetailEntity PODetailEntity in orderData.OrderDetails)
                            {
                                PODetails             = new PurchaseOrderDetail();
                                PODetails.PO_ID       = autoId;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PandS_Code  = PODetailEntity.PandSCode;
                                PODetails.PandS_Name  = PODetailEntity.PandSName;
                                PODetails.PO_Amount   = PODetailEntity.POAmount;
                                PODetails.PO_Discount = PODetailEntity.PODiscount;
                                PODetails.PO_No       = PODetailEntity.PONo;
                                PODetails.PO_Price    = Convert.ToDecimal(PODetailEntity.POPrice);
                                PODetails.PO_Qty      = PODetailEntity.POQty;
                                PODetails.GST_Code    = PODetailEntity.GSTCode;
                                PODetails.GST_Rate    = PODetailEntity.GSTRate;

                                if (entities.PurchaseOrderDetails.AsNoTracking().FirstOrDefault(x => x.ID == PODetailEntity.ID) == null)
                                {
                                    entities.PurchaseOrderDetails.Add(PODetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PODetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                                int PSId = Convert.ToInt32(PODetailEntity.PONo);
                                if (PSId != 0)
                                {
                                    ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                    if (ps != null)
                                    {
                                        ps.PandS_Qty_on_PO = ps.PandS_Qty_on_PO + PODetailEntity.POQty;
                                        entities.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int UpdationQuotation(PurchaseQuotationForm quotationData)
        {
            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    PurchaseQuotation obj = entities.PurchaseQuotations.Where(e => e.PQ_No == quotationData.Quotation.QuotationNo
                                                                              ).SingleOrDefault();
                    if (obj != null)
                    {
                        //obj.ID = quotationData.Quotation.ID;
                        obj.Sup_Id = quotationData.Quotation.SupplierID;
                        // obj.PQ_Conv_to_PO = quotationData.Quotation.PQ_Conv_to_PO;
                        // obj.PQ_Conv_to_PI = quotationData.Quotation.PQ_Conv_to_PI;
                        obj.PQ_Date        = quotationData.Quotation.QuotationDate;
                        obj.PQ_GST_Amt     = Convert.ToDecimal(quotationData.Quotation.TotalTax);
                        obj.PQ_No          = quotationData.Quotation.QuotationNo;
                        obj.PQ_TandC       = quotationData.Quotation.TermsAndConditions;
                        obj.PQ_Tot_aft_Tax = Convert.ToDecimal(quotationData.Quotation.TotalAfterTax);
                        obj.PQ_Tot_bef_Tax = Convert.ToDecimal(quotationData.Quotation.TotalBeforeTax);
                        obj.PQ_Valid_for   = quotationData.Quotation.ValidForDays;
                        obj.Exc_Inc_GST    = quotationData.Quotation.ExcIncGST;
                        obj.ModifiedDate   = DateTime.Now;
                        entities.SaveChanges();
                    }

                    var objPurchase = entities.PurchaseQuotationDetails.Where
                                          (e => e.PQ_ID == obj.ID).ToList();
                    if (objPurchase != null)
                    {
                        foreach (var item in objPurchase)
                        {
                            entities.PurchaseQuotationDetails.Remove(item);
                            entities.SaveChanges();
                        }
                    }
                    PurchaseQuotationDetail PQDetails;
                    if (quotationData.QuotationDetails != null)
                    {
                        foreach (PurchaseQuotationDetailEntity PQDetailEntity in quotationData.QuotationDetails)
                        {
                            PQDetails             = new PurchaseQuotationDetail();
                            PQDetails.PQ_ID       = obj.ID;
                            PQDetails.PQ_No       = PQDetailEntity.PQNo;
                            PQDetails.PandS_Code  = PQDetailEntity.PandSCode;
                            PQDetails.PandS_Name  = PQDetailEntity.PandSName;
                            PQDetails.PQ_Amount   = PQDetailEntity.PQAmount;
                            PQDetails.PQ_Discount = PQDetailEntity.PQDiscount;
                            PQDetails.PQ_No       = PQDetailEntity.PQNo;
                            PQDetails.PQ_Price    = Convert.ToDecimal(PQDetailEntity.PQPrice);
                            PQDetails.PQ_Qty      = PQDetailEntity.PQQty;
                            PQDetails.GST_Code    = PQDetailEntity.GSTCode;
                            PQDetails.GST_Rate    = PQDetailEntity.GSTRate;

                            entities.PurchaseQuotationDetails.Add(PQDetails);
                            entities.SaveChanges();
                        }
                    }
                    return(obj.ID);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int DeleteInvoice(string pqNo)
        {
            int PIStatus = 0;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    var PInvoice = entities.PurchaseInvoices.Where(e => e.PI_No == pqNo).FirstOrDefault();
                    if (PInvoice != null)
                    {
                        var PO_PI = entities.CashAndBankTransactions.Where(x => x.SO_CN_PO_DN_No == pqNo
                                                                           ).FirstOrDefault();

                        if (PO_PI != null)
                        {
                            if (PO_PI.Amt_Due != null)
                            {
                                PInvoice.PI_Status = Convert.ToByte(PI_Status.UnPaid);
                                PIStatus           = Convert.ToByte(PI_Status.UnPaid);
                            }
                            else
                            {
                                PInvoice.PI_Status = Convert.ToByte(PI_Status.Paid);
                                PIStatus           = Convert.ToByte(PI_Status.Paid);
                            }
                        }
                        else
                        {
                            PInvoice.PI_Status = Convert.ToByte(PI_Status.Cancelled);
                            PIStatus           = Convert.ToByte(PI_Status.Cancelled);

                            PInvoice.PI_Tot_bef_Tax = 0;
                            PInvoice.PI_Tot_aft_Tax = 0;
                            PInvoice.PI_GST_Amt     = 0;
                            entities.SaveChanges();

                            var pd = entities.PurchaseInvoiceDetails.Where(e => e.PI_ID == PInvoice.ID).ToList();
                            if (pd != null)
                            {
                                foreach (var item in pd)
                                {
                                    int PSId = Convert.ToInt32(item.PI_No);
                                    if (PSId != 0)
                                    {
                                        ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                        if (ps != null)
                                        {
                                            ps.PandS_Qty_in_stock = ps.PandS_Qty_in_stock - item.PI_Qty;
                                            entities.SaveChanges();
                                        }
                                    }

                                    item.PI_Qty      = 0;
                                    item.PI_Price    = 0;
                                    item.PI_Discount = 0;
                                    item.PI_Amount   = 0;
                                    item.GST_Rate    = 0;
                                    entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(PIStatus);
        }
        /// <summary>
        /// This method is used to add or edit purchase invoice
        /// </summary>
        /// <param name="invoiceData"></param>
        /// <returns></returns>
        public int AddUpdateInvoice(PurchaseInvoiceForm invoiceData)
        {
            int autoId = 0;
            //Add purchase invoice
            PurchaseInvoice obj = new PurchaseInvoice();

            obj.ID     = invoiceData.Invoice.ID;
            obj.Sup_Id = invoiceData.Invoice.SupplierID;

            obj.PI_Date         = invoiceData.Invoice.InvoiceDate;
            obj.PI_GST_Amt      = Convert.ToDecimal(invoiceData.Invoice.TotalTax);
            obj.PI_No           = invoiceData.Invoice.InvoiceNo;
            obj.PI_TandC        = invoiceData.Invoice.TermsAndConditions;
            obj.PI_Tot_aft_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalAfterTax);
            obj.PI_Tot_bef_Tax  = Convert.ToDecimal(invoiceData.Invoice.TotalBeforeTax);
            obj.Our_PO_No       = invoiceData.Invoice.OurPONo;
            obj.PI_Pmt_Due_Date = invoiceData.Invoice.PaymentDueDate;
            obj.Exc_Inc_GST     = invoiceData.Invoice.ExcIncGST;
            obj.PI_Status       = Convert.ToByte(PI_Status.UnPaid);
            obj.IsDeleted       = false;

            try
            {
                using (SDNPurchasingDBEntities entities = new SDNPurchasingDBEntities())
                {
                    if (entities.PurchaseInvoices.AsNoTracking().FirstOrDefault(x => x.ID == invoiceData.Invoice.ID) == null)
                    {
                        //obj.CreatedBy = invoiceData.PIModel.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        entities.PurchaseInvoices.Add(obj);
                        entities.SaveChanges();
                        autoId = obj.ID;
                    }
                    else
                    {
                        // obj.ModifiedBy = invoiceData.PIModel.ModifiedBy;
                        obj.ModifiedDate          = DateTime.Now;
                        entities.Entry(obj).State = EntityState.Modified;
                        autoId = entities.SaveChanges();
                    }

                    invoiceData.Invoice.ID = autoId;
                    CreateDebitNote(invoiceData);

                    if (autoId > 0)
                    {
                        PurchaseInvoiceDetail PIDetails;
                        if (invoiceData.InvoiceDetails != null)
                        {
                            foreach (PurchaseInvoiceDetailEntity PIDetailEntity in invoiceData.InvoiceDetails)
                            {
                                PIDetails             = new PurchaseInvoiceDetail();
                                PIDetails.PI_ID       = autoId;
                                PIDetails.PI_No       = PIDetailEntity.PINo;
                                PIDetails.PandS_Code  = PIDetailEntity.PandSCode;
                                PIDetails.PandS_Name  = PIDetailEntity.PandSName;
                                PIDetails.PI_Amount   = PIDetailEntity.PIAmount;
                                PIDetails.PI_Discount = PIDetailEntity.PIDiscount;
                                PIDetails.PI_No       = PIDetailEntity.PINo;
                                PIDetails.PI_Price    = Convert.ToDecimal(PIDetailEntity.PIPrice);
                                PIDetails.PI_Qty      = PIDetailEntity.PIQty;
                                PIDetails.GST_Code    = PIDetailEntity.GSTCode;
                                PIDetails.GST_Rate    = PIDetailEntity.GSTRate;


                                if (entities.PurchaseInvoiceDetails.AsNoTracking().FirstOrDefault(x => x.ID == PIDetailEntity.ID) == null)
                                {
                                    entities.PurchaseInvoiceDetails.Add(PIDetails);
                                    entities.SaveChanges();
                                }
                                else
                                {
                                    entities.Entry(PIDetails).State = EntityState.Modified;
                                    entities.SaveChanges();
                                }
                                int PSId = Convert.ToInt32(PIDetailEntity.PINo);
                                if (PSId != 0)
                                {
                                    ProductsAndService ps = entities.ProductsAndServices.SingleOrDefault(e => e.ID == PSId);
                                    if (ps != null)
                                    {
                                        ps.PandS_Qty_in_stock = ps.PandS_Qty_in_stock + PIDetailEntity.PIQty;
                                        entities.SaveChanges();
                                    }
                                }
                            }
                        }
                    }

                    int i = entities.Database.ExecuteSqlCommand("exec PRC_Insert_PurchaseInvoiceData {0}", obj.ID);
                }
                return(autoId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }