예제 #1
0
        void AddFinanceData(string pharmacyUUID, string skuUUID, DateTimeOffset period, float?sale, float?purchase, float?remain)
        {
            var financeData = MainDatabase.Create <FinanceData>();

            financeData.Pharmacy = pharmacyUUID;
            financeData.Period   = period;
            financeData.DrugSKU  = skuUUID;
            financeData.Sale     = sale;
            financeData.Purchase = purchase;
            financeData.Remain   = remain;
        }
예제 #2
0
        //SuggestClient Api;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            RequestWindowFeature(WindowFeatures.NoTitle);
            Window.AddFlags(WindowManagerFlags.KeepScreenOn);

            // Create your application here
            SetContentView(Resource.Layout.Pharmacy);

            FindViewById <Button>(Resource.Id.paCloseB).Click += (s, e) => {
                Finish();
            };

            FindViewById <Button>(Resource.Id.paSaveB).Click += (s, e) => {
                var transaction = MainDatabase.BeginTransaction();

                Pharmacy item;
                if (Pharmacy == null)
                {
                    item           = MainDatabase.Create2 <Pharmacy>();
                    item.CreatedAt = DateTimeOffset.Now;

                    /* Contracts */
                    if (!string.IsNullOrEmpty(ContractsNames.Text))
                    {
                        var ll            = ContractsNames.Parent as LinearLayout;
                        var contractUUIDs = (string)ll.GetTag(Resource.String.ContractUUIDs);
                        if (!string.IsNullOrEmpty(contractUUIDs))
                        {
                            foreach (var contract in contractUUIDs.Split(';'))
                            {
                                var contractData = MainDatabase.Create <ContractData>();
                                contractData.Pharmacy = item.UUID;
                                contractData.Contract = contract;
                            }
                        }
                    }
                    /* ./Contracts */
                }
                else
                {
                    item = Pharmacy;

                    /* Contracts */
                    if (string.IsNullOrEmpty(ContractsNames.Text))
                    {
                        var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID);
                        foreach (var contractData in contractDatas)
                        {
                            MainDatabase.DeleteEntity(transaction, contractData);;
                        }
                        contractDatas = null;
                    }
                    else
                    {
                        var ll            = ContractsNames.Parent as LinearLayout;
                        var contractUUIDs = (string)ll.GetTag(Resource.String.ContractUUIDs);
                        if (!string.IsNullOrEmpty(contractUUIDs))
                        {
                            var contracts     = contractUUIDs.Split(';');
                            var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID);
                            foreach (var contractData in contractDatas)
                            {
                                MainDatabase.DeleteEntity(transaction, contractData);
                            }
                            contractDatas = null;
                            foreach (var contract in contractUUIDs.Split(';'))
                            {
                                var contractData = MainDatabase.Create2 <ContractData>();
                                contractData.Pharmacy = item.UUID;
                                contractData.Contract = contract;
                            }
                        }
                    }
                    /* ./Contracts */
                }

                item.UpdatedAt = DateTimeOffset.Now;
                item.IsSynced  = false;
                item.SetState((PharmacyState)State.SelectedItemPosition);
                item.Brand      = FindViewById <EditText>(Resource.Id.paBrandET).Text;
                item.NumberName = FindViewById <EditText>(Resource.Id.paNumberNameET).Text;
                item.LegalName  = FindViewById <EditText>(Resource.Id.paLegalNameET).Text;

                if (string.IsNullOrEmpty(NetName.Text))
                {
                    item.Net = string.Empty;
                }
                else
                {
                    item.Net = NetUUID;
                }

                var address = FindViewById <AutoCompleteTextView>(Resource.Id.paAddressACTV);
                item.Address = address.Text;
                bool isChanged = (bool)address.GetTag(Resource.String.IsChanged);
                if (isChanged)
                {
                    item.AddressFiasId = (string)address.GetTag(Resource.String.fias_id);
                    item.AddressQCGeo  = (string)address.GetTag(Resource.String.qc_geo);
                    item.AddressGeoLat = (string)address.GetTag(Resource.String.geo_lat);
                    item.AddressGeoLon = (string)address.GetTag(Resource.String.geo_lon);
                }

                if (string.IsNullOrEmpty(Subway.Text))
                {
                    item.Subway = string.Empty;
                }
                else
                {
                    var subwayUUID = (string)Subway.GetTag(Resource.String.SubwayUUID);
                    if (!string.IsNullOrEmpty(subwayUUID))
                    {
                        item.Subway = subwayUUID;
                    }
                }

                if (string.IsNullOrEmpty(Region.Text))
                {
                    item.Region = string.Empty;
                }
                else
                {
                    var regionUUID = (string)Region.GetTag(Resource.String.RegionUUID);
                    if (!string.IsNullOrEmpty(regionUUID))
                    {
                        item.Region = regionUUID;
                    }
                }

                item.Phone = FindViewById <EditText>(Resource.Id.paPhoneET).Text;

                if (string.IsNullOrEmpty(Place.Text))
                {
                    item.Place = string.Empty;
                }
                else
                {
                    var placeUUID = (string)Place.GetTag(Resource.String.PlaceUUID);
                    if (!string.IsNullOrEmpty(placeUUID))
                    {
                        item.Place = placeUUID;
                    }
                }

                if (Category.SelectedItemPosition > 0)
                {
                    item.Category = Categories[Category.SelectedItemPosition].uuid;
                }
                else
                {
                    item.Category = string.Empty;
                }
                //if (string.IsNullOrEmpty(Category.Text)) {
                //	item.Category = string.Empty;
                //} else {
                //	var categoryUUID = (string)Category.GetTag(Resource.String.CategoryUUID);
                //	if (!string.IsNullOrEmpty(categoryUUID)) {
                //		item.Category = categoryUUID;
                //	}
                //}
                item.TurnOver = Helper.ToInt(FindViewById <EditText>(Resource.Id.paTurnOverET).Text);
                item.Comment  = FindViewById <EditText>(Resource.Id.paCommentET).Text;

                if (!item.IsManaged)
                {
                    MainDatabase.SaveEntity(transaction, item);
                }

                transaction.Commit();

                //var sync = new SyncItem() {
                //	Path = @"Pharmacy",
                //	ObjectUUID = Pharmacy.UUID,
                //	JSON = JsonConvert.SerializeObject(Pharmacy)
                //};

                //MainDatabase.AddToQueue(sync);

                //StartService(new Intent("com.xamarin.SyncService"));
                GetSharedPreferences(MainActivity.C_MAIN_PREFS, FileCreationMode.Private)
                .Edit()
                .PutString(MainActivity.C_SAVED_PHARMACY_UUID, item.UUID)
                .Commit();

                Finish();
            };

            //Api = new SuggestClient(Secret.DadataApiToken, Secret.DadataApiURL);


            #region State
            State  = FindViewById <Spinner>(Resource.Id.paStateS);
            States = MainDatabase.GetStates();
            var stateAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, States.ToArray());
            stateAdapter.SetDropDownViewResource(Resource.Layout.SpinnerItem);
            State.Adapter = stateAdapter;
            #endregion

            #region Net
            Nets    = MainDatabase.GetNets();
            NetName = FindViewById <AutoCompleteTextView>(Resource.Id.paNetACTV);
            var netChoiceButton = FindViewById <Button>(Resource.Id.paNetB);
            netChoiceButton.Click += (object sender, EventArgs e) => {
                new AlertDialog.Builder(this)
                .SetTitle("Аптечная сеть")
                .SetCancelable(true)
                .SetItems(Nets.Select(item => item.name).ToArray(), (caller, arguments) => {
                    SetNet(arguments.Which);
                    //Toast.MakeText(this, @"Selected " + arguments.Which, ToastLength.Short).Show();
                })
                .Show();
            };
            #endregion

            ContractsNames         = FindViewById <AutoCompleteTextView>(Resource.Id.paContractsACTV);
            ContractsChoice        = FindViewById <Button>(Resource.Id.paContractsB);
            ContractsChoice.Click += ContractsChoice_Click;

            Address = FindViewById <AutoCompleteTextView>(Resource.Id.paAddressACTV);
            Address.SetTag(Resource.String.IsChanged, false);

            Subway = FindViewById <AutoCompleteTextView>(Resource.Id.paSubwayACTV);

            Region = FindViewById <AutoCompleteTextView>(Resource.Id.paRegionACTV);

            Place = FindViewById <AutoCompleteTextView>(Resource.Id.paPlaceACTV);

            //Category = FindViewById<AutoCompleteTextView>(Resource.Id.paCategoryACTV);

            #region Category
            Category   = FindViewById <Spinner>(Resource.Id.paCategoryS);
            Categories = new List <Category>();
            Categories.Add(new Category {
                name = @"Выберите категорию!", uuid = Guid.Empty.ToString()
            });
            Categories.AddRange(MainDatabase.GetItems <Category>());;
            var categoryAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Categories.Select(cat => cat.name).ToArray());
            categoryAdapter.SetDropDownViewResource(Resource.Layout.SpinnerItem);
            Category.Adapter = categoryAdapter;
            #endregion

            var pharmacyUUID = Intent.GetStringExtra("UUID");
            if (string.IsNullOrEmpty(pharmacyUUID))
            {
                var shared    = GetSharedPreferences(MainActivity.C_MAIN_PREFS, FileCreationMode.Private);
                var agentUUID = shared.GetString(SigninDialog.C_AGENT_UUID, string.Empty);
                try {
                    var agent = MainDatabase.GetItem <Agent>(agentUUID);
                    Address.Text = agent.city;
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }

                FindViewById <TextView>(Resource.Id.paInfoTV).Text = @"ДОБАВЛЕНИЕ НОВОЙ АПТЕКИ";
                FindViewById <TableRow>(Resource.Id.paRowLastAttendance).Visibility     = ViewStates.Gone;
                FindViewById <TableRow>(Resource.Id.paRowNextAttendanceDate).Visibility = ViewStates.Gone;

                InitViews();
                return;
            }

            Pharmacy = MainDatabase.GetEntity <Pharmacy>(pharmacyUUID);

            FindViewById <TextView>(Resource.Id.paInfoTV).Text = "АПТЕКА : " + Pharmacy.GetName();

            FindViewById <TextView>(Resource.Id.paUUIDTV).Text = Pharmacy.UUID;

            State.SetSelection((int)Pharmacy.GetState());
            FindViewById <EditText>(Resource.Id.paBrandET).Text      = Pharmacy.Brand;
            FindViewById <EditText>(Resource.Id.paNumberNameET).Text = Pharmacy.NumberName;
            FindViewById <EditText>(Resource.Id.paLegalNameET).Text  = Pharmacy.LegalName;

            //NetName.Text = string.IsNullOrEmpty(Pharmacy.Net) ?
            //	string.Empty : MainDatabase.GetNet(Pharmacy.Net).name;
            //NetUUID = Pharmacy.Net;

            if (!string.IsNullOrEmpty(Pharmacy.Net))
            {
                SetNet(Nets.FindIndex(net => string.Compare(net.uuid, Pharmacy.Net) == 0));
            }

            ContractDatas = MainDatabase.GetPharmacyDatas <ContractData>(Pharmacy.UUID);
            if (ContractDatas.Count > 0)
            {
                ContractsNames.Text = string.Join(", ", ContractDatas.Select(cd => MainDatabase.GetItem <Contract>(cd.Contract).name).ToArray());
                var ll = ContractsNames.Parent as LinearLayout;
                ll.SetTag(Resource.String.ContractUUIDs,
                          string.Join(@";", ContractDatas.Select(cd => cd.Contract).ToArray())
                          );
            }
            Address.Text = Pharmacy.Address;

            Subway.Text = string.IsNullOrEmpty(Pharmacy.Subway) ?
                          string.Empty : MainDatabase.GetItem <Subway>(Pharmacy.Subway).name;

            Region.Text = string.IsNullOrEmpty(Pharmacy.Region) ?
                          string.Empty : MainDatabase.GetItem <Region>(Pharmacy.Region).name;

            FindViewById <EditText>(Resource.Id.paPhoneET).Text = Pharmacy.Phone;

            Place.Text = string.IsNullOrEmpty(Pharmacy.Place) ?
                         string.Empty : MainDatabase.GetItem <Place>(Pharmacy.Place).name;

            //Category.Text = string.IsNullOrEmpty(Pharmacy.Category) ?
            //	string.Empty : MainDatabase.GetItem<Category>(Pharmacy.Category).name;
            if (!string.IsNullOrEmpty(Pharmacy.Category))
            {
                Category.SetSelection(Categories.FindIndex(cat => string.Compare(cat.uuid, Pharmacy.Category) == 0));
            }

            FindViewById <EditText>(Resource.Id.paTurnOverET).Text = Pharmacy.TurnOver.HasValue ?
                                                                     Pharmacy.TurnOver.Value.ToString() : string.Empty;

            FindViewById <TextView>(Resource.Id.paLastAttendanceTV).Text = Pharmacy.LastAttendanceDate.HasValue ?
                                                                           Pharmacy.LastAttendanceDate.Value.ToString(@"dd.MM.yyyy") : @"<нет визита>";

            FindViewById <TextView>(Resource.Id.paNextAttendanceDateTV).Text = Pharmacy.NextAttendanceDate.HasValue ?
                                                                               Pharmacy.NextAttendanceDate.Value.ToString(@"dd.MM.yyyy") : DateTimeOffset.Now.ToString(@"dd.MM.yyyy");

            FindViewById <EditText>(Resource.Id.paCommentET).Text = Pharmacy.Comment;


            InitViews();
        }
예제 #3
0
        void GenerateData_Click(object sender, EventArgs e)
        {
            var rnd = new Random();

            var step        = 7;
            var visitsCount = 10;
            var baseDate    = DateTimeOffset.Now.AddDays(-1 * visitsCount * step);

            var promotions  = MainDatabase.GetItems <Promotion>();
            var drugSKUs    = MainDatabase.GetDrugSKUs();
            var pharmacies  = MainDatabase.GetItems <Pharmacy>();
            var transaction = MainDatabase.BeginTransaction();

            MainDatabase.DeleteAttendancies();
            MainDatabase.DeleteDistributions();
            MainDatabase.DeleteFinanceData();
            MainDatabase.DeleteSaleData();

            MainDatabase.DeleteItems <CoterieData>();
            MainDatabase.DeleteItems <PresentationData>();
            MainDatabase.DeleteItems <Employee>();
            MainDatabase.DeleteItems <PromotionData>();
            MainDatabase.DeleteItems <CompetitorData>();
            MainDatabase.DeleteItems <MessageData>();
            MainDatabase.DeleteItems <ResumeData>();
            MainDatabase.DeleteItems <PhotoData>();

            transaction.Commit();

            transaction = MainDatabase.BeginTransaction();

            //int count = 0;
            foreach (var pharmacy in pharmacies)
            {
                //count++;
                // Визиты
                if (true)
                {
                    for (int i = 0; i < visitsCount; i++)
                    {
                        var attendance = MainDatabase.Create <Attendance>();
                        attendance.Pharmacy = pharmacy.UUID;
                        attendance.When     = baseDate.AddDays(i * step);

                        foreach (var SKU in drugSKUs)
                        {
                            var distribution = MainDatabase.CreateData <DistributionData>(attendance.UUID);
                            distribution.DrugSKU     = SKU.uuid;
                            distribution.IsExistence = rnd.NextDouble() > 0.5;
                            distribution.Count       = rnd.Next(0, 10);
                            distribution.Price       = (int)(rnd.NextDouble() * 200);
                            distribution.IsPresence  = rnd.NextDouble() > 0.5;
                            distribution.HasPOS      = rnd.NextDouble() > 0.5;
                            distribution.Order       = @"Order " + SKU.uuid;
                            distribution.Comment     = @"Comment " + SKU.uuid;
                        }
                    }
                }

                // Продажи
                var financesCount = 5;
                baseDate = DateTimeOffset.Now.AddMonths(-3);
                for (int i = 0; i < financesCount; i++)
                {
                    foreach (var SKU in drugSKUs)
                    {
                        var financeData = MainDatabase.Create <FinanceData>();
                        financeData.Pharmacy = pharmacy.UUID;
                        financeData.Period   = baseDate.AddMonths(i);
                        financeData.DrugSKU  = SKU.uuid;
                        financeData.Sale     = rnd.NextDouble() > 0.3 ? null : (float?)(rnd.NextDouble() * 20);
                        financeData.Purchase = rnd.NextDouble() > 0.4 ? null : (float?)(rnd.NextDouble() * 20);
                        financeData.Remain   = rnd.NextDouble() > 0.5 ? null : (float?)(rnd.NextDouble() * 20);
                    }
                }

                // Содержание визите = Презентаци
                int employeesCount = 3;
                //int presentationsCount = 5;
                var attendanceLast = MainDatabase.GetAttendaces(pharmacy.UUID).OrderByDescending(i => i.When).FirstOrDefault();
                var positions      = MainDatabase.GetItems <Position>();
                var workTypes      = MainDatabase.GetItems <WorkType>();
                var brands         = MainDatabase.GetItems <DrugBrand>();
                var employees      = MainDatabase.GetEmployees(pharmacy.UUID);

                if (employees.Count == 0)
                {
                    for (int emp = 0; emp < employeesCount; emp++)
                    {
                        var employee = MainDatabase.CreateEmployee(pharmacy.UUID);
                        employee.SetSex(Sex.Male);
                        employee.Name = (rnd.NextDouble() > 0.5 ? @"Иванов" : @"Петров") + @" "
                                        + (rnd.NextDouble() > 0.5 ? @"Иван" : @"Петр") + @" "
                                        + (rnd.NextDouble() > 0.5 ? @"Иванович" : @"Петрович");
                        employee.Position  = positions[rnd.Next(positions.Count)].uuid;
                        employee.BirthDate = DateTimeOffset.Now.AddYears(-rnd.Next(25, 40)).AddDays((rnd.NextDouble() - 0.5) * 365);
                        employees.Add(employee);
                    }
                }

                foreach (var employee in employees)
                {
                    foreach (var brand in brands)
                    {
                        if (rnd.NextDouble() > 0.5)
                        {
                            continue;
                        }

                        for (int wt = 0; wt < workTypes.Count; wt++)
                        {
                            if (rnd.NextDouble() > 0.5)
                            {
                                continue;
                            }

                            var presentationData = MainDatabase.CreateData <PresentationData>(attendanceLast.UUID);
                            presentationData.Employee = employee.UUID;
                            presentationData.Brand    = brand.uuid;
                            presentationData.WorkType = workTypes[wt].uuid;
                        }
                    }
                }

                // Содержание визите = Фарм-кружок
                foreach (var employee in employees)
                {
                    if (rnd.NextDouble() > 0.5)
                    {
                        continue;
                    }

                    foreach (var brand in brands)
                    {
                        if (rnd.NextDouble() > 0.5)
                        {
                            continue;
                        }

                        var coterieData = MainDatabase.CreateData <CoterieData>(attendanceLast.UUID);
                        coterieData.Employee = employee.UUID;
                        coterieData.Brand    = brand.uuid;
                    }
                }

                // Акция
                var promotionData = MainDatabase.CreateData <PromotionData>(attendanceLast.UUID);
                promotionData.Promotion = promotions[rnd.Next(promotions.Count - 1)].uuid;
                promotionData.Text      = @"promotionData";

                // Активность конкурентов
                var competitorData = MainDatabase.CreateData <CompetitorData>(attendanceLast.UUID);
                competitorData.Text = @"competitorData";

                // Сообщения от аптеки
                var messageTypes = MainDatabase.GetItems <MessageType>();
                foreach (var item in messageTypes)
                {
                    if (rnd.NextDouble() > 0.5)
                    {
                        continue;
                    }

                    var messageData = MainDatabase.CreateData <MessageData>(attendanceLast.UUID);
                    messageData.Type = item.uuid;
                    messageData.Text = @"messageData " + item.uuid;
                }

                // Резюме визита;
                var resumeData = MainDatabase.CreateData <ResumeData>(attendanceLast.UUID);
                resumeData.Text = @"resumeData";
            }

            transaction.Commit();

            RefreshView();
        }