Example #1
0
        public ActionResult SaveInvoice(string invoiceType, string invoiceDate, string conversionRate, string remarks, long customerId, long customerSiteId, long periodId, long currencyId)
        {
            string message = "";

            try
            {
                bool saved = false;
                if (SessionHelper.Invoice != null)
                {
                    if (SessionHelper.Invoice.InvoiceDetail.Count == 0)
                    {
                        message = "No detail information available to save!";
                    }
                    else
                    {
                        SessionHelper.Invoice.InvoiceType    = invoiceType;
                        SessionHelper.Invoice.InvoiceDate    = Convert.ToDateTime(invoiceDate);
                        SessionHelper.Invoice.ConversionRate = Convert.ToDecimal(conversionRate);
                        SessionHelper.Invoice.Remarks        = remarks;
                        SessionHelper.Invoice.CustomerId     = customerId;
                        SessionHelper.Invoice.CustomerSiteId = customerSiteId;
                        SessionHelper.Invoice.PeriodId       = periodId;
                        SessionHelper.Invoice.CurrencyId     = currencyId;

                        if (SessionHelper.Invoice.InvoiceDate >= SessionHelper.Calendar.StartDate && SessionHelper.Invoice.InvoiceDate <= SessionHelper.Calendar.EndDate)
                        {
                            if (SessionHelper.Invoice.InvoiceNo == "New")
                            {
                                SessionHelper.Invoice.InvoiceNo = InvoiceHelper.GetInvoiceNo(AuthenticationHelper.CompanyId.Value, SessionHelper.Invoice.SOBId, SessionHelper.Invoice.PeriodId, SessionHelper.Invoice.CurrencyId);
                            }

                            foreach (InvoiceDetailModel invoiceDetail in SessionHelper.Invoice.InvoiceDetail)
                            {
                                invoiceDetail.Amount = invoiceDetail.Quantity * invoiceDetail.Rate;

                                if (invoiceDetail.TaxId != null)
                                {
                                    TaxDetailModel taxDetail = TaxHelper.GetTaxDetail(invoiceDetail.TaxId.ToString()).FirstOrDefault(x => x.StartDate <= SessionHelper.Invoice.InvoiceDate && x.EndDate >= SessionHelper.Invoice.InvoiceDate);

                                    if (taxDetail != null)
                                    {
                                        invoiceDetail.TaxAmount = invoiceDetail.Amount * taxDetail.Rate / 100;
                                    }
                                    else
                                    {
                                        invoiceDetail.TaxAmount = 0;
                                    }
                                }
                            }

                            InvoiceHelper.Update(SessionHelper.Invoice);
                            SessionHelper.Invoice = null;
                            saved   = true;
                            message = "Saved successfully";
                        }
                        else
                        {
                            message = "GL Date must lie in the range of the selected period";
                        }
                    }
                }
                else
                {
                    message = "No information available to save!";
                }
                return(Json(new { success = saved, message = message }));
            }
            catch (Exception e)
            {
                message = e.Message;
                return(Json(new { success = false, message = message }));
            }
        }