unit U_LLRKInPutMH_RCCG;

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, Menus, ImgList, ShellAPI, cxGridBandedTableView,
  cxGridDBBandedTableView, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore,    
   dxSkinDarkRoom,  
     
     
     
    
    
    
   dxSkinOffice2013White,  
    dxSkinSharpPlus, 
  dxSkinSpringTime,   
  dxSkinsDefaultPainters,   
   dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmLLRKInPutMH_RCCG = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    Label26: TLabel;
    Panel3: TPanel;
    labMYType: TLabel;
    Panel4: TPanel;
    Label13: TLabel;
    CRTime: TDateTimePicker;
    Panel5: TPanel;
    Label31: TLabel;
    Label32: TLabel;
    PS: TEdit;
    Qty: TEdit;
    KgQty: TEdit;
    Panel7: TPanel;
    Panel1: TPanel;
    Label20: TLabel;
    PTID: TEdit;
    Label23: TLabel;
    MYPrice: TEdit;
    Label24: TLabel;
    MYMDMoney: TEdit;
    Label28: TLabel;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    ImageList1: TImageList;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    ADOQuery2: TADOQuery;
    Label45: TLabel;
    Label50: TLabel;
    ADOQueryTemp: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label33: TLabel;
    GYSName: TBtnEditA;
    Label22: TLabel;
    MYName: TEdit;
    Label12: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    MYKZ: TEdit;
    MYMF: TEdit;
    Label2: TLabel;
    Label11: TLabel;
    MYHX: TEdit;
    MYColor: TEdit;
    Label4: TLabel;
    MYColorNo: TEdit;
    Label51: TLabel;
    Label43: TLabel;
    MYMDPrice: TEdit;
    MDMFlag: TCheckBox;
    MDKGFlag: TCheckBox;
    HSFlag: TCheckBox;
    BHSFlag: TCheckBox;
    Label1: TLabel;
    Note: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    FeeName: TBtnEditA;
    Label9: TLabel;
    FeeMoney: TEdit;
    Label3: TLabel;
    ToGYSName: TBtnEditA;
    Panel2: TPanel;
    Panel6: TPanel;
    Label5: TLabel;
    KHNameJC: TBtnEditA;
    Label8: TLabel;
    Label10: TLabel;
    Label17: TLabel;
    PBBatchNo: TEdit;
    MLOrderNo: TcxButtonEdit;
    Panel8: TPanel;
    Panel9: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label18: TLabel;
    Label19: TLabel;
    Edit1: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label21: TLabel;
    RCColor: TEdit;
    MDMLConNo: TcxButtonEdit;
    btn1: TButton;
    scdsubid: TEdit;
    Label25: TLabel;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label27: TLabel;
    Label29: TLabel;
    GangNo: TEdit;
    Tv1Column5: TcxGridDBColumn;
    ZKZT: TComboBox;
    LXHSL: TEdit;
    Button1: TButton;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure QtyChange(Sender: TObject);
    procedure MDMFlagClick(Sender: TObject);
    procedure MDKGFlagClick(Sender: TObject);
    procedure GYSNameBtnClick(Sender: TObject);
    procedure HSFlagClick(Sender: TObject);
    procedure BHSFlagClick(Sender: TObject);
    procedure ToGYSNameBtnClick(Sender: TObject);
    procedure FeeNameBtnClick(Sender: TObject);
    procedure FeeMoneyChange(Sender: TObject);
    procedure MYMDPriceChange(Sender: TObject);
    procedure KHNameJCBtnClick(Sender: TObject);
    procedure MLOrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MLConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    procedure InitData();
    function SaveData(): Boolean;
    function YFData(): Boolean;
    procedure GetMoney();
    { Private declarations }
  public
    canshu1: string;
    PState, CopyInt: Integer;
    FMainId: string;
    FRead: string;
    FMYType, FMYTypeFlag, FML, FNewMainId, FNewSubid: string;
    { Public declarations }
  end;

var
  frmLLRKInPutMH_RCCG: TfrmLLRKInPutMH_RCCG;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_GYSList, U_OrderListSel, U_PBCGRKListSel,
  U_MLConTract_NXSel, U_OrderMXSelist, U_MLSCDXZ, U_FHSQListSel;

{$R *.dfm}
function TfrmLLRKInPutMH_RCCG.YFData(): Boolean;
var
  CRID, OrdMainId, YFID, FComTaiTou, FCRID, FFactoryName, FFactoryNo: string;
