Beispiel #1
0
        private static void GrabarRegistroCalidad(ref Application sbo_application, SAPbobsCOM.Company sbo_company, ItemEvent oItemEvent, string sessionId)
        {
            Form      oForm = null;
            DataTable dt    = null;
            Grid      grid  = null;

            try
            {
                oForm = sbo_application.Forms.Item(oItemEvent.FormUID);
                var _atributos    = new List <RegistroCalidad_Detalle>();
                var _totales      = new List <RegistroCalidad_Totales>();
                var _lotes        = new List <RegistroCalidad_Lotes>();
                var _DataTables   = new List <RegCalidadDataTables>();
                var _TextosCortos = CommonFunctions.GET(ServiceLayer.MaestroTextosCortos, null, null, sessionId, out _).DeserializeList <MaestroTextosCortos>();

                var titulosStdGrid = new List <string>
                {
                    "#",
                    "Muestra[Editable]",
                    "Fecha",
                    "Hora"
                };

                for (int i = 0; i < oForm.DataSources.DataTables.Count; i++)
                {
                    dt = oForm.DataSources.DataTables.Item(i);
                    var grduid = "gr" + dt.UniqueID.Replace("dt", "");
                    grid = oForm.Items.Item(grduid).Specific as Grid;
                    var dtXML    = XDocument.Parse(dt.SerializeAsXML(BoDataTableXmlSelect.dxs_All));
                    var Columns  = dtXML.Element("DataTable").Element("Columns").Elements("Column");
                    var Rows     = dtXML.Element("DataTable").Element("Rows").Elements("Row");
                    int _LineNum = 1;

                    var _dtCal = new RegCalidadDataTables {
                        dtXML = dt.SerializeAsXML(BoDataTableXmlSelect.dxs_All)
                    };
                    _DataTables.Add(_dtCal);

                    foreach (var Row in Rows)
                    {
                        var Cells = Row.Element("Cells").Elements("Cell");
                        foreach (var Cell in Cells)
                        {
                            string ColValue  = string.Empty;
                            var    ColName   = Cell.Element("ColumnUid").Value;
                            var    MaxLength = dtXML.Element("DataTable").Element("Columns").Elements("Column").Where(i => i.Attribute("Uid").Value == ColName).Select(i => i.Attribute("MaxLength").Value).FirstOrDefault();

                            if (int.Parse(MaxLength) == 254 && !titulosStdGrid.Any(word => ColName.Contains(word)))
                            {
                                ColValue = Cell.Element("Value").Value;
                                if (!string.IsNullOrEmpty(ColValue))
                                {
                                    ColValue = CommonFunctions.GET(ServiceLayer.MaestroTextosCortos, null, $"?$filter=Code eq {ColValue}", sessionId, out _).DeserializeList <MaestroTextosCortos>().First().U_Texto;
                                }
                            }
                            else
                            {
                                ColValue = Cell.Element("Value").Value;
                            }

                            var _attr = new RegistroCalidad_Detalle
                            {
                                DocEntry  = null,
                                LineId    = null,
                                U_LineNum = _LineNum,
                                U_Attr    = ColName,
                                U_Value   = (ColValue.Length > 254) ? "" : ColValue,
                                U_Text    = (ColValue.Length > 254) ? ColValue : "",
                                U_Title   = grid.Item.Description,
                            };

                            _atributos.Add(_attr);
                        }
                        _LineNum++;
                    }

                    foreach (var column in _atributos.Where(i => i.U_Title == grid.Item.Description).GroupBy(i => i.U_Attr))
                    {
                        if (!titulosStdGrid.Any(word => column.Key == word))
                        {
                            sbo_application.StatusBar.SetText($"Procesando {column.Key}", BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Warning);

                            if (double.TryParse(_atributos.Where(i => i.U_Title == grid.Item.Description && i.U_Attr == column.Key).Select(i => i.U_Value).FirstOrDefault(), out _))
                            {
                                var _sum   = _atributos.Where(i => i.U_Title == grid.Item.Description && i.U_Attr == column.Key).Average(i => i.U_Value.GetDoubleFromString(","));
                                var _count = _atributos.Count(i => i.U_Title == grid.Item.Description && i.U_Attr == column.Key);

                                if (_sum > 0)
                                {
                                    var _total = new RegistroCalidad_Totales
                                    {
                                        DocEntry  = null,
                                        LineId    = null,
                                        U_LineNum = _count,
                                        U_Attr    = column.Key,
                                        U_Value   = _sum.GetStringFromDouble(2),
                                        U_Title   = grid.Item.Description
                                    };

                                    _totales.Add(_total);
                                }
                            }
                            else
                            {
                                var _count = _atributos.Count(i => i.U_Title == grid.Item.Description && i.U_Attr == column.Key);
                                var _total = new RegistroCalidad_Totales
                                {
                                    DocEntry  = null,
                                    LineId    = null,
                                    U_LineNum = _count,
                                    U_Attr    = column.Key,
                                    U_Value   = _atributos.Where(i => i.U_Title == grid.Item.Description && i.U_Attr == column.Key).Select(i => i.U_Value).FirstOrDefault(),
                                    U_Title   = grid.Item.Description
                                };

                                _totales.Add(_total);
                            }
                        }
                    }
                }

                var _lotesStr = oForm.DataSources.UserDataSources.Item("UDLotes").ValueEx;
                if (!string.IsNullOrEmpty(_lotesStr))
                {
                    _lotes = _lotesStr.DeserializeList <RegistroCalidad_Lotes>();
                }

                var Obj = oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDCab).ValueEx.DeserializeJsonToDynamic();

                if (string.IsNullOrEmpty(oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx))
                {
                    var Registro = new RegistroCalidad
                    {
                        DocEntry             = null,
                        DocNum               = null,
                        U_PuntoControl       = oForm.Title.RemoveParents(),
                        U_Version            = 0.0,
                        U_FormXML            = _DataTables.SerializeJson(),
                        U_TotalKg            = _lotes.Sum(i => i.U_Kg),
                        U_BaseType           = Obj.Tipo.ToString(),
                        U_BaseEntry          = Obj.Valor.ToString(),
                        DFO_RQLTY1Collection = _atributos,
                        DFO_RQLTY2Collection = _totales,
                        DFO_RQLTY3Collection = _lotes,
                        DFO_RQLTY4Collection = null
                    };

                    var response = CommonFunctions.POST(ServiceLayer.RegistroCalidad, Registro, sessionId, out System.Net.HttpStatusCode httpStatus);
                    if (httpStatus == System.Net.HttpStatusCode.Created)
                    {
                        var RegOK = response.DeserializeJsonObject <RegistroCalidad>();

                        oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx = RegOK.DocEntry.ToString();
                        if (Registro.U_TotalKg > 0)
                        {
                            sbo_application.StatusBar.SetText("Calculando y actualizando calidad en tarjas", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                            CommonFunctions.ActualizarTotalesPorLote((int)RegOK.DocEntry, sessionId);
                        }
                        sbo_application.MessageBox($"Registro creado con éxito, número de correlativo {RegOK.DocEntry}");

                        oForm.Title = oForm.Title.TrimEnd(')') + $" Correlativo: {RegOK.DocEntry})";

                        foreach (var lot in RegOK.DFO_RQLTY3Collection)
                        {
                            string upd = $"update \"@DFO_OCAOF\" set \"U_RegCalidad\" = '{RegOK.DocEntry}' where \"U_IdTarja\"='{lot.U_BatchNum}'";
                            var    rs  = sbo_company.GetBusinessObject(BoObjectTypes.BoRecordset) as Recordset;
                            rs.DoQuery(upd);
                        }
                    }
                    else
                    {
                        var _Error = response.DeserializeJsonToDynamic();
                        throw new Exception($"Error en el registro : {_Error.error.message.value.ToString()}");
                    }
                }
                else
                {
                    string delR1 = $"delete from \"@DFO_RQLTY1\" where \"DocEntry\"={oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx}";
                    string delR2 = $"delete from \"@DFO_RQLTY2\" where \"DocEntry\"={oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx}";
                    string delR3 = $"delete from \"@DFO_RQLTY3\" where \"DocEntry\"={oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx}";
                    string delR4 = $"delete from \"@DFO_RQLTY4\" where \"DocEntry\"={oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx}";

                    var rs = sbo_company.GetBusinessObject(BoObjectTypes.BoRecordset) as Recordset;
                    rs.DoQuery(delR1);
                    rs.DoQuery(delR2);
                    rs.DoQuery(delR3);
                    rs.DoQuery(delR4);

                    var Registro = new RegistroCalidad
                    {
                        U_PuntoControl       = oForm.Title.RemoveParents(),
                        U_Version            = 0.0,
                        U_FormXML            = _DataTables.SerializeJson(),
                        U_TotalKg            = _lotes.Sum(i => i.U_Kg),
                        U_BaseType           = Obj.Tipo.ToString(),
                        U_BaseEntry          = Obj.Valor.ToString(),
                        DFO_RQLTY1Collection = _atributos,
                        DFO_RQLTY2Collection = _totales,
                        DFO_RQLTY3Collection = _lotes,
                        DFO_RQLTY4Collection = null
                    };

                    var response = CommonFunctions.PATCH(ServiceLayer.RegistroCalidad, Registro, oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx, sessionId, out System.Net.HttpStatusCode httpStatus);
                    if (httpStatus == System.Net.HttpStatusCode.NoContent)
                    {
                        if (Registro.U_TotalKg > 0)
                        {
                            CommonFunctions.ActualizarTotalesPorLote(int.Parse(oForm.DataSources.UserDataSources.Item(CommonForms.FormCalidad.UDEntry).ValueEx), sessionId);
                        }

                        sbo_application.MessageBox($"Registro {Registro.DocEntry} actualizado con éxito");
                    }
                    else
                    {
                        var _Error = response.DeserializeJsonToDynamic();
                        throw new Exception($"Error en la actualiazacion : {_Error.error.message.value.ToString()}");
                    }
                }
            }
            catch
            {
                throw;
            }
        }