unit U_OrderSubSel;

interface

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

type
  TfrmOrderSubSel = class(TForm)
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Order_Sub: TClientDataSet;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQuery1: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    PFTPPS: TEdit;
    Label2: TLabel;
    PFTPQty: TEdit;
    Label3: TLabel;
    FactoryName: TBtnEditA;
    PFTPUnit: TComboBox;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label4: TLabel;
    Label5: TLabel;
    PFDlyDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    ClientDataSet1: TClientDataSet;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel2: TPanel;
    Button1: TButton;
    v3Column4: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    PFGenDanPerson: TBtnEditA;
    Panel3: TPanel;
    Label30: TLabel;
    Label29: TLabel;
    Label31: TLabel;
    ClothOneQty: TEdit;
    OneQtyUnit: TComboBox;
    MPRTOneQty: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv3CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PFGenDanPersonBtnClick(Sender: TObject);
    procedure PFTPPSChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FFFMainId:String;
  end;

var
  frmOrderSubSel: TfrmOrderSubSel;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun, U_GYSList, U_GYSSelList;

{$R *.dfm}

procedure TfrmOrderSubSel.FormDestroy(Sender: TObject);
begin
  //frmOrderSubSel:=nil;
end;

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

procedure TfrmOrderSubSel.FormShow(Sender: TObject);
begin
  PFDlyDate.Date:=SGetServerDate(ADOQuery1);
  ReadCxGrid('订单明细',Tv1,'订单明细选择');
  ReadCxGrid('订单安排明细',Tv1,'订单明细选择');

  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main where Mainid='''+Trim(FFFMainId)+'''');
      Open;
    end;
    SCSHDataNew(ADOQuery1,Panel3,2);

    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,APFlag=Cast(0 as bit) from JYOrder_Sub A Where MainId='''+Trim(FFFMainId)+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_Sub);
    SInitCDSData20(ADOQuery1,Order_Sub);
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.PRTHX,PRTColor=isnull(B.PRTColorNo,'''')+'' ''+isnull(B.PRTColorEng,'''')+'' ''+isnull(B.PRTColor,''''), ');
      sql.Add(' B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A');
      sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
      sql.Add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet1);
    SInitCDSData20(ADOQuery1,ClientDataSet1);
  finally
    ADOQuery1.EnableControls;
  end;
  PFTPUnit.ItemIndex:=OneQtyUnit.ItemIndex;
end;

procedure TfrmOrderSubSel.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid('订单明细',Tv1,'订单明细选择');
  WriteCxGrid('订单安排明细',Tv1,'订单明细选择');
  Close;
end;

procedure TfrmOrderSubSel.ToolButton1Click(Sender: TObject);
begin
  {if Order_Sub.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end; }
  ModalResult:=1;
end;

procedure TfrmOrderSubSel.ToolButton2Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,True);
end;

procedure TfrmOrderSubSel.ToolButton3Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,False);
end;

procedure TfrmOrderSubSel.FactoryNameBtnClick(Sender: TObject);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        FactoryName.TxtCode:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
        FactoryName.Text:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmOrderSubSel.Button1Click(Sender: TObject);
var
  FReal:Double;
begin
  if Order_Sub.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Trim(FactoryName.Text)='' then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if Trim(PFGenDanPerson.Text)='' then
  begin
    Application.MessageBox('跟单员不能为空!','提示',0);
    Exit;
  end;
  if Trim(PFTPPS.Text)='' then
  begin
    Application.MessageBox('安排匹数不能为空!','提示',0);
    Exit;
  end else
  if TryStrToFloat(PFTPPS.Text,FReal)=False then
  begin
    Application.MessageBox('安排匹数非法数字!','提示',0);
    Exit;
  end;
  if Trim(PFTPQty.Text)='' then
  begin
    Application.MessageBox('安排数量不能为空!','提示',0);
    Exit;
  end else
  if TryStrToFloat(PFTPQty.Text,FReal)=False then
  begin
    Application.MessageBox('安排数量非法数字!','提示',0);
    Exit;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      if Order_Sub.FieldByName('SSel').AsBoolean=true then
      begin

        with ClientDataSet1 do
        begin
          Append;
          FieldByName('SubId').Value:=Order_Sub.fieldbyname('SubId').Value;
          FieldByName('PRTHX').Value:=Order_Sub.fieldbyname('PRTHX').Value;
          FieldByName('PRTColor').Value:=Trim(Order_Sub.fieldbyname('PRTColorNo').AsString)+' '+
                                         Trim(Order_Sub.fieldbyname('PRTColorEng').AsString)+' '+
                                         Trim(Order_Sub.fieldbyname('PRTColor').AsString);
          FieldByName('FactoryName').Value:=Trim(FactoryName.Text);
          FieldByName('FactoryNo').Value:=Trim(FactoryName.TxtCode);
          FieldByName('PFGenDanPerson').Value:=Trim(PFGenDanPerson.Text);
          FieldByName('PFDlyDate').Value:=PFDlyDate.Date;
          FieldByName('PFTPPS').Value:=Trim(PFTPPS.Text);
          FieldByName('PFTPQty').Value:=Trim(PFTPQty.Text);
          FieldByName('PFTPUnit').Value:=Trim(PFTPUnit.Text);
          FieldByName('PRTOrdQty').Value:=Order_Sub.fieldbyname('PRTOrdQty').Value;
          FieldByName('OrderUnit').Value:=Order_Sub.fieldbyname('OrderUnit').Value;
          Post;
        end;
        Edit;
        FieldByName('APFlag').Value:=True;
        FieldByName('SSel').Value:=False;
        Post;
      end;
      Next;
    end;
  end;
  Order_Sub.EnableControls;
end;

procedure TfrmOrderSubSel.Tv3CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet1.IsEmpty then Exit;
  ClientDataSet1.Delete;
end;

procedure TfrmOrderSubSel.PFGenDanPersonBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PFGenDanPerson';
      flagname:='跟单员';
      fnote:=True;
      V1Note.Caption:='电话';
      if ShowModal=1 then
      begin
        PFGenDanPerson.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderSubSel.PFTPPSChange(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(PFTPPS.Text)<>'' then
  begin
    if TryStrToFloat(Trim(PFTPPS.Text),FReal)=False then Exit;
    if Trim(ClothOneQty.Text)<>'' then
    begin
      PFTPQty.Text:=FloatToStr(StrToFloat(PFTPPS.Text)*StrToFloat(ClothOneQty.Text));
    end;
  end;
end;

end.
