Example #1
0
        internal static List <InvoiceLightVm> GetInvListForHb(int hbId)
        {
            AccountingEntities db2 = new AccountingEntities();

            var invList = db2.Invoices.Where(x => x.HouseBillId == hbId)
                          .Select(x => new InvoiceLightVm
            {
                InvoiceCode = x.InvoiceCode,
                InvoiceId   = x.InvoiceId,
                InvoiceDate = x.InvoiceDate,
                InvoiceType = x.InvoiceType,
                HouseBillId = x.HouseBillId
            })
                          .ToList();

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

            var operCostList = operCostObj.OperationCostAccVms;


            foreach (var item in invList)
            {
                var operCostTotalList = operCostObj.OperationCostTotalAccVms;
                GetHbInvTotal(item.InvoiceType, ref operCostList, ref operCostTotalList);
                item.OperationCostTotalAccVms = operCostTotalList;
            }
            return(invList);
        }
Example #2
0
 public static void GetCCCashDepositCostList(int operationId)
 {
     var operCostObj                 = AccountingHelper.GetOperationCost(operationId, 0);
     var operCostList                = operCostObj.OperationCostAccVms;
     var operCostTotalList           = operCostObj.OperationCostTotalAccVms;
     Dictionary <int, string> ccCost = ListCommonHelper.GetCustClearCostList();
 }
Example #3
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 #4
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 #5
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);
        }
Example #6
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);
        }