public static CashInVm GetCashReceiptForPartners(string receiptType, int receiptId = 0)
        {
            CashInVm cashVm;

            if (receiptId != 0)
            {
                cashVm = CashOutHelper.FillCashVmForReceiptView(receiptId);
                return(cashVm);
            }
            cashVm = new CashInVm();

            if (receiptType == "out")
            {
                cashVm.CashType = "cashout";
            }
            else
            {
                cashVm.CashType = "cashin";
            }

            cashVm.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, false);
            //cashVm.PaymentTermId = 1;
            //cashVm.CurrencyId = 1;
            CashInCheckVm cashCheckVm = new CashInCheckVm();

            cashVm.CashInReceiptChecks.Add(cashCheckVm);

            return(cashVm);
        }
Example #2
0
        public static string AddArCashDeposit(CashInVm cashInVm)
        {
            string             isSaved       = "true";
            AccountingEntities db            = new AccountingEntities();
            ArCashDeposit      arCashDeposit = new ArCashDeposit();

            Mapper.CreateMap <CashInVm, ArCashDeposit>()
            .IgnoreAllNonExisting();
            Mapper.Map(cashInVm, arCashDeposit);

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    arCashDeposit.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashIn, true);
                    db.ArCashDeposits.Add(arCashDeposit);

                    db.SaveChanges();



                    cashInVm.ReceiptId   = arCashDeposit.ReceiptId;
                    cashInVm.ReceiptCode = arCashDeposit.ReceiptCode;

                    string creditAccountId = ((int)AccountingChartEnum.DepositRevenues).ToString();

                    //Add invoice to accounting transactions table
                    CashHelper.AddReceiptToTransTable(creditAccountId, cashInVm, "ArCashDeposit");

                    //Add or update deposit balance for this client
                    CashDepositBalanceHelper.AddEditArDepBalance(cashInVm.ShipperId, cashInVm.ConsigneeId,
                                                                 cashInVm.CurrencyId, cashInVm.ReceiptAmount.Value, db);

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


            return(isSaved);
        }
        public static CashInVm GetCashReceiptForExpense(int receiptId = 0)
        {
            CashInVm cashVm;

            if (receiptId != 0)
            {
                cashVm = CashOutHelper.FillCashVmForReceiptView(receiptId);
                return(cashVm);
            }
            cashVm               = new CashInVm();
            cashVm.CashType      = "cashout";
            cashVm.ReceiptCode   = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, false);
            cashVm.PaymentTermId = 1;
            cashVm.CurrencyId    = 1;

            CashOutExpense cashOutExpense = new CashOutExpense();

            cashVm.CashOutReceiptExpenses.Add(cashOutExpense);


            return(cashVm);
        }
Example #4
0
        public static string AddEditOperation(OperationVm operationVm, out int operId)
        {
            string                    isSaved     = "true";
            int                       operationId = operationVm.OperationId;
            OperationsEntities        db          = new OperationsEntities();
            Operation                 operationDb;
            List <OperationContainer> operationContListDb;
            // notifications
            NotificationMsgVm notifi = new NotificationMsgVm();

            if (operationId == 0)
            {
                operationDb         = new Operation();
                operationContListDb = new List <OperationContainer>();
            }
            else
            {
                operationDb = db.Operations.Include("OperationContainers")
                              .Where(x => x.OperationId == operationId).FirstOrDefault();
                if (operationDb.StatusId > 2)
                {
                    operId  = operationDb.OperationId;
                    isSaved = "false .. Cann't update operation is not open ";
                    return(isSaved);
                }
                operationContListDb = operationDb.OperationContainers.ToList();

                //Get quotContainers Ids sent from the screen
                List <long> containerVmIds = operationVm.OperationContainers.Select(x => x.OperConId).ToList();
                var         containerDel   = operationContListDb.Where(x => !containerVmIds.Contains(x.OperConId)).ToList();

                foreach (var item in containerDel)
                {
                    db.OperationContainers.Remove(item);
                }
            }

            Mapper.CreateMap <OperationVm, Operation>().IgnoreAllNonExisting();
            // .ForMember(x => x.OperationContainers, v=> v.Ignore());
            Mapper.CreateMap <OperationContainerVm, OperationContainer>().IgnoreAllNonExisting();
            Mapper.Map(operationVm, operationDb);

            bool updateHB = false;

            if (operationId == 0)
            {
                //Generate code at save event
                if (operationDb.OrderFrom == 1)
                {
                    operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Export, true);
                }
                else
                {
                    operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Import, true);
                }

                db.Operations.Add(operationDb);
                //update quotation status if any
                int?quoteId = operationVm.QuoteId;
                if (quoteId != null)
                {
                    //status = 2 -- opened
                    Quotation quoteToUpdate = db.Quotations.Where(x => x.QuoteId == quoteId).FirstOrDefault();
                    quoteToUpdate.StatusId = 2;
                }

                notifi.NotificationTypeID = (operationDb.OrderFrom == 1) ? 1 : 3;
                notifi.ObjectID           = -1;
            }
            else
            {
                List <HouseBillListVm> hbList = HouseBillHelper.GetHBList(operationId, operationDb.OrderFrom);
                if (hbList.Count == 1)
                {
                    if (!operationDb.IsConsolidation)
                    {
                        HouseBill hb = db.HouseBills.Where(x => x.OperationId == operationDb.OperationId).FirstOrDefault();
                        hb.CBM = operationDb.CBM;
                        hb.GoodsDescription     = operationDb.GoodsDescription;
                        hb.GrossWeight          = operationDb.GrossWeight;
                        hb.NetWeight            = operationDb.NetWeight;
                        hb.NumberOfPackages     = operationDb.NumberOfPackages;
                        hb.CollectedFreightCost = operationDb.FreightCostAmount;
                        hb.CollectedThcCost     = operationDb.ThcCostAmount;



                        // hb.ShipperId = operationDb.ShipperId;
                        //  hb.ConsigneeId = operationDb.ConsigneeId;
                        // hb.NotifierId = operationDb.NotifierId;
                        //  hb.NotifierAsConsignee = operationDb.NotifierAsConsignee;
                        //  hb.AgentId = operationDb.AgentId;

                        hb.FromPortId = operationDb.FromPortId;
                        hb.ToPortId   = operationDb.ToPortId;
                    }
                }
            }
            operId = 0;
            try
            {
                db.SaveChanges();
                operId = operationDb.OperationId;
                if (notifi.ObjectID == -1)
                {
                    notifi.ObjectID        = operId;
                    notifi.NotificationMsg = " New " + (operationDb.OrderFrom == 1 ? "Export " : "Import ") + " Operation Code: " + operationDb.OperationCode;
                    NotificationHelper.Create_Notification(notifi);
                }

                //update hb
                if (updateHB)
                {
                }
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.Message;
            }

            return(isSaved);
        }
