public void Import(IObjectSpace os, String file_name) { FileHelperEngine <DealDataImport> engine = new FileHelperEngine <DealDataImport>(); engine.Options.IgnoreFirstLines = 1; engine.Options.IgnoreEmptyLines = true; // DealDataImport[] deal_data = engine.ReadStream(reader); DealDataImport[] deal_data = engine.ReadFile(file_name); IList <fmCOrder> orders = new List <fmCOrder>(); IList <crmStage> stages = new List <crmStage>(); IList <crmDeliveryUnit> delivery_units = new List <crmDeliveryUnit>(); IList <crmDeliveryItem> delivery_items = new List <crmDeliveryItem>(); IList <crmPaymentUnit> payment_units = new List <crmPaymentUnit>(); IList <crmPaymentItem> payment_items = new List <crmPaymentItem>(); IList <csMaterial> materials = os.GetObjects <csMaterial>(); foreach (DealDataImport record in deal_data) { fmCOrder order = null; crmStage stage = null; crmDeliveryUnit delivery_unit = null; crmDeliveryItem delivery_item = null; crmPaymentUnit payment_unit = null; crmPaymentItem payment_item = null; if (String.IsNullOrEmpty(record.StageCode)) { throw new ArgumentException("Stage Code is Empty", "StageCode"); } if (record.StageCode.Substring(0, 3) == "Adv") { stage = StageStructure.FirstStage; } else { stage = StageStructure.Stages.FirstOrDefault(x => x.Code == record.StageCode); if (stage == null) { stage = StageStructure.FirstStage.SubStagesCreate(); stage.Code = record.StageCode; } if (!stages.Contains(stage)) { stage.StageType = Contract.StageType.FINANCE; stage.DeliveryMethod = DeliveryMethod.UNITS_SHEDULE; stage.PaymentMethod = PaymentMethod.SCHEDULE; // stage.DateEnd = stage.DateBegin; // stage.DateFinish = stage.DateEnd; stages.Add(stage); } } if (record.StageCode.Substring(0, 3) != "Adv") { if (String.IsNullOrEmpty(record.OrderCode)) { throw new ArgumentException("Order Code is Empty", "OrderCode"); } order = orders.FirstOrDefault(x => x.Code == record.OrderCode); if (order == null) { order = os.FindObject <fmCOrder>(new BinaryOperator("Code", record.OrderCode, BinaryOperatorType.Equal)); if (order == null) { throw new ArgumentException("Order unknow", "OrderCode"); } else { orders.Add(order); } stage.Order = order; } if (record.DateContract == null) { throw new ArgumentException("Date Contract is Empty", "DateContract"); } delivery_unit = stage.DeliveryPlan.DeliveryUnits.FirstOrDefault(x => x.DatePlane == record.DateContract); if (record.DateContract > stage.DateEnd) { stage.DateEnd = (DateTime)record.DateContract; } if (delivery_unit == null) { delivery_unit = stage.DeliveryPlan.DeliveryUnitCreate(); delivery_unit.DatePlane = (DateTime)record.DateContract; } if (!delivery_units.Contains(delivery_unit)) { delivery_units.Add(delivery_unit); } delivery_unit.Order = order; if (record.Count == null) { throw new ArgumentException("Count is Empty", "Count"); } if (record.Price == null) { throw new ArgumentException("Price is Empty", "Price"); } if (String.IsNullOrEmpty(record.NomenclatureCode)) { throw new ArgumentException("Nomenclature Code is Empty", "NomenclatureCode"); } if (!record.NomenclatureCode.Contains("*I") && !record.NomenclatureCode.Contains("*E")) { csMaterial material = materials.FirstOrDefault(x => x.CodeTechnical == record.NomenclatureCode); if (material == null) { throw new ArgumentException("Nomenclature unknow", "NomenclatureCode"); } delivery_item = delivery_unit.DeliveryItems.FirstOrDefault(x => x.Nomenclature == material); if (delivery_item == null) { delivery_item = delivery_unit.DeliveryItemsCreateMaterial(); ((crmDeliveryMaterial)delivery_item).Material = material; } delivery_item.CostCalculateMethod = CostCalculateMethod.CALC_COST; delivery_item.NDSCalculateMethod = NDSCalculateMethod.FROM_COST; delivery_item.FullCalculateMethod = FullCalculateMethod.CALC_FULL; delivery_item.Price = (Decimal)record.Price; delivery_item.CountUnit = delivery_item.Nomenclature.BaseUnit; if (delivery_items.Contains(delivery_item)) { delivery_item.CountValue += (Decimal)record.Count; } else { delivery_item.CountValue = (Decimal)record.Count; delivery_items.Add(delivery_item); } } } if (record.DateContract == null) { throw new ArgumentException("Date Contract is Empty", "DateContract"); } payment_unit = stage.PaymentPlan.PaymentUnits.FirstOrDefault(x => x.DatePlane == record.DateContract && x is crmPaymentCasheLess); if (payment_unit == null) { payment_unit = stage.PaymentPlan.PaymentCasheLessCreate(); payment_unit.DatePlane = (DateTime)record.DateContract; if (payment_unit.DatePlane > stage.DateFinish) { stage.DateFinish = payment_unit.DatePlane; } } if (!payment_units.Contains(payment_unit)) { ((crmPaymentCasheLess)payment_unit).SummFull = (Decimal)record.SummaPayment; payment_units.Add(payment_unit); } else { ((crmPaymentCasheLess)payment_unit).SummFull += (Decimal)record.SummaPayment; } // payment_item = payment_unit.PaymentItems.FirstOrDefault(x => x.Order == order); // if (payment_item == null) { // payment_item = payment_unit.PaymentItemsCreateMoney(); // } //if (payment_unit.PaymentItems.Count == 0) { // payment_item = payment_unit.PaymentItemsCreateMoney(); //} //else { // payment_item = payment_unit.PaymentItems[0]; //} //if (payment_items.Contains(payment_item)) { // payment_item.SummFull += (Decimal)record.SummaPayment; // payment_item.AccountSumma += (Decimal)record.SummaPayment; //} //else { // payment_item.SummFull = (Decimal)record.SummaPayment; // payment_item.AccountSumma = (Decimal)record.SummaPayment; // payment_items.Add(payment_item); //} } IList <crmDeliveryUnit> del_delivery_units = new List <crmDeliveryUnit>(); IList <crmPaymentUnit> del_payment_units = new List <crmPaymentUnit>(); foreach (crmStage stage in stages) { foreach (crmDeliveryUnit delivery_unit in stage.DeliveryPlan.DeliveryUnits) { if (!delivery_units.Contains(delivery_unit)) { del_delivery_units.Add(delivery_unit); } } foreach (crmPaymentUnit payment_unit in stage.PaymentPlan.PaymentUnits) { if (!payment_units.Contains(payment_unit)) { del_payment_units.Add(payment_unit); } } } os.Delete(del_delivery_units); os.Delete(del_payment_units); }
public void Import(IObjectSpace os, String file_name) { FileHelperEngine <DealDataImport> engine = new FileHelperEngine <DealDataImport>(); engine.Options.IgnoreFirstLines = 1; engine.Options.IgnoreEmptyLines = true; Int32 line = 0; // DealDataImport[] deal_data = engine.ReadStream(reader); DealDataImport[] deal_data = engine.ReadFile(file_name); IList <fmCOrder> orders = new List <fmCOrder>(); IList <crmStage> stages = new List <crmStage>(); IList <crmDeliveryUnit> delivery_units = new List <crmDeliveryUnit>(); IList <crmDeliveryItem> delivery_items = new List <crmDeliveryItem>(); IList <crmPaymentUnit> payment_units = new List <crmPaymentUnit>(); IList <crmPaymentItem> payment_items = new List <crmPaymentItem>(); IList <csNomenclature> materials = os.GetObjects <csNomenclature>(); foreach (DealDataImport record in deal_data) { fmCOrder order = null; crmStage stage = null; crmDeliveryUnit delivery_unit = null; crmDeliveryItem delivery_item = null; crmPaymentUnit payment_unit = null; line++; // crmPaymentItem payment_item = null; if (!String.IsNullOrEmpty(record.OrderCode)) { order = orders.FirstOrDefault(x => x.Code == record.OrderCode); if (order == null) { order = os.FindObject <fmCOrder>(new BinaryOperator("Code", record.OrderCode, BinaryOperatorType.Equal)); if (order == null) { throw new ArgumentException("Неизвестный заказ: " + record.OrderCode + " Строка: " + line, "OrderCode"); } else { orders.Add(order); } } } if (String.IsNullOrEmpty(record.StageCode)) { throw new ArgumentException("Номер этапа пустой. Строка: " + line, "StageCode"); } if (record.StageCode.Substring(0, 3) == "Adv") { stage = StageStructure.FirstStage; } else { stage = StageStructure.Stages.FirstOrDefault(x => x.Code == record.StageCode); if (stage == null) { stage = StageStructure.FirstStage.SubStagesCreate(); stage.Code = record.StageCode; } if (!stages.Contains(stage)) { stage.Order = order; stage.StageType = Contract.StageType.FINANCE; stage.DeliveryMethod = DeliveryMethod.UNITS_SHEDULE; stage.PaymentMethod = PaymentMethod.SCHEDULE; // stage.DateEnd = stage.DateBegin; // stage.DateFinish = stage.DateEnd; stages.Add(stage); } } if (record.StageCode.Substring(0, 3) != "Adv") { if (order == null) { throw new ArgumentException("Заказ не найден", "OrderCode"); } if (record.DateContract == null) { throw new ArgumentException("Незаполнена плановая дата исполнения. Строка: " + line, "DateContract"); } delivery_unit = stage.DeliveryPlan.DeliveryUnits.FirstOrDefault(x => x.DatePlane == record.DateContract); if (record.DateContract > stage.DateEnd) { stage.DateEnd = (DateTime)record.DateContract; } if (delivery_unit == null) { delivery_unit = stage.DeliveryPlan.DeliveryUnitCreate(); delivery_unit.DatePlane = (DateTime)record.DateContract; } if (!delivery_units.Contains(delivery_unit)) { delivery_units.Add(delivery_unit); } delivery_unit.Order = order; if (record.Count == null) { throw new ArgumentException("Кол-во пустое. Строка: " + line, "Count"); } if (record.Price == null) { throw new ArgumentException("Цена пустая. Строка: " + line, "Price"); } if (String.IsNullOrEmpty(record.NomenclatureCode)) { throw new ArgumentException("Номенклатура пустая. Строка: " + line, "NomenclatureCode"); } if (!record.NomenclatureCode.Contains("*I") && !record.NomenclatureCode.Contains("*E")) { // csMaterial material = ; csNomenclature nomenclature = materials.FirstOrDefault(x => x.CodeTechnical == record.NomenclatureCode); if (nomenclature == null) { throw new ArgumentException("Номенклатура: " + record.NomenclatureCode + " ненайдена. Строка: " + line, "NomenclatureCode"); } delivery_item = delivery_unit.DeliveryItems.FirstOrDefault(x => x.Nomenclature == nomenclature); if (delivery_item == null) { if (nomenclature is csMaterial) { delivery_item = delivery_unit.DeliveryItemsCreateMaterial(); ((crmDeliveryMaterial)delivery_item).Material = nomenclature as csMaterial; } else if (nomenclature is csService) { delivery_item = delivery_unit.DeliveryItemsCreateService(); ((crmDeliveryService)delivery_item).Service = nomenclature as csService; } else { throw new ArgumentException("Номенклатура: " + record.NomenclatureCode + " неверного типа. Строка: " + line, "NomenclatureCode"); } } delivery_item.CostCalculateMethod = CostCalculateMethod.CALC_COST; delivery_item.NDSCalculateMethod = NDSCalculateMethod.FROM_COST; delivery_item.FullCalculateMethod = FullCalculateMethod.CALC_FULL; delivery_item.Price = (Decimal)record.Price; delivery_item.CountUnit = delivery_item.Nomenclature.BaseUnit; if (delivery_items.Contains(delivery_item)) { delivery_item.CountValue += (Decimal)record.Count; } else { delivery_item.CountValue = (Decimal)record.Count; delivery_items.Add(delivery_item); } } } if (record.DateContract == null) { throw new ArgumentException("Дата плановая исполнения не заполнена. Строка: " + line, "DateContract"); } payment_unit = stage.PaymentPlan.PaymentUnits.FirstOrDefault(x => x.DatePlane == record.DateContract && x is crmPaymentCasheLess); //if (payment_unit != null && !payment_units.Contains(payment_unit)) { // os.Delete(payment_unit); // payment_unit = null; //} if (payment_unit == null) { payment_unit = stage.PaymentPlan.PaymentCasheLessCreate(); payment_unit.DatePlane = (DateTime)record.DateContract; if (payment_unit.DatePlane > stage.DateFinish) { stage.DateFinish = payment_unit.DatePlane; } } payment_unit.Order = order; if (!payment_units.Contains(payment_unit)) { ((crmPaymentCasheLess)payment_unit).SummFull = (Decimal)record.SummaPayment; payment_units.Add(payment_unit); } else { ((crmPaymentCasheLess)payment_unit).SummFull += (Decimal)record.SummaPayment; } // payment_item = payment_unit.PaymentItems.FirstOrDefault(x => x.Order == order); // if (payment_item == null) { // payment_item = payment_unit.PaymentItemsCreateMoney(); // } //if (payment_unit.PaymentItems.Count == 0) { // payment_item = payment_unit.PaymentItemsCreateMoney(); //} //else { // payment_item = payment_unit.PaymentItems[0]; //} //if (payment_items.Contains(payment_item)) { // payment_item.SummFull += (Decimal)record.SummaPayment; // payment_item.AccountSumma += (Decimal)record.SummaPayment; //} //else { // payment_item.SummFull = (Decimal)record.SummaPayment; // payment_item.AccountSumma = (Decimal)record.SummaPayment; // payment_items.Add(payment_item); //} } IList <crmDeliveryUnit> del_delivery_units = new List <crmDeliveryUnit>(); IList <crmDeliveryItem> del_delivery_items = new List <crmDeliveryItem>(); IList <crmPaymentUnit> del_payment_units = new List <crmPaymentUnit>(); IList <crmPaymentItem> del_payment_items = new List <crmPaymentItem>(); foreach (crmStage stage in stages) { foreach (crmDeliveryUnit delivery_unit in stage.DeliveryPlan.DeliveryUnits) { if (!delivery_units.Contains(delivery_unit)) { del_delivery_units.Add(delivery_unit); foreach (crmDeliveryItem item in delivery_unit.DeliveryItems) { del_delivery_items.Add(item); } } else { foreach (crmDeliveryItem item in delivery_unit.DeliveryItems) { if (!delivery_items.Contains(item)) { del_delivery_items.Add(item); } } } } foreach (crmPaymentUnit payment_unit in stage.PaymentPlan.PaymentUnits) { if (!payment_units.Contains(payment_unit)) { del_payment_units.Add(payment_unit); foreach (crmPaymentItem item in payment_unit.PaymentItems) { del_payment_items.Add(item); } } else { foreach (crmPaymentItem item in payment_unit.PaymentItems) { if (!payment_items.Contains(item)) { del_payment_items.Add(item); } } } } } // Не удаляем PaymentItems, поскольку не создаем их, а создаем PaymentUnitCashLess, который сам создает Item // os.Delete(del_payment_items); os.Delete(del_payment_units); os.Delete(del_delivery_items); os.Delete(del_delivery_units); }