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.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;

namespace Nuvolar.Module.CSharp
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class OrderIn_VC : Nuvolar.Module.OrderInVC
    {
        public OrderIn_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();
        }

        private void aPrintLastInvoice_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            OrderInHeader _OrderInHeader = View.SelectedObjects[0] as OrderInHeader;

            XPCollection<InvoiceRows> _InvoiceRows_Collection = new XPCollection<InvoiceRows>(_ocur.Session,
                CriteriaOperator.Parse("(OrderInRows.OrderInHeader = ? or OrderInRowsOther.OrderInHeader = ?) and IsNull(InvoiceHeader.GCRecord)=True and IsNull(InvoiceHeader)=False",
                _OrderInHeader.Oid, _OrderInHeader.Oid));

            if (_InvoiceRows_Collection.Count > 0)
            {
                _InvoiceRows_Collection.Sorting.Add(new SortProperty("InvoiceHeader.DateCreated", DevExpress.Xpo.DB.SortingDirection.Descending));
                _InvoiceRows_Collection.Sorting.Add(new SortProperty("InvoiceHeader.DocumentNumber", DevExpress.Xpo.DB.SortingDirection.Ascending));

                InvoiceHeader _InvoiceHeader = _InvoiceRows_Collection[0].InvoiceHeader;
                XAFPrintHelper _XAFPrintHelper = new XAFPrintHelper(_ocur, Frame, _InvoiceHeader.InvoiceType.ReportDataV2, CriteriaOperator.Parse("InvoiceHeader.Oid = ?", _InvoiceHeader.Oid));
                _XAFPrintHelper.ShowPreviewV2();
            }
        }

        private void aPrintLastDeliveryNoteOut_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            OrderInHeader _OrderInHeader = View.SelectedObjects[0] as OrderInHeader;

            XPCollection<DeliveryNoteOutRows> _DeliveryNoteOutRows_Collection = new XPCollection<DeliveryNoteOutRows>(_ocur.Session, 
                CriteriaOperator.Parse("StorageOutRowsInRows.StorageOutRows.OrderInRows.OrderInHeader.Oid = ?",_OrderInHeader.Oid));

            if (_DeliveryNoteOutRows_Collection.Count>0)
            {
                _DeliveryNoteOutRows_Collection.Sorting.Add(new SortProperty("DeliveryNoteOutHeader.DateCreated", DevExpress.Xpo.DB.SortingDirection.Descending));
                _DeliveryNoteOutRows_Collection.Sorting.Add(new SortProperty("DeliveryNoteOutHeader.DocumentNumber", DevExpress.Xpo.DB.SortingDirection.Ascending));

                DeliveryNoteOutHeader _DeliveryNoteOutHeader = _DeliveryNoteOutRows_Collection[0].DeliveryNoteOutHeader;
                XAFPrintHelper _XAFPrintHelper = new XAFPrintHelper(_ocur, Frame, _DeliveryNoteOutHeader.DeliveryNoteOutType.ReportDataV2, CriteriaOperator.Parse("DeliveryNoteOutHeader.Oid = ?", _DeliveryNoteOutHeader.Oid));
                _XAFPrintHelper.ShowPreviewV2();
            }
        }
    }
}
