unit U_ClothContractList_CG_help;

interface

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

type
  TfrmClothContractList_CG_help = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNoM: TEdit;
    Label5: TLabel;
    C_CodeNameM: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    ScrollBox1: TScrollBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    v1Qty1: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    Label6: TLabel;
    FactoryNoName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractList_CG_help: TfrmClothContractList_CG_help;

implementation
uses
  U_DataLink,U_ClothContractInPut_CG,U_Fun,U_ProductOrderList,U_ZDYHelp;

{$R *.dfm}

procedure TfrmClothContractList_CG_help.FormDestroy(Sender: TObject);
begin
  frmClothContractList_CG_help:=nil;
end;

procedure TfrmClothContractList_CG_help.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmClothContractList_CG_help.FormCreate(Sender: TObject);
begin
  ScrollBox1.Align:=alClient;
end;

procedure TfrmClothContractList_CG_help.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption,Tv1,'指示单管理');
end;

procedure TfrmClothContractList_CG_help.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add('from Contract_Main A');
      sql.Add('inner join  Contract_sub B on B.MainID=A.mainID ');
      sql.Add('where  A.FillTime>= '''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
      sql.Add('and   A.FillTime< '''+Trim(FormatDateTime('yyyy-MM-dd',endDate.DateTime+1))+''' ');
      sql.Add('and  A.conType=''坯布采购计划'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractList_CG_help.InitForm();
begin
  ReadCxGrid(self.Caption,Tv1,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmClothContractList_CG_help.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmClothContractList_CG_help.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmClothContractInPut_CG:=TfrmClothContractInPut_CG.Create(Application);
    with frmClothContractInPut_CG do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPut_CG.Free;
  end;
end;

procedure TfrmClothContractList_CG_help.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty=false then
  begin
    Application.MessageBox('已到坯，不能删除坯布合同！','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if trim(Order_Main.FieldByName('mainID').AsString)='CM001' then exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmClothContractList_CG_help.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YF_Money_CR where MainID='''+Trim(Order_Main.fieldbyname('MainID').AsString)+'''');
      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;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmClothContractList_CG_help.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

procedure TfrmClothContractList_CG_help.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM:string;

begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\坯布订购合同.rmf' ;
  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('exec ClothContract_QryList :MainId,:WSql');
    Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                         +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
    Parameters.ParamByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
    Parameters.ParamByName('WSql').Value:='';
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  FConNoM:=Trim(CDS_PRT.fieldbyname('ConNoM').AsString);
  //SDofilter(ADOQueryMain,' ConNoM='''+Trim(Order_Main.fieldbyname('ConNoM').AsString)+'''');
  //SCreateCDS20(ADOQueryMain,Order_Main);
  //SInitCDSData20(ADOQueryMain,Order_Main);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\坯布订购合同.rmf'),'提示',0);
  end;
  //SDofilter(ADOQueryMain,'');
  //SCreateCDS20(ADOQueryMain,Order_Main);
  //SInitCDSData20(ADOQueryMain,Order_Main);
  //Order_Main.Locate('ConNoM',FConNoM,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

procedure TfrmClothContractList_CG_help.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothContractList_CG_help.TBAddClick(Sender: TObject);
begin
  try
    frmClothContractInPut_CG:=TfrmClothContractInPut_CG.Create(Application);
    with frmClothContractInPut_CG do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothContractInPut_CG.Free;
  end;
end;

procedure TfrmClothContractList_CG_help.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmClothContractList_CG_help.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothContractList_CG_help.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=2;
end;

procedure TfrmClothContractList_CG_help.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=1;
end;

procedure TfrmClothContractList_CG_help.ToolButton5Click(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
  begin
    Application.MessageBox('到货数量未确定！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('OrderNo',null,[]) then
  begin
    Application.MessageBox('指定订单编号不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存去向表
    with ClientDataSet3 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('ToId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'To','Contract_Sub_MxTo',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取去向流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('ToId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Sub_MxTo where ToId='''+Trim(ClientDataSet3.fieldbyname('ToId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('ToId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MxId').Value:=Trim(ClientDataSet2.fieldbyname('MxId').AsString);
          FieldByName('ToId').Value:=Trim(maxno);
          FieldByName('OrdSubId').Value:=Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString);
          FieldByName('TPDate').Value:=ClientDataSet3.fieldbyname('TPDate').Value;
          FieldByName('TPPerson').Value:=ClientDataSet3.fieldbyname('TPPerson').Value;
          FieldByName('TPNote').Value:=ClientDataSet3.fieldbyname('TPNote').Value;
          if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
            FieldByName('TPQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
          else
            FieldByName('TPQty').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('Qty1').AsString)<>'' then
            FieldByName('Qty1').Value:=ClientDataSet3.fieldbyname('Qty1').Value
          else
            FieldByName('Qty1').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('Qty2').AsString)<>'' then
            FieldByName('Qty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value
          else
            FieldByName('Qty2').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('Qty3').AsString)<>'' then
            FieldByName('Qty3').Value:=ClientDataSet3.fieldbyname('Qty3').Value
          else
            FieldByName('Qty3').Value:=0;
          Post;
        end;
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('ToId').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmClothContractList_CG_help.ToolButton3Click(Sender: TObject);
begin
  if FInt=2 then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    if ClientDataSet3.IsEmpty=false then
    begin
      Application.MessageBox('已产生坯布领料数据，不能删除到坯记录！','提示',0);
      Exit;
    end;
    if Trim(ClientDataSet2.fieldbyname('MxId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet2.Delete;
  end else
  if FInt=3 then
  begin
    if ClientDataSet3.IsEmpty then Exit;
    if Trim(ClientDataSet3.fieldbyname('ToId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub_MxTo where ToId='''+Trim(ClientDataSet3.fieldbyname('ToId').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet3.Delete;
    with ClientDataSet2 do
    begin
      Edit;
      if ClientDataSet3.IsEmpty=False then
       // FieldByName('KCSL').Value:=ClientDataSet2.fieldbyname('MxQty').Value-tv3.DataController.Summary.FooterSummaryValues[0]
      else
        FieldByName('KCSL').Value:=ClientDataSet2.fieldbyname('MxQty').Value;
      Post;
    end;
  end;
end;

procedure TfrmClothContractList_CG_help.v2Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKPlace';
      flagname:='入库地点';
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('RKPlace').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractList_CG_help.conPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNoM.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec ClothContract_QryList :MainId,:WSql');
        Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractList_CG_help.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  IF Order_Main.IsEmpty then exit;
  ModalResult:=1;
end;

end.
