[HttpPost]// POST api/values
        public string Post()
        {
            DanpheHTTPResponse <object> responseData = new DanpheHTTPResponse <object>();
            int CreatedBy = ToInt(this.ReadQueryStringData("CreatedBy"));

            responseData.Status = "OK";//by default status would be OK, hence assigning at the top
            string ipDataString = this.ReadPostData();
            string reqType      = this.ReadQueryStringData("reqType");

            try
            {
                BillingDbContext billingDbContext = new BillingDbContext(connString);
                if (reqType == "postSettlementInvoice")//submit
                {
                    BillSettlementModel settlement = DanpheJSONConvert.DeserializeObject <BillSettlementModel>(ipDataString);
                    //List<BillingTransactionItemModel> billTxnItm = DanpheJSONConvert.DeserializeObject<List<BillingTransactionItemModel>>(ipDataString);
                    RbacUser currentUser = HttpContext.Session.Get <RbacUser>("currentuser");

                    using (var dbTransaction = billingDbContext.Database.BeginTransaction())
                    {
                        try
                        {
                            var txns = settlement.BillingTransactions;

                            //step:0, As EF automatically Inserts Child collection (billingtransactionmodel) while inserting settlement
                            // we have to first create a new list and set settlement.BillingTransactions as null.
                            List <BillingTransactionModel> newTxnList = new List <BillingTransactionModel>();
                            foreach (BillingTransactionModel txn in txns)
                            {
                                BillingTransactionModel newTxn = BillingTransactionModel.GetCloneWithItems(txn);
                                newTxnList.Add(newTxn);
                            }
                            settlement.BillingTransactions = null;

                            //Step1: assign server side values to the input model and Save the settlementModel
                            settlement.SettlementReceiptNo = GetSettlementReceiptNo(billingDbContext);
                            settlement.CreatedOn           = System.DateTime.Now;
                            settlement.SettlementDate      = System.DateTime.Now;
                            settlement.FiscalYearId        = BillingBL.GetFiscalYear(billingDbContext).FiscalYearId;
                            settlement.CreatedBy           = currentUser.EmployeeId;

                            billingDbContext.BillSettlements.Add(settlement);
                            billingDbContext.SaveChanges();
                            if (newTxnList != null && newTxnList.Count > 0)
                            {
                                //step2: Update necessary fields of BillingTransaction acc to above Settlement Object
                                foreach (var txn in newTxnList)
                                {
                                    billingDbContext.BillingTransactions.Attach(txn);
                                    txn.SettlementId      = settlement.SettlementId;
                                    txn.BillStatus        = "paid";
                                    txn.PaidAmount        = txn.TotalAmount;
                                    txn.PaidDate          = settlement.SettlementDate;
                                    txn.PaymentReceivedBy = currentUser.EmployeeId; //added: sud: 29may'18
                                    txn.PaidCounterId     = settlement.CounterId;   //added: sud: 29may'18

                                    billingDbContext.Entry(txn).Property(b => b.BillStatus).IsModified        = true;
                                    billingDbContext.Entry(txn).Property(b => b.SettlementId).IsModified      = true;
                                    billingDbContext.Entry(txn).Property(b => b.PaidAmount).IsModified        = true;
                                    billingDbContext.Entry(txn).Property(b => b.PaidDate).IsModified          = true;
                                    billingDbContext.Entry(txn).Property(b => b.PaymentReceivedBy).IsModified = true; //added: sud: 29may'18
                                    billingDbContext.Entry(txn).Property(b => b.PaidCounterId).IsModified     = true; //added: sud: 29may'18

                                    //setp3: Update BillStatus and PaidDate of each transaction items attached with above transactions

                                    List <BillingTransactionItemModel> txnItems = billingDbContext.BillingTransactionItems
                                                                                  .Where(b => b.BillingTransactionId == txn.BillingTransactionId).ToList();

                                    if (txnItems != null && txnItems.Count > 0)
                                    {
                                        for (int i = 0; i < txnItems.Count; i++)
                                        {
                                            txnItems[i] = BillingTransactionBL.UpdateTxnItemBillStatus(billingDbContext,
                                                                                                       txnItems[i],
                                                                                                       "paid",
                                                                                                       currentUser.EmployeeId,
                                                                                                       settlement.SettlementDate,
                                                                                                       settlement.CounterId);
                                        }
                                        billingDbContext.SaveChanges();
                                    }
                                }
                                billingDbContext.SaveChanges();
                            }

                            //step: 4 Add new row to deposit table if Deposit is deducted
                            if (settlement.DepositDeducted != null && settlement.DepositDeducted > 0)
                            {
                                VisitModel patientVisit = billingDbContext.Visit.Where(visit => visit.PatientId == settlement.PatientId)
                                                          .OrderByDescending(a => a.PatientVisitId)
                                                          .FirstOrDefault();
                                BillingDeposit depositModel = new BillingDeposit()
                                {
                                    Amount         = settlement.DepositDeducted,
                                    DepositType    = "depositdeduct",
                                    IsActive       = true,
                                    FiscalYearId   = BillingBL.GetFiscalYear(billingDbContext).FiscalYearId,
                                    Remarks        = "Deposit used in Settlement Receipt No. SR" + settlement.SettlementReceiptNo + " on " + settlement.SettlementDate,
                                    CreatedBy      = currentUser.EmployeeId,
                                    CreatedOn      = DateTime.Now,
                                    CounterId      = settlement.CounterId,
                                    PatientVisitId = patientVisit != null ? (int?)patientVisit.PatientVisitId : null,
                                    SettlementId   = settlement.SettlementId,
                                    PatientId      = settlement.PatientId,
                                    DepositBalance = 0,
                                    ReceiptNo      = BillingBL.GetDepositReceiptNo(connString),
                                    PaymentMode    = "cash",//yubraj 4th July '19
                                };

                                billingDbContext.BillingDeposits.Add(depositModel);
                                billingDbContext.SaveChanges();

                                ////update iscurrent and isactive in deposit table for settlement
                                //List<BillingDeposit> depositDetail= (from dep in billingDbContext.BillingDeposits
                                //                                   where dep.PatientId==settlement.PatientId  &&
                                //                                   dep.IsCurrent==true &&
                                //                                   dep.IsActive==true
                                //                                   select dep).ToList();

                                //if (depositDetail != null)
                                //{
                                //    depositDetail.ForEach(d =>
                                //    {
                                //        //d.IsActive = false;
                                //        d.IsCurrent = false;
                                //        billingDbContext.SaveChanges();
                                //    });
                                //}
                            }

                            dbTransaction.Commit();

                            responseData.Status  = "OK";
                            responseData.Results = settlement;
                        }
                        catch (Exception ex)
                        {
                            dbTransaction.Rollback();
                            responseData.Status       = "Failed";
                            responseData.ErrorMessage = ex.ToString();
                        }
                    }
                }
                //responseData.Results = null;
            }
            catch (Exception ex)
            {
                responseData.Status       = "Failed";
                responseData.ErrorMessage = ex.Message + " exception details:" + ex.ToString();
            }
            return(DanpheJSONConvert.SerializeObject(responseData, true));
        }
        [HttpPost]// POST api/values
        public string Post(string reqType)
        {
            DanpheHTTPResponse <object> responseData = new DanpheHTTPResponse <object>();
            string ipDataString = this.ReadPostData();

            responseData.Status = "OK";//by default status would be OK, hence assigning at the top
            try
            {
                BillingDbContext billingDbContext = new BillingDbContext(connString);
                MasterDbContext  masterDbContext  = new MasterDbContext(connString);
                RbacUser         currentUser      = HttpContext.Session.Get <RbacUser>("currentuser");
                if (reqType == "returnInvoice")//submit
                {
                    //var currentDate = DateTime.Now;
                    BillInvoiceReturnModel billInvoiceRet = DanpheJSONConvert.DeserializeObject <BillInvoiceReturnModel>(ipDataString);
                    if (billInvoiceRet != null)
                    {
                        //Transaction Begins
                        using (var dbContextTransaction = billingDbContext.Database.BeginTransaction())
                        {
                            try
                            {
                                BillingFiscalYear currFiscYear = BillingBL.GetFiscalYear(billingDbContext);
                                //credit note number will continue from current fiscal year, regardless whether the bill was generated in earlier fiscal year.. (sud:30Aug'18)
                                int?maxCreditNoteNum = billingDbContext.BillReturns.Where(a => a.FiscalYearId == currFiscYear.FiscalYearId).Max(a => a.CreditNoteNumber);
                                billInvoiceRet.CreatedOn = DateTime.Now;
                                if (maxCreditNoteNum == null || !maxCreditNoteNum.HasValue)
                                {
                                    maxCreditNoteNum = 0;
                                }

                                billInvoiceRet.FiscalYear       = currFiscYear.FiscalYearFormatted;
                                billInvoiceRet.FiscalYearId     = currFiscYear.FiscalYearId;
                                billInvoiceRet.CreditNoteNumber = (int?)(maxCreditNoteNum + 1);
                                billInvoiceRet.CreatedBy        = currentUser.EmployeeId;
                                billingDbContext.BillReturns.Add(billInvoiceRet);
                                billingDbContext.SaveChanges();

                                //update transactiontable after bill is returned..
                                int invoiceNo = billInvoiceRet.RefInvoiceNum;
                                //BillingTransactionModel billTxn = billingDbContext.BillingTransactions
                                //  .Where(b => b.InvoiceNo == billInvoiceRet.RefInvoiceNum)
                                //  .FirstOrDefault();
                                //changed: sud: 18July, since Invoice No will repeat with fiscal year, so need to find by billingtransactionid.
                                BillingTransactionModel billTxn = billingDbContext.BillingTransactions
                                                                  .Where(b => b.BillingTransactionId == billInvoiceRet.BillingTransactionId)
                                                                  .FirstOrDefault();
                                billingDbContext.BillingTransactions.Attach(billTxn);
                                billTxn.ReturnStatus = true;
                                billingDbContext.Entry(billTxn).Property(a => a.ReturnStatus).IsModified = true;
                                billingDbContext.SaveChanges();

                                if (billTxn.IsInsuranceBilling == true)
                                {
                                    double deductableInsuranceAmount = (billTxn.TotalAmount ?? default(double)) * -1;
                                    BillingBL.UpdateInsuranceCurrentBalance(connString,
                                                                            billTxn.PatientId,
                                                                            billTxn.InsuranceProviderId ?? default(int),
                                                                            currentUser.EmployeeId, deductableInsuranceAmount, true);
                                }

                                var invoiceItems = billingDbContext.BillingTransactionItems.Where(b => b.BillingTransactionId == billInvoiceRet.BillingTransactionId).ToList();
                                //replaced calling centralized function in BillingBL
                                for (int i = 0; i < invoiceItems.Count; i++)
                                {
                                    invoiceItems[i] = BillingTransactionBL.UpdateTxnItemBillStatus(billingDbContext,
                                                                                                   invoiceItems[i],
                                                                                                   "returned",
                                                                                                   currentUser.EmployeeId);
                                }

                                //Yubraj: 18th Dec '18 :: Updating IsActive in deposit table while invoice return
                                List <BillingDeposit> deposit = (from dpt in billingDbContext.BillingDeposits
                                                                 where dpt.BillingTransactionId == billInvoiceRet.BillingTransactionId &&
                                                                 (dpt.DepositType == "depositdeduct" || dpt.DepositType == "ReturnDeposit")
                                                                 select dpt).ToList();
                                if (deposit != null)
                                {
                                    deposit.ForEach(a =>
                                    {
                                        a.IsActive        = true; //keeping false was affecting the patient deposit info
                                        a.ModifiedRemarks = "Updated after invoice return of BillTxnId: " + billInvoiceRet.BillingTransactionId.ToString();
                                        a.ModifiedOn      = DateTime.Now;
                                        a.ModifiedBy      = currentUser.EmployeeId;
                                    });
                                }
                                billInvoiceRet.ReturnedItems = invoiceItems.ToList();
                                billingDbContext.SaveChanges();

                                dbContextTransaction.Commit(); //end of transaction
                                responseData.Results = billInvoiceRet;
                            }
                            catch (Exception ex)
                            {
                                dbContextTransaction.Rollback();
                                throw ex;
                            }
                        }
                    }
                    else
                    {
                        responseData.Status       = "Failed";
                        responseData.ErrorMessage = "billTransactionitems is invalid";
                    }
                }
            }
            catch (Exception ex)
            {
                responseData.Status       = "Failed";
                responseData.ErrorMessage = ex.Message + " exception details:" + ex.ToString();
            }
            return(DanpheJSONConvert.SerializeObject(responseData, true));
        }
예제 #3
0
        [HttpPost]// POST api/values
        public string Post()
        {
            DanpheHTTPResponse <object> responseData = new DanpheHTTPResponse <object>(); //type 'object' since we have variable return types

            responseData.Status = "OK";                                                   //by default status would be OK, hence assigning at the top
            try
            {
                string           ipDataString     = this.ReadPostData();
                string           reqType          = this.ReadQueryStringData("reqType");
                RbacUser         currentUser      = HttpContext.Session.Get <RbacUser>("currentuser");
                BillingDbContext billingDbContext = new BillingDbContext(connString);
                if (reqType == "postBillTransaction")//submit
                {
                    DateTime currentDate = DateTime.Now;
                    BillingTransactionModel billTransaction = DanpheJSONConvert.DeserializeObject <BillingTransactionModel>(ipDataString);

                    if (billTransaction != null)
                    {
                        //Transaction Begins
                        using (var dbContextTransaction = billingDbContext.Database.BeginTransaction())
                        {
                            try
                            {
                                //step:1 -- make copy of billingTxnItems into new list, so thate EF doesn't add txn items again.
                                //step:2-- if there's deposit deduction, then add to deposit table.
                                billTransaction = BillingTransactionBL.PostBillingTransaction(billingDbContext, connString, billTransaction, currentUser.EmployeeId, currentDate);

                                //step:3-- if there's deposit balance, then add a return transaction to deposit table.
                                if (billTransaction.PaymentMode != "credit" && billTransaction.DepositBalance != null && billTransaction.DepositBalance > 0)
                                {
                                    BillingDeposit dep = new BillingDeposit()
                                    {
                                        DepositType = "ReturnDeposit",
                                        Remarks     = "Deposit Refunded from InvoiceNo. " + billTransaction.InvoiceCode + billTransaction.InvoiceNo,
                                        //Remarks = "ReturnDeposit" + " for transactionid:" + billTransaction.BillingTransactionId,
                                        Amount               = billTransaction.DepositBalance,
                                        IsActive             = true,
                                        BillingTransactionId = billTransaction.BillingTransactionId,
                                        DepositBalance       = 0,
                                        FiscalYearId         = billTransaction.FiscalYearId,
                                        CounterId            = billTransaction.CounterId,
                                        CreatedBy            = billTransaction.CreatedBy,
                                        CreatedOn            = currentDate,
                                        PatientId            = billTransaction.PatientId,
                                        PatientVisitId       = billTransaction.PatientVisitId,
                                        PaymentMode          = billTransaction.PaymentMode,
                                        PaymentDetails       = billTransaction.PaymentDetails,
                                        ReceiptNo            = billTransaction.ReceiptNo
                                    };

                                    billingDbContext.BillingDeposits.Add(dep);
                                    billingDbContext.SaveChanges();
                                }

                                //For cancel BillingTransactionItems
                                List <BillingTransactionItemModel> item = (from itm in billingDbContext.BillingTransactionItems
                                                                           where itm.PatientId == billTransaction.PatientId && itm.PatientVisitId == billTransaction.PatientVisitId && itm.BillStatus == "provisional" && itm.Quantity == 0
                                                                           select itm).ToList();
                                if (item.Count() > 0)
                                {
                                    item.ForEach(itm =>
                                    {
                                        var txnItem = BillingTransactionBL.UpdateTxnItemBillStatus(billingDbContext, itm, "adtCancel", billTransaction.CreatedBy.Value, currentDate, billTransaction.CounterId, null);
                                    });
                                }


                                dbContextTransaction.Commit();
                                responseData.Results = billTransaction;
                            }
                            catch (Exception ex)
                            {
                                //rollback all changes if any error occurs
                                dbContextTransaction.Rollback();
                                throw ex;
                            }
                        }
                    }
                    else
                    {
                        responseData.Status       = "Failed";
                        responseData.ErrorMessage = "billTransaction is invalid";
                    }
                }
            }
            catch (Exception ex)
            {
                responseData.Status       = "Failed";
                responseData.ErrorMessage = ex.Message + " exception details:" + ex.ToString();
            }
            return(DanpheJSONConvert.SerializeObject(responseData, true));
        }