Пример #1
0
    protected void btnSaveMedicine_Click(object sender, EventArgs e)
    {
        Page.Validate();
        //if (Page.IsValid)
        //{
        if (customIsValid)
        {
            int?medicineid = 0;
            // int? newMedicineID=0;
            // bool flag = false;
            //  Button lnk = (Button)sender;
            // var gvrow = (GridViewRow)lnk.NamingContainer;
            foreach (GridViewRow gvrow in gvAddBatchNumber.Rows)
            {
                lblOrderIDToMC = ((Label)gvrow.Cells[0].FindControl("lblOrderIDToMC"));
                lblmediname    = ((Label)gvrow.Cells[1].FindControl("lblMedicineName"));
                lblAmount      = ((Label)gvrow.Cells[2].FindControl("lblMedicineQuantity"));
                TextBox txtBatch           = ((TextBox)gvrow.Cells[4].FindControl("txtBatchNumber"));
                TextBox txtManufactureDate = ((TextBox)gvrow.Cells[5].FindControl("AddtxtManufactureDate"));
                TextBox txtExpiryDate      = ((TextBox)gvrow.Cells[6].FindControl("AddtxtExpiryDate"));
                TextBox txtPackSize        = ((TextBox)gvrow.Cells[7].FindControl("txtPackSize"));
                txtPrice = ((TextBox)gvrow.Cells[3].FindControl("txtPrice"));
                // dc.GetMaxMedicineID(lblmediname.Text,ref medicineid);
                var getCompany = (from a in dc.Medicine_In_Stocks
                                  where a.MedicineName == lblmediname.Text
                                  select a).First();
                //var getmediInfo = from a in dc.Medicine_In_Stocks
                //                  where a.MedicineName == lblmediname.Text
                //                  select a;
                //foreach (Medicine_In_Stock item in  getmediInfo)
                //{
                //    item.Price =Convert.ToDecimal( txtPrice.Text);
                //}
                //try
                //{
                //    dc.SubmitChanges();
                //}
                //catch (Exception ex)
                //{

                //    lblch.Text = ex.ToString();
                //}
                if (gvrow.RowType == DataControlRowType.DataRow)
                {
                    Medicine_In_Stock ms = new Medicine_In_Stock
                    {
                        MedicineName = lblmediname.Text,

                        Amount             = Convert.ToInt32(lblAmount.Text),
                        BatchNo            = txtBatch.Text,
                        ManufactureDate    = Convert.ToDateTime(txtManufactureDate.Text),
                        ExpiryDate         = Convert.ToDateTime(txtExpiryDate.Text),
                        PackSize           = txtPackSize.Text,
                        Price              = Convert.ToDecimal(txtPrice.Text),
                        MedicineCategoryID = getCompany.MedicineCategoryID,
                        MedicineCompanyID  = getCompany.MedicineCompanyID,
                        Image              = getCompany.Image,
                        Description        = getCompany.Description
                    };
                    dc.Medicine_In_Stocks.InsertOnSubmit(ms);
                    dc.SubmitChanges();
                    dc.GetMaxMedicineID(lblmediname.Text, ref medicineid);
                    // Label lb = (gvrow.Cells[0].FindControl("lblOrderIDtoMC") as Label);
                    //var s = (from a in dc.Order_To_Medicine_Company_Per_Medicines
                    //        join medi in dc.Medicine_In_Stocks on a.MedicineID equals medi.MedicineID
                    //        where a.OrderIDToMC == Convert.ToInt32(lb.Text) && medi.MedicineName==Convert.ToString ((Label)gvrow.Cells[1].FindControl("lblMedicineName"))
                    //        select medi.BatchNo).Single();
                    // if (s == Convert.ToString((Label)gvrow.Cells[2].FindControl("txtBatchNumber")))
                    // {
                    //var sub = dc.Medicine_In_Stocks
                    //          .Where(m => m.MedicineName == Convert.ToString((Label)gvrow.Cells[1].FindControl("lblMedicineName")) && m.BatchNo == Convert.ToString((Label)gvrow.Cells[2].FindControl("txtBatchNumber")))
                    //          .FirstOrDefault();
                    //int q = Convert.ToInt32(sub.Amount);
                    // int q = Convert.ToInt32((Label)gvrow.Cells[2].FindControl("lblMedicineQuantity"));
                    //// int.TryParse(quantity, out q);
                    //var mediInStock = from m in dc.Medicine_In_Stocks
                    //           where m.MedicineName == Convert.ToString((Label)gvrow.Cells[1].FindControl("lblMedicineName")) && m.BatchNo == Convert.ToString((Label)gvrow.Cells[2].FindControl("txtBatchNumber"))
                    //           select m;
                    //foreach (Medicine_In_Stock mis in mediInStock)
                    //{
                    //    mis.Amount = q;


                    //};
                    //try
                    //{
                    //    dc.SubmitChanges();
                    //}
                    //catch (Exception ex)
                    //{
                    //    lblerr.Text = ex.ToString();
                    //}
                    //if (flag == false)
                    //{

                    //}
                    //}
                }
            }

            var o = from or in dc.Order_To_Medicine_Companies
                    where or.OrderIDToMC == Convert.ToInt32(lblOrderIDToMC.Text)
                    select or;
            foreach (Order_To_Medicine_Company om in o)
            {
                om.Status = "Delivered";
            }
            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                lblch.Text = ex.ToString();
            }
            decimal id        = 0;
            var     ordermedi = from or in dc.Order_To_Medicine_Company_Per_Medicines
                                //join ord in dc.Order_To_Medicine_Company_Per_Medicines on or.OrderIDToMC equals ord.OrderIDToMC
                                //join mediname in dc.Medicine_In_Stocks on ord.MedicineID equals mediname.MedicineID
                                where or.OrderIDToMC == Convert.ToInt32(lblOrderIDToMC.Text)
                                select or;
            foreach (Order_To_Medicine_Company_Per_Medicine item in ordermedi)
            {
                var a = (from s in dc.Medicine_In_Stocks
                         where s.MedicineID == item.MedicineID     //&& s.MedicineName == lblmediname.Text
                         select s.MedicineID).First();
                if (a > 0)
                {
                    id = a;
                    break;
                }
            }
            var orderidmedi = from or in dc.Order_To_Medicine_Company_Per_Medicines
                              // join ord in dc.Order_To_Medicine_Company_Per_Medicines on or.OrderIDToMC equals ord.OrderIDToMC
                              //join mediname in dc.Medicine_In_Stocks on ord.MedicineID equals mediname.MedicineID
                              where or.OrderIDToMC == Convert.ToInt32(lblOrderIDToMC.Text) && or.MedicineID == id
                              select or;
            foreach (Order_To_Medicine_Company_Per_Medicine item in orderidmedi)
            {
                item.MedicineID = medicineid;
                item.NetAmount  = (Convert.ToInt32(lblAmount.Text) * Convert.ToDecimal(txtPrice.Text));
            }
            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                lblerr.Text = ex.ToString();
            }
            Purchase sp = new Purchase
            {
                Date        = DateTime.Now,
                OrderIDToMC = Convert.ToInt32(lblOrderIDToMC.Text)
            };
            // flag = true;
            dc.Purchases.InsertOnSubmit(sp);
            dc.SubmitChanges();
            var sname = (from a in dc.Order_To_Medicine_Companies
                         join medi in dc.Medicine_Companies on a.MedicineCompanyID equals medi.MedicineCompanyID
                         where a.OrderIDToMC == Convert.ToInt32(lblOrderIDToMC.Text)
                         select medi.MedicineCompanyName).First();
            Transaction tc = new Transaction
            {
                Order_          = Convert.ToInt32(lblOrderIDToMC.Text),
                Date            = DateTime.Now.Date,
                Time            = Convert.ToDateTime(DateTime.Now),
                Type            = "Purchase from Company",
                Client_Supplier = sname
            };
            dc.Transactions.InsertOnSubmit(tc);
            dc.SubmitChanges();
            BindGridDataOrders();
//}
        }
    }