unit U_ClothSCYWYInPut;

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, cxDropDownEdit,
  StrUtils, cxPC, ShellAPI, cxCalendar, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmClothSCYWYInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    Label2: TLabel;
    QDTime: TDateTimePicker;
    Label3: TLabel;
    Label11: TLabel;
    CarType: TcxButtonEdit;
    Label9: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label22: TLabel;
    Label5: TLabel;
    DeliveryDate: TDateTimePicker;
    Label6: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    PiKgQty: TEdit;
    PiQty: TEdit;
    Label10: TLabel;
    Label13: TLabel;
    C_Qty: TEdit;
    Label16: TLabel;
    Label26: TLabel;
    Label31: TLabel;
    Label36: TLabel;
    PiKgQtyFD: TEdit;
    Label37: TLabel;
    C_Note: TRichEdit;
    Label41: TLabel;
    Label42: TLabel;
    LabName: TComboBox;
    Label47: TLabel;
    Label33: TLabel;
    BZNote: TcxButtonEdit;
    Label54: TLabel;
    gongyi: TcxButtonEdit;
    C_Spec: TRichEdit;
    Label58: TLabel;
    SuoShuiNote: TcxButtonEdit;
    ShaChang: TEdit;
    KZ: TEdit;
    MF: TEdit;
    KHName: TcxButtonEdit;
    Label43: TLabel;
    YWY: TcxButtonEdit;
    Label59: TLabel;
    lidan: TcxButtonEdit;
    Label60: TLabel;
    FactoryNoName: TcxButtonEdit;
    Label61: TLabel;
    GDY: TcxButtonEdit;
    Label62: TLabel;
    ConType: TcxButtonEdit;
    Label4: TLabel;
    C_Color: TEdit;
    PSNote: TEdit;
    Label7: TLabel;
    PBFactoryNoName: TcxButtonEdit;
    Label14: TLabel;
    TJGongYiCode: TcxButtonEdit;
    Label15: TLabel;
    ZhenCun: TEdit;
    Label27: TLabel;
    JTZB: TcxButtonEdit;
    Label17: TLabel;
    Label20: TLabel;
    Label28: TLabel;
    MinWet: TEdit;
    MaxWet: TEdit;
    Label21: TLabel;
    C_NDXS: TEdit;
    ConNo: TcxButtonEdit;
    C_CodeName: TEdit;
    C_Code: TEdit;
    OrderDetail: TEdit;
    GYChkFlag: TCheckBox;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure C_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv5MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure KHNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FactoryNoNameClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure PBFactoryNoNameClick(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CarTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    FXS, Fint: Integer;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FMainId, FConNo, PType, FSubId: string;
    { Public declarations }
  end;

var
  frmClothSCYWYInPut: TfrmClothSCYWYInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompressionFun, U_ZdyAttachGYS,
  U_ShaKCSelList, U_SCPerson, U_ContractListNXSel, U_FjList_RZ, U_JTAllSel,
  U_CPManageFMSel, U_ZdyAttachment, U_ClothGongYiListSel, U_MachineTypeSel,
  U_YCLViewSel, U_PhoneOrder;


{$R *.dfm}

procedure TfrmClothSCYWYInPut.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmClothSCYWYInPut.InitData();
var
  PBNO, PBFlag: string;
begin
  {if PState = 1 then
  begin
    ConNo.ReadOnly := True;
  end;  }
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('  select OM.*,OS.*    ');
    sql.Add(',OrderDetail=(select Top 1 OrderDetail  from PBOrder_Main B where B.OrderNo=OM.ConNo and B.Valid=''Y'')');
    sql.Add(' from Cloth_Main OM   Left join Cloth_Sub OS on OM.MainId=OS.MainId');
    if PState = 1 then
    begin
      sql.Add(' where OM.MainId= ' + quotedstr(Trim(FMainId)));
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1 ');
    end;
    Open;
  end;

  SCSHData(ADOTemp, ScrollBox1, 0);
  SCSHData(ADOTemp, ScrollBox1, 2);
  GYChkFlag.Checked := ADOTemp.FieldByName('GYChkFlag').AsBoolean;
  OrderDetail.Text := Trim(ADOTemp.fieldbyname('OrderDetail').AsString);
end;

procedure TfrmClothSCYWYInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
begin

end;

procedure TfrmClothSCYWYInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmClothSCYWYInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'CM', 'Cloth_Main', 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 Cloth_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MainId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'Cloth_Main', ScrollBox1, 0);
     // FieldByName('ConNo').Value := Trim(ConNo.Text);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;

    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Cloth_Main where ConNo=''' + Trim(ConNo.Text) + '''');
      Open;
    end;
    if ADOTemp.RecordCount > 1 then
    begin
      if Application.MessageBox('订单号重复，确定要继续保存数据吗？', '提示', 32 + 4) <> IDYES then
      begin
        ADOCmd.Connection.RollbackTrans;
        Result := False;
        Exit;
      end;

    end;


    ///保存子表
    if Trim(FSubId) = '' then
    begin
      if GetLSNo(ADOTemp, maxsubno, '9', 'Cloth_Sub', 4, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取子流水号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxsubno := Trim(FSubId);
    end;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Cloth_Sub where ');
      sql.Add('  SubId=''' + Trim(maxsubno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FSubId) = '' then
        Append
      else
        Edit;
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('SubId').Value := Trim(maxsubno);
      RTSetsavedata(ADOCmd, 'Cloth_Sub', ScrollBox1, 2);
      if GYChkFlag.Checked = True then
      begin
        FieldByName('GYChkFlag').Value := 1;
      end
      else
      begin
        FieldByName('GYChkFlag').Value := 0;
      end;
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Cloth_Sub_WJG_Qty :MainId');
      Parameters.ParamByName('MainId').Value := Trim(maxno);
      ExecSQL;
    end;
    ///保存子表
    ADOCmd.Connection.CommitTrans;
    FMainId := Trim(maxno);
    FSubId := Trim(maxsubno);
    Result := True;
  except
    Result := False;

    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmClothSCYWYInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
begin
  QDTime.SetFocus;

  if Trim(ConNo.Text) = '' then
  begin
    Application.MessageBox('订单号不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(C_CodeName.Text) = '' then
  begin
    Application.MessageBox('产品名称不能为空!', '提示', 0);
    Exit;
  end;
//  if Trim(C_Code.Text) = '' then
//  begin
//    Application.MessageBox('产品编号不能为空!', '提示', 0);
//    Exit;
//  end;
  if Trim(LabName.Text) = '' then
  begin
    Application.MessageBox('标签格式不能为空!', '提示', 0);
    Exit;
  end;

  if DeliveryDate.Checked = False then
  begin
    Application.MessageBox('交货日期不能为空!', '提示', 0);
    Exit;
  end;
  if QDTime.Checked = False then
  begin
    Application.MessageBox('下单日期不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(C_Qty.Text) = '' then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(C_Qty.Text) <> '' then
  begin
    if TryStrToFloat(C_Qty.Text, FReal) = False then
    begin
      Application.MessageBox('数量非法数字!', '提示', 0);
      Exit;
    end;
  end;

  if Trim(PiQty.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(PiQty.Text, FReal) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    Exit;
  end;
//  if Trim(PiQty.Text) = '' then
//  begin
//    Application.MessageBox('匹重不能为空!', '提示', 0);
//    Exit;
//  end;
//  if TryStrToFloat(PiQty.Text, FReal) = False then
//  begin
//    Application.MessageBox('匹重非法数字!', '提示', 0);
//    Exit;
//  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmClothSCYWYInPut.C_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  if fsj = '' then
  begin
    {try
      frmCPManageCX:=TfrmCPManageCX.Create(Application);
      with frmCPManageCX do
      begin
        if ShowModal=1 then
        begin
          Self.C_CodeName.Text:=Trim(frmCPManageCX.CDS_Main.fieldbyname('CYName').AsString);
          Self.C_Code.Text:=Trim(frmCPManageCX.CDS_Main.fieldbyname('CYNo').AsString);
          Self.C_Spec.Text:=Trim(frmCPManageCX.CDS_Main.fieldbyname('CYSpec').AsString);
        end;
      end;
    finally
      frmCPManageCX.Free;
    end;  }
  end
  else
  begin
    FWZ := Pos('/', fsj);
    try
      frmZDYHelp := TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag := Copy(fsj, 1, FWZ - 1);
        flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
        if Trim(flag) = 'C_CodeName' then
        begin
          fnote := True;
          V1Note.Caption := '类型';
        end;
        if ShowModal = 1 then
        begin
          TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
  end;

end;

procedure TfrmClothSCYWYInPut.Tv3MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 1;
end;

procedure TfrmClothSCYWYInPut.Tv5MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 2;
end;

procedure TfrmClothSCYWYInPut.C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
//    with frmCPManageFMSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        C_Code.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYNo').AsString;
//        C_CodeName.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
//        C_Spec.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYSpec').AsString;
//        MF.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYMF').AsString;
//        KZ.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYKZ').AsString;
//
//      end;
//    end;
//  finally
//    frmCPManageFMSel.Free;
//  end;


end;

procedure TfrmClothSCYWYInPut.KHNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        KHName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmClothSCYWYInPut.FactoryNoNameClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      FactType.Text := '染厂';
      if ShowModal = 1 then
      begin
        FactoryNoName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmClothSCYWYInPut.FormCreate(Sender: TObject);
begin
  QDTime.DateTime := SGetServerDate10(ADOTemp);
  DeliveryDate.DateTime := SGetServerDate10(ADOTemp);
end;

procedure TfrmClothSCYWYInPut.PBFactoryNoNameClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      FactType.Text := '坯布厂';
      if ShowModal = 1 then
      begin
        PBFactoryNoName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmClothSCYWYInPut.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothGongYiListSel := TfrmClothGongYiListSel.Create(Application);
    with frmClothGongYiListSel do
    begin
      if ShowModal = 1 then
      begin
        Self.TJGongYiCode.Text := frmClothGongYiListSel.Order_Main.fieldbyname('GongYiCode').AsString;
        Self.C_Spec.Text := frmClothGongYiListSel.Order_Main.fieldbyname('C_Spec').AsString;
        Self.MF.Text := frmClothGongYiListSel.Order_Main.fieldbyname('MF').AsString;
        Self.KZ.Text := frmClothGongYiListSel.Order_Main.fieldbyname('KZ').AsString;
        Self.CarType.Text := frmClothGongYiListSel.Order_Main.fieldbyname('CarType').AsString;
        Self.JTZB.Text := frmClothGongYiListSel.Order_Main.fieldbyname('JTZB').AsString;
        Self.ZhenCun.Text := frmClothGongYiListSel.Order_Main.fieldbyname('ZhenCun').AsString;
      end;
    end;
  finally
    frmClothGongYiListSel.Free;
  end;
end;

procedure TfrmClothSCYWYInPut.CarTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmMachineTypeSel := TfrmMachineTypeSel.Create(Application);
    with frmMachineTypeSel do
    begin
      if ShowModal = 1 then
      begin
        CarType.Text := Trim(CDS_HZ.fieldbyname('MCZX').AsString) + Trim(CDS_HZ.fieldbyname('MCLS').AsString) + Trim(CDS_HZ.fieldbyname('MCType').AsString);
        JTZB.Text := Trim(CDS_HZ.fieldbyname('MCClass').AsString);
      end;
    end;
  finally
    frmMachineTypeSel.Free;
  end;
end;

procedure TfrmClothSCYWYInPut.ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPhoneOrder := TfrmPhoneOrder.Create(Application);
    with frmPhoneOrder do
    begin
      if ShowModal = 1 then
      begin
        Self.C_Code.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('SPCode').asstring);
        Self.C_CodeName.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('MPRTCodeName').asstring);
        Self.C_NDXS.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('SPXS').asstring);
        Self.lidan.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('Filler').asstring);
        Self.ConNo.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('OrderNo').asstring);
        Self.OrderDetail.Text := Trim(frmPhoneOrder.CDS_HZ.fieldbyname('OrderDetail').asstring);
      end;
    end;
  finally
    frmPhoneOrder.Free;
  end;
end;

end.

