ImpDEx/Main.cpp
2021-09-12 22:10:49 +02:00

382 lines
13 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#include <io.h>
#include <stdio.h>
#include <inifiles.hpp>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ImpName = "datos-imp\\default";
LoadDataBase( ImpName );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadDataBase( AnsiString DB )
{
TbImp->Active = false;
try {
TbImp->TableName = DB+".db";
TbImp->Active = true;
// Cargamos la configuración de esta asociación...
TIniFile *pIniFile = new TIniFile(DB+".ini");
ComboBox1->ItemIndex = pIniFile->ReadInteger("columnas", "cantidad", 1);
ComboBox2->ItemIndex = pIniFile->ReadInteger("columnas", "posID", 1);
ComboBox3->ItemIndex = pIniFile->ReadInteger("columnas", "posDESCR", 2);
ComboBox4->ItemIndex = pIniFile->ReadInteger("columnas", "posPVD", 3);
viudos->Checked = pIniFile->ReadBool("opciones", "comprobarViudos", true);
anuevos->Checked = pIniFile->ReadBool("opciones", "anadirNuevos", false);
FileToImport->Text = pIniFile->ReadString("opciones", "origen", "");
delete pIniFile;
} catch(...) {
// nothing
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SaveCFG( AnsiString DB )
{
// Guardamos la configuración de esta asociación...
TIniFile *pIniFile = new TIniFile(DB+".ini");
pIniFile->WriteInteger("columnas", "cantidad", ComboBox1->ItemIndex);
pIniFile->WriteInteger("columnas", "posID", ComboBox2->ItemIndex);
pIniFile->WriteInteger("columnas", "posDESCR", ComboBox3->ItemIndex);
pIniFile->WriteInteger("columnas", "posPVD", ComboBox4->ItemIndex);
pIniFile->WriteBool("opciones", "comprobarViudos", viudos->Checked);
pIniFile->WriteBool("opciones", "anadirNuevos", anuevos->Checked);
pIniFile->WriteString("opciones", "origen", FileToImport->Text);
delete pIniFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TbImpBeforeOpen(TDataSet *DataSet)
{
if ( access( (TbImp -> TableName).c_str(), 0 ) != 0 )
{
// Usamos la propiedad FielDefs para definir
// las columnas que contendrá la tabla
TbImp -> FieldDefs -> Clear();
/********************\
|* Datos Básicos *|
\********************/
TbImp -> FieldDefs -> Add("idS", ftString, 15, false );
TbImp -> FieldDefs -> Add("DescS", ftString, 50, false );
TbImp -> FieldDefs -> Add("PVD", ftCurrency, 0, false );
TbImp -> FieldDefs -> Add("status", ftInteger, 0, false );
// 0 -- OK
// 1 -- Origen sin destino ( EN LOS PRODUCTOS A IMPORTAR no esta este )
// 2 -- Destino sin origen ( PRODUCTO a IMPORTAR no lo tenemos en lista )
TbImp -> FieldDefs -> Add("idT", ftInteger, 0, false );
TbImp -> FieldDefs -> Add("DescT", ftInteger, 0, false );
TbImp -> IndexDefs-> Clear();
TbImp->IndexDefs->Add("Primary", "idS", TIndexOptions() << ixPrimary << ixUnique);
// Creamos la base...
TbImp -> CreateTable();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MergeImport( AnsiString Fich )
{
FILE *iFich;
char buff[500];
try {
if ( viudos->Checked )
{
// Ponemos a estado 2(Viudos) TODOS los registros
TbImp->First();
while( !TbImp->Eof )
{
TbImp->Edit();
TbImp->FieldByName("status")->AsInteger = 2;
TbImp->Post();
TbImp->Next();
}
}
AnsiString IDs, Desc; Currency PVD;
// Abrimos el fichero
iFich = fopen(Fich.c_str(), "r" );
while( !feof(iFich) )
{
fgets( buff, 500, iFich );
// Decodificamos la cadena...
if ( DecoCampos( buff,
ComboBox2->ItemIndex, IDs,
ComboBox3->ItemIndex, Desc,
ComboBox4->ItemIndex, PVD ) )
{
// Buscamos si el IDs existe dentro de las asociaciones
if ( ! (TbImp -> Locate( "idS", IDs, TLocateOptions() ) ) )
{
if ( anuevos->Checked )
{
// Si no esta, lo agregamos...
TbImp -> Insert();
TbImp -> FieldByName( "idS" ) -> AsString = IDs;
TbImp -> FieldByName( "DescS" ) -> AsString = Desc;
TbImp -> FieldByName( "status" ) -> AsInteger = 1;
TbImp -> FieldByName( "PVD" ) -> AsCurrency = PVD;
TbImp -> Post();
}
} else {
TbImp -> Edit();
TbImp -> FieldByName( "PVD" ) -> AsCurrency = PVD;
TbImp -> FieldByName( "status" ) -> AsInteger = (TbImp->FieldByName("idT")->AsInteger)?0:2;
TbImp -> Post();
}
}
}
fclose(iFich);
}catch(...){
ShowMessage("No fue posible importar los datos" );
}
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::DecoCampos( char *datos,
int posID, AnsiString &ID,
int posDesc, AnsiString &Desc,
int posPVD, Currency &PVD )
{
bool dev = false; char buff[500];
if ( ObtenCampo( datos, posID, buff ) )
{
ID = AnsiString(buff).Trim();
if ( ObtenCampo( datos, posDesc, buff ) )
{
Desc = AnsiString(buff).Trim();
if ( ObtenCampo( datos, posPVD, buff ) )
{
EliminaCaracter( buff, '.' );
AnsiString sPVD = AnsiString(buff).Trim();
try
{
PVD = sPVD.ToDouble();
dev = true;
} catch(...) {
PVD = 0;
dev = true;
}
}
}
}
return dev;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::EliminaCaracter( char *datos, char c )
{
char *ini, *cdatos;
ini = datos;
cdatos = datos;
while ( *ini != '\0' )
if ( *ini != c ) *(cdatos++)= *(ini++); else ini++;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::ObtenCampo( char *datos, int pos, char *dest )
{
char *inicio, *cdest;
inicio = datos;
cdest = dest;
// ATENCION DEBE SER TEXTO - - T A B U L A D O - -
// Nos saltamos todos los campos iniciales...
while ( *inicio != '\0' && pos > 0 )
if ( *(inicio++) == '\t' ) pos--;
if ( *inicio == '\0' )
return false;
while ( *inicio != '\0' && *inicio != '\t' )
*(cdest++) = *(inicio++);
*cdest = '\0';
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TbImpCalcFields(TDataSet *DataSet)
{
// Atencion: Los incrementos que aplicamos son LINEALES
TbImp->FieldByName("inc")->AsCurrency = TbImp->FieldByName("PVD")->AsCurrency - TbImp->FieldByName("PVDa")->AsCurrency;
TbImp->FieldByName("newPVP")->AsCurrency = TbImp->FieldByName("PVP")->AsCurrency + TbImp->FieldByName("inc")->AsCurrency;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Salir1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Acercade1Click(TObject *Sender)
{
ShowMessage( "www . infdj .com\n\tImportador De Datos Externos\n\tImporta las listas de precio de tus fabricantes hacia tus tarifas actualizando estas de forma totalmente automática.\nhttp://jd.infdj.com" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BarradeEstado1Click(TObject *Sender)
{
StatusBar1->Visible = !StatusBar1->Visible;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BarradeHerramientas1Click(TObject *Sender)
{
CoolBar1->Visible = !CoolBar1->Visible;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToolButton1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ConfigurarImportacin1Click(TObject *Sender)
{
PageControl1->ActivePage = TabSheet1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CargarDatosacotejar1Click(TObject *Sender)
{
OpenDialog1->Title = "Datos a importar";
OpenDialog1->InitialDir = GetCurrentDir();
OpenDialog1->Filter = "Datos Tabulados (*.txt)|*.TXT";
if (OpenDialog1->Execute())
{
if (FileExists(OpenDialog1->FileName))
{
FileToImport->Text = OpenDialog1->FileName;
SaveCFG( ImpName );
MergeImport(OpenDialog1->FileName);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NuevaAsociacion1Click(TObject *Sender)
{
SaveDialog1->Options.Clear();
SaveDialog1->InitialDir = GetCurrentDir();
SaveDialog1->Filter = "Asociaciones (*.ini)|*.INI";
SaveDialog1->FilterIndex = 0;
if (SaveDialog1->Execute())
{
ImpName = SaveDialog1->FileName;
AsociacionActual->Caption = ExtractFileName( ImpName );
LoadDataBase( ImpName );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Cargarasociaciones1Click(TObject *Sender)
{
OpenDialog1->Options.Clear();
OpenDialog1->InitialDir = GetCurrentDir();
OpenDialog1->Filter = "Asociaciones (*.ini)|*.INI";
OpenDialog1->FilterIndex = 0;
if (OpenDialog1->Execute())
{
ImpName = OpenDialog1->FileName.SubString(1,OpenDialog1->FileName.Length()-4) ;
AsociacionActual->Caption = ExtractFileName( ImpName );
LoadDataBase( ImpName );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
if ( TbImp->FieldByName("status")->AsInteger == 0 )
{
if ( TbImp->FieldByName("PVD")->AsCurrency > TbImp->FieldByName("PVDa")->AsCurrency )
{
DBGrid1 -> Canvas -> Font -> Color = clMaroon;
} else {
if ( TbImp->FieldByName("PVD")->AsCurrency < TbImp->FieldByName("PVDa")->AsCurrency )
DBGrid1 -> Canvas -> Font -> Color = clGreen;
}
DBGrid1 -> DefaultDrawColumnCell( Rect, DataCol, Column, State );
} else {
DBGrid1 -> Canvas -> Font -> Color = clWindowText;
}
if ( Column -> ID == 2 )
{
switch( TbImp->FieldByName("status")->AsInteger )
{
case 1: // Origen sin destino
DBGrid1 -> Canvas -> Draw( Rect.Left,Rect.Top, OsD->Picture->Graphic );
break;
case 2: // Destino sin origen
DBGrid1 -> Canvas -> Draw( Rect.Left,Rect.Top, DsO->Picture->Graphic );
break;
default: // OK!
DBGrid1 -> Canvas -> Brush -> Color = clWindow;
DBGrid1 -> Canvas -> FillRect( Rect );
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FusionarNuevosdatos1Click(TObject *Sender)
{
int pid;
if (MessageDlg("Se actualizarán todos los productos cazados de la lista. ¿Desea continuar?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
{
TbImp->First(); // Recorremos toda la lista de asociaciones
while( !TbImp->Eof )
{
// Por cada pareja CAZADA
pid = TbImp->FieldByName("idT")->AsInteger;
if ( pid > 0 && TbDatosT->Locate( "ForcedIndex", pid, TLocateOptions() ) )
{
// Actualizamos los nuevos precios
TbDatosT->Edit();
TbDatosT->FieldByName("Precio Venta 1")->AsCurrency = TbImp->FieldByName("newPVP")->AsCurrency;
TbDatosT->FieldByName("Precio Costo")->AsCurrency = TbImp->FieldByName("PVD")->AsCurrency;
TbDatosT->Post();
}
TbImp->Next();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::VersoloNOAsociados1Click(TObject *Sender)
{
((TMenuItem*)Sender)->Checked = true;
TbImp->Filter = "[idT] <= 0";
TbImp->Filtered = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FiltrarporNoAsociados1Click(TObject *Sender)
{
((TMenuItem*)Sender)->Checked = true;
TbImp->Filtered = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FiltrarporAsociados1Click(TObject *Sender)
{
((TMenuItem*)Sender)->Checked = true;
TbImp->Filter = "[idT] > 0";
TbImp->Filtered = true;
}
//---------------------------------------------------------------------------