begin
  Result := False;
  FFactoryName := Trim(GYSName.Text);
  FFactoryNo := Trim(GYSName.TxtCode);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YS_Money_KC where FactoryNo=' + quotedstr(Trim(FFactoryNo)));
    sql.Add(' and KCType=''GYS'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YS_Money_CRID select 0 ');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end;

    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(FFactoryName);
      FieldByName('FactoryNo').Value := Trim(FFactoryNo);
      FieldByName('ZdyStr1').Value := '应付付';
      FieldByName('KCType').Value := 'GYS';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where FactoryNo=' + quotedstr(Trim(FFactoryNo)));
    sql.Add(' and YFTypeId=''' + Trim(FMainId) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'JG', 'YS_Money_CR', 4, 1) = False then
    begin
      Application.MessageBox('取应付最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID=''' + Trim(YFID) + '''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      Append;
      FieldByName('SCQty').Value := strtofloatdef(Qty.text, 0);
      FieldByName('SCPS').Value := strtofloatdef(PS.text, 0);
    end
    else
      Edit;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(FMainid);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('YFName').Value := '加工费';
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应付款登记';
    FieldByName('CRFlag').Value := '应付付';
    FieldByName('ChkStatusP').Value := '未审核';
    FieldByName('ChkStatusQ').Value := '未审核';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(FFactoryName);
    FieldByName('FactoryNo').Value := Trim(FFactoryNo);
    FieldByName('CRTime').Value := CRTime.Date;
    if MDMFlag.Checked = True then
    begin
      FieldByName('Qty').Value := Strtofloatdef(Qty.text, 0);
      FieldByName('QtyUnit').Value := 'M';
    end
    else
    begin
      FieldByName('Qty').Value := Strtofloatdef(KGQty.text, 0);
      FieldByName('QtyUnit').Value := 'KG';
    end;
    FieldByName('PS').Value := Strtofloatdef(PS.text, 0);
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := Strtofloatdef(MYMDPrice.text, 0);
    if HSFlag.Checked = true then
    begin
      FieldByName('HanShui').Value := '含税';
    end
    else
    begin
      FieldByName('HanShui').Value := '不含税';
    end;
    FieldByName('CKNO').Value := trim(FMainid);
    FieldByName('SJQty').Value := Strtofloatdef(Qty.text, 0);
    FieldByName('MiQty').Value := Strtofloatdef(Qty.text, 0);
    FieldByName('KGQty').Value := Strtofloatdef(KGQty.text, 0);
    FieldByName('money').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('JSmoney').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('HSmoney').Value := Strtofloatdef(MYMDMoney.text, 0);
    FieldByName('P_CodeName').Value := Trim(MYName.text);
    FieldByName('P_MF').Value := Trim(MYMF.text);
    FieldByName('P_KZ').Value := Trim(MYKZ.Text);
    FieldByName('P_Color').Value := Trim(MYColor.Text);
    FieldByName('P_ColorNo').Value := Trim(MYColorNo.Text);
    FieldByName('P_HX').Value := Trim(MYHX.Text);
    FieldByName('BZType').Value := '￥';
    FieldByName('status').Value := '0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_KC Set KCMoney=(select isnull(Sum(HSMoney*QtyFlag),0) from YS_Money_CR A where A.CRID=YS_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

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

procedure TfrmLLRKInPutMH_RCCG.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,KM.KHNameJC,B.MLConNo,B.MLOrderNo ');
    sql.Add(' from CK_MYSC_CR A  ');
    sql.Add(' left join ML_OrderMainNew B on A.MainId=B.NewMLID');
    sql.Add(' left join KH_Main KM on KM.KHNO=B.KHNO ');
    sql.Add(' where MYID=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQuery1, Panel4, 2);
  SCSHDataNew(ADOQuery1, Panel5, 2);
  SCSHDataNew(ADOQuery1, Panel4, 0);
  if Trim(ADOQuery1.FieldByName('MLConNo').AsString) = '' then
  begin
    MDMLConNo.Text := Trim(ADOQuery1.FieldByName('MDMLConNo').AsString);
  end
  else
  begin
    MDMLConNo.Text := Trim(ADOQuery1.FieldByName('MLConNo').AsString);
  end;

  MDMFlag.Checked := ADOQuery1.FieldByName('MDMflag').AsBoolean;
  MDKGFlag.Checked := ADOQuery1.FieldByName('MDKGFlag').AsBoolean;
  HSFlag.Checked := ADOQuery1.FieldByName('HSFlag').AsBoolean;
  BHSFlag.Checked := ADOQuery1.FieldByName('BHSFlag').AsBoolean;
  FNewMainId := Trim(ADOQuery1.fieldbyname('MainId').AsString);
  FNewSubid := Trim(ADOQuery1.fieldbyname('SubId').AsString);
  if PState = 0 then
  begin
    CRTime.Date := SGetServerDate(ADOTemp);
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID=''' + Trim(FMainId) + '''');
    open;
  end;
  SCreateCDS20(ADOQuery1, ClientDataSet1);
  SInitCDSData20(ADOQuery1, ClientDataSet1);
end;

procedure TfrmLLRKInPutMH_RCCG.FormShow(Sender: TObject);
begin
  InitData();
  if Trim(FMYType) = '染色' then
  begin
    PBBatchNo.ReadOnly := True;
  end;
end;

function TfrmLLRKInPutMH_RCCG.SaveData(): Boolean;
var
  maxno, maxmxno, maxnoflag, fsj, FMXID: string;
begin
  Result := False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JG', 'CK_MYSC_CR', 4, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CK_MYSC_CR where MYId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if isempty then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
      end
      else
      begin
        Edit;
      end;
      FieldByName('MYId').Value := Trim(maxno);
      FieldByName('CRType').Value := '加工码单';
      FieldByName('CRFlag').Value := '入库';
      FieldByName('CRQtyFlag').Value := 1;
      if Trim(FMainId) = '' then
      begin

      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditerCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel4, 2);
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel5, 2);
      FieldByName('MDMflag').Value := MDMFlag.Checked;
      FieldByName('MDKGflag').Value := MDKGflag.Checked;
      FieldByName('HSFlag').AsBoolean := HSFlag.Checked;
      FieldByName('BHSFlag').AsBoolean := BHSFlag.Checked;
      FieldByName('MYType').Value := '加工费';
      FieldByName('MYTypeFlag').Value := Trim(FMYTypeFlag);
      FieldByName('MainId').Value := Trim(FNewMainId);
      FieldByName('SubId').Value := Trim(FNewSubid);
      FieldByName('scdSubId').Value := Trim(scdSubId.text);
      Post;
    end;
    FMainId := Trim(maxno);
    YFData();
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not Eof do
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_MYSC_CR_MX where MXID=''' + Trim(ClientDataSet1.fieldbyname('MXID').AsString) + '''');
          Open;
        end;
        FMXID := Trim(ADOCmd.fieldbyname('MXID').AsString);
        if Trim(FMXID) = '' then
        begin
          if GetLSNo(ADOCmd, maxmxno, 'MX', 'CK_MYSC_CR_MX', 4, 1) = False then
          begin
            ClientDataSet1.EnableControls;
            Application.MessageBox('取加工码单明细数据最大号失败', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxmxno := Trim(FMXID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_MYSC_CR_MX where MXID=''' + Trim(maxmxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FMXID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
            FieldByName('Editer').Value := Trim(DName);
          end;
          FieldByName('MYID').Value := Trim(FMainId);
          FieldByName('MXID').Value := Trim(maxmxno);
          FieldByName('FSubId').Value := Trim(scdSubId.text);
          FieldByName('CDZSD').Value := Trim(MLOrderNo.text);
          RTSetSaveDataCDS(ADOCmd, Tv1, ClientDataSet1, 'CK_MYSC_CR_MX', 0);
          Post;
        end;
        Edit;
        FieldByName('MYID').Value := Trim(FMainId);
        FieldByName('MXID').Value := Trim(maxmxno);
        Post;
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate CK_MYSC_CR Set MXHZQty=(select Sum(MQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' ,MXHZKgQty=(select Sum(KgQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' ,MXHZPS=(select Count(*) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' where MYID=''' + Trim(FMainId) + '''');
      sql.Add(' Update CK_MYSC_CR Set QtyChaE=isnull(MXHZQty,0)-isnull(Qty,0),KgQtyChaE=isnull(MXHZKgQty,0)-isnull(KgQty,0),PSChaE=isnull(MXHZPS,0)-isnull(PS,0) ');
      sql.Add(' where MYID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
    if Trim(ToGYSName.Text) = '云翔仓库' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' UPdate ML_OrderSubidNew Set HCPS=(select sum(PS) from CK_MYSC_CR A where A.SubID=ML_OrderSubidNew.NewSubid) ');
        if MDMFlag.Checked then
        begin
          sql.Add(' ,HCQty=(select sum(Qty) from CK_MYSC_CR A where A.SubID=ML_OrderSubidNew.NewSubid) ');
        end;
        if MDKGFlag.Checked then
        begin
          sql.Add(' ,HCQty=(select sum(KgQty) from CK_MYSC_CR A where A.SubID=ML_OrderSubidNew.NewSubid) ');
        end;
        sql.Add(' where NewSubid=''' + Trim(FNewSubid) + '''');
        ExecSQL;
      end;
    end;
    if Trim(FMYType) = '染色' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
        Parameters.ParamByName('PBBatchNo').Value := Trim(PBBatchNo.Text);
        Parameters.ParamByName('JGCNO').Value := Trim(GYSName.TxtCode);
        ExecSQL;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ClientDataSet1.EnableControls;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.TBSaveClick(Sender: TObject);
var
  FReal, X: Double;
  FInt: Integer;
begin
  ToolBar1.SetFocus;
  if Trim(GYSName.Text) = '' then
  begin
    Application.MessageBox('供应商不能为空!', '提示', 0);
    Exit;
  end;

  if Trim(MYName.Text) = '' then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if (MDMFlag.Checked = False) and (MDKGFlag.Checked = False) then
  begin
    Application.MessageBox('请选择计价方式', '提示', 0);
    Exit;
  end;
  if MDMFlag.Checked then
  begin
    if Trim(Qty.Text) = '' then
    begin
      Application.MessageBox('米数不能为空!', '提示', 0);
      Exit;
    end;
  end;
  if MDKGFlag.Checked then
  begin
    if Trim(KgQty.Text) = '' then
    begin
      Application.MessageBox('公斤数不能为空!', '提示', 0);
      Exit;
    end;
  end;
  if (HSFlag.Checked = False) and (BHSFlag.Checked = False) then
  begin
    Application.MessageBox('请选择含税或不含税', '提示', 0);
    Exit;
  end;
  if Trim(PS.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToInt(PS.Text, FInt) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    exit;
  end;
  if (Trim(Qty.Text) = '') and (Trim(KgQty.text) = '') then
  begin
    Application.MessageBox('米数和公斤数不能同时为空!', '提示', 0);
    exit;
  end;
  if Trim(Qty.Text) <> '' then
  begin
    if TryStrToFloat(Qty.Text, FReal) = False then
    begin
      Application.MessageBox('米数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(KgQty.Text) <> '' then
  begin
    if TryStrToFloat(KgQty.Text, FReal) = False then
    begin
      Application.MessageBox('公斤数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(FMYType) <> '成品再加工' then
  begin
    if Trim(PBBatchNo.Text) = '' then
    begin
      Application.MessageBox('坯布联系号不能为空!', '提示', 0);
      Exit;
    end;
  end;
  if ClientDataSet1.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('ZKZT', null, []) = True then
  begin
    Application.MessageBox('状态不能为空!', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('ZKZT', '', []) = true then
  begin
    application.messagebox('状态不能为空', '提示');
    exit;
  end;

  if Trim(FMYType) = '染色' then
  begin
    with ADOTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select sum(B.MQty) AS ZMS from  CK_MYSC_CR A  INNER JOIN CK_MYSC_CR_MX B on A.MYId=B.MYId where A.PBBatchNo=''' + trim(PBBatchNo.text) + ''' and A.MYTypeFlag=''染色'' ');
      Open;
    end;
    X := 0;
//    with ClientDataSet1 do
//    begin
//      First;
//      while not Eof do
//      begin
//        X := X + StrToFloatDef(Trim(ClientDataSet1.fieldbyname('MQty').AsString), 0);
//        Next;
//      end;
//
//    end;
    if (StrToFloatDef(Trim(Qty.Text), 0) + ADOTemp.fieldbyname('ZMS').AsFloat) > StrToFloatDef(Trim(LXHSL.Text), 0) then
    begin
      Application.MessageBox('入库数量大于坯布联系号数量,不能保存!', '提示', 0);
      Exit;
    end;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

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

procedure TfrmLLRKInPutMH_RCCG.QtyChange(Sender: TObject);
begin
  GetMoney();
end;

procedure TfrmLLRKInPutMH_RCCG.GetMoney();
var
  FQty, FPrice, FFeeMoney: string;
  FReal: Double;
begin
  if MDMFlag.Checked = true then
  begin
    FQty := Qty.text;
  end
  else if MDKGFlag.Checked = true then
  begin
    FQty := KgQty.text;
  end;
  FPrice := MYMDPrice.text;
  FFeeMoney := FeeMoney.Text;
  if TryStrToFloat(FQty, FReal) = False then
  begin
    FQty := '0';
  end;
  if TryStrToFloat(FPrice, FReal) = False then
  begin
    FPrice := '0';
  end;
  if TryStrToFloat(FFeeMoney, FReal) = False then
  begin
    FFeeMoney := '0';
  end;
  MYMDMoney.Text := FloatToStr(strtofloatdef(FPrice, 0) * strtofloatdef(FQty, 0) + strtofloatdef(FFeeMoney, 0));
end;

procedure TfrmLLRKInPutMH_RCCG.MDMFlagClick(Sender: TObject);
begin

  if MDMFlag.Checked = true then
  begin
    if MDKGFlag.Checked = true then
      MDKGFlag.Checked := false;
  end
  else
  begin
    if MDKGFlag.Checked = false then
      MDKGFlag.Checked := true;
  end;
  GetMoney();
end;

procedure TfrmLLRKInPutMH_RCCG.MDKGFlagClick(Sender: TObject);
begin

  if MDKGFlag.Checked = true then
  begin
    if MDMFlag.Checked = true then
      MDMFlag.Checked := false;
  end
  else
  begin
    if MDMFlag.Checked = false then
      MDMFlag.Checked := true;
  end;
  GetMoney();
end;

procedure TfrmLLRKInPutMH_RCCG.GYSNameBtnClick(Sender: TObject);
begin
  if Trim(FMYType) = '染色' then
  begin
    try
      frmPBCGRKListSel := TfrmPBCGRKListSel.Create(Application);
      with frmPBCGRKListSel do
      begin
        if ShowModal = 1 then
        begin
          Self.GYSName.Text := Trim(frmPBCGRKListSel.Order_Main.fieldbyname('JCDanWeiJC').AsString);
          Self.GYSName.TxtCode := Trim(frmPBCGRKListSel.Order_Main.fieldbyname('JCKHNo').AsString);
          Self.MYName.Text := Trim(frmPBCGRKListSel.Order_Main.fieldbyname('MYName').AsString);
          Self.PBBatchNo.Text := Trim(frmPBCGRKListSel.Order_Main.fieldbyname('PBBatchNo').AsString);
          Self.LXHSL.Text := Trim(frmPBCGRKListSel.Order_Main.fieldbyname('Qty').AsString);
        end;
      end;
    finally
      frmPBCGRKListSel.Free;
    end;
  end
  else
  begin
    try
      frmGYSList := TfrmGYSList.Create(Application);
      with frmGYSList do
      begin
        if ShowModal = 1 then
        begin
          Self.GYSName.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          Self.GYSName.TxtCode := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
        end;
      end;
    finally
      frmGYSList.Free;
    end;
  end;

end;

procedure TfrmLLRKInPutMH_RCCG.HSFlagClick(Sender: TObject);
begin
  if HSFlag.Checked = true then
  begin
    if BHSFlag.Checked = true then
      BHSFlag.Checked := false;
  end
  else
  begin
    if BHSFlag.Checked = false then
      BHSFlag.Checked := true;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.BHSFlagClick(Sender: TObject);
begin
  if BHSFlag.Checked = true then
  begin
    if HSFlag.Checked = true then
      HSFlag.Checked := false;
  end
  else
  begin
    if HSFlag.Checked = false then
      HSFlag.Checked := true;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.ToGYSNameBtnClick(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal = 1 then
      begin
        Self.ToGYSName.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        Self.ToGYSName.TxtCode := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.FeeNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FeeNameMD';
      flagname := '费用名称';
      if ShowModal = 1 then
      begin
        FeeName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.FeeMoneyChange(Sender: TObject);
begin
  GetMoney();
end;

procedure TfrmLLRKInPutMH_RCCG.MYMDPriceChange(Sender: TObject);
begin
  GetMoney();
end;

procedure TfrmLLRKInPutMH_RCCG.KHNameJCBtnClick(Sender: TObject);
begin
  try
    frmOrderListSel := TfrmOrderListSel.Create(Application);
    with frmOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        FNewMainId := Trim(frmOrderListSel.Order_Main.fieldbyname('NewMLID').AsString);
        Self.MDMLConNo.Text := Trim(frmOrderListSel.Order_Main.fieldbyname('MLConNo').AsString);
        Self.MLOrderNo.Text := Trim(frmOrderListSel.Order_Main.fieldbyname('MLOrderNo').AsString);
        Self.KHNameJC.Text := Trim(frmOrderListSel.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmOrderListSel.Free;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.MLOrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmOrderMXSelist := TfrmOrderMXSelist.Create(Application);
    with frmOrderMXSelist do
    begin
      if ShowModal = 1 then
      begin
        Self.MLOrderNo.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLOrderNo').AsString);
        Self.MDMLConNo.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLConNo').AsString);
        Self.MYColor.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLColor').AsString);
        Self.MYColorNo.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLColorNo').AsString);
        Self.MYHX.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLHX').AsString);
        Self.MYHX.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLHX').AsString);
        Self.MYHX.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('MLHX').AsString);
        Self.KHNameJC.Text := Trim(frmOrderMXSelist.Order_Main.fieldbyname('KHNameJC').AsString);
        Self.FNewMainId := Trim(frmOrderMXSelist.Order_Main.fieldbyname('NewMLID').AsString);
        Self.FNewSubid := Trim(frmOrderMXSelist.Order_Main.fieldbyname('NewSubid').AsString);
      end;
    end;
  finally
    frmOrderMXSelist.Free;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  Freal: Double;
  i: Integer;
begin
  if Key = #13 then
  begin
    if Trim(Edit1.Text) <> '' then
    begin
      Tv1.Columns[0].FocusWithSelection;
      i := ClientDataSet1.RecordCount;
      i := i + 1;
      if TryStrToFloat(Edit1.Text, Freal) = False then
      begin
        Application.MessageBox('明细数量非法数字!', '提示', 0);
        Exit;
      end;
      with ClientDataSet1 do
      begin
        Append;
        FieldByName('HSQty').Value := Trim(Edit1.Text);
        FieldByName('ZKZT').Value := Trim(ZKZT.Text);
        FieldByName('GangNo').Value := Trim(GangNo.Text);
        if MDMFlag.Checked then
        begin
          FieldByName('MQty').Value := Trim(Edit1.Text);
        end;
        if MDKGFlag.Checked then
        begin
          FieldByName('KgQty').Value := Trim(Edit1.Text);
        end;
        FieldByName('JH').Value := i;
        Post;
      end;
      Edit1.Text := '';
    end;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if Trim(ClientDataSet1.fieldbyname('MXID').AsString) = '' then
  begin
    ClientDataSet1.Delete;
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) = IDYES then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_MYSC_CR_MX where MXID=''' + Trim(ClientDataSet1.fieldbyname('MXID').AsString) + '''');
      sql.Add(' UPdate CK_MYSC_CR Set MXHZQty=(select Sum(MQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' ,MXHZKgQty=(select Sum(KgQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' ,MXHZPS=(select Count(*) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
      sql.Add(' where MYID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
    ClientDataSet1.Delete;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.MLConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmMLConTract_NXSel := TfrmMLConTract_NXSel.Create(Application);
    with frmMLConTract_NXSel do
    begin
      if ShowModal = 1 then
      begin
        Self.MDMLConNo.text := Trim(Order_Main.fieldbyname('ConNo').AsString);
        Self.KHNameJC.text := Trim(Order_Main.fieldbyname('KHNameJC').AsString);
        Self.MYColor.text := Trim(Order_Sub.fieldbyname('ConColor').AsString);
        Self.MYHX.text := Trim(Order_Sub.fieldbyname('ConHX').AsString);
      end;
    end;
  finally
    frmMLConTract_NXSel.Free;
  end;

end;

procedure TfrmLLRKInPutMH_RCCG.btn1Click(Sender: TObject);
begin
  try
    frmmlscdxz := Tfrmmlscdxz.Create(Application);
    with frmmlscdxz do
    begin
      if ShowModal = 1 then
      begin
//        InitGrid();
        Self.MYName.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('CPName').AsString);
        Self.RCColor.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('CPColor').AsString);
        Self.MYColorNo.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('SeHao').AsString);
        Self.scdsubid.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('subid').AsString);
      end;
    end;
  finally
    frmmlscdxz.Free;
  end;
end;

procedure TfrmLLRKInPutMH_RCCG.Button1Click(Sender: TObject);
begin
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(Application);
    with frmFHSQListSel do
    begin
      if ShowModal = 1 then
      begin
//        InitGrid();
//        Self.MYName.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('CPName').AsString);
//        Self.RCColor.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('CPColor').AsString);
//        Self.MYColorNo.Text := Trim(frmmlscdxz.Order_Main.fieldbyname('SeHao').AsString);
        Self.scdsubid.Text := Trim(frmFHSQListSel.ClientDataSet2.fieldbyname('FSId').AsString);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
end;

end.

