UserDetailsEditor.cs 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoyaAdminLib;
using System.Globalization;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Web.Script.Serialization;
using ThermoPrinterLibrary;
using System.Text.RegularExpressions;
using System.Threading;

namespace MoyaSignup
{
    public partial class UserDetailsEditor : UserControl
    {
        public event EventHandler CloseView;

        public static string BARCODE_PREFIX = "279";
        public static int BARCODE_LENGTH = 12;

        private ThermoPrinter thermoPrinter;
        public ThermoPrinter ThermoPrinter
        {
            set { thermoPrinter = value; }
            get { return thermoPrinter; }
        }

        User currentUser = null;
        public UserDetailsEditor()
        {
            InitializeComponent();
            cbGender.Items.Add("Valitse");
            cbGender.Items.Add("Mies");
            cbGender.Items.Add("Nainen");

            /*
            if(Properties.Settings.Default.ThermoPrinter == "TMT188II")
                thermoPrinter = new ThermoPrinter(ThermoPrinter.Models.TMT188II);
            else
                thermoPrinter = new ThermoPrinter(ThermoPrinter.Models.IDP3210);

            try
            {
                thermoPrinter.Start(Properties.Settings.Default.printerPort);
                thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
                thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
                thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
            }
            catch
            {
                thermoPrinter = null;
            }*/
            
            
        
        }
        
        public void LoadUser(User user)
        {
            btnSaveData.Visible = false;

            txtEmail.Text = user.Email;
            txtFirstName.Text = user.Firstname;
            txtLastName.Text = user.Lastname;
            txtNick.Text = user.Nick;
            //takePictureControl1.loadUserImage(user);
            txtAdress.Text = user.StreetAddress;
            txtZip.Text = user.ZipCode;
            txtCity.Text = user.postOffice;
            txtPhone.Text = user.PhoneNumber;

            DateTime birthday = DateTime.MinValue;
            DateTime.TryParse(user.Birthday, out birthday);

            if (user.Gender == "MALE")
                cbGender.SelectedItem = "Mies";
            else if (user.Gender == "FEMALE")
                cbGender.SelectedItem = "Nainen";
            else
                cbGender.SelectedItem = "Valitse";

            if(user.Birthday != null && user.Birthday.Length >= 10)
            txtAge.Text = user.Birthday.Substring(0, 10);

            if (user.UserId == 0) 
            {
                // new user
                takePictureControl1.TakePicture();
                txtFirstName.ReadOnly = false;
                txtLastName.ReadOnly = false;
                txtNick.ReadOnly = false;
                txtAge.ReadOnly = false;

                txtAdress.ReadOnly = false;
                txtZip.ReadOnly = false;
                txtCity.ReadOnly = false;
                txtPhone.ReadOnly = false;

                if (txtEmail.Text.Length > 0)
                {

                    string[] fields = txtEmail.Text.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
                    if (fields.Length == 2)
                    {

                        fields = fields[0].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                        if (fields.Length == 2)
                        {
                            txtFirstName.Text = fields[0].Substring(0, 1).ToUpper() + fields[0].Substring(1, fields[0].Length - 1).ToLower();
                            txtLastName.Text = fields[1].Substring(0, 1).ToUpper() + fields[1].Substring(1, fields[1].Length - 1).ToLower();
                        }
                    }
                }

            }
            else
            {
                // old user
                takePictureControl1.loadUserImage(user);
                btnSaveData.Text = "Tallenna kuva ja tulosta lipputosite";
                btnSaveData.Visible = true;
            } 
               
            currentUser = user;
            lblMessage.Text = "Voit pyytää muutoksia tietoihin infotiskillä";
        }


        


        void clear() 
        {
            this.cbGender.SelectedIndex = 0;
            txtAdress.Text = "";
            //txtAge.Text = "";
            txtCity.Text = "";
            txtEmail.Text = "";
            txtFirstName.Text = "";
            txtNick.Text = "";
            txtPhone.Text = "";
            txtZip.Text = "";
            txtAge.Text = ""; this.takePictureControl1.Clear();
            currentUser = null;

        }
        void checkFields()
        {
            string errorType = getErrorMessage();
            if (errorType != null)
            {
                this.lblMessage.Text = errorType + " puuttuu tai on virheellinen. Täytä puuttuvat kentät oikeilla tiedoilla.";
                this.lblMessage.ForeColor = Color.Red;
                return;
            }

        }
        string getErrorMessage()
        {
            int tmp;
            //checking validity

            if (this.txtFirstName.Text.Trim().Length < 5)
            {
                    return "Nimi";
            }
            else if (this.txtNick.Text.Trim().Length < 2)
            {
                    return "Nick (nimimerkki)";
            }
            /*else if (this.txtAge.Text.Trim().Length < 1 || (!int.TryParse(this.txtAge.Text.Trim(), out tmp)))
            {
                    return "Syntymäaika";
            }*/
            else if (this.cbGender.SelectedIndex == 0)
            {
                    return "Sukupuoli";
            }
            else if (this.txtPhone.Text.Length < 2)
            {
                    return "Puhelinnumero";
            }
            else if (this.txtEmail.Text.Trim().Length < 5)
            {
                    return "Sähköposti";
            }
            else if (this.txtAdress.Text.Trim().Length < 2)
            {
                    return "Lähiosoite";
            }
            else if (this.txtZip.Text.Trim().Length != 5 || (!int.TryParse(this.txtZip.Text.Trim(), out tmp)))
            {
                    return "Postinumero";
            }
            else if (this.txtCity.Text.Trim().Length < 2)
            {
                    return "Paikkakunta";
            }
            else
            {
                return null;
            }

        }

