private void BtnExport_Click(object sender, EventArgs e)
        {
            if (index == -1)
            {
                MessageBox.Show("Please choose a product to export");
            }
            else
            {
                if (validate())
                {
                    string exportID    = userID;
                    string productID   = txtProductID.Text.Trim();
                    string productName = txtProductName.Text;
                    string dateExport  = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    int    numPack     = int.Parse(txtNumberPackage.Text);
                    int    numSub      = 0;
                    if (!string.IsNullOrEmpty(txtNumberSub.Text))
                    {
                        numSub = int.Parse(txtNumberSub.Text);
                    }
                    float pricePackageExport   = listProduct.ElementAt(index).PriceToExportPackage;
                    float priceSubExport       = listProduct.ElementAt(index).PriceToExportSub;
                    int   currentNumberPackage = ProDAO.getNumberPackageByID(productID);
                    int   currentNumSub        = ProDAO.getNumSub(productID);
                    int   currentCount         = ProDAO.getCountExport(productID);
                    if (numPack == 0 && numSub == 0)
                    {
                        MessageBox.Show("please input a number of package or dub package to export");
                        return;
                    }
                    if (pricePackageExport != 0)
                    {
                        if (numSub == 0)
                        {
                            if (numPack <= currentNumberPackage && numSub <= currentNumSub)
                            {
                                Export ep = new Export();
                                ep.ExportID              = userID;
                                ep.ProductID             = productID;
                                ep.ProductName           = productName;
                                ep.DateExport            = dateExport;
                                ep.NumberPackageExport   = numPack;
                                ep.NumberSubExport       = numSub;
                                ep.PricePackageExport    = pricePackageExport;
                                ep.PriceSubPackageExport = priceSubExport;
                                ep.PriceExport           = numPack * pricePackageExport;
                                ep.CountExport           = currentCount;

                                bool checkUpdateProdutc = ProDAO.updateProductAtCountExport(0, currentNumberPackage - numPack, productID);
                                bool checkInsertExport  = exportDAO.insertExport(ep);
                                if (checkUpdateProdutc && checkInsertExport)
                                {
                                    MessageBox.Show("Add to export list successful");
                                    listExport.Add(ep);
                                    index                 = -1;
                                    txtProductID.Text     = "";
                                    txtProductName.Text   = "";
                                    txtDesCription.Text   = "";
                                    txtNumberPackage.Text = "";
                                    txtNumberSub.Text     = "";
                                    lbPackage.Text        = "";
                                    lbSub.Text            = "";
                                }
                                else
                                {
                                    MessageBox.Show("Add to export list fail");
                                    index                 = -1;
                                    txtProductID.Text     = "";
                                    txtProductName.Text   = "";
                                    txtDesCription.Text   = "";
                                    txtNumberPackage.Text = "";
                                    txtNumberSub.Text     = "";
                                    lbPackage.Text        = "";
                                    lbSub.Text            = "";
                                }
                            }
                            else
                            {
                                MessageBox.Show("Number export greater than number produts have been, please inmput again");
                            }
                        }
                        else
                        {
                            if (priceSubExport != 0)
                            {
                                if (numSub < currentNumSub && numPack <= currentNumberPackage)
                                {
                                    Export ep = new Export();
                                    ep.ExportID              = userID;
                                    ep.ProductID             = productID;
                                    ep.ProductName           = productName;
                                    ep.DateExport            = dateExport;
                                    ep.NumberPackageExport   = numPack;
                                    ep.NumberSubExport       = numSub;
                                    ep.PricePackageExport    = pricePackageExport;
                                    ep.PriceSubPackageExport = priceSubExport;
                                    ep.PriceExport           = numPack * pricePackageExport + numSub * priceSubExport;
                                    ep.CountExport           = currentCount;

                                    if (numSub + currentCount > currentNumSub)
                                    {
                                        bool checkUpdateProdutc = ProDAO.updateProductAtCountExport((numSub + currentCount - currentNumSub), currentNumberPackage - 1 - numPack, productID);
                                        bool checkInsertExport  = exportDAO.insertExport(ep);
                                        if (checkUpdateProdutc && checkInsertExport)
                                        {
                                            MessageBox.Show("Add to export list successful");
                                            listExport.Add(ep);
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                        else
                                        {
                                            MessageBox.Show("Add to export list fail");
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                    }
                                    else if (numSub + currentCount == currentNumSub)
                                    {
                                        bool checkUpdateProdutc = ProDAO.updateProductAtCountExport(0, currentNumberPackage - 1 - numPack, productID);
                                        bool checkInsertExport  = exportDAO.insertExport(ep);
                                        if (checkUpdateProdutc && checkInsertExport)
                                        {
                                            MessageBox.Show("Add to export list successful");
                                            listExport.Add(ep);
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                        else
                                        {
                                            MessageBox.Show("Add to export list fail");
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                    }
                                    else
                                    {
                                        bool checkUpdateProdutc = ProDAO.updateProductAtCountExport((numSub + currentCount), currentNumberPackage - numPack, productID);
                                        bool checkInsertExport  = exportDAO.insertExport(ep);
                                        if (checkUpdateProdutc && checkInsertExport)
                                        {
                                            MessageBox.Show("Add to export list successful");
                                            listExport.Add(ep);
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                        else
                                        {
                                            MessageBox.Show("Add to export list fail");
                                            index                 = -1;
                                            txtProductID.Text     = "";
                                            txtProductName.Text   = "";
                                            txtDesCription.Text   = "";
                                            txtNumberPackage.Text = "";
                                            txtNumberSub.Text     = "";
                                            lbPackage.Text        = "";
                                            lbSub.Text            = "";
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Please input number sub package less than number sub of one package or yout can export a package");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Price to export had not been input, please input at manage product function");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Price to export had not been input, please input at manage product function");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void BtnRemove_Click(object sender, EventArgs e)
        {
            if ((dgvSubmitExport.RowCount - 1) == 0)
            {
                MessageBox.Show("List export is empty");
            }
            else
            {
                flag = dgvSubmitExport.CurrentCell.RowIndex;
                if (flag != -1)
                {
                    string productID      = listExport.ElementAt(flag).ProductID;
                    string dateExport     = listExport.ElementAt(flag).DateExport;
                    int    oldNumPack     = listExport.ElementAt(flag).NumberPackageExport;
                    int    oldNumSub      = listExport.ElementAt(flag).NumberSubExport;
                    int    oldCount       = listExport.ElementAt(flag).CountExport;
                    int    currentPackage = ProDAO.getNumberPackageByID(productID);
                    int    currentSub     = ProDAO.getNumSub(productID);

                    listExport.RemoveAt(flag);
                    if (oldNumSub != 0)
                    {
                        if (oldCount + oldNumSub >= currentSub)
                        {
                            bool checkUpdatePro = ProDAO.updateProductAtCountExport(oldCount, currentPackage + 1 + oldNumPack, productID);
                            bool checkDelete    = exportDAO.removeExportByDateTime(dateExport);
                            if (checkUpdatePro)
                            {
                                MessageBox.Show("Remove successful", "Notification");
                                loadData(listExport);
                                flag = -1;
                            }
                            else
                            {
                                MessageBox.Show("Remove fail", "Warning");
                                flag = -1;
                            }
                        }
                        else
                        {
                            bool checkUpdatePro = ProDAO.updateProductAtCountExport(oldCount, currentPackage + oldNumPack, productID);
                            bool checkDelete    = exportDAO.removeExportByDateTime(dateExport);
                            if (checkUpdatePro)
                            {
                                MessageBox.Show("Remove successful", "Notification");
                                loadData(listExport);
                                flag = -1;
                            }
                            else
                            {
                                MessageBox.Show("Remove fail", "Warning");
                                flag = -1;
                            }
                        }
                    }
                    else
                    {
                        bool checkUpdatePro = ProDAO.updateProductAtCountExport(oldCount, currentPackage + oldNumPack, productID);
                        bool checkDelete    = exportDAO.removeExportByDateTime(dateExport);
                        if (checkUpdatePro)
                        {
                            MessageBox.Show("Remove successful", "Notification");
                            loadData(listExport);
                            flag = -1;
                        }
                        else
                        {
                            MessageBox.Show("Remove fail", "Warning");
                            flag = -1;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please choose product to Remove", "Warning");
                }
            }
        }