unit U_OrderStatus;

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, cxCheckBox;

type
  TfrmOrderStatus = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    TBTP: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNo: TEdit;
    WFBCodeName: TEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column3: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1OrderDate: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure WFBCodeNameChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure v1Column16PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    function DelData():Boolean;
  public
    { Public declarations }
  end;

var
  frmOrderStatus: TfrmOrderStatus;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmOrderStatus.FormDestroy(Sender: TObject);
begin
  frmOrderStatus:=nil;
end;

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

procedure TfrmOrderStatus.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmOrderStatus.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select Customnoname=C.ZDYName,A.MainID As FMainId ,');
      sql.Add(' case when A.Chker<>'''' then cast(1 as bit) else cast(0 as bit) end as OSFlag2,');
      sql.Add(' case when B.OSDate1 is null then BegSCDate else B.OSDate1 end as OSDate1,');
      sql.Add(' case when B.OSDate3 is null then ZGDate else B.OSDate3 end as OSDate3,');
      sql.Add(' case when B.OSDate6 is null then ETDDate else B.OSDate6 end as OSDate6,');
      sql.Add(' case when B.OSDate7 is null then ETADate else B.OSDate7 end as OSDate7,A.*,B.* ');
      SQL.Add('from WFBOrder_Main A left join WFBOrder_Status B on A.MainId=B.MainId');
      sql.Add('left join KH_ZDY C on A.Customno=C.ZDYNO and C.Type=''CustomNoName'' ');
      sql.Add('where A.OrderDate>=:begdate and A.OrderDate<: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 TfrmOrderStatus.FormShow(Sender: TObject);
begin
  ReadCxGrid('状态列表F',Tv1,'无纺布订单');
  InitGrid();
  if Trim(DParameters1)='查看' then
  begin
    TBTP.Visible:=False;
    Tv1.OptionsSelection.CellSelect:=False;
  end;
end;

procedure TfrmOrderStatus.WFBCodeNameChange(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 TfrmOrderStatus.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('状态列表F',Tv1,'无纺布订单');
end;

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

procedure TfrmOrderStatus.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('OSId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'OS','WFBOrder_Status',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取库存流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(Order_Main.fieldbyname('OSId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFBOrder_Status where OSId='''+Trim(Order_Main.fieldbyname('OSId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(Order_Main.fieldbyname('OSId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('OSId').Value:=Trim(maxno);
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('FMainId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv1,Order_Main,'Order_Sub_To',2);
          if Trim(Order_Main.fieldbyname('OSID').AsString)<>'' then
          begin
            FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
            FieldByName('Editer').Value:=Trim(DName);
          end else
          begin
            FieldByName('Filltime').Value:=SGetServerDateTime(ADOQueryTemp);
            FieldByName('Filler').Value:=Trim(DName);
          end;
          Post;
        end;
        //Next;
      //end;
      with Order_Main do
      begin
        Edit;
        FieldByName('OSId').Value:=Trim(maxno);
        Post;
      end;
    //end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('更新成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('更新失败！','提示',0);
  end;
end;

function TfrmOrderStatus.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 TfrmOrderStatus.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('订单状态列表',cxGrid1);
end;

procedure TfrmOrderStatus.v1Column16PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='hdmc';
      flagname:='货代名称';
      if ShowModal=1 then
      begin
        Order_Main.Edit;
        Order_Main.FieldByName('OSStr1').Value:=ClientDataSet1.fieldbyname('ZDYName').AsString;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
