public SaleLead FindOrCreateSaleLead(CustomerDTO cus, int orderNo, EmployeeDTO emp) { var cusSpec = new Specification<SalePositionLead>(c => c.SaleLead.CustomerId == cus.CustomerId); var saleSpec = new Specification<SalePositionLead>(s => s.SaleLead.Sale.OrderNumber == orderNo); var cusAndSale = cusSpec.And(saleSpec); var lead = _repoSalePositionLead.Find(cusAndSale); if (lead == null) { var cusNameSpec = new Specification<SalePositionLead>(c => c.SaleLead.Customer.Name == cus.CustomerName); var cusNameAndSale = cusNameSpec.And(saleSpec); lead = _repoSalePositionLead.Find(cusNameAndSale); if(lead == null) { var empSpec = new Specification<SalePositionLead>(x => x.EmployeeInSalePosition.Employee.Id == emp.EmployeeId); var otherCusSpec = new Specification<SalePositionLead>(v => v.SaleLead.Customer.Id == cus.CustomerId); var otherAndEmp = empSpec.And(otherCusSpec); lead = _repoSalePositionLead.Find(otherAndEmp); if(lead==null) { var cusNameAndEmpSpec = empSpec.And(cusNameSpec); lead = _repoSalePositionLead.Find(cusNameAndEmpSpec); if(lead == null) { var customer = _repoCustomer.Get(cus.CustomerId); var state = _repoSaleLeadState.AsQueryable().FirstOrDefault(x => x.Id > 0); var l = CreateSaleLead(customer.Id, state.Id, 0, DateTime.Now); _repoSaleLead.Add(l); return l; } else { return lead.SaleLead; } } else { return lead.SaleLead; } } else { return lead.SaleLead; } } else { return lead.SaleLead; } }
public SalePositionLead FindOrCreateSalePositionLead(Sale sale, CustomerDTO cus) { var empSaleSpec = new Specification <SalePositionLead>(s => s.SaleLead.Sale.Id == sale.Id); var emp = _repoSalePositionLead.Find(empSaleSpec); if (emp == null) { var sa = FindOrCreateSaleLead(cus, sale.OrderNumber, sale.Date, false); var sl = _repoSaleLead.AsQueryable().FirstOrDefault(s => s.Sale.Id == sale.Id); var empPos = _repoEmployeeInPosition.GetEmployeeInSalePosition().FirstOrDefault(s => s.Position.Name == "Sales"); emp = new SalePositionLead { FinalisedSale = false, SaleLead = sl, SaleLeadId = sl.Id, EmployeeInSalePosition = empPos, EmployeeInSalePostionId = empPos.Id }; _repoSalePositionLead.Add(emp); } return(emp); }