using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace 全在庫管理
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
dataGridView1.MultiSelect = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
comboBox2.Enabled = false;
textBox1.TextChanged += new EventHandler(init_view);
textBox2.TextChanged += new EventHandler(init_view);
textBox3.TextChanged += new EventHandler(init_view);
textBox5.TextChanged += new EventHandler(init_view);
contextMenuStrip1.Items.Add("更新");
contextMenuStrip1.Items.Add("出庫");
contextMenuStrip1.Items.Add("複写");
contextMenuStrip1.Items.Add("削除");
init();
}
~Form1()
{
dt.Dispose();
}
private void init()
{
string q = @"select * from tbl where 出庫日 is null order by ID";
if (checkBox3.Checked) q = @"select * from tbl where 出庫日 is not null order by ID";
if (checkBox4.Checked) q = @"select * from tbl where 入庫日 = #" + DateTime.Now.ToString("yyyy/MM/dd") + "# order by ID";
if (checkBox5.Checked) q = @"select * from tbl where 出庫日 = #" + DateTime.Now.ToString("yyyy/MM/dd") + "# order by ID";
dt = Util.getDt(q);
list_add();
}
public void list_add()
{
comboBox1.Items.Clear();
comboBox2.Items.Clear();
using (DataView dv = new DataView(dt))
{
using (DataTable tmp = dv.ToTable(true, new string[] { "仕入月" }))
{
DataRow[] dr = tmp.Select("仕入月 is not null");
for (int i = 0; i < dr.Length; i++)
{
comboBox1.Items.Add(DateTime.Parse(dr[i]["仕入月"].ToString()).ToString("yyyy年MM月"));
comboBox2.Items.Add(DateTime.Parse(dr[i]["仕入月"].ToString()).ToString("yyyy年MM月"));
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
init_view(sender, e);
}
public void init_view(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
string tmp = "";
bool chk = false;
if (textBox2.Text != "")
{
tmp = "工番 like '%" + textBox2.Text + "%'";
chk = true;
}
if (textBox1.Text != "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + "製品名称 like '%" + textBox1.Text + "%'";
chk = true;
}
if (textBox3.Text != "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 仕入先 like '%" + textBox3.Text + "%'";
chk = true;
}
if (textBox5.Text != "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 区分 like '%" + textBox5.Text + "%'";
chk = true;
}
if (comboBox1.Text != "" && comboBox2.Enabled == false)
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 仕入月 = #" + DateTime.Parse(comboBox1.Text).ToString("yyyy/MM/01") + "#";
}
else if (comboBox1.Text != "" && comboBox2.Enabled == true && comboBox2.Text != "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 仕入月 >= #" + DateTime.Parse(comboBox1.Text).ToString("yyyy/MM/01")
+ "# and 仕入月 <= #" + DateTime.Parse(comboBox2.Text).ToString("yyyy/MM/01") + "#";
}
else if (comboBox1.Text != "" && comboBox2.Enabled == true && comboBox2.Text == "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 仕入月 >= #" + DateTime.Parse(comboBox1.Text).ToString("yyyy/MM/01") + "#";
}
else if (comboBox1.Text == "" && comboBox2.Enabled == true && comboBox2.Text != "")
{
if (chk) tmp = tmp + " and ";
tmp = tmp + " 仕入月 <= #" + DateTime.Parse(comboBox2.Text).ToString("yyyy/MM/01") + "#";
}
System.Diagnostics.Debug.Print(tmp.ToString());
DataRow[] dr = dt.Select(tmp, "ID");
decimal[,] rc = new decimal[6, 4];
for (int i = 0; i < dr.Length; i++)
{
decimal res00;
decimal.TryParse(dr[i]["数量"].ToString(), out res00);
decimal res01;
decimal.TryParse(dr[i]["計上単価"].ToString(), out res01);
DateTime res02;
if (!DateTime.TryParse(dr[i]["仕入月"].ToString(), out res02)) res02 = new DateTime(1970, 1, 1);
dataGridView1.Rows.Add(
dr[i]["ID"].ToString(),
dr[i]["工番"].ToString(),
dr[i]["製品名称"].ToString(),
dr[i]["仕入先"].ToString(),
res00.ToString(),
string.Format("{0:#,0}", res01),
string.Format("{0:#,0}", (res00 * res01)),
dr[i]["区分"].ToString(),
res02.ToString("yyyy年MM月"),
dr[i]["備考"].ToString()
);
switch (dr[i]["区分"].ToString())
{
case "内作材料①":
if ((res00 * res01) >= 50000)
{
rc[1, 2] = rc[1, 2] + (res00 * res01);
}
else
{
rc[1, 3] = rc[1, 3] + (res00 * res01);
}
rc[1, 1] = rc[1, 1] + (res00 * res01);
break;
case "外作材料①":
if ((res00 * res01) >= 50000)
{
rc[2, 2] = rc[2, 2] + (res00 * res01);
}
else
{
rc[2, 3] = rc[2, 3] + (res00 * res01);
}
rc[2, 1] = rc[2, 1] + (res00 * res01);
break;
case "内作製品":
if ((res00 * res01) >= 50000)
{
rc[3, 2] = rc[3, 2] + (res00 * res01);
}
else
{
rc[3, 3] = rc[3, 3] + (res00 * res01);
}
rc[3, 1] = rc[3, 1] + (res00 * res01);
break;
case "外作製品":
if ((res00 * res01) >= 50000)
{
rc[4, 2] = rc[4, 2] + (res00 * res01);
}
else
{
rc[4, 3] = rc[4, 3] + (res00 * res01);
}
rc[4, 1] = rc[4, 1] + (res00 * res01);
break;
default:
if ((res00 * res01) >= 50000)
{
rc[5, 2] = rc[5, 2] + (res00 * res01);
}
else
{
rc[5, 3] = rc[5, 3] + (res00 * res01);
}
rc[5, 1] = rc[5, 1] + (res00 * res01);
break;
}
}
textBox4.Text = string.Format("{0:#,0}", rc[1, 1]);
textBox8.Text = string.Format("{0:#,0}", rc[2, 1]);
textBox11.Text = string.Format("{0:#,0}", rc[3, 1]);
textBox14.Text = string.Format("{0:#,0}", rc[4, 1]);
textBox17.Text = string.Format("{0:#,0}", rc[5, 1]);
textBox20.Text = string.Format("{0:#,0}", rc[1, 1] + rc[2, 1] + rc[3, 1] + rc[4, 1] + rc[5, 1]);
textBox6.Text = string.Format("{0:#,0}", rc[1, 2]);
textBox9.Text = string.Format("{0:#,0}", rc[2, 2]);
textBox12.Text = string.Format("{0:#,0}", rc[3, 2]);
textBox15.Text = string.Format("{0:#,0}", rc[4, 2]);
textBox18.Text = string.Format("{0:#,0}", rc[5, 2]);
textBox21.Text = string.Format("{0:#,0}", rc[1, 2] + rc[2, 2] + rc[3, 2] + rc[4, 2] + rc[5, 2]);
textBox7.Text = string.Format("{0:#,0}", rc[1, 3]);
textBox10.Text = string.Format("{0:#,0}", rc[2, 3]);
textBox13.Text = string.Format("{0:#,0}", rc[3, 3]);
textBox16.Text = string.Format("{0:#,0}", rc[4, 3]);
textBox19.Text = string.Format("{0:#,0}", rc[5, 3]);
textBox22.Text = string.Format("{0:#,0}", rc[1, 3] + rc[2, 3] + rc[3, 3] + rc[4, 3] + rc[5, 3]);
label16.Text = dr.Length.ToString() + "件取得しました";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox5.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
checkBox1.Checked = false;
comboBox2.Enabled = false;
textBox23.Text = "";
textBox24.Text = "";
textBox25.Text = "";
textBox26.Text = "";
textBox27.Text = "";
textBox28.Text = "";
textBox29.Text = "";
textBox30.Text = "";
init_view(sender, e);
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGridView1.ClearSelection();
if (e.RowIndex >= 0 && e.ColumnIndex >= 0) dataGridView1.Rows[e.RowIndex].Selected = true;
foreach (ToolStripMenuItem tsmi in contextMenuStrip1.Items)
{
if (tsmi.Text == "出庫取消")
{
contextMenuStrip1.Items.Remove(tsmi);
break;
}
}
if (checkBox3.Checked || checkBox5.Checked) contextMenuStrip1.Items.Add("出庫取消");
}
}
private string[] parse_num(string[] tmp)
{
string[] res = new string[3];
decimal out001;
if (tmp[0] == "" || !decimal.TryParse(tmp[0], out out001))
{
res[0] = "0";
}
else
{
res[0] = out001.ToString();
}
decimal out002;
if (tmp[1] == "" || !decimal.TryParse(tmp[1], out out002))
{
res[1] = "0";
}
else
{
res[1] = out002.ToString();
}
DateTime out003;
if (tmp[2].Length == 6 && tmp[2].Substring(4, 1) != "/")
{
tmp[2] = tmp[2].Substring(0, 4) + "/" + tmp[2].Substring(4, 2) + "/1";
}
else if (tmp[2].Length == 6 && tmp[2].Substring(4, 1) == "/")
{
tmp[2] = tmp[2].Substring(0, 4) + "/" + tmp[2].Substring(5, 1) + "/1";
}
else if (tmp[2].Length == 7 && tmp[2].Substring(4, 1) == "/")
{
tmp[2] = tmp[2].Substring(0, 4) + "/" + tmp[2].Substring(5, 2) + "/1";
}
if (tmp[2] == "" || !DateTime.TryParse(tmp[2], out out003))
{
res[2] = "1970/01/01";
}
else
{
res[2] = out003.ToString("yyyy/MM/01");
}
return res;
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (dataGridView1.Rows.Count == 0) return;
set_selection(dataGridView1.SelectedRows[0].Index);
string q = "";
switch (e.ClickedItem.Text.ToString())
{
case "更新":
if (dgvr.Cells[4].Value == null || dgvr.Cells[5].Value == null || dgvr.Cells[8].Value == null)
{
MessageBox.Show("数量、計上単価、仕入月が空白では処理できません。");
return;
}
string[] res = parse_num(new string[] {
dgvr.Cells[4].Value.ToString(),
dgvr.Cells[5].Value.ToString(),
dgvr.Cells[8].Value.ToString()
} );
q = @"update tbl set " +
"工番='" + dgvr.Cells[1].Value.ToString() + "'," +
"製品名称='" + dgvr.Cells[2].Value.ToString() + "'," +
"仕入先='" + dgvr.Cells[3].Value.ToString() + "'," +
"数量=" + res[0] + "," +
"計上単価=" + res[1] + "," +
"区分='" + dgvr.Cells[7].Value.ToString() + "'," +
"仕入月=#" + res[2] + "#," +
"備考='" + dgvr.Cells[9].Value.ToString() + "' " +
"where ID = " + dgvr.Cells[0].Value.ToString() + "";
break;
case "出庫":
q = @"update tbl set " +
"出庫日=#" + DateTime.Now.ToString("yyyy/MM/dd") + "# " +
"where ID = " + dgvr.Cells[0].Value.ToString() + "";
break;
case "出庫取消":
q = @"update tbl set " +
"出庫日=NULL " +
"where ID = " + dgvr.Cells[0].Value.ToString() + "";
break;
case "複写":
q = @"insert into tbl select 工番,製品名称,仕入先,数量,計上単価,区分,仕入月,備考,出庫日,入庫日 from tbl " +
"where ID = " + dgvr.Cells[0].Value.ToString() + "";
break;
case "削除":
q = @"delete from tbl " +
"where ID = " + dgvr.Cells[0].Value.ToString() + "";
break;
}
Util.exeQry(q);
init();
init_view(sender, e);
init_selection(e.ClickedItem.Text.ToString());
MessageBox.Show(e.ClickedItem.Text.ToString() + "しました。");
}
private void init_selection(string tmp)
{
if (dataGridView1.Rows.Count == 0) return;
switch (tmp)
{
case "更新":
case "出庫":
case "削除":
if (dgv_rows_count == dataGridView1.Rows.Count)
{
dataGridView1.Rows[dgv_selected].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dgv_view_index;
}
else
{
if (dgv_selected >= 1) dataGridView1.Rows[dgv_selected - 1].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dgv_view_index;
}
break;
case "複写":
case "新規":
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;
break;
}
}
private DataGridViewRow dgvr;
private int dgv_view_index;
private int dgv_selected;
private int dgv_rows_count;
private void set_selection(int rows_index)
{
dgvr = dataGridView1.SelectedRows[0];
dgv_view_index = dataGridView1.FirstDisplayedScrollingRowIndex;
dgv_selected = rows_index;
dgv_rows_count = dataGridView1.Rows.Count;
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
DateTime out001;
if (!DateTime.TryParse(comboBox1.Text, out out001)) comboBox1.Text = "";
init_view(sender, e);
}
private void comboBox2_TextChanged(object sender, EventArgs e)
{
DateTime out001;
if (!DateTime.TryParse(comboBox2.Text, out out001)) comboBox2.Text = "";
init_view(sender, e);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
comboBox2.Enabled = true;
}
else
{
comboBox2.Text = "";
comboBox2.Enabled = false;
}
init_view(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
checkBox3.Checked = false;
string[] res = parse_num(new string[] {
textBox26.Text,
textBox27.Text,
textBox29.Text
});
string q = @"insert into tbl (工番,製品名称,仕入先,数量,計上単価,区分,仕入月,備考,入庫日) values " +
"('" + textBox23.Text + "','" + textBox24.Text + "','" + textBox25.Text + "'" +
"," + res[0] + "," + res[1] + ",'" + textBox28.Text + "',#" + res[2] + "#,'" + textBox30.Text + "',#" + DateTime.Now.ToString("yyyy/MM/dd") + "#)";
Util.exeQry(q);
init();
init_view(sender, e);
init_selection("新規");
button1.PerformClick();
MessageBox.Show("新規登録しました。");
}
private void btn5678_click(object sender, EventArgs e)
{
switch (((Button)sender).Text)
{
case "更新":
contextMenuStrip1.Items[0].PerformClick();
break;
case "出庫":
contextMenuStrip1.Items[1].PerformClick();
break;
case "複写":
contextMenuStrip1.Items[2].PerformClick();
break;
case "削除":
contextMenuStrip1.Items[3].PerformClick();
break;
}
}
private void btn34_click(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("表示されている行が" + ((Button)sender).Text + "されます。実行しますか?","",MessageBoxButtons.OKCancel);
if (res == DialogResult.Cancel) return;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
string q = "";
if (((Button)sender).Text == "全て出庫")
{
q = @"update tbl set " +
"出庫日=#" + DateTime.Now.ToString("yyyy/MM/dd") + "# " +
"where ID = " + dataGridView1.Rows[r].Cells[0].Value.ToString() + "";
}
else
{
q = @"delete from tbl " +
"where ID = " + dataGridView1.Rows[r].Cells[0].Value.ToString() + "";
}
Text = r + "件目処理中...";
Util.exeQry(q);
}
Text = "全在庫管理 v1.0";
init();
init_view(sender, e);
button1.PerformClick();
MessageBox.Show(((Button)sender).Text + "しました。");
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
checkBox4.Checked = false;
checkBox5.Checked = false;
}
init();
init_view(sender, e);
button1.PerformClick();
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
{
checkBox3.Checked = false;
checkBox5.Checked = false;
}
init();
init_view(sender, e);
button1.PerformClick();
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
if (checkBox5.Checked)
{
checkBox3.Checked = false;
checkBox4.Checked = false;
}
init();
init_view(sender, e);
button1.PerformClick();
}
}
}