Beispiel #1
0
 public bool Save(ReciveOrder newReciveOrder, string _WHCode)
 {
     int TempId = dc.CreateReciveOrder(newReciveOrder,_WHCode);
     if (TempId > 0)
     {
         return true;
     }
     return false;
 }
        public ReciveOrder getByReciveCode(string _reciveCode)
        {
            ReciveOrder reciveOrder = null;

            try
            {
                Conn = db.openConnAccess();
                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" SELECT ID,ReciveOrderCode,DateRecive,PO,SPCode,NetDc,NetVat,Net FROM tbReciveOrder ");
                sb.Append(" WHERE (ReciveOrderCode='" + _reciveCode + "')");
                string sql;
                sql = sb.ToString();

                com = new OleDbCommand();
                com.CommandText = sql;
                com.CommandType = CommandType.Text;
                com.Connection = Conn;
                dr = com.ExecuteReader();
                if (dr.HasRows)
                {

                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    int index = 1;
                    foreach (DataRow drw in dt.Rows)
                    {
                        reciveOrder = new ReciveOrder();
                        reciveOrder.ID = Convert.ToInt32(drw["ID"].ToString());
                        reciveOrder.Index = Convert.ToString(index);
                        reciveOrder.ReciveCode = drw["ReciveOrderCode"].ToString();
                        reciveOrder.DateRecive =Convert.ToDateTime(drw["DateRecive"].ToString());
                        reciveOrder.PO = drw["PO"].ToString();
                        reciveOrder.SPCode = drw["SPCode"].ToString();
                        Supiler supiler = serviceSuplier.getByCode(drw["SPCode"].ToString());
                        reciveOrder.SPName = supiler.SPName;
                        IList<ReciveOrderDetail> reciveOrderDetails = this.getReciveOrderDetailByReciveCode(drw["ReciveOrderCode"].ToString());
                        reciveOrder.reciveOrderDetails = reciveOrderDetails;
                        reciveOrder.NetDc = Convert.ToDouble(drw["NetDc"].ToString());
                        reciveOrder.NetVat = Convert.ToDouble(drw["NetVat"].ToString());
                        reciveOrder.Net = Convert.ToDouble(drw["Net"].ToString());

                    }

                }

                dr.Close();

            }
            catch (Exception ex)
            {
                dr.Close();
                Conn.Close();
                return null;
                throw ex;

            }
            finally
            {
                Conn.Close();
            }

            return reciveOrder;
        }
        public int CreateReciveOrder(ReciveOrder newReciveOrder, string _WHCode)
        {
            int result = -1;
            try
            {
                Conn = db.openConnAccess();
                tr = Conn.BeginTransaction();

                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO tbReciveOrder(ReciveOrderCode,DateRecive,PO,SPCode,NetDc,NetVat,Net)");
                sb.Append(" VALUES (@ReciveOrderCode,@DateRecive,@PO,@SPCode,@NetDc,@NetVat,@Net)");

                string sqlSave;
                sqlSave = sb.ToString();

                com = new OleDbCommand();
                com.Connection = Conn;
                com.CommandText = sqlSave;
                com.Transaction = tr;
                com.Parameters.Clear();
                com.Parameters.Add("@ReciveOrderCode", OleDbType.VarChar).Value = newReciveOrder.ReciveCode;
                string dateRecive = String.Format("{0:dd/MM/yyyy}", newReciveOrder.DateRecive);
                com.Parameters.Add("@DateRecive", OleDbType.VarChar).Value = dateRecive;
                com.Parameters.Add("@PO", OleDbType.VarChar).Value = newReciveOrder.PO;
                com.Parameters.Add("@SPCode", OleDbType.VarChar).Value = newReciveOrder.SPCode;
                com.Parameters.Add("@NetDc", OleDbType.VarChar).Value = newReciveOrder.NetDc;
                com.Parameters.Add("@NetVat", OleDbType.VarChar).Value = newReciveOrder.NetVat;
                com.Parameters.Add("@Net", OleDbType.VarChar).Value = newReciveOrder.Net;
                com.ExecuteNonQuery();

                foreach (ReciveOrderDetail r in newReciveOrder.reciveOrderDetails)
                {
                    sb.Remove(0, sb.Length);
                    sb.Append("INSERT INTO tbReciveOrderDetail(ReciveOrderCode,PCode,Cost,NumberRecive,Total)");
                    sb.Append(" VALUES (@ReciveOrderCode,@PCode,@Cost,@NumberRecive,@Total)");

                    string sqlSaveDetail;
                    sqlSaveDetail = sb.ToString();

                    com = new OleDbCommand();
                    com.Connection = Conn;
                    com.CommandText = sqlSaveDetail;
                    com.Transaction = tr;
                    com.Parameters.Clear();
                    com.Parameters.Add("@ReciveOrderCode", OleDbType.VarChar).Value = r.ReciveCode;
                    com.Parameters.Add("@PCode", OleDbType.VarChar).Value = r.PCode;
                    com.Parameters.Add("@Cost", OleDbType.VarChar).Value = r.Cost;
                    com.Parameters.Add("@NumberRecive", OleDbType.VarChar).Value = r.NumberRecive;
                    com.Parameters.Add("@Total", OleDbType.VarChar).Value = r.Total;
                    com.ExecuteNonQuery();

                    ProductOnWareHouse pOnwarehouse = serviceProductOnWarhose.getByPCodeAndWHCode(r.PCode, _WHCode);

                    if (pOnwarehouse == null)
                    {
                        ProductOnWareHouse newproductOnWareHouse = new ProductOnWareHouse();
                        newproductOnWareHouse.PCode = r.PCode;
                        newproductOnWareHouse.WHCode = _WHCode;
                        Product p = serviceProduct.getByCode(r.PCode);
                        newproductOnWareHouse.UCode = p.UCode;
                        Unit u = serviceUnit.getByCode(p.UCode);
                        newproductOnWareHouse.QtyWithUnit = u.QtyUnit;
                        int curnumber = r.NumberRecive * u.QtyUnit;
                        newproductOnWareHouse.RealQty = curnumber;

                        int tmp = serviceProductOnWarhose.CreateProductOnWareHouse(newproductOnWareHouse);
                        if (tmp > 0) {
                            Console.WriteLine("save newproductOnWareHouse Complate");
                        }

                    }
                    else {

                        Product p = serviceProduct.getByCode(r.PCode);
                        pOnwarehouse.UCode = p.UCode;
                        Unit u = serviceUnit.getByCode(p.UCode);
                        pOnwarehouse.QtyWithUnit = u.QtyUnit;

                        int oldblance = pOnwarehouse.RealQty;
                        int curnumber = r.NumberRecive * u.QtyUnit;
                        int newbalnce = oldblance + curnumber;
                        pOnwarehouse.RealQty = newbalnce;
                        int tmp = serviceProductOnWarhose.UpdateProductOnWareHouse(pOnwarehouse);
                        if (tmp > 0)
                        {
                            Console.WriteLine("update pOnwarehouse Complate");
                        }

                    }

                    ProductWithCost productWithCost = serviceProductWithCost.getByPCodeAndCost(r.PCode,Convert.ToString(r.Cost));

                    if (productWithCost == null)
                    {
                        ProductWithCost newproductWithCost = new ProductWithCost();
                        newproductWithCost.PCode = r.PCode;
                        newproductWithCost.Cost = r.Cost;
                        newproductWithCost.RecordDate = DateTime.Today;
                        int tmp = serviceProductWithCost.CreateProductWithCost(newproductWithCost);
                        if (tmp > 0)
                        {
                            Console.WriteLine("save newproductWithCost Complate");
                        }
                    }
                    else {

                        productWithCost.PCode = r.PCode;
                        productWithCost.Cost = r.Cost;
                        productWithCost.RecordDate = DateTime.Today;
                        int tmp = serviceProductWithCost.UpdateProductWithCost(productWithCost);
                        if (tmp > 0)
                        {
                            Console.WriteLine("update productWithCost Complate");
                        }

                    }

                    ProductOnSupiler _productOnSupiler = servicePrductOnSupiler.getByPCodeAndSPCost(r.PCode, newReciveOrder.SPCode);

                    if (_productOnSupiler ==null)
                    {
                        ProductOnSupiler newProductOnSupiler = new ProductOnSupiler();
                        newProductOnSupiler.PCode = r.PCode;
                        newProductOnSupiler.SPCode = newReciveOrder.SPCode;
                        newProductOnSupiler.Cost = r.Cost;
                        newProductOnSupiler.RecordDate = DateTime.Today;
                        int tmp = servicePrductOnSupiler.CreateProductOnSupiler(newProductOnSupiler);
                        if (tmp > 0)
                        {
                            Console.WriteLine("save newProductOnSupiler Complate");
                        }
                    }
                    else {
                        _productOnSupiler.PCode = r.PCode;
                        _productOnSupiler.SPCode = newReciveOrder.SPCode;
                        _productOnSupiler.Cost = r.Cost;
                        _productOnSupiler.RecordDate = DateTime.Today;

                        int tmp = servicePrductOnSupiler.UpdateProductOnSuplier(_productOnSupiler);
                        if (tmp > 0)
                        {
                            Console.WriteLine("update _productOnSupiler Complate");
                        }

                    }

                    Product updateProduct = serviceProduct.getByCode(r.PCode);
                    updateProduct.LastCost = r.Cost;
                    int tmpProduct = serviceProduct.UpdateProduct(updateProduct);
                    if (tmpProduct > 0)
                    {
                        Console.WriteLine("update updateProduct Complate");
                    }

                }

                tr.Commit();
                result = 1;

            }
            catch (Exception ex)
            {
                tr.Rollback();
                Conn.Close();
                return result;
                throw ex;

            }
            finally
            {
                Conn.Close();
            }

            return result;
        }