Example #5
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 #6
0
        public static string AddEditTruckingOrderDetails(TruckingOrderDetailVm truckingOrderVm)
        {
            int    truckingOrderId = truckingOrderVm.TruckingOrderId;
            string isSaved         = "true";
            EasyFreightEntities db = new EasyFreightEntities();
            TruckingOrderDetail orderDetailDb;
            //check if detauils is inserted before
            int detailCount = db.TruckingOrderDetails.Where(x => x.TruckingOrderId == truckingOrderId).Count();

            TruckingOrder orderDb;

            orderDb = db.TruckingOrders.Where(x => x.TruckingOrderId == truckingOrderId).FirstOrDefault();
            // change the status
            if (truckingOrderVm.StatusId == 3)
            {
                orderDb.StatusId = 3; //closed
            }

            List <TruckingOrderCost> dbOrderCostList = new List <TruckingOrderCost>();

            if (detailCount == 0) //Add new case
            {
                orderDetailDb = new TruckingOrderDetail();
            }
            else
            {
                orderDetailDb = db.TruckingOrderDetails.Include("TruckingOrderCosts").Where(x => x.TruckingOrderId == truckingOrderId).FirstOrDefault();

                //delete any removed costs on the screen
                dbOrderCostList = orderDetailDb.TruckingOrderCosts.ToList();

                //Get cost Ids sent from the screen
                List <int> truckingOrderCostVmIds = truckingOrderVm.TruckingOrderCosts.Select(x => x.TruckingOrderCostId).ToList();
                var        orderCostDel           = dbOrderCostList.Where(x => !truckingOrderCostVmIds.Contains(x.TruckingOrderCostId)).ToList();

                foreach (var item in orderCostDel)
                {
                    db.TruckingOrderCosts.Remove(item);
                }
            }

            Mapper.CreateMap <TruckingOrderDetailVm, TruckingOrderDetail>().IgnoreAllNonExisting();
            Mapper.CreateMap <TruckingCostVm, TruckingOrderCost>().IgnoreAllNonExisting();
            Mapper.Map(truckingOrderVm, orderDetailDb);

            if (detailCount == 0)
            {
                orderDetailDb.TruckingOrderCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.TruckingOrder, true);
                db.TruckingOrderDetails.Add(orderDetailDb);

                if (truckingOrderVm.StatusId != 3)
                {
                    orderDb.StatusId = 2; // open
                }
            }

            List <OperationContainer> dbOperationContainerList = new List <OperationContainer>();
            OperationsEntities        opdb = new OperationsEntities();

            dbOperationContainerList = opdb.OperationContainers.Where(x => x.OperationId == truckingOrderVm.OperationId).ToList();

            foreach (var item in truckingOrderVm.OperationContainers)
            {
                long operConIdvmId = item.OperConId;
                OperationContainer operationContainerDb = dbOperationContainerList
                                                          .Where(x => x.OperConId == operConIdvmId).FirstOrDefault();
                operationContainerDb.ContainerNumber = item.ContainerNumber;
                operationContainerDb.SealNumber      = item.SealNumber;
                //Mapper.Map(item, operationContainerDb);
            }

            try
            {
                db.SaveChanges();

                opdb.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.InnerException.InnerException.Message;
            }

            return(isSaved);
        }
Example #7
0
        public static AgentNoteVm GetAgentNoteInfo(int operId, byte noteType, int agNoteId = 0, bool forEdit = true)
        {
            AgentNoteVm        agNoteVm = new AgentNoteVm(noteType);
            OperationsEntities db1      = new OperationsEntities();


            AccountingEntities db = new AccountingEntities();

            AgentNote agNoteDb;

            if (agNoteId != 0)
            {
                agNoteDb = db.AgentNotes.Where(x => x.AgentNoteId == agNoteId).FirstOrDefault();
                Mapper.CreateMap <AgentNote, AgentNoteVm>().IgnoreAllNonExisting();
                Mapper.CreateMap <AgentNoteDetail, AgentNoteDetailVm>().IgnoreAllNonExisting();
                Mapper.Map(agNoteDb, agNoteVm);

                operId = agNoteDb.OperationId;

                //agNoteVm.FromPort = operationInfo.FromPort;
                //agNoteVm.ToPort = operationInfo.ToPort;
            }
            else
            {
                agNoteDb = new AgentNote();
                agNoteVm.AgentNoteCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AgentNote, false);
            }

            var operationInfo = db1.OperationViews.Where(x => x.OperationId == operId).FirstOrDefault();

            Mapper.CreateMap <OperationView, AgentNoteVm>()
            .ForMember(x => x.CreateBy, y => y.Ignore())
            .ForMember(x => x.CreateDate, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(operationInfo, agNoteVm);

            if (operationInfo.OrderFrom == 1)
            {
                agNoteVm.CustomerName = operationInfo.ShipperNameEn;
            }
            else
            {
                agNoteVm.CustomerName = operationInfo.ConsigneeNameEn;
            }

            agNoteVm.AgentName = operationInfo.AgentNameEn;

            var operCostObj = AccountingHelper.GetOperationCost(agNoteVm.OperationId, 0);

            var agentCostList      = operCostObj.OperationCostAccVms.Where(x => x.IsAgent).ToList();
            var agentCostTotalList = operCostObj.OperationCostTotalAccVms.Where(x => x.TotalAgentRate != 0).ToList();

            var operCostList = db1.OperationCosts.Include("OperationCostLib").Include("Currency")
                               .Where(x => x.OperationId == operId && x.IsAgentCost == true).ToList();

            agNoteVm.ContainerSummary = operCostObj.ContainerSummary;

            agNoteVm.OperationCostAccVms      = agentCostList;
            agNoteVm.OperationCostTotalAccVms = agentCostTotalList;
            agNoteVm.HouseBillId = agentCostList.FirstOrDefault().HouseBillId;
            if (agentCostList.Count > 0)
            {
                agNoteVm.CurrencyId   = agentCostList.FirstOrDefault().CurrencyId;
                agNoteVm.CurrencySign = agentCostList.FirstOrDefault().CurrencySign;
            }


            if (forEdit)
            {
                //Get prev agent note details for this operation
                List <int> usedCost = db.AgentNoteDetails
                                      .Where(x => x.AgentNote.OperationId == operId)
                                      .Select(x => x.OperCostId)
                                      .ToList();
                var newCosts = operCostList.Where(x => !usedCost.Contains(x.OperCostId)).ToList();

                AgentNoteDetailVm agentNoteDetailVm;
                foreach (var item in newCosts)
                {
                    agentNoteDetailVm = new AgentNoteDetailVm()
                    {
                        AgentNoteDetailId = 0,
                        CostName          = item.OperationCostLib.OperCostNameEn,
                        CurrencyId        = item.CurrencyId,
                        CurrencySign      = item.Currency.CurrencySign,
                        ExchangeRate      = 1,
                        MainAmount        = agNoteVm.AgentNoteType == 1 ? item.OperationCostSelling : item.OperationCostNet,
                        MainCurrencyId    = item.CurrencyId,
                        MainCurrencySign  = item.Currency.CurrencySign,
                        OperCostId        = item.OperCostId,
                        OperCostLibId     = item.OperCostLibId
                    };

                    agNoteVm.AgentNoteDetails.Add(agentNoteDetailVm);
                }
            }
            else
            {
                foreach (var item in agNoteVm.AgentNoteDetails)
                {
                    item.CostName = operCostList
                                    .Where(x => x.OperCostId == item.OperCostId).FirstOrDefault().OperationCostLib.OperCostNameEn;
                }
            }
            return(agNoteVm);
        }
