unit U_XiShouInPut;

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
  TfrmXiShouInPut = 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
  frmXiShouInPut: TfrmXiShouInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_KHListSelJJ,U_XiaoShouDataOldList;

{$R *.dfm}

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

procedure TfrmXiShouInPut.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;
  end;
end;



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

function TfrmXiShouInPut.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('CRType').Value:='老系统销售';
      FieldByName('CRQtyFlag').Value:=-1;
      RTSetsavedata(ADOQueryCmd,'CK_YCL_CR_Main',Panel2,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;
        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 TfrmXiShouInPut.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('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;



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



procedure TfrmXiShouInPut.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;
  if Trim(FactoryName.Text)<>'' then
  begin
    Button1.Click;
  end;
end;

procedure TfrmXiShouInPut.Button1Click(Sender: TObject);
begin
  if Trim(FactoryName.Text)='' then
  begin
    Application.MessageBox('客户不能为空！','提示',0);
    Exit;
  end;
  try
    frmXiaoShouDataOldList:=TfrmXiaoShouDataOldList.Create(Application);
    with frmXiaoShouDataOldList do
    begin
      FKHName:=Trim(Self.FactoryName.Text);
      if ShowModal=1 then
      begin
        Self.CKNO.Text:=Trim(frmXiaoShouDataOldList.Order_Main.fieldbyname('CKOrdNo').AsString);
        Self.CRTime.Date:=frmXiaoShouDataOldList.Order_Main.fieldbyname('CRTime').AsDateTime;
      end;
    end;
  finally
    frmXiaoShouDataOldList.Free;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.CRTime,AA.CKOrdNo,AA.QtyUnit,AA.JQty,AA.HSQty,JM.ConNo,JM.CustomerNoName,SPName=JM.MPRTCodeName ');
    sql.Add(',SPColor=JS.PRTColor,SPMF=JM.MPRTMF,SPKZ=JS.PRTKZ,SPColorNo=JS.SOrdDefStr1,SPHX=JS.PRTHX,Note=SOrdDefNote8 ');
    sql.Add(',SPID=SOrdDefNote8');
    sql.Add(',Case when isnull(JS.PriceUnit,'''')=''人民币'' then ''￥''  ');
    sql.Add(' when isnull(PriceUnit,'''')=''RMB'' then ''￥''  ');
    sql.Add(' when isnull(PriceUnit,'''')='''' then ''￥''  ');
    sql.Add(' else ''$'' end as BZType ');
    sql.Add(',Price=JS.PRTPrice,HZFee=isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
    sql.Add(',Money=Cast(JS.PRTPrice*AA.HSQty as decimal(18,2))+isnull(JS.SOrdqty1,0)+isnull(JS.SOrdqty2,0)+isnull(JS.SOrdqty3,0)+isnull(JS.SOrdqty4,0)');
    sql.Add('from (');
    sql.Add(' select MainId,SubId,CRTime,CKOrdNo,QtyUnit,JQty=Count(*),HSQty=sum(Qty) ');
    sql.Add(' from CK_BanCP_CR A  ');
    sql.Add(' where isnull(A.CKOrdNo,'''')='''+Trim(CKNO.Text)+'''');
    sql.Add(' and isnull(A.CKOrdNo,'''')<>'''' ');
    sql.Add(' and not exists(select * from CK_YCL_CR_Main CM where isnull(CM.CKNO,'''')=isnull(A.CKOrdNo,'''') )');
    sql.Add('Group by MainId,SubId,CRTime,CKOrdNo,QtyUnit)AA');
    sql.Add('inner join JYOrder_Sub JS on AA.SubId=JS.SubId ');
    sql.Add('inner join JYOrder_Main JM on JS.MainId=JM.MainId ');

    sql.Add('Union All ');

    sql.Add('select CRTime,CKOrdNo=InOutNo,QtyUnit,JQty=Sum(RollNum),HSQty=Sum(Qty),ConNo,CustomerNoName=CustName');
    sql.Add(',SPName=P_CodeName,SPColor=P_Color,SPMF=Cast(P_MF as varchar(20)),SPKZ=Cast(P_KZ as varchar(20)),SPColorNo=P_ColNo,SPHX=P_HX');
    sql.Add(',Note=Cast(Null as varchar(200)),SPID=Cast(Null as varchar(200)),BZType=''￥'' ');
    sql.Add(',Price,HZFee=0.00,Money=sum(Money)');
    sql.Add(' from CP_InOut1 A ');
    sql.Add(' where  isnull(A.InOutNo,'''')='''+Trim(CKNO.Text)+'''');
    SQL.Add(' and CRType=''销售出库'' ');
    sql.Add('Group by CRTime,InOutNo,QtyUnit,ConNo,CustName,P_CodeName,P_Color,P_MF,P_KZ,P_ColNo,P_HX,Price');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;

end.
