Ejemplo n.º 1
0
        public static DataTable getTypeReport(DateTime dt)
        {
            using (var context = new ControllerModel())
            {
                DataTable dtb = new DataTable();

                dtb.Columns.Add("Tên loại");
                dtb.Columns.Add("Số lượng");

                var listtype = TypeController.getList();
                foreach (Types ty in listtype)
                {
                    var amountofcustomers = context.Histories.Include("Customers").Where(o => o.dayadd.Day == dt.Day &&
                                                                                         o.dayadd.Month == dt.Month &&
                                                                                         o.dayadd.Year == dt.Year &&
                                                                                         o.Customers.available == true).Select(o => o.Customers).Distinct().ToList();

                    int count = 0;
                    var test  = amountofcustomers.Where(o => o.typeid == ty.typeid).ToList();
                    if (test.Count() > 0)
                    {
                        count = test.Count();
                        DataRow dtr = dtb.NewRow();
                        dtr["Tên loại"] = ty.name;
                        dtr["Số lượng"] = count;
                        dtb.Rows.Add(dtr);
                    }
                }
                return(dtb);
            }
        }
Ejemplo n.º 2
0
        public static bool AddnewReport(DateTime dt)
        {
            using (var context = new ControllerModel())
            {
                try
                {
                    var check = context.Reports.Where(o => o.datereport.Day == dt.Day &&
                                                      o.datereport.Month == dt.Month &&
                                                      o.datereport.Year == dt.Year).Count();

                    var amountofcustomers = context.Histories.Include("Customers").Where(o => o.dayadd.Day == dt.Day &&
                                                                                         o.dayadd.Month == dt.Month &&
                                                                                         o.dayadd.Year == dt.Year &&
                                                                                         o.Customers.available == true).Select(o => o.Customers).Distinct().ToList();

                    var newCustomer = context.Customers.Where(o => o.dayadd.Day == dt.Day &&
                                                              o.dayadd.Month == dt.Month &&
                                                              o.dayadd.Year == dt.Year && o.available == true).Count();

                    context.Histories.Where(o => o.dayadd.Day == dt.Day &&
                                            o.dayadd.Month == dt.Month &&
                                            o.dayadd.Year == dt.Year).ToList().ForEach(o => o.ischeck = true);

                    if (check == 0)
                    {
                        var report = new Reports
                        {
                            amountofactivecustomer = amountofcustomers.Count(),
                            amountofnewcustomer    = newCustomer,
                            serverreport           = false,
                            datereport             = dt,
                            dayadd    = DateTime.Now,
                            dayupdate = DateTime.Now
                        };

                        context.Reports.Add(report);

                        var listtype = TypeController.getList();
                        foreach (Types ty in listtype)
                        {
                            int count = 0;
                            var test  = amountofcustomers.Where(o => o.typeid == ty.typeid).ToList();
                            if (test.Count > 0)
                            {
                                count = test.Count();
                                var tr = new SNI.Models.TypesReports
                                {
                                    Types     = context.Types.Where(o => o.typeid == ty.typeid).FirstOrDefault(),
                                    Reports   = report,
                                    amounts   = count,
                                    dayadd    = DateTime.Now,
                                    dayupdate = DateTime.Now
                                };
                                context.TypesReports.Add(tr);
                            }
                        }
                        context.SaveChanges();
                    }
                    else
                    {
                        var report1 = context.Reports.Where(o => o.datereport.Day == dt.Day &&
                                                            o.datereport.Month == dt.Month &&
                                                            o.datereport.Year == dt.Year).FirstOrDefault();

                        report1.amountofactivecustomer = amountofcustomers.Count();
                        report1.amountofnewcustomer    = newCustomer;
                        report1.serverreport           = false;
                        report1.datereport             = dt;
                        report1.dayupdate = DateTime.Now;


                        var listtype = TypeController.getList();
                        foreach (Types ty in listtype)
                        {
                            int count = 0;
                            var test  = amountofcustomers.Where(o => o.typeid == ty.typeid).ToList();

                            if (test.Count > 0)
                            {
                                var check1 = context.TypesReports.Where(o => o.typeid == ty.typeid && o.reportid == report1.reportid).Count();
                                count = test.Count();

                                if (check1 == 0)
                                {
                                    var tr = new SNI.Models.TypesReports
                                    {
                                        Types     = context.Types.Where(o => o.typeid == ty.typeid).FirstOrDefault(),
                                        Reports   = report1,
                                        amounts   = count,
                                        dayadd    = DateTime.Now,
                                        dayupdate = DateTime.Now
                                    };
                                    context.TypesReports.Add(tr);
                                }
                                else
                                {
                                    var typer = context.TypesReports.Where(o => o.typeid == ty.typeid && o.reportid == report1.reportid).FirstOrDefault();
                                    typer.amounts = count;
                                }
                            }
                            else
                            {
                                var gettpyr = context.TypesReports.Where(o => o.typeid == ty.typeid && o.reportid == report1.reportid).FirstOrDefault();
                                if (gettpyr != null)
                                {
                                    context.TypesReports.Remove(gettpyr);
                                }
                            }
                        }
                        context.SaveChanges();
                    }
                    JsonPost.PostAPI(dt);
                    return(true);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }