unit uGenerarPedidosCliUtils; interface uses Windows, SysUtils, Classes, pngimage, JSDialog, uBizPresupuestosCliente, uBizPedidosCliente; type TdmGenerarPedidosCli = class(TDataModule) JsListaPedidosGenerados: TJSDialog; end; function GenerarPedidoCliPre(const IDPresupuesto : Integer) : Boolean; overload; function GenerarPedidoCliPre(APresupuesto : IBizPresupuestoCliente) : Boolean; overload; function VerProcesoCompletoDeLosPedidosDelPresupuesto(APresupuesto: Integer):Boolean; implementation {$R *.dfm} uses uDialogUtils, uBizDetallesPresupuestoCliente, uBizDetallesPedidoCliente, uPresupuestosClienteController, uPedidosClienteController, uClientesController, uDetallesPedidoClienteController, uControllerDetallesBase, uBizContactos, schPresupuestosClienteClient_Intf, schPedidosClienteClient_Intf; var dmGenerarPedidosCli: TdmGenerarPedidosCli; APresupuestosClienteController : IPresupuestosClienteController; APedidosClienteController : IPedidosClienteController; ADetallesPedidosCliController : IDetallesPedidoClienteController; AClientesController : IClientesController; { Métodos auxiliares } procedure CopiarPresupuestoAPedido(APresupuesto: IBizPresupuestoCliente; APedido : IBizPedidoCliente); begin if not Assigned(APedido) then raise Exception.Create ('Albarán no asignado (CopiarPresupuestoAPedido)'); if not Assigned(APresupuesto) then raise Exception.Create ('Pedido no asignado (CopiarPresupuestoAPedido)'); if not APresupuesto.DataTable.Active then APresupuesto.DataTable.Active := True; // El albarán tiene que venir ya abierto y posicionado donde hay que copiar APedido.ID_PRESUPUESTO := APresupuesto.ID; APedido.REFERENCIA_CLIENTE := APresupuesto.REFERENCIA_CLIENTE; APedido.TIPO_PEDIDO := APresupuesto.TIPO_PRESUPUESTO; APedido.IMPORTE_NETO := APresupuesto.IMPORTE_NETO; APedido.IMPORTE_PORTE := APresupuesto.IMPORTE_PORTE; APedido.DESCUENTO := APresupuesto.DESCUENTO; APedido.IMPORTE_DESCUENTO := APresupuesto.IMPORTE_DESCUENTO; APedido.BASE_IMPONIBLE := APresupuesto.BASE_IMPONIBLE; APedido.IVA := APresupuesto.IVA; APedido.IMPORTE_IVA := APresupuesto.IMPORTE_IVA; APedido.IMPORTE_TOTAL := APresupuesto.IMPORTE_TOTAL; APedido.DataTable.FieldByName(fld_PedidosClienteOBSERVACIONES).AsVariant := APresupuesto.DataTable.FieldByName(fld_PresupuestosClienteOBSERVACIONES).AsVariant; APedido.ID_FORMA_PAGO := APresupuesto.ID_FORMA_PAGO; APresupuestosClienteController.RecuperarCliente(APresupuesto); APedido.Cliente := APresupuesto.Cliente; //Siempre cogeremos la primera direcci¢n del contacto para el envio APedido.CALLE := APedido.Cliente.Direcciones.CALLE; APedido.CODIGO_POSTAL := APedido.Cliente.Direcciones.CODIGO_POSTAL; APedido.POBLACION := APedido.Cliente.Direcciones.POBLACION; APedido.PROVINCIA := APedido.Cliente.Direcciones.PROVINCIA; APedido.PERSONA_CONTACTO := APedido.Cliente.Direcciones.PERSONA_CONTACTO; APedido.TELEFONO := APedido.Cliente.Direcciones.TELEFONO; end; procedure CopiarArticulosPresupuesto(AOrigen: IBizDetallesPresupuestoCliente; ADestino : IBizDetallesPedidoCliente); var i : integer; ADetallesController : IDetallesPedidoClienteController; begin if not Assigned(AOrigen) then raise Exception.Create ('Origen no asignado (CopiarArticulosPresupuesto)'); if not Assigned(ADestino) then raise Exception.Create ('Destino no asignado (CopiarArticulosPresupuesto)'); if not AOrigen.DataTable.Active then AOrigen.DataTable.Active := True; if not ADestino.DataTable.Active then ADestino.DataTable.Active := True; ADetallesController := TDetallesPedidoClienteController.Create; try //OJO IMPORTANTE //Siempre que vayamos a trabajar con los detalles debemos hacer un beginupdate de los mismos y un endupdate para //obligarle siempre a recalcular los detalles una sola vez ADetallesController.BeginUpdate(ADestino); AOrigen.DataTable.First; for i := 0 to AOrigen.DataTable.RecordCount - 1 do begin ADetallesController.Add(ADestino, TIPO_DETALLE_CONCEPTO); ADestino.Edit; ADestino.REFERENCIA := AOrigen.REFERENCIA; if AOrigen.ID_ARTICULO > 0 then ADestino.ID_ARTICULO := AOrigen.ID_ARTICULO; ADestino.CONCEPTO := AOrigen.CONCEPTO; ADestino.CANTIDAD := AOrigen.CANTIDAD; ADestino.IMPORTE_UNIDAD := AOrigen.IMPORTE_UNIDAD; ADestino.IMPORTE_TOTAL := AOrigen.IMPORTE_TOTAL; ADestino.DESCUENTO := AOrigen.DESCUENTO; ADestino.IMPORTE_PORTE := AOrigen.IMPORTE_PORTE; ADestino.VISIBLE := AOrigen.VISIBLE; ADestino.REFERENCIA_PROVEEDOR := AOrigen.REFERENCIA_PROVEEDOR; ADestino.Post; AOrigen.Next; end; finally ADetallesController.EndUpdate(ADestino); ADetallesController := NIL; end; end; procedure Inicializar; begin dmGenerarPedidosCli := TdmGenerarPedidosCli.Create(nil); APresupuestosClienteController := TPresupuestosClienteController.Create; APedidosClienteController := TPedidosClienteController.Create; ADetallesPedidosCliController := TDetallesPedidoClienteController.Create; AClientesController := TClientesController.Create; end; procedure Finalizar; begin FreeAndNIL(dmGenerarPedidosCli); APresupuestosClienteController := nil; APedidosClienteController := nil; ADetallesPedidosCliController := nil; AClientesController := nil; end; function GenerarPedidoCliPre(const IDPresupuesto : Integer) : Boolean; overload; var APresupuesto : IBizPresupuestoCliente; begin Result := False; try if not Assigned(APresupuestosClienteController) then Inicializar; APresupuesto := APresupuestosClienteController.Buscar(IDPresupuesto); if Assigned(APresupuesto) then Result := GenerarPedidoCliPre(APresupuesto); if Assigned(APresupuestosClienteController) then Finalizar; finally APresupuesto := NIL; end; end; function GenerarPedidoCliPre(APresupuesto : IBizPresupuestoCliente) : Boolean; overload; var i: Integer; PedidosNuevos: IBizPedidoCliente; begin Result := False; if not Assigned(APresupuesto) then raise Exception.Create('Presupuesto de cliente no asignado (GenerarPedidoCliPre)'); if not APresupuesto.DataTable.Active then APresupuesto.DataTable.Active := True; if not Assigned(APresupuestosClienteController) then Inicializar; try PedidosNuevos := APedidosClienteController.Nuevo; //(False); PedidosNuevos._Cliente := NIL; CopiarPresupuestoAPedido(APresupuesto, PedidosNuevos); CopiarArticulosPresupuesto(APresupuesto.Detalles, PedidosNuevos.Detalles); // PedidosNuevos.CalcularImporteTotal; APedidosClienteController.Guardar(PedidosNuevos); if PedidosNuevos.DataTable.RecordCount = 1 then ShowInfoMessage('El pedido se ha dado de alta con el código ' + PedidosNuevos.REFERENCIA); { else begin with dmGenerarAlbaranesCli.JsListaAlbaranesGenerados.Content do begin Clear; AlbaranesNuevos.DataTable.Last; for i := 0 to AlbaranesNuevos.DataTable.RecordCount - 1 do begin if Length(AlbaranesNuevos.REFERENCIA) > 0 then Add(AlbaranesNuevos.REFERENCIA + ': ' + AlbaranesNuevos.NOMBRE); AlbaranesNuevos.DataTable.Prior; end; end; dmGenerarAlbaranesCli.JsListaAlbaranesGenerados.Execute; end; Result := True; end; } finally PedidosNuevos := NIL; if Assigned(APresupuestosClienteController) then Finalizar; end; end; { function ElegirPedidoYGenerarPedidoCliPre(var IDAlbaran: Integer) : Boolean; overload; var APedido : IBizPedidoCliente; AAlbaran : IBizAlbaranCliente; begin Result := False; IDAlbaran := -1; AAlbaran := NIL; try if not Assigned(APedidosClienteController) then Inicializar; APedido := APedidosClienteController.ElegirPedidos(APedidosClienteController.BuscarPendientes, 'Elija el pedido de cliente que desea utilizar para dar de alta el albarán de cliente.' , False); if Assigned(APedido) then begin Result := GenerarPedidoCliPre(APedido, AAlbaran); if Result then IDAlbaran := AAlbaran.ID; end; finally if Assigned(APedidosClienteController) then Finalizar; end; end; } {function GenerarAlbaranes(AAlbaranes : IBizAlbaraneCliente; AListaPresupuestos : IBizPresupuestoCliente): Boolean; var AAlbaranActual : IBizAlbaranCliente; APresupuestosController : IPresupuestosClienteController; I: Integer; bEnEdicion : Boolean; begin // ATENCIÓN!!! AFacturas tiene que estar vacio para no pisar facturas // ya generadas. { if not Assigned(AAlbaranes) then raise Exception.Create ('Albaran no asignado (Anadir)'); if not Assigned(AListaPresupuestos) then raise Exception.Create ('Presupuestos no asignados (Anadir)'); if not AAlbaranes.DataTable.Active then AAlbaranes.DataTable.Active := True; if not AListaPresupuestos.DataTable.Active then AListaPresupuestos.DataTable.Active := True; // ShowHourglassCursor; // Application.ProcessMessages; APresupuestosController := TPresupuestosClienteController.Create; try // Ordenar por fecha de albaran AListaPresupuestos.DataTable.Sort([fld_PresupuestosClienteFECHA_PRESUPUESTO], [uDADataTable.sdAscending]); AListaPresupuestos.First; for I := 0 to AListaPresupuestos.DataTable.RecordCount - 1 do begin AListaPresupuestos._Cliente := NIL; AAlbaranActual := NIL; // Busco si hay alguna factura ya hecha de ese Proveedor AAlbaranes.DataTable.First; if AAlbaranes.DataTable.Locate(fld_AlbaranesClienteID_Cliente, AListaPresupuestos.ID_Cliente, []) then begin AAlbaranActual := AAlbaranes; AAlbaranesClienteController.RecuperarProveedor(AAlbaranActual); AAlbaranActual.Proveedor.DataTable.Active := True; end else begin // No hay factura de ese Proveedor. Creo una nueva AAlbaranesClienteController.Anadir(AAlbaranes); APresupuestosController.RecuperarProveedor(AListaPresupuestos); AAlbaranes.Proveedor := AListaPresupuestos.Proveedor; //Siempre asignaremos por defecto la referencia de factura de proveedor que tenga el primer albaran del proveedor bEnEdicion := (AAlbaranes.DataTable.State in dsEditModes); if not bEnEdicion then AAlbaranes.Edit; AAlbaranes.REFERENCIA_PROVEEDOR := AListaPresupuestos.REF_FACTURA_PROV; AAlbaranes.Post; if bEnEdicion then AAlbaranes.Edit; //Si el albaran es de tipo devolución hacemos la factura de tipo abono if (AListaPresupuestos.TIPO = CTE_TIPO_ALBARAN_DEV) then begin bEnEdicion := (AAlbaranes.DataTable.State in dsEditModes); if not bEnEdicion then AAlbaranes.Edit; AAlbaranes.TIPO := CTE_TIPO_ABONO; AAlbaranes.Post; if bEnEdicion then AAlbaranes.Edit; end; AAlbaranActual := AAlbaranes; end; // Ya tengo la factura. Le añado los conceptos del albarán AAlbaranActual.Detalles.DataTable.Last; // Añado el título AAlbaranesClienteController.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_TITULO); // Self.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_TITULO); with AAlbaranActual.Detalles do begin Edit; CONCEPTO := 'Albarán ' + AListaPresupuestos.REFERENCIA + ' del ' + DateToStr(AListaPresupuestos.FECHA_ALBARAN); Post; end; // Añado el contenido del albarán CopiarArticulosPresupuesto(AListaPresupuestos.Detalles, AAlbaranActual.Detalles); {En los albaranes a proveedor el porte es a nivel del artículo Self.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_CONCEPTO); with AAlbaranActual.Detalles do begin Edit; CONCEPTO := 'Porte del albarán'; CANTIDAD := 1; IMPORTE_UNIDAD := AListaPresupuestos.IMPORTE_PORTE; Post; end; } { // Añado el resumen AAlbaranesClienteController.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_SUBTOTAL); //Self.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_SUBTOTAL); with AAlbaranActual.Detalles do begin Edit; CONCEPTO := 'Total del albarán ' + AListaPresupuestos.REFERENCIA; Post; end; // Añado una línea en blanco AAlbaranesClienteController.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_CONCEPTO); // Self.DetallesController.Add(AAlbaranActual.Detalles, TIPO_DETALLE_CONCEPTO); with AAlbaranActual.Detalles do begin Edit; CONCEPTO := ''; Post; end; // Guardo la factura que acabo de generar o editar AAlbaranActual.CalcularImporteTotal; AAlbaranesClienteController.Guardar(AAlbaranActual); // Self.Guardar(AAlbaranActual); // Asocio la factura con el albarán AListaPresupuestos.Edit; AListaPresupuestos.ID_FACTURA := AAlbaranActual.ID; AListaPresupuestos.Post; APresupuestosController.Guardar(AListaPresupuestos); AListaPresupuestos.Next; end; Result := True; finally APresupuestosController := NIL; // HideHourglassCursor end; end; } function VerProcesoCompletoDeLosPedidosDelPresupuesto(APresupuesto: Integer):Boolean; begin if not Assigned(APedidosClienteController) then Inicializar; Result := APedidosClienteController.VerProcesoCompleto(APresupuesto); if Assigned(APedidosClienteController) then Finalizar; end; end.