unit U_YarnDRtInPut;

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,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator,  
  dxDateRanges, dxBarBuiltInMenu, U_BaseList,  
      
     
     
     
    
    
    
    
    
    
     
      
     
   
     
   dxScrollbarAnnotations;

type
  TfrmYarnDRtInPut = class(Tform)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    OutType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    ToCoName: TBtnEditC;
    cxStyleRepository1: TcxStyleRepository;
    dazi1: TcxStyle;
    Label2: TLabel;
    DlyDate: TDateTimePicker;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label27: TLabel;
    QtyUnit: TComboBox;
    Label3: TLabel;
    DRMID: TEdit;
    Label4: TLabel;
    Filler: TEdit;
    Label5: TLabel;
    OrderNo: TEdit;
    ToolButton6: TToolButton;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    ToolButton7: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToCoNameBtnUpClick(Sender: TObject);
    procedure ToCoNameBtnDnClick(Sender: TObject);
    procedure JSQtyUnitChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure OutTypeChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FDRMID: string;
    { Public declarations }
  end;

var
  frmYarnDRtInPut: TfrmYarnDRtInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompanySel, U_TatPlanSel, U_TatYarnSel,
  U_YarnStkSel, U_JZYarnSel;

{$R *.dfm}

procedure TfrmYarnDRtInPut.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmYarnDRtInPut.InitData();
var
  fsj: string;
