Class barang dan form barang


Class barang
                Class barang adalah background proses untuk tambah, edit, delete data barang dan beberapa fungsi dan prosedur lainnya. Sedangkan untuk proses input data akan dikerjakan oleh class form barang. Untuk codingnya sebagai berikut :
Class barang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

namespace SIP_Toko
{
    public class barang
    {
        public string kodebarang { get; set; }
        public string namabarang { get; set; }
        public string satuan { get; set; }
        public double hargabeli { get; set; }
        public int stockmin { get; set; }
        public int stockmax { get; set; }
        public int stock { get; set; }
        public int rate { get; set; }
        private CommonClass CClass = new CommonClass();
               
        public List<barang> barangx(int pilihan)
        {
            string sql ="";
            MySqlConnection db = new MySqlConnection(CClass.connectionSQL);
            db.Open();
            MySqlCommand dbcmd = db.CreateCommand();
            if (pilihan == 0)
            {
                sql = "select * from barang order by kodebarang;";
            }
            else if (pilihan == 1)
            {
                sql = "select * from barang order by kodebarang desc;";
            }

            dbcmd.CommandText = sql;
            MySqlDataReader reader = dbcmd.ExecuteReader();
            List<barang> xx = new List<barang>(40);
            while (reader.Read())
            {
                barang barangy = new barang();
                barangy.kodebarang = reader.GetString(0);
                barangy.namabarang = reader.GetString(1);
                barangy.satuan = reader.GetString(2);
                barangy.hargabeli = double.Parse(reader.GetString(3));
                barangy.stockmin = int.Parse(reader.GetString(4));
                barangy.stockmax = int.Parse(reader.GetString(5));
                barangy.stock = int.Parse(reader.GetString(6));
                barangy.rate = int.Parse(reader.GetString(7));
                xx.Add(barangy);
                // lakukan sesuatu sesuka anda didalam scope ini          
            }

            db.Close();
            return xx;
        }

