unit U_ProductSSedit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxEdit, cxNavigator, DB, cxDBData, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, DBClient, Menus, ComCtrls, ToolWin,
  cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, cxButtonEdit, cxDropDownEdit,
  StdCtrls, ExtCtrls;

type
  TFRMProductSSedit = class(TForm)
    ADOCmd: TADOQuery;                              
    ADOTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1SSGH: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    ToolButton2: TToolButton;
    CDS_CON: TClientDataSet;
    DSCON: TDataSource;
    ADOQuery1: TADOQuery;
    Panel1: TPanel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    V4Column1: TcxGridDBColumn;
    TV4Column1: TcxGridDBColumn;
    TV4Column3: TcxGridDBColumn;
    TV4Column4: TcxGridDBColumn;
    TV4Column5: TcxGridDBColumn;
    TV4Column2: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    Panel2: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    Label5: TLabel;
    RCGangNo: TEdit;
    procedure ToolButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1MPRTKZPropertiesChange(Sender: TObject);
    procedure v1MPRTKZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV4DblClick(Sender: TObject);
    procedure RCGangNoKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FSSId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  FRMProductSSedit: TFRMProductSSedit;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CPManageSel, getpic, U_LabelList, U_ContractListSel, U_YCLViewSel,
  U_PhoneOrder, U_ProductOrderListSel;

{$R *.dfm}

procedure TFRMProductSSedit.ToolButton1Click(Sender: TObject);
begin

  CopyAddRow(Tv1, Order_Main);
  with Order_Main do
  begin
    Edit;
    FieldByName('OrderNo').Value := '';
    FieldByName('SSCodeName').Value := '';
    FieldByName('SScolor').Value := '';
    FieldByName('RCFactoryName').Value := '';
    FieldByName('PBFactoryName').Value := '';

    Post;
  end;
end;

procedure TFRMProductSSedit.FormShow(Sender: TObject);
begin
  readCxGrid('指示单录入AA', Tv1, '生产指示单管理');

  fFlileFlag := UserDataFlag + 'HX';
  InitData();
   BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  if PState = 1 then
  begin

  end
  else
  begin
    CopyAddRow(Tv1, Order_Main);
    with Order_Main do
    begin
      Edit;
      FieldByName('ssType').Value := '缩水';
      Post;
    end;
  end;

end;

