public static MyInvoice NewInvoice2(string orderIds, string customerBillingScheme, int billingTypeId, string billingTypeValue, string billingTypeLabel, int timeTypeId, string timeTypeValue, string timeTypeLabel, double discount) { TugDataModel.TugDataEntities db = new TugDataModel.TugDataEntities(); double grandTotal = 0.0; MyInvoice _invoice = new MyInvoice(); //_invoice.OrderID = orderId; _invoice.OrderIDs = orderIds; List<string> strOrderIDs = orderIds.Split(',').ToList(); List<int> iOrderIDs = new List<int>(); if (strOrderIDs != null) { foreach (var item in strOrderIDs) { iOrderIDs.Add(TugBusinessLogic.Module.Util.toint(item)); } } //_invoice.OrderCode = ; //_invoice int billingTemplateId = Convert.ToInt32(customerBillingScheme.Split('%')[0].Split('~')[0]); List<MyBillingItem> listBillingItemTemplate = GetCustomerBillSchemeItems(billingTemplateId); _invoice.BillingTemplateID = billingTemplateId; _invoice.BillingTypeID = billingTypeId; _invoice.BillingTypeValue = billingTypeValue; _invoice.BillingTypeLabel = billingTypeLabel; _invoice.TimeTypeID = timeTypeId; _invoice.TimeTypeValue = timeTypeValue; _invoice.TimeTypeLabel = timeTypeLabel; _invoice.Discount = discount; _invoice.BillingCode = TugBusinessLogic.Utils.AutoGenerateBillCode(); //var list = db.V_OrderScheduler.Where(u => u.OrderID == orderId).OrderBy(u => u.OrderID).Select(u => u); //var list = db.V_OrderScheduler.Where(u => (iOrderIDs.Contains((int)u.OrderID) && u.HasBilling == "否" && u.HasBillingInFlow == "否") // || (iOrderIDs.Contains((int)u.OrderID) && u.HasBilling == "是" && u.HasBillingInFlow == "否" && (u.BillingType == 0 || u.BillingType == null))).OrderBy(u => u.OrderID).Select(u => u); var list = db.V_OrderScheduler.Where(u => (iOrderIDs.Contains((int)u.OrderID) && u.HasBilling == "否" && u.HasBillingInFlow == "否") || (iOrderIDs.Contains((int)u.OrderID) && u.HasBilling == "是" && u.HasBillingInFlow == "否" && (u.BillingType == 0 || u.BillingType == null))) .OrderBy(u => u.ServiceWorkDate).ThenBy(u => u.DepartBaseTime).ThenBy(u => u.ServiceNatureLabel).Select(u => u); var services = list.Select(u => new {u.OrderServiceID, u.ServiceNatureID, u.ServiceNatureLabel, u.ServiceWorkDate, u.ServiceWorkPlace }) .Distinct().ToList().OrderBy(u => u.ServiceWorkDate).ThenBy(u => u.ServiceNatureLabel); if (services != null) { Dictionary<int, MyService> dicService = new Dictionary<int, MyService>(); Dictionary<int, List<MyScheduler>> dicScheduler = new Dictionary<int, List<MyScheduler>>(); foreach (var service in services) { MyService ms = new MyService(); ms.OrderServicId = (int)service.OrderServiceID; ms.ServiceId = (int)service.ServiceNatureID; ms.ServiceName = service.ServiceNatureLabel; ms.ServiceWorkDate = service.ServiceWorkDate; ms.ServiceWorkPlace = service.ServiceWorkPlace; dicService.Add(ms.OrderServicId, ms); var schedulers = list.Where(u => u.OrderServiceID == (int)service.OrderServiceID) .Select(u => new { u.IDX, u.ServiceNatureID, u.ServiceWorkDate, u.ServiceNatureLabel, u.TugID, u.TugName1, u.TugName2, u.DepartBaseTime, u.ArrivalBaseTime, u.RopeUsed, u.RopeNum, u.Remark }).ToList().OrderBy(u => u.ServiceWorkDate).ThenBy(u => u.DepartBaseTime).ThenBy(u => u.ServiceNatureLabel); if (schedulers != null) { List<MyScheduler> lstScheduler = new List<MyScheduler>(); foreach (var scheduler in schedulers) { MyScheduler mySch = new MyScheduler(); mySch.SchedulerID = scheduler.IDX; mySch.ServiceNatureID = (int)scheduler.ServiceNatureID; mySch.TugID = (int)scheduler.TugID; mySch.TugCnName = scheduler.TugName1; //mySch.TugEnName = scheduler.TugName2; //mySch.TugSimpleName = scheduler.TugSimpleName; //mySch.TugPower = scheduler.Power; mySch.DepartBaseTime = scheduler.DepartBaseTime; mySch.ArrivalBaseTime = scheduler.ArrivalBaseTime; int iDiffHour, iDiffMinute; TugBusinessLogic.Utils.CalculateTimeDiff(mySch.DepartBaseTime, mySch.ArrivalBaseTime, out iDiffHour, out iDiffMinute); mySch.WorkTime = iDiffHour.ToString() + "h." + iDiffMinute.ToString("D2") + "m."; mySch.WorkTimeConsumption = TugBusinessLogic.Utils.CalculateTimeConsumption2(iDiffHour, iDiffMinute, timeTypeId, timeTypeValue, timeTypeLabel); mySch.RopeUsed = scheduler.RopeUsed; mySch.RopeNum = (int)scheduler.RopeNum; //mySch.Remark = scheduler.Remark; mySch.UnitPriceOfFeulFee = GetFuelFee(ms.ServiceWorkDate); mySch.DiscoutPriceOfFeulFee = GetDiscoutPriceOfFuelFee(_invoice.BillingTemplateID); mySch.PriceOfFeulFee = CalculateFuelFee(ms.ServiceWorkDate, mySch.WorkTimeConsumption); //打折前+mySch.DiscoutPriceOfFeulFee; #region 全包 if (_invoice.BillingTypeID == 6 || _invoice.BillingTypeValue == "0" || _invoice.BillingTypeLabel == "全包") { MyBillingItem tmp = listBillingItemTemplate.FirstOrDefault(u => u.ItemID == service.ServiceNatureID); if (tmp != null) { mySch.UnitPrice = mySch.Price = mySch.SubTotaHKS = (double)tmp.UnitPrice; } else { mySch.UnitPrice = mySch.Price = mySch.SubTotaHKS = 0; } mySch.DiscountSubTotalHKS = Math.Round(mySch.SubTotaHKS, 2); mySch.TotalHKs = mySch.DiscountSubTotalHKS; grandTotal += mySch.TotalHKs; } #endregion #region 半包 if (_invoice.BillingTypeID == 7 || _invoice.BillingTypeValue == "1" || _invoice.BillingTypeLabel == "半包") { double discount_price = 0.0, top_total_price = 0.0, mid_total_price = 0.0, bottom_total_price = 0.0; MyBillingItem tmp = listBillingItemTemplate.FirstOrDefault(u => u.ItemID == service.ServiceNatureID); if (tmp != null) { mySch.UnitPrice = mySch.Price = (double)tmp.UnitPrice; } else { mySch.UnitPrice = mySch.Price = 0; } top_total_price += mySch.Price; //List<CustomField> banbaoShowItems = GetBanBaoShowItems(); List<MyCustomField> banbaoShowItems = GetBanBaoShowItems(); List<MyBillingItem> lstMyBillingItems = new List<MyBillingItem>(); #region 条目费用计算 if (banbaoShowItems != null) { foreach (MyCustomField item in banbaoShowItems) { tmp = listBillingItemTemplate.FirstOrDefault(u => u.ItemID == item.IDX); MyBillingItem mbi = new MyBillingItem(); if (tmp == null) { if (item.IDX == 16 || item.CustomValue == "B10" || item.CustomLabel == "25%港外附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.25, 2); } else if (item.IDX == 17 || item.CustomValue == "B11" || item.CustomLabel == "50% 18时至22时附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.5, 2); } else if (item.IDX == 18 || item.CustomValue == "B12" || item.CustomLabel == "100% 22时至08时附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 19 || item.CustomValue == "B13" || item.CustomLabel == "100%假日附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 20 || item.CustomValue == "B14" || item.CustomLabel == "100%台风附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 21 || item.CustomValue == "C15" || item.CustomLabel == "使用3600BHP以上的拖轮+15%") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.15, 2); } else if (item.IDX == 22 || item.CustomValue == "C80" || item.CustomLabel == "燃油附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = mySch.DiscoutPriceOfFeulFee + (mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee); } else if (item.IDX == 120 || item.CustomValue == "E81" || item.CustomLabel == "燃油附加费额外费用") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee; } else if (item.IDX == 121 || item.CustomValue == "E82" || item.CustomLabel == "18时至22时附加费50耗时") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = 0; //存放耗时 } else if (item.IDX == 122 || item.CustomValue == "E83" || item.CustomLabel == "22时至08时附加费100耗时") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = 0; //存放耗时 } else { mbi.Currency = "港币"; mbi.ItemID = -1; mbi.ItemValue = ""; mbi.ItemLabel = ""; mbi.UnitPrice = 0; mbi.Price = 0; } if (item.CustomValue.StartsWith("B")) { mid_total_price += (double)mbi.Price; } if (item.CustomValue.StartsWith("C")) { if (item.CustomValue.Equals("C78")) //折扣 discount_price += (double)mbi.Price; else if (item.CustomValue.Equals("C82")) //拖缆费 bottom_total_price += (double)mbi.Price; //else if (item.CustomValue.Equals("E81")) //燃油附加费额外费用 // bottom_total_price += (double)mbi.Price; } } else { mbi.Currency = tmp.Currency; mbi.ItemID = tmp.ItemID; mbi.ItemValue = tmp.ItemValue; mbi.ItemLabel = tmp.ItemLabel; mbi.UnitPrice = tmp.UnitPrice; if (tmp.ItemID == 23 || tmp.ItemValue == "C82" || tmp.ItemLabel == "拖缆费") { mbi.Price = tmp.UnitPrice * mySch.RopeNum; } else if (tmp.ItemID == 40 || tmp.ItemValue == "C78" || tmp.ItemLabel == "折扣") { mbi.Price = tmp.UnitPrice; mySch.DiscoutPrice = (double)tmp.UnitPrice; } else if (tmp.ItemID == 119 || tmp.ItemValue == "E80" || tmp.ItemLabel == "燃油附加费折扣") { mbi.Price = tmp.UnitPrice; } else { mbi.Price = tmp.UnitPrice; } if (item.CustomValue.StartsWith("B")) { mid_total_price += (double)mbi.Price; } if (item.CustomValue.StartsWith("C")) { if (item.CustomValue.Equals("C78")) //折扣 discount_price += (double)mbi.Price; else if (item.CustomValue.Equals("C82")) //拖缆费 bottom_total_price += (double)mbi.Price; //else if (item.CustomValue.Equals("E81")) //燃油附加费额外费用 // bottom_total_price += (double)mbi.Price; } mbi.TypeID = tmp.TypeID; mbi.TypeValue = tmp.TypeValue; mbi.TypeLabel = tmp.TypeLabel; } lstMyBillingItems.Add(mbi); } } #endregion //bottom_total_price += mySch.PriceOfFeulFee; bottom_total_price += (mySch.UnitPriceOfFeulFee + mySch.DiscoutPriceOfFeulFee) + (mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee); mySch.BillingItems = lstMyBillingItems; mySch.SubTotaHKS = top_total_price; //+mid_total_price; //mySch.DiscountSubTotalHKS = Math.Round(mySch.SubTotaHKS * _invoice.Discount, 2); mySch.DiscountSubTotalHKS = Math.Round(mySch.SubTotaHKS + mySch.DiscoutPrice, 2); mySch.TotalHKs = mySch.DiscountSubTotalHKS + bottom_total_price; grandTotal += mySch.TotalHKs; } #endregion #region 计时 if (_invoice.BillingTypeID == 8 || _invoice.BillingTypeValue == "2" || _invoice.BillingTypeLabel == "计时") { double top_total_price = 0.0, mid_total_price = 0.0, bottom_total_price = 0.0, discount_price = 0.0; MyBillingItem tmp = listBillingItemTemplate.FirstOrDefault(u => u.ItemID == service.ServiceNatureID); if (tmp != null) { mySch.UnitPrice = (double)tmp.UnitPrice; mySch.Price = mySch.UnitPrice * mySch.WorkTimeConsumption; } else { mySch.UnitPrice = mySch.Price = 0; } top_total_price += mySch.Price; //List<CustomField> banbaoShowItems = GetTiaoKuanShowItems(); List<MyCustomField> banbaoShowItems = GetTiaoKuanShowItems(); List<MyBillingItem> lstMyBillingItems = new List<MyBillingItem>(); #region 条目费用计算 if (banbaoShowItems != null) { foreach (MyCustomField item in banbaoShowItems) { tmp = listBillingItemTemplate.FirstOrDefault(u => u.ItemID == item.IDX); MyBillingItem mbi = new MyBillingItem(); if (tmp == null) { if (item.IDX == 16 || item.CustomValue == "B10" || item.CustomLabel == "25%港外附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.25, 2); } else if (item.IDX == 17 || item.CustomValue == "B11" || item.CustomLabel == "50% 18时至22时附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.5, 2); } else if (item.IDX == 18 || item.CustomValue == "B12" || item.CustomLabel == "100% 22时至08时附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 19 || item.CustomValue == "B13" || item.CustomLabel == "100%假日附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 20 || item.CustomValue == "B14" || item.CustomLabel == "100%台风附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price, 2); } else if (item.IDX == 21 || item.CustomValue == "C15" || item.CustomLabel == "使用3600BHP以上的拖轮+15%") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = Math.Round(mySch.Price * 0.15, 2); } else if (item.IDX == 22 || item.CustomValue == "C80" || item.CustomLabel == "燃油附加费") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = mySch.DiscoutPriceOfFeulFee + (mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee); } else if (item.IDX == 120 || item.CustomValue == "E81" || item.CustomLabel == "燃油附加费额外费用") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee; } else if (item.IDX == 121 || item.CustomValue == "E82" || item.CustomLabel == "18时至22时附加费50耗时") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = 0; //存放耗时 } else if (item.IDX == 122 || item.CustomValue == "E83" || item.CustomLabel == "22时至08时附加费100耗时") { mbi.Currency = "港币"; mbi.ItemID = item.IDX; mbi.ItemValue = item.CustomValue; mbi.ItemLabel = item.CustomLabel; mbi.UnitPrice = mbi.Price = 0; //存放耗时 } else { mbi.Currency = "港币"; mbi.ItemID = -1; mbi.ItemValue = ""; mbi.ItemLabel = ""; mbi.UnitPrice = 0; mbi.Price = 0; } if (item.CustomValue.StartsWith("B")) { mid_total_price += (double)mbi.Price; } if (item.CustomValue.StartsWith("C")) { if (item.CustomValue.Equals("C78")) //折扣 discount_price += (double)mbi.Price; else if (item.CustomValue.Equals("C82")) //拖缆费 bottom_total_price += (double)mbi.Price; } } else { mbi.Currency = tmp.Currency; mbi.ItemID = tmp.ItemID; mbi.ItemValue = tmp.ItemValue; mbi.ItemLabel = tmp.ItemLabel; mbi.UnitPrice = tmp.UnitPrice; if (tmp.ItemID == 23 || tmp.ItemValue == "C82" || tmp.ItemLabel == "拖缆费") { mbi.Price = tmp.UnitPrice * mySch.RopeNum; } else if (tmp.ItemID == 40 || tmp.ItemValue == "C78" || tmp.ItemLabel == "折扣") { mbi.Price = tmp.UnitPrice; mySch.DiscoutPrice = (double)tmp.UnitPrice; } else if (tmp.ItemID == 119 || tmp.ItemValue == "E80" || tmp.ItemLabel == "燃油附加费折扣") { mbi.Price = tmp.UnitPrice; } else { mbi.Price = tmp.UnitPrice; } if (item.CustomValue.StartsWith("B")) { mid_total_price += (double)mbi.Price; } if (item.CustomValue.StartsWith("C")) { if (item.CustomValue.Equals("C78")) //折扣 discount_price += (double)mbi.Price; else if (item.CustomValue.Equals("C82")) //拖缆费 bottom_total_price += (double)mbi.Price; } mbi.TypeID = tmp.TypeID; mbi.TypeValue = tmp.TypeValue; mbi.TypeLabel = tmp.TypeLabel; } lstMyBillingItems.Add(mbi); } } #endregion //bottom_total_price += mySch.PriceOfFeulFee; bottom_total_price += (mySch.UnitPriceOfFeulFee + mySch.DiscoutPriceOfFeulFee) + (mySch.PriceOfFeulFee - mySch.UnitPriceOfFeulFee); mySch.BillingItems = lstMyBillingItems; mySch.SubTotaHKS = top_total_price; //+mid_total_price; //mySch.DiscountSubTotalHKS = Math.Round(mySch.SubTotaHKS * _invoice.Discount, 2); mySch.DiscountSubTotalHKS = Math.Round(mySch.SubTotaHKS + mySch.DiscoutPrice, 2); mySch.TotalHKs = mySch.DiscountSubTotalHKS + bottom_total_price; grandTotal += mySch.TotalHKs; } #endregion lstScheduler.Add(mySch); } dicScheduler.Add((int)service.OrderServiceID, lstScheduler); } } _invoice.ServiceNature = dicService; _invoice.Schedulers = dicScheduler; } _invoice.GrandTotalHKS = grandTotal; _invoice.FinalAmount = grandTotal - (grandTotal * _invoice.Discount / 100); return _invoice; }
public static MyInvoice GenerateInvoice(int orderId, string orderDate) { TugDataModel.TugDataEntities db = new TugDataModel.TugDataEntities(); MyInvoice _invoice = new MyInvoice(); //List<V_Invoice> list = db.V_Invoice.Where(u => u.OrderID == orderId).OrderBy(u => u.ServiceNatureID).Select(u => u).ToList(); var list = db.V_Invoice.Where(u => u.OrderID == orderId).OrderBy(u => u.OrderServiceID).Select(u => u); if (list != null) { //_invoice.CustormerID = (int)list.FirstOrDefault().CustomerID; //_invoice.CustomerName = list.FirstOrDefault().CustomerName; _invoice.OrderIDs = ((int)list.FirstOrDefault().OrderID).ToString(); _invoice.JobNo = list.FirstOrDefault().JobNo; _invoice.Rmark = list.FirstOrDefault().BillingRemark; _invoice.BillingTemplateID = (int)list.FirstOrDefault().BillingTemplateID; //_invoice.OrderCode = list.FirstOrDefault().OrderCode; Dictionary<int, MyService> dicServiceNature = new Dictionary<int, MyService>(); //var services2 = list.Select(u => new {u.ServiceNatureID, u.ServiceNatureLabel}).ToList(); var services = list.Select(u => new { u.OrderServiceID, u.ServiceNatureID, u.ServiceNatureLabel, u.ServiceWorkDate, u.ServiceWorkPlace }).Distinct().ToList(); //var services = db.V_Invoice.Where(u => u.OrderID == orderId).OrderBy(u => u.ServiceNatureID).Select(u => new {u.ServiceNatureID, u.ServiceNatureLabel}).Distinct().ToList(); Dictionary<int, List<MyScheduler>> dicSchedulers = new Dictionary<int, List<MyScheduler>>(); double grandTotal = 0; if (services != null && services.Count > 0) { foreach (var item in services) { MyService ms = new MyService(); ms.ServiceId = (int)item.ServiceNatureID; ms.ServiceName = item.ServiceNatureLabel; ms.ServiceWorkDate = item.ServiceWorkDate; ms.ServiceWorkPlace = item.ServiceWorkPlace; dicServiceNature.Add(ms.ServiceId, ms); var ships = list.Where(u => u.ServiceNatureID == item.ServiceNatureID) .Select(u => new {u.SchedulerID, u.TugID, u.TugName1, u.TugName2 }).Distinct() .OrderBy(u => u.TugName1).ToList(); List<MyScheduler> listScheduler = new List<MyScheduler>(); if (ships != null && ships.Count > 0) { foreach (var ship in ships) { MyScheduler sch = new MyScheduler(); sch.TugID = (int)ship.TugID; sch.TugCnName = ship.TugName1; //sch.TugEnName = ship.TugName2; //sch.TugSimpleName = ship.TugSimpleName; //sch.TugPower = ship.Power; var schedulers = list.Where(u => u.ServiceNatureID == item.ServiceNatureID && u.SchedulerID == ship.SchedulerID) .OrderBy(u => u.OrderID).OrderBy(u => u.ServiceNatureID) .Select(u => new { u.SchedulerID, u.TugID, u.TugName1, u.TugName2, u.InformCaptainTime, u.CaptainConfirmTime, u.DepartBaseTime, u.ArrivalShipSideTime, u.WorkCommencedTime, u.WorkCompletedTime, u.ArrivalBaseTime, u.UnitPrice, u.RopeUsed, u.RopeNum, u.OrderSchedulerRemark, u.BillingItemIDX, u.ItemID, u.BillingItemValue, u.BillingItemLabel, u.Currency, u.PositionTypeID }).OrderBy(u => u.ItemID).ToList(); if (schedulers != null && schedulers.Count > 0) { sch.SchedulerID = (int)schedulers[0].SchedulerID; //sch.InformCaptainTime = schedulers[0].InformCaptainTime; //sch.CaptainConfirmTime = schedulers[0].CaptainConfirmTime; sch.DepartBaseTime = schedulers[0].DepartBaseTime; //sch.ArrivalShipSideTime = schedulers[0].ArrivalShipSideTime; //sch.WorkCommencedTime = schedulers[0].WorkCommencedTime; //sch.WorkCompletedTime = schedulers[0].WorkCompletedTime; sch.ArrivalBaseTime = schedulers[0].ArrivalBaseTime; int iDiffHour, iDiffMinute; TugBusinessLogic.Utils.CalculateTimeDiff(sch.DepartBaseTime, sch.ArrivalBaseTime, out iDiffHour, out iDiffMinute); sch.WorkTime = iDiffHour.ToString() + "h." + iDiffMinute.ToString("D2") + "m."; //_invoice.BillingTypeID = (int)list[0].BillingTypeID; //_invoice.BillingTypeValue = list[0].BillingTypeValue; //_invoice.BillingTypeLabel = list[0].BillingTypeLabel; //_invoice.TimeTypeID = (int)list[0].TimeTypeID; //_invoice.TimeTypeValue = list[0].TimeTypeValue; //_invoice.TimeTypeLabel = list[0].TimeTypeLabel; sch.WorkTimeConsumption = TugBusinessLogic.Utils.CalculateTimeConsumption2(iDiffHour, iDiffMinute, (int)list.FirstOrDefault().TimeTypeID, list.FirstOrDefault().TimeTypeValue, list.FirstOrDefault().TimeTypeLabel); sch.UnitPriceOfFeulFee = GetFuelFee(ms.ServiceWorkDate); sch.DiscoutPriceOfFeulFee = GetDiscoutPriceOfFuelFee(_invoice.BillingTemplateID); sch.PriceOfFeulFee = CalculateFuelFee(ms.ServiceWorkDate, sch.WorkTimeConsumption) + sch.DiscoutPriceOfFeulFee; double servicePrice = (double)schedulers.FirstOrDefault(u => u.BillingItemValue.StartsWith("A")).UnitPrice; sch.UnitPrice = servicePrice; if (((int)list.FirstOrDefault().BillingTypeID == 5 || list.FirstOrDefault().BillingTypeValue == "0" || list.FirstOrDefault().BillingTypeLabel == "全包") || ((int)list.FirstOrDefault().BillingTypeID == 6 || list.FirstOrDefault().BillingTypeValue == "1" || list.FirstOrDefault().BillingTypeLabel == "半包")) sch.Price = servicePrice; else sch.Price = servicePrice * sch.WorkTimeConsumption; sch.RopeUsed = schedulers[0].RopeUsed; sch.RopeNum = (int)schedulers[0].RopeNum; //sch.Remark = schedulers[0].OrderSchedulerRemark; double serviceNaturePrice = 0; double upTotalPrice = 0; double midTotalPrice = 0; double discoutPrice = 0; #region 一条船的费用项目 List<MyBillingItem> billingItems = new List<MyBillingItem>(); foreach (var subItem in schedulers) { MyBillingItem bit = new MyBillingItem(); bit.IDX = subItem.BillingItemIDX; bit.ItemID = subItem.ItemID; bit.ItemValue = subItem.BillingItemValue; bit.ItemLabel = subItem.BillingItemLabel; bit.UnitPrice = subItem.UnitPrice; //if (subItem.ItemID == 22 || subItem.BillingItemValue == "E80" || subItem.BillingItemLabel == "燃油附加费") //{ // bit.UnitPrice = GetFuelFee(orderDate); // bit.Price = CalculateFuelFee(orderDate, sch.WorkTimeConsumption); // //bit.Price = subItem.UnitPrice * sch.WorkTimeConsumption; //} if (subItem.ItemID == 23 || subItem.BillingItemValue == "C82" || subItem.BillingItemLabel == "拖缆费") bit.Price = subItem.UnitPrice * sch.RopeNum; else bit.Price = subItem.UnitPrice; bit.Currency = subItem.Currency; bit.TypeID = subItem.PositionTypeID; if(subItem.BillingItemValue.StartsWith("A")) serviceNaturePrice += (double)bit.Price; else if (subItem.BillingItemValue.StartsWith("B")) upTotalPrice += (double)bit.Price; else if(subItem.BillingItemValue.StartsWith("C")) { if(subItem.BillingItemValue.Equals("C78")) discoutPrice += (double)bit.Price; else if(subItem.BillingItemValue.Equals("C82")) midTotalPrice += (double)bit.Price; } //totalPrice += upTotalPrice + midTotalPrice; billingItems.Add(bit); } #endregion sch.SubTotaHKS = serviceNaturePrice; //+upTotalPrice; sch.DiscountSubTotalHKS = sch.SubTotaHKS + discoutPrice; sch.TotalHKs = sch.DiscountSubTotalHKS + midTotalPrice + sch.PriceOfFeulFee; sch.BillingItems = billingItems; grandTotal += sch.TotalHKs; } listScheduler.Add(sch); } } dicSchedulers.Add((int)item.ServiceNatureID, listScheduler); } } _invoice.ServiceNature = dicServiceNature; _invoice.Schedulers = dicSchedulers; _invoice.BillingID = list.FirstOrDefault().BillingID; //_invoice.BillingTemplateID = (int)list.FirstOrDefault().BillingTemplateID; _invoice.BillingCode = list.FirstOrDefault().BillingCode; _invoice.BillingTypeID = (int)list.FirstOrDefault().BillingTypeID; _invoice.BillingTypeValue = list.FirstOrDefault().BillingTypeValue; _invoice.BillingTypeLabel = list.FirstOrDefault().BillingTypeLabel; _invoice.TimeTypeID = (int)list.FirstOrDefault().TimeTypeID; _invoice.TimeTypeValue = list.FirstOrDefault().TimeTypeValue; _invoice.TimeTypeLabel = list.FirstOrDefault().TimeTypeLabel; _invoice.Month = list.FirstOrDefault().Month; _invoice.GrandTotalHKS = Math.Round(grandTotal,2); } return _invoice; }
public static MyInvoice GenerateInvoice2(int billingId) { TugDataModel.TugDataEntities db = new TugDataModel.TugDataEntities(); MyInvoice _invoice = new MyInvoice(); //List<V_Invoice> list = db.V_Invoice.Where(u => u.OrderID == orderId).OrderBy(u => u.ServiceNatureID).Select(u => u).ToList(); //var list = db.V_Invoice2.Where(u => u.BillingID == billingId).OrderBy(u => u.OrderID).ThenBy(u=>u.OrderServiceID).ThenBy(u => u.SchedulerID).Select(u => u); var list = db.V_Invoice2.Where(u => u.BillingID == billingId) .OrderBy(u => u.ServiceWorkDate).ThenBy(u => u.DepartBaseTime).ThenBy(u => u.ServiceNatureLabel).Select(u => u); string strOrderIds = ""; var _orderIds = list.Where(u => u.BillingID == billingId).OrderBy(u=>u.ServiceWorkDate).ThenBy(u => u.DepartBaseTime).ThenBy(u=>u.ServiceNatureLabel) .Select(u => new { u.OrderID }).Distinct().ToList(); if (_orderIds != null) { foreach (var item in _orderIds) { strOrderIds += item.OrderID.ToString() + ","; } } if (strOrderIds != "") { strOrderIds = strOrderIds.Substring(0, strOrderIds.Length - 1); } if (list != null) { _invoice.CustomerID = (int)list.FirstOrDefault().CustomerID; _invoice.CustomerName = list.FirstOrDefault().CustomerName; _invoice.CustomerShipName = list.FirstOrDefault().ShipName; _invoice.OrderIDs = strOrderIds; _invoice.BillingID = list.FirstOrDefault().BillingID; _invoice.IsShowShipLengthRule = list.FirstOrDefault().IsShowShipLengthRule; _invoice.IsShowShipTEUSRule = list.FirstOrDefault().IsShowShipTEUSRule; _invoice.CustomerShipLength = TugBusinessLogic.Module.Util.toint(list.FirstOrDefault().Length); _invoice.CustomerShipTEUS = TugBusinessLogic.Module.Util.toint(list.FirstOrDefault().TEUS); _invoice.BillingTemplateID = (int)list.FirstOrDefault().BillingTemplateID; _invoice.BillingCode = list.FirstOrDefault().BillingCode; _invoice.BillingTypeID = (int)list.FirstOrDefault().BillingTypeID; _invoice.BillingTypeValue = list.FirstOrDefault().BillingTemplateTypeValue; _invoice.BillingTypeLabel = list.FirstOrDefault().BillingTemplateTypeLabel; _invoice.TimeTypeID = (int)list.FirstOrDefault().TimeTypeID; _invoice.TimeTypeValue = list.FirstOrDefault().TimeTypeValue; _invoice.TimeTypeLabel = list.FirstOrDefault().TimeTypeLabel; _invoice.Discount = (double)list.FirstOrDefault().Discount; _invoice.FinalAmount = (double)list.FirstOrDefault().FinalAmount; _invoice.BillingCode = list.FirstOrDefault().BillingCode; _invoice.Ratio1 = (double?)list.FirstOrDefault().Ratio1; _invoice.Ratio2 = (double?)list.FirstOrDefault().Ratio2; _invoice.Ratio3 = (double?)list.FirstOrDefault().Ratio3; _invoice.Ratio4 = (double?)list.FirstOrDefault().Ratio4; _invoice.Ratio5 = (double?)list.FirstOrDefault().Ratio5; _invoice.Ratio6 = (double?)list.FirstOrDefault().Ratio6; _invoice.Ratio7 = (double?)list.FirstOrDefault().Ratio7; _invoice.MinTime = (double?)list.FirstOrDefault().MinTime; _invoice.Month = list.FirstOrDefault().Month; _invoice.Date = list.FirstOrDefault().Date; _invoice.JobNo = list.FirstOrDefault().JobNo; _invoice.Rmark = list.FirstOrDefault().Remark; Dictionary<int, MyService> dicServiceNature = new Dictionary<int, MyService>(); var services = list.Select(u => new { u.OrderServiceID, u.ServiceNatureID, u.ServiceNatureLabel, u.ServiceWorkDate, u.ServiceWorkPlace }).Distinct().OrderBy(u=>u.ServiceWorkDate).ThenBy(u=>u.ServiceNatureLabel).ToList(); Dictionary<int, List<MyScheduler>> dicSchedulers = new Dictionary<int, List<MyScheduler>>(); double grandTotal = 0; if (services != null && services.Count > 0) { foreach (var item in services) { MyService ms = new MyService(); ms.OrderServicId = (int)item.OrderServiceID; ms.ServiceId = (int)item.ServiceNatureID; ms.ServiceName = item.ServiceNatureLabel; ms.ServiceWorkDate = item.ServiceWorkDate; ms.ServiceWorkPlace = item.ServiceWorkPlace; dicServiceNature.Add(ms.OrderServicId, ms); //同一个服务项下面有多条调度 var ships = list.Where(u => u.OrderServiceID == item.OrderServiceID) .Select(u => new { u.OrderServiceID, u.SchedulerID, u.ServiceNatureID, u.TugID, u.TugName1, u.TugName2 }).Distinct() .OrderBy(u => u.TugName1).ToList(); List<MyScheduler> listScheduler = new List<MyScheduler>(); if (ships != null) { foreach (var ship in ships) { MyScheduler sch = new MyScheduler(); sch.TugID = (int)ship.TugID; sch.TugCnName = ship.TugName1; sch.ServiceNatureID = (int)ship.ServiceNatureID; //sch.TugEnName = ship.TugName2; //sch.TugSimpleName = ship.TugSimpleName; //sch.TugPower = ship.Power; var schedulers = list.Where(u => u.OrderServiceID == item.OrderServiceID && u.SchedulerID == ship.SchedulerID) .OrderBy(u => u.OrderID).ThenBy(u => u.OrderServiceID).ThenBy(u => u.SchedulerID) .Select(u => new { u.SchedulerID, u.TugID, u.TugName1, u.TugName2, u.DepartBaseTime, u.ArrivalBaseTime, u.UnitPrice, u.RopeUsed, u.RopeNum, u.SchedulerRemark, u.BillingItemIDX, u.ItemID, u.BillingItemValue, u.BillingItemLabel, u.Currency }).OrderBy(u => u.ItemID).ToList(); if (schedulers != null && schedulers.Count > 0) { sch.SchedulerID = (int)schedulers[0].SchedulerID; sch.DepartBaseTime = schedulers[0].DepartBaseTime; sch.ArrivalBaseTime = schedulers[0].ArrivalBaseTime; int iDiffHour, iDiffMinute; TugBusinessLogic.Utils.CalculateTimeDiff(sch.DepartBaseTime, sch.ArrivalBaseTime, out iDiffHour, out iDiffMinute); sch.WorkTime = iDiffHour.ToString() + "h." + iDiffMinute.ToString("D2") + "m."; sch.WorkTimeConsumption = TugBusinessLogic.Utils.CalculateTimeConsumption2(iDiffHour, iDiffMinute, (int)list.FirstOrDefault().TimeTypeID, list.FirstOrDefault().TimeTypeValue, list.FirstOrDefault().TimeTypeLabel); sch.RopeUsed = schedulers[0].RopeUsed; sch.RopeNum = (int)schedulers[0].RopeNum; sch.UnitPriceOfFeulFee = GetFuelFee(ms.ServiceWorkDate); sch.DiscoutPriceOfFeulFee = GetDiscoutPriceOfFuelFee(_invoice.BillingTemplateID); sch.PriceOfFeulFee = CalculateFuelFee(ms.ServiceWorkDate, sch.WorkTimeConsumption) + sch.DiscoutPriceOfFeulFee; double servicePrice = (double)schedulers.FirstOrDefault(u => u.BillingItemValue.StartsWith("A")).UnitPrice; sch.UnitPrice = servicePrice; if (((int)list.FirstOrDefault().BillingTypeID == 5 || list.FirstOrDefault().BillingTemplateTypeValue == "0" || list.FirstOrDefault().BillingTemplateTypeLabel == "全包") || ((int)list.FirstOrDefault().BillingTypeID == 6 || list.FirstOrDefault().BillingTemplateTypeValue == "1" || list.FirstOrDefault().BillingTemplateTypeLabel == "半包")) sch.Price = servicePrice; else sch.Price = servicePrice * sch.WorkTimeConsumption; double serviceNaturePrice = 0; double upTotalPrice = 0; double midTotalPrice = 0; double discoutPrice = 0; #region 一条船的费用项目 List<MyBillingItem> billingItems = new List<MyBillingItem>(); foreach (var subItem in schedulers) { MyBillingItem bit = new MyBillingItem(); bit.IDX = subItem.BillingItemIDX; bit.ItemID = subItem.ItemID; bit.ItemValue = subItem.BillingItemValue; bit.ItemLabel = subItem.BillingItemLabel; bit.UnitPrice = subItem.UnitPrice; bit.Currency = subItem.Currency; //if (subItem.ItemID == 22 || subItem.BillingItemValue == "E80" || subItem.BillingItemLabel == "燃油附加费") //{ // bit.UnitPrice = GetFuelFee(orderDate); // bit.Price = CalculateFuelFee(orderDate, sch.WorkTimeConsumption); // //bit.Price = subItem.UnitPrice * sch.WorkTimeConsumption; //} if (subItem.ItemID == 23 || subItem.BillingItemValue == "C82" || subItem.BillingItemLabel == "拖缆费") bit.Price = subItem.UnitPrice * sch.RopeNum; else bit.Price = subItem.UnitPrice; if (subItem.BillingItemValue.StartsWith("A")) serviceNaturePrice += (double)bit.Price; else if (subItem.BillingItemValue.StartsWith("B")) upTotalPrice += (double)bit.Price; else if (subItem.BillingItemValue.StartsWith("C") || subItem.BillingItemValue.StartsWith("E")) { if (subItem.BillingItemValue.Equals("C78")) //折扣 discoutPrice += (double)bit.Price; else if (subItem.BillingItemValue.Equals("C82") || subItem.BillingItemValue.Equals("C15") || subItem.BillingItemValue.Equals("C80")) //拖缆费、3600以上、燃油费 midTotalPrice += (double)bit.Price; } //totalPrice += upTotalPrice + midTotalPrice; billingItems.Add(bit); } #endregion sch.SubTotaHKS = sch.Price + upTotalPrice; sch.DiscountSubTotalHKS = sch.SubTotaHKS + discoutPrice; sch.TotalHKs = sch.DiscountSubTotalHKS + midTotalPrice; sch.BillingItems = billingItems; grandTotal += sch.TotalHKs; } listScheduler.Add(sch); } } dicSchedulers.Add((int)item.OrderServiceID, listScheduler); } } _invoice.ServiceNature = dicServiceNature; _invoice.Schedulers = dicSchedulers; _invoice.GrandTotalHKS = Math.Round(grandTotal, 2); //_invoice.FinalAmount = _invoice.GrandTotalHKS - _invoice.GrandTotalHKS * _invoice.Discount / 100; } return _invoice; }