unit U_BpSmCk;

interface

uses
  Windows, Messages, SysUtils,StrUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin,
  cxButtonEdit, cxDropDownEdit, DBClient, ADODB, cxGridCustomPopupMenu,
  cxGridPopupMenu, BtnEdit, Menus, cxLookAndFeelPainters, cxButtons,
  cxCheckBox, cxCalendar;

type
  TfrmBpSmCk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    FirstName: TBtnEditA;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    orderNO: TBtnEditA;
    Label6: TLabel;
    PRTCodeName: TEdit;
    Label7: TLabel;
    PRTColor: TEdit;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    Label8: TLabel;
    InoutNo: TEdit;
    GroupBox1: TGroupBox;
    Panel3: TPanel;
    GroupBox2: TGroupBox;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label9: TLabel;
    MJID: TEdit;
    CDS_CP: TClientDataSet;
    DS_CP: TDataSource;
    CDS_Inout: TClientDataSet;
    DS_inout: TDataSource;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1SSel: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1pbRollNum: TcxGridDBColumn;
    v1pbQty: TcxGridDBColumn;
    v1rkRollNum: TcxGridDBColumn;
    v1rkqty: TcxGridDBColumn;
    v1ckRollNum: TcxGridDBColumn;
    v1ckQty: TcxGridDBColumn;
    v1kcRollNum: TcxGridDBColumn;
    v1kcQty: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel4: TPanel;
    cxButton1: TcxButton;
    Panel5: TPanel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ToCustName: TBtnEditA;
    Label10: TLabel;
    custName: TBtnEditA;
    Label11: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FirstNameBtnClick(Sender: TObject);
    procedure orderNOBtnClick(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Tv3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToCustNameBtnClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
  private
    fcrId:integer;
    TblCprk:string;
    procedure InitGrid();
    function savedate():Boolean;
    procedure SetComboBox();
    function YSData(Order_Main10:TClientDataSet):Boolean;
    procedure DoCRkNum();
    procedure CreateTable();
    procedure DropTable();
    { Private declarations }
  public
    fkeyNO:string;
    FType:integer;
    fCKName:string;
    { Public declarations }
  end;

var
  frmBpSmCk: TfrmBpSmCk;

implementation
 uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_Bpkclist, U_ProductOrderNewList;
{$R *.dfm}
procedure TfrmBpSmCk.DropTable();
var
  strSQL:string;
begin
 // TblCprk := '[##CP_INOut123' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk;
	with adoqueryTmp do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;
procedure TfrmBpSmCk.CreateTable();
var
  strSQL:string;
begin
  TblCprk := '[##CP_INOut123' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk + #13
			    + 'CREATE TABLE ' + TblCprk + ' ('
			    + 'MainID VARCHAR(20),'
          + 'subID  VARCHAR(20))'#13;
	with adoqueryTmp do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;
procedure TfrmBpSmCk.DocRkNum();
var
  strsql:string;
begin
  if cds_Cp.IsEmpty then exit;
  strsql:='';

  with adoqueryTmp do
	begin
    Close;
    SQL.Clear;
    SQL.Add('delete from '+TblCprk);
    execsql;
  end;
  with cds_Cp do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      with adoqueryTmp do
      begin
        Close;
	   	  SQL.Clear;
	   	  SQL.Add(' insert into '+TblCprk+'(MainID) values( ');
        SQL.Add(' '+quotedstr(trim(CDS_CP.fieldbyname('mainID').AsString)));
        sql.add(')');
	   	  execsql;
      end;
      next;
    end;
    EnableControls;
  end;
  with adoqueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CP_KC A');
    sql.Add('inner join '+TblCprk+' B on B.mainID=A.MainID ');
    sql.Add('where A.Qty<>0');
    open;
  end;
    SCreateCDS20(adoqueryTmp,CDS_Inout);
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
end;
function TfrmBpSmCk.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou,cust:String;
begin
  Result:=False;
  with Order_Main10 do
  begin

    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from JYOrder_main where MainID='''+Trim(Order_Main10.fieldbyname('MainID').AsString)+'''');
      Open;
      cust:=FieldbyName('customerNOName').AsString;
    end;
    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from JYOrder_sub where MainID='''+Trim(Order_Main10.fieldbyname('MainID').AsString)+'''');
      sql.Add(' and subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
      Open;
      Price:=FieldbyName('PrtPrice').AsString;
    end;

    with ADOQueryTmp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(cust)+''' and YFDefFlag1=0 ');
      Open;
    end;
    if not ADOQueryTmp.IsEmpty then
    begin
      CRID:=ADOQueryTmp.fieldbyname('CRID').AsString;
    end
    else
    begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(cust);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
    end;
    with ADOQueryTmp do
    begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(Order_Main10.fieldbyname('MainID').AsString)+'''');
        sql.Add(' and subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
        sql.Add(' and YFName=''样品销售'' ');
        Open;
    end;
      if ADOQueryTmp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取坯布应付最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('MainId').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(cust);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',date()));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=Price;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(cust);
          FieldByName('YFName').Value:='样品销售';
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
          FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from BP_INout A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD and A.CRType=''样品零售''  )');
        sql.Add(',PS=(select isnull(count(*),0) from BP_INout A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD and A.CRType=''样品零售''  )');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;
  end;
  Result:=True;
end;
procedure TfrmBpSmCk.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPCK'' ');
    if trim(fCKName)<>'' then
    sql.Add('and zdyName='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count>0 then ckName.ItemIndex:=0;

  CRType.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPCKTYPE''');
    if trim(fCKName)<>'' then
    sql.Add('and note='+quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count>0 then CRType.ItemIndex:=0;
end;
function TfrmBpSmCk.savedate():Boolean;
var
  fkcID:integer;
  Filler:string;
  FillTime:TdateTime;
  maxno:string;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FType=0 then
    begin
      Filler:=DName;
      FillTime:=SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CP_InOut');
        sql.Add('where CRNO='+quotedstr(trim(fkeyNO)));
        open;
        Filler:=adoqueryTmp.fieldbyname('Filler').AsString;
        FillTime:=adoqueryTmp.fieldbyname('FillTime').AsDateTime;
      end;
    end;

  {  with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update CP_kc set RollNum=A.RollNum + B.RollNum, Qty=A.Qty + B.Qty ');
      sql.Add('from  CP_kc  A');
      sql.Add('inner join CP_INout B on B.kcid=A.kcid');
      sql.Add('where B.CRNO='+quotedstr(trim(fkeyNO)));
      execsql;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CP_INout');
      sql.Add('where CRNO='+quotedstr(trim(fkeyNO)));
      execsql;
    end;  }
  //  CDS_Main.DisableControls;
 //   CDS_Main.First;
    with  CDS_Main do
    begin
    //  while not eof do
    //  begin

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CP_INout');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value:=trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRID').Value:=fcrid;
        ADOQueryCmd.FieldByName('CKName').Value:=trim(CKName.Text);
        ADOQueryCmd.FieldByName('InoutNO').Value:=trim(InoutNO.Text);
        ADOQueryCmd.FieldByName('custNo').Value:=trim(custName.TxtCode);
        ADOQueryCmd.FieldByName('custName').Value:=trim(custName.Text);
        ADOQueryCmd.FieldByName('TocustNo').Value:=trim(TocustName.TxtCode);
        ADOQueryCmd.FieldByName('TocustName').Value:=trim(TocustName.Text);
        ADOQueryCmd.FieldByName('mainID').Value:=trim(fieldbyname('MainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value:=trim(CRType.Text);
        ADOQueryCmd.FieldByName('CRFlag').Value:='出库';
        ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('CPTYPE').Value:=trim(fieldbyname('CPTYPE').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value:=trim(fieldbyname('P_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value:=trim(fieldbyname('P_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value:=trim(fieldbyname('P_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value:=trim(fieldbyname('P_Color').AsString);
        ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
        ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value:=fieldbyname('RollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('QtyUnit').AsString);
//        ADOQueryCmd.FieldByName('note').Value:=trim(fieldbyname('note').AsString);
        ADOQueryCmd.FieldByName('Filler').Value:=trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value:=dateTimetostr(FillTime);
   //     ADOQueryCmd.FieldByName('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.Text);
        if FType=1 then
        begin
          ADOQueryCmd.FieldByName('editer').Value:=trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value:=SGetServerDateTime(adoqueryTmp);
        end;
        ADOQueryCmd.FieldByName('kcID').Value:=FieldByName('kcID').AsInteger;
        ADOQueryCmd.Post;

       with ADOQueryCmd do
       begin
         close;
         sql.Clear;
         sql.Add('update CP_kc set RollNum=A.RollNum - B.RollNum, Qty=A.Qty - B.Qty ');
         sql.Add('from  CP_kc  A');
         sql.Add('inner join CP_INout B on B.kcid=A.kcid');
         sql.Add('where B.CRNO='+quotedstr(trim(fkeyNO)));
         sql.Add('and B.CRID='''+inttostr(fcriD)+'''');
         execsql;
       end;
       with ADOQueryCmd do
       begin
         close;
         sql.Clear;
         sql.Add('update CP_kc set RollNum=1');
         sql.Add('from  CP_kc  A');
         sql.Add('where A.RollNum=0 and A.Qty<>0 ');
         execsql;
       end;

    {   if trim(CRType.Text)='样品零售' then
       begin
         if not YSData(CDS_Main) then
         begin
           ADOQueryCmd.Connection.RollbackTrans;
           exit;
         end;
       end;  }
    end;
 //   CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    fcrId:=fcrId+1;
    result:=true;
  except
  //  CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmBpSmCk.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      Close;
      sql.Clear;
      Filtered:=False;
      sql.add('exec P_View_Order_CP :begdate,:enddate,:WSql') ;
      Parameters.ParamByName('begdate').Value:='2012-10-01';
      Parameters.ParamByName('enddate').Value:='2011-10-01';
      Parameters.ParamByName('WSql').Value:=' and 1=2 ';
      Open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_CP);
    SInitCDSData20(adoqueryTmp,CDS_CP);

    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from CP_InOut A');
   //   sql.Add('left join  JYOrder_sub B on B.mainID=A.mainID and B.subID=A.subID ');
      sql.Add('where A.CRNO='+quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Inout);
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date:= CDS_Main.fieldbyname('CRTime').AsDateTime;
      CKName.ItemIndex:=CKName.Items.IndexOf(trim(CDS_Main.fieldbyname('CKName').AsString));
      CRType.ItemIndex:=CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      InoutNO.Text:=trim(CDS_Main.fieldbyname('InoutNO').AsString);
    //  orderNo.Text:=trim(CDS_Main.fieldbyname('orderNo').AsString);
    //  PRTCodeName.Text:=trim(CDS_Main.fieldbyname('PRTCodeName').AsString);
     // PRTColor.Text:=trim(CDS_Main.fieldbyname('PRTColor').AsString);
   //   FirstName.TxtCode:=trim(CDS_Main.fieldbyname('FirstNo').AsString);
   //   FirstName.Text:=trim(CDS_Main.fieldbyname('FirstName').AsString);
    end;
  finally
  end;
end;

procedure TfrmBpSmCk.FormDestroy(Sender: TObject);
begin
  frmBpSmCk:=nil;
end;

procedure TfrmBpSmCk.FormCreate(Sender: TObject);
begin
  CRTime.Date:=DServerDate;
  GroupBox2.Align:=alclient;

  CreateTable();
  fcrID:=1;
end;

procedure TfrmBpSmCk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DropTable();
  writeCxGrid(self.Caption,Tv1,'成品仓库');
  writeCxGrid(self.Caption,Tv2,'成品仓库');
  writeCxGrid(self.Caption,Tv3,'成品仓库');
  Action:=cahide;
end;

procedure TfrmBpSmCk.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption,Tv1,'成品仓库');
  readCxGrid(self.Caption,Tv2,'成品仓库');
  readCxGrid(self.Caption,Tv3,'成品仓库');
  SetComboBox();
  InitGrid();
  if fType=0 then
  begin
    if GetLSNo(ADOQueryCmd,fkeyNo,'CK','CP_InOut',3,1)=False then
    begin
      Application.MessageBox('取最大号失败!','提示',0);
      Exit;
    end;
    inoutNo.Text:='C'+Trim(RightBStr(fkeyNo,Length(fkeyNo)-2));
  end;
  MJID.SetFocus;
end;

procedure TfrmBpSmCk.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then exit;
  CRTime.SetFocus;
  if  trim(CRType.Text)='' then
  begin
    application.MessageBox('出库类型不能为空！','提示信息',0);
    exit;
  end;
  if  trim(inoutNo.Text)='' then
  begin
    application.MessageBox('出库单号不能为空！','提示信息',0);
    exit;
  end;

  if savedate() then
  begin
    InitCDSToCDS(CDS_Main,CDS_Inout);
    CDS_Main.Delete;
    with CDS_Inout do
    begin
      edit;
      fieldbyname('SDefNote').Value:='出库成功';
      post;
    end;
  end
  else
  begin
   //  InitCDSToCDS(CDS_Main,CDS_Inout);
    with CDS_Inout do
    begin
      append;
      fieldbyname('MJID').Value:=CDS_Main.fieldbyname('MJID').AsString;
      fieldbyname('SDefNote').Value:='出库失败';
      post;
    end;
  end;
end;

procedure TfrmBpSmCk.v1P_CodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Cloth';
      flagname:='坯布名称';
      if ShowModal=1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.FieldByName('P_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBpSmCk.ToolButton2Click(Sender: TObject);
begin
  if  CDS_Main.IsEmpty then exit;
  CDS_Main.Delete;
end;

procedure TfrmBpSmCk.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBpSmCk.FirstNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Factory';
      flagname:='入库地点';
      MainType:='RanFactory';
      if ShowModal=1 then
      begin
        FirstName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        FirstName.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBpSmCk.orderNOBtnClick(Sender: TObject);
begin
 {  frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
   with  frmProductOrderNewList do
   begin
     DParameters1:='高权限';
     if showmodal=1 then
     begin
       self.fmainID:=trim(Order_Main.fieldbyname('MainID').AsString);
       self.fsubID:=trim(Order_Main.fieldbyname('subID').AsString);
       self.orderNo.Text:=trim(Order_Main.fieldbyname('orderNo').AsString);
       self.PRTCodeName.Text:=trim(Order_Main.fieldbyname('PRTCodeName').AsString);
       self.PRTColor.Text:=trim(Order_Main.fieldbyname('PRTColor').AsString);
     end;
     free;
   end;  }
end;

procedure TfrmBpSmCk.cxButton1Click(Sender: TObject);
begin
  frmProductOrderNewList:=TfrmProductOrderNewList.create(self);
  with frmProductOrderNewList do
  begin
   // fType:=10;
  //  fckName:=self.fckName;
    if showmodal=1 then
    begin
      self.CDS_CP.EmptyDataSet;
      Order_Main.DisableControls;
      Order_Main.First;

      while not Order_Main.Eof  do
      begin

        if (order_main.fieldbyname('ssel').AsBoolean) and
          (not self.CDS_CP.Locate('MprtCode',order_main.fieldbyname('MprtCode').AsString,[])) then
        begin
          InitCDSToCDS(order_main,self.CDS_CP);
        end;
        Order_Main.Next;
      end;
      Order_Main.EnableControls;
    end;
    free;
  end;
  DocRkNum();
  MJID.SetFocus;
end;

procedure TfrmBpSmCk.MJIDKeyPress(Sender: TObject; var Key: Char);
var
  IsCz:boolean;
begin
  IsCz:=false;
  if key=#13 then
  begin
    if trim(MJID.Text)='' then exit;

    if CDS_Main.Locate('MJID',trim(MJID.Text),[]) then
    begin
      TBsave.Click;
    end
    else
    begin
      with CDS_Inout do
      begin
        append;
        fieldbyname('MJID').Value:=trim(MJID.Text);
        fieldbyname('SDefNote').Value:='条码错误';
        post;
      end;
    end;
    MJID.text:='';
    MJID.SetFocus;
  end;
end;

procedure TfrmBpSmCk.Tv3CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  i:integer;
begin
  i:=tv3.GetColumnByFieldName('SDefNote').Index;
  if (AViewInfo.GridRecord.Values[i]='出库失败') or (AViewInfo.GridRecord.Values[i]='条码错误') then
  ACanvas.Brush.Color:=clred;

end;

procedure TfrmBpSmCk.ToCustNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoName';
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        ToCustName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        ToCustName.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBpSmCk.custNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoName';
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        CustName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustName.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
        ToCustName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        ToCustName.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
