procedure TFFactura.BitBtn4Click(Sender: TObject);
var numfact, i, OrdTrans : integer;
Fecha, FechaVence, iv: String;
FPagoOK : Boolean;
MntContado, MntCheque, MntTarjeta, MntOtros, Saldo, Pagado :String;
BalanceAnterior, BalanceTotal : Double;
Tipo :char;
begin
If cbTipo.ItemIndex = 0 then
begin
FFormaPago:=TFFormaPago.Create(self);
FFormaPago.Valores(SubTotal, Impuesto, Total);
try
FFormaPago.ShowModal;
finally
FPagoOK := FFormaPago.OK;
MntContado := FFormaPago.FEContado.Text;
MntCheque := FFormaPago.FECheque.Text;
MntTarjeta := FFormaPago.FETarjeta.Text;
MntOtros := FFormaPago.FEOtro.Text;
Saldo := FloatToStr(FFormaPago.Saldo);
Pagado := FloatToStr(FFormaPago.Pagado);
FFormaPago.Free;
end;
Tipo:= 'F';
end else
begin
FPagoOK := True;
MntContado := '0';
MntCheque := '0';
MntTarjeta := '0';
MntOtros := '0';
Saldo := '0';
Pagado := '0';
Tipo := 'C';
end;
If FPagoOk then
begin
ShortDateFormat := 'yyyy-mm-dd';
Fecha := DateToStr(now);
FechaVence := DateToStr(IncDay(now,(StrToInt(Label21.Caption))));
ShortDateFormat := 'dd/mm/yyyy';
// Obtener el numero de factura
QTemp.SQL.Text := 'Select Max(CodFactura) From Factura where Tipo = '+QuotedStr(Tipo);
QTemp.Open;
If QTemp.RecordCount = 0 then NumFact:=1
else NumFact := QTemp.Fields[0].AsInteger+1;
QTemp.Close;
//Iniciar la Transaccion
// DataModule1.AC1.BeginTrans;
try
If Edit2.Text <> '' then
// Insertar en la tabla de Factura Si tiene ORDEN
QTemp.SQL.Text := 'Insert Into Factura Values '+
'( '+IntToStr(NumFact)+','+IntToStr(0)+', '+QuotedStr(Tipo)+', '+QuotedStr(Fecha)+', '+Edit1.Text+', '+QuotedStr(Label19.Caption)+', '+QuotedStr(Edit4.Text)+', '+QuotedStr(Edit5.Text)+', '+QuotedStr(Edit3.Text)+
', ''N'', '+FloatToStr(0.13)+', ''N'', '+FloatToStr(0.00)+', '+QuotedStr(SGTotal.Cells[1,0])+', '+QuotedStr(SGTotal.Cells[1,1])+', '+
QuotedStr(SGTotal.Cells[1,2])+', '+QuotedStr(SGTotal.Cells[1,3])+', '+Edit2.Text+', '+QuotedStr(MntContado)+', '+QuotedStr(MntCheque)+', '+
QuotedStr(MntTarjeta)+', '+QuotedStr(MntOtros)+', '+QuotedStr(Saldo)+', '+QuotedStr(Pagado)+', '+QuotedStr(FloatToStr(TipoCambio))+')'
else
// Insertar en la tabla de Factura si NO tiene ORDEN
QTemp.SQL.Text := 'Insert Into Factura ( CodFactura, Secuencia, Tipo, Fecha, CodCliente, Terminos, OrdenCompra, CodVendedor, Excento, TasaImpuesto, Anulada, MntEnvio, MntSubTotal, MntDescuento, MntImpuesto, MntTotal, '+
'MntContado, MntCheque, MntTarjeta, MntOtros, Saldo, Pagado, TC) Values '+
'( '+IntToStr(NumFact)+', '+IntToStr(0)+', '+QuotedStr(Tipo)+', '+QuotedStr(Fecha)+', '+Edit1.Text+', '+QuotedStr(Label19.Caption)+', '+QuotedStr(Edit4.Text)+', '+QuotedStr(Edit5.Text)+', '+QuotedStr(Edit3.Text)+
', ''N'', '+FloatToStr(0.13)+', ''N'','+FloatToStr(0.00)+', '+QuotedStr(SGTotal.Cells[1,0])+', '+QuotedStr(SGTotal.Cells[1,1])+', '+
QuotedStr(SGTotal.Cells[1,2])+', '+QuotedStr(SGTotal.Cells[1,3])+', '+QuotedStr(MntContado)+', '+QuotedStr(MntCheque)+', '+
QuotedStr(MntTarjeta)+', '+QuotedStr(MntOtros)+', '+QuotedStr(Saldo)+', '+QuotedStr(Pagado)+', '+QuotedStr(FloatToStr(TipoCambio))+')';
QTemp.ExecSQL;
//Insertar en la tabla de FacturaItem
For i := 1 to SgFact.RowCount-1 do
begin
if SGFact.Cells[6,i] = 'Si' then iv:='True)' else iv:='False)';
QTemp.SQL.Text := 'Insert Into FacturaItem Values '+
'( '+IntToStr(numfact)+', '+IntToStr(0)+', '+QuotedStr(Tipo)+', '+IntToStr(i)+', '+QuotedStr(SGFact.Cells[0,i])+', '+QuotedStr(SGFact.Cells[1,i])+', '+QuotedStr(SGFact.Cells[2,i])+', '+QuotedStr(SGFact.Cells[3,i])+', '+
QuotedStr(SGFact.Cells[4,i])+', '+iv;
QTemp.ExecSQL;
end;
//Calcula el siguiente numero de OrdTrans
QTemp.SQL.Text := 'SELECT Max(OrdTrans) AS Expr1 '+
'FROM CxCobrar;';
QTemp.Active := True;
If QTemp.RecordCount = 0 then OrdTrans:=1
else OrdTrans := QTemp.Fields[0].AsInteger+1;
QTemp.Close;
If Tipo = 'F' then
begin
// Insertar en la tabla de CxCobrar
QTemp.SQL.Text := 'Select BalanceTotal From CxCobrar where CodCliente = '+Edit1.Text+
' Order By OrdTrans;';
QTemp.Open;
IF QTemp.RecordCount = 0 then BalanceAnterior := 0
else
begin
QTemp.Last;
BalanceAnterior := QTemp.Fields[0].AsFloat;
end;
QTemp.Close;
BalanceTotal := StrToFloat(Saldo) + BalanceAnterior;
QTemp.SQL.Text := 'Insert Into CxCobrar ( CodTransac, Secuencia, TipoTransac, OrdTrans, FechaTransac, FechaVencimiento, CodCliente, SubTotal, Descuento, Impuesto, Total, Efectivo, Cheque, Tarjeta, Otro, Balance, BalanceTotal, BalanceAnterior, PagosRec, EstadoCuenta )'+
' Values ('+IntToStr(NumFact)+', '+IntToStr(0)+', ''FA'', '+IntToStr(OrdTrans)+', '+QuotedStr(Fecha)+', '+QuotedStr(FechaVence)+', '+Edit1.Text+', '+QuotedStr(SGTotal.Cells[1,0])+', '+QuotedStr(SGTotal.Cells[1,1])+', '+
QuotedStr(SGTotal.Cells[1,2])+', '+QuotedStr(SGTotal.Cells[1,3])+', '+QuotedStr(MntContado)+', '+QuotedStr(MntCheque)+', '+
QuotedStr(MntTarjeta)+', '+QuotedStr(MntOtros)+', '+QuotedStr(Saldo)+', '+QuotedStr(FloatToStr(BalanceTotal))+', '+QuotedStr(FloatToStr(BalanceAnterior))+', '+IntToStr(0)+','+IntToStr(0)+')';
QTemp.ExecSQL;
end;
// Actualizar la tabla de series
For i := 1 to SgFact.RowCount-1 do
begin
if Tipo = 'F' then
QTemp.SQL.Text := 'Update Series Set NumFactura = '+IntToStr(numfact)+', Estado = ''Facturado'' Where '+
'NumSerie = '+SGFact.Cells[2,i]
else
QTemp.SQL.Text := 'Update Series Set NumFactura = '+IntToStr(numfact)+', Estado = ''Consignac'' Where '+
'NumSerie = '+SGFact.Cells[2,i];
QTemp.ExecSQL;
end;
// Actualizar la tabla de OrdenProdItem
For i := 1 to SgFact.RowCount-1 do
begin
if Tipo = 'F' then
QTemp.SQL.Text := 'Update OrdenProdItem Set Estacion = ''Est05'' Where '+
'NumSerie = '+SGFact.Cells[2,i]
else
QTemp.SQL.Text := 'Update OrdenProdItem Set Estacion = ''Est06'' Where '+
'NumSerie = '+SGFact.Cells[2,i];
QTemp.ExecSQL;
end;
// Actualizar la tabla de Articulos
For i := 1 to SgFact.RowCount-1 do
begin
QTemp.SQL.Text := 'Update Articulos Set Disponible = Disponible - '+SGFact.Cells[3,i]+' Where '+
'CodParte = '+QuotedStr(SGFact.Cells[0,i]);
QTemp.ExecSQL;
end;
// Actualizar la tabla de Materiales
For i := 1 to SgFact.RowCount-1 do
begin
QTemp.SQL.Text := 'Update Materiales Set Disponible = Disponible - '+SGFact.Cells[3,i]+' Where '+
'CodMaterial = '+QuotedStr(SGFact.Cells[0,i]);
QTemp.ExecSQL;
end;
// Completa la Transaccion
// DataModule1.AC1.CommitTrans;
// Imprime la factura si el usuario quiere
FImprimirFactura:=TFImprimirFactura.Create(self);
FImprimirFactura.numfact := numfact;
FImprimirFactura.Tipo := Tipo;
try
FImprimirFactura.ShowModal;
finally
FImprimirFactura.Free;
end;
except
on E:Exception do
begin
// DataModule1.AC1.RollbackTrans;
MessageDlg('No fue posible completar la transacción, por favor contacte al administrador',mtError,[mbOK],0);
end;
end;//try
end;
Close;
end;