Subversion Repository Public Repository

ChrisCompleteCodeTrunk

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Action_Tire_Payment_Processor
{
    public partial class GiftCardRegistrationForm : Form
    {
        public GiftCardRegistrationForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int GiftCardNumber = 0;
            int GiftCardNumberTo = 0;
            float CardAmount = 0.00f;

            try
            {
                GiftCardNumber = int.Parse(tbGiftCardNumber.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An invalid Gift Card # has been entered.");
                tbGiftCardNumber.Focus();
                return;
            }
            try
            {
                if (!tbGiftCardNumberEnd.Text.Trim().Equals(""))
                {
                    GiftCardNumberTo = int.Parse(tbGiftCardNumberEnd.Text);
                }
                else
                {
                    GiftCardNumberTo = GiftCardNumber;
                }
                if (GiftCardNumber > GiftCardNumberTo) throw new Exception("Invalid Card Number Range");
            }
            catch
            {
                MessageBox.Show("An invalid Card # range has been entered.");
                tbGiftCardNumberEnd.Focus();
                return;
            }
            try
            {
                CardAmount = float.Parse(tbAmount.Text);
                if (CardAmount <= 0f) throw new Exception("Bad Amount");
            }
            catch
            {
                MessageBox.Show("An invalid $ amount has been entered.");
                tbAmount.Focus();
                return;
            }

            string query = "SELECT * FROM Gift_Card_Registry WHERE CardNbr >= @CARDNUMBER AND CardNbr <= @CARDNUMBERTO";
            SqlCommand cmd = new SqlCommand(query, Database.sql);
            cmd.Parameters.AddWithValue("@CARDNUMBER", GiftCardNumber);
            cmd.Parameters.AddWithValue("@CARDNUMBERTO", GiftCardNumberTo);
            DataTable results = Database.Query(cmd);

            if (results.Rows.Count == 1)
            {
                MessageBox.Show("Card #" + GiftCardNumber + " is already registered with a balance of $" + results.Rows[0]["Balance"]);
                return;
            }
            if (results.Rows.Count > 1)
            {
                string cards = "There are "+results.Rows.Count+" existing cards within the specified range.\nPlease see the list of the duplicates below and refine the Card # range so that it does not include any of the existing cards.\n";
                foreach (DataRow r in results.Rows)
                {
                    cards += r["CardNbr"].ToString();
                    if (r != results.Rows[results.Rows.Count-1])
                    {
                        cards += ", ";
                    }
                }
                MessageBox.Show(cards);
                return;
            }

            IEnumerable<int> cardNumbers = Enumerable.Range(GiftCardNumber, (GiftCardNumberTo-GiftCardNumber)+1);

            string msg = "";

            foreach (int c in cardNumbers)
            {
                query = "INSERT INTO Gift_Card_Registry (CardNbr, CardType, OriginalAmount, Balance) VALUES (@CardNumber, @CardType, @OriginalAmount, @Balance)";
                cmd = new SqlCommand(query, Database.sql);
                cmd.Parameters.AddWithValue("@CardNumber", c);
                cmd.Parameters.AddWithValue("@CardType", "Gift Card");
                cmd.Parameters.AddWithValue("@OriginalAmount", CardAmount);
                cmd.Parameters.AddWithValue("@Balance", CardAmount);
                Database.NonQuery(cmd);

                query = "INSERT INTO Gift_Card_Transactions (StoreID, TransType, Amount, CardNbr, CardType, DateCreated, CreatedBy) VALUES " +
                    "(@StoreID, @TransType, @Amount, @CardNbr, @CardType, GetDate(), @CreatedBy)";
                cmd = new SqlCommand(query, Database.sql);
                cmd.Parameters.AddWithValue("@StoreID", Settings.GetOption("STORE"));
                cmd.Parameters.AddWithValue("@TransType", "load");
                cmd.Parameters.AddWithValue("@Amount", CardAmount);
                cmd.Parameters.AddWithValue("@CardNbr", c);
                cmd.Parameters.AddWithValue("@CardType", "Gift Card");
                cmd.Parameters.AddWithValue("@CreatedBy", Environment.UserName);
                Database.NonQuery(cmd);

                msg += "Gift Card #" + c + " registered with a balance of $" + String.Format("{0:C}", CardAmount) + "\n";
            }

            MessageBox.Show(msg);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void tbAmount_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbAmount_Leave(object sender, EventArgs e)
        {
            if (tbAmount.Text.IndexOf('.') == -1) tbAmount.Text += ".00";
            if (tbAmount.Text.Length == 1 && tbAmount.Text[0] == '.') tbAmount.Text = "0"+tbAmount.Text;
            if (tbAmount.Text.Length >= 1 && tbAmount.Text[tbAmount.Text.Length - 1] == '.') tbAmount.Text += "00";
            if (tbAmount.Text.Length >= 3 && tbAmount.Text[tbAmount.Text.Length - 2] == '.') tbAmount.Text += "0";
        }
    }
}

Commits for ChrisCompleteCodeTrunk/ATTP/Action Tire Payment Processor/GiftCardRegistrationForm.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000