Example #8
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 #9
0
        public static InvoiceVm GetInvoiceInfoNew(int hbId = 0, int invId = 0, bool forEdit = true)
        {
            InvoiceVm invoiceVm = new InvoiceVm(0);


            AccountingEntities db = new AccountingEntities();

            if (invId != 0)
            {
                Invoice invDb = db.Invoices.Where(x => x.InvoiceId == invId).FirstOrDefault();
                Mapper.CreateMap <Invoice, InvoiceVm>().IgnoreAllNonExisting();
                Mapper.CreateMap <InvoiceDetail, InvoiceDetailVm>().IgnoreAllNonExisting();
                Mapper.CreateMap <InvoiceTotal, InvoiceTotalVm>().IgnoreAllNonExisting();
                Mapper.Map(invDb, invoiceVm);

                //invoiceVm.FromPort = hbInfo.FromPort;
                //invoiceVm.ToPort = hbInfo.ToPort;
                invoiceVm.PaymentTermName = invDb.PaymentTerm.PaymentTermEn;
                invoiceVm.CurrencySign    = invDb.Currency.CurrencySign;
                //invType = invDb.InvoiceType;

                //Add Tax part
                //decimal totalBeforeTax = invoiceVm.InvoiceDetails.Sum(x => x.InvoiceAmount);
                // invoiceVm.InvoiceTotals.FirstOrDefault().TotalBeforeTax = totalBeforeTax;
                // invoiceVm.InvoiceTotals.FirstOrDefault().TaxDepositAmount = totalBeforeTax - invoiceVm.InvoiceTotals.FirstOrDefault().TotalAmount;
            }
            else
            {
                invoiceVm.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AccountingInvoice, false);
            }

            if (hbId == 0)
            {
                hbId = invoiceVm.HouseBillId.Value;
            }

            OperationsEntities db1 = new OperationsEntities();
            var hbInfo             = db1.HouseBillViews.Where(x => x.HouseBillId == hbId).FirstOrDefault();

            Mapper.CreateMap <HouseBillView, InvoiceVm>()
            .ForMember(x => x.CreateBy, y => y.Ignore())
            .ForMember(x => x.CreateDate, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(hbInfo, invoiceVm);

            if (hbInfo.OrderFrom == 1)
            {
                invoiceVm.CustomerName = hbInfo.ShipperNameEn;
                invoiceVm.ConsigneeId  = null;
            }
            else
            {
                invoiceVm.CustomerName = hbInfo.ConsigneeNameEn;
                invoiceVm.ShipperId    = null;
            }

            //Get Cost list
            var operCostObj = AccountingHelper.GetOperationCost(invoiceVm.OperationId, invoiceVm.HouseBillId.Value);

            var operCostList      = operCostObj.OperationCostAccVms;
            var operCostTotalList = operCostObj.OperationCostTotalAccVms;

            GetHbInvTotal(0, ref operCostList, ref operCostTotalList);

            invoiceVm.OperationCostAccVms      = operCostList;
            invoiceVm.OperationCostTotalAccVms = operCostTotalList;



            if (forEdit)
            {
                //Get prev invoice details for this HB
                // adding condation  [x.Invoice.IsDeleted==null] get undeleted invoices
                var prevInvDetails = db.InvoiceDetails
                                     .Where(x => x.Invoice.HouseBillId == hbId && x.Invoice.IsDeleted == null).OrderBy(x => x.ItemOrder)
                                     .Select(x => new { x.CostFkId, x.FkType })
                                     .ToList();
                InvoiceDetailVm invDetailVm;
                //Add other costs that are not inserted in inv details table
                for (int i = 1; i < 4; i++) // loop for the three cost types
                {
                    List <int> usedCost = invoiceVm.InvoiceDetails.Where(x => x.FkType == i).Select(x => x.CostFkId).ToList();
                    usedCost.AddRange(prevInvDetails.Where(x => x.FkType == i).Select(x => x.CostFkId).ToList());
                    var newCosts = operCostList.Where(x => !usedCost.Contains(x.CostFkId) && x.FkType == i).ToList();
                    foreach (var item in newCosts)
                    {
                        invDetailVm = new InvoiceDetailVm()
                        {
                            CostFkId       = item.CostFkId,
                            CostName       = item.CostName,
                            ExchangeRate   = 1,
                            FkType         = item.FkType,
                            InvDetailId    = 0,
                            InvoiceAmount  = item.SellingRate,
                            InvoiceId      = invoiceVm.InvoiceId,
                            MainAmount     = item.SellingRate,
                            MainCurrencyId = item.CurrencyIdSelling,
                            MainCurrSign   = item.CurrencySignSelling
                        };

                        invoiceVm.InvoiceDetails.Add(invDetailVm);
                    }
                }

                if (invoiceVm.InvoiceTotals.Count == 0)
                {
                    InvoiceTotalVm invTotal = new InvoiceTotalVm()
                    {
                        CurrencyId   = invoiceVm.InvCurrencyId,
                        CurrencySign = invoiceVm.CurrencySign,
                        InvoiceId    = invoiceVm.InvoiceId,
                        TotalAmount  = 0
                    };

                    invoiceVm.InvoiceTotals.Add(invTotal);
                }
            }
            else
            {
                Dictionary <int, string> operCost  = new Dictionary <int, string>();
                Dictionary <int, string> truckCost = new Dictionary <int, string>();
                Dictionary <int, string> ccCost    = new Dictionary <int, string>();
                if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.OperationCost))
                {
                    operCost = ListCommonHelper.GetOperationCostList();
                }
                if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.TruckingCost))
                {
                    truckCost = ListCommonHelper.GetTruckingCostNameList();
                }
                if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.CCCost))
                {
                    ccCost = ListCommonHelper.GetCustClearCostList();
                }
                //Get cost name for invoice items
                foreach (var item in invoiceVm.InvoiceDetails)
                {
                    item.CostName = invoiceVm.OperationCostAccVms
                                    .Where(x => x.CostFkId == item.CostFkId && x.FkType == item.FkType)
                                    .FirstOrDefault().CostName;
                }
            }
            invoiceVm.InvoiceDetails = invoiceVm.InvoiceDetails.OrderBy(z => z.ItemOrder).ToList();
            return(invoiceVm);
        }
