Example #1
0
        private void txtSaleID_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                PharmaLinq                = new Linq.PharmaLinqDataContext(flag.Con);
                btnSaveSale.IsEnabled     = true;
                btnDeletProduct.IsEnabled = true;
                btnPrint.IsEnabled        = true;

                if (txtSaleID.Text != (PharmaLinq.Sales.Where(item => item.Exist == true).Max(item => item.ID_Sale) + 1).ToString())
                {
                    Linq.Sale sale = PharmaLinq.Sales.Single(item => item.Exist == true && item.ID_Sale == Convert.ToInt32(txtSaleID.Text));
                    txtBarcode.IsEnabled = true;
                    txtBarcode.Clear();
                    txtSaleDate.SelectedDate = sale.SaleDate;
                    txtUser.Text             = PharmaLinq.Users.Single(item => item.Exist == true && item.ID_User == IDUser).User_Name;
                    salePrice         = Math.Round(Convert.ToDouble(sale.SalePrice), 2);
                    txtSalePrice.Text = salePrice.ToString();

                    Dt.Clear();
                    QuantityDt.Clear();
                    DelDt.Clear();
                    Dt = flag.Fill_DataGrid_join(" SELECT [SalesAction].ID_Product ,Products.Name ,SalesAction.Price ,[Unit] ,[Quantity_SaleAction],Products.EX_Date ,[SalePrecentage] ,[TotalPrice],ID_SaleAction FROM [dbo].[SalesAction] inner join Products on Products.ID_Product=SalesAction.ID_Product  where SalesAction.Exist ='true' And  ID_Sale = '" + txtSaleID.Text + "'");

                    for (int i = 0; i < Dt.Rows.Count; i++)
                    {
                        QuantityDt.Rows.Add(Dt.Rows[i].ItemArray[4], Dt.Rows[i].ItemArray[3]);
                    }

                    dgSale.DataContext = Dt;
                    dtcount            = Dt.Rows.Count;
                    txtPaid.Text       = sale.paid.ToString();
                    cmbCustomer.Text   = PharmaLinq.Customers.SingleOrDefault(item => item.ID_Customer == sale.ID_Customer).CustomerName;

                    for (int i = 0; i < cmbPayType.Items.Count; i++)
                    {
                        cmbPayType.SelectedIndex = i;

                        if (cmbPayType.SelectedValue.ToString() == sale.PayType)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                flag.Con.Close();
                MessageBox.Show(ex.Message);
            }
        }
