Advertisement
touhid_xml

Lab Report Add C#

Sep 22nd, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10. using System.Data.SQLite;
  11. using Newtonsoft.Json;
  12. namespace Checkup_Diagnostic
  13. {
  14.     public partial class AddTestReport : Form
  15.     {
  16.         //user data
  17.         private int userId;
  18.         private string userName;
  19.         private string fullName;
  20.         private int userRole;
  21.         private string loginTime;
  22.         // end of user data
  23.  
  24.         private CurrentUser CU = new CurrentUser();
  25.         private LabratoryHelper LH = new LabratoryHelper();
  26.         System.Drawing.Text.PrivateFontCollection privateFonts;
  27.         private Font _customFont;
  28.         private string conString = "SERVER=localhost;" + "DATABASE=dms;" +
  29.       "UID=dms;" + "PASSWORD=11223344;";
  30.  
  31.         /* Tables */
  32.         private string tablePatients = "patients";
  33.         private string tableDoctors = "doctors";
  34.         private string tableUsers = "users";
  35.         private string tableAppointments = "appointments";
  36.         private string tableReferences = "reference";
  37.  
  38.  
  39.         private List<CartItem> LabCart = new List<CartItem>();
  40.  
  41.         public AddTestReport()
  42.         {
  43.             InitializeComponent();
  44.             loadUserInfo();
  45.             CU.LoadInfo();
  46.             privateFonts = new System.Drawing.Text.PrivateFontCollection();
  47.             privateFonts.AddFontFile("fonts/open-sans/OpenSans-Regular.ttf");
  48.         }
  49.  
  50.         private void Labratory_Load(object sender, EventArgs e)
  51.         {
  52.              CU.LoadInfo(); //Load current userInfo
  53.              loadPatient(); //Load Patinet list to dropdown
  54.              cbxTest.Enabled = false;
  55.              button1.Enabled = false;
  56.              btnProceed.Enabled = false;
  57.  
  58.             //default value
  59.              tbxSubTotal.Text = "0";
  60.              tbxDiscount.Text = "0";
  61.              tbxTotal.Text = "0";
  62.            
  63.         }
  64.  
  65.         private void button1_Click(object sender, EventArgs e)
  66.         {
  67.            if(is_valid()){
  68.                LH.loadUserInfo(int.Parse(((ComboBoxItem)cbxTest.SelectedItem).HiddenValue.ToString()));
  69.                
  70.            
  71.             CartItem CartItem = new CartItem() {
  72.                 ID = LH.ItemId,
  73.                 TestName = LH.ItemName,
  74.                 Category = LH.Category,
  75.                 ItemPrice = LH.ItemPrice
  76.                
  77.  
  78.                 };
  79.  
  80.             LabCart.Add(CartItem);
  81.             dataGridView1.DataSource = null;
  82.             dataGridView1.DataSource = LabCart;
  83.                tbxDiscount.Enabled = true;
  84.                btnProceed.Enabled = true;
  85.  
  86.            
  87.                    decimal subtotal = LabCart.Sum(x => x.ItemPrice);
  88.                   // MessageBox.Show(subtotal.ToString());
  89.                    decimal number;
  90.                   // decimal discount;
  91.  
  92.                    tbxSubTotal.Text = subtotal.ToString();
  93.                  
  94.                        if (decimal.TryParse(tbxDiscount.Text.Trim(), out number))
  95.                        {
  96.                            decimal total = subtotal - number;
  97.                            tbxTotal.Text = total.ToString();
  98.                        }
  99.                    else{
  100.                        tbxTotal.Text = subtotal.ToString();
  101.                    }
  102.              
  103.               // MessageBox.Show();
  104.            }
  105.  
  106.            
  107.  
  108.            
  109.            
  110.  
  111.         }
  112.  
  113.         private bool is_valid()
  114.         {
  115.  
  116.             if (cbxPatients.SelectedIndex == -1)
  117.             {
  118.                 MessageBox.Show("Select Patient  Field is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  119.                 cbxPatients.Focus();
  120.                 return false;
  121.             }
  122.  
  123.  
  124.  
  125.             if (cbxTest.SelectedIndex == -1)
  126.             {
  127.                 MessageBox.Show("Select Test  Field is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  128.                 cbxPatients.Focus();
  129.                 return false;
  130.             }
  131.  
  132.             return true;
  133.         }
  134.  
  135.         private void loadPatient()
  136.         {
  137.             MySqlConnection connection = new MySqlConnection(this.conString);
  138.             MySqlCommand command = connection.CreateCommand();
  139.  
  140.  
  141.             MySqlDataReader Reader;
  142.  
  143.  
  144.             command.CommandText = "SELECT * FROM `patients` WHERE `status`= 1 ORDER BY `patient_id` DESC";
  145.  
  146.  
  147.  
  148.             connection.Open();
  149.  
  150.             Reader = command.ExecuteReader();
  151.             while (Reader.Read())
  152.             {
  153.  
  154.                 cbxPatients.Items.Add(new ComboBoxItem(Reader.GetValue(1).ToString(), Reader.GetValue(0).ToString()));
  155.  
  156.  
  157.             }
  158.             command.Cancel();
  159.             Reader.Close();
  160.             connection.Close();
  161.         }
  162.  
  163.  
  164.         private void cbxPatients_SelectedIndexChanged(object sender,
  165.         System.EventArgs e)
  166.         {
  167.             cbxPatients.Enabled = false;
  168.             cbxTest.Enabled = true;
  169.             button1.Enabled = true;
  170.             loadTests();
  171.  
  172.         }
  173.  
  174.         private void loadTests()
  175.         {
  176.             MySqlConnection connection = new MySqlConnection(this.conString);
  177.             MySqlCommand command = connection.CreateCommand();
  178.  
  179.  
  180.             MySqlDataReader Reader;
  181.  
  182.  
  183.             command.CommandText = "SELECT `item_id`,`item_name` FROM `test_items` WHERE `status` = 1 ORDER BY `item_id` DESC";
  184.  
  185.  
  186.  
  187.             connection.Open();
  188.  
  189.             Reader = command.ExecuteReader();
  190.             while (Reader.Read())
  191.             {
  192.  
  193.  
  194.                 cbxTest.Items.Add(new ComboBoxItem(Reader.GetValue(1).ToString(), Reader.GetValue(0).ToString()));
  195.  
  196.  
  197.             }
  198.             command.Cancel();
  199.             Reader.Close();
  200.             connection.Close();
  201.         }
  202.  
  203.         private void button2_Click(object sender, EventArgs e)
  204.         {
  205.             var confirmResult = MessageBox.Show("Are you sure to cancel the report?",
  206.                                     "Confirm Cancel!!",
  207.                                     MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  208.             if (confirmResult == DialogResult.Yes)
  209.             {
  210.                
  211.  
  212.             }
  213.             else
  214.             {
  215.                 //do nothing
  216.             }
  217.         }
  218.  
  219.         private void tbxDiscountOnChange(object sender, EventArgs e)
  220.         {
  221.  
  222.             if(is_valid_decimal()){
  223.                 decimal number;
  224.  
  225.                 if (decimal.TryParse(tbxDiscount.Text.Trim().ToString(), out number))
  226.                 {
  227.                     decimal total = decimal.Parse(tbxSubTotal.Text.Trim()) - number;
  228.                     tbxTotal.Text = total.ToString();
  229.                 }
  230.             }
  231.            
  232.         }
  233.  
  234.         private bool is_valid_decimal()
  235.         {
  236.             decimal number;
  237.  
  238.             if (tbxDiscount.Text.Trim() != string.Empty)
  239.             {
  240.                 if (!decimal.TryParse(tbxDiscount.Text.Trim().ToString(), out number))
  241.                 {
  242.                     MessageBox.Show("Only decimal number is allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  243.                     tbxDiscount.Text = "0";
  244.                     tbxDiscount.Focus();
  245.                     return false;
  246.                 }
  247.             }
  248.            
  249.             return true;
  250.         }
  251.  
  252.         private void printPreviewDialog1_Load(object sender, EventArgs e)
  253.         {
  254.            
  255.         }
  256.  
  257.         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  258.         {
  259.             Font _customFont = new Font(privateFonts.Families[0], 12, FontStyle.Regular);
  260.             //e.Graphics.DrawImage(Checkup_Diagnostic.Properties.Resources.mac_book_pro, 10, 10, Checkup_Diagnostic.Properties.Resources.mac_book_pro.Width, Checkup_Diagnostic.Properties.Resources.mac_book_pro.Height);
  261.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(10, 100));
  262.            
  263.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(10, 120));
  264.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Times New Roman", 14, FontStyle.Regular), Brushes.Black, new Point(10, 140));
  265.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(10, 160));
  266.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Tahoma", 14, FontStyle.Regular), Brushes.Black, new Point(10, 180));
  267.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", new Font("Georgia", 14, FontStyle.Regular), Brushes.Black, new Point(10, 200));
  268.             e.Graphics.DrawString("Hello World! Welcome to C# Programming. I am very happy to be a C# programmer.", _customFont, Brushes.Black, new Point(10, 220));
  269.            
  270.            
  271.  
  272.         }
  273.  
  274.         private void button3_MouseClick(object sender, MouseEventArgs e)
  275.         {
  276.            
  277.  
  278.         }
  279.  
  280.      
  281.         private void btnProceed_Click(object sender, EventArgs e)
  282.         {
  283.            // String jsonData = JsonConvert.SerializeObject(LabCart);
  284.            
  285.        
  286.            /**
  287.             foreach (DataGridViewRow row in dataGridView1.Rows)
  288.             {
  289.                 MessageBox.Show(row.Cells["ID"].Value.ToString());
  290.             }
  291.             **/
  292.          
  293.                 int PatientId = int.Parse(((ComboBoxItem)cbxPatients.SelectedItem).HiddenValue);
  294.                 int TestId;
  295.  
  296.                 MySqlConnection con = new MySqlConnection(this.conString);
  297.                 MySqlCommand command = con.CreateCommand();
  298.                 command.CommandText = "INSERT INTO `lab_report`(`report_patient`, `report_subtotal`, `report_discount`, `added_by`, `time_added`,  `status`) VALUES ( @patient , @subtotal , @discount , @user , @time , @status )";
  299.  
  300.                 command.Parameters.AddWithValue("@patient", PatientId);
  301.                 command.Parameters.AddWithValue("@subtotal", double.Parse(tbxSubTotal.Text.ToString()));
  302.                 command.Parameters.AddWithValue("@discount", double.Parse(tbxDiscount.Text.ToString()));
  303.                 command.Parameters.AddWithValue("@user", this.userId);
  304.                 command.Parameters.AddWithValue("@time", DateTime.Now);
  305.                 command.Parameters.AddWithValue("@status", 1);
  306.  
  307.                 con.Open();
  308.                     int a = command.ExecuteNonQuery();
  309.                     if (a > 0)
  310.                     {
  311.                         TestId = int.Parse(command.LastInsertedId.ToString());
  312.                        // MessageBox.Show("Insert ID: " + TestId.ToString());
  313.                         foreach(CartItem item in LabCart){
  314.                             //MessageBox.Show(item.ID.ToString());
  315.                             MySqlCommand cmd = con.CreateCommand();
  316.                             cmd.CommandText = "INSERT INTO `test_selected`(`ts_testid`,`ts_temid`, `ts_itemname`, `ts_price`,  `time_added`, `status`) VALUES ( @testid , @itemid , @itemname, @price, @time , @status )";
  317.                             cmd.Parameters.AddWithValue("@testid", TestId);
  318.                             cmd.Parameters.AddWithValue("@itemid", item.ID);
  319.                             cmd.Parameters.AddWithValue("@itemname", item.TestName);
  320.                             cmd.Parameters.AddWithValue("@price", item.ItemPrice);
  321.                             cmd.Parameters.AddWithValue("@time", DateTime.Now);
  322.                             cmd.Parameters.AddWithValue("@status", 1);
  323.                             int b = cmd.ExecuteNonQuery();
  324.                             if(b > 0){
  325.  
  326.                             }
  327.                             else
  328.                             {
  329.                                 MessageBox.Show("Unable to add to database. Item ID: " + item.ID.ToString() , "Database Error", MessageBoxButtons.OK,  MessageBoxIcon.Error);
  330.                             }
  331.                    
  332.  
  333.                         }
  334.  
  335.                         this.Close();
  336.                         MessageBox.Show("Test Report Assigned successfully to the Lab");
  337.                     }
  338.                     else
  339.                     {
  340.                         MessageBox.Show("Uanable to add Lab Report", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  341.                     }
  342.                     con.Close();
  343.  
  344.  
  345.            
  346.         }
  347.  
  348.         private bool is_valid_for_proceed()
  349.         {
  350.  
  351.             if (tbxSubTotal.Text.Trim() == String.Empty || int.Parse(tbxSubTotal.Text.Trim()) == 0)
  352.             {
  353.             MessageBox.Show("Add some test item", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  354.             return false;
  355.             }
  356.  
  357.             return true;
  358.         }
  359.  
  360.  
  361.         private void loadUserInfo()
  362.         {
  363.             // SQLiteConnection.CreateFile("app.sqlite");
  364.  
  365.             SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=app.sqlite;Version=3;");
  366.             m_dbConnection.Open();
  367.  
  368.  
  369.  
  370.  
  371.             string SQLTableRead = "SELECT * FROM login";
  372.  
  373.             SQLiteCommand command3 = new SQLiteCommand(SQLTableRead, m_dbConnection);
  374.             //command2.ExecuteNonQuery();
  375.             command3.CommandText = SQLTableRead;
  376.  
  377.             SQLiteDataReader TableReader = command3.ExecuteReader();
  378.  
  379.             if (TableReader.Read())
  380.             {
  381.                 //  MessageBox.Show(TableReader["id"].ToString());
  382.                 this.userId = int.Parse(TableReader["loginid"].ToString());
  383.                 this.userName = TableReader["username"].ToString();
  384.                 this.fullName = TableReader["fullname"].ToString();
  385.                 this.userRole = int.Parse(TableReader["role"].ToString());
  386.                 this.loginTime = TableReader["time_added"].ToString();
  387.             }
  388.  
  389.             TableReader.Close();
  390.             m_dbConnection.Close();
  391.  
  392.  
  393.         }
  394.  
  395.        
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement