unit U_FHSQInPut220313;

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;

type
  TfrmFHSQInPut220313 = 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;
    Label10: TLabel;
    Label14: TLabel;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    CRType: TComboBox;
    Label2: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    PrtCodeName: TEdit;
    prtmf: TEdit;
    Label9: TLabel;
    prtkz: TEdit;
    ConNo: TBtnEditA;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    SHDanWei: TBtnEditC;
    Label16: TLabel;
    ranchang: TBtnEditC;
    PrtCode: TBtnEditA;
    CustomerNoName: TBtnEditA;
    Label6: TLabel;
    prtspec: TEdit;
    Label15: TLabel;
    CarType: TEdit;
    Label24: TLabel;
    gongyi: TEdit;
    Label4: TLabel;
    BatchNo: TComboBox;
    v1Column1: TcxGridDBColumn;
    cv1Column2: TcxGridDBColumn;
    cv1Column3: TcxGridDBColumn;
    cv1Column4: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    dazi1: TcxStyle;
    cv1Column5: TcxGridDBColumn;
    cv1Column6: TcxGridDBColumn;
    cv1Column7: TcxGridDBColumn;
    cv1Column8: TcxGridDBColumn;
    lbl2: TLabel;
    DLYDATE: TDateTimePicker;
    Label3: TLabel;
    GuiXing: TBtnEditC;
    v1Column2: TcxGridDBColumn;
    Label7: TLabel;
    XSType: TComboBox;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ConNoBtnClick(Sender: TObject);
    procedure SHDanWeiBtnUpClick(Sender: TObject);
    procedure SHDanWeiBtnDnClick(Sender: TObject);
    procedure ranchangBtnUpClick(Sender: TObject);
    procedure ShouKuanKeyPress(Sender: TObject; var Key: Char);
    procedure PrtCodeBtnClick(Sender: TObject);
    procedure CustomerNoNameBtnClick(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FMainId, FFMainId, ChkStr, ConPriceStr: string;
    FXS: Integer;
    { Public declarations }
  end;

var
  frmFHSQInPut220313: TfrmFHSQInPut220313;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_SQKCHZSelList, U_ContractListNXSel,
  U_CPManageFMSel, U_ZdyAttachment, U_ClothSCYWYListSel, U_ProductOrderListSel;

{$R *.dfm}

procedure TfrmFHSQInPut220313.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货申请录入', Tv1, '发货管理');
end;

procedure TfrmFHSQInPut220313.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(',GCCode=(select GCCode from Cloth_Main CM where CM.MainId=A.Mainid)');
    //sql.Add(',BatchNoHZ=(select BatchNoHZ from Cloth_Sub CS where CS.MainId=A.Mainid)');
    sql.Add(' from FHSQ_Sub A');
    if PState = 1 then
    begin
      sql.Add('where FSId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;

  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from FHSQ_Main A');
    sql.Add(' where FSId=''' + Trim(FMainId) + ''' ');
    Open;
  end;

//  fsj := ' select DISTINCT A.APBatchNo Name from JYCon_Sub_AnPai A inner join Cloth_Main B on A.MainId=B.MainId where  isnull(A.APBatchNo,'''')<>'''' and B.Valid=''Y'' and B.ConNo=' + quotedstr(Trim(ADOQuery1.FieldByName('Conno').AsString));
//  SInitComBoxBySql(ADOTemp, BatchNo, False, fsj);

  SCSHDataNew(ADOQuery1, ScrollBox1, 1);

  BatchNo.Clear;
  BatchNo.Items.Add(Trim(ADOQuery1.fieldbyname('BatchNo').AsString));
  BatchNo.ItemIndex := BatchNo.Items.IndexOf(Trim(ADOQuery1.fieldbyname('BatchNo').AsString));
//  ConNo.TxtCode := ;


end;

procedure TfrmFHSQInPut220313.FormShow(Sender: TObject);
var
  fsj: string;
begin
  readCxGrid('发货申请录入', Tv1, '发货管理');

  InitData();
end;

function TfrmFHSQInPut220313.SaveData(): Boolean;
var
  maxno, maxmnno, FSSId: string;
  fconNO, fmxType: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'SM', 'FHSQ_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 FHSQ_Main where FSID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('SQType').Value := '订单';
      FieldByName('FSID').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'FHSQ_Main', ScrollBox1, 1);

      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('SQDate').Value := SGetServerDate(ADOTemp);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;

    FMainId := Trim(maxno);
    ///保存子表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'SS', 'FHSQ_Sub', 4, 1) = False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('SSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from FHSQ_Sub ');
          sql.Add(' where SSId=''' + Trim(maxno) + '''');
          Open;
        end;
        if ADOCmd.IsEmpty then
        begin
          FSSId := '';
        end
        else
        begin
          FSSId := Trim(maxno);
        end;
        with ADOCmd do
        begin
          if Trim(FSSId) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('FSID').Value := Trim(FMainId);
          FieldByName('SSID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'FHSQ_Sub', 0);
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('SSConMainId').Value := Order_Sub.fieldbyname('SSConMainId').Value;
          FieldByName('SSConSubId').Value := Order_Sub.fieldbyname('SSConSubId').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SSID').Value := Trim(maxno);
        Next;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('update FHSQ_Main set DBConNo=cast((select '' ''+ SSConNo from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId and X.SSConNo<>FHSQ_Main.ConNo for xml Path('''')) as varchar(100)) ');
        SQL.Add(', SQPSHZ= (select sum(SQPS) from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId ) ');
        SQL.Add(', SQqtyHZ= (select sum(SQqty) from FHSQ_Sub X where X.FSId=FHSQ_Main.FSId ) ');
        SQL.Add('where FSID=''' + Trim(FMainId) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.EnableControls;

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    Result := False;
  end;
end;

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

  if Trim(CRType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;

  if Trim(PrtCodeName.Text) = '' then
  begin
    Application.MessageBox('产品名称不能为空！', '提示', 0);
    Exit;
  end;

//  if (Trim(SQPSHZ.Text) = '') and (Trim(SQqtyHZ.Text) = '') then
//  begin
//    Application.MessageBox('申请数量和匹数不能全为空！', '提示', 0);
//    Exit;
//  end;
//
//  if Order_Sub.IsEmpty then
//  begin
//    Application.MessageBox('明细不能为空！', '提示', 0);
//    Exit;
//  end;

//  if Order_Sub.Locate('SCConNo', null, []) = True then
//  begin
//    Application.MessageBox('白坯单号不能为空！', '提示', 0);
//    Exit;
//  end;
  if (Order_Sub.Locate('SQPS', null, [])) and (Order_Sub.Locate('SQqty', null, [])) = True then
  begin
    Application.MessageBox('细码申请匹数和数量不能全为空！', '提示', 0);
    Exit;
  end;
//  if Order_Sub.Locate('SQPS', 0, []) = True then
//  begin
//    Application.MessageBox('申请匹数不能为0！', '提示', 0);
//    Exit;
//  end;
//  if Order_Sub.Locate('SQQty', null, []) = True then
//  begin
//    Application.MessageBox('申请数量不能为空！', '提示', 0);
//    Exit;
//  end;

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

procedure TfrmFHSQInPut220313.ToolButton1Click(Sender: TObject);
var
  fsj, FConMainId: string;
begin

  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        Order_Main.locate('SSel', true, []);
        FConMainId := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
        while Order_Main.locate('SSel', true, []) do
        begin

          if FConMainId <> trim(frmProductOrderListSel.Order_Main.Fieldbyname('MainId').AsString) then
          begin
            Application.MessageBox('不能选择多个合同！', '提示', 0);
            break;
          end;

          with Self.Order_Sub do
          begin
            Append;
            FieldByName('SCConNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('Orderno').AsString);
            FieldByName('SSKHName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
            FieldByName('SSConNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('ConNO').AsString);
            FieldByName('MainId').Value := frmProductOrderListSel.Order_Main.fieldbyname('MainId').Value;
            FieldByName('SubId').Value := frmProductOrderListSel.Order_Main.fieldbyname('SubId').Value;
            FieldByName('PRTColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
//          Post;
          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmFHSQInPut220313.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SSId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      sql.add(' and isnull(FHPS,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 FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from FHSQ_Sub A where A.FSID=FHSQ_Main.FSID)');
      sql.Add('where FSID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmFHSQInPut220313.ConNoBtnClick(Sender: TObject);
var
  fsj, FConMainId: string;
begin
//  if Order_Sub.IsEmpty = False then
//  begin
//    Application.MessageBox('已有明细数据，不能更改合同!', '提示', 0);
//    Exit;
//  end;
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        Order_Main.locate('SSel', true, []);
        FConMainId := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
        Self.ConNo.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('ConNo').AsString);
        Self.CustomerNoName.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
//        Self.ranchang.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('FactoryNoName').AsString);
        Self.SHDanWei.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
        Self.PrtCode.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
        Self.PrtCodeName.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
        Self.prtmf.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
        Self.prtkz.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
        Self.prtspec.Text := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
        while Order_Main.locate('SSel', true, []) do
        begin

          if FConMainId <> trim(frmProductOrderListSel.Order_Main.Fieldbyname('MainId').AsString) then
          begin
            Application.MessageBox('不能选择多个合同！', '提示', 0);
            break;
          end;
//          if self.Order_Sub.locate('ConSubId', trim(Order_Main.Fieldbyname('SubId').AsString), []) then
//          begin
//            Order_Main.Edit;
//            Order_Main.fieldbyname('SSel').value := false;
//            continue;
//          end;
          with Self.Order_Sub do
          begin
            Append;
            FieldByName('SCConNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('Orderno').AsString);
            FieldByName('SSKHName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
            FieldByName('SSConNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('ConNO').AsString);
            FieldByName('MainId').Value := frmProductOrderListSel.Order_Main.fieldbyname('MainId').Value;
            FieldByName('SubId').Value := frmProductOrderListSel.Order_Main.fieldbyname('SubId').Value;
            FieldByName('PRTColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
//            FieldByName('kuanhao').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('kuanhao').AsString);
            FieldByName('orderps').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTps').AsString);
            FieldByName('orderqty').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('prtorderqty').AsString);
//          Post;
          end;
          Order_Main.Edit;
          Order_Main.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
//    fsj := ' select DISTINCT A.APBatchNo Name from JYCon_Sub_AnPai A inner join Cloth_Main B on A.MainId=B.MainId ';
//    fsj := fsj + 'where  isnull(A.APBatchNo,'''')<>'''' and B.Valid=''Y'' and B.ConNo=' + quotedstr(ConNo.Text);
//    fsj := fsj + ' order by A.filltime';
//    SInitComBoxBySql(ADOTemp, BatchNo, False, fsj);
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmFHSQInPut220313.SHDanWeiBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SHDanWei';
      flagname := '收货单位';
      if ShowModal = 1 then
      begin
        Self.SHDanWei.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut220313.SHDanWeiBtnDnClick(Sender: TObject);
begin
  SHDanWei.Text := '';
end;

procedure TfrmFHSQInPut220313.ranchangBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ranchang';
      flagname := '染厂';
      if ShowModal = 1 then
      begin
        Self.ranchang.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut220313.ShouKuanKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

procedure TfrmFHSQInPut220313.PrtCodeBtnClick(Sender: TObject);
begin
  try
    frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
    with frmCPManageFMSel do
    begin
      if ShowModal = 1 then
      begin
        PrtCode.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYNo').AsString;
        PrtCodeName.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
        prtmf.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYMF').AsString;
        prtkz.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYKZ').AsString;

      end;
    end;
  finally
    frmCPManageFMSel.Free;
  end;
end;

procedure TfrmFHSQInPut220313.CustomerNoNameBtnClick(Sender: TObject);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CustomerNoName';
      flagname := '客户';
      if ShowModal = 1 then
      begin
        Self.CustomerNoName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

end.