Example #2
0
        public void Save()
        {
            if (dgSale.Items.Count > 0)
            {
                PharmaLinq = new Linq.PharmaLinqDataContext(flag.Con);
                int j = 0;

                Linq.Sale sale = new Linq.Sale();

                if (!isnew)
                {
                    if (DelDt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dgSale.Items.Count; i++)
                        {
                            try
                            {
                                for (j = 0; j < DelDt.Rows.Count; j++)
                                {
                                    if (Convert.ToInt32(Dt.Rows[i].ItemArray[8]) == Convert.ToInt32(DelDt.Rows[j].ItemArray[0]))
                                    {
                                        salePrice   -= Convert.ToDouble(Dt.Rows[i].ItemArray[7]);
                                        txtPaid.Text = (Convert.ToDouble(txtPaid.Text) - Convert.ToDouble(Dt.Rows[i].ItemArray[7])).ToString();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    sale = PharmaLinq.Sales.Single(item => item.Exist == true && item.ID_Sale == Convert.ToInt32(txtSaleID.Text));
                }


                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    Linq.SalesAction saleaction = new Linq.SalesAction();
                    Linq.Product     product    = PharmaLinq.Products.Single(item => item.ID_Product == Convert.ToInt32(Dt.Rows[i].ItemArray[0]));

                    if (!isnew && (i + 1) <= dtcount)
                    {
                        saleaction = PharmaLinq.SalesActions.Single(item => item.Exist == true && item.ID_Sale == Convert.ToInt32(txtSaleID.Text) && item.ID_SaleAction == Convert.ToInt32(Dt.Rows[i].ItemArray[8]));
                        if (QuantityDt.Rows[i].ItemArray[1].ToString() == "قطعة")
                        {
                            if (product.BiggerUnit == "الثانوية")
                            {
                                product.Quantity     += (Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]) % (int)product.SubQuantity);
                                product.UnitQuantity += (Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]) / product.SubQuantity);

                                if (product.Quantity >= product.SubQuantity)
                                {
                                    product.Quantity -= (int)product.SubQuantity;
                                    product.UnitQuantity++;
                                }
                            }
                            else
                            {
                                product.Quantity += Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]);
                            }
                        }
                        else
                        {
                            if (product.BiggerUnit == "الثانوية")
                            {
                                product.UnitQuantity += Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]);
                            }
                            else
                            {
                                product.Quantity     += (Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]) / (int)product.SubQuantity);
                                product.UnitQuantity += (Convert.ToInt32(QuantityDt.Rows[i].ItemArray[0]) % product.SubQuantity);

                                if (product.UnitQuantity >= product.SubQuantity)
                                {
                                    product.UnitQuantity -= (int)product.SubQuantity;
                                    product.Quantity++;
                                }
                            }
                        }
                        for (j = 0; j < DelDt.Rows.Count; j++)
                        {
                            if (saleaction.ID_SaleAction == Convert.ToInt32(DelDt.Rows[j].ItemArray[0]))
                            {
                                saleaction.Exist = false;
                                goto skip;
                            }
                        }
                    }



                    saleaction.ID_Sale             = Convert.ToInt32(txtSaleID.Text);
                    saleaction.ID_Product          = Convert.ToInt32(Dt.Rows[i].ItemArray[0]);
                    saleaction.Quantity_SaleAction = Convert.ToInt32(Dt.Rows[i].ItemArray[4]);
                    saleaction.Price          = Convert.ToDecimal(Dt.Rows[i].ItemArray[2]);
                    saleaction.TotalPrice     = Convert.ToDecimal(Dt.Rows[i].ItemArray[7]);
                    saleaction.SalePrecentage = Convert.ToInt32(Dt.Rows[i].ItemArray[6]);
                    saleaction.Unit           = Convert.ToString(Dt.Rows[i].ItemArray[3]);
                    saleaction.Exist          = true;

                    if (isnew || i + 1 > dtcount)
                    {
                        PharmaLinq.SalesActions.InsertOnSubmit(saleaction);
                    }
                    //Quantity Decrement
                    if (Dt.Rows[i].ItemArray[3].ToString() == "قطعة")
                    {
                        if (product.BiggerUnit == "الثانوية")
                        {
                            product.Quantity     -= (Convert.ToInt32(Dt.Rows[i].ItemArray[4]) % (int)product.SubQuantity);
                            product.UnitQuantity -= (Convert.ToInt32(Dt.Rows[i].ItemArray[4]) / product.SubQuantity);

                            if (product.Quantity < 0)
                            {
                                product.Quantity += (int)product.SubQuantity;
                                product.UnitQuantity--;
                            }
                        }
                        else
                        {
                            product.Quantity -= Convert.ToInt32(Dt.Rows[i].ItemArray[4]);
                        }
                    }
                    else
                    {
                        if (product.BiggerUnit == "الثانوية")
                        {
                            product.UnitQuantity -= Convert.ToInt32(Dt.Rows[i].ItemArray[4]);
                        }
                        else
                        {
                            product.Quantity     -= (Convert.ToInt32(Dt.Rows[i].ItemArray[4]) / (int)product.SubQuantity);
                            product.UnitQuantity -= (Convert.ToInt32(Dt.Rows[i].ItemArray[4]) % product.SubQuantity);

                            if (product.UnitQuantity < 0)
                            {
                                product.UnitQuantity += (int)product.SubQuantity;
                                product.Quantity--;
                            }
                        }
                    }
                    //End Quantity Decrement

                    skip :;
                    PharmaLinq.SubmitChanges();
                }



                sale.SalePrice   = Convert.ToDecimal(salePrice);
                sale.Exist       = true;
                sale.PayType     = cmbPayType.Text;
                sale.paid        = Convert.ToDecimal(txtPaid.Text);
                sale.ID_Customer = Convert.ToInt32(DtCustomer.Rows[cmbCustomer.SelectedIndex].ItemArray[1]);

                if (isnew)
                {
                    sale.ID_Sale  = Convert.ToInt32(txtSaleID.Text);
                    sale.SaleDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy HH:mm"));
                    sale.ID_User  = IDUser;

                    PharmaLinq.Sales.InsertOnSubmit(sale);
                }
                PharmaLinq.SubmitChanges();

                MessageBox.Show("تم حفظ الفاتورة");



                USC_SALES usc_sale = new USC_SALES {
                    IDUser = IDUser
                };
                MainWindow.GetMainForm.GridUsc.Children.Clear();
                MainWindow.GetMainForm.GridUsc.Children.Add(usc_sale);
            }
            else
            {
                MessageBox.Show("يجب اضافة منتجات");
            }
        }