unit U_CaiGouSQInPut;

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, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmCaiGouSQInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryTemp: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    TBTP: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column12PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBTPClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState:Integer;
    FMainId,FSQType,Canshu1,ZFormName:String;

    { Public declarations }
  end;

var
  frmCaiGouSQInPut: TfrmCaiGouSQInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun, U_ZdyAttachGYS,U_ClothSCHZMXListSel;

{$R *.dfm}

procedure TfrmCaiGouSQInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('采购申请登记',Tv1,'采购申请管理');
end;

procedure TfrmCaiGouSQInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*');
    SQL.Add(' from CaiGou_SQ A ');
    sql.Add(' where A.SQId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  

end;

procedure TfrmCaiGouSQInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('采购申请登记',Tv1,'采购申请管理');
  InitData();
//  if Trim(Canshu1)='高权限' then
  begin
    TBTP.Visible:=True;
  end;
end;


function TfrmCaiGouSQInPut.SaveData():Boolean;
var
  maxno,FSKID:string;
begin

  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CaiGou_SQ where SQID='''+Trim(Order_Sub.fieldbyname('SQID').AsString)+'''');
          Open;
        end;
        FSKID:=Trim(ADOTemp.fieldbyname('SQID').AsString);
        if Trim(FSKID)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'SQ','CaiGou_SQ',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取采购申请登记表流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(FSKID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from CaiGou_SQ where SQID='''+Trim(FSKID)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FSKID)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('SQID').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'CaiGou_SQ',0);
          FieldByName('FactoryNo').Value:=Trim(Order_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('PBID').Value:=Trim(Order_Sub.fieldbyname('PBID').AsString);
          FieldByName('PXID').Value:=Trim(Order_Sub.fieldbyname('PXID').AsString);
          FieldByName('MXPBID').Value:=Trim(Order_Sub.fieldbyname('MXPBID').AsString);
          FieldByName('SQType').Value:='纱线';
          if Trim(FSKID)='' then
          begin
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            if Trim(ZFormName)='可修改' then
            begin
              FieldByName('Editer').Value:=Trim(DName)+'高权限修改';
            end else
            begin
              FieldByName('Editer').Value:=Trim(DName);
            end;
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update CaiGou_SQ Set SQMoney=SQQty*SQPrice where SQID='''+Trim(maxno)+'''');
          ExecSQL;
        end;
        if Trim(Order_Sub.fieldbyname('SQQtyUpHZ').AsString)<>'' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CaiGou_SQ Set QtySXStr=''汇总'' where SQID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end;
        if Trim(ZFormName)='可修改' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set SPName='''+Trim(Order_Sub.fieldbyname('SPName').AsString)+'''');
            sql.Add(',Price='+Trim(Order_Sub.fieldbyname('SQPrice').AsString));
            sql.Add(' where SQID='''+Trim(maxno)+'''');
            sql.Add(' Update YF_Money_CR Set P_CodeName='''+Trim(Order_Sub.fieldbyname('SPName').AsString)+'''');
            sql.Add(',Price='+Trim(Order_Sub.fieldbyname('SQPrice').AsString));
            sql.Add(' where SQID='''+Trim(maxno)+'''');
            sql.Add(' Update YF_Money_CR Set Money=Price*Qty');
            sql.Add(' where SQID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end;
        Edit;
        FieldByName('SQID').Value:=Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmCaiGouSQInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('SQDate',null,[]) then
  begin
    Application.MessageBox('申请日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('SQPerson',null,[]) then
  begin
    Application.MessageBox('申请人不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('SPType',null,[]) then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('SPName',null,[]) then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  {if Order_Sub.Locate('SPSpec',null,[]) then
  begin
    Application.MessageBox('规格不能为空!','提示',0);
    Exit;
  end;  }
  {if Order_Sub.Locate('PinPai',null,[]) then
  begin
    Application.MessageBox('品牌不能为空!','提示',0);
    Exit;
  end;}
  {if Order_Sub.Locate('FactoryName',null,[]) then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end; }
  {if Order_Sub.Locate('SQQty',null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;  }
  {if Order_Sub.Locate('SQPrice',null,[]) then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end; }
  if Order_Sub.Locate('SQQtyUp;SQQtyUpHZ', VarArrayOf([null, null]), [loPartialKey])  then
  begin
    if Order_Sub.FieldByName('SQQtyHZ').AsString<>'' then
    begin
      Application.MessageBox('汇总数量上限%不能为空!','提示',0);
      Exit;
    end else
    begin
      Application.MessageBox('数量上限%不能为空!','提示',0);
      Exit;
    end;
    Application.MessageBox('数量上限不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('SQQty;SQQtyHZ', VarArrayOf([null, null]), [loPartialKey])  then
  begin
    if Order_Sub.FieldByName('SQQtyUpHZ').AsString<>'' then
    begin
      Application.MessageBox('汇总数量不能为空!','提示',0);
      Exit;
    end else
    begin
      Application.MessageBox('数量不能为空!','提示',0);
      Exit;
    end;

  end;
  {if Order_Sub.Locate('SQQtyUp',null,[]) then
  begin
    Application.MessageBox('数量上限不能为空!','提示',0);
    Exit;
  end; }
  if Trim(Canshu1)<>'高权限' then
  begin
    if Trim(Order_Sub.fieldbyname('ConNO').AsString)='' then
    begin
      Order_Sub.EnableControls;
      Application.MessageBox('白坯单号不能为空!','提示',0);
      Exit;
    end;
  end;

  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not eof do
    begin
      if Trim(Order_Sub.fieldbyname('SPType').AsString)<>'购纱' then
      begin
        if Trim(Order_Sub.fieldbyname('ConNO').AsString)='' then
        begin
          Order_Sub.EnableControls;
          Application.MessageBox('白坯单号不能为空!','提示',0);
          Exit;
        end;
      end;
      next;
    end;
  end;
  Order_Sub.EnableControls; 
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
   // ModalResult:=1;
  end;

end;

procedure TfrmCaiGouSQInPut.ToolButton3Click(Sender: TObject);
var
  FSQDate:TDate;
begin
  FSQDate:=SGetServerDate(ADOQueryTemp);
  try
    frmClothSCHZMXListSel:=TfrmClothSCHZMXListSel.Create(Application);
    with frmClothSCHZMXListSel do
    begin
      if ShowModal=1 then
      begin
        with frmClothSCHZMXListSel.CDS_YuanLiao do
        begin
          while Locate('SSel',True,[]) do
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('ConNo').Value:=Trim(frmClothSCHZMXListSel.ConNo1.Text);
              FieldByName('SPName').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('YCLName').Value;
              FieldByName('SPSpec').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('YCLSpec').Value;
//              FieldByName('SPColor').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('SXColor').Value;
              FieldByName('PinPai').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('PinPai').Value;
              FieldByName('SQQty').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('YLQty').Value;
              FieldByName('FactoryName').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('GYSName').Value;
              FieldByName('PBID').Value:=frmClothSCHZMXListSel.CDS_YuanLiao.FieldByName('PBID').Value;
              FieldByName('SQQtyUnit').Value:='Kg';
              FieldByName('SQPerson').Value:=Trim(DName);
              FieldByName('SQDate').Value:=FSQDate;
              //Post;
            end;
            Delete;
          end;
        end;
//        with frmClothSCHZMXListSel.CDS_PBMX do
//        begin
//          while Locate('SSel',True,[]) do
//          begin
//            with Self.Order_Sub do
//            begin
//              Append;
//              FieldByName('ConNo').Value:=Trim(frmClothSCHZMXListSel.ConNo1.Text);
//              FieldByName('SPName').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('YCLName').Value;
//              FieldByName('SPSpec').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('YCLSpec').Value;
//              FieldByName('PinPai').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('PinPai').Value;
//              FieldByName('SQQty').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('YLQty10').Value;
//              FieldByName('FactoryName').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('GYSName').Value;
//              FieldByName('MXPBID').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('PBID').Value;
//              FieldByName('PXID').Value:=frmClothSCHZMXListSel.CDS_PBMX.FieldByName('PXID').Value;
//              FieldByName('SQQtyUnit').Value:='Kg';
//              FieldByName('SQPerson').Value:=Trim(DName);
//              FieldByName('SQDate').Value:=FSQDate;
//              //Post;
//            end;
//            Delete;
//          end;
//        end;
      end;
    end;
  finally
    frmClothSCHZMXListSel.Free;
  end;

end;

procedure TfrmCaiGouSQInPut.ToolButton4Click(Sender: TObject);
var
  FWorker,FDate:String;
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('SQID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CaiGou_SQ where SQId='''+Trim(Order_Sub.fieldbyname('SQId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmCaiGouSQInPut.v1Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXYCLName';
      flagname:='纱线名称';
      ViewFlag:=True;
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXSPSpec';
      flagname:='纱线规格';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPSpec').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column10PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXSPCF';
      flagname:='纱线成分';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPCF').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column12PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXPinPai';
      flagname:='纱线品牌';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('PinPai').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column14PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin

  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXSQPerson';
      flagname:='申请人';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SQPerson').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.TBTPClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty=False then
  begin
    CopyAddRowCDS(Order_Sub);
    with Order_Sub do
    begin
      Edit;
      FieldByName('SQId').Value:=null;
      FieldByName('ConNo').Value:=null;
      FieldByName('PBID').Value:=null;
//      post;
    end;
  end
  else
  begin
    with Order_Sub do
    begin
      Append;
      FieldByName('SQQtyUnit').Value:='Kg';
      FieldByName('SQPerson').Value:=Trim(DName);
      FieldByName('SQDate').Value:=SGetServerDate(ADOQueryTemp);
//      Post;
    end;
  end;

end;

procedure TfrmCaiGouSQInPut.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Trim(ZFormName)<>'可修改' then
  begin
    if Trim(Order_Sub.fieldbyname('ConNo').AsString)='' then
    begin
      v1Column4.Options.Editing:=True;
      v1Column6.Options.Editing:=True;
    end else
    begin
      v1Column4.Options.Editing:=False;
      v1Column6.Options.Editing:=False;
    end;
  end else
  begin
    v1Column4.Options.Editing:=True;
  end;

end;

end.
