Ejemplo n.º 1
0
        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            ProductModel             model;
            List <OpeningStockModel> Stocks;
            decimal?nullval     = null;
            int?    integernull = null;

            if (!productNameExists)
            {
                if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Service))
                {
                    if (string.IsNullOrEmpty(product_name.Text))
                    {
                        Common.ErrorMessage((string)Application.Current.Resources["error_message_Tax"], header);
                    }
                    if (serviceCompanyLogo.Source != null)
                    {
                        SaveImageFile(serviceCompanyLogo.Source.ToString());
                    }

                    model = new ProductModel(0, product_name.Text, null,
                                             string.IsNullOrEmpty(service_Charge.Text) ? nullval : Convert.ToDecimal(service_Charge.Text),
                                             string.IsNullOrEmpty(trade_price.Text) ? nullval : Convert.ToDecimal(trade_price.Text),
                                             string.IsNullOrEmpty(wholeseller_price.Text) ? nullval : Convert.ToDecimal(wholeseller_price.Text),
                                             string.IsNullOrEmpty(reseller_price.Text) ? nullval : Convert.ToDecimal(reseller_price.Text),
                                             Convert.ToInt32(item_Type.SelectedItem), string.IsNullOrEmpty(weight_.Text) ? nullval : Convert.ToDecimal(weight_.Text),
                                             barcode_.Text, string.IsNullOrEmpty(_taxValue) ? nullval : Convert.ToDecimal(_taxValue),
                                             string.IsNullOrEmpty(minimum_level.Text) ? integernull : Convert.ToInt32(minimum_level.Text),
                                             string.IsNullOrEmpty(reorder_level.Text) ? integernull : Convert.ToInt32(reorder_level.Text),
                                             null, is_texinclusive.IsChecked.Value, shortname_.Text, serviceDescription_.Text,
                                             UserModelVm.BranchId, string.Empty, string.Empty, null,
                                             (serviceCompanyLogo.Source != null) ? System.IO.Path.GetFileName(serviceCompanyLogo.Source.ToString()) : "", UserModelVm.CompanyId, null);
                    controller.SaveUpdateProduct(model);
                    SuccessRetrun();
                }
                else
                {
                    if (string.IsNullOrEmpty(product_name.Text) || string.IsNullOrEmpty(retail_price.Text) || string.IsNullOrEmpty(trade_price.Text) || string.IsNullOrEmpty(itemType.Text) || item_Type.SelectedIndex == -1)
                    {
                        Common.ErrorMessage((string)Application.Current.Resources["error_message_Tax"], header);
                    }
                    else if (is_texinclusive.IsChecked.Value == false && string.IsNullOrEmpty(_taxValue))
                    {
                        Common.ErrorMessage((string)Application.Current.Resources["taxPercentage_ErrorMsg"], header);
                    }
                    else if (Convert.ToDecimal(retail_price.Text) < Convert.ToDecimal(trade_price.Text))
                    {
                        Common.ErrorMessage((string)Application.Current.Resources["taxPercentage_ErrorInRetailPriceMsg"], header);
                    }
                    else
                    {
                        Stocks = lvProductDetails.Items.Cast <OpeningStockModel>().Select(x => x).ToList();
                        bool anyDuplicate   = Stocks.GroupBy(x => x.Quantity).Any(g => g.Count() > 1);
                        bool IsStocks       = Stocks.Where(x => x.ProductCode > 0 && x.Quantity > 0).Any();
                        bool IsChildProduct = Stocks.Where(x => x.RetailPrice > 0 && x.Quantity > 0).Any();
                        if ((Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Kit) || Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Package)) && !IsStocks)
                        {
                            Common.ErrorMessage((string)Application.Current.Resources["error_message_addProduct"], header);
                        }
                        else if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Bulk) && string.IsNullOrEmpty(weight_.Text))
                        {
                            Common.ErrorMessage((string)Application.Current.Resources["error_message_addWeight"], header);
                        }
                        else if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.RePack) && (string.IsNullOrEmpty(weight_.Text) || string.IsNullOrEmpty(bulk_Code.Text)))
                        {
                            Common.ErrorMessage((string)Application.Current.Resources["error_message_addWeightAndBulkCode"], header);
                        }
                        else if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Standard) && anyDuplicate)
                        {
                            Common.ErrorMessage((string)Application.Current.Resources["error_message_addChildItems"], header);
                        }
                        else
                        {
                            List <ProductItemContentModel> productItemContentModel = new List <ProductItemContentModel>();
                            List <SubProductItemModel>     subProductItemModel     = new List <SubProductItemModel>();

                            if (!string.IsNullOrEmpty(category_code.Text))
                            {
                                categoryId = _categories.FirstOrDefault(x => x.CategoryName.ToLower() == category_code.Text.ToLower()).Id;
                            }
                            if (CompanyLogo.Source != null)
                            {
                                SaveImageFile(CompanyLogo.Source.ToString());
                            }
                            model = new ProductModel(0, product_name.Text, categoryId, string.IsNullOrEmpty(retail_price.Text) ? nullval : Convert.ToDecimal(retail_price.Text),
                                                     string.IsNullOrEmpty(trade_price.Text) ? nullval : Convert.ToDecimal(trade_price.Text), string.IsNullOrEmpty(wholeseller_price.Text) ? nullval : Convert.ToDecimal(wholeseller_price.Text), string.IsNullOrEmpty(reseller_price.Text) ? nullval : Convert.ToDecimal(reseller_price.Text),
                                                     Convert.ToInt32(item_Type.SelectedItem), string.IsNullOrEmpty(weight_.Text) ? nullval : Convert.ToDecimal(weight_.Text), barcode_.Text, string.IsNullOrEmpty(_taxValue) ? nullval : Convert.ToDecimal(_taxValue), string.IsNullOrEmpty(minimum_level.Text) ? integernull : Convert.ToInt32(minimum_level.Text), string.IsNullOrEmpty(reorder_level.Text) ? integernull : Convert.ToInt32(reorder_level.Text), null, is_texinclusive.IsChecked.Value, shortname_.Text, description_.Text, UserModelVm.BranchId, string.Empty, string.Empty, null,
                                                     (CompanyLogo.Source != null) ? System.IO.Path.GetFileName(CompanyLogo.Source.ToString()) : "", UserModelVm.CompanyId, string.IsNullOrEmpty(bulk_Code.Text) ? integernull : _products.FirstOrDefault(x => x.ItemName.ToLower() == bulk_Code.Text.ToLower() && x.ItemType == Convert.ToInt64(CommonEnum.ItemTypes.Bulk)).Id);

                            int parentProductId = controller.SaveUpdateProduct(model);
                            if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Kit) || Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Package))
                            {
                                productItemContentModel.AddRange(Stocks.Where(z => z.ProductCode > 0 && z.Quantity > 0).Select(x =>
                                {
                                    return(new ProductItemContentModel(0, Convert.ToInt32(x.ProductCode), x.Quantity, parentProductId, UserModelVm.UserId, null, "", "", x.isFreeProduct));
                                }).ToList());
                                controller.SaveProductItemContent(productItemContentModel);
                            }
                            if (Convert.ToString(item_Type.SelectedItem) == Convert.ToString(CommonEnum.ItemTypes.Standard) && IsChildProduct)
                            {
                                subProductItemModel.AddRange(Stocks.Select(x =>
                                {
                                    return(new SubProductItemModel(0, parentProductId, x.Quantity, x.RetailPrice.Value, UserModelVm.UserId, null, "", ""));
                                }).ToList());
                                controller.SaveSubProductItems(subProductItemModel);
                            }
                            SuccessRetrun();
                        }
                    }
                }
            }
            else
            {
                productNameExists = true;
                Common.ErrorMessage((string)Application.Current.Resources["product_exists"], header);
            }
        }