uMain.cpp
上传用户:liuxiao
上传日期:2022-07-06
资源大小:1115k
文件大小:19k
源码类别:

打印编程

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "uMain.h"
  5. #include "uCommon.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "frxClass"
  9. #pragma link "frxPreview"
  10. #pragma link "WinSkinData"
  11. #pragma link "frxDBSet"
  12. #pragma link "frxDesgn"
  13. #pragma resource "*.dfm"
  14. TfrmMain *frmMain;
  15. //---------------------------------------------------------------------------
  16. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  17.     : TForm(Owner)
  18. {
  19.     //
  20.     Caption = g_strAppTitle;
  21.     Application->Title = g_strAppTitle;
  22.     // 使用皮肤
  23.     sknMain->Active = true;
  24.     // 连接数据库
  25.     con->Connected = false;
  26.     con->ConnectionString = String("Provider=Microsoft.Jet.OLEDB.4.0;")
  27.             + "Data Source=" + ExtractFilePath(ParamStr(0)) + "data.mdb;"
  28.             + "Persist Security Info=False";
  29.     con->Connected = true;
  30.     //
  31.     tblTemp->Open();
  32.     // 默认为欢迎页面
  33.     pgcMain->ActivePageIndex = 0;
  34.     //
  35.     lblAppTitle->Caption = g_strAppTitle;
  36.     lblAppTitle->Left = (Width - lblAppTitle->Width) / 2;
  37.     lblAppVer->Caption = g_strAppVer;
  38.     lblAppVer->Left = lblAppTitle->Left + (lblAppTitle->Width - lblAppVer->Width);
  39.     //
  40.     dtpGenBillDate->Date = Now();
  41.     dtpQueryStart->Date = Now();
  42.     dtpQueryEnd->Date = Now();
  43.     //
  44.     // frReport->AddFunction("function NumToRMBStr(fValue: DOUBLE): String;", "Myfunction", "小写金额转大写的函数");
  45. }
  46. //---------------------------------------------------------------------------
  47. void __fastcall TfrmMain::btnCloseOrderClick(TObject *Sender)
  48. {
  49.     pgcMain->ActivePageIndex = 0;
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TfrmMain::miExitAppClick(TObject *Sender)
  53. {
  54.     Close();    
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TfrmMain::miShowOrderClick(TObject *Sender)
  58. {
  59.     pgcMain->ActivePageIndex = 1;
  60.     if(!tblTemp->Active)
  61.         tblTemp->Active = true;
  62.     if(tblTemp->RecordCount)
  63.     {
  64.         tblTemp->First();
  65.         while(!tblTemp->Eof)
  66.             tblTemp->Delete();
  67.     }
  68.     CrnClearOrderForm();
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TfrmMain::miShowQueryClick(TObject *Sender)
  72. {
  73.     pgcMain->ActivePageIndex = 2;
  74.     btnQueryAllClick(Sender);
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TfrmMain::miPrintBillClick(TObject *Sender)
  78. {
  79.     if(qryOrder->Active && qryOrder->RecordCount)
  80.     {
  81.         if(MessageBox(Handle,
  82.                 String().sprintf("确定要打印单据号为 %s 的这条记录吗? ",
  83.                 qryOrder->FieldByName("fBillNumber")->AsString).c_str(),
  84.                 g_strAppName.c_str(),
  85.                 MB_YESNO | MB_ICONQUESTION) == IDYES)
  86.         {
  87.             frReport->LoadFromFile(ExtractFilePath(ParamStr(0)) + "Report.fr3");
  88.             ((TfrxMemoView *)frReport->FindObject("mmoTitle"))->Memo->Text =
  89.                     qryOrder->FieldByName("fTitle")->AsString; // + "     ";;
  90.             ((TfrxMemoView *)frReport->FindObject("mmoBillNumber"))->Memo->Text =
  91.                     qryOrder->FieldByName("fBillNumber")->AsString; //  + "     ";;
  92.             ((TfrxMemoView *)frReport->FindObject("mmoClient"))->Memo->Text =
  93.                     qryOrder->FieldByName("fClient")->AsString; //  + "     ";;
  94.             ((TfrxMemoView *)frReport->FindObject("mmoGenBillDate"))->Memo->Text =
  95.                     FormatDateTime("yyyy-mm-dd",
  96.                     qryOrder->FieldByName("fGenBillDate")->AsDateTime);
  97.             ((TfrxMemoView *)frReport->FindObject("mmoOpera"))->Memo->Text =
  98.                     qryOrder->FieldByName("fOpera")->AsString; //  + "     ";
  99.             ((TfrxMemoView *)frReport->FindObject("mmoBillAuthor"))->Memo->Text =
  100.                     qryOrder->FieldByName("fBillAuthor")->AsString; //  + "     ";
  101.             ((TfrxMemoView *)frReport->FindObject("mmoDiscount"))->Memo->Text =
  102.                     String("让利 ¥") + FormatFloat("#0.00#",
  103.                     qryOrder->FieldByName("fDiscount")->AsFloat);
  104.             ((TfrxMemoView *)frReport->FindObject("Memo30"))->Memo->Text =
  105.                     qryOrder->FieldByName("fAddress")->AsString;
  106.             ((TfrxMemoView *)frReport->FindObject("Memo32"))->Memo->Text =
  107.                     qryOrder->FieldByName("fTel")->AsString;
  108.             ((TfrxMemoView *)frReport->FindObject("Memo34"))->Memo->Text =
  109.                     qryOrder->FieldByName("fGath")->AsString;
  110.             ((TfrxMemoView *)frReport->FindObject("Memo36"))->Memo->Text =
  111.                     qryOrder->FieldByName("fIssue")->AsString;
  112.             ((TfrxMemoView *)frReport->FindObject("Memo38"))->Memo->Text =
  113.                     qryOrder->FieldByName("fCheck")->AsString;
  114.             ((TfrxMemoView *)frReport->FindObject("Memo40"))->Memo->Text =
  115.                     qryOrder->FieldByName("fHotLine")->AsString;
  116.             float fSumValue = 0.0;
  117.             qryDetail->First();
  118.             for(int i=0; i<qryDetail->RecordCount; i++)
  119.             {
  120.                 fSumValue += qryDetail->FieldByName("fSum")->AsFloat;
  121.                 qryDetail->Next();
  122.             }
  123.             ((TfrxMemoView *)frReport->FindObject("mmoSumValue"))->Memo->Text =
  124.                     FormatFloat("#0.00#", fSumValue);
  125.             ((TfrxMemoView *)frReport->FindObject("mmoRMBStr"))->Memo->Text =
  126.                     NumToRMBStr(fSumValue);
  127.                     
  128.             qryDetail->First();
  129.             frReport->ShowReport();
  130.         }
  131.     }
  132.     else
  133.     {
  134.         MessageBox(Handle, "请先选择一条订单再选择打印",
  135.                 g_strAppName.c_str(), MB_OK | MB_ICONINFORMATION);
  136.         pgcMain->ActivePageIndex = 2;
  137.         btnQueryAllClick(Sender);
  138.     }
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TfrmMain::miShowAboutClick(TObject *Sender)
  142. {
  143.     MessageBox(Handle,
  144.             (g_strAppTitle + g_strAppVer + "rn"
  145.             + "-------------------------rn"
  146.             + "by CrazyCamel(CrazyCamel@126.com)rn"
  147.             + "2006.12 - -#").c_str(),
  148.             g_strAppName.c_str(),
  149.             MB_OK | MB_ICONINFORMATION);
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TfrmMain::FormKeyPress(TObject *Sender, char &Key)
  153. {
  154.     if(Key == VK_RETURN)
  155.     {
  156.         Key = 0;
  157.         SelectNext(ActiveControl, true, true);
  158.     }
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TfrmMain::tmrClockTimer(TObject *Sender)
  162. {
  163.     stbMain->Panels->Items[1]->Text = FormatDateTime(" yyyy-mm-dd hh:nn:ss", Now());
  164. }
  165. //---------------------------------------------------------------------------
  166. void __fastcall TfrmMain::CrnClearOrderForm()
  167. {
  168.     for(int i=0; i<ComponentCount; i++)
  169.     {
  170.         if(Components[i]->ClassNameIs("TLabeledEdit"))
  171.             if(((TLabeledEdit *)Components[i])->Parent == tsOrder)
  172.                 ((TLabeledEdit *)Components[i])->Text = "";
  173.     }
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TfrmMain::btnContinueDetailClick(TObject *Sender)
  177. {
  178.     if(edtTitle->Text.Trim().Length() < 1)
  179.     {
  180.         MessageBox(Handle, "单据标题必须填写!",
  181.                 g_strAppName.c_str(),
  182.                 MB_OK | MB_ICONWARNING);
  183.         pgcMain->ActivePageIndex = 1;
  184.         return;
  185.     }
  186.     pgcMain->ActivePageIndex = 2;
  187. }
  188. //---------------------------------------------------------------------------
  189. void __fastcall TfrmMain::btnOrderInfoClick(TObject *Sender)
  190. {
  191.     pgcMain->ActivePageIndex = 1;
  192. }
  193. //---------------------------------------------------------------------------
  194. void __fastcall TfrmMain::btnSaveAndPrintClick(TObject *Sender)
  195. {
  196.     qryOrder->Close();
  197.     qryOrder->SQL->Text = String().sprintf(
  198.             "select top 1 * from tblOrder where fBillNumber='%s'",
  199.             edtBillNumber->Text);
  200.     qryOrder->Open();
  201.     if(qryOrder->RecordCount)
  202.     {
  203.         MessageBox(Handle, "已经存在相同单据号的订单记录!",
  204.                 g_strAppName.c_str(), MB_OK | MB_ICONWARNING);
  205.         edtBillNumber->SetFocus();
  206.         return;
  207.     }
  208.     if(tblTemp->RecordCount < 1)
  209.     {
  210.         MessageBox(Handle, "至少要输入一条药品明细记录!",
  211.                 g_strAppName.c_str(), MB_OK | MB_ICONWARNING);
  212.         dbgrdTemp->SetFocus();
  213.         return;
  214.     }
  215.     qryOrder->Append();
  216.     qryOrder->FieldByName("fBillNumber")->AsString = edtBillNumber->Text;
  217.     qryOrder->FieldByName("fGenBillDate")->AsDateTime = dtpGenBillDate->Date;
  218.     qryOrder->FieldByName("fClient")->AsString = edtClient->Text;
  219.     qryOrder->FieldByName("fOpera")->AsString = edtOpera->Text;
  220.     qryOrder->FieldByName("fBillAuthor")->AsString = edtBillAuthor->Text;
  221.     qryOrder->FieldByName("fAddress")->AsString = edtAddress->Text;
  222.     qryOrder->FieldByName("fTel")->AsString = edtTel->Text;
  223.     qryOrder->FieldByName("fGath")->AsString = edtGath->Text;
  224.     qryOrder->FieldByName("fIssue")->AsString = edtIssue->Text;
  225.     qryOrder->FieldByName("fCheck")->AsString = edtCheck->Text;
  226.     qryOrder->FieldByName("fHotLine")->AsString = edtHotLine->Text;
  227.     qryOrder->FieldByName("fTitle")->AsString = edtTitle->Text;
  228.     qryOrder->Post();
  229.     qryDetail->Close();
  230.     qryDetail->SQL->Text = "select top 1 * from tblDetail";
  231.     qryDetail->Open();
  232.     int nCount = tblTemp->RecordCount;
  233.     tblTemp->First();
  234.     
  235.     for(int i=0; i<nCount; i++)
  236.     {
  237.         qryDetail->Append();
  238.         qryDetail->FieldByName("fBillNumber")->AsString = edtBillNumber->Text;
  239.         qryDetail->FieldByName("fBatchNumber")->AsString = tblTemp->FieldByName("fBatchNumber")->AsString;
  240.         qryDetail->FieldByName("fLeechdom")->AsString = tblTemp->FieldByName("fLeechdom")->AsString;
  241.         qryDetail->FieldByName("fSpec")->AsString = tblTemp->FieldByName("fSpec")->AsString;
  242.         qryDetail->FieldByName("fProFact")->AsString = tblTemp->FieldByName("fProFact")->AsString;
  243.         qryDetail->FieldByName("fUnit")->AsString = tblTemp->FieldByName("fUnit")->AsString;
  244.         qryDetail->FieldByName("fAmount")->AsInteger = tblTemp->FieldByName("fAmount")->AsInteger;
  245.         qryDetail->FieldByName("fUnitPrice")->AsCurrency = tblTemp->FieldByName("fUnitPrice")->AsCurrency;
  246.         qryDetail->FieldByName("fSum")->AsCurrency = tblTemp->FieldByName("fSum")->AsCurrency;
  247.         qryDetail->FieldByName("fRefer")->AsCurrency = tblTemp->FieldByName("fRefer")->AsCurrency;
  248.         qryDetail->FieldByName("fCasing")->AsString = tblTemp->FieldByName("fCasing")->AsString;
  249.         qryDetail->FieldByName("fBale")->AsString = tblTemp->FieldByName("fBale")->AsString;
  250.         qryDetail->Post();
  251.         tblTemp->Next();
  252.     }
  253.     if(nCount)
  254.     {
  255.         tblTemp->First();
  256.         while(!tblTemp->Eof)
  257.             tblTemp->Delete();
  258.     }
  259.     MessageBox(Handle,
  260.             String().sprintf("订单添加成功. 共有 %d 条药品明细.", nCount).c_str(),
  261.             g_strAppName.c_str(),
  262.             MB_OK | MB_ICONINFORMATION);
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TfrmMain::btnQueryAllClick(TObject *Sender)
  266. {
  267.     qryOrder->Close();
  268.     qryOrder->SQL->Text = "select * from tblOrder";
  269.     qryOrder->Open();
  270.     dbgrdOrderCellClick(NULL);
  271.     stbMain->Panels->Items[0]->Text = String().sprintf("共有 %d 条订单记录.", qryOrder->RecordCount);    
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TfrmMain::btnQueryClick(TObject *Sender)
  275. {
  276.     String strSQL("");
  277.     if(edtQueryByBillNumber->Text.Trim().Length() > 0)
  278.         strSQL += String().sprintf(" and fBillNumber='%s'", edtQueryByBillNumber->Text);
  279.     if(edtQueryByClient->Text.Trim().Length() > 0)
  280.         strSQL += String().sprintf(" and fClient like '%%%s%%'", edtQueryByClient->Text);
  281.     if(edtQueryByBillAuthor->Text.Trim().Length() > 0)
  282.         strSQL = String().sprintf(" and fBillAuthor='%s'", edtQueryByBillAuthor->Text);
  283.     if(edtQueryByOpera->Text.Trim().Length() > 0)
  284.         strSQL = String().sprintf(" and fOpera='%s'", edtQueryByOpera->Text);
  285.     if(chkQueryByDate->Checked)
  286.         strSQL = String().sprintf(" and fGenBillDate between #%s# and #%s#",
  287.                 FormatDateTime("yyy-mm-dd", dtpQueryStart->Date),
  288.                 FormatDateTime("yyy-mm-dd", dtpQueryEnd->Date + 1));
  289.     qryOrder->Close();
  290.     qryOrder->SQL->Text = "select * from tblOrder where 1=1" + strSQL;
  291.     qryOrder->Open();
  292.     if(qryOrder->RecordCount)
  293.     {
  294.         qryDetail->Close();
  295.         qryDetail->SQL->Text = String().sprintf(
  296.                 "select * from tblDetail where fBillNumber='%s'",
  297.                 qryOrder->FieldByName("fBillNumber")->AsString);
  298.         qryDetail->Open();
  299.     }
  300.     stbMain->Panels->Items[0]->Text = String().sprintf("共有 %d 条订单记录.", qryOrder->RecordCount);
  301. }
  302. //---------------------------------------------------------------------------
  303. void __fastcall TfrmMain::btnDeleteOrderClick(TObject *Sender)
  304. {
  305.     if(qryOrder->Active && qryOrder->RecordCount)
  306.     {
  307.         if(MessageBox(Handle,
  308.                 String().sprintf("确定要删除单据号为 %s 的这条记录吗? "
  309.                 "相关的药品明细记录也将删除.",
  310.                 qryOrder->FieldByName("fBillNumber")->AsString).c_str(),
  311.                 g_strAppName.c_str(),
  312.                 MB_YESNO | MB_ICONQUESTION) == IDYES)
  313.         {
  314.             //
  315.             qryDetail->Close();
  316.             qryDetail->SQL->Text = String().sprintf(
  317.                     "delete from tblDetail where fBillNumber='%s'",
  318.                     qryOrder->FieldByName("fBillNumber")->AsString);
  319.             qryDetail->ExecSQL();
  320.             //
  321.             qryOrder->Delete();
  322.         }
  323.         btnQueryAllClick(Sender);
  324.     }
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TfrmMain::btnDeleteDetailClick(TObject *Sender)
  328. {
  329.     if(qryDetail->Active && qryDetail->RecordCount)
  330.     {
  331.         if(MessageBox(Handle,
  332.                 String().sprintf("确定要删除批号为 %s 的这条记录吗?",
  333.                 qryDetail->FieldByName("fBatchNumber")->AsString).c_str(),
  334.                 g_strAppName.c_str(),
  335.                 MB_YESNO | MB_ICONQUESTION) == IDYES)
  336.         {
  337.             qryDetail->Delete();
  338.         }
  339.     }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TfrmMain::btnClearDetailClick(TObject *Sender)
  343. {
  344.     CrnClearOrderForm();
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TfrmMain::dbgrdOrderCellClick(TColumn *Column)
  348. {
  349.     if(qryOrder->RecordCount)
  350.     {
  351.         qryDetail->Close();
  352.         qryDetail->SQL->Text = String().sprintf(
  353.                 "select * "
  354.                 "from tblDetail "
  355.                 "where fBillNumber='%s'",
  356.                 qryOrder->FieldByName("fBillNumber")->AsString);
  357.         qryDetail->Open();
  358.     }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TfrmMain::btnSaveDetailClick(TObject *Sender)
  362. {
  363.     if(qryDetail->State == dsEdit || qryDetail->State == dsInsert)
  364.         qryDetail->Post();
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TfrmMain::btnSaveOrderClick(TObject *Sender)
  368. {
  369.     if(qryOrder->State == dsEdit || qryOrder->State == dsInsert)
  370.         qryOrder->Post();
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TfrmMain::btnDeleteTempClick(TObject *Sender)
  374. {
  375.     if(tblTemp->Active && tblTemp->RecordCount)
  376.     {
  377.         if(MessageBox(Handle,
  378.                 String().sprintf("确定要删除批号为 %s 的这条记录吗?",
  379.                 tblTemp->FieldByName("fBatchNumber")->AsString).c_str(),
  380.                 g_strAppName.c_str(),
  381.                 MB_YESNO | MB_ICONQUESTION) == IDYES)
  382.         {
  383.             tblTemp->Delete();
  384.         }
  385.     }
  386. }
  387. //---------------------------------------------------------------------------
  388. void __fastcall TfrmMain::btnSaveTempClick(TObject *Sender)
  389. {
  390.     if(tblTemp->State == dsEdit || tblTemp->State == dsInsert)
  391.         tblTemp->Post();
  392. }
  393. //---------------------------------------------------------------------------
  394. void __fastcall TfrmMain::qryDetailBeforePost(TDataSet *DataSet)
  395. {
  396.     if(DataSet->FieldByName("fBillNumber")->AsString.Trim().Length() < 1)
  397.     {
  398.         if(qryOrder->Active && qryOrder->RecordCount)
  399.             DataSet->FieldByName("fBillNumber")->AsString =
  400.                 qryOrder->FieldByName("fBillNumber")->AsString;
  401.     }
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TfrmMain::miDesignReportClick(TObject *Sender)
  405. {
  406.     frReport->LoadFromFile(ExtractFilePath(ParamStr(0)) + "Report.fr3");
  407.     frReport->DesignReport();
  408. }
  409. //---------------------------------------------------------------------------
  410. Variant __fastcall TfrmMain::frReportUserFunction(
  411.       const AnsiString MethodName, Variant &Params)
  412. {
  413. //    if(UpperCase(MethodName) == UpperCase("NumToRMBStr"))
  414. //        return NumToRMBStr(Params.GetElement(0));
  415.     return 0;
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TfrmMain::qryDetailAfterInsert(TDataSet *DataSet)
  419. {
  420.     DataSet->FieldByName("fAmount")->AsInteger = 0;
  421.     DataSet->FieldByName("fUnitPrice")->AsFloat = 0.0;
  422.     DataSet->FieldByName("fSum")->AsFloat = 0.0;
  423.     DataSet->FieldByName("fRefer")->AsFloat = 0.0;
  424. }
  425. //---------------------------------------------------------------------------
  426. void __fastcall TfrmMain::tblTempAfterInsert(TDataSet *DataSet)
  427. {
  428.     DataSet->FieldByName("fAmount")->AsInteger = 0;
  429.     DataSet->FieldByName("fUnitPrice")->AsFloat = 0.0;
  430.     DataSet->FieldByName("fSum")->AsFloat = 0.0;
  431.     DataSet->FieldByName("fRefer")->AsFloat = 0.0;    
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TfrmMain::tblTempfUnitPriceChange(TField *Sender)
  435. {
  436.     Sender->DataSet->FieldByName("FSum")->AsCurrency = Sender->AsCurrency
  437.             * Sender->DataSet->FieldByName("fAmount")->AsCurrency;
  438. }
  439. //---------------------------------------------------------------------------
  440. void __fastcall TfrmMain::tblTempfAmountChange(TField *Sender)
  441. {
  442.     Sender->DataSet->FieldByName("FSum")->AsCurrency = Sender->AsCurrency
  443.             * Sender->DataSet->FieldByName("fUnitPrice")->AsCurrency;
  444. }
  445. //---------------------------------------------------------------------------