unit U_OrderSubFH;

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
  TfrmOrderSubFH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBDel: 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;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    v1Column14: TcxGridDBColumn;
    v1Column15: 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 TBAddClick(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBTPClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure v1Column9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function DelData():Boolean;
    procedure OneKeyPostHD(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet);
  public
    { Public declarations }
  end;

var
  frmOrderSubFH: TfrmOrderSubFH;

implementation
uses
  U_DataLink,U_Fun,U_ProductOrderList,U_ZDYHelp;

{$R *.dfm}

procedure TfrmOrderSubFH.FormDestroy(Sender: TObject);
begin
  frmOrderSubFH:=nil;
end;

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

procedure TfrmOrderSubFH.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;
procedure TfrmOrderSubFH.InitGrid();
var
  fsj:String;
begin
  try
    //ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_FHQryList :WSQl');
      fsj:=' and OT.FHDate>='+QuotedStr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime)))
           +' and OT.FHDate<'+QuotedStr(Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1)));
      Parameters.ParamByName('WSQl').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    //ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrderSubFH.FormShow(Sender: TObject);
begin
  ReadCxGrid('发货列表',Tv1,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmOrderSubFH.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 TfrmOrderSubFH.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货列表',Tv1,'指示单管理');
end;

procedure TfrmOrderSubFH.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
end;

procedure TfrmOrderSubFH.TBAddClick(Sender: TObject);
begin
  try
    frmProductOrderList:=TfrmProductOrderList.Create(Application);
    with frmProductOrderList do
    begin
      FFInt:=1;
      frmProductOrderList.TBAdd.Visible:=False;
      frmProductOrderList.TBEdit.Visible:=False;
      frmProductOrderList.TBDel.Visible:=False;
      frmProductOrderList.TBExport.Visible:=False;
      frmProductOrderList.TBPrint.Visible:=False;
      frmProductOrderList.TBTP.Visible:=False;
      frmProductOrderList.cxGrid2.Visible:=False;
      frmProductOrderList.ToolButton1.Visible:=True;
      frmProductOrderList.ToolButton2.Visible:=True;
      frmProductOrderList.ToolButton3.Visible:=True;
      frmProductOrderList.ToolButton4.Visible:=False;
      frmProductOrderList.ToolButton5.Visible:=False;
      frmProductOrderList.CheckBox1.Visible:=False;
      frmProductOrderList.CheckBox2.Visible:=True;
      if ShowModal=1 then
      begin
          with frmProductOrderList.Order_Main do
          begin
            frmProductOrderList.Order_Main.First;
            while not Eof do
            begin
              if frmProductOrderList.Order_Main.FieldByName('SSel').AsBoolean=True then
              begin
                with Self.Order_Main do
                begin
                  Append;
                  Self.Order_Main.FieldByName('SubId').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('SubId').AsString);
                  Self.Order_Main.FieldByName('OrderNoM').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('OrderNo').AsString);
                  Self.Order_Main.FieldByName('PRTCodeNameM').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTCodeName').AsString);
                  Self.Order_Main.FieldByName('PRTSpec').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTSpec').AsString);
                  Self.Order_Main.FieldByName('PRTMF').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTMF').AsString);
                  Self.Order_Main.FieldByName('PRTKZ').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTKZ').AsString);
                  Self.Order_Main.FieldByName('PRTColor').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTColor').AsString);
                  if Trim(frmProductOrderList.Order_Main.fieldbyname('PRTOrderQty').AsString)<>'' then
                    Self.Order_Main.FieldByName('PRTOrderQty').Value:=frmProductOrderList.Order_Main.fieldbyname('PRTOrderQty').AsFloat;
                  Self.Order_Main.FieldByName('OrderUnit').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('OrderUnit').AsString);
                  Self.Order_Main.FieldByName('SSel').Value:=1;
                  if Trim(frmProductOrderList.Order_Main.fieldbyname('PRTQty').AsString)<>'' then
                    Self.Order_Main.FieldByName('PRTQty').Value:=frmProductOrderList.Order_Main.fieldbyname('PRTQty').AsFloat;
                  if Trim(frmProductOrderList.Order_Main.fieldbyname('Qty1').AsString)<>'' then
                    Self.Order_Main.FieldByName('CQty1').Value:=frmProductOrderList.Order_Main.fieldbyname('Qty1').AsFloat;
                  if Trim(frmProductOrderList.Order_Main.fieldbyname('CPKC').AsString)<>'' then
                    Self.Order_Main.FieldByName('CPKC').Value:=frmProductOrderList.Order_Main.fieldbyname('CPKC').AsFloat;
                  if Trim(frmProductOrderList.Order_Main.fieldbyname('CPKCP').AsString)<>'' then
                    Self.Order_Main.FieldByName('CPKCP').Value:=frmProductOrderList.Order_Main.fieldbyname('CPKCP').AsFloat;
                  Self.Order_Main.FieldByName('PRTUnit').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTUnit').AsString);
                end;
              end;
              frmProductOrderList.Order_Main.Next;
            end;
          end;
      end;
    end;
  finally
    frmProductOrderList.Free;
  end;
