Ejemplo n.º 1
0
        public bool insertpurchase(purchaseBLL p, out int purchaseID)
        {
            bool isSuccess = false;

            purchaseID = -1;
            SqlConnection con = new SqlConnection(myconnstrng);

            try
            {
                String sql = "INSERT INTO Purchase_Transactions (Transaction_Type,Sup_ID,Sub_Total,TDiscount,TSGST,TCGST,TIGST,Grand_Total) VALUES(@Transaction_Type,@sup_id,@subTotal,@totalDiscount,@totalSgst,@totalCgst,@totalIgst,@grandTotal);select @@IDENTITY;";

                SqlCommand cmd = new SqlCommand(sql, con);
                //add parameteres  values
                cmd.Parameters.AddWithValue("@Transaction_Type", p.Transaction_Type);
                cmd.Parameters.AddWithValue("@sup_id", p.Sup_ID);
                cmd.Parameters.AddWithValue("@subTotal", p.Sub_Total);
                cmd.Parameters.AddWithValue("@totalDiscount", p.TDiscount);
                cmd.Parameters.AddWithValue("@totalSgst", p.TSGST);
                cmd.Parameters.AddWithValue("@totalCgst", p.TCGST);
                cmd.Parameters.AddWithValue("@totalIgst", p.TIGST);
                cmd.Parameters.AddWithValue("@grandTotal", p.Grand_Total);

                con.Open();

                object o = cmd.ExecuteScalar();

                if (o != null)
                {
                    isSuccess  = true;
                    purchaseID = int.Parse(o.ToString());
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
            return(isSuccess);
        }
        public void save()
        {
            purchaseBLL purchaseBLL = new purchaseBLL();

            string sname = textSupplierName.Text;

            if (comboPurchaseType.Text != "")
            {
                if (sname != "")
                {
                    if (dgvAddedProducts.Rows.Count != 0)
                    {
                        SupplierMasterBLL s = smDAL.getSuplierIdFromName(sname);

                        decimal subTotal, totalDiscount, totalSgst, totalCgst, totalIgst, grandTotal;

                        string type = comboPurchaseType.Text;
                        decimal.TryParse(textSubTotal.Text, out subTotal);
                        decimal.TryParse(textSubDiscount.Text, out totalDiscount);
                        decimal.TryParse(textSgst.Text, out totalSgst);
                        decimal.TryParse(textCgst.Text, out totalCgst);
                        decimal.TryParse(textIgst.Text, out totalIgst);
                        decimal.TryParse(textGrandTotal.Text, out grandTotal);

                        purchaseBLL.Transaction_Type = type;
                        purchaseBLL.Sup_ID           = s.SupplierID;
                        purchaseBLL.Sub_Total        = subTotal;
                        purchaseBLL.TDiscount        = totalDiscount;
                        purchaseBLL.TSGST            = totalSgst;
                        purchaseBLL.TCGST            = totalCgst;
                        purchaseBLL.TIGST            = totalIgst;
                        purchaseBLL.Grand_Total      = grandTotal;

                        purchaseBLL.PurchaseDetails = purchasedt;
                        bool isSuccess = false;

                        // using (TransactionScope scope = new TransactionScope())

                        //  int purchaseid = -1; already declaraed at the top as a global variable
                        bool b = purchaseDAL.insertpurchase(purchaseBLL, out purchaseid);
                        for (int i = 0; i < purchasedt.Rows.Count; i++)
                        {
                            purchasedetailsBLL pdBLL = new purchasedetailsBLL();

                            stockBLL stockBLL = new stockBLL();

                            string           productName = purchasedt.Rows[i][1].ToString();
                            ProductMasterBLL p           = ProductMasterDAL.GetProductIDFromName(productName);


                            pdBLL.Purchase_ID  = purchaseid;
                            pdBLL.Product_ID   = p.Product_ID;
                            pdBLL.Sup_ID       = s.SupplierID;
                            pdBLL.Product_Name = purchasedt.Rows[i][1].ToString();
                            pdBLL.Unit         = purchasedt.Rows[i][2].ToString();
                            pdBLL.Qty          = Math.Round(decimal.Parse(purchasedt.Rows[i][3].ToString()), 2);
                            pdBLL.Rate         = Math.Round(decimal.Parse(purchasedt.Rows[i][4].ToString()), 2);
                            pdBLL.Discount_Per = Math.Round(decimal.Parse(purchasedt.Rows[i][6].ToString()), 2);
                            pdBLL.GST_Type     = purchasedt.Rows[i][7].ToString();
                            pdBLL.GST_Per      = Math.Round(decimal.Parse(purchasedt.Rows[i][8].ToString()), 2);
                            pdBLL.Total        = Math.Round(decimal.Parse(purchasedt.Rows[i][9].ToString()), 2);



                            int Product_id = p.Product_ID;
                            stockBLL.Product_Id = Product_id;
                            stockBLL.Quantity   = Math.Round(decimal.Parse(purchasedt.Rows[i][3].ToString()), 2);
                            stockBLL.Unit       = purchasedt.Rows[i][2].ToString();

                            bool y = pdetailsDAL.insertpurchasedetails(pdBLL);

                            if (y == true)
                            {
                                stockBLL Padded = stockDAL.CheakeProductAddedOrNot(Product_id);
                                //MessageBox.Show("Product is added",Padded.Product_Id.ToString());
                                if (Product_id == Padded.Product_Id)
                                {
                                    bool x = stockDAL.Update(stockBLL);
                                }
                                else
                                {
                                    bool z = stockDAL.InsertStockNewProduct(stockBLL);
                                }
                            }

                            isSuccess = b && y;

                            isSuccess = true;
                        }
                        if (isSuccess == true)
                        {
                            //scope.Complete();
                            MessageBox.Show("Transaction Completed");
                            //clear();
                        }
                        else
                        {
                            MessageBox.Show("Transaction Failed");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Add product Details");
                    }
                }
                else
                {
                    MessageBox.Show("Please Select Customer Details");
                }
            }
            else
            {
                MessageBox.Show("Please Select Purchase Type GST OR NOGST");
            }
        }