#include "campservice.h" #include #include #include #include "campshopitem.h" #include "campseller.h" #include CampService::CampService() { m_pluginId = "CAMP"; m_seller = new CampSeller(this); } void CampService::addPerson(CampDataPtr data, AddressbookDataPtr address) { AddressItemPtr addrItem(new AddressItem); addrItem->setAdbItem(address); addrItem->setAddress(address->toString()); addrItem->setFirstName(address->firstName()); addrItem->setLastName(address->lastName()); addrItem->setCampData(data); if (data->people().isEmpty()) { setOwner(data, addrItem); } data->addPerson(addrItem); } void CampService::addService(CampDataPtr data, AccServicePtr service) { ServiceItemPtr serviceItem(new ServiceItem); serviceItem->setName(service->accServiceName()); serviceItem->setCode(service->accServiceCode()); serviceItem->setPrice(service->price()); serviceItem->setSalePossible(service->salePossible()); serviceItem->setType(service->serviceType()); data->addServiceItem(serviceItem); } void CampService::addService(CampDataPtr data, AccServicePtr service, QDecDouble price, QString description) { ServiceItemPtr item = addServiceInt(data, service); item->setPrice(price); item->setDescription(description); } void CampService::setOwner(CampDataPtr data, AddressItemPtr person) { foreach (AddressItemPtr p, data->people()) { p->setOwner(false); } person->setOwner(true); data->setOwnerFirstame(person->firstName()); data->setOwnerLastname(person->lastName()); data->setOwnerAddress(person->address()); } CampDataPtr CampService::create() { CampDataPtr data(new CampData); data->setStart(QDate::currentDate()); data->setEnd(QDate::currentDate()); return data; } void CampService::calculate(CampDataPtr data) { SettingsService srv("CAMP"); m_settings = srv.loadSettings(); calcServices(data); calcPeople(data); calcPrice(data); } void CampService::saveCamp(CampDataPtr data) { if (!checkPermission(PERM_ADD)) { return; } SeasonService seasonSrv; SeasonPtr season = seasonSrv.active(); data->setSeason(season); Transaction tr; try { odb::database *db = Context::instance().db(); NumberSeriesService numSrv; data->setNumSer(numSrv.nextStrForPlugin("CAMP")); db->persist(data); foreach (ServiceItemPtr item, data->services()) { item->setCampData(data.toWeakRef()); db->persist(item); } foreach (AddressItemPtr item, data->people()) { item->setCampData(data.toWeakRef()); db->persist(item); } tr.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); emit dbErrorUpdate(ex.what()); return; } } void CampService::calcPeople(CampDataPtr data) { foreach (ServiceItemPtr service, data->services()) { if (service->type() == AccService::ACCFEE) { data->removeServiceItem(service); } } Service srvPrices; QList prices = srvPrices.all("active = 1"); int days = data->start().daysTo(data->end()); foreach (AddressItemPtr item, data->people()) { QDate first(1,1,1); qint64 daysStart = item->adbItem()->birthDate().daysTo(data->start()); first = first.addDays(daysStart); int startAge = first.year() - 1; first = QDate(1,1,1); qint64 daysEnd = item->adbItem()->birthDate().daysTo(data->end()); first = first.addDays(daysEnd); int endAge = first.year() - 1; if (!item->personPrice().isNull()) { item->setPrice(item->personPrice()->price() * days); addAccFee(data, item, startAge, endAge, days); continue; } else { item->setPrice(0); } foreach (PersonPricePtr price, prices) { if (price->fromAge() <= endAge && price->toAge() >= endAge) { item->setPersonPrice(price); item->setPrice(price->price() * days); break; } } if (item->adbItem()->ztp()) { continue; } addAccFee(data, item, startAge, endAge, days); } } void CampService::calcServices(CampDataPtr data) { QDecDouble sale = data->sale(); bool fixedSale = data->fixedSale(); int days = data->start().daysTo(data->end()); foreach (ServiceItemPtr item, data->services()) { item->setFullPrice(item->price() * days); if (sale != QDecDouble(0) && !fixedSale && item->salePossible()) { QDecDouble itemSale = (item->fullPrice() * sale) / 100; item->setSale(itemSale); item->setTotalPrice(item->fullPrice() - itemSale); } else { item->setSale(0); item->setTotalPrice(item->fullPrice()); } } } void CampService::calcPrice(CampDataPtr data) { QDecDouble totalPrice(0); QDecDouble sale(0); foreach (ServiceItemPtr service, data->services()) { totalPrice += service->totalPrice(); sale += service->sale(); } foreach (AddressItemPtr addr, data->people()) { totalPrice += addr->price(); } if (data->fixedSale()) { totalPrice -= data->sale(); sale = data->sale(); } switch (m_settings->rounding()) { case Enums::R_UP: totalPrice = QDecDouble(ceil(totalPrice.toDouble() * pow(10, m_settings->decimalPlaces())) / pow(10, m_settings->decimalPlaces())); break; case Enums::R_DOWN: totalPrice = QDecDouble(floor(totalPrice.toDouble() * pow(10, m_settings->decimalPlaces())) / pow(10, m_settings->decimalPlaces())); break; case Enums::R_MATH: totalPrice = QDecDouble(round(totalPrice.toDouble() * pow(10, m_settings->decimalPlaces())) / pow(10, m_settings->decimalPlaces())); break; case Enums::R_NONE: break; } data->setTotalPrice(totalPrice); data->setTotalSale(sale); } void CampService::addAccFee(CampDataPtr data, AddressItemPtr item, int startAge, int endAge, int days) { auto addAccService = [this, item, data](int count){ ServiceItemPtr srvItem(new ServiceItem); srvItem->setName(item->firstName() + " " + item->lastName()); srvItem->setDescription(m_settings->accFeeText()); srvItem->setPrice(m_settings->accFee()); srvItem->setTotalPrice(m_settings->accFee() * count); srvItem->setFullPrice(srvItem->totalPrice()); srvItem->setType(AccService::ACCFEE); srvItem->setSalePossible(false); srvItem->setCampData(data); data->addServiceItem(srvItem); }; if (startAge == endAge || (startAge >= m_settings->accFeeStartAge() && startAge <= m_settings->accFeeEndAge())) { if (endAge >= m_settings->accFeeStartAge() && endAge <= m_settings->accFeeEndAge()) { addAccService(days); } } else { if (endAge >= m_settings->accFeeStartAge() && endAge <= m_settings->accFeeEndAge()) { QDate tmp(data->end().year(), item->adbItem()->birthDate().month(), item->adbItem()->birthDate().day()); int count = tmp.daysTo(data->end()); addAccService(count); } } } QList CampService::shopItems() { CampShopItemPtr item(new CampShopItem); QList items; items.append(item); return items; } ShopItemPtr CampService::shopItem(int ) { return CampShopItemPtr(new CampShopItem); } void CampService::addedToVoucher(int , int ) { } ISeller *CampService::seller() { return m_seller; } ServiceItemPtr CampService::addServiceInt(CampDataPtr data, AccServicePtr service) { ServiceItemPtr serviceItem(new ServiceItem); serviceItem->setName(service->accServiceName()); serviceItem->setCode(service->accServiceCode()); serviceItem->setPrice(service->price()); serviceItem->setSalePossible(service->salePossible()); serviceItem->setType(service->serviceType()); serviceItem->setCampData(data); data->addServiceItem(serviceItem); return serviceItem; }