unit U_CPRK;

interface

uses
  Windows, Messages,strutils, SysUtils, Variants, cxTextEdit,Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, ToolWin, StdCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, BtnEdit, cxContainer,
  cxCurrencyEdit, RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset;

type
  TfrmCPRK = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTIME: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1BCKZ: TcxGridDBColumn;
    v1BCFK: TcxGridDBColumn;
    v1ClothQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1ClothUnit: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    CDS_main: TClientDataSet;
    v1C_Color: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1MJId: TcxGridDBColumn;
    Label2: TLabel;
    kcKw: TBtnEditA;
    Edit1: TEdit;
    Label3: TLabel;
    v1Column8: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    CRType: TComboBox;
    Label16: TLabel;
    Label4: TLabel;
    CustomerNoName: TBtnEditC;
    Label5: TLabel;
    Edit2: TEdit;
    ADOQueryTemp: TADOQuery;
    Label6: TLabel;
    note: TEdit;
    SYRName: TComboBox;
    Label25: TLabel;
    Label7: TLabel;
    TbaoFee: TcxCurrencyEdit;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    TBPrint: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1C_ColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure kcKwBtnClick(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v1C_ColorPropertiesEditValueChanged(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
  private
    procedure IntGrid();
    function savedate():boolean;
    function YSData(crkNo:string):Boolean;
    function YFData(crkNo:string):Boolean;
    function YSData_other(crkNo:string;FYFName:string;Fmoney:double):Boolean;
    { Private declarations }
  public
    fkeyNo:string;
    fBCID:string;
    fFlag:integer;
    fCKName:string;
    { Public declarations }
  end;

var
  frmCPRK: TfrmCPRK;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachCP, U_ZdyAttachment,
  U_CPKWListSel;
{$R *.dfm}
function TfrmCPRK.YSData_other(crkNo:string;FYFName:string;Fmoney:double):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou,customName:String;
begin
  Result:=False;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CustomerNoName.text)+'''');
      sql.Add(' and YFDefFlag1=0 ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      CRID:=ADOQueryTemp.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(CustomerNoName.text);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from YF_Money_CR ');
      sql.Add('where MainID='''+Trim(crkNO)+'''');
      sql.Add('and YFName='''+trim(FYFName)+''' ');
      Open;
    end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',4,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(crkNO);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(CustomerNoName.text);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Qty').Value:=0;
          FieldByName('Price').Value:=0;
          FieldByName('HuiLv').Value:=1;
          FieldByName('money').Value:=fmoney;
          FieldByName('BBmoney').Value:=fmoney;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:='M';
          FieldByName('ComTaiTou').Value:=Trim(CustomerNoName.text);
          FieldByName('YFName').Value:=trim(fyfName);
          FieldByName('P_CodeName').Value:='';
          FieldByName('P_Color').Value:='';
          FieldByName('MainId').Value:=trim(crkNO);
          FieldByName('subID').Value:='';
          FieldByName('SYRName').Value:=Trim(SYRName.Text);
          FieldByName('YFDefFlag1').Value:=0;
          FieldByName('status').Value:='1';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+trim(YFID)+'''');
          Open;
          edit;
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
          post;
        end;
      end;
  Result:=True;
end;
function TfrmCPRK.YFData(crkNo:string):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;

     with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName=''建威纺织''');
        sql.Add(' and YFDefFlag1=1 ');
        Open;
      end;
    if not ADOQueryTemp.IsEmpty then
    begin
      CRID:=ADOQueryTemp.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:='建威纺织';
          FieldByName('YFDefFlag1').Value:=1;
          FieldByName('ZdyStr1').Value:='应付付';
          Post;
        end;
     end;

    with ADOQueryCmd do
    begin
        Close;
        sql.Clear;
        sql.Add('delete from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(crkNO)+'''');
        sql.Add(' and YFName=''退货入库'' ');
        execsql;
    end;
    with ADOQueryTemp do
    begin
        Close;
        sql.Clear;
        sql.Add('select C_CodeName,C_Color,SUM(RollNum) as RollNum,SUM(Qty) as Qty from CK_BanCP_CR  ');
        sql.Add(' where CKOrdNo='''+Trim(crkNO)+'''');
        sql.Add(' and CRType=''退货入库'' ');
        sql.Add(' group by C_CodeName,C_Color  ');
        Open;
    end;
    while not ADOQueryTemp.eof do
    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(crkNO);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应付款登记';
          FieldByName('CRFlag').Value:='应付付';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:='建威纺织';
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTIME.DateTime));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=0;
          FieldByName('PS').Value:=-1 * ADOQueryTemp.fieldbyname('RollNum').AsFloat;
          FieldByName('Qty').Value:=-1 * ADOQueryTemp.fieldbyname('Qty').AsFloat;
          FieldByName('P_CodeName').Value:=ADOQueryTemp.fieldbyname('C_CodeName').AsString;
          FieldByName('P_Color').Value:=ADOQueryTemp.fieldbyname('C_Color').AsString;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:='M';
          FieldByName('ComTaiTou').Value:='建威纺织';
          FieldByName('YFName').Value:='退货入库';
          FieldByName('MainId').Value:=Trim(crkNO);
          FieldByName('subID').Value:='';
          FieldByName('YFDefFlag1').Value:=1;
     //     FieldByName('status').Value:='1';
          Post;
        end;
        ADOQueryTemp.next;
     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;
  Result:=True;
end;
function TfrmCPRK.YSData(crkNo:string):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;

     with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CustomerNoName.Text)+'''');
        sql.Add(' and YFDefFlag1=0 ');
        Open;
      end;
    if not ADOQueryTemp.IsEmpty then
    begin
      CRID:=ADOQueryTemp.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(CustomerNoName.Text);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
     end;

    with ADOQueryCmd do
    begin
        Close;
        sql.Clear;
        sql.Add('delete from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(crkNO)+'''');
        sql.Add(' and YFName=''退货入库'' ');
        execsql;
    end;
    with ADOQueryTemp do
    begin
        Close;
        sql.Clear;
        sql.Add('select C_CodeName,C_Color,SUM(RollNum) as RollNum,SUM(Qty) as Qty from CK_BanCP_CR  ');
        sql.Add(' where CKOrdNo='''+Trim(crkNO)+'''');
        sql.Add(' and CRType=''退货入库'' ');
        sql.Add(' group by C_CodeName,C_Color  ');
        Open;
    end;
    while not ADOQueryTemp.eof do
    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(crkNO);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(CustomerNoName.Text);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTIME.DateTime));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=0;
          FieldByName('PS').Value:=-1 * ADOQueryTemp.fieldbyname('RollNum').AsFloat;
          FieldByName('Qty').Value:=-1 * ADOQueryTemp.fieldbyname('Qty').AsFloat;
          FieldByName('P_CodeName').Value:=ADOQueryTemp.fieldbyname('C_CodeName').AsString;
          FieldByName('P_Color').Value:=ADOQueryTemp.fieldbyname('C_Color').AsString;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:='M';
          FieldByName('ComTaiTou').Value:=Trim(CustomerNoName.Text);
          FieldByName('YFName').Value:='退货入库';
          FieldByName('MainId').Value:=Trim(crkNO);
          FieldByName('SYRName').Value:=Trim(SYRName.Text);
          FieldByName('subID').Value:='';
          FieldByName('status').Value:='1';
          Post;
        end;
        ADOQueryTemp.next;
      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;
  Result:=True;
end;
function TfrmCPRK.savedate():boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo:String;
begin
  result:=false;
  ADOQueryCmd.Connection.BeginTrans;

  try
    if fflag=0 then
    begin
      if GetLSNo(ADOQueryCmd,fkeyNo,'RK','CK_BanCP_CR',5,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取成品仓库最大号失败！','提示',0);
        Exit;
      end;
    end;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if trim(Fieldbyname('BCID').AsString)='' then
        begin
          with ADOQueryCmd  do
          begin
            Close;
            sql.Clear;
            SQL.add('Update CK_BanCP_CRID Set CRID=CRID+1');
            sql.Add('select * from CK_BanCP_CRID');
            Open;
          end;
          CRID:=ADOQueryCmd.fieldbyname('CRID').AsInteger;
          if GetLSNo(ADOQueryCmd,MaxCkNo,'JR','CK_BanCP_CR',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
         end
         else
         begin
           CRID:=fieldbyname('CRID').AsInteger;
           MaxCkNo:=fieldbyname('BCID').AsString;
         end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_CR ');
            sql.Add('where BCID='+quotedstr(trim(MaxCkNo)));
            sql.Add('and  CRID='+inttostr(CRID));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;

            IF trim(CRType.Text)='期初入库' then
            begin
              FieldByName('MainId').Value:='CM001';
              FieldByName('SubId').Value:='CS001';
            end
            else
            begin
              FieldByName('MainId').Value:='';
              FieldByName('SubId').Value:='';
            end;
            FieldByName('MainId').Value:='CM001';
            FieldByName('SubId').Value:='CS001';
            FieldByName('C_Code').Value:=Trim(CDS_Main.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(CDS_Main.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(CDS_Main.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(CDS_Main.fieldbyname('C_Color').AsString);
            FieldByName('batchNo').Value:=Trim(CDS_Main.fieldbyname('batchNo').AsString);
            FieldByName('APID').Value:=Trim(CDS_Main.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJId').AsString);
            FieldByName('MJxh').Value:=Trim(CDS_Main.fieldbyname('MJxh').AsString);
            FieldByName('kcKw').Value:=Trim(CDS_Main.fieldbyname('kcKw').AsString);
            FieldByName('RCGangNo').Value:=Trim(CDS_Main.fieldbyname('RCGangNo').AsString);
            FieldByName('BCGangNo').Value:=Trim(CDS_Main.fieldbyname('BCGangNo').AsString);
            FieldByName('BCFK').Value:=CDS_Main.fieldbyname('BCFK').AsFloat;
            FieldByName('BCKZ').Value:=CDS_Main.fieldbyname('BCKZ').AsFloat;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('ckordNo').Value:=Trim(fkeyNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.DateTime);
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRType').Value:=trim(CRType.Text);
            //FieldByName('JTType').Value:=Trim(XJFlag);
            FieldByName('CRID').Value:=CRID;
            FieldByName('KGQty').Value:=CDS_Main.fieldbyname('KGQty').AsFloat;
            FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty').AsFloat;
            FieldByName('RollNum').Value:=CDS_Main.fieldbyname('RollNum').AsFloat;
            FieldByName('QtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
            FieldByName('CPType').Value:=Trim(CDS_Main.fieldbyname('CPType').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
            FieldByName('kcKw').Value:=Trim(kcKw.Text);
          //  fieldbyname('kcKw').Value:=GetKw(ADOQueryTemp,CDS_Main.fieldbyname('C_CodeName').AsString,CDS_Main.fieldbyname('C_Color').AsString);
            FieldByName('ThCust').Value:=Trim(CustomerNoName.Text);
            FieldByName('note').Value:=Trim(CDS_Main.fieldbyname('note').AsString);
            FieldByName('SYRName').Value:=Trim(SYRName.Text);
            FieldByName('ckname').Value:=Trim(fckName);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC ');
            sql.Add('where BCID='+quotedstr(trim(MaxCkNo)));
            sql.Add('and  CRID='+inttostr(CRID));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;
            FieldByName('CRID').Value:=CRID;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
            FieldByName('MJxh').Value:=Trim(CDS_Main.fieldbyname('MJxh').AsString);
            FieldByName('kcKw').Value:=Trim(CDS_Main.fieldbyname('kcKw').AsString);
            FieldByName('KCKGQty').Value:=CDS_Main.fieldbyname('KGQty').AsFloat;
            FieldByName('KCQty').Value:=CDS_Main.fieldbyname('Qty').AsFloat;
            FieldByName('KCRollNum').Value:=CDS_Main.fieldbyname('RollNum').AsFloat;
            FieldByName('KCQtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.DateTime);
            FieldByName('CPType').Value:=Trim(CDS_Main.fieldbyname('CPType').AsString);
            FieldByName('CRType').Value:=trim(CRType.Text);
            FieldByName('C_Code').Value:=Trim(CDS_Main.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(CDS_Main.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(CDS_Main.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(CDS_Main.fieldbyname('C_Color').AsString);
            FieldByName('batchNo').Value:=Trim(CDS_Main.fieldbyname('batchNo').AsString);
            FieldByName('RCGangNo').Value:=Trim(CDS_Main.fieldbyname('RCGangNo').AsString);
            FieldByName('BCGangNo').Value:=Trim(CDS_Main.fieldbyname('BCGangNo').AsString);
           // fieldbyname('kcKw').Value:=GetKw(ADOQueryTemp,CDS_Main.fieldbyname('C_CodeName').AsString,CDS_Main.fieldbyname('C_Color').AsString);
            FieldByName('ckname').Value:=Trim(fckName);

            FieldByName('kcKw').Value:=Trim(kcKw.Text);
            Post;
          end;
       CDS_Main.Next;
      end;
    end;
    CDS_Main.EnableControls;
    IF trim(CRType.Text)='退货入库' then
    begin
      IF trim(CustomerNoName.Text)='广东门市部' then
      begin
        {iF not YFData(fkeyNO) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          exit;
        end;  }
      end
      else
      begin
        iF not YSData(fkeyNO) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          exit;
        end;
      end;

      IF strtofloatdef(trim(TbaoFee.Text),0)<>0 then
      begin
        iF not YSData_other(fkeyNO,'退包费',strtofloatdef(trim(TbaoFee.Text),0)) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          exit;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmCPRK.IntGrid();
begin
  with adoqueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from CK_BanCP_CR A');
    sql.Add(' where ckordNo='+quotedstr(fkeyNO));
    IF trim(fCKName)<>'' then
    sql.Add(' and A.ckName='''+trim(fckName)+''' ');
   // sql.Add('and  BCID='+quotedstr(fBCID));
    open;
  end;
  SCreateCDS20(adoqueryTmp,CDS_main);
  SInitCDSData20(adoqueryTmp,CDS_main);

  if not CDS_main.IsEmpty then
  begin
    CRTIME.DateTime:=CDS_main.fieldbyname('CRTime').AsDateTime;
    kcKw.Text:=CDS_main.fieldbyname('kckw').AsString;
    CRType.ItemIndex:=CRType.Items.IndexOf(trim(CDS_main.fieldbyname('CRType').AsString));
    CustomerNoName.Text:=CDS_main.fieldbyname('thcust').AsString;
  //  note.Text:=CDS_main.fieldbyname('note').AsString;
    SYRName.Text:=CDS_main.fieldbyname('SYRName').AsString;
  end;
end;

procedure TfrmCPRK.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=cahide;
end;

procedure TfrmCPRK.FormDestroy(Sender: TObject);
begin
  frmCPRK:=nil;
end;

procedure TfrmCPRK.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alclient;
  CRTIME.DateTime:=date();
end;

procedure TfrmCPRK.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
 try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal=1 then
      begin
        Self.CDS_main.Edit;
        Self.CDS_main.FieldByName('C_Code').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.CDS_main.FieldByName('C_CodeName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        Self.CDS_main.FieldByName('C_Color').Value:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.CDS_main.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
    self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPRK.ToolButton1Click(Sender: TObject);
var
  maxno:string;
begin
  if GetLSNo(ADOQueryCmd,maxno,'S','APTM',4,1)=False then
  begin
    Application.MessageBox('取最大号失败!','提示',0);
    Exit;
  end;
  with CDS_main do
  begin
    Append;
    fieldbyname('BCID').Value:='';
    fieldbyname('CPTYpe').Value:='正品';
    fieldbyname('MJID').Value:=maxno;
    fieldbyname('MJXH').Value:=1;
    fieldbyname('QtyUnit').Value:='KG';
    Post;
  end;
end;

procedure TfrmCPRK.ToolButton2Click(Sender: TObject);
begin
  if   CDS_main.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from CK_BanCP_CR ');
    sql.Add('where BCID='+quotedstr(trim(CDS_main.fieldbyname('BCID').AsString)));
    sql.Add('and CKOrdNo='+quotedstr(trim(CDS_main.fieldbyname('CKOrdNo').AsString)));
    sql.Add('delete from CK_BanCP_KC ');
    sql.Add('where BCID='+quotedstr(trim(CDS_main.fieldbyname('BCID').AsString)));
    sql.Add('AND crid='+quotedstr(trim(CDS_main.fieldbyname('crid').AsString)));
    execsql;
  end;
  CDS_main.Delete;
end;

procedure TfrmCPRK.FormShow(Sender: TObject);
begin
  SYRName.Items.Clear;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''SYRNAME''');
    open;
    IF not IsEmpty then
    begin
      while not eof do
      begin
        SYRName.Items.Add(trim(fieldbyname('zdyname').AsString));
        next;
      end;
      SYRName.ItemIndex:=0;
    end;
  end;
  IF fFlag=1 then
  begin
    CRType.Enabled:=false;
  //  CustomerNoName.Enabled:=false;
  end;
  IntGrid();
end;

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

procedure TfrmCPRK.TBSaveClick(Sender: TObject);
begin
  if CDS_main.IsEmpty then exit;
  if trim(CRType.Text)='' then
  begin
    application.MessageBox('入库类型不能为空！','提示信息',0);
    exit;
  end;
  if trim(CRType.Text)='退货入库' then
  begin
    if trim(CustomerNoName.Text)='' then
    begin
      application.MessageBox('退货客户不能为空！','提示信息',0);
      exit;
    end;
  end;
  if CDS_main.Locate('Qtyunit','',[]) then
  begin
    application.MessageBox('数量单位不能为空！','提示信息',0);
    exit;
  end;
  if CDS_main.Locate('Qtyunit',NULL,[]) then
  begin
    application.MessageBox('数量单位不能为空！','提示信息',0);
    exit;
  end;
  if CDS_main.Locate('MJID',NULL,[]) then
  begin
    application.MessageBox('卷条码不能为空！','提示信息',0);
    exit;
  end;
  if CDS_main.Locate('MJXH',NULL,[]) then
  begin
    application.MessageBox('卷号不能为空！','提示信息',0);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    ModalResult:=1;
  end
  else
  application.MessageBox('数据保存失败！','提示信息',0);
end;

procedure TfrmCPRK.v1C_ColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
      MainType:=Trim(DName);
    //  fnote:=True;
      if ShowModal=1 then
      begin
        Self.CDS_main.Edit;
        Self.CDS_main.FieldByName('C_Color').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'#';
      //  Self.CDS_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPRK.kcKwBtnClick(Sender: TObject);
begin
  try
    frmCPKWListSel:=TfrmCPKWListSel.Create(Application);
    with frmCPKWListSel do
    begin
      if ShowModal=1 then
      begin
        kcKw.Text:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmCPKWListSel.Free;
  end;
end;

procedure TfrmCPRK.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
  maxno:string;
begin
  if Key=#13 then
  begin
    if Trim(Edit1.Text)='' then Exit;
    if TryStrToFloat(Edit1.Text,FReal)=False then
    begin
      Application.MessageBox('数量非法数字!','提示',0);
      Exit;
    end;
    if CDS_main.IsEmpty then
    begin
      CDS_main.Append;
      CDS_main.fieldbyname('Qty').Value:=Edit1.Text;
      CDS_main.fieldbyname('kcKw').Value:=kcKw.Text;
      CDS_main.fieldbyname('RollNum').Value:=1;
      CDS_main.fieldbyname('MJXH').Value:=1;
      CDS_main.fieldbyname('CPTYpe').Value:='正品';
      CDS_main.fieldbyname('QtyUnit').Value:='KG';
      try
        if GetLSNo(ADOQueryCmd,maxno,'S','APTM',4,1)=False then
        begin
          Application.MessageBox('取最大号失败!','提示',0);
          Exit;
        end;
      except
        Application.MessageBox('取最大号失败!','提示',0);
      end;
      CDS_main.fieldbyname('MJID').Value:=Trim(maxno);
      CDS_main.Post;
    end else
    begin
      CopyAddRowCDS(CDS_main);
      tv1.Controller.EditingController.ShowEdit();
      with CDS_main do
      begin
        Edit;
        FieldByName('MJXH').Value:=FieldByName('MJXH').Value+1;
        try

          if GetLSNo(ADOQueryCmd,maxno,'S','APTM',4,1)=False then
          begin

            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;

        except
          Application.MessageBox('取最大号失败!','提示',0);
        end;
        FieldByName('MJId').Value:=Trim(maxno);
        fieldbyname('Qty').Value:=Edit1.Text;
        fieldbyname('kcKw').Value:=kcKw.Text;
        fieldbyname('RollNum').Value:=1;
        Post;
      end;
    end;
    Edit1.Text:='';
  end;

  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmCPRK.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_main do
  begin
    Edit;
    FieldByName('C_CodeName').Value:=Trim(mvalue);
    Post;
  end;

  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(CDS_main.fieldbyname('C_CodeName').AsString)));
    open;
  end;
  IF ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('品名不存在！','提示信息',0);
    with CDS_main do
    begin
      Edit;
      FieldByName('C_CodeName').Value:='';
      Post;
    end;
  end;

  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPRK.v1C_ColorPropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_main do
  begin
    Edit;
    FieldByName('C_Color').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(CDS_main.fieldbyname('C_CodeName').AsString)));
    sql.Add('and DEFstr3='+quotedstr(trim(CDS_main.fieldbyname('C_Color').AsString)));
    open;
  end;
  IF ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('颜色不存在！','提示信息',0);
    with CDS_main do
    begin
      Edit;
      FieldByName('C_Color').Value:='';
      Post;
    end;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;
