unit U_OrderAttachment;

interface

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

type
  TfrmOrderAttachment = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Customnoname: TEdit;
    Label2: TLabel;
    SCXName: TEdit;
    Label3: TLabel;
    OrderCode: TEdit;
    Label4: TLabel;
    OrderNo: TEdit;
    Label5: TLabel;
    Label11: TLabel;
    ADefStr3: TEdit;
    Label12: TLabel;
    ADefStr4: TEdit;
    Label13: TLabel;
    Label14: TLabel;
    ADefStr5: TEdit;
    Label15: TLabel;
    ADefStr6: TEdit;
    Label16: TLabel;
    ADefStr7: TEdit;
    Label17: TLabel;
    ADefStr8: TEdit;
    Label18: TLabel;
    ADefStr9: TEdit;
    Label19: TLabel;
    cxGrid5: TcxGrid;
    TvSub: TcxGridDBTableView;
    vSubColumn3: TcxGridDBColumn;
    vSubColumn4: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TvSubColumn1: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Label20: TLabel;
    Label21: TLabel;
    QFDate: TDateTimePicker;
    QFPerson: TEdit;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    vSubColumn1: TcxGridDBColumn;
    DataSource1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    vSubColumn5: TcxGridDBColumn;
    vSubColumn6: TcxGridDBColumn;
    vSubColumn7: TcxGridDBColumn;
    BegRKDate: TDateTimePicker;
    cxGridPopupMenu1: TcxGridPopupMenu;
    vSubColumn8: TcxGridDBColumn;
    vSubColumn9: TcxGridDBColumn;
    Label6: TLabel;
    ADefStr10: TRichEdit;
    ADOQuery2: TADOQuery;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label22: TLabel;
    Label24: TLabel;
    procedure ADefStr3DblClick(Sender: TObject);
    procedure ADefStr4DblClick(Sender: TObject);
    procedure ADefStr5DblClick(Sender: TObject);
    procedure ADefStr6DblClick(Sender: TObject);
    procedure ADefStr9DblClick(Sender: TObject);
    procedure ADefStr7DblClick(Sender: TObject);
    procedure ADefStr8DblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure Label8Click(Sender: TObject);
  private
    { Private declarations }
    procedure SaveJiangYe();
  public

    { Public declarations }
    FAMainId:string;
  end;

var
  frmOrderAttachment: TfrmOrderAttachment;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_GetPGJBInList,U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmOrderAttachment.ADefStr3DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPBZ';
      flagname:='包装';
      if ShowModal=1 then
      begin
        Self.ADefStr3.Text:=Self.ADefStr3.Text+Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr4DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPMT';
      flagname:='唛头';
      if ShowModal=1 then
      begin
        Self.ADefStr4.Text:=Self.ADefStr4.Text+Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr5DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPBM';
      flagname:='布面';
      if ShowModal=1 then
      begin
        Self.ADefStr5.Text:=Self.ADefStr5.Text+Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr6DblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPFeel';
      flagname:='手感';
      if ShowModal=1 then
      begin
        Self.ADefStr6.Text:=Trim(ClientDataSet1.fieldbyname('zdyname').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr9DblClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPFX';
      flagname:='分切';
      if ShowModal=1 then
      begin
        Self.ADefStr9.Text:=Self.ADefStr9.Text+Trim(ReturnStr);
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr7DblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPXS';
      flagname:='吸水';
      if ShowModal=1 then
      begin
        Self.ADefStr7.Text:=Trim(ClientDataSet1.fieldbyname('zdyname').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderAttachment.ADefStr8DblClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPSLD';
      flagname:='色牢度';
      if ShowModal=1 then
      begin
        Self.ADefStr8.Text:=Trim(ClientDataSet1.fieldbyname('zdyname').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderAttachment.FormShow(Sender: TObject);
begin
  ReadCxGrid('无纺布计划单1',TvSub,'指示单管理');
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_WFBOrder_ListAtt :begdate,:endate,:MainId');
    ADOQuery1.Parameters.ParamByName('MainId').Value:=Trim(FAMainId);
    ADOQuery1.Parameters.ParamByName('begdate').Value:='';
    ADOQuery1.Parameters.ParamByName('endate').Value:='';
    Open;
  end;
 // SCreateCDS20(ADOQuery1,Order_Sub);
  //SInitCDSData20(ADOQuery1,Order_Sub);

  SCSHDataWTag(ADOQuery1,Panel1);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFBOrder_Main_Attachment where MainId='''+Trim(FAMainId)+'''');
    Open;
  end;
  if ADOQuery1.IsEmpty then
  begin
    Label7.Caption:='未生成';
    Label7.Font.Color:=clRed;
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('select Top 1* from WFBOrder_Main_Attachment ');
      Open;
    end;
    if ADOQuery1.IsEmpty=False then
    begin
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from WFBOrder_Main_Attachment order by FillTime desc ');
        Open;
      end;
      with ADOQuery1 do
      begin
        ADefStr3.Text:=Trim(fieldbyname('ADefStr3').AsString);
        ADefStr4.Text:=Trim(fieldbyname('ADefStr4').AsString);
        ADefStr5.Text:=Trim(fieldbyname('ADefStr5').AsString);
        ADefStr6.Text:=Trim(fieldbyname('ADefStr6').AsString);
        ADefStr7.Text:=Trim(fieldbyname('ADefStr7').AsString);
        ADefStr8.Text:=Trim(fieldbyname('ADefStr8').AsString);
        ADefStr9.Text:=Trim(fieldbyname('ADefStr9').AsString);

      end;
    end;
  end else
  begin
    Label7.Caption:='已生成';
    Label7.Font.Color:=clBlue;
    if Trim(ADOQuery1.fieldbyname('ADefStr11').AsString)='clBlue' then
    begin
      ADefStr10.Font.Color:=clBlue;
    end else
    if Trim(ADOQuery1.fieldbyname('ADefStr11').AsString)='clRed' then
    begin
      ADefStr10.Font.Color:=clRed;
    end else
    if Trim(ADOQuery1.fieldbyname('ADefStr11').AsString)='clOlive' then
    begin
      ADefStr10.Font.Color:=clOlive;
    end else
    if Trim(ADOQuery1.fieldbyname('ADefStr11').AsString)='clFuchsia' then
    begin
      ADefStr10.Font.Color:=clFuchsia;
    end else
    begin
      ADefStr10.Font.Color:=clBlack;
    end;
  end;
  with ADOCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from WFBOrder_Main_Attachment where MainId='''+Trim(FAMainId)+'''');
    Open;
  end;
  if ADOCmd.IsEmpty then
  begin
   QFPerson.Text:=Trim(DName);
   QFDate.DateTime:=SGetServerDate(ADOQuery1);
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select A.*,Case when OrdUnit=''㎏'' then A.SOrdQty');
    SQL.Add('     else A.SOrdQty*A.SWFBKZ*1.00/1000 end as OrdQty,');
    sql.Add(' Case when B.OrderType=''制品'' then A.SWFBFK2 else A.SWFBFK end as FSWFBFK, ');
    sql.add('YLPBStr=dbo.F_Get_WFBOrder_SubStr(A.SubId,''YLPB'')');
    SQL.Add('from WFBOrder_Sub A');
    SQL.Add('inner join WFBOrder_Main B on A.MainId=B.MainId');
    sql.Add(' where A.MainId='''+Trim(FAMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_Sub);
  SInitCDSData20(ADOQuery1,CDS_Sub);
end;

procedure TfrmOrderAttachment.TBSaveClick(Sender: TObject);
var
  maxnno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from WFBOrder_Main_Attachment where MainId='''+Trim(FAMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQuery1);
      end;
      FieldByName('MainId').value:=Trim(FAMainId);
      FieldByName('ADefStr11').Value:=Trim(ADefStr10.Hint);
      SSetsavedata(ADOCmd,'WFBOrder_Main_Attachment',Panel1,9);
      Post;
    end;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFBOrder_Sub where SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          Edit;
          if Trim(CDS_Sub.fieldbyname('SOrderMQty').AsString)<>'' then
            FieldByName('SOrderMQty').Value:=CDS_Sub.fieldbyname('SOrderMQty').AsString;
          FieldByName('SNote').Value:=CDS_Sub.fieldbyname('SNote').AsString;
          Post;
        end;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    SaveJiangYe();
    Application.MessageBox('保存成功！','提示',0);
    Label7.Caption:='已生成';
    Label7.Font.Color:=clBlue;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常！','提示',0);
  end;
end;
procedure TfrmOrderAttachment.SaveJiangYe();
var
  maxno,maxnosub:string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.MainId,A.SWFBColor from WFBOrder_Sub A inner join WFBOrder_Main_Attachment B');
      sql.Add(' on A.MainId=B.MainId where A.MainId='''+Trim(FAMainId)+'''');
      sql.Add(' group by A.MainId,A.SWFBColor');
      Open;
    end;
    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFBYCL_MainIdColor_JiangYe where MainId='''+Trim(FAMainId)+'''');
          sql.Add(' and SWFBColor='''+Trim(ADOQuery1.fieldbyname('SWFBColor').AsString)+'''');
          Open;
        end;
        if ADOTemp.IsEmpty then
        begin
          if GetLSNo(ADOCmd,maxno,'MC','WFBYCL_MainIdColor_JiangYe',2,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取浆料表最大号失败！','提示',0);
            Exit;
          end;
          with ADOCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from WFBYCL_MainIdColor_JiangYe where 1<>1');
            Open;
          end;
          with ADOCmd do
          begin
            Append;
            FieldByName('MCID').Value:=Trim(maxno);
            FieldByName('MainId').Value:=Trim(FAMainId);
            FieldByName('SWFBColor').Value:=Trim(ADOQuery1.fieldbyname('SWFBColor').AsString);
            Post;
          end;
        end else
        begin
          maxno:=Trim(ADOTemp.fieldbyname('MCID').AsString);
        end;
        with ADOQuery2 do
        begin
          Close;
          sql.Clear;
          sql.Add('exec  P_JiangLiaoListMainIdColor :MainId,:SWFBColor,:WSQl');
          Parameters.ParamByName('MainId').Value:=Trim(FAMainId);
          Parameters.ParamByName('SWFBColor').Value:=Trim(ADOQuery1.fieldbyname('SWFBColor').AsString);
          Parameters.ParamByName('WSQl').Value:='';
          Open;
        end;
        with ADOQuery2 do
        begin
          First;
          while not Eof do
          begin
            with ADOTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from WFBYCL_JiangYe where MainId='''+Trim(FAMainId)+'''');
              sql.Add(' and SWFBColor='''+Trim(ADOQuery1.fieldbyname('SWFBColor').AsString)+'''');
              sql.Add(' and YCLCode='''+Trim(ADOQuery2.fieldbyname('YCLCode').AsString)+'''');
              Open;
            end;
            if Trim(ADOTemp.fieldbyname('YJID').AsString)='' then
            begin
              if GetLSNo(ADOCmd,maxnosub,'YJ','WFBYCL_JiangYe',3,1)=False then
              begin
                ADOCmd.Connection.RollbackTrans;
                Application.MessageBox('取浆料表最大号失败！','提示',0);
                Exit;
              end;
            end else
            begin
              maxnosub:=Trim(ADOTemp.fieldbyname('YJID').AsString);
            end;
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from WFBYCL_JiangYe where YJID='''+Trim(maxnosub)+'''');
              open;
            end;
            with ADOCmd do
            begin
              if Trim(ADOTemp.fieldbyname('YJID').AsString)='' then
                Append
              else
                Edit;
              FieldByName('MCID').Value:=Trim(maxno);
              FieldByName('YJID').Value:=Trim(maxnosub);
              FieldByName('MainId').Value:=Trim(FAMainId);
              FieldByName('SWFBColor').Value:=Trim(ADOQuery1.fieldbyname('SWFBColor').AsString);
              FieldByName('YCLCode').Value:=Trim(ADOQuery2.fieldbyname('YCLCode').AsString);
              FieldByName('YCLName').Value:=Trim(ADOQuery2.fieldbyname('YCLName').AsString);
              FieldByName('YGQty').Value:=Trim(ADOQuery2.fieldbyname('YGQty').AsString);
              Post;
            end;
            Next;
          end;
        end;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('浆料表保存失败！','提示',0);
  end;
end;
procedure TfrmOrderAttachment.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('无纺布计划单1',TvSub,'指示单管理');
  Close;
end;

procedure TfrmOrderAttachment.Label8Click(Sender: TObject);
begin
  ADefStr10.Font.Color:=TLabel(Sender).Color;
  ADefStr10.Hint:=TLabel(Sender).Hint;
end;

end.
