using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace LLNet.BLL.PDV
{
    public class PDV
    {
        private static BLL.BLLServer bll;
        private static Model.PDV.DsPDV DsFactura;
        private static Model.PDV.DsPDV DsPedido;
        private static Model.PDV.DsPDV DsRecibo;
        private static Model.PDV.DsPDV dsNotaCredito;
        private static Model.PDV.DsPDV DsVale;
        private static Model.PDV.DsPDV DsVale2;
        private static decimal? Jornada;

        public PDV()
        { 
        }

        public static void PostData()
        {
            // Guardando datos del pedido
             bll.updateDataSet(DsPedido, DsPedido.WMS_DOCUMENTOS.TableName);
            bll.updateDataSet(DsPedido, DsPedido.WMS_SOCIOS.TableName);

            // Guardando datos de la factura
            if (DsFactura.WMS_DOCUMENTOS.Count > 0)
            {
                bll.updateDataSet(DsFactura, DsFactura.WMS_DOCUMENTOS.TableName);
                bll.updateDataSet(DsFactura, DsFactura.WMS_SKUS_X_DOCUMENTO.TableName);
            }
            else
                Console.WriteLine("sin factura");

            if (DsRecibo.WMS_DOCUMENTOS.Count > 0)
            {
                
                
                // Guardando datos del recibo
                bll.updateDataSet(DsRecibo, DsRecibo.WMS_DOCUMENTOS.TableName);
                bll.updateDataSet(DsRecibo, DsRecibo.DOC_ABONO_DOCUMENTOS.TableName);
                bll.updateDataSet(DsRecibo, DsRecibo.WMS_PAGO_DOCUMENTOS.TableName);
            }
            else
                Console.WriteLine("sin recibo");

            if (DsPedido.DOC_VALES.Count > 0)
            {
                bll.updateDataSet(DsPedido, DsPedido.DOC_VALES.TableName);
            }

            if (DsFactura.DOC_VALES.Count > 0)
            {
                bll.updateDataSet(DsFactura, DsFactura.DOC_VALES.TableName);
            }

            if (dsNotaCredito.WMS_DOCUMENTOS.Count > 0)
            {
                bll.updateDataSet(dsNotaCredito, dsNotaCredito.WMS_DOCUMENTOS.TableName);
            }

            if (dsNotaCredito.WMS_SKUS_X_DOCUMENTO.Count > 0)
            {
                bll.updateDataSet(dsNotaCredito, dsNotaCredito.WMS_SKUS_X_DOCUMENTO.TableName);
            }

            if (DsVale.DOC_VALES.Count > 0)
            {
                bll.updateDataSet(DsVale, DsVale.DOC_VALES.TableName);
            }

            if (DsVale2.DOC_VALES.Count > 0)
            {
                bll.updateDataSet(DsVale2, DsVale2.DOC_VALES.TableName);
            }

            if (DsRecibo.PAGO_ANTICIPADO.Count > 0)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("FACTURA", DsRecibo.PAGO_ANTICIPADO[0].FACTURA.ToString());
                qp.AddReplaceParam("MONTO", DsRecibo.PAGO_ANTICIPADO[0].MONTO.ToString());
                bll.ExecuteSql("CobrarPagoAnticipado", qp, "PDV");
            }

            if (DsPedido.WMS_DOCUMENTOS.Count > 0)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("DOCUMENTO", DsFactura.WMS_DOCUMENTOS[0].DOCUMENTO.ToString());
                bll.ExecuteSql("wms_p_ponderar_precio", qp, "PDV");
                bll.ExecuteSql("wms_p_documentos.procesar_facturas_in", qp, "PDV");
                bll.ExecuteSql("CalculaPuntos", qp, "PDV");
                bll.ExecuteSql("ActualizarVales", qp, "PDV");
                bll.ExecuteSql("CobrarValePorPagos", qp, "PDV");
                qp.AddReplaceParam("PEDIDO", DsPedido.WMS_DOCUMENTOS[0].DOCUMENTO.ToString());
                bll.ExecuteSql("GuardarSeries", qp, "PDV");
            }

            if (DsRecibo.WMS_DOCUMENTOS.Count > 0)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("RECIBO", DsRecibo.WMS_DOCUMENTOS[0].DOCUMENTO.ToString());
                bll.ExecuteSql("ActualizaValesHijos", qp, "PDV");
            }

        }

        public static void Facturacion(BLL.BLLServer bllInstance, DAL.IDAL dalInstance, ref object[] pArgs)
        {
            try
            {
                dalInstance.BeginTransaction();

                bll = bllInstance;
                DsPedido = pArgs[0] as Model.PDV.DsPDV;
                DsFactura = pArgs[1] as Model.PDV.DsPDV;
                DsRecibo = pArgs[2] as Model.PDV.DsPDV;
                Jornada = pArgs[3] as decimal?;
                dsNotaCredito = pArgs[4] as Model.PDV.DsPDV;
                DsVale2 = pArgs[5] as Model.PDV.DsPDV;
                DsVale = pArgs[6] as Model.PDV.DsPDV;
                PostData();

                dalInstance.Commit();
            }
            catch (Exception ex)
            {
                dalInstance.Rollback();
                throw new Exception(ex.Message);
            }
        }

        public static void InsertKits(BLL.BLLServer bllInstance, DAL.IDAL moduleInstance, ref DataSet param)
        {
            try
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                moduleInstance.BeginTransaction();

                // Establecemos los parametros iniciales
                DataRow row = param.Tables[0].Rows[0];
                decimal documento = Convert.ToDecimal(row[0]);
                decimal linea = Convert.ToDecimal(row[1]);
                decimal sku = Convert.ToDecimal(row[2]);
                decimal cantidad = Convert.ToDecimal(row[3]);

                qp.AddReplaceParam("SKU", sku.ToString());
                qp.AddReplaceParam("CANTIDAD", cantidad.ToString());

                // obtenemos los productos del combo
                bllInstance.ExecuteSql("ArmarArbolPedido", qp, "PDV");

                qp.Clear();
                qp.AddReplaceParam("DOCUMENTO", documento.ToString());

                Model.PDV.DsPDV dsPdv = new LLNet.Model.PDV.DsPDV();
                Utils.Helpers.Merge(bllInstance.getTypedDataSet(dsPdv.GetType(), dsPdv.TMP_ARBOL_SKU.TableName), dsPdv);

                decimal detalleLinea = Convert.ToDecimal(bllInstance.GetSingleValue("SigDetalleDocumento", qp, "PDV"));

                // insertamos los productos del combo con precio cero
                foreach (Model.PDV.DsPDV.TMP_ARBOL_SKURow asr in dsPdv.TMP_ARBOL_SKU)
                {
                    if (sku != asr.SKU)
                    {
                        Model.PDV.DsPDV.WMS_SKUS_X_DOCUMENTORow sdr = dsPdv.WMS_SKUS_X_DOCUMENTO.NewWMS_SKUS_X_DOCUMENTORow();
                        sdr.DOCUMENTO = documento;
                        sdr.LINEA = detalleLinea;
                        sdr.LINEA_PADRE = linea;
                        sdr.SKU = asr.SKU;
                        if (asr["COSTO_PROMEDIO"] != DBNull.Value)
                            sdr.COSTO_PROMEDIO = asr.COSTO_PROMEDIO;

                        sdr.PRECIO_UNITARIO = 0;
                        sdr.IMPUESTO = 0;
                        sdr.CANTIDAD = asr.TOTAL;
                        Utils.QueryParams qParams = new LLNet.Utils.QueryParams();
                        qParams.AddReplaceParam("Param", "TDDF");
                        sdr.SITUACION_SKU = Convert.ToDecimal(bllInstance.GetSingleValue("GetParam", qParams, "General"));
                        dsPdv.WMS_SKUS_X_DOCUMENTO.AddWMS_SKUS_X_DOCUMENTORow(sdr);
                        detalleLinea += 1; 
                    }                    
                }

                bllInstance.updateDataSet(dsPdv);

                if (dsPdv.TMP_ARBOL_SKU.Count > 1)
                {
                    qp.AddReplaceParam("LINEA", linea.ToString());
                    bllInstance.ExecuteSql("ActualizarKit", qp, "PDV");
                }

                //-- Completamos la transaccion
                moduleInstance.Commit();
            }
            catch (Exception e)
            {
                moduleInstance.Rollback();
                throw new Exception(e.Message);
            }
        }

        public static void FillSkuTree(BLL.BLLServer bllInstance, DAL.IDAL moduleInstance, ref object[] pArgs)
        {
            try
            {
                moduleInstance.BeginTransaction();

                Model.PDV.DsPDV dsArbol = pArgs[0] as Model.PDV.DsPDV;

                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("SKU", pArgs[1].ToString());
                bllInstance.ExecuteSql("ArmarArbol", qp, "PDV");
                Utils.Helpers.Merge(bllInstance.getTypedDataSet(dsArbol.GetType(), dsArbol.TMP_ARBOL_SKU.TableName), dsArbol);                
                moduleInstance.Commit();
            }
            catch (Exception e)
            {
                moduleInstance.Rollback();
                throw new Exception(e.Message);
            }
        }
    }
}