private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvBill.Rows.Count > 0)
            {
                if (int.Parse(txtAmountRem.Text) != 0 && txtCusName.Text.Equals(""))
                {
                    MessageBox.Show("يجب ادخال اسم العميل لأن المبلغ المدفوع لا يساوي قيمة الفاتورة");
                    return;
                }
                else
                {
                    try
                    {
                        con.Open();
                        OleDbCommand cmd = new OleDbCommand("insert into Sell_Bill (Date_B_Bill,Total,Description,Cus_Name,Discount,Seller_N,S_AmountPay,S_AmountRem)" +
                                                            "values ('" + txtDate.Text + "','" + txtTotal.Text + "','" + txtDiscription.Text + "','" + txtCusName.Text + "','" + txtDiscount.Text + "'," +
                                                            "'" + txtSellerN.Text + "','" + txtAmountPay.Text + "','" + txtAmountRem.Text + "')", con);
                        cmd.ExecuteNonQuery();
                        con.Close();


                        string           LastIDBill = "";
                        OleDbDataAdapter da         = new OleDbDataAdapter("select TOP 1 S_Bill_ID from Sell_Bill ORDER BY S_Bill_ID DESC", con);
                        DataTable        dt         = new DataTable();
                        da.Fill(dt);
                        LastIDBill = dt.Rows[0][0].ToString();


                        DataTable dt1 = new DataTable();
                        dt1.Columns.Add("P_ID");
                        dt1.Columns.Add("Pro_name");
                        dt1.Columns.Add("Cat_Name");
                        dt1.Columns.Add("Sell_Price");
                        dt1.Columns.Add("Qty");
                        for (int ii = 0; ii < dgvBill.Rows.Count; ii++)
                        {
                            dt1.Rows.Add(dgvBill.Rows[ii].Cells[0].Value.ToString(), dgvBill.Rows[ii].Cells[1].Value.ToString(), dgvBill.Rows[ii].Cells[2].Value.ToString(), dgvBill.Rows[ii].Cells[3].Value.ToString(), dgvBill.Rows[ii].Cells[4].Value.ToString());
                        }


                        for (int i = 0; i < dgvBill.Rows.Count; i++)
                        {
                            OleDbDataAdapter daq = new OleDbDataAdapter("select Qty from Product where Pro_ID = " + dt1.Rows[i][0].ToString() + "", con);
                            DataTable        dtq = new DataTable();
                            daq.Fill(dtq);
                            string qty = Convert.ToString(int.Parse(dtq.Rows[0][0].ToString()) - int.Parse(dt1.Rows[i][4].ToString()));
                            con.Open();
                            OleDbCommand cmd4 = new OleDbCommand("update Product set Qty= " + qty + " where Pro_ID = " + dt1.Rows[i][0].ToString() + "", con);
                            cmd4.ExecuteNonQuery();
                            con.Close();

                            con.Open();
                            OleDbCommand cmd1 = new OleDbCommand("insert into S_D_Bill (Pro_Name,Qty,S_Price,S_Bill_ID)" +
                                                                 "values ('" + dt1.Rows[i][1].ToString() + "','" + dt1.Rows[i][4].ToString() + "','" + dt1.Rows[i][3].ToString() + "'," + LastIDBill + ")", con);
                            cmd1.ExecuteNonQuery();
                            con.Close();
                        }


                        if (int.Parse(txtAmountRem.Text) > 0)
                        {
                            con.Open();
                            OleDbCommand cmd2 = new OleDbCommand("insert into Cus_Account (Cus_Am_Rem,S_Bill_ID,Cus_Name)" +
                                                                 "values ('" + txtAmountRem.Text + "'," + LastIDBill + ",'" + txtCusName.Text + "')", con);
                            cmd2.ExecuteNonQuery();
                            con.Close();
                        }

                        FRM_SellBill sb = new FRM_SellBill();
                        con.Open();
                        OleDbCommand cmd3 = new OleDbCommand("insert into BoxInfo (Proc_type,Proc_Date,Deposit,Withdraw,Discreption)" +
                                                             "values ('فاتورة بيع','" + txtDate.Text + "','" + txtAmountPay.Text + "','0','" + sb.Text + "')", con);
                        cmd3.ExecuteNonQuery();
                        con.Close();

                        btnSave.Enabled      = btnDelete.Enabled = btnUpdate.Enabled = btnCancel.Enabled = btnChoPro.Enabled = btnChoCus.Enabled = false;
                        txtDiscount.ReadOnly = txtAmountPay.ReadOnly = txtQty.ReadOnly = txtDiscription.ReadOnly = true;



                        MessageBox.Show("تمت عملية حفظ بيانات الفاتورة بنجاح");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("هناك خطأ في بيانات الفاتورة المدخلة");
                    }
                }
            }
            else
            {
                MessageBox.Show("لا يوجد أي منتج في الفاتورة");
            }
        }
Example #2
0
        private void TM_SBill_Click(object sender, EventArgs e)
        {
            FRM_SellBill sb = new FRM_SellBill();

            sb.ShowDialog();
        }