Ejemplo n.º 1
0
        private void FrmStock_Load(object sender, EventArgs e)
        {
            try {
                if (this.Entity != null && this.Entity.donation != null && this.Entity.donation.IsStored())
                {
                    MessageBox.Show("El ítem que está intentando editar pertenece a una donación ya almacenada.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    groupBox1.Enabled = false;
                    cmdAccept.Enabled = false;
                }

                //Traducciones
                SessionHelper.RegisterForTranslation(this, Codes.MNU_GE013);
                SessionHelper.RegisterForTranslation(cmdAccept, Codes.BTN_ACCEPT);
                SessionHelper.RegisterForTranslation(cmdClose, Codes.BTN_CLOSE);

                SessionHelper.RegisterForTranslation(lblLot, Codes.LBL_LOT);
                SessionHelper.RegisterForTranslation(lblDepot, Codes.LBL_DEPOT);
                SessionHelper.RegisterForTranslation(lblDescription, Codes.LBL_DESCRIPTION);
                SessionHelper.RegisterForTranslation(lblType, Codes.LBL_ITEM_TYPE);
                SessionHelper.RegisterForTranslation(lblItemQuantity, Codes.LBL_QUANTITY);
                SessionHelper.RegisterForTranslation(lblDuedate, Codes.LBL_DUEDATE);
                SessionHelper.RegisterForTranslation(lblLocation, Codes.LBL_LOCATION);

                LoadDonations();
                LoadDepots();
                LoadItemTypes();

                if (this.IsUpdate)
                {
                    cmbDonation.Enabled = false;
                    StockBLL stockBll    = new StockBLL();
                    ResultBM stockResult = stockBll.GetStock(this.Entity.id);

                    if (!stockResult.IsValid())
                    {
                        MessageBox.Show(stockResult.description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    this.Entity = stockResult.GetValue <StockBM>();

                    this.availableStock     = this.Entity.GetAmountItemsToStockWithoutThis();
                    numericQuantity.Maximum = this.availableStock;
                    numericQuantity.Value   = this.Entity.Quantity;

                    txtName.Text     = this.Entity.Name;
                    dtDueDate.Value  = this.Entity.DueDate;
                    txtLocation.Text = this.Entity.Location;


                    //Posicionar donador
                    bool found = false;

                    for (int i = 0; i < cmbItemType.Items.Count && !found; ++i)
                    {
                        found = ((ItemTypeBM)cmbItemType.Items[i]).id == this.Entity.itemType.id;
                        if (found)
                        {
                            cmbItemType.SelectedIndex = i;
                        }
                    }

                    found = false;

                    for (int i = 0; i < cmbDonation.Items.Count && !found; ++i)
                    {
                        found = ((DonationBM)cmbDonation.Items[i]).id == this.Entity.donation.id;
                        if (found)
                        {
                            cmbDonation.SelectedIndex = i;
                        }
                    }

                    found = false;

                    for (int i = 0; i < cmbDepot.Items.Count && !found; ++i)
                    {
                        found = ((DepotBM)cmbDepot.Items[i]).id == this.Entity.depot.id;
                        if (found)
                        {
                            cmbDepot.SelectedIndex = i;
                        }
                    }
                }
                else
                {
                    this.Entity = new StockBM();
                    //Cuando es nuevo, se toma el valor del elemento seleccionado del combo de donación como valores inicializadores.
                    //En dicho caso, el máximo disponible es el máximo asignable y el número que se sugiere stockear
                    DonationBM donationBm = (DonationBM)cmbDonation.SelectedItem;
                    if (donationBm != null)
                    {
                        this.availableStock     = donationBm.Items - donationBm.stocked;
                        numericQuantity.Maximum = this.availableStock;
                        numericQuantity.Value   = this.availableStock;
                    }
                }
                CalculateMaxStockLeft((DonationBM)cmbDonation.SelectedItem, (int)numericQuantity.Value);
            }
            catch (Exception exception) {
                MessageBox.Show("Se ha producido el siguiente error: " + exception.Message, "EXCEPCIÓN", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }