1 | /* |
---|
2 | Copyright (C) 2012 |
---|
3 | Alejandro Mujica (amujica@cenditel.gob.ve) |
---|
4 | Erwin Paredes (eparedes@cenditel.gob.ve) |
---|
5 | José Ruiz (jruiz@cenditel.gob.ve) |
---|
6 | Rodolfo Rangel (rrangel@cenditel.gob.ve) |
---|
7 | Julie Vera (jvera@cenditel.gob.ve) |
---|
8 | |
---|
9 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
10 | Tecnologías Libres |
---|
11 | |
---|
12 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
13 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
14 | |
---|
15 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
16 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
17 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
18 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
19 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
20 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | Autor: Alejandro J. Mujica |
---|
25 | Fecha de creación: 01/08/2013 |
---|
26 | Este archivo contiene la implementación de un conjunto de consultas a base de |
---|
27 | datos utilizadas a lo largo del proyecto. |
---|
28 | */ |
---|
29 | |
---|
30 | # include <sstream> |
---|
31 | # include <stdexcept> |
---|
32 | |
---|
33 | # include <tpl_dynDlist.H> |
---|
34 | |
---|
35 | # include <queries.H> |
---|
36 | # include <dbQuery.H> |
---|
37 | |
---|
38 | # include <strQuery.H> |
---|
39 | |
---|
40 | std::string singleQuote(const std::string & str) { |
---|
41 | return "'" + str + "'"; |
---|
42 | } |
---|
43 | |
---|
44 | std::string doubleQuote(const std::string & str) { |
---|
45 | return "\"" + str + "\""; |
---|
46 | } |
---|
47 | |
---|
48 | std::string concat(const std::string & table, const std::string & field, |
---|
49 | const char & connector) { |
---|
50 | return table + connector + field; |
---|
51 | } |
---|
52 | |
---|
53 | std::string as(const std::string & field, const std::string & alias) { |
---|
54 | return field + " AS " + alias; |
---|
55 | } |
---|
56 | |
---|
57 | std::string concatas(const std::string & table, const std::string & field, |
---|
58 | const std::string & alias) { |
---|
59 | return as(concat(table, field), alias); |
---|
60 | } |
---|
61 | |
---|
62 | std::string concatas(const std::string & table, const std::string & field) { |
---|
63 | return concatas(table, field, concat(table, field, '_')); |
---|
64 | } |
---|
65 | |
---|
66 | std::string op(const std::string & attr, const std::string & value, |
---|
67 | const std::string & oper) { |
---|
68 | return attr + " " + oper + " " + value; |
---|
69 | } |
---|
70 | |
---|
71 | std::string idToStr(const size_t & id) { |
---|
72 | |
---|
73 | std::stringstream sstr; |
---|
74 | |
---|
75 | sstr << id; |
---|
76 | |
---|
77 | return sstr.str(); |
---|
78 | } |
---|
79 | |
---|
80 | std::string between(const std::string & field, const std::string & beg, |
---|
81 | const std::string & end) { |
---|
82 | return field + " BETWEEN " + singleQuote(beg) + " AND " + singleQuote(end); |
---|
83 | } |
---|
84 | |
---|
85 | std::string inYear(const std::string & field, const std::string & year) { |
---|
86 | |
---|
87 | std::string beg = year + "-01-01"; |
---|
88 | |
---|
89 | std::string end = year + "-12-31"; |
---|
90 | |
---|
91 | return between(field, beg, end); |
---|
92 | } |
---|
93 | |
---|
94 | ProductInfo loadInfoByProductId(AutoConnection & conn, const size_t & id) { |
---|
95 | |
---|
96 | DBQuery query(conn); |
---|
97 | |
---|
98 | StrQuery strQuery; |
---|
99 | |
---|
100 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
101 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::NAME)); |
---|
102 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, |
---|
103 | DB::TECHNICAL_SPECIFICATION)); |
---|
104 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE)); |
---|
105 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::TRADEMARK)); |
---|
106 | strQuery.addSelect(concatas(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME)); |
---|
107 | strQuery.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::ID)); |
---|
108 | strQuery.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::NAME)); |
---|
109 | strQuery.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::USED_CAPACITY)); |
---|
110 | strQuery.addSelect(concatas(DB::COMPANY_TABLE_NAME, DB::RIF)); |
---|
111 | strQuery.addSelect(concatas(DB::COMPANY_TABLE_NAME, DB::COMPANY_NAME)); |
---|
112 | |
---|
113 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
114 | strQuery.addFrom(DB::MEASUREMENT_UNIT_TABLE_NAME); |
---|
115 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
116 | strQuery.addFrom(DB::COMPANY_TABLE_NAME); |
---|
117 | |
---|
118 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID), |
---|
119 | idToStr(id))); |
---|
120 | strQuery.addWhere(op(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::ID), |
---|
121 | concat(DB::PRODUCT_TABLE_NAME, |
---|
122 | DB::MEASUREMENT_UNIT_ID))); |
---|
123 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
124 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
125 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
126 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
127 | strQuery.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF), |
---|
128 | concat(DB::PLANT_TABLE_NAME, DB::UE_RIF))); |
---|
129 | |
---|
130 | if (not query.exec(strQuery)) { |
---|
131 | |
---|
132 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
133 | |
---|
134 | throw std::logic_error(msg); |
---|
135 | } |
---|
136 | |
---|
137 | ProductInfo info; |
---|
138 | |
---|
139 | if (not query.next()) |
---|
140 | return info; |
---|
141 | |
---|
142 | info.plantId = |
---|
143 | std::atol(query.getValue(concat(DB::PLANT_TABLE_NAME, DB::ID, '_'))); |
---|
144 | info.plantName = |
---|
145 | query.getValue(concat(DB::PLANT_TABLE_NAME, DB::NAME, '_')); |
---|
146 | info.usedCapacity = |
---|
147 | std::atof(query.getValue(concat(DB::PLANT_TABLE_NAME, |
---|
148 | DB::USED_CAPACITY, '_'))); |
---|
149 | info.companyName = |
---|
150 | query.getValue(concat(DB::COMPANY_TABLE_NAME, DB::COMPANY_NAME, '_')); |
---|
151 | info.companyRif = |
---|
152 | query.getValue(concat(DB::COMPANY_TABLE_NAME, DB::RIF, '_')); |
---|
153 | info.productId |
---|
154 | = std::atol(query.getValue(concat(DB::PRODUCT_TABLE_NAME, |
---|
155 | DB::ID, '_'))); |
---|
156 | info.name = query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::NAME, '_')); |
---|
157 | info.technicalSpecifications = |
---|
158 | query.getValue(concat(DB::PRODUCT_TABLE_NAME, |
---|
159 | DB::TECHNICAL_SPECIFICATION, '_')); |
---|
160 | info.tariffCode = |
---|
161 | query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE, '_')); |
---|
162 | info.measurementUnit = |
---|
163 | query.getValue(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME, '_')); |
---|
164 | info.trademark = |
---|
165 | query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::TRADEMARK, '_')); |
---|
166 | |
---|
167 | return info; |
---|
168 | } |
---|
169 | |
---|
170 | void listInputIds(AutoConnection & conn, const size_t & productId, |
---|
171 | const std::string & year, List <size_t> & inputIds) { |
---|
172 | |
---|
173 | DBQuery query(conn); |
---|
174 | |
---|
175 | StrQuery strQuery; |
---|
176 | |
---|
177 | strQuery.addSelect(DB::INPUT_ID); |
---|
178 | strQuery.addFrom(DB::PRODUCT_INPUT_TABLE_NAME); |
---|
179 | strQuery.addWhere(op(DB::PRODUCT_ID, idToStr(productId))); |
---|
180 | strQuery.addWhere(op(DB::QUANTITY, "0", "<>")); |
---|
181 | strQuery.addWhere(op(DB::YEAR, year)); |
---|
182 | |
---|
183 | if (not query.exec(strQuery)) { |
---|
184 | |
---|
185 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
186 | |
---|
187 | throw std::logic_error(msg); |
---|
188 | } |
---|
189 | |
---|
190 | while (query.next()) |
---|
191 | inputIds.append(std::atol(query.getValue(DB::INPUT_ID))); |
---|
192 | } |
---|
193 | |
---|
194 | void listInputs(AutoConnection & conn, const size_t & inputId, |
---|
195 | const std::string & year, List <InputInfo> & inputs) { |
---|
196 | |
---|
197 | DBQuery query(conn); |
---|
198 | |
---|
199 | StrQuery strQuery; |
---|
200 | |
---|
201 | strQuery.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::ID)); |
---|
202 | strQuery.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::NAME)); |
---|
203 | strQuery.addSelect(concatas(DB::INPUT_TABLE_NAME, |
---|
204 | DB::TECHNICAL_SPECIFICATION)); |
---|
205 | strQuery.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE)); |
---|
206 | strQuery.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::TRADEMARK)); |
---|
207 | strQuery.addSelect(concatas(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME)); |
---|
208 | |
---|
209 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
210 | strQuery.addFrom(DB::MEASUREMENT_UNIT_TABLE_NAME); |
---|
211 | |
---|
212 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID), |
---|
213 | idToStr(inputId))); |
---|
214 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, |
---|
215 | DB::MEASUREMENT_UNIT_ID), |
---|
216 | concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::ID))); |
---|
217 | |
---|
218 | if (not query.exec(strQuery)) { |
---|
219 | |
---|
220 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
221 | |
---|
222 | throw std::logic_error(msg); |
---|
223 | } |
---|
224 | |
---|
225 | if (not query.next()) { |
---|
226 | |
---|
227 | std::stringstream msg; |
---|
228 | |
---|
229 | msg << "Input with id = " << inputId << " does not exists in database"; |
---|
230 | |
---|
231 | throw std::domain_error(msg.str()); |
---|
232 | } |
---|
233 | |
---|
234 | InputInfo inputInfo; |
---|
235 | |
---|
236 | inputInfo.id = |
---|
237 | std::atol(query.getValue(concat(DB::INPUT_TABLE_NAME, DB::ID, '_'))); |
---|
238 | inputInfo.name = |
---|
239 | query.getValue(concat(DB::INPUT_TABLE_NAME, DB::NAME, '_')); |
---|
240 | inputInfo.technicalSpecifications = |
---|
241 | query.getValue(concat(DB::INPUT_TABLE_NAME, |
---|
242 | DB::TECHNICAL_SPECIFICATION, '_')); |
---|
243 | inputInfo.tariffCode = |
---|
244 | query.getValue(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE, '_')); |
---|
245 | inputInfo.trademark = |
---|
246 | query.getValue(concat(DB::INPUT_TABLE_NAME, DB::TRADEMARK, '_')); |
---|
247 | inputInfo.measurementUnit = |
---|
248 | query.getValue(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME, '_')); |
---|
249 | |
---|
250 | strQuery.clear(); |
---|
251 | query.clear(); |
---|
252 | |
---|
253 | strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::RIF)); |
---|
254 | strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NAME)); |
---|
255 | strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NATIONALITY)); |
---|
256 | strQuery.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
257 | DB::BOUGHT_QUANTITY)); |
---|
258 | |
---|
259 | strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); |
---|
260 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
261 | |
---|
262 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), |
---|
263 | year)); |
---|
264 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
265 | idToStr(inputId))); |
---|
266 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::SUPPLIER_ID), |
---|
267 | concat(DB::SUPPLIER_TABLE_NAME, DB::ID))); |
---|
268 | |
---|
269 | strQuery.addOrderBy(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF)); |
---|
270 | |
---|
271 | |
---|
272 | if (not query.exec(strQuery)) { |
---|
273 | |
---|
274 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
275 | |
---|
276 | throw std::logic_error(msg); |
---|
277 | } |
---|
278 | |
---|
279 | std::string lastRif = ""; |
---|
280 | |
---|
281 | inputInfo.quantity = 0.0; |
---|
282 | |
---|
283 | while (query.next()) { |
---|
284 | |
---|
285 | std::string rif = |
---|
286 | query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF, '_')); |
---|
287 | |
---|
288 | std::string nationality = query.getValue(concat(DB::SUPPLIER_TABLE_NAME, |
---|
289 | DB::NATIONALITY, '_')); |
---|
290 | |
---|
291 | inputInfo.quantity += std::atof( |
---|
292 | query.getValue(concat(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
293 | DB::BOUGHT_QUANTITY, '_'))); |
---|
294 | |
---|
295 | if (nationality != "E" and rif == lastRif) { |
---|
296 | continue; |
---|
297 | } |
---|
298 | |
---|
299 | lastRif = rif; |
---|
300 | |
---|
301 | inputInfo.companyRif = rif; |
---|
302 | |
---|
303 | inputInfo.companyName = |
---|
304 | query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::NAME, '_')); |
---|
305 | |
---|
306 | inputInfo.nationality = nationality[0]; |
---|
307 | |
---|
308 | long supplierId = std::atol(query.getValue(0)); |
---|
309 | |
---|
310 | inputInfo.origCountry = getOrigCountry(conn, supplierId); |
---|
311 | inputInfo.procCountry = getProcCountry(conn, supplierId); |
---|
312 | |
---|
313 | inputs.append(inputInfo); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void productInfoFromInput(AutoConnection & conn, const InputInfo & inputInfo, |
---|
318 | List <ProductInfo> & list) { |
---|
319 | |
---|
320 | DBQuery query(conn); |
---|
321 | |
---|
322 | StrQuery strQuery; |
---|
323 | |
---|
324 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
325 | |
---|
326 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
327 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
328 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
329 | |
---|
330 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID), |
---|
331 | idToStr(inputInfo.id))); |
---|
332 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE), |
---|
333 | concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE))); |
---|
334 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
335 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
336 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF), |
---|
337 | singleQuote(inputInfo.companyRif))); |
---|
338 | |
---|
339 | if (not query.exec(strQuery)) { |
---|
340 | |
---|
341 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
342 | |
---|
343 | throw std::logic_error(msg); |
---|
344 | } |
---|
345 | |
---|
346 | while (query.next()) |
---|
347 | list.append(loadInfoByProductId(conn, |
---|
348 | std::atol(query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::ID, |
---|
349 | '_'))))); |
---|
350 | } |
---|
351 | |
---|
352 | void listProductsId(AutoConnection & conn, Good * good, |
---|
353 | const std::string & year, |
---|
354 | List <Aleph::pair<long, long>> & list) { |
---|
355 | |
---|
356 | DBQuery query(conn); |
---|
357 | |
---|
358 | StrQuery strQuery; |
---|
359 | |
---|
360 | const std::string & rif = good->getType() == PRODUCT_GOOD ? |
---|
361 | (static_cast <Product *>(good))->getProducerPlant() |
---|
362 | ->getOwnerCompany()->getRif() : |
---|
363 | (static_cast <Input *>(good))->getProviderCompany()->getRif(); |
---|
364 | |
---|
365 | const std::string & tariffCode = good->getTariffCode(); |
---|
366 | |
---|
367 | strQuery.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID)); |
---|
368 | strQuery.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::INPUT_ID)); |
---|
369 | |
---|
370 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
371 | strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); |
---|
372 | strQuery.addFrom(DB::PRODUCT_INPUT_TABLE_NAME); |
---|
373 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
374 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
375 | |
---|
376 | strQuery.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF), |
---|
377 | singleQuote(rif))); |
---|
378 | strQuery.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::ID), |
---|
379 | concat(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
380 | DB::SUPPLIER_ID))); |
---|
381 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
382 | concat(DB::INPUT_TABLE_NAME, DB::ID))); |
---|
383 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE), |
---|
384 | singleQuote(tariffCode))); |
---|
385 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
386 | concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::INPUT_ID))); |
---|
387 | strQuery.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID), |
---|
388 | concat(DB::PRODUCT_TABLE_NAME, DB::ID))); |
---|
389 | strQuery.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::YEAR), year)); |
---|
390 | strQuery.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::QUANTITY), |
---|
391 | "0", "<>")); |
---|
392 | |
---|
393 | if (not query.exec(strQuery)) { |
---|
394 | |
---|
395 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
396 | |
---|
397 | throw std::logic_error(msg); |
---|
398 | } |
---|
399 | |
---|
400 | while (query.next()) |
---|
401 | list.append(Aleph::pair<long, long>(std::atol(query.getValue(0)), |
---|
402 | std::atol(query.getValue(1)))); |
---|
403 | } |
---|
404 | |
---|
405 | void listRequesters(AutoConnection & conn, Good * good, |
---|
406 | const std::string & year, |
---|
407 | List <Aleph::pair<ProductInfo, long>> & list) { |
---|
408 | |
---|
409 | List <Aleph::pair <long, long>> ids; |
---|
410 | |
---|
411 | listProductsId(conn, good, year, ids); |
---|
412 | |
---|
413 | for(List <Aleph::pair<long, long>>::Iterator it(ids); it.has_current(); |
---|
414 | it.next()) { |
---|
415 | |
---|
416 | Aleph::pair <long, long> & p = it.get_current(); |
---|
417 | |
---|
418 | ProductInfo info = loadInfoByProductId(conn, p.first); |
---|
419 | |
---|
420 | list.append(Aleph::pair<ProductInfo, long>(info, p.second)); |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | void getProducedQuantityAndPrice(AutoConnection & conn, const long & id, |
---|
425 | const std::string & year, |
---|
426 | real & producedQuantity, real & price) { |
---|
427 | |
---|
428 | DBQuery query(conn); |
---|
429 | |
---|
430 | StrQuery strQuery; |
---|
431 | |
---|
432 | strQuery.addSelect(DB::PRODUCED_QUANTITY); |
---|
433 | strQuery.addSelect(DB::PRICE); |
---|
434 | |
---|
435 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
436 | |
---|
437 | strQuery.addWhere(op(DB::PRODUCT_ID, idToStr(id))); |
---|
438 | strQuery.addWhere(inYear(DB::YEAR, year)); |
---|
439 | |
---|
440 | if (not query.exec(strQuery)) { |
---|
441 | |
---|
442 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
443 | |
---|
444 | throw std::logic_error(msg); |
---|
445 | } |
---|
446 | |
---|
447 | if (not query.next()) |
---|
448 | return; |
---|
449 | |
---|
450 | producedQuantity = std::atof(query.getValue(0)); |
---|
451 | price = std::atof(query.getValue(1)); |
---|
452 | } |
---|
453 | |
---|
454 | void getProducedQuantityAndPrice(AutoConnection & conn, Good * good, |
---|
455 | const std::string & year) { |
---|
456 | |
---|
457 | real producedQuantity = 0.0; |
---|
458 | |
---|
459 | real price = 0.0; |
---|
460 | |
---|
461 | getProducedQuantityAndPrice(conn, good->getId(), year, producedQuantity, |
---|
462 | price); |
---|
463 | |
---|
464 | good->setTotalQuantity(producedQuantity); |
---|
465 | |
---|
466 | good->setUnitarianPrice(price); |
---|
467 | } |
---|
468 | |
---|
469 | void getProducedQuantityAndPrice(AutoConnection & conn, ProductInfo & info, |
---|
470 | const std::string & year) { |
---|
471 | |
---|
472 | getProducedQuantityAndPrice(conn, info.productId, year, info.totalQuantity, |
---|
473 | info.unitarianPrice); |
---|
474 | } |
---|
475 | |
---|
476 | real getUnitarianRequest(AutoConnection & conn, const size_t & inputId, |
---|
477 | const size_t & productId) { |
---|
478 | |
---|
479 | DBQuery query(conn); |
---|
480 | |
---|
481 | StrQuery strQuery; |
---|
482 | |
---|
483 | strQuery.addSelect(DB::QUANTITY); |
---|
484 | |
---|
485 | strQuery.addFrom(DB::PRODUCT_INPUT_TABLE_NAME); |
---|
486 | |
---|
487 | strQuery.addWhere(op(DB::PRODUCT_ID, idToStr(productId))); |
---|
488 | strQuery.addWhere(op(DB::INPUT_ID, idToStr(inputId))); |
---|
489 | |
---|
490 | if (not query.exec(strQuery)) { |
---|
491 | |
---|
492 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
493 | |
---|
494 | throw std::logic_error(msg); |
---|
495 | } |
---|
496 | |
---|
497 | if (not query.next()) |
---|
498 | return 0.0; |
---|
499 | |
---|
500 | return std::atof(query.getValue(0)); |
---|
501 | } |
---|
502 | |
---|
503 | real computeInputProportionFromSupplier(AutoConnection & conn, |
---|
504 | const std::string & supplierRif, |
---|
505 | const std::string & supplierName, |
---|
506 | const std::string & nationality, |
---|
507 | const size_t & inputId, |
---|
508 | const std::string & year) { |
---|
509 | |
---|
510 | DBQuery query(conn); |
---|
511 | |
---|
512 | StrQuery strQuery; |
---|
513 | |
---|
514 | const std::string & to_compare = nationality == "E" ? |
---|
515 | supplierName : supplierRif; |
---|
516 | |
---|
517 | if (nationality == "E") { |
---|
518 | strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NAME)); |
---|
519 | } else { |
---|
520 | strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::RIF)); |
---|
521 | } |
---|
522 | strQuery.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
523 | DB::BOUGHT_QUANTITY)); |
---|
524 | |
---|
525 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
526 | strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); |
---|
527 | |
---|
528 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
529 | idToStr(inputId))); |
---|
530 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::SUPPLIER_ID), |
---|
531 | concat(DB::SUPPLIER_TABLE_NAME, DB::ID))); |
---|
532 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), |
---|
533 | year)); |
---|
534 | |
---|
535 | if (not query.exec(strQuery)) { |
---|
536 | |
---|
537 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
538 | |
---|
539 | throw std::logic_error(msg); |
---|
540 | } |
---|
541 | |
---|
542 | real sum = 0; |
---|
543 | |
---|
544 | real suppliedQuantity = 0; |
---|
545 | |
---|
546 | while (query.next()) { |
---|
547 | |
---|
548 | if (to_compare == query.getValue(0)) { |
---|
549 | suppliedQuantity = std::atof(query.getValue(1)); |
---|
550 | } |
---|
551 | |
---|
552 | sum += std::atof(query.getValue(1)); |
---|
553 | } |
---|
554 | |
---|
555 | return suppliedQuantity / sum; |
---|
556 | } |
---|
557 | |
---|
558 | real computeProportionByTariffCode(AutoConnection & conn, |
---|
559 | const std::string & tariffCode, |
---|
560 | const std::string & rif, |
---|
561 | const size_t & productId, |
---|
562 | const std::string & year) { |
---|
563 | |
---|
564 | DBQuery query(conn); |
---|
565 | |
---|
566 | StrQuery strQuery; |
---|
567 | |
---|
568 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
569 | strQuery.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
570 | DB::PRODUCED_QUANTITY)); |
---|
571 | |
---|
572 | strQuery.addFrom(DB::COMPANY_TABLE_NAME); |
---|
573 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
574 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
575 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
576 | |
---|
577 | strQuery.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF), |
---|
578 | singleQuote(rif))); |
---|
579 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE), |
---|
580 | singleQuote(tariffCode))); |
---|
581 | strQuery.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF), |
---|
582 | concat(DB::PLANT_TABLE_NAME, DB::UE_RIF))); |
---|
583 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
584 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
585 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID), |
---|
586 | concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
587 | DB::PRODUCT_ID))); |
---|
588 | strQuery.addWhere(inYear(concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
589 | DB::YEAR), year)); |
---|
590 | |
---|
591 | if (not query.exec(strQuery)) { |
---|
592 | |
---|
593 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
594 | |
---|
595 | throw std::logic_error(msg); |
---|
596 | } |
---|
597 | |
---|
598 | real producedQuantity = 0; |
---|
599 | |
---|
600 | real sum = 0; |
---|
601 | |
---|
602 | std::string strProductId = idToStr(productId); |
---|
603 | |
---|
604 | if (not query.hasResult()) |
---|
605 | return 1; |
---|
606 | |
---|
607 | while (query.next()) { |
---|
608 | |
---|
609 | if (strProductId == query.getValue(0)) |
---|
610 | producedQuantity = std::atof(query.getValue(1)); |
---|
611 | |
---|
612 | sum += std::atof(query.getValue(1)); |
---|
613 | } |
---|
614 | |
---|
615 | return producedQuantity / sum; |
---|
616 | } |
---|
617 | |
---|
618 | void computeArcQuantityUpstream(AutoConnection & conn, Good * src, Good * tgt, |
---|
619 | const std::string & year, ArcInfo & info) { |
---|
620 | |
---|
621 | std::string rif; |
---|
622 | std::string companyName; |
---|
623 | std::string nationality; |
---|
624 | |
---|
625 | if (src->getType() == PRODUCT_GOOD) { |
---|
626 | |
---|
627 | Product * product = static_cast<Product *>(src); |
---|
628 | |
---|
629 | rif = product->getProducerPlant()->getOwnerCompany()->getRif(); |
---|
630 | companyName = product->getProducerPlant()->getOwnerCompany()->getName(); |
---|
631 | nationality = |
---|
632 | product->getProducerPlant()->getOwnerCompany()->getNationality(); |
---|
633 | |
---|
634 | } else { |
---|
635 | |
---|
636 | Input * input = static_cast<Input *>(src); |
---|
637 | |
---|
638 | rif = input->getProviderCompany()->getRif(); |
---|
639 | companyName = input->getProviderCompany()->getName(); |
---|
640 | nationality = input->getProviderCompany()->getNationality(); |
---|
641 | } |
---|
642 | |
---|
643 | real proportionFromSupplier = |
---|
644 | computeInputProportionFromSupplier(conn, rif, companyName, nationality, |
---|
645 | info.inputId, year); |
---|
646 | |
---|
647 | real proportionByTariffCode = |
---|
648 | computeProportionByTariffCode(conn, src->getTariffCode(), rif, |
---|
649 | src->getId(), year); |
---|
650 | |
---|
651 | info.quantity = tgt->getQuantity() * info.reqQuantity * |
---|
652 | proportionFromSupplier * proportionByTariffCode; |
---|
653 | info.totalQuantity = info.quantity; |
---|
654 | info.usedQuantity = info.quantity; |
---|
655 | } |
---|
656 | |
---|
657 | real getDeclaredBoughtQuantity(AutoConnection & conn, const std::string & rif, |
---|
658 | const long & inputId, const std::string & year) { |
---|
659 | |
---|
660 | DBQuery query(conn); |
---|
661 | |
---|
662 | StrQuery strQuery; |
---|
663 | |
---|
664 | strQuery.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
665 | DB::BOUGHT_QUANTITY)); |
---|
666 | |
---|
667 | strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); |
---|
668 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
669 | |
---|
670 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), |
---|
671 | year)); |
---|
672 | strQuery.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF), |
---|
673 | singleQuote(rif))); |
---|
674 | strQuery.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::ID), |
---|
675 | concat(DB::INPUT_SUPPLIER_TABLE_NAME, |
---|
676 | DB::SUPPLIER_ID))); |
---|
677 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
678 | idToStr(inputId))); |
---|
679 | |
---|
680 | if (not query.exec(strQuery)) { |
---|
681 | |
---|
682 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
683 | |
---|
684 | throw std::logic_error(msg); |
---|
685 | } |
---|
686 | |
---|
687 | if (not query.next()) |
---|
688 | return 0.0; |
---|
689 | |
---|
690 | return std::atof(query.getValue(0)); |
---|
691 | } |
---|
692 | |
---|
693 | real computePorportionByProductInput(AutoConnection & conn, |
---|
694 | const long & inputId, |
---|
695 | const size_t & productId, |
---|
696 | const std::string & year) { |
---|
697 | |
---|
698 | DBQuery query(conn); |
---|
699 | |
---|
700 | StrQuery strQuery; |
---|
701 | |
---|
702 | strQuery.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID)); |
---|
703 | strQuery.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::QUANTITY)); |
---|
704 | strQuery.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
705 | DB::PRODUCED_QUANTITY)); |
---|
706 | |
---|
707 | strQuery.addFrom(DB::PRODUCT_INPUT_TABLE_NAME); |
---|
708 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
709 | |
---|
710 | strQuery.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::INPUT_ID), |
---|
711 | idToStr(inputId))); |
---|
712 | strQuery.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID), |
---|
713 | concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
714 | DB::PRODUCT_ID))); |
---|
715 | strQuery.addWhere(inYear(concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
716 | DB::YEAR), year)); |
---|
717 | |
---|
718 | if (not query.exec(strQuery)) { |
---|
719 | |
---|
720 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
721 | |
---|
722 | throw std::logic_error(msg); |
---|
723 | } |
---|
724 | |
---|
725 | real sum = 0; |
---|
726 | |
---|
727 | real neededQuantity = 0; |
---|
728 | |
---|
729 | while (query.next()) { |
---|
730 | |
---|
731 | real q = std::atof(query.getValue(1)) * std::atof(query.getValue(2)); |
---|
732 | |
---|
733 | if (std::atol(query.getValue(0)) == productId) |
---|
734 | neededQuantity = q; |
---|
735 | |
---|
736 | sum += q; |
---|
737 | } |
---|
738 | |
---|
739 | return neededQuantity / sum; |
---|
740 | } |
---|
741 | |
---|
742 | real computeProportionByTartiffCode(AutoConnection & conn, |
---|
743 | const std::string & rif, |
---|
744 | const std::string & tariffCode, |
---|
745 | const long & productId, |
---|
746 | const std::string & year) { |
---|
747 | |
---|
748 | DBQuery query(conn); |
---|
749 | |
---|
750 | StrQuery strQuery; |
---|
751 | |
---|
752 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
753 | strQuery.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
754 | DB::PRODUCED_QUANTITY)); |
---|
755 | |
---|
756 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
757 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
758 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
759 | strQuery.addFrom(DB::COMPANY_TABLE_NAME); |
---|
760 | |
---|
761 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE), |
---|
762 | singleQuote(tariffCode))); |
---|
763 | strQuery.addWhere(op(concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
764 | DB::PRODUCT_ID), |
---|
765 | concat(DB::PRODUCT_TABLE_NAME, DB::ID))); |
---|
766 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
767 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
768 | strQuery.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF), |
---|
769 | concat(DB::PLANT_TABLE_NAME, DB::UE_RIF))); |
---|
770 | strQuery.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF), |
---|
771 | singleQuote(rif))); |
---|
772 | strQuery.addWhere(inYear(concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
773 | DB::YEAR), year)); |
---|
774 | |
---|
775 | if (not query.exec(strQuery)) { |
---|
776 | |
---|
777 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
778 | |
---|
779 | throw std::logic_error(msg); |
---|
780 | } |
---|
781 | |
---|
782 | real sum = 0; |
---|
783 | |
---|
784 | real quantity = 0; |
---|
785 | |
---|
786 | while (query.next()) { |
---|
787 | |
---|
788 | real q = std::atof(query.getValue(1)); |
---|
789 | |
---|
790 | if (std::atol(query.getValue(0)) == productId) |
---|
791 | quantity = q; |
---|
792 | |
---|
793 | sum += q; |
---|
794 | } |
---|
795 | |
---|
796 | return quantity / sum; |
---|
797 | } |
---|
798 | |
---|
799 | void computeArcQuantityDownstream(AutoConnection & conn, Good * src, Good * tgt, |
---|
800 | const std::string & year, ArcInfo & arcInfo) { |
---|
801 | |
---|
802 | real proportionByProductInput = |
---|
803 | computePorportionByProductInput(conn, arcInfo.inputId, tgt->getId(), |
---|
804 | year); |
---|
805 | |
---|
806 | |
---|
807 | const std::string & rif = src->getType() == PRODUCT_GOOD ? |
---|
808 | (static_cast <Product *>(src))->getProducerPlant() |
---|
809 | ->getOwnerCompany()->getRif() : |
---|
810 | (static_cast <Input *>(src))->getProviderCompany()->getRif(); |
---|
811 | |
---|
812 | real declaredQuantity = |
---|
813 | getDeclaredBoughtQuantity(conn, rif, arcInfo.inputId, year); |
---|
814 | |
---|
815 | real proportion = src->getQuantity() / src->getTotalQuantity(); |
---|
816 | |
---|
817 | real proportionByTartiffCode = |
---|
818 | computeProportionByTartiffCode(conn, rif, src->getTariffCode(), |
---|
819 | src->getId(), year); |
---|
820 | |
---|
821 | arcInfo.quantity = declaredQuantity * proportion * |
---|
822 | proportionByTartiffCode * proportionByProductInput; |
---|
823 | |
---|
824 | arcInfo.totalQuantity = declaredQuantity * proportionByTartiffCode * |
---|
825 | proportionByProductInput; |
---|
826 | |
---|
827 | arcInfo.reqQuantity = |
---|
828 | getUnitarianRequest(conn, arcInfo.inputId, tgt->getId()); |
---|
829 | } |
---|
830 | |
---|
831 | real getAcquisitionPrice(AutoConnection & conn, Good * good, const long & id, |
---|
832 | const std::string & year) { |
---|
833 | |
---|
834 | DBQuery query(conn); |
---|
835 | |
---|
836 | StrQuery strQuery; |
---|
837 | |
---|
838 | std::string priceField = DB::NAC_ACQ_PRICE; |
---|
839 | |
---|
840 | if (good->getType() == INPUT_GOOD and |
---|
841 | COMPANY(good)->getNationality() == "E") |
---|
842 | priceField = DB::ACQ_PRICE; |
---|
843 | |
---|
844 | strQuery.addSelect(priceField); |
---|
845 | |
---|
846 | strQuery.addFrom(DB::ACQ_PRICE_TABLE_NAME); |
---|
847 | |
---|
848 | strQuery.addWhere(op(DB::INPUT_ID, idToStr(id))); |
---|
849 | strQuery.addWhere(inYear(DB::YEAR, year)); |
---|
850 | |
---|
851 | if (not query.exec(strQuery)) { |
---|
852 | |
---|
853 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
854 | |
---|
855 | throw std::logic_error(msg); |
---|
856 | } |
---|
857 | |
---|
858 | |
---|
859 | if (not query.next()) |
---|
860 | return 0.0; |
---|
861 | |
---|
862 | return std::atof(query.getValue(0)); |
---|
863 | } |
---|
864 | |
---|
865 | std::string getCountry(AutoConnection & conn, const long & supplierId, |
---|
866 | const std::string & field) { |
---|
867 | |
---|
868 | DBQuery query(conn); |
---|
869 | |
---|
870 | StrQuery strQuery; |
---|
871 | |
---|
872 | strQuery.addSelect(concatas(DB::COUNTRY_TABLE_NAME, DB::NAME)); |
---|
873 | |
---|
874 | strQuery.addFrom(DB::COUNTRY_TABLE_NAME); |
---|
875 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
876 | |
---|
877 | strQuery.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::ID), |
---|
878 | idToStr(supplierId))); |
---|
879 | strQuery.addWhere(op(field, concat(DB::COUNTRY_TABLE_NAME, DB::ID))); |
---|
880 | |
---|
881 | if (not query.exec(strQuery)) { |
---|
882 | |
---|
883 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
884 | |
---|
885 | throw std::logic_error(msg); |
---|
886 | } |
---|
887 | |
---|
888 | if (not query.next()) |
---|
889 | return ""; |
---|
890 | |
---|
891 | return std::string(query.getValue(0)); |
---|
892 | } |
---|
893 | |
---|
894 | std::string getOrigCountry(AutoConnection & conn, const long & supplierId) { |
---|
895 | return getCountry(conn, supplierId, DB::ORIG_COUNTRY_ID); |
---|
896 | } |
---|
897 | |
---|
898 | std::string getProcCountry(AutoConnection & conn, const long & supplierId) { |
---|
899 | return getCountry(conn, supplierId, DB::PROC_COUNTRY_ID); |
---|
900 | } |
---|
901 | |
---|
902 | void listSuggestedSuppliers(AutoConnection & conn, |
---|
903 | const std::string & tariffCode, |
---|
904 | const std::string & year, |
---|
905 | List<ProductInfo> & list) { |
---|
906 | |
---|
907 | DBQuery query(conn); |
---|
908 | |
---|
909 | StrQuery strQuery; |
---|
910 | |
---|
911 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
912 | |
---|
913 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
914 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
915 | |
---|
916 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE), |
---|
917 | singleQuote(tariffCode))); |
---|
918 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID), |
---|
919 | concat(DB::PLANT_TABLE_NAME, DB::ID))); |
---|
920 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::USED_CAPACITY), |
---|
921 | "100", "<")); |
---|
922 | |
---|
923 | if (not query.exec(strQuery)) { |
---|
924 | |
---|
925 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
926 | |
---|
927 | throw std::logic_error(msg); |
---|
928 | } |
---|
929 | |
---|
930 | while (query.next()) { |
---|
931 | |
---|
932 | long productId = std::atol(query.getValue(0)); |
---|
933 | |
---|
934 | ProductInfo info = loadInfoByProductId(conn, productId); |
---|
935 | |
---|
936 | getProducedQuantityAndPrice(conn, info, year); |
---|
937 | |
---|
938 | list.append(info); |
---|
939 | } |
---|
940 | } |
---|
941 | |
---|
942 | void listRealProductId(AutoConnection & conn, const long & id, |
---|
943 | const std::string & year, List<long> & ids) { |
---|
944 | |
---|
945 | DBQuery query(conn); |
---|
946 | |
---|
947 | StrQuery strQuery; |
---|
948 | |
---|
949 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
950 | |
---|
951 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
952 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
953 | strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); |
---|
954 | strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); |
---|
955 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
956 | |
---|
957 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), |
---|
958 | idToStr(id))); |
---|
959 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::SUPPLIER_ID), |
---|
960 | concat(DB::SUPPLIER_TABLE_NAME, DB::ID))); |
---|
961 | strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), |
---|
962 | year)); |
---|
963 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID), |
---|
964 | concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID))); |
---|
965 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE), |
---|
966 | concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE))); |
---|
967 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID), |
---|
968 | concat(DB::PLANT_TABLE_NAME, DB::ID))); |
---|
969 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF), |
---|
970 | concat(DB::SUPPLIER_TABLE_NAME, DB::RIF))); |
---|
971 | |
---|
972 | if (not query.exec(strQuery)) { |
---|
973 | |
---|
974 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
975 | |
---|
976 | throw std::logic_error(msg); |
---|
977 | } |
---|
978 | |
---|
979 | while (query.next()) |
---|
980 | ids.append(std::atol(query.getValue(0))); |
---|
981 | } |
---|
982 | |
---|
983 | std::string getInputName(AutoConnection & conn, const long & id) { |
---|
984 | |
---|
985 | DBQuery query(conn); |
---|
986 | |
---|
987 | StrQuery strQuery; |
---|
988 | |
---|
989 | strQuery.addSelect(DB::NAME); |
---|
990 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
991 | strQuery.addWhere(op(DB::ID, idToStr(id))); |
---|
992 | |
---|
993 | if (not query.exec(strQuery)) { |
---|
994 | |
---|
995 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
996 | |
---|
997 | throw std::logic_error(msg); |
---|
998 | } |
---|
999 | |
---|
1000 | query.next(); |
---|
1001 | |
---|
1002 | return query.getValue(0); |
---|
1003 | } |
---|
1004 | |
---|
1005 | PlantLocation getPlantLocation(AutoConnection & conn, const long & id) { |
---|
1006 | |
---|
1007 | DBQuery query(conn); |
---|
1008 | |
---|
1009 | StrQuery strQuery; |
---|
1010 | |
---|
1011 | strQuery.addSelect(concatas(DB::PARISH_TABLE_NAME, DB::NAME)); |
---|
1012 | strQuery.addSelect(concatas(DB::TOWN_TABLE_NAME, DB::NAME)); |
---|
1013 | strQuery.addSelect(concatas(DB::STATE_TABLE_NAME, DB::NAME)); |
---|
1014 | |
---|
1015 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
1016 | strQuery.addFrom(DB::PARISH_TABLE_NAME); |
---|
1017 | strQuery.addFrom(DB::TOWN_TABLE_NAME); |
---|
1018 | strQuery.addFrom(DB::STATE_TABLE_NAME); |
---|
1019 | |
---|
1020 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), idToStr(id))); |
---|
1021 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::PARISH_CODE), |
---|
1022 | concat(DB::PARISH_TABLE_NAME, DB::CODE))); |
---|
1023 | strQuery.addWhere(op(concat(DB::PARISH_TABLE_NAME, DB::TOWN_CODE), |
---|
1024 | concat(DB::TOWN_TABLE_NAME, DB::CODE))); |
---|
1025 | strQuery.addWhere(op(concat(DB::TOWN_TABLE_NAME, DB::STATE_CODE), |
---|
1026 | concat(DB::STATE_TABLE_NAME, DB::CODE))); |
---|
1027 | |
---|
1028 | if (not query.exec(strQuery)) { |
---|
1029 | |
---|
1030 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1031 | |
---|
1032 | throw std::logic_error(msg); |
---|
1033 | } |
---|
1034 | |
---|
1035 | if (not query.next()) |
---|
1036 | throw std::domain_error("There's not location info for plant"); |
---|
1037 | |
---|
1038 | PlantLocation plantLocation; |
---|
1039 | |
---|
1040 | plantLocation.parish = |
---|
1041 | query.getValue(concat(DB::PARISH_TABLE_NAME, DB::NAME, '_')); |
---|
1042 | plantLocation.town = |
---|
1043 | query.getValue(concat(DB::TOWN_TABLE_NAME, DB::NAME, '_')); |
---|
1044 | plantLocation.state = |
---|
1045 | query.getValue(concat(DB::STATE_TABLE_NAME, DB::NAME, '_')); |
---|
1046 | |
---|
1047 | return plantLocation; |
---|
1048 | } |
---|
1049 | |
---|
1050 | std::string getCiiu(AutoConnection & conn, const std::string & rif) { |
---|
1051 | |
---|
1052 | DBQuery query(conn); |
---|
1053 | |
---|
1054 | StrQuery strQuery; |
---|
1055 | |
---|
1056 | strQuery.addSelect(DB::CIIU_ID); |
---|
1057 | |
---|
1058 | strQuery.addFrom(DB::COMPANY_ACTIVITY_CIIU_TABLE_NAME); |
---|
1059 | |
---|
1060 | strQuery.addWhere(op(DB::COMPANY_RIF, singleQuote(rif))); |
---|
1061 | strQuery.addWhere(op(DB::ACTIVE, "true")); |
---|
1062 | strQuery.addWhere(op(DB::MAIN, "true")); |
---|
1063 | |
---|
1064 | if (not query.exec(strQuery)) { |
---|
1065 | |
---|
1066 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1067 | |
---|
1068 | throw std::logic_error(msg); |
---|
1069 | } |
---|
1070 | |
---|
1071 | if (not query.next()) |
---|
1072 | return ""; |
---|
1073 | |
---|
1074 | std::string ciiu = query.getValue(DB::CIIU_ID); |
---|
1075 | |
---|
1076 | return ciiu; |
---|
1077 | } |
---|
1078 | |
---|
1079 | real getOperatingStaffNumber(AutoConnection & conn, const std::string & rif, |
---|
1080 | const std::string & year) { |
---|
1081 | |
---|
1082 | DBQuery query(conn); |
---|
1083 | |
---|
1084 | StrQuery strQuery; |
---|
1085 | |
---|
1086 | /* Se le suma una unidad al año porque el registro de empleos se hace al |
---|
1087 | año siguiente. |
---|
1088 | */ |
---|
1089 | int y = std::atoi(year.c_str()) + 1; |
---|
1090 | |
---|
1091 | std::stringstream yearStream; |
---|
1092 | |
---|
1093 | yearStream << y; |
---|
1094 | |
---|
1095 | std::string maxDate = yearStream.str() + "-12-31"; |
---|
1096 | |
---|
1097 | strQuery.addSelect(DB::OPERATING); |
---|
1098 | |
---|
1099 | strQuery.addFrom(DB::COMPANY_OCCUPANCY_TABLE_NAME); |
---|
1100 | |
---|
1101 | strQuery.addWhere(op(DB::COMPANY_RIF, singleQuote(rif))); |
---|
1102 | strQuery.addWhere(op(DB::ACTIVE, "TRUE")); |
---|
1103 | strQuery.addWhere(op(DB::INSERTION_DATE, singleQuote(maxDate), "<=")); |
---|
1104 | |
---|
1105 | strQuery.addOrderBy(DB::INSERTION_DATE); |
---|
1106 | |
---|
1107 | strQuery.setOrderByOption("DESC"); |
---|
1108 | |
---|
1109 | if (not query.exec(strQuery)) { |
---|
1110 | |
---|
1111 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1112 | |
---|
1113 | throw std::logic_error(msg); |
---|
1114 | } |
---|
1115 | |
---|
1116 | if (not query.next()) |
---|
1117 | return 0.0; |
---|
1118 | |
---|
1119 | return std::atof(query.getValue(0)); |
---|
1120 | } |
---|
1121 | |
---|
1122 | real getTotalAmount(AutoConnection & conn, const std::string & rif, |
---|
1123 | const std::string & year) { |
---|
1124 | |
---|
1125 | DBQuery query(conn); |
---|
1126 | |
---|
1127 | StrQuery strQuery; |
---|
1128 | |
---|
1129 | std::string select; |
---|
1130 | |
---|
1131 | strQuery.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
1132 | DB::PRICE)); |
---|
1133 | strQuery.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
1134 | DB::PRODUCED_QUANTITY)); |
---|
1135 | |
---|
1136 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
1137 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
1138 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
1139 | |
---|
1140 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF), |
---|
1141 | singleQuote(rif))); |
---|
1142 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID), |
---|
1143 | concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID))); |
---|
1144 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID), |
---|
1145 | concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
1146 | DB::PRODUCT_ID))); |
---|
1147 | strQuery.addWhere(inYear(concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME, |
---|
1148 | DB::YEAR), year)); |
---|
1149 | |
---|
1150 | if (not query.exec(strQuery)) { |
---|
1151 | |
---|
1152 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1153 | |
---|
1154 | throw std::logic_error(msg); |
---|
1155 | } |
---|
1156 | |
---|
1157 | real total = 0.0; |
---|
1158 | |
---|
1159 | while (query.next()) |
---|
1160 | total += (std::atof(query.getValue(0)) * std::atof(query.getValue(1))); |
---|
1161 | |
---|
1162 | |
---|
1163 | return total; |
---|
1164 | } |
---|
1165 | |
---|
1166 | void listCompaniesLike(AutoConnection & conn, const std::string & company, |
---|
1167 | List <CompanyRifName> & list) { |
---|
1168 | |
---|
1169 | DBQuery query(conn); |
---|
1170 | |
---|
1171 | StrQuery strQuery; |
---|
1172 | |
---|
1173 | strQuery.addSelect(DB::RIF); |
---|
1174 | strQuery.addSelect(DB::COMPANY_NAME); |
---|
1175 | |
---|
1176 | strQuery.addFrom(DB::COMPANY_TABLE_NAME); |
---|
1177 | |
---|
1178 | std::string like; |
---|
1179 | |
---|
1180 | like.append("%"); |
---|
1181 | like.append(company); |
---|
1182 | like.append("%"); |
---|
1183 | |
---|
1184 | std::string where; |
---|
1185 | |
---|
1186 | where.append("WHERE "); |
---|
1187 | where.append(DB::COMPANY_NAME); |
---|
1188 | where.append(" LIKE "); |
---|
1189 | where.append(singleQuote(like)); |
---|
1190 | |
---|
1191 | where.append(" OR "); |
---|
1192 | where.append(DB::RIF); |
---|
1193 | where.append(" LIKE "); |
---|
1194 | where.append(singleQuote(like)); |
---|
1195 | |
---|
1196 | strQuery.setWhere(where); |
---|
1197 | |
---|
1198 | if (not query.exec(strQuery)) { |
---|
1199 | |
---|
1200 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1201 | |
---|
1202 | throw std::logic_error(msg); |
---|
1203 | } |
---|
1204 | |
---|
1205 | while (query.next()) { |
---|
1206 | |
---|
1207 | CompanyRifName company; |
---|
1208 | |
---|
1209 | company.rif = query.getValue(0); |
---|
1210 | company.name = query.getValue(1); |
---|
1211 | |
---|
1212 | list.append(company); |
---|
1213 | } |
---|
1214 | } |
---|
1215 | |
---|
1216 | void listProductsByCompany(AutoConnection & conn, const std::string & rif, |
---|
1217 | List <ProductIdName> & list) { |
---|
1218 | |
---|
1219 | DBQuery query(conn); |
---|
1220 | |
---|
1221 | StrQuery strQuery; |
---|
1222 | |
---|
1223 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID)); |
---|
1224 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::NAME)); |
---|
1225 | strQuery.addSelect(concatas(DB::PRODUCT_TABLE_NAME, |
---|
1226 | DB::TECHNICAL_SPECIFICATION)); |
---|
1227 | |
---|
1228 | strQuery.addFrom(DB::PRODUCT_TABLE_NAME); |
---|
1229 | strQuery.addFrom(DB::PLANT_TABLE_NAME); |
---|
1230 | |
---|
1231 | strQuery.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF), |
---|
1232 | singleQuote(rif))); |
---|
1233 | strQuery.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID), |
---|
1234 | concat(DB::PLANT_TABLE_NAME, DB::ID))); |
---|
1235 | |
---|
1236 | strQuery.addOrderBy(concat(DB::PRODUCT_TABLE_NAME, DB::NAME)); |
---|
1237 | |
---|
1238 | if (not query.exec(strQuery)) { |
---|
1239 | |
---|
1240 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1241 | |
---|
1242 | throw std::logic_error(msg); |
---|
1243 | } |
---|
1244 | |
---|
1245 | while (query.next()) { |
---|
1246 | |
---|
1247 | ProductIdName product; |
---|
1248 | |
---|
1249 | product.id = std::atol(query.getValue(0)); |
---|
1250 | product.name = query.getValue(1); |
---|
1251 | product.technicalSpecifications = query.getValue(2); |
---|
1252 | |
---|
1253 | list.append(product); |
---|
1254 | } |
---|
1255 | } |
---|
1256 | |
---|
1257 | void setArcMeasurementUnit(AutoConnection & conn, ArcInfo & info) { |
---|
1258 | |
---|
1259 | DBQuery query(conn); |
---|
1260 | |
---|
1261 | StrQuery strQuery; |
---|
1262 | |
---|
1263 | strQuery.addSelect(concatas(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME)); |
---|
1264 | |
---|
1265 | strQuery.addFrom(DB::INPUT_TABLE_NAME); |
---|
1266 | strQuery.addFrom(DB::MEASUREMENT_UNIT_TABLE_NAME); |
---|
1267 | |
---|
1268 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID), |
---|
1269 | idToStr(info.inputId))); |
---|
1270 | strQuery.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::MEASUREMENT_UNIT_ID), |
---|
1271 | concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::ID))); |
---|
1272 | |
---|
1273 | if (not query.exec(strQuery)) { |
---|
1274 | |
---|
1275 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1276 | |
---|
1277 | throw std::logic_error(msg); |
---|
1278 | } |
---|
1279 | |
---|
1280 | query.next(); |
---|
1281 | |
---|
1282 | info.measurementUnit = query.getValue(0); |
---|
1283 | } |
---|
1284 | |
---|
1285 | void getProductionYears(AutoConnection & conn, const long productId, |
---|
1286 | List<std::string> & years) { |
---|
1287 | |
---|
1288 | DBQuery query(conn); |
---|
1289 | |
---|
1290 | StrQuery strQuery; |
---|
1291 | |
---|
1292 | strQuery.addSelect(DB::YEAR); |
---|
1293 | |
---|
1294 | strQuery.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME); |
---|
1295 | |
---|
1296 | strQuery.addWhere(op(DB::PRODUCT_ID, idToStr(productId))); |
---|
1297 | |
---|
1298 | if (not query.exec(strQuery)) { |
---|
1299 | |
---|
1300 | std::string msg = "Cannot execute query: " + std::string(strQuery); |
---|
1301 | |
---|
1302 | throw std::logic_error(msg); |
---|
1303 | } |
---|
1304 | |
---|
1305 | while (query.next()) { |
---|
1306 | std::string date = query.getValue(0); |
---|
1307 | years.append(date.substr(0, 4)); |
---|
1308 | } |
---|
1309 | } |
---|
1310 | |
---|