public void lineSumTest() { var l = new LineSum(); Assert.AreEqual(7, l.lineSum(8)); Assert.AreEqual(6, l.lineSum(0)); Assert.AreEqual(2, l.lineSum(1)); Assert.AreEqual(15, l.lineSum(1344)); Assert.AreEqual(19, l.lineSum(2001)); Assert.AreEqual(22, l.lineSum(2002)); Assert.AreEqual(16, l.lineSum(2017)); Assert.AreEqual(92, l.lineSum(1000000000000000)); }
private static void fillInFromEnvelopeLines(FFDataSet dataSet, LineSum lineSum, List <EnvBal> envelopes, decimal skipAmount) { decimal remainingAmount = lineSum.RemainingAmount - skipAmount; while (remainingAmount > 0.0m) { FFDataSet.EnvelopeLineRow envLine = null; EnvBal envelope = getNegativeEnvelope(envelopes); if (envelope == null) { break; } // Make a new envelope line for this envelope envLine = dataSet.EnvelopeLine.NewEnvelopeLineRow(); envLine.lineItemID = lineSum.Line.id; envLine.envelopeID = envelope.envelopeID; envLine.description = ""; envLine.amount = 0.0m; dataSet.EnvelopeLine.AddEnvelopeLineRow(envLine); // decide how much to change the envelopeLine.amount if (remainingAmount >= +Math.Abs(envelope.balance)) { envLine.amount = Math.Abs(envelope.balance); remainingAmount -= Math.Abs(envelope.balance); envelope.balance = 0.0m; } else { envLine.amount = remainingAmount; envelope.balance += remainingAmount; remainingAmount = 0.0m; } } }
static public void Distribute(int accountID) { List <EnvBal> envelopes = new List <EnvBal>(); Queue <LineSum> holdLines = new Queue <LineSum>(); decimal skipAmount = 0.0m; FFDataSet dataSet = new FFDataSet(); dataSet.myInit(); dataSet.myFillForAutoDistribute(); for (int index = 0; index < dataSet.LineItem.Count; index++) { FFDataSet.LineItemRow line = dataSet.LineItem[index]; if (line.accountID != accountID) { continue; } FFDataSet.EnvelopeLineRow[] envLines = line.GetEnvelopeLineRows(); decimal envSum = envLineSum(envLines); // Decide what to do with this line if (envSum == line.amount) { updateEnvelopes(line.creditDebit, envLines, envelopes); } else if (copyFromOppLines(dataSet, line, envSum)) { envLines = line.GetEnvelopeLineRows(); updateEnvelopes(line.creditDebit, envLines, envelopes); if (envLines.Length > 1) { line.envelopeID = SpclEnvelope.SPLIT; } else if (envLines.Length == 1) { line.envelopeID = envLines[0].envelopeID; } } else if (line.creditDebit == LineCD.CREDIT && envLines.Length > 0) { updateEnvelopes(line.creditDebit, envLines, envelopes); skipAmount += line.amount - envSum; } else if (line.creditDebit == LineCD.CREDIT && envLines.Length == 0) { skipAmount += line.amount; } else if (line.creditDebit == LineCD.DEBIT) { if (envLines.Length > 0) { updateEnvelopes(line.creditDebit, envLines, envelopes); } holdLines.Enqueue(new LineSum(line, line.amount - envSum)); } // See if there are enough negative envelopes and skip amounts to disribute the // next line being held. while (holdLines.Count > 0 && holdLines.Peek().RemainingAmount <= skipAmount) { LineSum lineSum = holdLines.Dequeue(); skipAmount -= lineSum.RemainingAmount; } while (holdLines.Count > 0 && holdLines.Peek().RemainingAmount <= skipAmount + getNegativeSum(envelopes)) { LineSum lineSum = holdLines.Dequeue(); fillInFromEnvelopeLines(dataSet, lineSum, envelopes, skipAmount); skipAmount = 0.0m; envLines = lineSum.Line.GetEnvelopeLineRows(); if (envLines.Length > 1) { lineSum.Line.envelopeID = SpclEnvelope.SPLIT; } else if (envLines.Length == 1) { lineSum.Line.envelopeID = envLines[0].envelopeID; } } } dataSet.mySaveData(); dataSet.Clear(); envelopes.Clear(); holdLines.Clear(); }