end;

procedure TfrmOrderSubFH.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomNoName';
      flagname:='客户';
      if ShowModal=1 then
      begin
        Self.Order_Main.Edit;
        Self.Order_Main.FieldByName('CustomNoName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Main.FieldByName('CustomNo').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderSubFH.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,MaxNo:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=false then
  begin
    Application.MessageBox('未选择记录！','提示',0);
    Exit;
  end;
  if  (Order_Main.Locate('SSel',True,[])) and (Order_Main.Locate('FHDate',null,[]))  then
  begin
    Application.MessageBox('发货日期不能为空！','提示',0);
    Exit;
  end;
  if  (Order_Main.Locate('SSel',True,[])) and (Order_Main.Locate('CustomNoName',null,[]))  then
  begin
    Application.MessageBox('客户不能为空！','提示',0);
    Exit;
  end;
  if  (Order_Main.Locate('SSel',True,[])) and (Order_Main.Locate('FHPrice',null,[]))  then
  begin
    Application.MessageBox('发货单价不能为空！','提示',0);
    Exit;
  end;
  if  (Order_Main.Locate('SSel',True,[])) and (Order_Main.Locate('JJUnit',null,[]))  then
  begin
    Application.MessageBox('计价单位不能为空！','提示',0);
    Exit;
  end;
  if  (Order_Main.Locate('SSel',True,[])) and (Order_Main.Locate('FHQty',null,[]))  then
  begin
    Application.MessageBox('发货数量不能为空！','提示',0);
    Exit;
  end;
  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 Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          if Trim(Order_Main.fieldbyname('ToId').AsString)='' then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'To','Order_Sub_To',4,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取发货流水号失败！','提示',0);
              Exit;
            end;
          end  else
          begin
            maxno:=Trim(Order_Main.fieldbyname('ToId').AsString);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Order_Sub_To where ToId='''+Trim(Order_Main.fieldbyname('ToId').AsString)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(Order_Main.fieldbyname('ToId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('ToId').Value:=Trim(maxno);
            FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
            SSetSaveDataCDSNew(ADOQueryCmd,Tv1,Order_Main,'Order_Sub_To',3);
            Post;
          end;
          with Order_Main do
          begin
            Edit;
            FieldByName('ToId').Value:=Trim(maxno);
            Post;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('发货成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('发货失败！','提示',0);
  end;
end;

procedure TfrmOrderSubFH.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Order_Sub_ToSK where ToId='''+Trim(Order_Main.fieldbyname('ToId').AsString)+'''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经收款不能删除发货记录！','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //TBRafresh.Click;
    //TBFind.Click;
    Order_Main.Delete;
  end;
end;

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

procedure TfrmOrderSubFH.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('发货列表',cxGrid1);
end;

procedure TfrmOrderSubFH.v1Column9PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='计价单位';
      if ShowModal=1 then
      begin
        Self.Order_Main.Edit;
        Self.Order_Main.FieldByName('JJUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        //Self.Order_Main.FieldByName('CustomNo').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderSubFH.ToolButton1Click(Sender: TObject);
begin
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=1;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmOrderSubFH.ToolButton2Click(Sender: TObject);
begin
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=0;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmOrderSubFH.OneKeyPostHD(Tv1:TcxGridDBTableView;CDS_Sub:TClientDataSet);
var
  FValue,FFValue,FColumn,FFColumn:String;
begin
  FColumn:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn:=Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue:=Trim(CDS_Sub.fieldbyname(FColumn).AsString);
  if Trim(FFColumn)<>'' then
  begin
    FFValue:=Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if CDS_Sub.FieldByName('Ssel').AsBoolean=True then
      begin
        if FValue='' then
        begin
          CDS_Sub.FieldByName(FColumn).Value:=null;
        end else
        begin
          CDS_Sub.FieldByName(FColumn).Value:=FValue;
        end;
        if Trim(FFColumn)<>'' then
        begin
          if FFValue='' then
          begin
            CDS_Sub.FieldByName(FFColumn).Value:=null;
          end else
          begin
            CDS_Sub.FieldByName(FFColumn).Value:=FFValue;
          end;
        end;
        Post;
      end;
      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmOrderSubFH.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  BegDate.SetFocus;
  OneKeyPostHD(Tv1,Order_Main);  
end;

end.