        public bool SaveBarang(barang barangSave)
        {
            try
            {
                MySqlConnection db = new MySqlConnection(CClass.connectionSQL);
                db.Open();
                MySqlCommand dbcmd = db.CreateCommand();
                string sql = "INSERT INTO `barang`(`kodebarang`, `namabarang`,
 `satuan`, `hargabeli`, `stockmin`, `stockmax`, `stock`, `rate`) VALUES ('"
barangSave.kodebarang + "','" + barangSave.namabarang + "','" + barangSave.satuan +  
"'," + barangSave.hargabeli + "," + barangSave.stockmin + "," + barangSave.stockmax + 
"," + barangSave.stock + "," + barangSave.rate + ");";
                dbcmd.CommandText = sql;
                MySqlDataReader reader = dbcmd.ExecuteReader();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateBarang(barang barangSave)
        {
            try
            {
                MySqlConnection db = new MySqlConnection(CClass.connectionSQL);
                db.Open();
                MySqlCommand dbcmd = db.CreateCommand();
                string sql = "UPDATE `barang` SET `namabarang`='"
barangSave.namabarang + "',`satuan`='" + barangSave.satuan + "',`hargabeli`="
barangSave.hargabeli + ",`stockmin`='" + barangSave.stockmin + "',`stockmax`='"
barangSave.stockmax + "',`stock`='" + barangSave.stock + "',`rate`='"
barangSave.rate + "' WHERE kodebarang='"+ barangSave.kodebarang +"';";
                dbcmd.CommandText = sql;
                MySqlDataReader reader = dbcmd.ExecuteReader();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteBarang(string kodedelete)
        {
            try
            {
                MySqlConnection db = new MySqlConnection(CClass.connectionSQL);
                db.Open();
                MySqlCommand dbcmd = db.CreateCommand();
                string sql = "delete from barang where kodebarang ='"
kodedelete + "';";
                dbcmd.CommandText = sql;               
                MySqlDataReader reader = dbcmd.ExecuteReader();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}



Class form barang
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SIP_Toko
{
    public partial class FrmBarang : Form
    {
        private barang barangxx = new barang();
        private string kodes,msg;

        bool edit;
        Dictionary<string, List<barang>> databarang =  
new Dictionary<string, List<barang>>();

        public FrmBarang()
        {
            InitializeComponent();
            databarang.Add("lama", barangxx.barangx(0));

            Grid.Columns.Add("kodebarang", "Kode Barang");
            Grid.Columns.Add("namabarang", "Nama Barang");
            Grid.Columns.Add("satuan", "Satuan");
            Grid.Columns.Add("hargabeli", "Harga Beli");
            Grid.Columns.Add("stockmin", "Stock Min");
            Grid.Columns.Add("stockmax", "Stock Max");
            Grid.Columns.Add("stock", "Stock");
            Grid.Columns.Add("Rate", "Rate Jual(%)");

            IsiGrid();           
        }

        public string KodeAuto()
        {
            databarang["lama"] = databarang["lama"] = barangxx.barangx(1);
            if (databarang["lama"].Count == 0)
            {
                return "BR0001";
            }
            else
            {
                int no = int.Parse(databarang["lama"][0].kodebarang.Substring(3,3));
                no = no + 1;
               
                return string.Format("BR{0}",no.ToString("0000"));
            }
        }

        public void IsiGrid()
        {          
            Grid.Rows.Clear();

            foreach (var barangz in databarang["lama"])
            {
                Grid.Rows.Add(new string[] { barangz.kodebarang, 
barangz.namabarang, barangz.satuan, "Rp. " + barangz.hargabeli.ToString(),
                barangz.stockmin.ToString(),barangz.stockmax.ToString(),
barangz.stock.ToString(),barangz.rate.ToString() });
            }
        }

        public void tombol(bool status)
        {
            CmdTambah.Visible = status;
            CmdEdit.Visible = status;
            CmdDelete.Visible = status;
            CmdRefresh.Visible = status;

            CmdSave.Visible = !status;
            CmdCancel.Visible = !status;
           
            Grid.Visible = status;

        }


        private void FrmBarang_Load(object sender, EventArgs e)
        {
            tombol(true);
            Grid.Location = new Point(12, 12);
            kodes = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[0].Value.ToString();
            TxtKode.Text = KodeAuto();
        }

        private void CmdTambah_Click(object sender, EventArgs e)
        {
            tombol(false);
            this.Text = "Add new";
            edit = false;
            TxtKode.ReadOnly = false;
        }

        private void CmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            ClearScreen();
            tombol(true);
            IsiGrid();
            this.Text = "Barang";
        }

        private void Grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tombol(false);
            edit = true;
            TxtKode.Text = Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
            TxtNama.Text = Grid.Rows[e.RowIndex].Cells[1].Value.ToString();
            TxtSatuan.Text = Grid.Rows[e.RowIndex].Cells[2].Value.ToString();
            TxtHargaBeli.Text = Grid.Rows[e.RowIndex].Cells[3].Value.ToString();
            TxtStockMin.Text = Grid.Rows[e.RowIndex].Cells[4].Value.ToString();
            TxtStockMax.Text = Grid.Rows[e.RowIndex].Cells[5].Value.ToString();
            TxtStock.Text = Grid.Rows[e.RowIndex].Cells[6].Value.ToString();
            TxtRate.Text = Grid.Rows[e.RowIndex].Cells[7].Value.ToString();
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(kodes + " Yakin akan dihapus??", "Alert", MessageBoxButtons.OKCancel,MessageBoxIcon.Stop) == DialogResult.OK)
            {
                if(barangxx.DeleteBarang(kodes.Trim()))
                {
                    MessageBox.Show("Data deleted!");
                    databarang["lama"] = barangxx.barangx(0);
                    IsiGrid();
                }               
            }
           
        }

        public void ClearScreen()
        {
            TxtKode.Text = "";
            TxtNama.Text = "";
            TxtSatuan.Text = "";
            TxtHargaBeli.Text = "";
            TxtStockMin.Text = "";
            TxtStockMax.Text = "";
            TxtStock.Text = "";
            TxtRate.Text = "";
        }

        private void CmdRefresh_Click(object sender, EventArgs e)
        {
            databarang["lama"] = barangxx.barangx(0);
            IsiGrid();
        }

        private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            kodes = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[0].Value.ToString();
        }

        private void CmdEdit_Click(object sender, EventArgs e)
        {
            edit = true;
            tombol(false);
            this.Text = "Editing";

            TxtKode.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[0].Value.ToString();
            TxtNama.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[1].Value.ToString();
            TxtSatuan.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[2].Value.ToString();
            TxtHargaBeli.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[3].Value.ToString();
            TxtStockMin.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[4].Value.ToString();
            TxtStockMax.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[5].Value.ToString();
            TxtStock.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[6].Value.ToString();
            TxtRate.Text = Grid.Rows[Grid.CurrentCell.RowIndex].Cells[7].Value.ToString();
            TxtKode.ReadOnly = true;
        }

        public bool CheckIsi()
        {
            if (TxtKode.Text.Trim() == "")
            {
                TxtKode.Focus();
                msg ="Kode barang";
                return false;
            }
            else if (TxtNama.Text.Trim() == "")
            {
                TxtNama.Focus();
                msg ="Nama barang";
                return false;
            }
            else if (TxtSatuan.Text.Trim() == "")
            {
                TxtSatuan.Focus();
                msg ="Satuan";
                return false;
            }
            else if (TxtHargaBeli.Text.Replace("Rp ", "").Trim() == "")
            {
                TxtHargaBeli.Focus();
                msg ="Harga beli";
                return false;
            }
            else if (TxtStockMin.Text.Trim() == "")
            {
                TxtStockMin.Focus();
                msg ="Stock minimum";
                return false;
            }
            else if (TxtStockMax.Text.Trim() == "")
            {
                TxtStockMax.Focus();
                msg ="Stock maksimum";
                return false;
            }
            else if (TxtStock.Text.Trim() == "")
            {
                TxtStock.Focus();
                msg ="Stock";
                return false;
            }
            else if (TxtRate.Text.Replace("%", "").Trim() == "")
            {
                TxtRate.Focus();
                msg ="Rate";
                return false;
            }
            else
            {
                return true;
            }           
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            barang barangs = new barang();
            if (!CheckIsi())
            {
                MessageBox.Show(msg + " belum terisi!");
                return;
            }
            TxtKode.Text = KodeAuto();

            barangs.kodebarang = TxtKode.Text.Trim();
            barangs.namabarang = TxtNama.Text.Trim();
            barangs.satuan = TxtSatuan.Text.Trim();
            barangs.hargabeli = double.Parse(TxtHargaBeli.Text.Replace("Rp ", "").Trim());
            barangs.stockmax = int.Parse(TxtStockMax.Text.Trim());
            barangs.stockmin = int.Parse(TxtStockMin.Text.Trim());
            barangs.stock = int.Parse(TxtStock.Text.Trim());
            barangs.rate = int.Parse(TxtRate.Text.Replace("%", "").Trim());

            if (!edit)
            {
                if (barangxx.SaveBarang(barangs))
                {
                    MessageBox.Show("Data Saved!");
                    databarang["lama"] = barangxx.barangx(0);
                }
                IsiGrid();
                tombol(true);
            }
            else
            {
                if (barangxx.UpdateBarang(barangs))
                {
                    MessageBox.Show("Data Saved!");
                    databarang["lama"] = barangxx.barangx(0);
                }
                IsiGrid();
                tombol(true);
            }
           
        }
  
    }
}




Dan untuk tampilannya sebagai berikut :


Comments

Popular posts from this blog

Flowchart Penjualan Grosir / Eceran

Flowchart Proses Pembelian Barang