procedure TFRMProductSSedit.InitData();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_SS where 1=1 ');

    if PState = 1 then
    begin
      sql.Add(' and SSId=''' + Trim(FSSId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    sql.Add(' order by SSId ');
    Open;
  end;
  SCreateCDS20(ADOTemp, Order_Main);
  SInitCDSData20(ADOTemp, Order_Main);

  if CopyInt = 99 then
  begin
    PState := 0;
    FSSId := '';

    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('SSId').Value := '';
//        FieldByName('SubId').Value := '';

        Post;
        Next;
      end;
    end;
  end;

end;

function TFRMProductSSedit.SaveData(): Boolean;
var
  maxno, Smaxno, maxorderno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Main.fieldbyname('SSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'SS', 'JYOrder_SS', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Smaxno := Trim(Order_Main.fieldbyname('SSid').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_SS where  ');
          sql.Add('   SSid=''' + Trim(Smaxno) + '''');
          Open;
        end;                                   
        with ADOCmd do                                             
        begin
          if Trim(Order_main.fieldbyname('SSid').AsString) = '' then
            Append
          else
            Edit;
//          FieldByName('MainId').Value := Trim(maxno);
          FieldByName('SSid').Value := Trim(Smaxno);

          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Main, 'JYOrder_SS', 0);
          FieldByName('Filler').Value := Trim(DName);
          FieldByName('DDTYpe').Value := '大货';
          Post;
        end;

        Order_Main.Edit;
        Order_Main.FieldByName('SSid').Value := Trim(Smaxno);
        Next;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

procedure TFRMProductSSedit.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('缩水录入', Tv1, '缩水管理');
end;

procedure TFRMProductSSedit.v1ConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Main do
        begin
          edit;
 //        Self.Order_Sub.FieldByName('mainid').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
//        Self.Order_Sub.FieldByName('subid').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          Self.Order_Main.FieldByName('OrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('ConNo').AsString);
          Self.Order_Main.FieldByName('SSCodeName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
          Self.Order_Main.FieldByName('SSCodeNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
          Self.Order_Main.FieldByName('sscolor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TFRMProductSSedit.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      CheckBox2.Checked := False;
      CheckBox1.Checked := True;
      if ShowModal = 1 then
      begin
        with Self.Order_Main do
        begin
          edit;

          FieldByName('RCFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COCode').AsString);
          FieldByName('RCFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COName').AsString);

        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TFRMProductSSedit.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  Order_Main.Delete;
end;

procedure TFRMProductSSedit.v1MPRTKZPropertiesChange(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := Trim(Order_Main.fieldbyname('SSGH').AsString);
  if Length(Trim(mvalue)) > 1 then
  begin
    mvalue := '%' + Trim(mvalue) + '%';
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from CK_SXPB_CR A  where A.CKName=''待检布'' and A.CRType in (''回修完成'',''加工完成'',''客户退货'',''改色完成'') ');
      sql.Add('and  A.RCGangNo like ' + quotedstr('%' + trim(mvalue) + '%'));
         Sql.add('and   CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and  CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_Con);
    SInitCDSData20(ADOQuery1, CDS_CON);
    if CDS_Con.IsEmpty then
      Panel1.Visible := False
    else
      Panel1.Visible := True;
  end;
end;

procedure TFRMProductSSedit.v1MPRTKZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalue: string;
begin
  Panel1.Visible := True;
  mvalue := Trim(Order_Main.fieldbyname('SSGH').AsString);
  if Length(Trim(mvalue)) > 1 then
  begin
    mvalue := '%' + Trim(mvalue) + '%';
    with ADOQuery1 do
    begin                                      
      Close;
      sql.Clear;
      sql.Add('select A.* from CK_SXPB_CR A  where A.CKName=''待检布'' and A.CRType in (''回修完成'',''加工完成'',''客户退货'',''改色完成'') ');
      sql.Add('and  A.RCGangNo like ' + quotedstr('%' + trim(mvalue) + '%'));
       Sql.add('and   CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and  CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_Con);
    SInitCDSData20(ADOQuery1, CDS_CON);
//    if CDS_Con.IsEmpty then
//      cxGrid4.Visible := False
//    else
//      cxGrid4.Visible := True;
  end;
end;

procedure TFRMProductSSedit.TV4DblClick(Sender: TObject);
begin
//  conno.OnChange := nil;
//  Order_Sub.EmptyDataSet;
  Panel1.Visible := False;
//  conNo.Text := CDS_CON.fieldbyname('conNO').AsString;
  Panel1.Visible := False;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where SPID=' + quotedstr(trim(CDS_CON.fieldbyname('SPID').AsString)));
    Open;
  end;

  if not ADOQuery1.IsEmpty then
  begin

    with ADOQuery1 do
    begin
//      First;
//      while not Eof do
//      begin
      with Order_main do
      begin
        edit;
        FieldByName('OrderNo').Value := Trim(ADOQuery1.fieldbyname('conno').AsString);
        FieldByName('SSGH').Value := Trim(ADOQuery1.fieldbyname('RCGangNo').AsString);
        FieldByName('SSCodeName').Value := Trim(ADOQuery1.fieldbyname('SPName').AsString);
        FieldByName('SScolor').Value := Trim(ADOQuery1.fieldbyname('SPColor').AsString);
        FieldByName('RCFactoryName').Value := Trim(ADOQuery1.fieldbyname('FactoryName').AsString);
        FieldByName('PBFactoryName').Value := Trim(ADOQuery1.fieldbyname('PBFactoryName').AsString);

        Post;
      end;
//        Next;
//      end;
    end;
  end;
//  conno.OnChange := ConNoChange;
end;

procedure TFRMProductSSedit.RCGangNoKeyPress(Sender: TObject;
  var Key: Char);
  var
  mvalue: string;
begin
  mvalue := Trim(RCGangNo.text);
  if Length(Trim(mvalue)) > 1 then
  begin
    mvalue := '%' + Trim(mvalue) + '%';
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from CK_SXPB_CR A  where A.CKName=''待检布'' and A.CRType in (''回修完成'',''加工完成'',''客户退货'',''改色完成'') ');
      sql.Add('and  A.RCGangNo like ' + quotedstr('%' + trim(mvalue) + '%'));
         Sql.add('and   CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      Sql.add('and  CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_Con);
    SInitCDSData20(ADOQuery1, CDS_CON);
    if CDS_Con.IsEmpty then
      Panel1.Visible := False
    else
      Panel1.Visible := True;
  end;
end;

end.