begin
  if PState = 0 then
  begin
    DlyDate.DateTime := strtodatetime(Trim(FormatDateTime('yyyy-MM-dd', Now)));

  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from Tat_Cloth_DR_Sub A');
    if PState = 1 then
    begin
      sql.Add('where DRMID=''' + Trim(FDRMID) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOQuery1, Order_Sub);
  SInitCDSData(ADOQuery1, Order_Sub);

  if PState = 0 then
  begin
    DRMID.text := '自动生成';
    Filler.text := DName;
  end;

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from Tat_Cloth_DR_Main A');
    sql.Add(' where DRMID=''' + Trim(FDRMID) + ''' ');
    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 1);

end;

procedure TfrmYarnDRtInPut.JSQtyUnitChange(Sender: TObject);
begin
  if not Order_Sub.IsEmpty then
  begin

    exit;
  end;
end;

procedure TfrmYarnDRtInPut.OutTypeChange(Sender: TObject);
begin
  if OutType.text = '整经出库' then
  begin
    ToCoName.text := '整经车间';
  end
  else
  begin
    ToCoName.text := '织造车间';
  end;
end;

procedure TfrmYarnDRtInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin

  readCxGrid(trim(self.Caption), Tv1, '发货管理');

  InitData();
end;

function TfrmYarnDRtInPut.SaveData(): Boolean;
var
  MaxMid, MaxSid: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    /////////////////              保存主表                /////////////////
    if Trim(FDRMID) = '' then
    begin
      if GetLSNo(ADOCmd, MaxMid, 'SC', 'Tat_Cloth_DR_Main', 4, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      MaxMid := Trim(FDRMID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Tat_Cloth_DR_Main where DRMID=''' + Trim(MaxMid) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FDRMID) = '' then
      begin
        Append;
        FieldByName('FillID').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditID').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Tat_Cloth_DR_Main', ScrollBox1, 1);
      FieldByName('DRMID').Value := Trim(MaxMid);
      FieldByName('Status').Value := '0';
      FieldByName('LX').Value := '纱线';
      Post;
    end;
  /////////////////              保存主表                /////////////////
  ///
  /////////////////              保存子表                /////////////////
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('DRSID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, MaxSid, 'TDS', 'Tat_Cloth_DR_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败！');
        end
        else
        begin
          MaxSid := Trim(Order_Sub.fieldbyname('DRSID').AsString);
        end;

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_Cloth_DR_Sub ');
          sql.Add(' where DRMID=''' + Trim(MaxMid) + '''');
          sql.Add(' and DRSID=''' + Trim(MaxSid) + '''');
          Open;
          if ADOCmd.IsEmpty then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Tat_Cloth_DR_Sub', 0);
          FieldByName('DRMID').Value := Trim(MaxMid);
          FieldByName('DRSID').Value := Trim(MaxSid);
          FieldByName('QtyUnit').Value := QtyUnit.Text;
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('StkID').Value := Order_Sub.fieldbyname('StkID').Value;
          FieldByName('YRID').Value := Order_Sub.fieldbyname('YRID').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('DRSID').Value := Trim(MaxSid);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
   /////////////////              保存子表                /////////////////
    FDRMID := Trim(MaxMid);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmYarnDRtInPut.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;

  if Trim(OutType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ToCoName.Text) = '' then
  begin
    Application.MessageBox('收获单位不能为空！', '提示', 0);
    Exit;
  end;
  if (Order_Sub.Locate('Qty', null, [])) = True then
  begin
    Application.MessageBox('数量不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmYarnDRtInPut.ToolButton1Click(Sender: TObject);
var
  FQtyUnit, FFSubid: string;
begin
 {
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
//          Self.CustomerNoName.Text := Trim(frmTradePlanSel.Order_Main.fieldbyname('CustomerNoName').AsString);
//          FQtyUnit := Trim(frmTradePlanSel.Order_Main.fieldbyname('orderUnit').AsString);
//          if Self.Order_Sub.Locate('Subid', Trim(frmTradePlanSel.Order_Main.fieldbyname('Subid').AsString), []) = False then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('SCConNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('Orderno').AsString);
              FieldByName('SSConMainId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConMainId').Value;
              FieldByName('SSConSubId').Value := frmTradePlanSel.CDS_1.fieldbyname('ConSubId').Value;
              FieldByName('MainId').Value := frmTradePlanSel.CDS_1.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmTradePlanSel.CDS_1.fieldbyname('SubId').Value;
              FieldByName('PRTColor').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_Color').AsString);
              FieldByName('SPrtCode').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_Code').AsString);
              FieldByName('SPrtCodeName').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_CodeName').AsString);
              FieldByName('PRTHX').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('P_HX').AsString);
//              FieldByName('orderUnit').Value := JSQtyUnit.Text;
//              if Trim(JSQtyUnit.Text) = 'Kg' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYGW').Value;
//              end
//              else if Trim(JSQtyUnit.Text) = 'M' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYLen').Value;
//              end
//              else if Trim(JSQtyUnit.Text) = 'Y' then
//              begin
//                FieldByName('sqqty').Value := frmTradePlanSel.Order_Main.fieldbyname('JYYLen').Value;
//              end;

            end;
          end;
          CDS_1.Delete;
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;   }
end;

procedure TfrmYarnDRtInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('DRSID').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
      sql.add(' and isnull(DlyPiece,0)>0');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已发货不能删除数据!', '提示', 0);
      exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Tat_Cloth_DR_Sub where DRSID=''' + Trim(Order_Sub.fieldbyname('DRSID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmYarnDRtInPut.ToolButton3Click(Sender: TObject);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTatPlanSel := TfrmTatPlanSel.Create(Application);
    with frmTatPlanSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('ConNo').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConNo').AsString);
            FieldByName('ConMId').Value := Trim(frmTatPlanSel.CDS_1.fieldbyname('ConMId').AsString);
            FieldByName('ConSId').Value := frmTatPlanSel.CDS_1.fieldbyname('ConSId').Value;
            FieldByName('BuyConNo').Value := frmTatPlanSel.CDS_1.fieldbyname('BuyConNo').Value;
            FieldByName('C_Code').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Code').Value;
            FieldByName('C_Name').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Name').Value;
            FieldByName('C_Spec').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Spec').Value;
            FieldByName('C_Width').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Width').Value;
            FieldByName('C_GramWeight').Value := frmTatPlanSel.CDS_1.fieldbyname('C_GramWeight').Value;
            FieldByName('C_Color').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Color').Value;
            FieldByName('C_ColorNo').Value := frmTatPlanSel.CDS_1.fieldbyname('C_ColorNo').Value;
            FieldByName('C_Pattern').Value := frmTatPlanSel.CDS_1.fieldbyname('C_Pattern').Value;
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmTatPlanSel.Free;
  end;
end;

procedure TfrmYarnDRtInPut.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '发货管理')
end;

procedure TfrmYarnDRtInPut.ToolButton5Click(Sender: TObject);
begin
  try
    frmTatYarnSel := TfrmTatYarnSel.Create(Application);
    with frmTatYarnSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_1.locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;

            Self.OrderNo.text := Trim(frmTatYarnSel.CDS_1.fieldbyname('OrderNo').AsString);

            FieldByName('MainID').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('MainID').AsString);
            FieldByName('SubID').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('SubID').AsString);
            FieldByName('YRID').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('YRID').AsString);

            FieldByName('Y_Code').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('Y_Code').AsString);
            FieldByName('Yarn_Name').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('Yarn_Name').AsString);
            FieldByName('Y_Name').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('Y_Name').AsString);

            FieldByName('Y_Spec').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('Y_Spec').AsString);

            FieldByName('Denier').Value := frmTatYarnSel.CDS_1.fieldbyname('Denier').asfloat;
            FieldByName('Z_Number').Value := frmTatYarnSel.CDS_1.fieldbyname('Z_Number').asfloat;
            FieldByName('F_Number').Value := frmTatYarnSel.CDS_1.fieldbyname('F_Number').asfloat;

            FieldByName('PH').Value := Trim(frmTatYarnSel.CDS_1.fieldbyname('BCPH').AsString);
          end;
          CDS_1.Edit;
          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmTatYarnSel.Free;
  end;
