private void btnUnApplyDelete_Click(object sender, RibbonControlEventArgs e) { if (_cells == null) _cells = new ExcelStyleCells(_excelApp); var excelBook = _excelApp.ActiveWorkbook; Worksheet excelSheet = excelBook.ActiveSheet; if (excelSheet.Name != SheetName01) return; var proxySheet = new BMUSheet.BulkMaterialUsageSheetService(); var opSheet = new BMUSheet.OperationContext(); if (drpBulkMaterialEnv.Label == null || drpBulkMaterialEnv.Label.Equals("")) return; proxySheet.Url = _eFunctions.GetServicesUrl(drpBulkMaterialEnv.SelectedItem.Label) + "/BulkMaterialUsageSheet"; _frmAuth.SelectedEnviroment = drpBulkMaterialEnv.SelectedItem.Label; _frmAuth.StartPosition = FormStartPosition.CenterScreen; if (_frmAuth.ShowDialog() != DialogResult.OK) return; opSheet.district = _frmAuth.EllipseDsct; opSheet.maxInstances = 100; opSheet.position = _frmAuth.EllipsePost; opSheet.returnWarnings = false; ClientConversation.authenticate(_frmAuth.EllipseUser, _frmAuth.EllipsePswd); var currentRow = TittleRow + 1; while ((_cells.GetNullIfTrimmedEmpty(_cells.GetCell(1, currentRow).Value)) != null) { var requestSheet = new BMUSheet.BulkMaterialUsageSheetDTO(); _cells.GetCell(1, currentRow).Select(); try { requestSheet.bulkMaterialUsageSheetId = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(1, currentRow).Value); var replySheet = proxySheet.unapply(opSheet, requestSheet); if (replySheet.errors.Length > 0) { foreach (var t in replySheet.errors) { _cells.GetCell(ResultColumn, currentRow).Value += " - " + t.messageText; } _cells.GetRange(1, currentRow, 6, currentRow).Style = _cells.GetStyle(StyleConstants.Error); _cells.GetRange(1, currentRow, 6, currentRow).Select(); } else { _cells.GetRange(1, currentRow, 6, currentRow).Style = _cells.GetStyle(StyleConstants.Success); _cells.GetRange(1, currentRow, 6, currentRow).Select(); DeleteHeader(proxySheet, opSheet, requestSheet, currentRow, currentRow); } } catch (Exception error) { _cells.GetRange(1, currentRow, 6, currentRow).Style = _cells.GetStyle(StyleConstants.Error); _cells.GetCell(ResultColumn, currentRow).Value = error.Message; _cells.GetCell(ResultColumn, currentRow).Select(); } finally { currentRow++; } } }
/// <summary> /// Crea las instancias a los servicios BulkMaterialUsageSheetService y BulkMaterialUsageSheetItemService /// </summary> private void BulkMaterialExcecute() { try { _cells.GetRange(1, TittleRow + 1, ResultColumn, MaxRows).ClearFormats(); _cells.GetRange(1, TittleRow + 1, ResultColumn, MaxRows).ClearComments(); if (_cells == null) _cells = new ExcelStyleCells(_excelApp); var excelBook = _excelApp.ActiveWorkbook; Worksheet excelSheet = excelBook.ActiveSheet; if (excelSheet.Name != SheetName01) return; var proxySheet = new BMUSheet.BulkMaterialUsageSheetService(); var opSheet = new BMUSheet.OperationContext(); var proxyItem = new BMUSheetItem.BulkMaterialUsageSheetItemService(); var opItem = new BMUSheetItem.OperationContext(); if (drpBulkMaterialEnv.Label == null || drpBulkMaterialEnv.Label.Equals("")) return; proxySheet.Url = _eFunctions.GetServicesUrl(drpBulkMaterialEnv.SelectedItem.Label) + "/BulkMaterialUsageSheet"; proxyItem.Url = _eFunctions.GetServicesUrl(drpBulkMaterialEnv.SelectedItem.Label) + "/BulkMaterialUsageSheetItem"; _frmAuth.SelectedEnviroment = drpBulkMaterialEnv.SelectedItem.Label; _frmAuth.StartPosition = FormStartPosition.CenterScreen; if (_frmAuth.ShowDialog() != DialogResult.OK) return; opSheet.district = _frmAuth.EllipseDsct; opSheet.maxInstances = 100; opSheet.position = _frmAuth.EllipsePost; opSheet.returnWarnings = false; opItem.district = _frmAuth.EllipseDsct; opItem.maxInstances = 100; opItem.position = _frmAuth.EllipsePost; opItem.returnWarnings = false; ClientConversation.authenticate(_frmAuth.EllipseUser, _frmAuth.EllipsePswd); try { _excelSheetItems.Sort.SortFields.Clear(); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(2, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(3, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(4, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(6, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(9, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(9, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(10, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.SortFields.Add(_cells.GetCell(11, TittleRow), XlSortOn.xlSortOnValues, XlOrder.xlDownThenOver, Type.Missing, Type.Missing); _excelSheetItems.Sort.Apply(); var currentRow = TittleRow + 1; while ((_cells.GetNullIfTrimmedEmpty(_cells.GetCell(3, currentRow).Value)) != null) { var currentHeader = currentRow; var requestSheet = new BMUSheet.BulkMaterialUsageSheetDTO(); var requestItemList = new List<BMUSheetItem.BulkMaterialUsageSheetItemDTO>(); var allRequestItemList = new List<BMUSheetItem.BulkMaterialUsageSheetItemDTO>(); _cells.GetCell(1, currentRow).Select(); //llenado de variables del encabezado de la hoja requestSheet.bulkMaterialUsageSheetId = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(1, currentRow).Value) != null ? _cells.GetEmptyIfNull(_cells.GetCell(1, currentRow).Value) : null; requestSheet.districtCode = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(2, currentRow).Value) ?? "ICOR"; requestSheet.warehouseId = _cells.GetEmptyIfNull(_cells.GetCell(3, currentRow).Value); requestSheet.defaultUsageDate = _cells.GetEmptyIfNull(_cells.GetCell(4, currentRow).Value); requestSheet.defaultAccountCode = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(6, currentRow).Value) != null ? _cells.GetEmptyIfNull(_cells.GetCell(6, currentRow).Value) : null; requestSheet.defaultAccountCode = requestSheet.defaultAccountCode ?? GetBulkAccountCode(_cells.GetNullIfTrimmedEmpty(_cells.GetCell(8, currentRow).Value)); //Crea el encabezado var replySheet = proxySheet.create(opSheet, requestSheet); //valida si el encabezado tiene errores if (replySheet.errors.Length > 0) { foreach (var t in replySheet.errors) _cells.GetCell(ResultColumn, currentRow).Value += " - " + t.messageText; _cells.GetRange(1, currentHeader, 6, currentRow).Style = _cells.GetStyle(StyleConstants.Error); _cells.GetRange(1, currentHeader, 6, currentRow).Select(); currentRow++; } else { //si el encabezado no tiene errores empueza a agregar los items a la coleccion. requestSheet.bulkMaterialUsageSheetId = replySheet.bulkMaterialUsageSheetDTO.bulkMaterialUsageSheetId; _cells.GetCell(1, currentRow).Value = replySheet.bulkMaterialUsageSheetDTO.bulkMaterialUsageSheetId; //mientras que el encabezado sea el mismo, llene la lista de items var sheetId = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(1, currentRow).Value); var warehouseId = _cells.GetEmptyIfNull(_cells.GetCell(3, currentRow).Value); var defaultUsageDate = _cells.GetEmptyIfNull(_cells.GetCell(4, currentRow).Value); var defaultAccountCode = (_cells.GetNullIfTrimmedEmpty(_cells.GetCell(6, currentRow).Value) != null ? _cells.GetEmptyIfNull(_cells.GetCell(6, currentRow).Value) : null); defaultAccountCode = defaultAccountCode ?? GetBulkAccountCode(_cells.GetNullIfTrimmedEmpty(_cells.GetCell(8, currentRow).Value)); while ( ( requestSheet.bulkMaterialUsageSheetId == sheetId || ( sheetId == null && requestSheet.warehouseId == warehouseId && requestSheet.defaultUsageDate == defaultUsageDate && requestSheet.defaultAccountCode == defaultAccountCode ) ) ) { ItemListAdd(currentRow, requestSheet, requestItemList, allRequestItemList, excelSheet); currentRow++; sheetId = _cells.GetNullIfTrimmedEmpty(_cells.GetCell(1, currentRow).Value); warehouseId = _cells.GetEmptyIfNull(_cells.GetCell(3, currentRow).Value); defaultUsageDate = _cells.GetEmptyIfNull(_cells.GetCell(4, currentRow).Value); defaultAccountCode = (_cells.GetNullIfTrimmedEmpty(_cells.GetCell(6, currentRow).Value) != null ? _cells.GetEmptyIfNull(_cells.GetCell(6, currentRow).Value) : null); defaultAccountCode = defaultAccountCode ?? GetBulkAccountCode(_cells.GetNullIfTrimmedEmpty(_cells.GetCell(8, currentRow).Value)); } try { if (requestItemList.Count > 0) { //esta operacion agrega la lista de items al encabezado var replyItem = proxyItem.multipleCreate(opItem, requestItemList.ToArray()); //recorre el resultado de la ejecucion de la operacion multipleCreate donde hubo errores. var errorCounter = 0; foreach (var rItem in replyItem.Where(rItem => rItem.errors.Length > 0)) { errorCounter++; var errorMessage = rItem.errors.Aggregate("", (current, error) => current + (error.messageText + ", ")); var currentItem = 0; foreach (var item in allRequestItemList) { if (_cells.GetEmptyIfNull(item.bulkMaterialUsageSheetId).ToUpper() == _cells.GetEmptyIfNull(rItem.bulkMaterialUsageSheetItemDTO.bulkMaterialUsageSheetId) & _cells.GetEmptyIfNull(item.bulkMaterialUsageSheetItemId).ToUpper() == _cells.GetEmptyIfNull(rItem.bulkMaterialUsageSheetItemDTO.bulkMaterialUsageSheetItemId) ) { requestItemList.Remove(item); _cells.GetRange(8, currentHeader + currentItem, 13, currentHeader + currentItem).Style = _cells.GetStyle(StyleConstants.Error); _cells.GetCell(ResultColumn, currentHeader + currentItem).Value += errorMessage; _cells.GetCell(ResultColumn, currentHeader + currentItem).Select(); } currentItem++; } } if (errorCounter > 0 & requestItemList.Count > 0) { try { var deleteHeader = false; replyItem = proxyItem.multipleCreate(opItem, requestItemList.ToArray()); foreach (var rItem in replyItem.Where(item => item.errors.Length > 0)) { deleteHeader = true; } if (deleteHeader) { DeleteHeader(proxySheet, opSheet, requestSheet, currentHeader, currentRow - 1); } else { ApplyHeader(proxySheet, opSheet, requestSheet, currentRow - 1, currentHeader); } } catch (Exception error) { MessageBox.Show(error.Message); DeleteHeader(proxySheet, opSheet, requestSheet, currentHeader, currentRow - 1); } } else if (errorCounter == 0 & requestItemList.Count > 0) { ApplyHeader(proxySheet, opSheet, requestSheet, currentRow - 1, currentHeader); } else { DeleteHeader(proxySheet, opSheet, requestSheet, currentHeader, currentRow - 1); } } else { _cells.GetCell(ResultColumn, currentRow - 1).Value += "No hay Items para Aplicar en esta hoja!"; DeleteHeader(proxySheet, opSheet, requestSheet, currentHeader, currentRow - 1); } } catch (Exception error) { MessageBox.Show(error.Message); } } } } catch (Exception error) { MessageBox.Show(error.Message); } } catch (Exception error) { MessageBox.Show(error.Message); } }