procedure TfrmCPRK.CustomerNoNameBtnUpClick(Sender: TObject);
begin
{  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CustomerNoName';
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
 try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      fywy:=trim(SYRName.Text);
     // fkhType:=trim(SYRName.Text);
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerNoName.TxtCode:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);

      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmCPRK.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  CustomerNoName.Text:='';
  CustomerNoName.TxtCode:='';
end;

procedure TfrmCPRK.Edit2KeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
  maxno:string;
  I:integer;
begin
  if Key=#13 then
  begin
    if Trim(Edit2.Text)='' then Exit;
    if TryStrToFlOAT(Edit2.Text,FReal)=False then
    begin
      Application.MessageBox('数量非法数字!','提示',0);
      Exit;
    end;
    if CDS_main.IsEmpty then  exit;
    i:=STRTOintdef(Edit2.Text,0);
    for i:=1 to STRTOintdef(Edit2.Text,0) do
    begin
      CopyAddRowCDS(CDS_main);
      with CDS_main do
      begin
        Edit;
        FieldByName('MJXH').Value:=FieldByName('MJXH').Value+1;
        try
          if GetLSNo(ADOQueryCmd,maxno,'S','APTM',4,1)=False then
          begin
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        except
          Application.MessageBox('取最大号失败!','提示',0);
        end;
        FieldByName('MJId').Value:=Trim(maxno);
    //    fieldbyname('Qty').Value:=Edit1.Text;
    //    fieldbyname('kcKw').Value:=kcKw.Text;
    //    fieldbyname('RollNum').Value:=1;
        Post;
      end;
    end;
     Edit1.Text:='';
  end;

  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmCPRK.v1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'W'THEN
  BEGIn
    mvalue:=trim(mvalue)+'W';
  END;


  with CDS_main do
  begin
    Edit;
    FieldByName('BCgangNo').Value:=Trim(mvalue);
    Post;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPRK.TBPrintClick(Sender: TObject);
