unit U_MLContractInPut_PB;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  cxGroupBox, cxCheckBox, ImgList, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, Menus, ShellAPI, cxSplitter;

type
  TfrmMLContractInPut_PB = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1Qty: TcxGridDBColumn;
    Label10: TLabel;
    v1XHNo: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    Label3: TLabel;
    CJDate: TDateTimePicker;
    v1ConNote: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1ConCode: TcxGridDBColumn;
    v1ConCodeName: TcxGridDBColumn;
    ADOQueryCmd: TADOQuery;
    Label2: TLabel;
    GYSName: TBtnEditC;
    Panel1: TPanel;
    Label1: TLabel;
    Panel21: TPanel;
    v1ConPrice: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    CJNO: TEdit;
    Label42: TLabel;
    Label4: TLabel;
    CJPS: TEdit;
    Label5: TLabel;
    CJPrice: TEdit;
    v1Column1: TcxGridDBColumn;
    Label6: TLabel;
    CJQty: TEdit;
    Label7: TLabel;
    CJQtyUnit: TComboBox;
    Label8: TLabel;
    HanShui: TComboBox;
    Label9: TLabel;
    Label11: TLabel;
    MLCodeName: TBtnEditA;
    Edit1: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure MLCodeNameBtnUpClick(Sender: TObject);
    procedure GYSNameBtnUpClick(Sender: TObject);
    procedure GYSNameBtnDnClick(Sender: TObject);
    procedure MLCodeNameBtnDnClick(Sender: TObject);
    procedure MLCodeNameBtnClick(Sender: TObject);
    procedure MLCodeNameChange(Sender: TObject);
  private
    procedure InitData();
    function SaveData(): Boolean;

    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmMLContractInPut_PB: TfrmMLContractInPut_PB;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun,U_GYSListPB, U_BPZdy_LRPB,U_OrderSel;

{$R *.dfm}
procedure TfrmMLContractInPut_PB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布采购计划', Tv1, '生产指示单管理');
end;

