// <ReferenceInclude>"POS.Devices.Opos_Constants.dll"</ReferenceInclude>
// <ReferenceInclude>"POS.Devices.OPOSCashDrawer.dll"</ReferenceInclude>
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Reflection;
using System.Windows.Forms;
using POS.Devices;
using POS;

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {
        public void Main(MercatorUi.ICustomizers.ExecAction Action)
        {
            if (Action == MercatorUi.ICustomizers.ExecAction.DossierOpen)
            {
                MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawer += new MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerEventHandler(MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer);
            }
            if (Action == MercatorUi.ICustomizers.ExecAction.DossierClose)
            {
                MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawer -= new MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerEventHandler(MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer);
            }
        }
        void MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer(MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerEventArgs e)
        {
            POS.Devices.OPOSCashDrawer tiroir = new POS.Devices.OPOSCashDrawer();
            int rc = 0;
            rc = tiroir.Open(Globals.ParamPos["PORT_TIR"]);
            if (rc != (int)OPOS_Constants.OPOS_SUCCESS)
            {
                Dialogs.Stop(((OPOS_Constants)Enum.ToObject(typeof(OPOS_Constants), rc)).ToString());
                e.Result = MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerResultEnum.Failed;
                return;
            }
            rc = tiroir.ClaimDevice(200);
            tiroir.DeviceEnabled = true;
            if (rc != (int)OPOS_Constants.OPOS_SUCCESS)
            {
                Dialogs.Stop(((OPOS_Constants)Enum.ToObject(typeof(OPOS_Constants), rc)).ToString());
                e.Result = MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerResultEnum.Failed;
                return;
            }
            rc = tiroir.OpenDrawer();
            if (rc != (int)OPOS_Constants.OPOS_SUCCESS)
            {
                Dialogs.Stop(((OPOS_Constants)Enum.ToObject(typeof(OPOS_Constants), rc)).ToString());
                e.Result = MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerResultEnum.Failed;
                return;
            }
            e.Result = MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerResultEnum.Done;
            tiroir.DeviceEnabled = false;
            tiroir.ReleaseDevice();
            tiroir.Close();
        }
    }
}