Example #1
0
        public JsonResult SalesReturnEntry(LP_PI_ViewModel objPI)
        {
            try
            {
                bool    flag = false;
                decimal previousTotalAmount, previousNetAmount, previousPaidAmount, previousBalanceAmount, totaltaxPercent, netAmountAfterReturn, taxAmountAfterReturn, totalReturnAmountInclusiveTax, taxAmountOnReturn = 0.00m;
                if (objPI.idx > 0)
                {
                    // var BankList = Helper.ConvertDataTable<Company_Bank_Property>(GetCompanyBankById(objPI.bankIdx));

                    objPIProperty     = new LP_P_Invoice_Property();
                    objPIProperty.idx = Convert.ToInt32(objPI.idx);
                    objPIBLL          = new LP_PInvoice_BLL(objPIProperty);
                    var data        = objPIBLL.SelectByID();
                    var vendorIdx   = int.Parse(data.Tables[0].Rows[0]["VendorID"].ToString());
                    var VendorsData = Helper.ConvertDataTable <Vendors_Property>(GetVendorById(vendorIdx));

                    var list = objPI.InvoiceDetails;
                    previousNetAmount     = decimal.Parse(data.Tables[0].Rows[0]["NetAmount"].ToString());
                    previousTotalAmount   = decimal.Parse(data.Tables[0].Rows[0]["TotalAmount"].ToString());
                    previousPaidAmount    = decimal.Parse(data.Tables[0].Rows[0]["PaidAmount"].ToString());
                    previousBalanceAmount = decimal.Parse(data.Tables[0].Rows[0]["BalanceAmount"].ToString());
                    decimal totalReturnAmount = decimal.Parse(list.Sum(x => x.ReturnAmount).ToString());
                    netAmountAfterReturn          = previousNetAmount - totalReturnAmount;
                    totalReturnAmountInclusiveTax = totalReturnAmount;//if No tax

                    if (totalReturnAmount > 0)
                    {
                        LP_PIReturn_Property objReturn    = new LP_PIReturn_Property();
                        LP_PIReturn_BLL      objReturnBLL = new LP_PIReturn_BLL(objReturn);
                        objReturn.piIdx             = objPI.idx;
                        objReturn.invoiceNo         = data.Tables[0].Rows[0]["InvoiceNo"].ToString();
                        objReturn.returnAmount      = totalReturnAmount;
                        objReturn.createdBy         = Convert.ToInt16(Session["UID"].ToString());
                        objReturn.VendorID          = int.Parse(data.Tables[0].Rows[0]["VendorID"].ToString());
                        objReturn.DetailData        = Helper.ToDataTable <LP_P_Invoice_Details>(objPI.InvoiceDetails.Where(x => x.Qty > 0 && x.ReturnQty > 0).ToList());
                        objReturn.UpdatedDetailData = Helper.ToDataTable <LP_P_Invoice_Details>(objPI.UpdatedInvoiceDetails.ToList());
                        objReturn.coaVendorIdx      = VendorsData[0].coaIdx;

                        objReturn.PaidAmount   = previousPaidAmount;
                        objReturn.BalancAmount = previousBalanceAmount;
                        objReturn.NetAmount    = previousNetAmount;
                        objReturn.TotalAmount  = previousTotalAmount;
                        //Account and Tax Updation After Return
                        var taxData = data.Tables[2];
                        if (taxData.Rows.Count > 0)
                        {
                            object sumObject;
                            sumObject                     = taxData.Compute("Sum(taxPercent)", string.Empty);
                            totaltaxPercent               = decimal.Parse(sumObject.ToString());
                            taxAmountAfterReturn          = ((netAmountAfterReturn) / 100) * totaltaxPercent;
                            taxAmountOnReturn             = ((totalReturnAmount) / 100) * totaltaxPercent;//Return Ka Tax Amount Hai
                            objReturn.UpdatedTaxAmount    = taxAmountAfterReturn;
                            totalReturnAmountInclusiveTax = totalReturnAmount + taxAmountOnReturn;
                            objReturn.taxData             = taxData;
                        }

                        else
                        {
                            objReturn.UpdatedTaxAmount = 0.00m;
                        }
                        objReturn.returnAmountInclusiveTax = totalReturnAmountInclusiveTax;
                        if (totalReturnAmountInclusiveTax <= previousBalanceAmount)
                        {
                            objReturn.UpdatedTotalAmount = netAmountAfterReturn + objReturn.UpdatedTaxAmount;
                            objReturn.UpdatedNetAmount   = netAmountAfterReturn;
                            //objReturn.UpdatedBalanceAmount = previousBalanceAmount - totalReturnAmount;
                            objReturn.UpdatedBalanceAmount = previousBalanceAmount - totalReturnAmountInclusiveTax;
                            objReturn.UpdatedPaidAmount    = previousPaidAmount;
                            objReturn.amountTobeReceived   = 0.00m;
                            if (totalReturnAmount < previousBalanceAmount)
                            {
                                //only balance Amount will be updated means only payable will decrease only
                                //isPaid Check will remain zero

                                objReturn.isPaid = 0;
                            }
                            else
                            {
                                //only balance Amount will be updated means only payable will decrease only
                                //isPaid Check will remain one

                                objReturn.isPaid = 1;
                            }
                        }
                        else if (totalReturnAmountInclusiveTax > previousBalanceAmount)
                        {
                            if (totalReturnAmountInclusiveTax == previousTotalAmount)
                            {
                                objReturn.UpdatedTotalAmount   = 0;
                                objReturn.UpdatedNetAmount     = 0;
                                objReturn.UpdatedBalanceAmount = 0;
                                objReturn.UpdatedPaidAmount    = 0;
                                decimal Amount = previousBalanceAmount - totalReturnAmountInclusiveTax;
                                objReturn.UpdatedPaidAmount  = previousPaidAmount + Amount;
                                objReturn.amountTobeReceived = -(Amount);
                                objReturn.isPaid             = 1;
                            }
                            else
                            {
                                objReturn.UpdatedTotalAmount   = netAmountAfterReturn + objReturn.UpdatedTaxAmount;
                                objReturn.UpdatedNetAmount     = netAmountAfterReturn;
                                objReturn.UpdatedBalanceAmount = 0;
                                decimal Amount = previousBalanceAmount - totalReturnAmountInclusiveTax;
                                objReturn.UpdatedPaidAmount  = previousPaidAmount + Amount;
                                objReturn.amountTobeReceived = -(Amount);
                                objReturn.isPaid             = 1;
                            }
                        }
                        objReturnBLL.Insert();
                    }
                }
                else
                {
                    //add
                    objPIProperty               = new LP_P_Invoice_Property();
                    objPIProperty.InvoiceNo     = objPI.InvoiceNo;
                    objPIProperty.InvoiceType   = objPI.InvoiceType;
                    objPIProperty.IsPaid        = objPI.IsPaid;
                    objPIProperty.NetAmount     = objPI.NetAmount;
                    objPIProperty.PaidAmount    = objPI.PaidAmount;
                    objPIProperty.BalanceAmount = objPI.BalanceAmount;

                    objPIProperty.PaymentType     = objPI.PaymentType;
                    objPIProperty.Status          = objPI.Status;
                    objPIProperty.Taxable         = objPI.Taxable;
                    objPIProperty.TotalAmount     = objPI.TotalAmount;
                    objPIProperty.Description     = objPI.Description;
                    objPIProperty.CreatedBy       = Convert.ToInt16(Session["UID"].ToString());
                    objPIProperty.Description     = objPI.Description;
                    objPIProperty.Reference       = objPI.Reference;
                    objPIProperty.BankId          = objPI.bankIdx;
                    objPIProperty.AccountChequeNo = objPI.accorChequeNumber;
                    objPIProperty.CreatedDate     = DateTime.Now;
                    objPIProperty.Status          = 0;
                    if (objPI.Description == null)
                    {
                        objPIProperty.Description = "";
                    }
                    else
                    {
                        objPIProperty.Description = objPI.Description;
                    }
                    if (objPI.PITAXLIST.Count() > 0)
                    {
                        objPIProperty.Taxable = true;
                    }
                    else
                    {
                        objPIProperty.Taxable = false;
                    }
                    //objPIProperty.Taxable= ((objPI.TaxesList.Count()>0)==true?true:false);
                    objPIProperty.VendorID    = objPI.VendorID;
                    objPIProperty.ParentDocID = objPI.ParentDocID;
                    //tax
                    objPIProperty.TaxData        = Helper.ToDataTable <LP_PI_Taxes_Property>(objPI.PITAXLIST);
                    objPIProperty.InvoiceDetails = Helper.ToDataTable <LP_P_Invoice_Details>(objPI.InvoiceDetails);

                    objPIBLL = new LP_PInvoice_BLL(objPIProperty);
                    flag     = objPIBLL.Insert();
                }
                return(Json(new { data = "", success = flag, msg = flag == true ? "Successfull" : "Failed", statuscode = flag == true ? 200 : 401 }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new { data = ex.InnerException, success = false, statuscode = 400, count = 0 }, JsonRequestBehavior.AllowGet));
            }
        }
 public LP_PIReturn_DAL(LP_PIReturn_Property objMRMasterProperty)
 {
     _objMRMasterProperty = objMRMasterProperty;
 }