procedure TfrmMLContractInPut_PB.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.MLOrderNo,B.MLCodeName,B.MLHZQty,B.MLHZQtyUnit');
    SQL.Add(' from ML_CGJG_Plan_MX A  ');
    sql.Add(' left join ML_OrderMainNew B on A.NewMLID=B.NewMLID');
    sql.Add(' where A.CJID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ML_CGJG_Plan where CJID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 2);
  Edit1.Text:=Trim(ADOQuery1.fieldbyname('MLCodeName').AsString);
  Edit1.Hint:=Trim(ADOQuery1.fieldbyname('MLCode').AsString);
  CJNO.Text:=Trim(ADOQuery1.fieldbyname('CJNO').AsString);
  if PState = 0 then
  begin
    CJDate.DateTime := SGetServerDateTime(ADOTemp);
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA=max(CJNO) from ML_CGJG_Plan where CJType=''坯布采购'' ');
      Open;
    end;
    if Trim(ADOTemp.fieldbyname('AA').AsString)='' then
    begin
      CJNO.Text:='坯布采购00001';
    end;
  end;
  
end;

procedure TfrmMLContractInPut_PB.FormShow(Sender: TObject);
begin
  readCxGrid('坯布采购计划', Tv1, '生产指示单管理');
  InitData();
end;

function TfrmMLContractInPut_PB.SaveData(): Boolean;
var
  maxno, FConNO, FUserID, FNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'CJ', 'ML_CGJG_Plan', 4, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_CGJG_Plan where CJID=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
      end
      else
      begin
        Edit;
      end;
      FieldByName('CJID').Value := Trim(maxno);
      FieldByName('CJType').Value := '坯布采购';
      RTSetsavedata(ADOCmd, 'ML_ConMain', ScrollBox1, 2);
      FieldByName('ChkStatus').Value := '未审核';
      Post;
    end;
    if Trim(FMainId)='' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Update_CJNO :CJID,:CJType');
        Parameters.ParamByName('CJID').Value:=Trim(maxno);
        Parameters.ParamByName('CJType').Value:='坯布采购';
        ExecSQL;
      end;
    end;
    FMainId := Trim(maxno);

    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('CMID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'CM', 'ML_CGJG_Plan_MX', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('CMID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_CGJG_Plan_MX where CJID=''' + Trim(FMainId) + '''');
          sql.Add(' and CMID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('CMID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('CJID').Value := Trim(FMainId);
          FieldByName('CMID').Value := Trim(maxno);
          FieldByName('NewMLID').Value := Trim(Order_Sub.fieldbyname('NewMLID').AsString);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'ML_CGJG_Plan_MX', 2);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('CMID').Value := Trim(maxno);
        Order_Sub.FieldByName('CJID').Value := Trim(FMainId);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmMLContractInPut_PB.TBSaveClick(Sender: TObject);
begin
  CJDate.SetFocus;
  {if CJNO.Text = '' then
  begin
    Application.MessageBox('计划单号不能为空', '提示');
    exit;
  end; }
  if GYSName.Text = '' then
  begin
    Application.MessageBox('供应商不能为空', '提示');
    exit;
  end;
  if MLCodeName.Text = '' then
  begin
    Application.MessageBox('坯布名称不能为空', '提示');
    exit;
  end;
  if CJQtyUnit.Text = '' then
  begin
    Application.MessageBox('数量单位不能为空', '提示');
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;


procedure TfrmMLContractInPut_PB.MLCodeNameBtnUpClick(Sender: TObject);
var
  FBPIDSH: string;
begin
  frmBPZDY_LRPB := TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    Flag := 'PBName';
    flagName := '坯布';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    Button1.Visible := False;
    ToolButton1.Visible := true;
    if ShowModal = 1 then
    begin
      MLCodeName.TxtCode := Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      MLCodeName.Text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;
  CJQty.Text:=MLCodeName.TxtCode;
end;

procedure TfrmMLContractInPut_PB.GYSNameBtnUpClick(Sender: TObject);
begin
  try
    frmGYSListPB:=TfrmGYSListPB.Create(Application);
    with frmGYSListPB do
    begin
      if ShowModal=1 then
      begin
        GYSName.Text:=Trim(frmGYSListPB.Order_Main.fieldbyname('KHNameJC').AsString);
        GYSName.TxtCode:=Trim(frmGYSListPB.Order_Main.fieldbyname('KHNO').AsString);
      end;
    end;
  finally
    frmGYSListPB.Free;
  end;
end;

procedure TfrmMLContractInPut_PB.GYSNameBtnDnClick(Sender: TObject);
begin
  GYSName.Text:='';
  GYSName.TxtCode:='';
end;

procedure TfrmMLContractInPut_PB.MLCodeNameBtnDnClick(Sender: TObject);
begin
  MLCodeName.Text:='';
  MLCodeName.TxtCode:='';
end;

procedure TfrmMLContractInPut_PB.MLCodeNameBtnClick(Sender: TObject);
var
  FBPIDSH: string;
begin
  frmBPZDY_LRPB := TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    Flag := 'PBName';
    flagName := '坯布';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    Button1.Visible := False;
    ToolButton1.Visible := true;
    if ShowModal = 1 then
    begin
      MLCodeName.TxtCode := Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      MLCodeName.Text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;
  Edit1.Text:=MLCodeName.Text;
  Edit1.Hint:=MLCodeName.Text;
end;

procedure TfrmMLContractInPut_PB.MLCodeNameChange(Sender: TObject);
begin
  if Trim(MLCodeName.Text)<>Trim(Edit1.Text) then
  begin
    MLCodeName.TxtCode :='';
  end else
  begin
    MLCodeName.TxtCode:=Trim(Edit1.Hint);
  end;
end;

end.