end;

procedure TfrmYarnDRtInPut.ToolButton6Click(Sender: TObject);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_Main.locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('StkID').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('StkID').AsString);

            FieldByName('Y_Code').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('Y_Code').AsString);
            FieldByName('Yarn_Name').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('Yarn_Name').AsString);
            FieldByName('Y_Name').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('Y_Name').AsString);

            FieldByName('Y_Spec').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('Y_Spec').AsString);

            FieldByName('Denier').Value := frmYarnStkSel.CDS_Main.fieldbyname('Denier').asfloat;
            FieldByName('Z_Number').Value := frmYarnStkSel.CDS_Main.fieldbyname('Z_Number').asfloat;
            FieldByName('F_Number').Value := frmYarnStkSel.CDS_Main.fieldbyname('F_Number').asfloat;

            FieldByName('PH').Value := Trim(frmYarnStkSel.CDS_Main.fieldbyname('BCPH').AsString);
          end;
          CDS_Main.Edit;
          CDS_Main.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;
end;

procedure TfrmYarnDRtInPut.ToolButton7Click(Sender: TObject);
begin
  try
    frmJZYarnSel := TfrmJZYarnSel.Create(Application);
    with frmJZYarnSel do
    begin
      if ShowModal = 1 then
      begin
        while CDS_2.locate('SSel', true, []) do
        begin
          with Self.Order_Sub do
          begin
            Append;

            Self.OrderNo.text := Trim(frmJZYarnSel.CDS_1.fieldbyname('OrderNo').AsString);

            FieldByName('MainID').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('MainID').AsString);
            FieldByName('SubID').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('SubID').AsString);
            FieldByName('YRID').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('YRID').AsString);
            FieldByName('StkID').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('BYIOID').AsString);

            FieldByName('Y_Code').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('Y_Code').AsString);
            FieldByName('Yarn_Name').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('Yarn_Name').AsString);
            FieldByName('Y_Name').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('Y_Name').AsString);

            FieldByName('Y_Spec').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('Y_Spec').AsString);

            FieldByName('Denier').Value := frmJZYarnSel.CDS_2.fieldbyname('Denier').asfloat;
            FieldByName('Z_Number').Value := frmJZYarnSel.CDS_2.fieldbyname('Z_Number').asfloat;
            FieldByName('F_Number').Value := frmJZYarnSel.CDS_2.fieldbyname('F_Number').asfloat;

            FieldByName('PH').Value := Trim(frmJZYarnSel.CDS_2.fieldbyname('PH').AsString);
          end;

          CDS_2.Edit;
          CDS_2.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmJZYarnSel.Free;
  end;
end;

procedure TfrmYarnDRtInPut.ToCoNameBtnUpClick(Sender: TObject);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.ToCoName.TxtCode := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.ToCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmYarnDRtInPut.ToCoNameBtnDnClick(Sender: TObject);
begin
  ToCoName.Text := '';
end;

end.

