unit U_OrderSubKC;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ExtCtrls,
  ToolWin, cxCalendar, cxButtonEdit;

type
  TfrmOrderSubKC = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    TBTP: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNoM: TEdit;
    PRTCodeNameM: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure PRTCodeNameMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function DelData():Boolean;
  public
    { Public declarations }
  end;

var
  frmOrderSubKC: TfrmOrderSubKC;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmOrderSubKC.FormDestroy(Sender: TObject);
begin
  frmOrderSubKC:=nil;
end;

procedure TfrmOrderSubKC.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmOrderSubKC.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmOrderSubKC.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_PRTKCQryList :Begdate,:EndDate ');
      Parameters.ParamByName('Begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('EndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrderSubKC.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品列表',Tv1,'指示单管理');
  InitGrid();
end;

procedure TfrmOrderSubKC.PRTCodeNameMChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmOrderSubKC.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('成品列表',Tv1,'指示单管理');
end;

procedure TfrmOrderSubKC.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
end;

procedure TfrmOrderSubKC.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,MaxNo:String;
begin
  if Order_Main.IsEmpty then Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
   // with Order_Main do
    //begin
      //First;
     // while not Eof do
      //begin
        {if Trim(Order_Main.fieldbyname('FHQty').AsString)='' then
          FQty:='0.0'
        else
          FQty:=Trim(Order_Main.fieldbyname('FHQty').AsString);
        if Trim(Order_Main.fieldbyname('Qty1').AsString)='' then
          FQty1:='0.0'
        else
          FQty1:=Trim(Order_Main.fieldbyname('Qty1').AsString); }
        if Trim(Order_Main.fieldbyname('KCId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'KC','Order_Sub_To',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取库存流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(Order_Main.fieldbyname('KCId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Order_Sub_KC where KCId='''+Trim(Order_Main.fieldbyname('KCId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(Order_Main.fieldbyname('KCId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('KCId').Value:=Trim(maxno);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv1,Order_Main,'Order_Sub_To',3);
          Post;
        end;
        //Next;
      //end;
      with Order_Main do
      begin
        Edit;
        FieldByName('KCId').Value:=Trim(maxno);
        Post;
      end;
    //end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('更新成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('更新失败！','提示',0);
  end;
end;

function TfrmOrderSubKC.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Order_Sub_KC where KCId='''+Trim(Order_Main.fieldbyname('KCId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmOrderSubKC.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('成品库存列表',cxGrid1);
end;

end.