var
  fPrintFile:string;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDS_Main.fieldbyname('MJID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
          CDS_Main.EnableControls;
          exit;
        end;
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add('select A.*,A.BCFK as MJFK,A.BCKZ as MJSJKZ,Qty as mjlen,QtyUnit as MJTypeother,C_CodeNameZW=(select Top 1 defnote3 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and Type=''CP'') ');
          sql.Add(',C_colorName=(select Top 1 DEFstr6 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and X.DEFstr3=A.C_Color and Type=''CP'') ');
          sql.Add(',factoryNameJZ=(select Top 1 X1.zdyNameZ from KH_Zdy_Attachment X1,WFB_MJJY X2 where X1.ZdyName=X2.factoryName and X2.MJID=A.MJID and X1.Type=''GYS'') ');
          sql.Add(' from CK_BanCP_CR A') ;
          SQL.Add(' where A.MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          SQL.Add(' and Qty>0 and CRFlag=''入库'' ');
          Open;
        end;
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用标签.rmf' ;
        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE']:=fImagePath;
          RMGridReport1.LoadFromFile(fPrintFile);
         // RMGridReport1.ShowReport;
          RMGridReport1.PrintReport;
        end else
        begin
          CDS_Main.EnableControls;
          Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',0);
          Exit;
        end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;

end;

end.