Example #10
0
        internal static string AddEditCashReceipt(CashInVm cashInVmObj, out int savedReceiptId, bool addToTrans = true)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int receiptId         = cashInVmObj.ReceiptId;

            savedReceiptId = receiptId;
            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptChecks")
                            .Include("CashOutReceiptInvs")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete invoice list .. will insert it again
                var invList = cashDbObj.CashOutReceiptInvs.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptInvs.Remove(item);
                }

                //Delete check list .. will insert it again
                var checkList = cashDbObj.CashOutReceiptChecks.ToList();
                foreach (var item in checkList)
                {
                    cashDbObj.CashOutReceiptChecks.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(cashInVmObj, cashDbObj);

            CashOutReceiptCheck cashCheckDb;

            foreach (var item in cashInVmObj.CashInReceiptChecks)
            {
                if (!string.IsNullOrEmpty(item.CheckNumber))
                {
                    cashCheckDb = new CashOutReceiptCheck();
                    Mapper.CreateMap <CashInCheckVm, CashOutReceiptCheck>().IgnoreAllNonExisting();
                    Mapper.Map(item, cashCheckDb);

                    cashDbObj.CashOutReceiptChecks.Add(cashCheckDb);
                }
            }

            if (cashInVmObj.OperationId != null) //CC Cash Deposit
            {
                CashOutCCCashDeposit cashDeposit = new CashOutCCCashDeposit();
                cashDeposit.OperationId = cashInVmObj.OperationId.Value;
                cashDeposit.ReceiptId   = cashInVmObj.ReceiptId;
                cashDbObj.CashOutCCCashDeposits.Add(cashDeposit);
            }

            else if (cashInVmObj.AgentId == null) //Cash Receipt for invoice
            {
                //Add Receipt invoices
                CashOutReceiptInv cashInvDb;
                foreach (var item in cashInVmObj.CashInReceiptInvs)
                {
                    if (item.IsSelected)
                    {
                        cashInvDb = new CashOutReceiptInv();
                        Mapper.CreateMap <CashInInvoiceVm, CashOutReceiptInv>().IgnoreAllNonExisting();
                        item.CashInReceipt = null;
                        Mapper.Map(item, cashInvDb);

                        cashDbObj.CashOutReceiptInvs.Add(cashInvDb);
                    }
                }
            }
            else //Cash Receipt for Agent Note
            {
                //Add Receipt Agent Notes
                CashOutReceiptAgNote cashAgNoteDb;
                foreach (var item in cashInVmObj.CashInReceiptInvs)
                {
                    if (item.IsSelected)
                    {
                        cashAgNoteDb = new CashOutReceiptAgNote();
                        Mapper.CreateMap <CashInInvoiceVm, CashOutReceiptAgNote>().IgnoreAllNonExisting();
                        item.CashInReceipt = null;
                        Mapper.Map(item, cashAgNoteDb);

                        cashDbObj.CashOutReceiptAgNotes.Add(cashAgNoteDb);
                    }
                }
            }


            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    savedReceiptId = cashInVmObj.ReceiptId;

                    #region Add To Transaction Table
                    if (addToTrans)
                    {
                        //Add shipper or consignee to accounting chart
                        string debitAccountId = "";
                        if (cashInVmObj.OperationId != null) //CC Cash Deposit
                        {
                            debitAccountId = AccountingChartHelper.GetCCCashDepAccountId(cashInVmObj.OperationId.Value);
                            if (string.IsNullOrEmpty(debitAccountId))
                            {
                                debitAccountId = AccountingChartHelper.AddCCCashDepositToChart(cashInVmObj.OperationId.Value);
                            }
                        }
                        else if (!string.IsNullOrEmpty(cashInVmObj.PartnerAccountId)) //Partner Drawing
                        {
                            debitAccountId = cashInVmObj.PartnerAccountId;
                        }
                        else if (cashInVmObj.AgentId == null) //Cash Receipt for invoice
                        {
                            if (cashInVmObj.InvoiceType == 1) //carrier
                            {
                                debitAccountId = AccountingChartHelper
                                                 .GetAccountIdByPkAndTbName(cashInVmObj.CarrierId.Value, "Carrier", "CarrierId");
                                if (string.IsNullOrEmpty(debitAccountId))
                                {
                                    debitAccountId = AccountingChartHelper.AddCarrierToChart(cashInVmObj.CarrierId.Value);
                                }
                            }
                            else if (cashInVmObj.InvoiceType == 2) //contractor
                            {
                                debitAccountId = AccountingChartHelper
                                                 .GetAccountIdByPkAndTbName(cashInVmObj.ContractorId.Value, "Contractor", "ContractorId");
                                if (string.IsNullOrEmpty(debitAccountId))
                                {
                                    debitAccountId = AccountingChartHelper.AddContractorToChart(cashInVmObj.ContractorId.Value);
                                }
                            }
                            else if (cashInVmObj.InvoiceType == 3) //custom clearance .. 22/11/2016
                            {
                                debitAccountId = ((int)AccountingChartEnum.CustomClearanceSupplier).ToString();
                            }
                        }
                        else //Cash Receipt for Agent Note
                        {
                            debitAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.AgentId.Value, "Agent", "AgentId");
                            if (string.IsNullOrEmpty(debitAccountId))
                            {
                                debitAccountId = AccountingChartHelper.AddAgentToChart(cashInVmObj.AgentId.Value, 2); //AgentNoteType = 2 .. Credit Agent
                            }
                        }

                        if (receiptId == 0)
                        {
                            //Add invoice to accounting transactions table

                            AddReceiptToTransTable(debitAccountId, cashInVmObj);

                            foreach (var item in cashInVmObj.CashInReceiptInvs)
                            {
                                if (item.IsSelected)
                                {
                                    if (item.InvoiceId != 0) //Cash Receipt for invoice
                                    {
                                        //Change Invoice status
                                        if (item.AmountDue == 0)
                                        {
                                            InvoiceHelper.ChangeInvStatus(item.InvoiceId, InvStatusEnum.Paid, true);
                                        }
                                        else if (item.CollectedAmount != 0 && item.AmountDue != 0)
                                        {
                                            InvoiceHelper.ChangeInvStatus(item.InvoiceId, InvStatusEnum.PartiallyPaid, true);
                                        }
                                    }
                                    else //Cash Receipt for Agent Note
                                    {
                                        //Change Agent Note status
                                        if (item.AmountDue == 0)
                                        {
                                            AgentNoteHelper.ChangeAgNoteStatus(item.AgentNoteId, InvStatusEnum.Paid);
                                        }
                                        else if (item.CollectedAmount != 0 && item.AmountDue != 0)
                                        {
                                            AgentNoteHelper.ChangeAgNoteStatus(item.AgentNoteId, InvStatusEnum.PartiallyPaid);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    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 #11
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 #12
0
        public static string AddEditQuotation(QuotationVm quoteVm)
        {
            string                    isSaved = "true";
            int                       quoteId = quoteVm.QuoteId;
            OperationsEntities        db      = new OperationsEntities();
            Quotation                 quotationDb;
            List <QuotationContainer> quotationContListDb;

            if (quoteId == 0)
            {
                quotationDb         = new Quotation();
                quotationContListDb = new List <QuotationContainer>();
            }
            else
            {
                quotationDb = db.Quotations.Include("QuotationContainers")
                              .Where(x => x.QuoteId == quoteId).FirstOrDefault();
                quotationContListDb = quotationDb.QuotationContainers.ToList();

                //Get quotContainers Ids sent from the screen
                List <int> containerVmIds = quoteVm.QuotationContainers.Select(x => x.ContainerTypeId.Value).ToList();
                var        containerDel   = quotationContListDb.Where(x => !containerVmIds.Contains(x.ContainerTypeId)).ToList();

                foreach (var item in containerDel)
                {
                    db.QuotationContainers.Remove(item);
                }
            }

            Mapper.CreateMap <QuotationVm, Quotation>().IgnoreAllNonExisting();
            Mapper.CreateMap <QuotationContainerVm, QuotationContainer>().IgnoreAllNonExisting();
            Mapper.Map(quoteVm, quotationDb);



            if (quoteId == 0)
            {
                if (quotationDb.OrderFrom == 1)
                {
                    quotationDb.QuoteCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.QuoteExport, true);
                }
                else
                {
                    quotationDb.QuoteCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.QuoteImport, true);
                }

                db.Quotations.Add(quotationDb);
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.Message;
            }

            return(isSaved);
        }
Example #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="operId"></param>
        /// <param name="invId"></param>
        /// <param name="invFor">if = 1  Invoice for Carrier .. if 2 invoice for Contractor .. if 3 custom clearance</param>
        /// <param name="forEdit"></param>
        /// <returns></returns>
        public static InvoiceVm GetInvoiceInfoFullOperation(int operId = 0, int invId = 0, byte invFor = 1, bool forEdit = true)
        {
            InvoiceVm          invoiceVm = new InvoiceVm(invFor);
            AccountingEntities db        = new AccountingEntities();

            if (invId != 0)
            {
                InvoiceAP invDb = db.InvoiceAPs.Include("InvoiceDetailAPs").Include("InvoiceTotalAPs")
                                  .Where(x => x.InvoiceId == invId && x.IsDeleted == null).FirstOrDefault();
                Mapper.CreateMap <InvoiceAP, InvoiceVm>()
                .IgnoreAllNonExisting();

                Mapper.Map(invDb, invoiceVm);

                Mapper.CreateMap <InvoiceDetailAP, InvoiceDetailVm>().IgnoreAllNonExisting();
                Mapper.CreateMap <InvoiceTotalAP, InvoiceTotalVm>().IgnoreAllNonExisting();

                InvoiceDetailVm invDetVm;
                foreach (var item in invDb.InvoiceDetailAPs)
                {
                    invDetVm = new InvoiceDetailVm();
                    Mapper.Map(item, invDetVm);
                    invoiceVm.InvoiceDetails.Add(invDetVm);
                }

                InvoiceTotalVm invTotalVm;
                foreach (var item in invDb.InvoiceTotalAPs)
                {
                    invTotalVm = new InvoiceTotalVm();
                    Mapper.Map(item, invTotalVm);
                    invoiceVm.InvoiceTotals.Add(invTotalVm);
                }


                invoiceVm.PaymentTermName = invDb.PaymentTerm.PaymentTermEn;
                invoiceVm.CurrencySign    = invDb.Currency.CurrencySign;
            }
            else
            {
                invoiceVm.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.APInvoice, false);
            }

            if (operId == 0)
            {
                operId = invoiceVm.OperationId;
            }

            OperationsEntities db1 = new OperationsEntities();
            var hbInfo             = db1.OperationViews.Where(x => x.OperationId == operId).FirstOrDefault();

            Mapper.CreateMap <OperationView, InvoiceVm>()
            .ForMember(x => x.CreateBy, y => y.Ignore())
            .ForMember(x => x.CreateDate, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(hbInfo, invoiceVm);

            if (invFor == 1) //Carrier
            {
                invoiceVm.ContractorId = null;
                invoiceVm.SupplierName = hbInfo.CarrierNameEn;
                invoiceVm.CustomerName = hbInfo.CarrierNameEn;
            }
            else if (invFor == 2)
            {
                var contractorInfo = db1.TruckingOrdersViews
                                     .Where(x => x.OperationId == operId)
                                     .Select(x => new { x.ContractorId, x.ContractorNameEn })
                                     .FirstOrDefault();
                if (contractorInfo != null)
                {
                    invoiceVm.CarrierId    = null;
                    invoiceVm.SupplierName = contractorInfo.ContractorNameEn;
                    invoiceVm.ContractorId = contractorInfo.ContractorId;
                    invoiceVm.CustomerName = contractorInfo.ContractorNameEn;
                }
            }
            else if (invFor == 3)
            {
                invoiceVm.SupplierName = "Custom Clearance";
                invoiceVm.CustomerName = "Custom Clearance";
            }

            //Get Cost list
            var operCostObj = AccountingHelper.GetOperationCost(invoiceVm.OperationId, 0);

            var operCostList      = operCostObj.OperationCostAccVms;
            var operCostTotalList = operCostObj.OperationCostTotalAccVms;

            // GetHbInvTotal(0, ref operCostList, ref operCostTotalList);

            invoiceVm.OperationCostAccVms      = operCostList;
            invoiceVm.OperationCostTotalAccVms = operCostTotalList;

            if (forEdit)
            {
                //Get prev invoice details for this HB
                var prevInvDetails = db.InvoiceDetailAPs
                                     .Where(x => x.InvoiceAP.OperationId == operId && x.InvoiceAP.IsDeleted == null)
                                     .Select(x => new { x.CostFkId, x.FkType })
                                     .ToList();

                InvoiceDetailVm invDetailVm;
                //invFor == 1 ..get operation cost only
                //invFor == 2 ..get trucking cost only
                //invFor == 3 ..get custom clearance cost only
                List <int> usedCost = invoiceVm.InvoiceDetails.Where(x => x.FkType == invFor).Select(x => x.CostFkId).ToList();
                usedCost.AddRange(prevInvDetails.Where(x => x.FkType == invFor).Select(x => x.CostFkId).ToList());
                var newCosts = operCostList.Where(x => !usedCost.Contains(x.CostFkId) && x.FkType == invFor).ToList();

                foreach (var item in newCosts.Where(x => x.NetRate > 0))
                {
                    invDetailVm = new InvoiceDetailVm()
                    {
                        CostFkId       = item.CostFkId,
                        CostName       = item.CostName,
                        ExchangeRate   = 1,
                        FkType         = item.FkType,
                        InvDetailId    = 0,
                        InvoiceAmount  = item.NetRate,
                        InvoiceId      = invoiceVm.InvoiceId,
                        MainAmount     = item.NetRate,
                        MainCurrencyId = item.CurrencyId,
                        MainCurrSign   = item.CurrencySign
                    };

                    invoiceVm.InvoiceDetails.Add(invDetailVm);
                }

                if (invoiceVm.InvoiceTotals.Count == 0)
                {
                    InvoiceTotalVm invTotal = new InvoiceTotalVm()
                    {
                        CurrencyId   = invoiceVm.InvCurrencyId,
                        CurrencySign = invoiceVm.CurrencySign,
                        InvoiceId    = invoiceVm.InvoiceId,
                        TotalAmount  = 0
                    };

                    invoiceVm.InvoiceTotals.Add(invTotal);
                }
            }
            else
            {
                Dictionary <int, string> operCost  = new Dictionary <int, string>();
                Dictionary <int, string> truckCost = new Dictionary <int, string>();
                Dictionary <int, string> ccCost    = new Dictionary <int, string>();
                if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.OperationCost))
                {
                    operCost = ListCommonHelper.GetOperationCostList();
                }
                else if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.TruckingCost))
                {
                    truckCost = ListCommonHelper.GetTruckingCostNameList();
                }
                else if (invoiceVm.InvoiceDetails.Any(x => x.FkType == (byte)CostType.CCCost))
                {
                    ccCost = ListCommonHelper.GetCustClearCostList();
                }
                //Get cost name for invoice items
                foreach (var item in invoiceVm.InvoiceDetails)
                {
                    item.CostName = invoiceVm.OperationCostAccVms
                                    .Where(x => x.CostFkId == item.CostFkId && x.FkType == item.FkType)
                                    .FirstOrDefault().CostName;
                }
            }

            return(invoiceVm);
        }
Example #14
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 #15
0
        /*
         * internal static string DeleteCashOutReceipt(int receiptId, int invID, string deleteReason)
         * {
         *
         *  string isSaved = "false";
         *
         *  if (receiptId == 0)
         *      return isSaved;
         *
         *  AccountingEntities db = new AccountingEntities();
         *
         *  CashOutReceipt cashOutDb = new CashOutReceipt();
         *  cashOutDb = db.CashOutReceipts
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *  CashOutReceiptCheck cashOutCheckDb = new CashOutReceiptCheck();
         *  cashOutCheckDb = db.CashOutReceiptChecks
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *  CashOutReceiptInv cashOutInvkDb = new CashOutReceiptInv();
         *  cashOutInvkDb = db.CashOutReceiptInvs
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *
         *
         *  int? transID = null;
         *  if (cashOutDb.TransId != null)
         *      transID = cashOutDb.TransId.Value;
         *  cashOutDb.IsDeleted = true;
         *  cashOutDb.DeleteReason = deleteReason;
         *  cashOutDb.DeletedBy = EasyFreight.DAL.AdminHelper.GetCurrentUserId();
         *  cashOutDb.TransId = null;
         *
         *
         *  if (cashOutCheckDb != null)
         *      db.CashOutReceiptChecks.Remove(cashOutCheckDb);
         *
         *  if (cashOutInvkDb != null)
         *      db.CashOutReceiptInvs.Remove(cashOutInvkDb);
         *
         *  try
         *  {
         *      db.SaveChanges();
         *
         *      if (transID.HasValue)
         *          AccountingHelper.DeleteTransaction(transID.Value);
         *
         *      List<CashInInvoiceVm> cashOutVmObj = GetCashInvList(invID);
         *
         *      if (invID != 0)
         *      {
         *          if (cashOutVmObj.Count == 0)
         *              InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Approved, true);
         *          else
         *          {
         *
         *              InvoiceAP invDb = db.InvoiceAPs.Where(x => x.InvoiceId == invID).FirstOrDefault();
         *
         *              decimal InvoiceTotals = invDb.InvoiceTotalAPs.Sum(s => s.TotalAmount);
         *
         *              decimal? totalPaidAmount = cashOutVmObj.Sum(s => s.PaidAmount);
         *
         *              if (totalPaidAmount == null)
         *                  InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Approved, true);
         *              else
         *              {
         *                  if (totalPaidAmount == InvoiceTotals)
         *                      InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Paid, true);
         *                  if (totalPaidAmount < InvoiceTotals)
         *                      InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.PartiallyPaid, true);
         *              }
         *          }
         *      }
         *
         *      isSaved = "true";
         *  }
         *  catch
         *  {
         *      isSaved = "false";
         *  }
         *
         *
         *
         *  return isSaved;
         *
         * }
         */

        internal static string AddEditOpenCashReceipt(CashInVm cashInVmObj, out int savedReceiptId, bool addToTrans = true)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();



            int receiptId = cashInVmObj.ReceiptId;

            savedReceiptId = receiptId;



            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptChecks")
                            .Include("CashOutReceiptInvs")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete invoice list .. will insert it again
                var invList = cashDbObj.CashOutReceiptInvs.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptInvs.Remove(item);
                }

                //Delete check list .. will insert it again
                var checkList = cashDbObj.CashOutReceiptChecks.ToList();
                foreach (var item in checkList)
                {
                    cashDbObj.CashOutReceiptChecks.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(cashInVmObj, cashDbObj);

            CashOutReceiptCheck cashCheckDb;

            foreach (var item in cashInVmObj.CashInReceiptChecks)
            {
                if (!string.IsNullOrEmpty(item.CheckNumber))
                {
                    cashCheckDb = new CashOutReceiptCheck();
                    Mapper.CreateMap <CashInCheckVm, CashOutReceiptCheck>().IgnoreAllNonExisting();
                    Mapper.Map(item, cashCheckDb);

                    cashDbObj.CashOutReceiptChecks.Add(cashCheckDb);
                }
            }

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    savedReceiptId = cashInVmObj.ReceiptId;

                    #region Add To Transaction Table
                    if (addToTrans)
                    {
                        string creditAccountId = "";

                        if (cashInVmObj.ShipperId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ShipperId.Value, "Shipper", "ShipperId");
                        }
                        if (cashInVmObj.CarrierId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.CarrierId.Value, "Carrier", "CarrierId");
                        }
                        if (cashInVmObj.ContractorId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ContractorId.Value, "Contractor", "ContractorId");
                        }
                        if (cashInVmObj.AgentId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.AgentId.Value, "Agent", "AgentId");
                        }
                        if (cashInVmObj.ConsigneeId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ConsigneeId.Value, "Consignee", "ConsigneeId");
                        }

                        //AccTransaction accTran = new AccTransaction()
                        //{
                        //    CreateBy = AdminHelper.GetCurrentUserId(),
                        //    CreateDate = DateTime.Now,
                        //    TransactionName = "pay open balance"
                        //};

                        //AccTransactionDetail accTranDetail = new AccTransactionDetail();

                        //accTranDetail.AccountId = creditAccountId;
                        //accTranDetail.DebitAmount = cashInVmObj.ReceiptAmount.Value;
                        //accTranDetail.CurrencyId = cashInVmObj.CurrencyId;

                        //accTran.AccTransactionDetails.Add(accTranDetail);

                        //db.AccTransactions.Add(accTran);

                        //db.SaveChanges();

                        AddReceiptToTransTable(creditAccountId, cashInVmObj, true);
                    }

                    #endregion

                    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 #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hbId"></param>
        /// <param name="operId"></param>
        /// <param name="invId"></param>
        /// <param name="invType">0 all currency, 1 EGP only, 2 other currency</param>
        /// <returns></returns>
        public static InvoiceVm GetInvoiceInfo(int hbId = 0, int operId = 0, int invId = 0, byte invType = 0)
        {
            InvoiceVm          invoiceVm = new InvoiceVm(invType);
            OperationsEntities db1       = new OperationsEntities();
            var hbInfo = db1.HouseBillViews.Where(x => x.HouseBillId == hbId).FirstOrDefault();

            //Get saved invoice for HB with same invoice type .. if any
            AccountingEntities db = new AccountingEntities();
            var savedInv          = db.Invoices.Where(x => x.HouseBillId == hbId && x.InvoiceType == invType).FirstOrDefault();

            if (savedInv != null)
            {
                invId = savedInv.InvoiceId;
            }

            //If invoice id != 0 ... will fill InvoiceVm from Invoice table
            if (invId != 0)
            {
                Invoice invDb = db.Invoices.Where(x => x.InvoiceId == invId).FirstOrDefault();
                Mapper.CreateMap <Invoice, InvoiceVm>().IgnoreAllNonExisting();
                Mapper.CreateMap <InvoiceTotal, InvoiceTotalVm>().IgnoreAllNonExisting();
                Mapper.Map(invDb, invoiceVm);

                invoiceVm.FromPort        = hbInfo.FromPort;
                invoiceVm.ToPort          = hbInfo.ToPort;
                invoiceVm.PaymentTermName = invDb.PaymentTerm.PaymentTermEn;
                invType = invDb.InvoiceType;
            }
            else
            {
                invoiceVm.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AccountingInvoice, false);
            }

            Mapper.CreateMap <HouseBillView, InvoiceVm>()
            .ForMember(x => x.CreateBy, y => y.Ignore())
            .ForMember(x => x.CreateDate, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(hbInfo, invoiceVm);

            if (hbInfo.OrderFrom == 1)
            {
                invoiceVm.CustomerName = hbInfo.ShipperNameEn;
                invoiceVm.ConsigneeId  = null;
            }
            else
            {
                invoiceVm.CustomerName = hbInfo.ConsigneeNameEn;
                invoiceVm.ShipperId    = null;
            }

            //Get Cost list
            var operCostObj = AccountingHelper.GetOperationCost(invoiceVm.OperationId, invoiceVm.HouseBillId.Value);

            var operCostList      = operCostObj.OperationCostAccVms;
            var operCostTotalList = operCostObj.OperationCostTotalAccVms;

            GetHbInvTotal(invType, ref operCostList, ref operCostTotalList);

            invoiceVm.OperationCostAccVms      = operCostList;
            invoiceVm.OperationCostTotalAccVms = operCostTotalList;

            return(invoiceVm);
        }
        internal static string AddEditExpenseCashReceipt(CashInVm cashInVmObj)
        {
            string isSaved = "true";

            AccountingEntities db    = new AccountingEntities();
            int            receiptId = cashInVmObj.ReceiptId;
            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptExpenses")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete expenses list .. will insert it again
                var invList = cashDbObj.CashOutReceiptExpenses.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptExpenses.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore());

            Mapper.CreateMap <CashOutExpense, CashOutReceiptExpense>();
            Mapper.Map(cashInVmObj, cashDbObj);

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    AddExpenseReceiptToTrans(cashInVmObj);

                    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 #18
