Example #1
0
        // GET: Invoices/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var invoice = _repository.GetInvoice(id);

            if (invoice == null)
            {
                return(HttpNotFound());
            }

            InvoiceDetailVm detailVm = new InvoiceDetailVm()
            {
                Id          = invoice.Id,
                InvoiceDate = invoice.Invoice_date,
                DueDate     = new DateTime(DateTime.Now.Year, DateTime.Now.Month, invoice.Due_date),
                Paid        = invoice.Paid.ToString(),
                TotalPrice  = invoice.TotalPrice,
                InvoiceItem = invoice.InvoiceItem
            };

            return(View(detailVm));
        }
Example #2
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 #3
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);
        }