[Igarden-commit] Garden/source/Reports PurchaseReport.h, NONE, 1.1.2.1 PurchaseReport.cpp, NONE, 1.
Status: Beta
Brought to you by:
jlbedell
|
From: Jeffrey B. <jlb...@us...> - 2006-11-15 03:56:34
|
Update of /cvsroot/igarden/Garden/source/Reports In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv5216 Added Files: Tag: Release_branch_v1 PurchaseReport.h PurchaseReport.cpp Log Message: Add PurchaseReport --- NEW FILE: PurchaseReport.cpp --- //*********************************************************************************************** // // The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at // http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF // ANY KIND, either express or implied. See the License for the specific language governing rights and // limitations under the License. // // The Original Code is iGarden xxxxxx. // // The Initial Developer of the Original Code is Redstart Software. // Portions created by Initial Developer are Copyright (C) 2004 Redstart Software. All Rights Reserved. // // Contributor(s): // Jeffrey Bedell(je...@re...) // /// 2) Plant Labels // Options: 3 popups: // Included Plots // Included Vendors // Sort By // 1 checkbox // Include pictures // No options interactions // Cell: full page across // Contains: // Canonical name (Common Name) #include "Persist.h" #include "PurchaseReport.h" #include "RGHypertextView.h" #include "RGReportHypertextCell.h" #include "ClientDocument.h" #include "Material.h" #include "MaterialRef.h" #include "InstanceData.h" #include "Layout.h" #include "Layer.h" #include "AttributeDictionary.h" #include "SourceMaterialFile.h" #include "GardenPlot.h" #include "Path.h" #include "DocumentTags.h" #include <XGCheckButton.h> #include <XGPopControl.h> #include <XGStaticText.h> #include <XGCommandButton.h> #include <XErrorDialogs.h> const short kPurchasePane = 191; const short kIncludedPlots = 10; const short kIncludedVendors = 11; const short kSortBy = 12; const short kFormat = 13; using yaaf::XGCheckButton; using yaaf::XGPopControl; using namespace std; class StringSet { map<String, bool> _strings; public: void AddString(const String& str); void AddStringSet(StringSet& str); void GetStringRepresentation(String& buf); }; class SiftedData { public: String _name; StringSet _suppliers; StringSet _locations; uint32 _count; double _volume; measureUnits_t _units; SiftedData(); SiftedData(String& name, StringSet &suppliers, String& locations, long count); SiftedData(String& name, StringSet &suppliers, String& locations, double volume, measureUnits_t units); void AddSuppliers(StringSet &suppliers); void AddPlot(String &plot); bool operator<(const SiftedData s1) const; }; typedef map<SiftedData, SiftedData> siftedList_t; #pragma mark *** PurchaseReport *** // --------------------------------------------------------------------------- /// PurchaseReport // PurchaseReport::PurchaseReport(ClientDocument *document) : RGHyperReport(document) { _AllPlots = true; _AllVendors = true; _showPictures = false; } // --------------------------------------------------------------------------- /// ~PurchaseReport // PurchaseReport::~PurchaseReport() { } const long kPlotMenuOffset = 2; const long kVendorMenuOffset = 2; // --------------------------------------------------------------------------- /// Setup // void PurchaseReport::Setup(RGDialog *dialog, XGDialog *dlog) { XGPopControl *pop; long count, n, numLayers; XGCheckButton *btn; XGUUID nilID; GardenPlot *plot; Layout *layout; Layer *layer; SourceMaterialFile *sourceDB = SourceMaterialFile::GetSourceDatabase(); layout = _doc->GetLayout(); //*** pop = dynamic_cast<XGPopControl*>(dlog->FindViewByID(kIncludedPlots)); AssertValidView(pop); pop->DeleteAll(); pop->Insert(0, "All Plots"); pop->Insert(1, "-"); count = layout->NumPlots(); for(n = 0; n < count; n++) { String plotName; plot = layout->GetIndexedPlot(n); MyAssert(plot != 0); plot->GetName(plotName); pop->Insert(n+kPlotMenuOffset, plotName.c_str()); } pop = dynamic_cast<XGPopControl*>(dlog->FindViewByID(kIncludedVendors)); AssertValidView(pop); pop->DeleteAll(); pop->Insert(0, "All Vendors"); pop->Insert(1, "-"); count = sourceDB->NumVendors(inDocumentOnly, showHiddenItems); for(n = 0; n < count; n++) { InfoSupplier *vendor; String vendorName; vendor = sourceDB->GetIndexedVendor(n, inDocumentOnly, showHiddenItems); MyAssert(vendor != 0); pop->Insert(n+kPlotMenuOffset, vendor->GetCompanyName().c_str()); } pop = dynamic_cast<XGPopControl*>(dlog->FindViewByID(kSortBy)); AssertValidView(pop); pop->DeleteAll(); pop->Insert(0, "Name"); pop->Insert(1, "Vendor"); pop->Insert(2, "Garden Plot"); pop->EnableView(); RebuildTemplates(); pop = dynamic_cast<XGPopControl*>(dlog->FindViewByID(kFormat)); count = _templates.Length(); for(n = 0; n < count; n++) { pop->Insert(n, _templates[n].name->c_str()); } _currentItem = 0; // Include unused? // layout->GetUnusedLayer()->AddToMaterialRefArray(&_list, nilID); count = layout->NumPlots(); for(n = 0; n < count; n++) { plot = layout->GetIndexedPlot(n); MyAssert(plot != 0); numLayers = plot->GetNumLayers(); for(n = 0; n < numLayers; n++) { layer = plot->GetIndexedLayer(n); if(_AllPlots) layer->AddToPathELementArray(&_list, nilID); else layer->AddToPathELementArray(&_list, _plotID); } } XGCommandButton *printBtn = dynamic_cast<XGCommandButton*>(dlog->FindViewByID(10001)); MyAssert(printBtn != NULL); if(AllowPrint()) printBtn->EnableView(); else printBtn->DisableView(); } // --------------------------------------------------------------------------- /// OKClicked // void PurchaseReport::OKClicked(RGDialog *dialog, XGDialog *dlog) { } // --------------------------------------------------------------------------- /// ControlClick // void PurchaseReport::ControlClick(RGDialog *dialog, XGDialog *dlog, long viewID) { } // --------------------------------------------------------------------------- /// PopupValue // void PurchaseReport::PopupValue(RGDialog *dialog, XGDialog *dlog, long viewID) { XGPopControl *btn; long val; btn = dynamic_cast<XGPopControl*>(dlog->FindViewByID(viewID)); AssertValidView(btn); val = btn->GetValue(); switch(viewID) { case kIncludedPlots: if(val == 0) _AllPlots = true; else { GardenPlot *plot; Layout *layout; layout = _doc->GetLayout(); _AllPlots = false; plot = layout->GetIndexedPlot(val-kPlotMenuOffset); MyAssert(plot != NULL); plot->getPlotID(_plotID); } break; case kIncludedVendors: if(val == 0) _AllVendors = true; else { InfoSupplier *supplier; _AllVendors = false; supplier = SourceMaterialFile::GetSourceDatabase()->GetIndexedVendor(val-kVendorMenuOffset, inDocumentOnly, showHiddenItems); MyAssert(supplier != NULL); supplier->GetSupplierID(_vendorID); } break; case kFormat: _currentItem = val; break; case kSortBy: break; } XGPopControl *sortCtl = dynamic_cast<XGPopControl*>(dlog->FindViewByID(kSortBy)); AssertValidView(sortCtl); if(_AllPlots && _AllVendors) sortCtl->EnableView(); else { sortCtl->SetValue(_AllPlots ? 1 : 0); sortCtl->DisableView(); } XGCommandButton *printBtn = dynamic_cast<XGCommandButton*>(dlog->FindViewByID(10001)); MyAssert(printBtn != NULL); if(AllowPrint()) printBtn->EnableView(); else printBtn->DisableView(); } // --------------------------------------------------------------------------- /// HeaderHeight // UInt32 PurchaseReport::HeaderHeight(XGDraw &draw) { #pragma unused(draw) return 30; } // --------------------------------------------------------------------------- /// PrintHeader // void PurchaseReport::PrintHeader(XGDraw &draw, UInt32 page) { #pragma unused(draw, page) } // --------------------------------------------------------------------------- /// ReportName // String PurchaseReport::ReportName() { return String("Shopping List"); } // --------------------------------------------------------------------------- /// BodyFontID // long PurchaseReport::BodyFontID() { return 140; } // --------------------------------------------------------------------------- /// AllowPrint // bool PurchaseReport::AllowPrint() { bool found = false; UInt32 n, numRows; Material *mat; MaterialRef *ref; InstanceData *instance; XGUUID plotID; numRows = _list.Length(); for(n = 0; n < numRows && !found; n++) { ref = dynamic_cast<MaterialRef*>(_list[n]); if(ref != NULL) { mat = ref->GetMaterial(ifMissingNULL); if(mat != NULL) { if(_AllPlots) found = true; else { instance = ref->LatestInstanceData(kInstanceSupplierIDs); if(instance != NULL) { uint32 supplier, supplierCount; XGUUID testID; supplierCount = instance->GetNumSuppliers(); for(supplier = 0; supplier < supplierCount && !found; supplier++) { instance->GetIndexedSupplier(supplier, testID); if(testID == _vendorID) found = true; } if(!found) { ref->GetPlotID(plotID); if(_plotID == plotID) found = true; } } else found = true; } } } } return found; } // --------------------------------------------------------------------------- /// ComputeReport // void PurchaseReport::ComputeReport() { RGReportHypertextCell *cell; Point cellSize; UInt32 n, numRows, index, numPlots, count, numLayers; Material *mat; String text; GardenDate date; char output[1024]; const char *comments, *namePtr; ShopTemplate_t format; Layout *layout; MaterialRef *ref; GardenPlot *plot; XGUUID nilID, plotID; siftedList_t siftedList; Path *path; StringSet supplierSet; String plotName, name; InstanceData *instance; SourceMaterialFile *sourceDB = SourceMaterialFile::GetSourceDatabase(); XGUUID testPlotID; PathElement *elem; bool show; format = _templates[_currentItem]; cellSize = InchesToPixels(8, 1); layout = _doc->GetLayout(); text = String(""); numRows = _list.Length(); for(n = 0; n < numRows; n++) { elem = _list[n]; elem->GetPlot()->getPlotID(plotID); if(_plotID != plotID && !_AllPlots) continue; ref = dynamic_cast<MaterialRef*>(elem); if(ref != NULL) { mat = ref->GetMaterial(ifMissingNULL); if(mat != NULL) { name = mat->GetDisplayName(kUsePreferencesSetting); // printf("Item [%ld] = '%s'\n", n, name.c_str()); instance = ref->LatestInstanceData(kInstanceSupplierIDs); if(instance != NULL) { uint32 supplierIndex, supplierCount; XGUUID testID; if(_AllVendors) { supplierCount = instance->GetNumSuppliers(); for(supplierIndex = 0; supplierIndex < supplierCount; supplierIndex++) { String companyName; InfoSupplier *supplier; instance->GetIndexedSupplier(supplierIndex, testID); supplier = sourceDB->LookupSupplier(testID); companyName = supplier->GetCompanyName(); supplierSet.AddString(companyName); } show = true; } else { show = false; supplierCount = instance->GetNumSuppliers(); for(supplierIndex = 0; supplierIndex < supplierCount; supplierIndex++) { InfoSupplier *supplier; instance->GetIndexedSupplier(supplierIndex, testID); if(testID == _vendorID) { String str; supplier = sourceDB->LookupSupplier(testID); str = supplier->GetCompanyName(); supplierSet.AddString(str); show = true; break; } } } } ref->GetPlotID(plotID); numPlots = layout->NumPlots(); plotName = L""; for(index = 0; index < numPlots; index++) { plot = layout->GetIndexedPlot(index); plot->getPlotID(testPlotID); if(plotID == testPlotID) { plot->GetName(plotName); break; } } if(show) { siftedList_t::iterator cur; SiftedData data(name, supplierSet, plotName, ref->GetNumItems()); cur = siftedList.find(data); if(cur == siftedList.end()) { siftedList[data] = data; // printf("Setting value for '%s' to %ld\n", namePtr, data.count); } else { (*cur).second._count += ref->GetNumItems(); (*cur).second.AddSuppliers(supplierSet); (*cur).second.AddPlot(plotName); // printf("Changing value for '%s' to %ld\n", namePtr, (*cur).second.count); } } } } path = elem->GetPathIfPresent(); if(path != NULL) { mat = path->GetBackdrop(); if(mat != 0) { if(mat->GetMaterialType() == kMaterialTypeMulch) { if(path->IsPathFillPossible()) { measureUnits_t units = (measureUnits_t)_doc->GetPrefsDB()->GetInt32Pref(kUnitsPref); double cubicMM, volume; StringSet mulchSuppliers; cubicMM = path->ComputeArea() * path->GetBackdropDepth(); // mulch sold as cubic yard in US, cubic metre in metric. if(units == kEnglishUnits) { volume = cubicMM / 764554857.984; } else { volume = cubicMM / 1000000000; } siftedList_t::iterator cur; name = mat->GetDisplayName(kUsePreferencesSetting); SiftedData data(name, mulchSuppliers, plotName, volume, units); cur = siftedList.find(data); if(cur == siftedList.end()) { siftedList[data] = data; // printf("Setting value for '%s' to %ld\n", namePtr, data.count); } else { (*cur).second._volume += volume; (*cur).second.AddPlot(plotName); // printf("Changing value for '%s' to %ld\n", namePtr, (*cur).second.count); } } } } } } siftedList_t::iterator iter; for(iter = siftedList.begin(); iter != siftedList.end(); iter++) { String supplierNames, plotNames; char numBuf[64]; namePtr = (*iter).first._name.c_str(); (*iter).second._suppliers.GetStringRepresentation(supplierNames); (*iter).second._locations.GetStringRepresentation(plotNames); if((*iter).second._count == 0) sprintf(numBuf, "%6.2lf %s", (*iter).second._volume, (*iter).second._units == kMetricUnits ? "Cubic Meters" : "Cubic Yards"); else sprintf(numBuf, "%ld", (*iter).second._count); FormatBuffer(output, format.vfmt, namePtr, supplierNames.c_str(), plotNames.c_str(), numBuf, ""); cell = new RGReportHypertextCell(this, _ht, cellSize, output); cell->SetUseBorder(true); _cells.push_back(cell); } } // --------------------------------------------------------------------------- /// FormatBuffer // void PurchaseReport::FormatBuffer(char *out, const char *format, const char *name, const char *vendors, const char *where, const char *countStr, const char *picture) { yformat(out, format, name, vendors, where, countStr, picture); } // --------------------------------------------------------------------------- /// CreatePurchaseReport // void PurchaseReport::CreatePurchaseReport(ClientDocument *doc) { PurchaseReport *report; report = new PurchaseReport(doc); report->Run(kPurchasePane); } // --------------------------------------------------------------------------- /// IsMyFile // bool PurchaseReport::IsMyFile(char *nameBuf) { return (CIstrncmp(nameBuf, "ShopList", 5) == 0); } // --------------------------------------------------------------------------- /// RebuildOneTemplate // void PurchaseReport::RebuildOneTemplate(XGFile &file) { long fileLen; char *fileData; ShopTemplate_t templ; String str; fileLen = file.GetEOF(); if(fileLen < 100000) { fileData = new char[fileLen+1]; fileData[fileLen] = '\0'; file.Read(fileLen, fileData); ExtractTitle(fileData, str); templ.name = new String(str); /****/ ExtractTag(fileData, "Border", str); templ.hasBorder = (CIstrcmp(str.c_str(), "true") == 0); /****/ ExtractTag(fileData, "Picture", str); templ.hasBorder = (CIstrcmp(str.c_str(), "true") == 0); /****/ templ.vfmt = fileData; _templates.push_back(templ); } } // --------------------------------------------------------------------------- /// DeleteTemplates // void PurchaseReport::DeleteTemplates() { long n, count; count = _templates.Length(); for(n = 0; n < count; n++) { delete _templates[n].name; delete [] _templates[n].vfmt; } _templates.DeleteAll(); } #pragma mark *** StringSet (PRIVATE) *** // --------------------------------------------------------------------------- /// StringSet::AddString // void StringSet::AddString(const String& str) { _strings[str] = true; } // --------------------------------------------------------------------------- /// StringSet::AddStringSet // void StringSet::AddStringSet(StringSet& str) { map<String, bool>::iterator iter; for(iter = str._strings.begin(); iter != str._strings.end(); iter++) { _strings[(*iter).first] = true; } } // --------------------------------------------------------------------------- /// StringSet::GetStringRepresentation // void StringSet::GetStringRepresentation(String& buf) { bool doComma = false; map<String, bool>::iterator iter; String str; for(iter = _strings.begin(); iter != _strings.end(); iter++) { str = (*iter).first; if(doComma) buf += L", "; buf += str; doComma = true; } } #pragma mark *** SiftedData (PRIVATE) *** // --------------------------------------------------------------------------- /// SiftedData::SiftedData // SiftedData::SiftedData() { _count = 0; _volume = 0.0; _units = kMetricUnits; } // --------------------------------------------------------------------------- /// SiftedData::SiftedData // SiftedData::SiftedData(String& name, StringSet &suppliers, String& locations, double volume, measureUnits_t units) { _name = name; AddSuppliers(suppliers); AddPlot(locations); _count = 0; _volume = volume; _units = units; } // --------------------------------------------------------------------------- /// SiftedData::SiftedData // SiftedData::SiftedData(String& name, StringSet &suppliers, String& locations, long count) { _name = name; AddSuppliers(suppliers); AddPlot(locations); _count = count; _volume = 0.0; _units = kMetricUnits; } // --------------------------------------------------------------------------- /// SiftedData::AddSuppliers // void SiftedData::AddSuppliers(StringSet &suppliers) { _suppliers.AddStringSet(suppliers); } // --------------------------------------------------------------------------- /// SiftedData::AddPlot // void SiftedData::AddPlot(String &plot) { _locations.AddString(plot); } // --------------------------------------------------------------------------- /// SiftedData::operator< // bool SiftedData::operator<(const SiftedData s1) const { return _name < s1._name; } --- NEW FILE: PurchaseReport.h --- //*********************************************************************************************** // // The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at // http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF // ANY KIND, either express or implied. See the License for the specific language governing rights and // limitations under the License. // // The Original Code is iGarden xxxxxx. // // The Initial Developer of the Original Code is Redstart Software. // Portions created by Initial Developer are Copyright (C) 2004 Redstart Software. All Rights Reserved. // // Contributor(s): // Jeffrey Bedell(je...@re...) // #ifndef PURCHASE_REPORT_H #define PURCHASE_REPORT_H #include "RGHyperReport.h" #include "Layout.h" #include <XGSDynArray.h> #include <map> #include "RGString.h" using yaaf::XGSDynArray; typedef struct { String *name; bool hasBorder; bool hasPicture; char *vfmt; } ShopTemplate_t; class PurchaseReport : public RGHyperReport { public: PurchaseReport(ClientDocument *document); virtual ~PurchaseReport(); virtual void Setup(RGDialog *dialog, XGDialog *dlog); virtual void OKClicked(RGDialog *dialog, XGDialog *dlog); virtual void ControlClick(RGDialog *dialog, XGDialog *dlog, long viewID); virtual void PopupValue(RGDialog *dialog, XGDialog *dlog, long viewID); virtual UInt32 HeaderHeight(XGDraw &draw); virtual void PrintHeader(XGDraw &draw, UInt32 page); // The report name is displayed at the top of the dialogs, and by default // Centered in the header virtual String ReportName(); // Compute all of the cells, and fill in the array virtual void ComputeReport(); static void CreatePurchaseReport(ClientDocument *doc); virtual long BodyFontID(); virtual bool IsMyFile(char *nameBuf); virtual void RebuildOneTemplate(XGFile &file); virtual void DeleteTemplates(); private: void FormatBuffer(char *out, const char *format, const char *name, const char *vendors, const char *where, const char *countStr, const char *picture); bool AllowPrint(); bool _AllPlots; bool _AllVendors; XGUUID _plotID; XGUUID _vendorID; bool _showPictures; long _currentItem; XGSDynArray<ShopTemplate_t> _templates; pathELementArray_t _list; }; #endif // PURCHASE_REPORT_H |