unit U_XiShouDataOldList;

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
  TfrmXiShouDataOldList = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: 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;
    Button1: TButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column7: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FactoryNameBtnClick(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    { Public declarations }
  end;

var
  frmXiShouDataOldList: TfrmXiShouDataOldList;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_GYSList,U_SCPerson,U_QCRKJLList,U_RSColorBig,
  U_RTPTColor,U_KHListSelJJ,U_ZHCPBigTP,U_LLRKMXInPut;

{$R *.dfm}

procedure TfrmXiShouDataOldList.TBCloseClick(Sender: TObject);
begin
  Close;
  
end;

procedure TfrmXiShouDataOldList.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,Panel1,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);
end;



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

function TfrmXiShouDataOldList.SaveData():Boolean;
var
  FCRID,Maxno,FFSPID,MMaxNo:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOQueryCmd,MMaxNo,'SM','CK_YCL_CR_Main',3,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取成品出库主编号失败!','提示',0);
        Exit;
      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('CRQtyFlag').Value:=-1;
      RTSetsavedata(ADOQueryCmd,'CK_YCL_CR_Main',Panel1,2);
      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;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select MXKCQty=sum(HSQty*CRQtyFlag) from CK_YCL_CR A inner join CK_YCL_CR_Main B on A.SMID=B.SMID where B.CKName=''成品仓库'' ');
          sql.Add(' and isnull(SPName,'''')='''+Trim(CDS_Sub.fieldbyname('SPName').AsString)+'''');
          sql.Add(' and isnull(QtyUnit,'''')='''+Trim(CDS_Sub.fieldbyname('QtyUnit').AsString)+'''');
          sql.Add(' and isnull(SPSpec,'''')='''+Trim(CDS_Sub.fieldbyname('SPSpec').AsString)+'''');
          sql.Add(' and ZFFlag=0 ');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!','提示',0);
          Exit;
        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 TfrmXiShouDataOldList.TBSaveClick(Sender: TObject);

begin
  ToolBar1.SetFocus;
  if Trim(KHName.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('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

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

procedure TfrmXiShouDataOldList.FactoryNameBtnClick(Sender: TObject);
begin
  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;

end.
