示例#1
0
        private bool SupplierExists(string name)
        {
            if (Suppliers is null)
            {
                return(false);
            }

            return(Suppliers.Any(s => s.Name == name));
        }
        public string GetSupplierNames()
        {
            var result = "";

            if (Suppliers != null && Suppliers.Any())
            {
                result = Session.Query <Supplier>().Where(x => Suppliers.Contains(x.Id)).Select(x => x.Name + " - " + x.HomeRegion.Name).OrderBy(x => x).ToList().Implode();
            }
            return(result);
        }
示例#3
0
        public override void SaveModel()
        {
            Projects data = new Projects();

            if (Status.IsNewInstance)
            {
                data.Id = Guid.NewGuid();
            }

            CopyToModel(data, this);

            if (ProjectContract != null)
            {
                foreach (ProjectContractViewModel model in ProjectContract)
                {
                    if (model != null)
                    {
                        ProjectContract pcdata = new ProjectContract();

                        CopyToModel(pcdata, model);
                        pcdata.ProjectId = data.Id;
                        data.ProjectContract.Add(pcdata);
                    }
                }
            }

            if (ConstructionAtlas.Any())
            {
                foreach (ConstructionAtlasViewModel model in ConstructionAtlas)
                {
                    ConstructionAtlas entity = new ConstructionAtlas();
                    CopyToModel(entity, model);
                    entity.ProjectContractId = data.Id;
                    data.ConstructionAtlas.Add(entity);
                }
            }

            if (Suppliers != null)
            {
                if (Suppliers.Any())
                {
                    foreach (var supplier in Suppliers)
                    {
                        SupplierTranscationItem supplierdata = new SupplierTranscationItem();
                        supplierdata.PlaceofReceipt = supplier.PlaceofReceipt;
                        supplierdata.ManufacturersBussinessItemsId = supplier.Id;
                        supplierdata.ProjectId = data.Id;

                        data.SupplierTranscationItem.Add(supplierdata);
                    }
                }
            }
            var result = _projectcontroller.CreateOrUpdate(data);

            if (result.HasError)
            {
                Errors = result.Errors;
            }

            Query(data.Id);
        }
        public byte[] Excel()
        {
            var wb = new Workbook();
            var ws = new Worksheet("Кто обновлялся и не делал заказы");

            var row      = 0;
            var colShift = 0;

            ws.Merge(row, 0, row, 6);
            ExcelHelper.WriteHeader1(ws, row, 0, "Кто обновлялся и не делал заказы", false, true);
            row++;             // 1

            ws.Merge(row, 1, row, 2);
            ExcelHelper.Write(ws, row, 0, "Регион:", false);
            string regionName;

            if (Regions != null && Regions.Any())
            {
                regionName = GetRegionNames();
            }
            else
            {
                regionName = "Все";
            }
            ExcelHelper.Write(ws, row, 1, regionName, false);
            row++;             // 2

            ws.Merge(row, 1, row, 2);
            ExcelHelper.Write(ws, row, 0, "Период:", false);
            if (UpdatePeriod.Begin != UpdatePeriod.End)
            {
                ExcelHelper.Write(ws, row, 1,
                                  "С " + UpdatePeriod.Begin.ToString("dd.MM.yyyy") + " по " + UpdatePeriod.End.ToString("dd.MM.yyyy"), false);
            }
            else
            {
                ExcelHelper.Write(ws, row, 1, "За " + UpdatePeriod.Begin.ToString("dd.MM.yyyy"), false);
            }
            row++;             // 3

            if (Sum.HasValue)
            {
                ws.Merge(row, 0, row, 3);
                ExcelHelper.Write(ws, row, 0, "Сумма заказов на адрес не более: " + Sum.Value, false);
                row++;
            }

            if (Suppliers != null && Suppliers.Any())
            {
                ws.Merge(row, 0, row, 3);
                var supplierName = GetSupplierNames();
                ExcelHelper.Write(ws, row, 0, "Те, у кого нет заказов на поставщиков: " + supplierName, false);
                row++;
            }

            ExcelHelper.WriteHeader1(ws, row, 0, "Код клиента", true, true);
            ExcelHelper.WriteHeader1(ws, row, 1, "Наименование клиента", true, true);
            ExcelHelper.WriteHeader1(ws, row, 2, "Код пользователя", true, true);
            ExcelHelper.WriteHeader1(ws, row, 3, "Комментарий пользователя", true, true);
            ExcelHelper.WriteHeader1(ws, row, 4, "Регион", true, true);
            ExcelHelper.WriteHeader1(ws, row, 5, "Регистратор", true, true);
            ExcelHelper.WriteHeader1(ws, row, 6, "Дата обновления", true, true);
            ExcelHelper.WriteHeader1(ws, row, 7, "Дата последнего заказа", true, true);
            ExcelHelper.WriteHeader1(ws, row, 8, "Включенные поставщики", true, true);
            ExcelHelper.WriteHeader1(ws, row, 9, "Отключенные поставщики", true, true);
            ExcelHelper.WriteHeader1(ws, row, 10, "Список отключенных", true, true);
            ExcelHelper.WriteHeader1(ws, row, 11, "Нет заказов на поставщиков", true, true);

            ws.Cells.ColumnWidth[0]  = 4000;
            ws.Cells.ColumnWidth[1]  = 12000;
            ws.Cells.ColumnWidth[2]  = 4000;
            ws.Cells.ColumnWidth[3]  = 12000;
            ws.Cells.ColumnWidth[4]  = 6000;
            ws.Cells.ColumnWidth[5]  = 8000;
            ws.Cells.ColumnWidth[6]  = 6000;
            ws.Cells.ColumnWidth[7]  = 6000;
            ws.Cells.ColumnWidth[8]  = 6000;
            ws.Cells.ColumnWidth[9]  = 6000;
            ws.Cells.ColumnWidth[10] = 6000;
            ws.Cells.ColumnWidth[11] = 6000;

            ws.Cells.Rows[row].Height = 514;
            row++;

            var reportData = Find(true);

            foreach (var item in reportData)
            {
                item.ForExport = true;
                ExcelHelper.Write(ws, row, colShift + 0, item.ClientId, true);
                ExcelHelper.Write(ws, row, colShift + 1, item.ClientName, true);
                ExcelHelper.Write(ws, row, colShift + 2, item.UserId, true);
                ExcelHelper.Write(ws, row, colShift + 3, item.UserName, true);
                ExcelHelper.Write(ws, row, colShift + 4, item.RegionName, true);
                ExcelHelper.Write(ws, row, colShift + 5, item.Registrant, true);
                ExcelHelper.Write(ws, row, colShift + 6, item.UpdateDate, true);
                ExcelHelper.Write(ws, row, colShift + 7, item.LastOrderDate, true);
                ExcelHelper.Write(ws, row, colShift + 8, item.EnabledSupCnt, true);
                ExcelHelper.Write(ws, row, colShift + 9, item.DisabledSupCnt, true);
                ExcelHelper.Write(ws, row, colShift + 10, item.DisabledSupName, true);
                ExcelHelper.Write(ws, row, colShift + 11, item.NoOrderSuppliers, true);
                row++;
            }

            wb.Worksheets.Add(ws);
            var ms = new MemoryStream();

            wb.Save(ms);

            return(ms.ToArray());
        }
        //Здесь join Customers.UserAddresses ua on ua.UserId = u.Id, чтобы отсеять пользователей, у которых нет адресов доставки.

        public IList <UpdatedAndDidNotDoOrdersField> Find(bool forExport)
        {
            var query = new DetachedSqlQuery();
            var sb    = new StringBuilder();

            var regionMask = SecurityContext.Administrator.RegionMask;

            if (Regions?.Any() == true)
            {
                regionMask &= Regions.Aggregate(0ul, (current, region) => current | region);
            }

            query.SetParameter("regionMask", regionMask);
            query.SetParameter("updateDateStart", UpdatePeriod.Begin);
            query.SetParameter("updateDateEnd", UpdatePeriod.End);

            sb.AppendLine(@"DROP TEMPORARY TABLE IF EXISTS usr;
CREATE TEMPORARY TABLE usr (INDEX idx(UserId) USING HASH) ENGINE MEMORY
select
	c.id as ClientId,
	c.Name as ClientName,
	reg.Region as RegionName,
	u.Id as UserId,
	u.Name as UserName,
	if (reg.ManagerName is not null, reg.ManagerName, c.Registrant) as Registrant,
	if(uu.UpdateDate > ifnull(nd.LastUpdateAt, '2000-01-01'), uu.UpdateDate, ifnull(nd.LastUpdateAt, '2000-01-01'))  as UpdateDate,
	max(oh.`WriteTime`) as LastOrderDate
from customers.Clients C
join customers.Users u on u.ClientId = c.id
	join usersettings.RetClientsSet rcs on rcs.ClientCode = c.Id
join usersettings.AssignedPermissions ap on ap.UserId = u.Id
join Customers.UserAddresses ua on ua.UserId = u.Id
join usersettings.UserUpdateInfo uu on uu.UserId = u.id
join farm.Regions reg on reg.RegionCode = c.RegionCode
left join accessright.regionaladmins reg on reg.UserName = c.Registrant
left join orders.OrdersHead oh on (oh.`regioncode` & :regionMask > 0) and oh.UserId = u.id
	left join Customers.AnalitFNetDatas nd on nd.UserId = u.Id
where
	(c.regioncode & :regionMask > 0)
	and c.Status = 1
	and ((
				uu.`Updatedate` > :updateDateStart
				and uu.`Updatedate` < :updateDateEnd
			)
			or (
				ifnull(nd.LastUpdateAt, '2000-01-01') > :updateDateStart
				and ifnull(nd.LastUpdateAt, '2000-01-01') < :updateDateEnd
			)
		)
	and rcs.InvisibleOnFirm = 0
	and rcs.ServiceClient = 0
	and u.Enabled = 1
	and u.PayerId <> 921
	and rcs.OrderRegionMask & u.OrderRegionMask & :regionMask > 0
	and u.SubmitOrders = 0
	and ap.PermissionId in (1, 81)
group by u.id; ");

            // вычисление суммы заказов на адрес
            if (Sum.HasValue && Sum.Value > 0)
            {
                sb.AppendLine(@"DROP TEMPORARY TABLE IF EXISTS sa;
CREATE TEMPORARY TABLE sa (INDEX idx(UserId) USING HASH) ENGINE MEMORY
select distinct usr.UserId
from usr
left outer join orders.OrdersHead oh on (oh.`regioncode` & :regionMask > 0) and oh.UserId = usr.UserId and oh.`WriteTime` > :updateDateStart and oh.`WriteTime` < :updateDateEnd
left join orders.orderslist ol on ol.OrderID = oh.RowID
group by usr.UserId, oh.AddressId
having IFNULL(SUM(ol.cost),0) < :sumPerAddress; ");
                query.SetParameter("sumPerAddress", Sum.Value);
            }

            sb.AppendLine(@"
drop temporary table if exists NoOrders;
create temporary table NoOrders (
	UserId int unsigned not null,
	Suppliers varchar(255),
	primary key (UserId)
) engine = memory;
");
            if (Suppliers?.Any() == true)
            {
                sb.AppendLine(@"
drop temporary table if exists TargetSuppliers;
create temporary table TargetSuppliers (
	SupplierId int unsigned,
	primary key (SupplierId)
) engine = memory;
");
                for (var i = 0; i < Suppliers.Length; i++)
                {
                    sb.AppendLine($"insert into TargetSuppliers(SupplierId) values (:supplier{i});");
                    query.SetParameter($"supplier{i}", Suppliers[i]);
                }

                sb.AppendLine(@"
insert into NoOrders(UserId, Suppliers)
select d.UserId, group_concat(d.Name)
from (
		select usr.UserId, s.Name
		from (usr, TargetSuppliers ts)
			join Customers.Suppliers s on s.Id = ts.SupplierId
				join Usersettings.PricesData pd on pd.FirmCode = s.Id
			left outer join orders.OrdersHead oh on (oh.`regioncode` & :regionMask > 0)
				and oh.UserId = usr.UserId
				and oh.`WriteTime` > :updateDateStart
				and oh.`WriteTime` < :updateDateEnd
				and pd.PriceCode = oh.PriceCode
		group by usr.UserId, s.Id
		having count(oh.RowId) = 0
	) as d
group by d.UserId;");
            }
            else
            {
                sb.AppendLine(@"
insert into NoOrders(UserId)
select usr.UserId
from usr
	left outer join orders.OrdersHead oh on (oh.`regioncode` & :regionMask > 0)
		and oh.UserId = usr.UserId
		and oh.`WriteTime` > :updateDateStart
		and oh.`WriteTime` < :updateDateEnd
group by usr.UserId
having count(oh.RowId) = 0;");
            }
            var join = "";

            if (Sum.HasValue && Sum.Value > 0)
            {
                join = "join sa on sa.UserId = usr.UserId ";
            }

            sb.AppendLine($@"
create temporary table SuppliersStat (
	UserId int unsigned not null,
	EnabledSupCnt int not null,
	DisabledSupCnt int not null,
	DisabledSuppliers varchar(255),
	primary key(UserId)
) engine = memory;

insert into SuppliersStat(UserId, EnabledSupCnt, DisabledSupCnt, DisabledSuppliers)
select d.UserId,
	sum(not d.DisabledByUser) as EnabledSupCnt,
	sum(d.DisabledByUser) as DisabledSupCnt,
	group_concat(d.DisabledSupplier) DisabledSuppliers
from (
	SELECT u.Id as UserId,
		supplier.Id as SupplierId,
		if(up.UserId is null, 1, 0) as DisabledByUser,
		if(up.UserId is null, supplier.Name, null) as DisabledSupplier
	FROM Customers.Users u
		join usr on usr.UserId = u.Id
		join Customers.Intersection i on i.ClientId = u.ClientId and i.AgencyEnabled = 1
		JOIN Customers.Clients drugstore ON drugstore.Id = i.ClientId
		JOIN usersettings.RetClientsSet r ON r.clientcode = drugstore.Id
		JOIN usersettings.PricesData pd ON pd.pricecode = i.PriceId
		JOIN Customers.Suppliers supplier ON supplier.Id = pd.firmcode
			JOIN usersettings.RegionalData rd ON rd.RegionCode = i.RegionId AND rd.FirmCode = pd.firmcode
		JOIN usersettings.PricesRegionalData prd ON prd.regioncode = i.RegionId AND prd.pricecode = pd.pricecode
		left join Customers.UserPrices up on up.PriceId = i.PriceId and up.UserId = ifnull(u.InheritPricesFrom, u.Id) and up.RegionId = i.RegionId
	WHERE supplier.Disabled = 0
			and (supplier.RegionMask & i.RegionId) > 0
			AND (drugstore.maskregion & i.RegionId & u.WorkRegionMask) > 0
			AND (r.WorkRegionMask & i.RegionId) > 0
			AND pd.agencyenabled = 1
			AND pd.enabled = 1
			AND pd.pricetype <> 1
			AND prd.enabled = 1
			AND if(not r.ServiceClient, supplier.Id != 234, 1)
			and i.AvailableForClient = 1
	group by u.Id, supplier.Id
) as d
group by d.UserId;

select usr.ClientId,
	usr.ClientName,
	usr.RegionName,
	usr.UserId,
	usr.UserName,
	usr.Registrant,
	usr.UpdateDate,
	usr.LastOrderDate,
	ss.EnabledSupCnt,
	ss.DisabledSupCnt,
	DisabledSuppliers as DisabledSupName,
	no.Suppliers as NoOrderSuppliers
from usr
	join SuppliersStat ss on ss.UserId = usr.UserId
	join NoOrders no on no.UserId = usr.UserId
	{join}
group by usr.UserId
order by {SortBy} {SortDirection}
;");

            query.Sql = sb.ToString();
            var result = query.GetSqlQuery(Session).ToList <UpdatedAndDidNotDoOrdersField>();

            Session.CreateSQLQuery(@"DROP TEMPORARY TABLE IF EXISTS usr;
DROP TEMPORARY TABLE IF EXISTS sa;
drop temporary table if exists NoOrders;
drop temporary table if exists TargetSuppliers;
DROP TEMPORARY TABLE IF EXISTS SuppliersStat;")
            .ExecuteUpdate();

            RowsCount = result.Count;
            if (forExport)
            {
                return(result.ToList());
            }
            return(result.Skip(CurrentPage * PageSize).Take(PageSize).ToList());
        }