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.Persistent.BaseImpl;
using Nuvolar.Module.Helpers;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class StorageOut_VC : ViewController
    {
        public StorageOut_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // 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();
        }

        /// <summary>
        /// Kitárolási bizonylatról => szállítólevél (egy az egyben !)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aCreateDeliveryNoteOutfromStorageOut_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
        {
            //Ellenőrzés, hogy készült-e már minden kitárolási sorról szállítólevél
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            StorageOutHeader _StorageOutHeader = _ocur.GetObject(View.CurrentObject) as StorageOutHeader;
            bool _Ok = true;
            foreach (StorageOutRows _StorageOutRows in _StorageOutHeader.StorageOutRows)
            {
                if (_StorageOutRows.QTT_To_DeliveryOut == _StorageOutRows.QTT)
                {
                    _Ok = false;
                    break;
                }
            }
            if (_StorageOutHeader.DeliveryNoteOutHeader != null)
            {
                _Ok = false;
            }
            if (_Ok)
            {
                DeliveryNoteOutHeader _DeliveryNoteOutHeader = BusinessDocumentHelper.CreateNewDeliveryNoteOutHeader(_ocur, e.PopupWindow.View.CurrentObject as DeliveryNoteView);
                long i = 1;
                foreach (StorageOutRows _StorageOutRows in _StorageOutHeader.StorageOutRows)
                {
                    foreach (StorageOutRowsInRows _StorageOutRowsInRows in _StorageOutRows.StorageOutRowsInRows)
                    {
                        _StorageOutRowsInRows.QTT_Will = _StorageOutRowsInRows.QTT;
                        BusinessDocumentHelper.AddNewDeliveryNoteOutRows(_ocur, _DeliveryNoteOutHeader, _StorageOutRowsInRows, i);
                        i += 1;
                    }

                }
                if (BusinessDocumentHelper.FinalizeDeliveryNoteOutHeader(_ocur, _DeliveryNoteOutHeader, _StorageOutHeader))
                {
                    //Nyomtatás
                    BusinessDocumentHelper.PrintDeliveryNoteOutHeader(_ocur, Frame, _DeliveryNoteOutHeader);
                }
            }

        }

        private void aCreateDeliveryNoteOutfromStorageOut_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e)
        {
            XPObjectSpace _onew = Application.CreateObjectSpace() as XPObjectSpace;
            DeliveryNoteView _DeliveryNoteView = _onew.CreateObject<DeliveryNoteView>();
            StorageOutHeader _StorageOutHeader = _onew.GetObject(View.CurrentObject) as StorageOutHeader;

            _DeliveryNoteView.Customers = _StorageOutHeader.Customers;
            _DeliveryNoteView.TransportAddress = _StorageOutHeader.Storage.Address;
            _DeliveryNoteView.DateExecution = DateTime.Now;
            _DeliveryNoteView.CurrencyName = _onew.FindObject<CurrencyName>(CriteriaOperator.Parse("ShortName='HUF'"));
            if (_StorageOutHeader.StorageMoveType.MoveType==eMoveType.InventoryTransfer)
            {
                _DeliveryNoteView.DeliveryNoteOutType = _StorageOutHeader.Storage.DeliveryNoteOutType_InventoryTransfer;
            }
            if (_StorageOutHeader.StorageMoveType.MoveType == eMoveType.ReturnBack)
            {
                _DeliveryNoteView.DeliveryNoteOutType = _StorageOutHeader.Storage.DeliveryNoteOutType_ReturnBack;
            }


            if (_StorageOutHeader.Customers != null)
            {
                if (_StorageOutHeader.Customers.Address1 != null) _DeliveryNoteView.TransportAddress = _onew.GetObjectByKey<Address>(_StorageOutHeader.Customers.Address1.Oid);
                if (_StorageOutHeader.Customers.Address2 != null) _DeliveryNoteView.TransportAddress = _onew.GetObjectByKey<Address>(_StorageOutHeader.Customers.Address2.Oid);
                if (_StorageOutHeader.Customers.ShipmentOption != null) _DeliveryNoteView.ShipmentOption = _onew.GetObjectByKey<ShipmentOption>(_StorageOutHeader.Customers.ShipmentOption.Oid);
                if (_StorageOutHeader.Customers.QualityOption != null) _DeliveryNoteView.QualityOption = _onew.GetObjectByKey<QualityOption>(_StorageOutHeader.Customers.QualityOption.Oid);
            }

            e.View = Application.CreateDetailView(_onew, "DeliveryNoteView_DetailView", false, _DeliveryNoteView);
        }

        /// <summary>
        /// Kitárolási bizonylathoz kapcsolódó szállítólevél újranyomtatása
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aPrint_DeliveryNoteOutHeader_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            StorageOutHeader _StorageOutHeader = View.CurrentObject as StorageOutHeader;
            BusinessDocumentHelper.PrintDeliveryNoteOutHeader(_ocur, Frame, _StorageOutHeader.DeliveryNoteOutHeader);

        }

        private void aStornoDeliveryNoteOutfromStorageOut_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            StorageOutHeader _StorageOutHeader = View.CurrentObject as StorageOutHeader;
            DeliveryNoteOutHeader _DeliveryNoteOutHeader = BusinessDocumentHelper.StornoDeliveryNoteOutHeaderfromStorageOutHeader(_ocur, _StorageOutHeader);
            if (_DeliveryNoteOutHeader!=null)
            {
                BusinessDocumentHelper.PrintDeliveryNoteOutHeader(_ocur, Frame, _DeliveryNoteOutHeader);
            }
            
        }
    }
}
