예제 #1
0
        //todo break this method into small methods
        public bool GenerateBill(int tenantId, int pricePerUnit, int currentMeterReading)
        {
            using (var scope = new TransactionScope())
            {
                var meterReadingRepository = new TenantMeterReadingRepository();
                int prevMonthMeterReading = meterReadingRepository.GetPreviousMonthMeterReading(tenantId);

                //create object of meter reading class as per current meter reading
                var tenantMeterReading = new TenantMeterReading
                    {
                        TenantId = tenantId,
                        MeterReading = currentMeterReading,
                        CreatedDate = DateTime.Now
                    };

                //calculate amount payable for current month
                int currentMonthUnitConsumed = tenantMeterReading.MeterReading - prevMonthMeterReading;
                double amountPayable = currentMonthUnitConsumed*pricePerUnit;
                // create object of bill payable class
                var billPayable = new BillPayable
                    {
                        TenantId = tenantId,
                        PricePerUnit = pricePerUnit,
                        UnitConsumed = currentMonthUnitConsumed,
                        AmountPayable = amountPayable,
                        CreatedDate = DateTime.Now
                    };

                //add amount payable class object
                var billPayableRepository = new BillPayableRepository();
                var billPaymentRepository = new BillPaymentRepository();

                meterReadingRepository.Add(tenantMeterReading);
                billPayableRepository.Add(billPayable);

                var lastBillPaid = billPaymentRepository.GetLastBillPaid(tenantId); //todo need both last bill paid amount and date

                var billInvoice = new BillInvoice
                    {
                        TenantId = tenantId,
                        CurrentMonthPayamentAmount = billPayable.AmountPayable,
                        LastBillPaid = (lastBillPaid == null) ? 0.00 : lastBillPaid.AmountPaid,
                        PendingAmount = 0,
                        LastBillPaidDate = DateTime.Now, //todo this will be changed
                        CreatedDate = DateTime.Now
                    };

                var billInvoiceRepository = new BillInvoiceRepository();
                billInvoiceRepository.Add(billInvoice);

                scope.Complete();
            }
            return true;
        }
예제 #2
0
        public bool Add(BillInvoice billInvoice)
        {
            using (var connection = new SqlConnection(Utility.ConnectionString))
            {
                var cmd =
                    new SqlCommand(
                        "INSERT INTO BillInvoice (tenantId, curentMonthPayableAmount, PendingAmount, LastBillPaidAmount, LastBillPaidDate, createdDate) VALUES (@tenantId, @currentMonthPayableAmount, @PendingAmount, @LastBillPaidAmount, @LastBillPaidDate, @createdDate)")
                        {
                            CommandType = CommandType.Text,
                            Connection = connection
                        };
                cmd.Parameters.AddWithValue("@tenantId", billInvoice.TenantId);
                cmd.Parameters.AddWithValue("@currentMonthPayableAmount", billInvoice.CurrentMonthPayamentAmount);
                cmd.Parameters.AddWithValue("@PendingAmount", billInvoice.PendingAmount);
                cmd.Parameters.AddWithValue("@LastBillPaidAmount", billInvoice.LastBillPaid);
                cmd.Parameters.AddWithValue("@LastBillPaidDate", billInvoice.LastBillPaidDate);
                cmd.Parameters.AddWithValue("@createdDate", billInvoice.CreatedDate);
                connection.Open();
                cmd.ExecuteNonQuery();
            }
            return true;

            return true;
        }