unit U_ClothContractPay;

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;

type
  TfrmClothContractPay = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: 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;
    v1Column3: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1ClothUnit: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2BatchNo: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxSplitter2: TcxSplitter;
    v1Qty1: TcxGridDBColumn;
    v2Qty1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    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 TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton3Click(Sender: TObject);
  private
    FInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractPay: TfrmClothContractPay;

implementation
uses
  U_DataLink,U_ClothContractInPut,U_Fun,U_ProductOrderList;

{$R *.dfm}

procedure TfrmClothContractPay.FormDestroy(Sender: TObject);
begin
  frmClothContractPay:=nil;
end;

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

procedure TfrmClothContractPay.FormCreate(Sender: TObject);
begin
  ScrollBox1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmClothContractPay.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布合同对账单',Tv1,'指示单管理');
end;

procedure TfrmClothContractPay.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec ClothContract_QryPayList :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))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractPay.InitForm();
begin
  ReadCxGrid('坯布合同对账单',Tv1,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmClothContractPay.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;

function TfrmClothContractPay.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    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 TfrmClothContractPay.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同对账单',cxGrid1);
end;

procedure TfrmClothContractPay.TBPrintClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
end;

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

procedure TfrmClothContractPay.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 TfrmClothContractPay.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothContractPay.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if FInt=2 then
  begin
    ClientDataSet2.Append;
    ClientDataSet2.Post;
  end else
  if FInt=3 then
  begin
    ClientDataSet3.Append;
    ClientDataSet3.Post;
  end;
end;

procedure TfrmClothContractPay.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Contract_Sub_Mx where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.PRTSpec,B.PRTColor,C.PRTCodeName,C.OrderNo from Contract_Sub_MxTo A inner join Order_Sub B on A.OrdSubId=B.SubId');
    SQL.Add(' inner join Order_Main C on C.MainId=B.MainId ');
    sql.Add('where A.MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

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

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

procedure TfrmClothContractPay.Tv3MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=3;
end;

procedure TfrmClothContractPay.ToolButton4Click(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('ComeDate',null,[]) then
  begin
    Application.MessageBox('到货日期不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BatchNo',null,[]) then
  begin
    Application.MessageBox('批号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('MxQty',null,[]) then
  begin
    Application.MessageBox('到货数量不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存明细表
    with ClientDataSet2 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','Contract_Sub_Mx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(ClientDataSet2.fieldbyname('MXId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MxId').Value:=Trim(maxno);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv2,ClientDataSet2,'Contract_Sub_Mx',0);
          Post;
        end;
        Next;
      end;
      with ClientDataSet2 do
      begin
        Edit;
        FieldByName('MxId').Value:=Trim(maxno);
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('确定成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('确定异常！','提示',0);
  end;
end;

procedure TfrmClothContractPay.cxGridDBColumn1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderList:=TfrmProductOrderList.Create(Application);
    with frmProductOrderList do
    begin
      FFInt:=1;
      frmProductOrderList.TBAdd.Visible:=False;
      frmProductOrderList.TBEdit.Visible:=False;
      frmProductOrderList.TBDel.Visible:=False;
      frmProductOrderList.TBExport.Visible:=False;
      frmProductOrderList.TBPrint.Visible:=False;
      frmProductOrderList.TBTP.Visible:=False;
      frmProductOrderList.cxGrid2.Visible:=False;

      if ShowModal=1 then
      begin
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('OrdSubId').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('OrderNo').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('PRTCodeName').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTCodeName').AsString);
          FieldByName('PRTSpec').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTSpec').AsString);
          FieldByName('PRTColor').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTColor').AsString);
          
        end;
      end;
    end;
  finally
    frmProductOrderList.Free;
  end;
end;

procedure TfrmClothContractPay.Tv2CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.PRTSpec,B.PRTColor,C.PRTCodeName,C.OrderNo from Contract_Sub_MxTo A inner join Order_Sub B on A.OrdSubId=B.SubId');
    SQL.Add(' inner join Order_Main C on C.MainId=B.MainId ');
    sql.Add('where A.MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

procedure TfrmClothContractPay.ToolButton3Click(Sender: TObject);
begin
  if FInt=2 then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    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;
  end;
end;

end.
