Example #1
0
        internal static ConcessionLetterVm GetDeliveryNoteInfo(int hbId, string langCode)
        {
            ConcessionLetterVm concLetterVm = new ConcessionLetterVm();
            OperationsEntities db           = new OperationsEntities();
            var hbView = db.HouseBillViews.Where(x => x.HouseBillId == hbId).Select(x => new
            {
                x.CarrierNameEn,
                x.FromPort,
                x.MBL,
                x.GrossWeight,
                x.CBM,
                x.ConsigneeNameEn,
                x.ToPort,
                x.CarrierType,
                x.NumberOfPackages,
                x.OperationId,
                x.VesselName,
                x.ArriveDate,
                x.HouseBL,
                x.GoodsDescription
            }).FirstOrDefault();

            int operId = hbView.OperationId;

            concLetterVm.CarrierName      = hbView.CarrierNameEn;
            concLetterVm.FromPort         = hbView.FromPort;
            concLetterVm.MBL              = hbView.MBL;
            concLetterVm.GrossWeight      = hbView.GrossWeight.Value;
            concLetterVm.CBM              = hbView.CBM.Value;
            concLetterVm.ConsigneeName    = hbView.ConsigneeNameEn;
            concLetterVm.Containers       = OperationHelper.GetContainersSummary(operId);
            concLetterVm.NumberOfPackages = hbView.NumberOfPackages.Value;
            concLetterVm.ToPort           = hbView.ToPort;
            concLetterVm.VesselName       = hbView.VesselName;
            concLetterVm.ArriveDate       = hbView.ArriveDate;
            concLetterVm.HouseBL          = hbView.HouseBL;
            concLetterVm.GoodsDescription = hbView.GoodsDescription;

            var containerList = db.OperationContainers.Where(x => x.OperationId == operId).Select(x => x.ContainerNumber).ToArray();

            concLetterVm.ContainerNumber = string.Join(" - ", containerList);

            concLetterVm.StaticLabels = CommonHelper.GetStaticLabels((int)StaticTextForScreenEnum.DeliveryNote, langCode);

            return(concLetterVm);
        }
Example #2
0
        public static TruckingOrderDetailVm GetTruckingOrderDetailInfo(int truckingOrderId)
        {
            TruckingOrderDetailVm trkoVmObj = new TruckingOrderDetailVm(truckingOrderId);
            EasyFreightEntities   db        = new EasyFreightEntities();

            int truckDetailCount = db.TruckingOrderDetails.Where(x => x.TruckingOrderId == truckingOrderId).Count();

            OperationsEntities db1 = new OperationsEntities();
            var truckViewObj       = db1.TruckingOrdersViews.Where(x => x.TruckingOrderId == truckingOrderId).FirstOrDefault();

            Mapper.CreateMap <TruckingOrdersView, TruckingOrderDetailVm>().IgnoreAllNonExisting();

            Mapper.Map(truckViewObj, trkoVmObj);

            trkoVmObj.ClientName        = truckViewObj.OrderFrom == 1 ? truckViewObj.ShipperNameEn : truckViewObj.ConsigneeNameEn;
            trkoVmObj.ContainersSummary = OperationHelper.GetContainersSummary(trkoVmObj.OperationId);

            List <TruckingOrderCost> truckCostList = db.TruckingOrderCosts.Where(x => x.TruckingOrderId == truckingOrderId).ToList();
            TruckingCostVm           truckingCostObj;

            if (truckCostList.Count() == 0)
            {
                truckingCostObj = new TruckingCostVm(trkoVmObj.TruckingOrderId);
                trkoVmObj.TruckingOrderCosts.Add(truckingCostObj);
            }
            else
            {
                Mapper.CreateMap <TruckingOrderCost, TruckingCostVm>().IgnoreAllNonExisting();
                trkoVmObj.TotalCostNet     = Math.Round(truckCostList.Sum(x => x.TruckingCostNet), 2);
                trkoVmObj.TotalCostSelling = Math.Round(truckCostList.Sum(x => x.TruckingCostSelling), 2);
                foreach (var cost in truckCostList)
                {
                    truckingCostObj = new TruckingCostVm();
                    Mapper.Map(cost, truckingCostObj);
                    truckingCostObj.TruckingCostSelling = Math.Round(truckingCostObj.TruckingCostSelling.Value, 2);
                    truckingCostObj.TruckingCostNet     = Math.Round(truckingCostObj.TruckingCostNet.Value, 2);
                    truckingCostObj.TruckingCostName    = cost.TruckingCostLib.TruckingCostName;
                    truckingCostObj.CurrencySign        = cost.Currency.CurrencySign;

                    trkoVmObj.TruckingOrderCosts.Add(truckingCostObj);
                }
            }

            List <OperationContainerVm> operationContainerList = OperationHelper.GetOperationContainers(trkoVmObj.OperationId);

            if (operationContainerList.Count() > 0)
            {
                //kamal
                int truckHouseId = db.TruckingOrders.Where(x => x.TruckingOrderId == truckingOrderId).Select(a => a.HouseBillId.Value).FirstOrDefault();
                List <HouseContainerVm> hcVmList = HouseBillHelper.GetHousContainerByHouseID(truckHouseId);
                if (hcVmList.Count > 0)
                {
                    foreach (var opcont in operationContainerList)
                    {
                        foreach (var item in hcVmList)
                        {
                            if (item.OperConId == opcont.OperConId)
                            {
                                trkoVmObj.OperationContainers.Add(opcont);
                            }
                        }
                    }
                }
                else
                {
                    foreach (var opcont in operationContainerList)
                    {
                        trkoVmObj.OperationContainers.Add(opcont);
                    }
                }
            }

            trkoVmObj.HbDate = db1.HouseBills
                               .Where(x => x.HouseBillId == truckViewObj.HouseBillId).FirstOrDefault().OperationDate.Value;

            //Get code generated if first insert
            if (truckDetailCount == 0)
            {
                trkoVmObj.TruckingOrderCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.TruckingOrder, false);
            }

            return(trkoVmObj);
        }
Example #3
0
        internal static string AddEditAgentNote(AgentNoteVm agentNoteVm)
        {
            string             isSaved = "true";
            AccountingEntities db      = new AccountingEntities();
            AgentNote          agNoteDb;
            int agNoteId = agentNoteVm.AgentNoteId;

            if (agNoteId == 0)
            {
                agNoteDb = new AgentNote();
            }
            else
            {
                agNoteDb = db.AgentNotes.Where(x => x.AgentNoteId == agNoteId).FirstOrDefault();
            }

            Mapper.CreateMap <AgentNoteVm, AgentNote>()
            .ForMember(x => x.AgentNoteDetails, y => y.Ignore())
            .IgnoreAllNonExisting();

            Mapper.Map(agentNoteVm, agNoteDb);

            AgentNoteDetail agNoteDetail;

            Mapper.CreateMap <AgentNoteDetailVm, AgentNoteDetail>().IgnoreAllNonExisting();

            foreach (var item in agentNoteVm.AgentNoteDetails)
            {
                if (item.IsSelected)
                {
                    agNoteDetail = new AgentNoteDetail();
                    Mapper.Map(item, agNoteDetail);
                    agNoteDb.AgentNoteDetails.Add(agNoteDetail);
                }
            }

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    string agentAccId = AccountingChartHelper.GetAccountIdByPkAndTbName(agNoteDb.AgentId, "Agent", "AgentId");
                    if (string.IsNullOrEmpty(agentAccId))
                    {
                        agentAccId = AccountingChartHelper.AddAgentToChart(agNoteDb.AgentId, agNoteDb.AgentNoteType);
                    }


                    if (agNoteId == 0)
                    {
                        agNoteDb.AgentNoteCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AgentNote, true);
                        db.AgentNotes.Add(agNoteDb);
                    }

                    db.SaveChanges();

                    if (agNoteId == 0)
                    {
                        agentNoteVm.AgentId       = agNoteDb.AgentId;
                        agentNoteVm.AgentNoteCode = agNoteDb.AgentNoteCode;
                        //Add invoice to accounting transactions table
                        AddAgentToTransTable(agentAccId, agentNoteVm);


                        OperationHelper.ChangeOperationStatus(agentNoteVm.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }
                    isSaved = "true" + agNoteDb.AgentNoteId;
                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;
                    AdminHelper.LastIdRemoveOne(PrefixForEnum.AgentNote);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    AdminHelper.LastIdRemoveOne(PrefixForEnum.AgentNote);
                }
            }


            return(isSaved);
        }
