using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nuvolar.Module.Helpers
{
    public static class BusinessDocumentHelper
    {

        #region DeliveryNoteOut
        /// <summary>
        /// Új kimenő szállítólevél fejlécet készít
        /// </summary>
        public static DeliveryNoteOutHeader CreateNewDeliveryNoteOutHeader(XPObjectSpace _ObjectSpace)
        {
            DeliveryNoteOutHeader _DeliveryNoteOutHeader = _ObjectSpace.CreateObject<DeliveryNoteOutHeader>();

            return _DeliveryNoteOutHeader;
        }
        /// <summary>
        /// Új kimenő szállíttólevél fejléc a _deliveryNoteView popup alapján
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_DeliveryNoteView"></param>
        /// <returns></returns>
        public static DeliveryNoteOutHeader CreateNewDeliveryNoteOutHeader(XPObjectSpace _ObjectSpace, DeliveryNoteView _DeliveryNoteView)
        {
            DeliveryNoteOutHeader _DeliveryNoteOutHeader = _ObjectSpace.CreateObject<DeliveryNoteOutHeader>();
            try
            {
                _DeliveryNoteOutHeader.CustomersFrom = _ObjectSpace.GetObject(_DeliveryNoteView.CustomersFrom);
                _DeliveryNoteOutHeader.ConnectInfo = "";
                _DeliveryNoteOutHeader.CurrencyName = _ObjectSpace.GetObject((_DeliveryNoteView.CurrencyName));
                _DeliveryNoteOutHeader.Customers = _ObjectSpace.GetObject((_DeliveryNoteView.Customers));
                _DeliveryNoteOutHeader.DateCreated = GeneralFunction.GetSQLTime(_ObjectSpace.Session);
                _DeliveryNoteOutHeader.DateExecution = GeneralFunction.GetSQLTime(_ObjectSpace.Session);
                _DeliveryNoteOutHeader.DeliveryNoteOutType = _ObjectSpace.GetObject((_DeliveryNoteView.DeliveryNoteOutType));
                _DeliveryNoteOutHeader.DocumentNumber = "";
                _DeliveryNoteOutHeader.IsEditable = false;
                _DeliveryNoteOutHeader.IsStorno = false;
                _DeliveryNoteOutHeader.Note = _ObjectSpace.GetObject((_DeliveryNoteView.Note));
                _DeliveryNoteOutHeader.QualityOption = _ObjectSpace.GetObject((_DeliveryNoteView.QualityOption));
                _DeliveryNoteOutHeader.ShipmentOption = _ObjectSpace.GetObject((_DeliveryNoteView.ShipmentOption));
                _DeliveryNoteOutHeader.TransportAddress = _ObjectSpace.GetObject((_DeliveryNoteView.TransportAddress));
            }
            catch (Exception)
            {
                _ObjectSpace.Delete(_DeliveryNoteOutHeader);
                _DeliveryNoteOutHeader = null;
            }

            return _DeliveryNoteOutHeader;
        }
        /// <summary>
        /// Létrehoz egy új DeliveryNoteOutRows objektumot (üresen, de a DeliveryNoteOutHeader-t beilleszti)
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_DeliveryNoteOutHeader"></param>
        /// <returns></returns>
        public static DeliveryNoteOutRows AddNewDeliveryNoteOutRows(XPObjectSpace _ObjectSpace, DeliveryNoteOutHeader _DeliveryNoteOutHeader)
        {
            DeliveryNoteOutRows _DeliveryNoteOutRows = _ObjectSpace.CreateObject<DeliveryNoteOutRows>();


            _DeliveryNoteOutRows.DeliveryNoteOutHeader = _DeliveryNoteOutHeader;

            return _DeliveryNoteOutRows;
        }
        /// <summary>
        /// Kitárolási sor FIFO lábaiból a QTT alapján egy kiszállítási sort készít
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_DeliveryNoteOutHeader"></param>
        /// <param name="_StorageOutRows"></param>
        /// <returns></returns>
        public static DeliveryNoteOutRows AddNewDeliveryNoteOutRows(XPObjectSpace _ObjectSpace, DeliveryNoteOutHeader _DeliveryNoteOutHeader, StorageOutRowsInRows _StorageOutRowsInRows, long _RowIndex)
        {
            DeliveryNoteOutRows _DeliveryNoteOutRows = _ObjectSpace.CreateObject<DeliveryNoteOutRows>();
            try
            {


                _DeliveryNoteOutRows.DeliveryNoteOutHeader = _DeliveryNoteOutHeader;
                _DeliveryNoteOutRows.DividedOriginal = 0;
                _DeliveryNoteOutRows.QTT = _StorageOutRowsInRows.QTT_Will;
                _DeliveryNoteOutRows.QTT_Invoiced = 0;
                _DeliveryNoteOutRows.QTT_Will = 0;
                _DeliveryNoteOutRows.RowIndex = _RowIndex;
                _DeliveryNoteOutRows.StorageOutRowsInRows = _ObjectSpace.GetObject(_StorageOutRowsInRows);

                _StorageOutRowsInRows.QTT_To_DeliveryOut = _DeliveryNoteOutRows.QTT; // A kitárolási sor is update !!!!
            }
            catch (Exception)
            {

                _ObjectSpace.Delete(_DeliveryNoteOutRows);
                _DeliveryNoteOutRows = null;
            }

            return _DeliveryNoteOutRows;
        }
        /// <summary>
        /// Véglegesíti a korábban elkezdett DeliveryNoteOutHeader-t
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_DeliveryNoteOutHeader"></param>
        /// <returns></returns>
        public static bool FinalizeDeliveryNoteOutHeader(XPObjectSpace _ObjectSpace, DeliveryNoteOutHeader _DeliveryNoteOutHeader)
        {
            bool _Ok = true;
            try
            {
                _ObjectSpace.CommitChanges();
            }
            catch (Exception)
            {

                _Ok = false;
            }

            return _Ok;
        }
        public static bool FinalizeDeliveryNoteOutHeader(XPObjectSpace _ObjectSpace, DeliveryNoteOutHeader _DeliveryNoteOutHeader, StorageOutHeader _StorageOutHeader)
        {
            bool _Ok = true;
            try
            {
                _StorageOutHeader.DeliveryNoteOutHeader = _ObjectSpace.GetObject(_DeliveryNoteOutHeader);
                _ObjectSpace.CommitChanges();
            }
            catch (Exception)
            {

                _Ok = false;
            }

            return _Ok;
        }
        /// <summary>
        /// Szállítólevél nyomtatása
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_Frame"></param>
        /// <param name="_DeliveryNoteOutHeader"></param>
        public static void PrintDeliveryNoteOutHeader(XPObjectSpace _ObjectSpace, Frame _Frame, DeliveryNoteOutHeader _DeliveryNoteOutHeader)
        {
            if (_DeliveryNoteOutHeader==null)
            {
                return;
            }
            if (_DeliveryNoteOutHeader.DeliveryNoteOutType.ReportDataV2 != null)
            {
                XAFPrintHelper _XAFPrintHelper = new XAFPrintHelper(_ObjectSpace, _Frame, _DeliveryNoteOutHeader.DeliveryNoteOutType.ReportDataV2,
                    CriteriaOperator.Parse("DeliveryNoteOutHeader.Oid = ?", _DeliveryNoteOutHeader.Oid));
                _XAFPrintHelper.ShowPreviewV2();
            }

        }
        /// <summary>
        /// Kitárolási bizonylatról kiszállítótt szállító stornírozása
        /// </summary>
        /// <param name="_ObjectSpace"></param>
        /// <param name="_DeliveryNoteOutHeader_Original"></param>
        /// <param name="_StorageOutHeader"></param>
        /// <returns></returns>
        public static DeliveryNoteOutHeader StornoDeliveryNoteOutHeaderfromStorageOutHeader(XPObjectSpace _ObjectSpace, StorageOutHeader _StorageOutHeader)
        {

            DeliveryNoteOutHeader _DeliveryNoteOutHeader = CreateNewDeliveryNoteOutHeader(_ObjectSpace);
            try
            {
                _StorageOutHeader.DeliveryNoteOutHeader.IsEditable = false;
                _StorageOutHeader.DeliveryNoteOutHeader.IsStorno = true;
                

                _DeliveryNoteOutHeader.ConnectInfo = _StorageOutHeader.DeliveryNoteOutHeader.DocumentNumber;
                _DeliveryNoteOutHeader.CurrencyName = _StorageOutHeader.DeliveryNoteOutHeader.CurrencyName;
                _DeliveryNoteOutHeader.Customers = _StorageOutHeader.DeliveryNoteOutHeader.Customers;
                _DeliveryNoteOutHeader.CustomersFrom = _StorageOutHeader.DeliveryNoteOutHeader.CustomersFrom;
                _DeliveryNoteOutHeader.DateCreated = GeneralFunction.GetSQLTime(_ObjectSpace.Session);
                _DeliveryNoteOutHeader.DateExecution = GeneralFunction.GetSQLTime(_ObjectSpace.Session);
                _DeliveryNoteOutHeader.DeliveryNoteOutType = _StorageOutHeader.DeliveryNoteOutHeader.DeliveryNoteOutType;
                _DeliveryNoteOutHeader.IsEditable = false;
                _DeliveryNoteOutHeader.IsStorno = true;
                _DeliveryNoteOutHeader.Note = _StorageOutHeader.DeliveryNoteOutHeader.Note;
                _DeliveryNoteOutHeader.QualityOption = _StorageOutHeader.DeliveryNoteOutHeader.QualityOption;
                _DeliveryNoteOutHeader.ShipmentOption = _StorageOutHeader.DeliveryNoteOutHeader.ShipmentOption;
                _DeliveryNoteOutHeader.TransportAddress = _StorageOutHeader.DeliveryNoteOutHeader.TransportAddress;

                foreach (DeliveryNoteOutRows _DeliveryNoteOutRows_Original in _StorageOutHeader.DeliveryNoteOutHeader.DeliveryNoteOutRows)
                {

                    _DeliveryNoteOutRows_Original.StorageOutRowsInRows.QTT_To_DeliveryOut = 0;
                    DeliveryNoteOutRows _DeliveryOutRows = _ObjectSpace.CreateObject<DeliveryNoteOutRows>();

                    _DeliveryOutRows.DeliveryNoteOutHeader = _DeliveryNoteOutHeader;
                    _DeliveryOutRows.ADRNumber = _DeliveryNoteOutRows_Original.ADRNumber;
                    _DeliveryOutRows.Note = _DeliveryNoteOutRows_Original.Note;
                    _DeliveryOutRows.QTT = _DeliveryNoteOutRows_Original.QTT * -1;
                    _DeliveryOutRows.QTT_Invoiced = 0;
                    _DeliveryOutRows.QTT_Will = 0;
                    _DeliveryOutRows.RowIndex = _DeliveryNoteOutRows_Original.RowIndex;
                    _DeliveryOutRows.StorageOutRowsInRows = _DeliveryNoteOutRows_Original.StorageOutRowsInRows;


                }

                _StorageOutHeader.DeliveryNoteOutHeader = null;
                _ObjectSpace.CommitChanges();
            }
            catch (Exception)
            {

            }
            finally
            {
                
            }
            return _DeliveryNoteOutHeader;
        }
        #endregion
        #region Invoice

        #endregion
    }
}
