Jumat, 22 Agustus 2014

[Delphi XE5] Pembuatan Form Penerimaan Barang (SQL Server)

Lagi Iseng-Iseng Bikin Program Menggunakan Embarcadero Delphi XE5.
Maaf masih nuebie, jadi jangan diketawain ya.. hehehe
Inilah Hasilnya :

Dan waktu di Jalankan hasilnya :


Kalau ada yg berminat ini source codenya:
unit PB;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.DBCtrls, Vcl.ExtCtrls,
  Data.DB, Data.Win.ADODB, Vcl.DBLookup, Vcl.ComCtrls, Data.FMTBcd, Vcl.Grids,
  Data.SqlExpr, Vcl.DBGrids;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    ADODataSet1: TADODataSet;
    DataSource1: TDataSource;
    DBLookupComboBox1: TDBLookupComboBox;
    ADODataSet2: TADODataSet;
    DateTimePicker1: TDateTimePicker;
    Label4: TLabel;
    Edit1: TEdit;
    DBText1: TDBText;
    Edit3: TEdit;
    Button1: TButton;
    ADODataSet3: TADODataSet;
    SG: TStringGrid;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Button2: TButton;
    Button3: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure aturkolom;
    procedure tampil;
    procedure SGKeyPress(Sender: TObject; var Key: Char);
    procedure SGSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
      State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPB: TForm2;
  y,sel : integer;
  sum : real;
  vSQL1, vsql2, vsql3,a,b: string;
implementation

{$R *.dfm}
uses Utama;

procedure TForm2.tampil;
Var i : Integer;
begin
  if ADODataSet3.RecordCount<>0 then
begin
  SG.RowCount:= ADODataSet3.RecordCount+1;
  for i:= 0 to ADODataSet3.RecordCount-1 do
  begin
    inc(y);
    SG.Cells[0,i+1]:=IntToStr(i+1)+'.';
    SG.Cells[1,i+1]:=ADODataSet3.FieldByName('ItemCode').AsString;
    SG.Cells[2,i+1]:=ADODataSet3.FieldByName('namaitem').AsString;
    SG.Cells[3,i+1]:=ADODataSet3.FieldByName('satuan').AsString;
    SG.Cells[4,i+1]:=ADODataSet3.FieldByName('qty').AsString;
    SG.Cells[5,i+1]:=ADODataSet3.FieldByName('harga').AsString;
    SG.Cells[6,i+1]:=ADODataSet3.FieldByName('total').AsString;
    {SG.Cells[6,i+1]:=ADODataSet3.FieldByName('suplier').AsString;}
    ADODataSet3.Next;
  end;
end;
end;

procedure TForm2.aturkolom;
begin
  with SG do
  begin
  ColCount:=9;
  RowCount:=2;
  FixedCols:=1;
  DefaultROwHeight:=18;
  {Options := [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSelect];}
  Options := options+[goediting];

  Cells[0,0]:='No.';
  Cells[1,0]:='Kode Barang';
  Cells[2,0]:='Nama Barang';
  Cells[3,0]:='Satuan';
  Cells[4,0]:='Qty';
  Cells[5,0]:='Harga';
  Cells[6,0]:='Total';
  Cells[4,1] := '0';
  Cells[5,1] := '0';
  Cells[6,1] := '0';

//lebar kolom
  ColWidths[0]:=25;
  ColWidths[1]:=90;
  ColWidths[2]:=300;
  ColWidths[3]:=50;
  ColWidths[4]:=60;
  ColWidths[5]:=100;
  ColWidths[6]:=100;
  ColWidths[7]:=0;
  ColWidths[8]:=0;
  end;
end;

procedure TForm2.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = chr(13) then
  begin
    SG.Row:=1;
    SG.Col:=1;
    sg.setfocus;
  end;
end;

procedure TForm2.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if key = chr(13) then
     edit3.SetFocus;
end;

procedure TForm2.Edit3KeyPress(Sender: TObject; var Key: Char);
var sql : string;
begin
    if key = chr(13) then
    Begin
      sql := 'Select Supplier, NamaSup From apsuppl Where Supplier = '+quotedstr(edit3.Text);
      adodataset1.Active := false;
      AdoDataSet1.CommandText := Sql;
      adodataset1.Active := true;
      edit1.SetFocus;
      if adodataset1.Eof then
         showmessage('Data Tidak Terdaftar')
    End;
end;

procedure TForm2.FormActivate(Sender: TObject);
var Yr, Mn, Dy : Word;
  awal : integer;
  ID,Kd : string;
begin
  y := 1;
  sel := 1;
  aturkolom;
  DecodeDate(Now, Yr, Mn, Dy);
  adodataset2.CommandText := 'select * from no_beli where bulan = '+inttostr(mn)+' and tahun = '+inttostr(yr);
  adodataset2.Active:=true;
  awal := adodataset2.FieldByName('Urut').Asinteger+1;
  kd := inttostr(awal);
  ID := Copy('0000'+kd,length('0000'+kd)-3,4);
  edit2.Text := 'PB'+FormatDateTime('YYMM',Now)+ID;{inttostr(Yr);}
  datetimepicker1.Date:=now;
  edit1.Text:='';
  edit3.Text:='';
  edit7.Text:=FloatToStrF(0, ffNumber, 12, 2);
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
release;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  DBLookupCombobox1.Visible:=False;
end;

procedure TForm2.SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
  State: TGridDrawState);
const _Padding = 2;
var _sg : TStringGrid;
    _text : string;
    _textwidth : integer;
    _cellwidth : integer;
    _xpos, _ypos : integer;
begin
    _sg := Sender as TStringGrid;
    _text := _sg.cells[ACol, ARow];
    _textwidth := _sg.Canvas.TextWidth(_text);
    _cellwidth := rect.Right - rect.Left + 1;
    //

    if ACol = 3 then begin
    //Centered
    _xpos := rect.left + ((_cellwidth - _textwidth) div 2) + _padding;
    end else if (ACol = 4) or (ACol = 5) or (ACol = 6) then begin
    //Right Justify
    _xpos := rect.left + (_cellwidth - _textwidth) - _padding;
    end else begin
    //Left Justify
    _xpos := rect.left + _padding;
    end;

    _ypos := rect.top + _padding;
    _sg.Canvas.TextRect(rect, _xpos, _ypos, _text);
end;

procedure TForm2.SGKeyPress(Sender: TObject; var Key: Char);
var ttl : real;
  i : integer;
begin
  if Key = chr(13) then
  begin
     adodataset3.Active := false;
     ttl := 0;
     if sel > 1 then
     begin
        if sel = 5 then
        begin
          sel := 0;
          edit6.Text:=inttostr(y+1);
          if sg.Cells[1,sg.RowCount-1] <> '' then
          begin
            sg.RowCount:=sg.RowCount+1;
            SG.Cells[4,SG.RowCount-1] := '0';
            SG.Cells[5,SG.RowCount-1] := '0';
            SG.Cells[6,SG.RowCount-1] := '0';
          end;
          inc(y);
        end;
        inc(sel);
     end
     else
     begin
     if sg.Cells[sel,y] <> '' then
        AdoDataSet3.CommandText := '';
        a := sg.Cells[1,y];
        vSQL1:= 'Select ItemCode, NamaItem, Satuan, Harga From IcAlt Where ItemCode = '+QuotedStr(a);
        AdoDataSet3.CommandText := VSql1;
        AdoDataSet3.Active := true;
        if adodataset3.Eof then
           showmessage('Data Tidak Terdaftar')
        else
          begin
            sg.Cells[0,y] := inttostr(y);
            sg.Cells[2,y] := adodataset3.FieldByName('NamaItem').AsString;
            sg.Cells[3,y] := adodataset3.FieldByName('Satuan').AsString;
            sg.Cells[7,y] := adodataset3.FieldByName('Harga').AsString;
            SG.Cells[5,y] := FloatToStrF(adodataset3.FieldByName('Harga').AsFloat, ffNumber, 12, 2);
            inc(sel);
         end;
     end;
     if (SG.Cells[7,y] <> '') and (SG.Cells[4,y] <> '') then
     begin
       ttl := Strtofloat(SG.Cells[7,y]) * strtofloat(SG.Cells[4,y]);
       SG.Cells[6,y] := FloatToStrF(ttl, ffNumber, 12, 2);
       SG.Cells[8,y] := floattostr(ttl);
       Sum := 0;
       for i := 1 to SG.RowCount-1 Do
         Sum := Sum + strtofloat(SG.Cells[8, i]);
         Edit7.Text:=FloatToStrF(SUM, ffNumber, 12, 2);
     end;
     edit4.Text:=inttostr(sel);
     edit5.Text:=inttostr(y);
     SG.Row:=strtoint(edit5.Text);
     SG.Col:=strtoint(edit4.Text);
     SG.SetFocus;
  end;
end;

procedure TForm2.SGSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  sel:=Acol;
  y:=Arow;
end;

end.
Semoga Bermanfaat..

Tidak ada komentar:

Posting Komentar