Example #4
0
        internal static string AddEditInvoice(InvoiceVm invoiceVm)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int  invoiceId        = invoiceVm.InvoiceId;
            byte invoiceType      = invoiceVm.InvoiceType;
            // int hbId = invoiceVm.HouseBillId;

            Invoice invDb;

            if (invoiceId == 0)
            {
                invDb = new Invoice();
            }
            else
            {
                invDb = db.Invoices.Include("InvoiceDetails").Include("InvoiceTotals")
                        .Where(x => x.InvoiceId == invoiceId).FirstOrDefault();
                //Delete Invoice details and totals .. will insert it again
                var invDbTotals  = invDb.InvoiceTotals;
                var invDbDetails = invDb.InvoiceDetails;
                foreach (var item in invDbDetails)
                {
                    invDb.InvoiceDetails.Remove(item);
                }
                foreach (var item in invDbTotals)
                {
                    invDb.InvoiceTotals.Remove(item);
                }
            }


            Mapper.CreateMap <InvoiceVm, Invoice>()
            .ForMember(x => x.InvoiceTotals, y => y.Ignore())
            .ForMember(x => x.InvoiceDetails, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(invoiceVm, invDb);

            InvoiceDetail invDetail;

            Mapper.CreateMap <InvoiceDetailVm, InvoiceDetail>().IgnoreAllNonExisting();
            foreach (var item in invoiceVm.InvoiceDetails)
            {
                if (item.IsSelected == true)
                {
                    invDetail = new InvoiceDetail();
                    Mapper.Map(item, invDetail);
                    invDb.InvoiceDetails.Add(invDetail);
                }
            }

            InvoiceTotal invTotalDb;

            foreach (var item in invoiceVm.InvoiceTotals)
            {
                invTotalDb = new InvoiceTotal()
                {
                    CurrencyId       = item.CurrencyId,
                    TotalAmount      = item.TotalAmount,
                    CurrencySign     = item.CurrencySign,
                    TaxDepositAmount = item.TaxDepositAmount,
                    TotalBeforeTax   = item.TotalBeforeTax,
                    VatTaxAmount     = item.VatTaxAmount
                };

                invDb.InvoiceTotals.Add(invTotalDb);
            }



            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    //Add shipper or consignee to accounting chart
                    string accountId = GetAccountId(invoiceVm.OrderFrom, invoiceVm.ShipperId, invoiceVm.ConsigneeId);

                    if (invoiceId == 0)
                    {
                        invDb.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AccountingInvoice, true);
                        db.Invoices.Add(invDb);
                    }

                    db.SaveChanges();

                    invoiceVm.InvoiceId   = invDb.InvoiceId;
                    invoiceVm.InvoiceCode = invDb.InvoiceCode;


                    //Change HB status
                    if (invoiceId == 0)
                    {
                        //Add invoice to accounting transactions table
                        AddInvToTransTable(accountId, invoiceVm);

                        HouseBillHelper.ChangeHBStatus(invDb.HouseBillId, (byte)StatusEnum.InvoiceIssued);
                        OperationHelper.ChangeOperationStatus(invDb.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }

                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;

                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
            }

            return(isSaved);
        }
Example #5
0
        internal static string AddEditInvoice(InvoiceVm invoiceVm)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int  invoiceId        = invoiceVm.InvoiceId;
            byte invFor           = invoiceVm.InvoiceType;
            //  int hbId = invoiceVm.HouseBillId;

            InvoiceAP invDb;

            if (invoiceId == 0)
            {
                invDb = new InvoiceAP();
            }
            else
            {
                invDb = db.InvoiceAPs.Include("InvoiceDetailAPs").Include("InvoiceTotalAPs")
                        .Where(x => x.InvoiceId == invoiceId).FirstOrDefault();
                //Delete Invoice details and totals .. will insert it again
                var invDbTotals  = invDb.InvoiceTotalAPs;
                var invDbDetails = invDb.InvoiceDetailAPs;
                foreach (var item in invDbDetails)
                {
                    invDb.InvoiceDetailAPs.Remove(item);
                }
                foreach (var item in invDbTotals)
                {
                    invDb.InvoiceTotalAPs.Remove(item);
                }
            }


            Mapper.CreateMap <InvoiceVm, InvoiceAP>()
            .ForMember(x => x.InvoiceTotalAPs, y => y.Ignore())
            .ForMember(x => x.InvoiceDetailAPs, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(invoiceVm, invDb);

            InvoiceDetailAP invDetail;

            Mapper.CreateMap <InvoiceDetailVm, InvoiceDetailAP>().IgnoreAllNonExisting();
            foreach (var item in invoiceVm.InvoiceDetails)
            {
                if (item.IsSelected == true)
                {
                    invDetail = new InvoiceDetailAP();
                    Mapper.Map(item, invDetail);
                    invDb.InvoiceDetailAPs.Add(invDetail);
                }
            }

            InvoiceTotalAP invTotalDb;

            foreach (var item in invoiceVm.InvoiceTotals)
            {
                invTotalDb = new InvoiceTotalAP()
                {
                    CurrencyId   = item.CurrencyId,
                    TotalAmount  = item.TotalAmount,
                    CurrencySign = item.CurrencySign
                };

                invDb.InvoiceTotalAPs.Add(invTotalDb);
            }



            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    //Add shipper or consignee to accounting chart
                    string creditAccountId = "";
                    if (invFor == 1) //carrier
                    {
                        creditAccountId = AccountingChartHelper
                                          .GetAccountIdByPkAndTbName(invoiceVm.CarrierId.Value, "Carrier", "CarrierId");
                        if (string.IsNullOrEmpty(creditAccountId))
                        {
                            creditAccountId = AccountingChartHelper.AddCarrierToChart(invoiceVm.CarrierId.Value);
                        }
                    }
                    else if (invFor == 2) //Contractor
                    {
                        creditAccountId = AccountingChartHelper
                                          .GetAccountIdByPkAndTbName(invoiceVm.ContractorId.Value, "Contractor", "ContractorId");
                        if (string.IsNullOrEmpty(creditAccountId))
                        {
                            creditAccountId = AccountingChartHelper.AddContractorToChart(invoiceVm.ContractorId.Value);
                        }
                    }
                    else if (invFor == 3) //Custom Clearance
                    {
                        creditAccountId = AccountingChartHelper.GetCCCashDepAccountId(invoiceVm.OperationId);

                        if (!string.IsNullOrEmpty(creditAccountId))
                        {
                            invDb.InvStatusId = 4; //paid with cash deposit
                        }
                        else // In this case there was no cash deposit paid .. I added an account to sotre
                             //any credit amount for custom clearance
                        {
                            creditAccountId = ((int)AccountingChartEnum.CustomClearanceSupplier).ToString();
                        }
                    }

                    if (invoiceId == 0)
                    {
                        invDb.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.APInvoice, true);
                        db.InvoiceAPs.Add(invDb);
                    }

                    db.SaveChanges();

                    invoiceVm.InvoiceId   = invDb.InvoiceId;
                    invoiceVm.InvoiceCode = invDb.InvoiceCode;

                    //Change HB status
                    if (invoiceId == 0)
                    {
                        //Add invoice to accounting transactions table
                        if (!string.IsNullOrEmpty(creditAccountId))
                        {
                            AddAPInvToTransTable(creditAccountId, invoiceVm);
                        }

                        //if (invFor == 3) //Custom Clearance
                        if (invFor == 3) //will link the cash out receipts with same currency to this invoice as it is paid
                        {
                            int     operId     = invoiceVm.OperationId;
                            int     savedInvId = invoiceVm.InvoiceId;
                            int     currId     = invoiceVm.InvCurrencyId;
                            decimal invAmount  = invoiceVm.InvoiceTotals.FirstOrDefault().TotalAmount;
                            //Get cashout receiptIds
                            var cashoutReceiptIds = db.CashOutReceipts.Include("CashOutReceiptInvs")
                                                    .Where(x => x.OperationId == operId && x.CurrencyId == currId)
                                                    .ToList();

                            CashOutReceiptInv cashOutReceiptInv;
                            decimal           prevPaidFromReceipt, receiptAmount, amountToPay = 0;

                            foreach (var item in cashoutReceiptIds)
                            {
                                receiptAmount = item.ReceiptAmount;

                                //Get what paid before by this receipt
                                prevPaidFromReceipt = item.CashOutReceiptInvs.Sum(x => x.PaidAmount);
                                amountToPay         = receiptAmount - prevPaidFromReceipt;
                                if (amountToPay <= 0) //This receipt total amount is used by another invoices
                                {
                                    continue;
                                }

                                if (invAmount <= 0) //This Invoice is total paid
                                {
                                    break;
                                }

                                if (amountToPay > invAmount)
                                {
                                    amountToPay = invAmount;
                                }

                                //Add cash out receipt invoice
                                cashOutReceiptInv            = new CashOutReceiptInv();
                                cashOutReceiptInv.ReceiptId  = item.ReceiptId;
                                cashOutReceiptInv.InvoiceId  = savedInvId;
                                cashOutReceiptInv.PaidAmount = amountToPay;

                                db.CashOutReceiptInvs.Add(cashOutReceiptInv);

                                invAmount = invAmount - amountToPay;
                            }

                            db.SaveChanges();
                        }
                        // HouseBillHelper.ChangeHBStatus(invDb.HouseBillId, (byte)StatusEnum.InvoiceIssued);
                        OperationHelper.ChangeOperationStatus(invDb.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }

                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;

                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
            }

            return(isSaved);
        }
