private static void CalcProfitValue(TranTotals total, TranProfit profit)
 {
     if (total.FinType == "FeesInDischargePortNet")
     {
         profit.TotalPaid += total.FinTotal;
     }
     else if (total.FinType == "FeesInDischargePortSelling")
     {
         profit.TotalEarned += total.FinTotal;
     }
     else if (total.FinType == "FeesInOriginNet")
     {
         profit.TotalPaid += total.FinTotal;
     }
     else if (total.FinType == "FeesInOriginSelling")
     {
         profit.TotalEarned += total.FinTotal;
     }
     else if (total.FinType == "TranChargesNet")
     {
         profit.TotalPaid += total.FinTotal;
     }
     else if (total.FinType == "TranChargesSelling")
     {
         profit.TotalEarned += total.FinTotal;
     }
     else if (total.FinType == "LineShippingNet")
     {
         profit.TotalPaid += total.FinTotal;
     }
     else if (total.FinType == "LineShippingSelling")
     {
         profit.TotalEarned += total.FinTotal;
     }
 }
        private static void CalcProfit(int TranID)
        {
            List <TranTotals> listOfTranTotals;

            using (FFAdminDBEntities db = new Models.FFAdminDBEntities())
            {
                listOfTranTotals = db.TranTotals.Where(y => y.TranID == TranID).ToList();

                if (listOfTranTotals == null || listOfTranTotals.Count == 0)
                {
                    return;
                }
                else
                {
                    // delete all profit and recalculate them again
                    db.TranProfit.RemoveRange(
                        db.TranProfit.Where(s => s.TranID == TranID)
                        );
                    db.SaveChanges();

                    // add new profits
                    foreach (var x in listOfTranTotals)
                    {
                        TranProfit p = db.TranProfit.FirstOrDefault(c => c.TranID == TranID && c.CurrencyID == x.CurrencyID);
                        if (p == null)
                        {
                            p = db.TranProfit.Add(
                                new TranProfit()
                            {
                                CurrencyID = x.CurrencyID,
                                TranID     = x.TranID
                            });
                            CalcProfitValue(x, p);
                            db.SaveChanges();
                            continue;
                        }
                        CalcProfitValue(x, p);
                    }
                }

                //save
                db.SaveChanges();
            }
        }