Beispiel #4
0
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (txtSearchSupiler.Text.Trim() == "")
            {
                MessageBox.Show("กรุณากรอกรหัสผู้จัดจำหน่ายด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSearchSupiler.Focus();
                return;
            }

            int count = lsvProductList.Items.Count;
            if (count == 0) {
                MessageBox.Show("กรุณาเลือกสินค้าด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSearchProduct.Focus();
                return;

            }
            if (MessageBox.Show("คุณต้องการเพิ่มการรับสินค้าใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {

                    ReciveOrder reciveOrder = serviceReciveOrder.getByReciveCode(txtPR.Text.Trim());
                    if (reciveOrder == null)
                    {
                        ReciveOrder newReciveOrder = new ReciveOrder();
                        newReciveOrder.ReciveCode = txtPR.Text.Trim();
                        newReciveOrder.DateRecive = dtpRecive.Value;
                        newReciveOrder.PO = txtPO.Text.Trim();
                        newReciveOrder.SPCode = txtSearchSupiler.Text.Trim();
                        newReciveOrder.SPName = txtSupilerName.Text.Trim();
                        newReciveOrder.NetDc = Convert.ToDouble(lblNetDC.Text.Trim());
                        newReciveOrder.NetVat = Convert.ToDouble(lblNetVAT.Text.Trim());
                        newReciveOrder.Net = Convert.ToDouble(lblNet.Text.Trim());

                        IList<ReciveOrderDetail> _reciveOrderDetails = new List<ReciveOrderDetail>();
                        ReciveOrderDetail _reciveOrderDetail;

                        int j = 1;
                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            _reciveOrderDetail =new ReciveOrderDetail();
                            _reciveOrderDetail.Index = Convert.ToString(j);
                            _reciveOrderDetail.ReciveCode=txtPR.Text.Trim();
                            _reciveOrderDetail.PCode = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            _reciveOrderDetail.PName = lsvProductList.Items[i].SubItems[1].Text.Replace(",", "");
                            _reciveOrderDetail.Cost =Convert.ToDouble(lsvProductList.Items[i].SubItems[2].Text.Replace(",", ""));
                            _reciveOrderDetail.NumberRecive = Convert.ToInt32(lsvProductList.Items[i].SubItems[3].Text.Replace(",", ""));
                            _reciveOrderDetail.Total = Convert.ToDouble(lsvProductList.Items[i].SubItems[4].Text.Replace(",", ""));
                            _reciveOrderDetails.Add(_reciveOrderDetail);
                             j++;

                        }

                        newReciveOrder.reciveOrderDetails = _reciveOrderDetails;

                        bool save = serviceReciveOrder.Save(newReciveOrder,cboWarehouse.SelectedValue.ToString());
                        if (save)
                        {
                            MessageBox.Show("เพิ่มการรับสินค้า เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ClearNet();
                            ClearProduct();
                            txtPO.Text = "";
                            lsvProductList.Items.Clear();
                            txtSearchProduct.Focus();
                            ClearSupiler();
                            txtPR.Text = serviceReciveOrder.getReciveCode();
                        }
                        else
                        {
                            MessageBox.Show("ไม่สามารถ เพิ่มการรับสินค้าใหม่ได้!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }
                    }
                    else
                    {

                        MessageBox.Show("มีการรับสินค้านี้อยู่แล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("ไม่สามารถ เพิ่มการรับสินค้าได้ เนื่องจาก !!! : " + ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }