unit U_OrderSubFHOK;

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
  TfrmOrderSubFHOK = 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;
    v1KHConNo: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Label4: TLabel;
    CustomNoName: TEdit;
    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);
  private
    { Private declarations }
    procedure InitGrid();
    function DelData():Boolean;
  public
    { Public declarations }
  end;

var
  frmOrderSubFHOK: TfrmOrderSubFHOK;

implementation
uses
  U_DataLink,U_Fun,U_ProductOrderList,U_ZDYHelp;

{$R *.dfm}

procedure TfrmOrderSubFHOK.FormDestroy(Sender: TObject);
begin
  frmOrderSubFHOK:=nil;
end;

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

procedure TfrmOrderSubFHOK.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmOrderSubFHOK.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_SKQryList :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 TfrmOrderSubFHOK.FormShow(Sender: TObject);
begin
  ReadCxGrid('收款列表',Tv1,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

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

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

procedure TfrmOrderSubFHOK.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;

      if ShowModal=1 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('PRTColor').AsString);
          Self.Order_Main.FieldByName('PRTKZ').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);

        end;
      end;
    end;
  finally
    frmProductOrderList.Free;
  end;
end;

procedure TfrmOrderSubFHOK.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 TfrmOrderSubFHOK.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,MaxNo,FHMoney,SKMoney,QKMoney: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('SKId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'SK','Order_Sub_ToSk',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取发货流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(Order_Main.fieldbyname('SKId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Order_Sub_ToSk where SKId='''+Trim(Order_Main.fieldbyname('SKId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(Order_Main.fieldbyname('SKId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('SKId').Value:=Trim(maxno);
          FieldByName('ToId').Value:=Trim(Order_Main.fieldbyname('ToId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv1,Order_Main,'Order_Sub_ToSk',3);
          Post;
        end;
        //Next;
      //end;

      FHMoney:=Trim(Order_Main.fieldbyname('FHMoney').AsString);
      SKMoney:=Trim(Order_Main.fieldbyname('SKMoney').AsString);
      if Trim(FHMoney)='' then FHMoney:='0.0';
      if Trim(SKMoney)='' then SKMoney:='0.0';
      QKMoney:=FloatToStr(StrToFloat(FHMoney)-StrToFloat(SKMoney));
      with Order_Main do
      begin
        Edit;
        FieldByName('SKId').Value:=Trim(maxno);
        FieldByName('QKMoney').Value:=Trim(QKMoney);
        Post;
      end;
    //end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('收款成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('收款失败！','提示',0);
  end;
end;

procedure TfrmOrderSubFHOK.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //TBRafresh.Click;
    //TBFind.Click;
    Order_Main.Delete;
  end;
end;

function TfrmOrderSubFHOK.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 TfrmOrderSubFHOK.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('收款列表',cxGrid1);
end;

procedure TfrmOrderSubFHOK.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;

end.