        private void UserDetailsEditor_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            takePictureControl1.Clear();
            takePictureControl1.Changed += takePictureControl1_Changed;
            //if(Debugger.IsAttached)
             //   btnSaveData.Visible = true;

         
        }

        void takePictureControl1_Changed(object sender, EventArgs e)
        {
            btnSaveData.Visible = true;
        }

        private void btnSaveData_Click(object sender, EventArgs e)
        {
            int userId = 0;
            string name = "";
            string info = "Siirry tämän kuitin kanssa infotiskille";
            if (currentUser.UserId > 0)
            {
                userId = currentUser.EventUserId;
                if(takePictureControl1.PictureTaken)
                    takePictureControl1.SaveUserImage(currentUser);
                name = currentUser.Firstname + " " + currentUser.Lastname;
            }
            else
            {
                Eventuser user = new Eventuser();
                user.login = txtEmail.Text;
                user.firstname = txtFirstName.Text;
                user.lastname = txtLastName.Text;
                user.nick = txtNick.Text;
                user.phoneNumber = txtPhone.Text;
                user.email = txtEmail.Text;
                user.streetAddress = txtAdress.Text;
                user.zipCode = txtZip.Text;
                user.postOffice = txtCity.Text;

                DateTime birthday = DateTime.MinValue;
                DateTime.TryParse(txtAge.Text, out birthday);
                if (birthday > DateTime.MinValue)
                    user.birthday = birthday.ToString("vvvv-MM-dd");

                //user.birthday = dateTimePickerAge.Value.ToString("vvvv-MM-dd");
                if (cbGender.SelectedItem != null && ((string)cbGender.SelectedItem) == "Mies")
                    user.gender = "MALE";
                else if (cbGender.SelectedItem != null && ((string)cbGender.SelectedItem) == "Nainen")
                    user.gender = "FEMALE";
                else
                    user.gender = "UNDEFINED";

                string password = "";
                string chars = "ABCDEFGHJKMNPQRSTUVWXYZ2345689" + "ABCDEFGHJKMNPQRSTUVWXYZ".ToLower();

                for (int k = 0; k < 8; k++)
                {
                    int i = Randomizer.Random.Next(chars.Length - 1);
                    password += chars[i];
                }

                Console.WriteLine("Password: " + password);

                user.password = password;

                RestClient client;

                client = new RestClient(RestClient.ApiURL, HttpVerb.POST);

                JavaScriptSerializer ser = new JavaScriptSerializer();

                client.PostData = ser.Serialize(user);
                client.ContentType = "application/json";
                try
                {
                    string json = client.MakeRequest("v2/user/create/");
                    Eventuser euser = ser.Deserialize<Eventuser>(json);
                    if (euser != null)
                    {
                        Console.WriteLine("EuserId: ", euser.eventuserId);
                        userId = euser.eventuserId;
                        currentUser = new User(euser);
                        takePictureControl1.SaveUserImage(currentUser);
                        name = euser.firstname + " " + euser.lastname;
                    }
                    else
                        Console.WriteLine("Euser is null!");

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                

            }

            string barcode = getBarcode(userId);

            if (thermoPrinter != null)
            {
                thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Left);
                thermoPrinter.PrintText(name.Replace("ä", "a").Replace("ö", "o").Replace("å", "a").Replace("Ä", "A").Replace("Ö", "O").Replace("Å", "A"));
                thermoPrinter.PrintAndFeedPaper(1);
                thermoPrinter.PrintText(info.Replace("ä", "a").Replace("ö", "o").Replace("å", "a").Replace("Ä", "A").Replace("Ö", "O").Replace("Å", "A"));
                thermoPrinter.PrintAndFeedPaper(2);
                thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
                thermoPrinter.PrintBarcode(ThermoPrinter.BarCodeTypes.CODE39, barcode);
                thermoPrinter.PrintAndFeedPaper(5);
                thermoPrinter.FullCut();
            }

            
            lblMessage.Text = "Tiedot on tallennettu. Voit siirtyä infotiskille tulostetun kuitin kanssa.";
            lblMessage.Visible = true;
            MessageBox.Show("Teidot tallennettu. Siirry infotiskille ja kerro nimesi.\r\nMikäli viereinen tulostin tulosti lapun ota se matkaan ja ojenna infotiskillä");

            //Thread.Sleep(3000);

            onCloseView();
        }

        private string getBarcode(int id)
        {
            string barcode = BARCODE_PREFIX;
            int padding = BARCODE_LENGTH - id.ToString().Length;
            barcode = barcode.PadRight(padding, '0');
            barcode += id.ToString();

            var allDigits = barcode.Select(c => int.Parse(c.ToString(CultureInfo.InvariantCulture))).ToArray();
            var s = 13 % 2 == 0 ? 3 : 1;
            var s2 = s == 3 ? 1 : 3;
            int checksum = (10 - (allDigits.Take(13 - 1).Select((c, ci) => c * (ci % 2 == 0 ? s : s2)).Sum() % 10)) % 10;

            barcode += checksum.ToString();

            return barcode;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            onCloseView();
        }
        private void onCloseView()
        {
            clear();
            if (CloseView != null)
                CloseView(this, null);
        }

        private void takePictureControl1_Load(object sender, EventArgs e)
        {

        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {

                //emailTimer.Enabled = true;
                //emailTimer.Start();
            
        }

        private void emailTimer_Tick(object sender, EventArgs e)
        {
            

        }

        private void txtAge_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtAge_Leave(object sender, EventArgs e)
        {
            
        }

        private void txtNick_TextChanged(object sender, EventArgs e)
        {
            /*
            if (txtNick.Text == "thermo2")
            {
                SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
                txtNick.Text = "OK";
            }
            else if (txtNick.Text == "thermo1")
            {
                SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.TMT188II);
                txtNick.Text = "OK";
            }*/

        }
    }
}