using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Xpo;
//using Nuvolar.Module.Win;

namespace Nuvolar.Module.BusinessObjects
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.

    public partial class PriceFixCustomers_VC : ViewController
    {
        //private WaitFormClass _WaitFormClass;
        public PriceFixCustomers_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            if (View.Id == "PriceFixCustomers_ListView")
            {
                //_WaitFormClass = new WaitFormClass();
                Load_PriceFixCustomers_ListView();
            }
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }

        private void aViewPriceFixCustomers_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            CollectionSource _CS = new CollectionSource(_onew, typeof(PriceFixCustomers));

            e.ShowViewParameters.CreatedView = Application.CreateListView("PriceFixCustomers_ListView", _CS, false);
            e.ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild;
            e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow;
        }
        private void Load_PriceFixCustomers_ListView()
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            ListView _ListView = View as ListView;

            XPCollection<CurrencyRate> _CurrencyRate_Collection = new XPCollection<CurrencyRate>(_ocur.Session, CriteriaOperator.Parse("IsMNB=True And GetDate(DateValid)=?", DateTime.Now.Date));




            XPCollection<Customers> _Customers_Collection = new XPCollection<Customers>(_ocur.Session, CriteriaOperator.Parse("IsNull(Pricing)=False"));


            //_WaitFormClass.Initwork(1, 1, _Customers_Collection.Count);
            foreach (Customers _Customers in _Customers_Collection)
            {
                //_WaitFormClass.DoWork();
                foreach (PricingFix _PricingFix in _Customers.Pricing.PricingFix)
                {
                    PriceFixCustomers _PriceFixCustomers = new PriceFixCustomers(_ocur.Session);
                    _PriceFixCustomers.Customers = _Customers;
                    _PriceFixCustomers.CurrencyName = _PricingFix.CurrencyName;
                    _PriceFixCustomers.Products = _PricingFix.Products;
                    if (_PricingFix.CurrencyName.ShortName == "HUF")
                    {
                        _PriceFixCustomers.PriceNettoHUF = _PricingFix.PriceNetto;
                        _PriceFixCustomers.PriceNettoDEV = 0;
                    }
                    else
                    {
                        foreach (CurrencyRate _CurrencyRate in _CurrencyRate_Collection)
                        {
                            if (_CurrencyRate.CurrencyName.ShortName == _PricingFix.CurrencyName.ShortName)
                            {
                                _PriceFixCustomers.PriceNettoHUF = _PricingFix.PriceNetto * _CurrencyRate.RateAverage;
                                _PriceFixCustomers.CurrencyRateValue = _CurrencyRate.RateAverage;
                            }
                        }
                        _PriceFixCustomers.PriceNettoDEV = _PricingFix.PriceNetto;
                    }
                    _PriceFixCustomers.CurrencyName = _PricingFix.CurrencyName;



                    XPCollection<DeliveryNoteInRRFC> _DeliveryNoteInRRFC_Collection = new XPCollection<DeliveryNoteInRRFC>(_ocur.Session, CriteriaOperator.Parse("IsNull(RegistryRowsFinancialControlling)=False And DeliveryNoteInRows.DeliveryNoteInHeader.IsEditable=False And DeliveryNoteInRows.Products=?", _PricingFix.Products));

                    _DeliveryNoteInRRFC_Collection.Sorting.Add(new SortProperty("RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution", DevExpress.Xpo.DB.SortingDirection.Descending));

                    if (_DeliveryNoteInRRFC_Collection.Count > 0)
                    {
                        _PriceFixCustomers.DateValid = _DeliveryNoteInRRFC_Collection[0].RegistryRowsFinancialControlling.RegistryHeader.F_DateExecution;
                        if (_DeliveryNoteInRRFC_Collection[0].RegistryRowsFinancialControlling.RegistryHeader.F_CurrencyName.ShortName == "HUF")
                        {
                            _PriceFixCustomers.PriceAverageNettoDEV = 0;
                            _PriceFixCustomers.PriceAverageNettoHUF = (_DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.SumPriceHUF) / _DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.QTT;
                        }
                        else
                        {
                            _PriceFixCustomers.PriceAverageNettoDEV = _DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.SumPriceDEV / _DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.QTT;
                            _PriceFixCustomers.PriceAverageNettoHUF = (_DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.SumPriceDEV * _DeliveryNoteInRRFC_Collection[0].RegistryRowsFinancialControlling.RegistryHeader.F_CurrencyRate) / _DeliveryNoteInRRFC_Collection[0].DeliveryNoteInRows.QTT;
                        }
                    }

                    if (_PriceFixCustomers.PriceNettoHUF!=0)
                    {
                        if (_PriceFixCustomers.CurrencyName.ShortName == "HUF")
                        {
                            _PriceFixCustomers.MarginPercent = (_PriceFixCustomers.PriceNettoHUF - _PriceFixCustomers.PriceAverageNettoHUF) / _PriceFixCustomers.PriceNettoHUF * 100;
                        }
                        else {
                            _PriceFixCustomers.MarginPercent = (_PriceFixCustomers.PriceNettoDEV - _PriceFixCustomers.PriceAverageNettoDEV) / _PriceFixCustomers.PriceNettoDEV * 100;
                        }
                    }
                    

                    _ListView.CollectionSource.Add(_PriceFixCustomers);
                }

            }
            //_WaitFormClass.FinalWork();

        }
    }
}