Example #6
0
        /// <summary>
        /// Get complete operation cost .. if has many HB .. will get the sum foreach on
        /// The will contains operation, trucking, custom clearance cost
        /// </summary>
        /// <param name="operationId">operation Id</param>
        /// <returns>OperationCostAccMainVm</returns>
        public static OperationCostAccMainVm GetOperationCost(int operationId, int hbId = 0, string langCode = "en")
        {
            OperationCostAccMainVm operCostMainVm;
            OperationsEntities     db = new OperationsEntities();

            if (hbId == 0)
            {
                operCostMainVm = db.OperationViews
                                 .Where(x => x.OperationId == operationId)
                                 .Select(x => new OperationCostAccMainVm
                {
                    BookingNumber = x.BookingNumber,
                    CarrierName   = x.CarrierNameEn,
                    FromPort      = x.FromPort,
                    ConsigneeName = x.ConsigneeNameEn,
                    MBL           = x.MBL,
                    OperationCode = x.OperationCode,
                    OperationId   = x.OperationId,
                    ShipperName   = x.ShipperNameEn,
                    ToPort        = x.ToPort,
                    VesselName    = x.VesselName,
                    AgentName     = x.AgentNameEn,
                    OrderFrom     = x.OrderFrom == 1 ? "Export" : "Import"
                }).FirstOrDefault();

                var hbList = db.HouseBills.Where(x => x.OperationId == operationId).Select(x => x.HouseBL).ToArray();
                operCostMainVm.HouseBL = string.Join(" , ", hbList);
            }
            else
            {
                operCostMainVm = db.HouseBillViews
                                 .Where(x => x.HouseBillId == hbId)
                                 .Select(x => new OperationCostAccMainVm
                {
                    BookingNumber = x.BookingNumber,
                    CarrierName   = x.CarrierNameEn,
                    FromPort      = x.FromPort,
                    ConsigneeName = x.CarrierNameEn,
                    MBL           = x.MBL,
                    OperationCode = x.OperationCode,
                    OperationId   = x.OperationId,
                    ShipperName   = x.ShipperNameEn,
                    ToPort        = x.ToPort,
                    VesselName    = x.VesselName,
                    AgentName     = x.AgentNameEn,
                    OrderFrom     = x.OrderFrom == 1 ? "Export" : "Import",
                    HouseBL       = x.HouseBL
                }).FirstOrDefault();
            }


            operCostMainVm.ContainerSummary = OperationHelper.GetContainersSummary(operCostMainVm.OperationId);

            dynamic operCostList;

            //Get operationcost
            if (hbId == 0)
            {
                operCostList = db.OperationCosts.Include("OperationCostLib").Include("Currency")
                               .Where(x => x.OperationId == operationId)
                               .GroupBy(g => new
                {
                    g.OperationCostLib.OperCostNameEn,
                    g.Currency.CurrencySign,
                    g.IsAgentCost,
                    g.CurrencyId,
                    g.OperCostLibId,
                    g.CurrencyIdSelling,
                    g.HouseBillId,
                    sellingCurrSign = g.Currency1.CurrencySign
                })
                               .Select(g => new
                {
                    OperCostNameEn       = g.Key.OperCostNameEn,
                    OperationCostNet     = g.Sum(x => x.OperationCostNet),
                    OperationCostSelling = g.Sum(x => x.OperationCostSelling),
                    CurrencySign         = g.Key.CurrencySign,
                    IsAgentCost          = g.Key.IsAgentCost,
                    CurrencyId           = g.Key.CurrencyId,
                    OperCostId           = g.Key.OperCostLibId,
                    CurrencyIdSelling    = g.Key.CurrencyIdSelling,
                    HouseBillId          = g.Key.HouseBillId,
                    CurrencySignSelling  = g.Key.sellingCurrSign
                })
                               .ToList();
            }
            else
            {
                //Will not get agent cost .. check with operation
                operCostList = db.OperationCosts.Include("OperationCostLibs")
                               .Include("Currency")
                               .Include("Currency1")
                               .Where(x => x.HouseBillId == hbId && x.IsAgentCost == false)
                               .Select(g => new
                {
                    OperCostNameEn       = g.OperationCostLib.OperCostNameEn,
                    OperationCostNet     = g.OperationCostNet,
                    OperationCostSelling = g.OperationCostSelling,
                    CurrencySign         = g.Currency.CurrencySign,
                    IsAgentCost          = g.IsAgentCost,
                    CurrencyId           = g.CurrencyId,
                    OperCostId           = g.OperCostId,
                    CurrencyIdSelling    = g.CurrencyIdSelling,
                    CurrencySignSelling  = g.Currency1.CurrencySign,
                    HouseBillId          = g.HouseBillId
                })
                               .ToList();
            }

            OperationCostAccVm operCostAccVm;
            int i = 1;

            foreach (var item in operCostList)
            {
                operCostAccVm                     = new OperationCostAccVm();
                operCostAccVm.Id                  = i;
                operCostAccVm.CostName            = item.OperCostNameEn;
                operCostAccVm.CurrencySign        = item.CurrencySign;
                operCostAccVm.IsAgent             = item.IsAgentCost;
                operCostAccVm.NetRate             = Math.Round(item.OperationCostNet, 2);
                operCostAccVm.SellingRate         = Math.Round(item.OperationCostSelling, 2);
                operCostAccVm.CurrencyId          = item.CurrencyId;
                operCostAccVm.FkType              = Convert.ToByte(CostType.OperationCost);
                operCostAccVm.CostFkId            = item.OperCostId;
                operCostAccVm.CurrencyIdSelling   = item.CurrencyIdSelling;
                operCostAccVm.CurrencySignSelling = item.CurrencySignSelling;
                operCostAccVm.HouseBillId         = item.HouseBillId;

                i = i + 1;
                operCostMainVm.OperationCostAccVms.Add(operCostAccVm);
            }

            //Get trucking cost
            dynamic truckingCost;

            if (hbId == 0)
            {
                EasyFreightEntities db1 = new EasyFreightEntities();
                truckingCost = db1.TruckingOrderCosts.Include("TruckingCostLibs").Include("Currency")
                               .Where(x => x.TruckingOrderDetail.TruckingOrder.OperationId == operationId)
                               .GroupBy(g => new { g.TruckingCostLib.TruckingCostName, g.Currency.CurrencySign, g.CurrencyId, g.TruckingCostId })
                               .Select(g => new
                {
                    NetRate             = g.Sum(x => x.TruckingCostNet),
                    SellingRate         = g.Sum(x => x.TruckingCostSelling),
                    CostName            = g.Key.TruckingCostName,
                    CurrencySign        = g.Key.CurrencySign,
                    CurrencyId          = g.Key.CurrencyId,
                    TruckingOrderCostId = g.Key.TruckingCostId
                }).ToList();
            }
            else
            {
                EasyFreightEntities db1 = new EasyFreightEntities();
                truckingCost = db1.TruckingOrderCosts.Include("TruckingCostLibs").Include("Currency")
                               .Where(x => x.TruckingOrderDetail.TruckingOrder.HouseBillId == hbId)
                               .Select(g => new
                {
                    NetRate             = g.TruckingCostNet,
                    SellingRate         = g.TruckingCostSelling,
                    CostName            = g.TruckingCostLib.TruckingCostName,
                    CurrencySign        = g.Currency.CurrencySign,
                    CurrencyId          = g.CurrencyId,
                    TruckingOrderCostId = g.TruckingOrderCostId
                }).ToList();
            }


            foreach (var item in truckingCost)
            {
                operCostAccVm                     = new OperationCostAccVm();
                operCostAccVm.Id                  = i;
                operCostAccVm.CostName            = item.CostName;
                operCostAccVm.CurrencySign        = item.CurrencySign;
                operCostAccVm.NetRate             = Math.Round(item.NetRate, 2);
                operCostAccVm.SellingRate         = Math.Round(item.SellingRate, 2);
                operCostAccVm.CurrencyId          = item.CurrencyId;
                operCostAccVm.FkType              = Convert.ToByte(CostType.TruckingCost);
                operCostAccVm.CostFkId            = item.TruckingOrderCostId;
                operCostAccVm.CurrencyIdSelling   = item.CurrencyId;
                operCostAccVm.CurrencySignSelling = item.CurrencySign;
                i = i + 1;
                operCostMainVm.OperationCostAccVms.Add(operCostAccVm);
            }

            //Get CC cost
            dynamic ccCost;

            if (hbId == 0)
            {
                ccCost = db.CustomClearanceDetails.Include("CustomClearanceCostLibs").Include("Currency")
                         .Where(x => x.CustomClearanceOrder.OperationId == operationId)
                         .GroupBy(g => new { g.CustomClearanceCostLib.CostNameEn, g.Currency.CurrencySign, g.CurrencyId, g.CCCostId })
                         .Select(g => new
                {
                    NetRate      = g.Sum(x => x.CCCostNet),
                    SellingRate  = g.Sum(x => x.CCCostSelling),
                    CostName     = g.Key.CostNameEn,
                    CurrencySign = g.Key.CurrencySign,
                    CurrencyId   = g.Key.CurrencyId,
                    CCDetailsId  = g.Key.CCCostId
                });
            }
            else
            {
                ccCost = db.CustomClearanceDetails.Include("CustomClearanceCostLibs").Include("Currency")
                         .Where(x => x.CustomClearanceOrder.HouseBillId == hbId)
                         .Select(g => new
                {
                    NetRate      = g.CCCostNet,
                    SellingRate  = g.CCCostSelling,
                    CostName     = g.CustomClearanceCostLib.CostNameEn,
                    CurrencySign = g.Currency.CurrencySign,
                    CurrencyId   = g.CurrencyId,
                    CCDetailsId  = g.CCDetailsId
                });
            }


            foreach (var item in ccCost)
            {
                operCostAccVm                     = new OperationCostAccVm();
                operCostAccVm.Id                  = i;
                operCostAccVm.CostName            = item.CostName;
                operCostAccVm.CurrencySign        = item.CurrencySign;
                operCostAccVm.NetRate             = Math.Round(item.NetRate, 2);
                operCostAccVm.SellingRate         = Math.Round(item.SellingRate, 2);
                operCostAccVm.CurrencyId          = item.CurrencyId;
                operCostAccVm.FkType              = Convert.ToByte(CostType.CCCost);
                operCostAccVm.CostFkId            = item.CCDetailsId;
                operCostAccVm.CurrencyIdSelling   = item.CurrencyId;
                operCostAccVm.CurrencySignSelling = item.CurrencySign;
                i = i + 1;
                operCostMainVm.OperationCostAccVms.Add(operCostAccVm);
            }

            //Totals
            var totalList = operCostMainVm.OperationCostAccVms.GroupBy(g => new { g.CurrencySign })
                            .Select(g => new
            {
                CurrencySign = g.Key.CurrencySign,
                TotalNetRate = g.Sum(x => x.NetRate),
                CurrencyId   = g.Where(x => x.CurrencySign == g.Key.CurrencySign).FirstOrDefault().CurrencyId
            }).ToList();

            var totalListSelling = operCostMainVm.OperationCostAccVms.GroupBy(g => g.CurrencySignSelling)
                                   .Select(g => new
            {
                CurrencySign     = g.Key,
                TotalSellingRate = g.Sum(x => x.SellingRate),
                TotalAgentRate   = g.Where(x => x.IsAgent).Sum(x => x.SellingRate),
                CurrencyId       = g.Where(x => x.CurrencySignSelling == g.Key).FirstOrDefault().CurrencyIdSelling
            }).ToList();

            Dictionary <int, string> usedCurrency = new Dictionary <int, string>();

            usedCurrency = totalList.Select(c => new { key = c.CurrencyId, value = c.CurrencySign }).Distinct().ToDictionary(x => x.key, x => x.value);
            foreach (var item in totalListSelling)
            {
                if (!usedCurrency.ContainsKey(item.CurrencyId))
                {
                    usedCurrency.Add(item.CurrencyId, item.CurrencySign);
                }
            }
            // usedCurrency.(totalListSelling.Select(c => c.CurrencyId).Distinct().ToList());
            //usedCurrency = usedCurrency.Distinct().ToList();


            OperationCostTotalAccVm _operTotalsVm;

            foreach (var item in usedCurrency)
            {
                var sellingItem = totalListSelling.Where(x => x.CurrencyId == item.Key).FirstOrDefault();
                var netItem     = totalList.Where(x => x.CurrencyId == item.Key).FirstOrDefault();

                _operTotalsVm = new OperationCostTotalAccVm();
                _operTotalsVm.CurrencySign     = netItem == null ? item.Value : netItem.CurrencySign;
                _operTotalsVm.TotalNetRate     = netItem == null ? 0 : netItem.TotalNetRate;
                _operTotalsVm.TotalSellingRate = sellingItem == null ? 0 : sellingItem.TotalSellingRate;
                _operTotalsVm.TotalAgentRate   = sellingItem == null ? 0 : sellingItem.TotalAgentRate;
                _operTotalsVm.CurrencyId       = netItem == null ? item.Key : netItem.CurrencyId;

                operCostMainVm.OperationCostTotalAccVms.Add(_operTotalsVm);
            }


            //OperationCostTotalAccVm operTotalsVm;
            //int currId;
            //foreach (var item in totalList)
            //{
            //    currId = item.CurrencyId;
            //    var sellingItem = totalListSelling.Where(x => x.CurrencyId == currId).FirstOrDefault();
            //    operTotalsVm = new OperationCostTotalAccVm();
            //    operTotalsVm.CurrencySign = item.CurrencySign;
            //    operTotalsVm.TotalNetRate = item.TotalNetRate;
            //    operTotalsVm.TotalSellingRate = sellingItem == null ? 0 : sellingItem.TotalSellingRate;
            //    operTotalsVm.TotalAgentRate = sellingItem  == null ? 0 : sellingItem.TotalAgentRate;
            //    operTotalsVm.CurrencyId = item.CurrencyId;

            //    operCostMainVm.OperationCostTotalAccVms.Add(operTotalsVm);
            //}

            return(operCostMainVm);
        }