0
        public static CashInVm GetCashReceiptInfo(int invId, int agNoteId = 0, int cashReceiptId     = 0,
                                                  int operationId         = 0, decimal receiptAmount = 0)
        {
            CashInVm cashVm;

            if (cashReceiptId != 0)
            {
                cashVm = FillCashVmForReceiptView(cashReceiptId);
                return(cashVm);
            }

            AccountingEntities db = new AccountingEntities();

            if (invId != 0) //Invoice
            {
                cashVm = db.APInvoiceViews.Where(x => x.InvoiceId == invId)
                         .Select(x => new CashInVm
                {
                    CarrierId    = x.CarrierId,
                    ContractorId = x.ContractorId,
                    CustomerName = x.InvoiceType == 1 ? x.CarrierNameEn : x.ContractorNameEn,
                    OrderFrom    = x.OrderFrom,
                    InvoiceType  = x.InvoiceType
                }).FirstOrDefault();
            }
            else if (operationId != 0) //operationId != 0 .. so it will be cash deposit for CC
            {
                OperationsEntities db1 = new OperationsEntities();
                cashVm = db1.Operations.Where(x => x.OperationId == operationId).Select(x => new CashInVm
                {
                    OperationId   = x.OperationId,
                    OperationCode = x.OperationCode,
                    Notes         = "Custom Clerance Cash Deposit For Operation " + x.OperationCode
                }).FirstOrDefault();

                if (receiptAmount != 0)
                {
                    cashVm.ReceiptAmount = receiptAmount;
                    cashVm.PaymentTermId = 1;
                }


                cashVm.CCCashDepositVmList = GetCCCashDepositList(operationId);
            }
            else //invId == 0 .. so agNoteId will not be 0 .. collect agent note
            {
                cashVm = db.AgentNoteViews.Where(x => x.AgentNoteId == agNoteId)
                         .Select(x => new CashInVm
                {
                    AgentId      = x.AgentId,
                    CustomerName = x.AgentNameEn,
                    OrderFrom    = x.OrderFrom
                }).FirstOrDefault();
            }


            cashVm.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, false);
            CashInCheckVm cashCheckVm = new CashInCheckVm();

            cashVm.CashInReceiptChecks.Add(cashCheckVm);

            if (agNoteId != 0) //Cash Receipt for Agent Note
            {
                FillCashVmForAgNote(agNoteId, ref cashVm);
                cashVm.CashType = "cashout";
                return(cashVm);
            }

            if (operationId != 0) //operationId != 0 .. so it will be cash deposit for CC
            {
                cashVm.CashType = "cashout";
                return(cashVm);
            }
            //Cash Receipt for Invoice

            //Get invoice list for this customer
            List <CashInInvoiceVm> cashInvList = new List <CashInInvoiceVm>();
            List <APInvoiceView>   invList     = new List <APInvoiceView>();

            if (cashVm.InvoiceType == 1)
            {
                invList = db.APInvoiceViews
                          .Where(x => x.CarrierId == cashVm.CarrierId && (x.InvStatusId != 4 && x.InvStatusId != 1)).ToList();
            }
            else if (cashVm.InvoiceType == 2)
            {
                invList = db.APInvoiceViews
                          .Where(x => x.ContractorId == cashVm.ContractorId && (x.InvStatusId != 4 && x.InvStatusId != 1)).ToList();
            }
            else if (cashVm.InvoiceType == 3) // Cutom clearance .. will get all not paid invoices
            //as there is only one grouped credit account for Cutom clearance suppliers .. 22/11/2016
            {
                invList = db.APInvoiceViews
                          .Where(x => x.InvoiceType == 3 && (x.InvStatusId != 4 && x.InvStatusId != 1)).ToList();
            }

            Mapper.CreateMap <APInvoiceView, CashInInvoiceVm>().IgnoreAllNonExisting();
            Mapper.Map(invList, cashInvList);


            cashVm.CashInReceiptInvs = cashInvList;
            List <int> invIds    = cashInvList.Select(x => x.InvoiceId).ToList();
            var        cashInvDb = db.CashOutReceiptInvs
                                   .Where(x => invIds.Contains(x.InvoiceId))
                                   .GroupBy(x => x.InvoiceId)
                                   .ToList();

            foreach (var item in cashVm.CashInReceiptInvs)
            {
                item.CollectedAmount = cashInvDb.Where(x => x.Key == item.InvoiceId)
                                       .Select(x => x.Sum(y => y.PaidAmount)).FirstOrDefault();
                item.AmountDue = item.TotalAmount - item.CollectedAmount;
            }


            cashVm.CashType = "cashout";

            return(cashVm);
        }