unit U_XiShouInPutWKC;

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,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGroupBox, cxCheckBox,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,IniFiles;

type
  TfrmXiShouInPutWKC = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    DataSource1: TDataSource;
    CDS_Sub: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label21: TLabel;
    Label26: TLabel;
    Label19: TLabel;
    Panel3: TPanel;
    labMYType: TLabel;
    Panel2: TPanel;
    Label13: TLabel;
    Label27: TLabel;
    CRTime: TDateTimePicker;
    FactoryName: TBtnEditA;
    CKNO: TEdit;
    Label22: TLabel;
    Panel16: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column7: TcxGridDBColumn;
    Button1: TButton;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    { Public declarations }
  end;

var
  frmXiShouInPutWKC: TfrmXiShouInPutWKC;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_KHListSelJJ,U_XiaoShouDataOldList,U_ZHCPListSel;


{$R *.dfm}

procedure TfrmXiShouInPutWKC.TBCloseClick(Sender: TObject);
begin
  Close;
  ReadCxGrid('老系统销售码单登记',Tv1,'贸易生产管理');
end;

procedure TfrmXiShouInPutWKC.InitData();
begin
  ReadCxGrid('老系统销售码单登记',Tv1,'贸易生产管理');
  CRTime.Date:=SGetServerDate(ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_YCL_CR_Main  ');
    sql.Add(' where SMID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel2,2);
  FactoryName.Text:=Trim(ADOQueryTemp.fieldbyname('FactoryName').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_YCL_CR A');
    sql.Add(' where SMID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
  if TBSave.Visible=False then
  begin
    FactoryName.Enabled:=False;
    FactoryName.Color:=clMenu;
    Button1.Visible:=False;
    Tv1.OptionsSelection.CellSelect:=False;
    CRTime.Enabled:=False;
    CRTime.Color:=clMenu;
  end;
end;



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

function TfrmXiShouInPutWKC.SaveData():Boolean;
var
  FCRID,Maxno,FFSPID,MMaxNo:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FMainId)='' then
    begin
      if Trim(Label21.Caption)='无库存销售' then
      begin
        if GetLSNo(ADOQueryCmd,MMaxNo,'WK','CK_YCL_CR_Main',3,1)=False then
        begin
          Result:=False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取销售码单登记号失败!','提示',0);
          Exit;
        end;
      end else
      if Trim(Label21.Caption)='免单销售' then
      begin
        if GetLSNo(ADOQueryCmd,MMaxNo,'MD','CK_YCL_CR_Main',3,1)=False then
        begin
          Result:=False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取销售码单登记号失败!','提示',0);
          Exit;
        end;
      end;

    end else
    begin
      MMaxNo:=Trim(FMainId);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_YCL_CR_Main where SMID='''+Trim(MMaxNo)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('Editer').Value:=Trim(DName);
      end;
      FieldByName('SMID').Value:=Trim(MMaxNo);
      FieldByName('CKName').Value:='销售出库';
      FieldByName('CRFlag').Value:='出库';
      FieldByName('CRType').Value:=Label21.Caption;
      FieldByName('CRQtyFlag').Value:=-1;
      RTSetsavedata(ADOQueryCmd,'CK_YCL_CR_Main',Panel2,2);
      FieldByName('CKNO').Value:=Trim(MMaxNo);
      FieldByName('SPCode').Value:=CDS_Sub.fieldbyname('SPCode').Value;
      Post;
    end;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_YCL_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
          open;
        end;
        FFSPID:=Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FFSPID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'SC','CK_YCL_CR',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取成品出库子编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(FFSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_YCL_CR where SPID='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('SMID').Value:=Trim(MMaxno);
          FieldByName('SPID').Value:=Trim(Maxno);
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_YCL_CR',2);
          if Trim(FFSPID)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        Edit;
        FieldByName('SPID').Value:=Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    FMainId:=Trim(MMaxNo);
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;

end;

procedure TfrmXiShouInPutWKC.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(FactoryName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('SPName',null,[])=True then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('HSQty',null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('JQty',null,[])=True then
  begin
    Application.MessageBox('匹数不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;



Procedure TfrmXiShouInPutWKC.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ModalResult:=1;
end;



procedure TfrmXiShouInPutWKC.FactoryNameBtnClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty=False then Exit;
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        Self.FactoryName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.FactoryName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

procedure TfrmXiShouInPutWKC.Button1Click(Sender: TObject);
begin
  try
    frmZHCPListSel:=TfrmZHCPListSel.Create(Application);
    with frmZHCPListSel do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FieldByName('SPCode').Value:=frmZHCPListSel.Order_Main.fieldbyname('ZIID').Value;
          FieldByName('SPMF').Value:=frmZHCPListSel.Order_Main.fieldbyname('ZIMF').Value;
          FieldByName('SPKZ').Value:=frmZHCPListSel.Order_Main.fieldbyname('ZIKZ').Value;
          FieldByName('SPName').Value:=frmZHCPListSel.Order_Main.fieldbyname('ZIName').Value;
          FieldByName('QtyUnit').Value:='M';
          Post;
        end;
      end;
    end;
  finally
    frmZHCPListSel.Free;
  end;
  
end;

end.
