Beispiel #1
0
        public void AddSelfInvoice_Executed(object sender)
        {
            if (AppProperties.FormHaveModifications)
            {
                if (SelectedMaterial == null || !SelectedMaterial.Price.HasValue)
                {
                    MessageBox.Show("Selezionare un materiale");
                    return;
                }
                if (!Quantity.HasValue)
                {
                    MessageBox.Show("Selezionare una quantità");
                    return;
                }
                if (SelectedMeasure == null)
                {
                    MessageBox.Show("Selezionare una misura");
                    return;
                }
                if (!MaterialPrice.HasValue)
                {
                    MessageBox.Show("Selezionare un costo");
                    return;
                }

                //var invoiceNumber = 1;
                var invoiceGroup    = Guid.NewGuid();
                var settings        = _contoData.GetSettings();
                var maxInvoiceValue = settings != null ? settings.MaxInvoiceValue : 990;
                maxInvoiceValue = maxInvoiceValue.HasValue ? maxInvoiceValue.Value : 990;
                var measure = _contoData.MeasureGet(SelectedMaterial.MeasureId);

                decimal invoiceCost = (Quantity.Value * SelectedMaterial.Price.Value) / measure.Grams * SelectedMeasure.Grams;
                //decimal quantityAtMaxInvoiceValue = (measure.Grams / SelectedMeasure.Grams);
                decimal quantityAtMaxInvoiceValue = maxInvoiceValue.Value * (Quantity.Value * SelectedMeasure.Grams) /
                                                    invoiceCost /
                                                    SelectedMeasure.Grams;

                var invoices = (int)Math.Truncate(invoiceCost / maxInvoiceValue.Value) + (invoiceCost % maxInvoiceValue.Value == 0 ? 0 : 1);

                var quantityTot = Quantity.Value;

                for (int i = 0; i < invoices; i++)
                {
                    var invoiceCurrentCost = invoiceCost > maxInvoiceValue ? maxInvoiceValue.Value : invoiceCost;
                    invoiceCost = invoiceCost - maxInvoiceValue.Value;
                    var quantity = quantityTot > quantityAtMaxInvoiceValue ? quantityAtMaxInvoiceValue : quantityTot;
                    quantityTot = quantityTot - quantityAtMaxInvoiceValue;
                    _contoData.SelfInvoicesAdd(new SelfInvoices
                    {
                        MaterialId = SelectedMaterial.Id,
                        Quantity   = quantity,
                        VatExcept  = VatExempt,
                        //InvoiceNumber = invoiceNumber++,
                        InvoiceYear    = InvoiceYear,
                        MeasureId      = SelectedMeasure.Id,
                        InCashFlow     = false,
                        InvoiceDate    = InvoiceDate,
                        InvoiceCost    = invoiceCurrentCost,
                        InvoiceGroupId = invoiceGroup
                    });
                }

                SelectedMaterial = null;
                MaterialPrice    = null;
                SelectedMeasure  = null;
                Quantity         = null;

                UpdateList();

                AppProperties.FormHaveModifications = false;
            }
            else
            {
                MessageBox.Show("AddSelfInvoice");
            }
        }