/// <summary> /// Calculate call chareg /// </summary> /// <param name="customer">Customer who made the call</param> /// <param name="cdr">CDR record of the call to calculate the chareg</param> /// <param name="package">Package of the customer</param> /// <returns>Charge for the given call (using CDR)</returns> public decimal CalcluateCallCharge(Customer customer, CallDetailsRecord cdr, Package package) { var billingPeriodType = GetBillingPeriodType(cdr.StartTime, package); var callType = GetCallType(customer.PhoneNumber, cdr.RecievingPhoneNumber); var charge = package.CallCharges.Where(c => c.PeriodType == billingPeriodType && c.CallType == callType).First(); var paidDurationInSeconds = GetPaidCallDuration(customer, cdr, package); return(GetCharegesForTheCall(charge, paidDurationInSeconds)); }
/// <summary> /// Convert a CallDetailsRecord (CDR) object to CallDetails object (call details objects in mobile bills) /// </summary> /// <param name="cdr">CDR to convert</param> /// <param name="customer">Customer who owns the CDR</param> /// <param name="package">Customer Package</param> /// <returns></returns> private CallDetails GetCallDetailsFromCdr(CallDetailsRecord cdr, Customer customer, Package package) { var callDetails = new CallDetails(); callDetails.StartTime = cdr.StartTime; callDetails.DestinationNumber = cdr.RecievingPhoneNumber; callDetails.DurationInSeconds = cdr.DurationInSeconds; callDetails.Charge = package.CallChargeCalcluationStratergy.CalcluateCallCharge(customer, cdr, package); return(callDetails); }
/// <summary> /// Get paid call duration (i.e. total call duration in seconds - no of free seconds) /// </summary> /// <returns>Call duration that requires to be paied</returns> private int GetPaidCallDuration(Customer customer, CallDetailsRecord cdr, Package package) { var freeCallDuration = package.FreeCallDurations .Where(d => d.CallType == GetCallType(customer.PhoneNumber, cdr.OriginatingPhoneNumber) && d.PeriodType == GetBillingPeriodType(cdr.StartTime, package)) .FirstOrDefault()?.DurationInSeconds ?? 0; if (cdr.DurationInSeconds > freeCallDuration) { return(cdr.DurationInSeconds - freeCallDuration); } else { return(0); } }