source: observatorio/simulacion/ModuloDinamico/queries.C @ 666042a

simulacion
Last change on this file since 666042a was 666042a, checked in by Alejandro <amujica@…>, 9 years ago

Ajuste, cuando un productor no declara precio de venta del ano en estudio, se utiliza el precio de compra declarado por el comprador

  • Property mode set to 100644
File size: 25.9 KB
Line 
1/*
2  Copyright (C) 2014
3  Alejandro Mujica (amujica@cenditel.gob.ve)
4  José Ruiz (jruiz@cenditel.gob.ve)
5  Julie Vera (jvera@cenditel.gob.ve)
6 
7  CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en
8  Tecnologías Libres
9 
10  Este programa es software libre; Usted puede usarlo bajo los términos de la
11  licencia de software GPL versión 2.0 de la Free Software Foundation.
12 
13  Este programa se distribuye con la esperanza de que sea útil, pero SIN
14  NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o
15  ADECUACIÓN A UN PROPÓSITO PARTICULAR.
16  Consulte la licencia GPL para más detalles. Usted debe recibir una copia
17  de la GPL junto con este programa; si no, escriba a la Free Software
18  Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA.
19*/
20
21/*
22  Autor:             Alejandro J. Mujica
23  Fecha de creación: 12/06/2014
24  Este archivo contiene la implementación de las diversas consultas
25  a base de datos efectuadas.
26*/
27
28# include <queries.H>
29
30# include <sstream>
31# include <stdexcept>
32
33# include <dbQuery.H>
34# include <autoConnection.H>
35# include <strQuery.H>
36
37const std::string DB::PRODUCT_TABLE_NAME = "produccion_producto";
38const std::string DB::INPUT_TABLE_NAME = "produccion_insumo";
39const std::string DB::PLANT_TABLE_NAME = "unidadecon_subunidad_economica";
40const std::string DB::COMPANY_TABLE_NAME = "unidadecon_unidad_economica";
41const std::string DB::COMPANY_ACTIVITY_CIIU_TABLE_NAME =
42  "unidadecon_actividad_ciiu";
43const std::string DB::PRODUCT_INPUT_TABLE_NAME = "produccion_producto_t_insumo";
44const std::string DB::MEASUREMENT_UNIT_TABLE_NAME = "comun_unida_medida";
45const std::string DB::INPUT_SUPPLIER_TABLE_NAME =
46  "cmproveedores_proveedorinsumo";
47const std::string DB::SUPPLIER_TABLE_NAME = "cmproveedores_proveedor";
48const std::string DB::PRODUCTION_QUANTITIES_TABLE_NAME =
49    "produccion_produccion_precios_r";
50const std::string DB::ACQ_PRICE_TABLE_NAME = "produccion_consumo_precios_r";
51const std::string DB::COUNTRY_TABLE_NAME = "comun_pais";
52 const std::string DB::PARISH_TABLE_NAME = "comun_parroquia";
53const std::string DB::TOWN_TABLE_NAME = "comun_municipio";
54const std::string DB::STATE_TABLE_NAME = "comun_entidad";
55const std::string DB::COMPANY_OCCUPANCY_TABLE_NAME =
56    "unidadecon_personal_ocupado";
57
58const std::string DB::ID = "id";
59const std::string DB::NAME = "nombre";
60const std::string DB::COMPANY_NAME = "razon_social";
61const std::string DB::TECHNICAL_SPECIFICATION = "esp_tec";
62const std::string DB::TARIFF_CODE = "codigo_aran_id";
63const std::string DB::MEASUREMENT_UNIT_ID = "unidad_medida_id";
64const std::string DB::TRADEMARK = "marca";
65const std::string DB::PLANT_ID = "planta_id_id";
66const std::string DB::CODE = "codigo";
67const std::string DB::PARISH_CODE = "parroquia_codigo";
68const std::string DB::TOWN_CODE = "municipio_codigo";
69const std::string DB::STATE_CODE = "entidad_codigo";
70const std::string DB::COMPANY_RIF = "unidad_economica_rif";
71const std::string DB::UE_RIF = "ue_rif";
72const std::string DB::RIF = "rif";
73const std::string DB::NATIONALITY = "nacional";
74const std::string DB::WORKDAY = "horas_turnos";
75const std::string DB::ACQ_PRICE = "cost_adq";
76const std::string DB::NAC_ACQ_PRICE = "prec_adq_nac";
77const std::string DB::CIIU_ID = "ciiu_id";
78const std::string DB::ACTIVE = "activo";
79const std::string DB::MAIN = "principal";
80const std::string DB::INPUT_ID = "insumo_id_id";
81const std::string DB::PRODUCT_ID = "producto_id_id";
82const std::string DB::SUPPLIER_ID = "proveedor_id_id";
83const std::string DB::PRODUCED_QUANTITY = "cant_producido";
84const std::string DB::PRICE = "precio_venta";
85const std::string DB::BOUGHT_QUANTITY = "cantidad_comprada_r";
86const std::string DB::QUANTITY = "cantidad";
87const std::string DB::ORIG_COUNTRY_ID= "pais_origen_id";
88const std::string DB::PROC_COUNTRY_ID = "pais_procedencia_id";
89const std::string DB::YEAR_R = "anho_r";
90const std::string DB::YEAR = "anho";
91const std::string DB::USED_CAPACITY = "capacidad_utilizada";
92const std::string DB::INSERTION_DATE = "fecha_insercion";
93const std::string DB::OPERATING = "operativo";
94const std::string DB::ADMINISTRATIVE = "administrativo";
95
96std::string single_quote(const std::string & str)
97{
98  return "'" + str + "'";
99}
100
101std::string double_quote(const std::string & str)
102{
103  return "\"" + str + "\"";
104}
105
106std::string concat(const std::string & table, const std::string & field,
107                   const char & connector)
108{
109  return table + connector + field;
110}
111
112std::string as(const std::string & field, const std::string & alias)
113{
114  return field + " AS " + alias;
115}
116
117std::string concatas(const std::string & table, const std::string & field,
118                     const std::string & alias)
119{
120  return as(concat(table, field), alias);
121}
122
123std::string concatas(const std::string & table, const std::string & field)
124{
125  return concatas(table, field, concat(table, field, '_'));
126}
127
128std::string op(const std::string & attr, const std::string & value,
129               const std::string & oper)
130{
131  return attr + " " + oper + " " + value;
132}
133
134std::string id_to_str(const size_t & id)
135{
136  std::stringstream sstr;
137  sstr << id;
138  return sstr.str();
139}
140
141std::string between(const std::string & field, const std::string & beg,
142                    const std::string & end)
143{
144  return field + " BETWEEN " + single_quote(beg) + " AND " + single_quote(end);
145}
146
147std::string in_year(const std::string & field, const std::string & year)
148{
149  std::string beg = year + "-01-01";
150  std::string end = year + "-12-31";
151  return between(field, beg, end);
152}
153
154Product load_product_by_id(const db_id_t & id)
155{
156  AutoConnection conn;
157  DBQuery query(conn);
158  StrQuery str_query;
159
160  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID));
161  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::NAME));
162  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE));
163  str_query.addSelect(concatas(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME));
164  str_query.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::ID));
165  str_query.addSelect(concatas(DB::COMPANY_TABLE_NAME, DB::RIF));
166  str_query.addSelect(concatas(DB::COMPANY_TABLE_NAME, DB::COMPANY_NAME));
167
168  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
169  str_query.addFrom(DB::MEASUREMENT_UNIT_TABLE_NAME);
170  str_query.addFrom(DB::PLANT_TABLE_NAME);
171  str_query.addFrom(DB::COMPANY_TABLE_NAME);
172
173  str_query.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID),
174                        id_to_str(id)));
175  str_query.addWhere(op(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::ID),
176                        concat(DB::PRODUCT_TABLE_NAME,
177                               DB::MEASUREMENT_UNIT_ID)));
178  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID),
179                        concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID)));
180  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID),
181                        concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID)));
182  str_query.addWhere(op(concat(DB::COMPANY_TABLE_NAME, DB::RIF),
183                        concat(DB::PLANT_TABLE_NAME, DB::UE_RIF)));
184
185  if (not query.exec(str_query))
186    {
187      std::string msg = "Cannot execute query: " + std::string(str_query);
188      throw std::logic_error(msg);
189    }
190
191  Product product;
192
193  if (not query.next())
194    return product;
195
196  product.set_company_name(
197    query.getValue(concat(DB::COMPANY_TABLE_NAME, DB::COMPANY_NAME, '_')));
198  product.set_company_rif(
199    query.getValue(concat(DB::COMPANY_TABLE_NAME, DB::RIF, '_')));
200  product.set_id(
201    std::atol(query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::ID, '_'))));
202  product.set_name(query.getValue(concat(DB::PRODUCT_TABLE_NAME,
203                                         DB::NAME, '_')));
204  product.set_tariff_code(
205    query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE, '_')));
206  product.set_measurement_unit(
207    query.getValue(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME, '_')));
208
209  return product;
210}
211
212void list_input_ids(const db_id_t & product_id, const std::string & year,
213                    List<db_id_t> & input_ids)
214{
215  AutoConnection conn;
216  DBQuery query(conn);
217  StrQuery str_query;
218
219  str_query.addSelect(DB::INPUT_ID);
220  str_query.addFrom(DB::PRODUCT_INPUT_TABLE_NAME);
221  str_query.addWhere(op(DB::PRODUCT_ID, id_to_str(product_id)));
222  str_query.addWhere(op(DB::QUANTITY, "0", "<>"));
223  str_query.addWhere(op(DB::YEAR, year));
224
225  if (not query.exec(str_query))
226    {
227      std::string msg = "Cannot execute query: " + std::string(str_query);
228      throw std::logic_error(msg);
229    }
230
231  while (query.next())
232    input_ids.append(std::atol(query.getValue(DB::INPUT_ID)));
233}
234
235void list_inputs(const db_id_t & input_id, const std::string & year,
236                 List<Input> & inputs)
237{
238  AutoConnection conn;
239  DBQuery query(conn);
240  StrQuery str_query;
241
242  str_query.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::ID));
243  str_query.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::NAME));
244  str_query.addSelect(concatas(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE));
245  str_query.addSelect(concatas(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME));
246
247  str_query.addFrom(DB::INPUT_TABLE_NAME);
248  str_query.addFrom(DB::MEASUREMENT_UNIT_TABLE_NAME);
249
250  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID),
251                               id_to_str(input_id)));
252  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME,
253                               DB::MEASUREMENT_UNIT_ID),
254                        concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::ID)));
255
256  if (not query.exec(str_query))
257    {
258      std::string msg = "Cannot execute query: " + std::string(str_query);
259      throw std::logic_error(msg);
260    }
261
262  if (not query.next())
263    {
264      std::stringstream msg;
265      msg << "Input with id = " << input_id << " doesn't exist in database";
266      throw std::domain_error(msg.str());
267    }
268
269  Input input;
270
271  input.set_id(
272    std::atol(query.getValue(concat(DB::INPUT_TABLE_NAME, DB::ID, '_'))));
273  input.set_name(query.getValue(concat(DB::INPUT_TABLE_NAME, DB::NAME, '_')));
274  input.set_tariff_code(
275    query.getValue(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE, '_')));
276  input.set_measurement_unit(
277    query.getValue(concat(DB::MEASUREMENT_UNIT_TABLE_NAME, DB::NAME, '_')));
278
279  str_query.clear();
280  query.clear();
281
282  str_query.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NAME));
283  str_query.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::RIF));
284  str_query.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NATIONALITY));
285
286  str_query.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME);
287  str_query.addFrom(DB::SUPPLIER_TABLE_NAME);
288     
289  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R),
290                        year));
291  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID),
292                        id_to_str(input_id)));
293  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME,
294                               DB::SUPPLIER_ID),
295                        concat(DB::SUPPLIER_TABLE_NAME, DB::ID)));
296
297  if (not query.exec(str_query))
298    {
299      std::string msg = "Cannot execute query: " + std::string(str_query);
300      throw std::logic_error(msg);
301    }
302
303  while (query.next())
304    {
305      input.set_company_rif(
306        query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF, '_')));
307      input.set_company_name(
308        query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::NAME, '_')));
309      input.set_nationality(query.getValue(concat(DB::SUPPLIER_TABLE_NAME,
310                                                  DB::NATIONALITY, '_')));
311      inputs.append(input);
312    }
313}
314
315void product_from_input(const Input & input, List<Product> & products)
316{
317  AutoConnection conn;
318  DBQuery query(conn);
319  StrQuery str_query;
320
321  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID));
322
323  str_query.addFrom(DB::INPUT_TABLE_NAME);
324  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
325  str_query.addFrom(DB::PLANT_TABLE_NAME);
326
327  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID),
328                        id_to_str(input.get_id())));
329  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE),
330                        concat(DB::PRODUCT_TABLE_NAME, DB::TARIFF_CODE)));
331  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID),
332                        concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID)));
333  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF),
334                        single_quote(input.get_company_rif())));
335
336  if (not query.exec(str_query))
337    {
338      std::string msg = "Cannot execute query: " + std::string(str_query);
339      throw std::logic_error(msg);
340    }
341
342  while (query.next())
343    products.append(load_product_by_id(
344      std::atol(query.getValue(concat(DB::PRODUCT_TABLE_NAME, DB::ID, '_')))));
345}
346
347real get_unitarian_request(const db_id_t & input_id,
348                           const db_id_t & product_id)
349{
350  AutoConnection conn;
351  DBQuery query(conn);
352  StrQuery str_query;
353
354  str_query.addSelect(DB::QUANTITY);
355
356  str_query.addFrom(DB::PRODUCT_INPUT_TABLE_NAME);
357
358  str_query.addWhere(op(DB::PRODUCT_ID, id_to_str(product_id)));
359  str_query.addWhere(op(DB::INPUT_ID, id_to_str(input_id)));
360
361  if (not query.exec(str_query))
362    {
363      std::string msg = "Cannot execute query: " + std::string(str_query);
364      throw std::logic_error(msg);
365    }
366
367  if (not query.next())
368    return 0.0;
369
370  return std::atof(query.getValue(0));
371}
372
373real get_acquisition_price(const std::string & nationality,
374                           const db_id_t & input_id,
375                           const std::string & year)
376{
377  AutoConnection conn;
378  DBQuery query(conn);
379  StrQuery str_query;
380
381  std::string price_field = DB::NAC_ACQ_PRICE;
382
383  if (nationality == "E")     
384    price_field = DB::ACQ_PRICE;
385
386  str_query.addSelect(price_field);
387
388  str_query.addFrom(DB::ACQ_PRICE_TABLE_NAME);
389
390  str_query.addWhere(op(DB::INPUT_ID, id_to_str(input_id)));
391  str_query.addWhere(in_year(DB::YEAR, year));
392
393  if (not query.exec(str_query))
394    {
395      std::string msg = "Cannot execute query: " + std::string(str_query);
396      throw std::logic_error(msg);
397    } 
398
399  if (not query.next()) 
400    return 0.0;
401
402  return std::atof(query.getValue(0));
403}
404
405real get_declared_bought_quantity(const std::string & rif,
406                                  const std::string & company_name,
407                                  const db_id_t & input_id,
408                                  const std::string & year)
409{
410  AutoConnection conn;
411
412  DBQuery query(conn);
413
414  StrQuery str_query;
415
416  str_query.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME,
417                               DB::BOUGHT_QUANTITY));
418
419  str_query.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME);
420  str_query.addFrom(DB::SUPPLIER_TABLE_NAME);
421
422  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R),
423                        year));
424  if (rif.size() > 0)
425    str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF),
426                          single_quote(rif)));
427  else
428    str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::NAME),
429                          single_quote(company_name)));
430
431  str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::ID),
432                        concat(DB::INPUT_SUPPLIER_TABLE_NAME,
433                              DB::SUPPLIER_ID)));
434  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID),
435                        id_to_str(input_id)));
436
437  if (not query.exec(str_query))
438    {
439      std::string msg = "Cannot execute query: " + std::string(str_query);
440      throw std::logic_error(msg);
441    }
442
443  if (not query.next())
444    return 0.0;
445
446  return std::atof(query.getValue(0));
447}
448
449Aleph::pair<real, real>
450get_produced_quantity_and_price(const db_id_t & product_id,
451                                const std::string & year)
452{
453  AutoConnection conn;
454  DBQuery query(conn);
455  StrQuery str_query;
456
457  str_query.addSelect(DB::PRODUCED_QUANTITY);
458  str_query.addSelect(DB::PRICE);
459
460  str_query.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME);
461
462  str_query.addWhere(op(DB::PRODUCT_ID, id_to_str(product_id)));
463  str_query.addWhere(in_year(DB::YEAR, year));
464
465  if (not query.exec(str_query))
466    {
467      std::string msg = "Cannot execute query: " + std::string(str_query);
468      throw std::logic_error(msg);
469    }
470
471  if (not query.next())
472    return Aleph::pair<real, real>(0.0, 0.0);
473
474  return Aleph::pair<real, real>(std::atof(query.getValue(0)),
475                                 std::atof(query.getValue(1)));
476}
477
478void list_product_ids(Good * good, List<Aleph::pair<long, long>> & list)
479{
480  AutoConnection conn;
481  DBQuery query(conn);
482  StrQuery str_query;
483
484  const std::string & rif = good->get_company_rif();
485
486  const std::string & tariff_code = good->get_tariff_code();
487
488  str_query.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID));
489  str_query.addSelect(concatas(DB::PRODUCT_INPUT_TABLE_NAME, DB::INPUT_ID));
490
491  str_query.addFrom(DB::SUPPLIER_TABLE_NAME);
492  str_query.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME);
493  str_query.addFrom(DB::PRODUCT_INPUT_TABLE_NAME);
494  str_query.addFrom(DB::INPUT_TABLE_NAME);
495  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
496
497  str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF),
498                        single_quote(rif)));
499  str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::ID),
500                        concat(DB::INPUT_SUPPLIER_TABLE_NAME,
501                               DB::SUPPLIER_ID)));
502  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID),
503                       concat(DB::INPUT_TABLE_NAME, DB::ID)));
504  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE),
505                       single_quote(tariff_code)));
506  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID),
507                         concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::INPUT_ID)));
508  str_query.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::PRODUCT_ID),
509                        concat(DB::PRODUCT_TABLE_NAME, DB::ID)));
510  str_query.addWhere(op(concat(DB::PRODUCT_INPUT_TABLE_NAME, DB::QUANTITY),
511                               "0", "<>"));
512
513  if (not query.exec(str_query))
514    {
515      std::string msg = "Cannot execute query: " + std::string(str_query);
516      throw std::logic_error(msg);
517    }
518
519  while (query.next())
520    list.append(Aleph::pair<long, long>(std::atol(query.getValue(0)),
521                                        std::atol(query.getValue(1))));
522}
523
524void list_requesters(Good * good, List<Aleph::pair<Product, long>> & list)
525{
526  List<Aleph::pair<long, long>> ids;
527
528  list_product_ids(good, ids);
529
530  for(List <Aleph::pair<long, long>>::Iterator it(ids); it.has_current();
531      it.next())
532    {
533      Aleph::pair<long, long> & p = it.get_current();
534
535      Product product = load_product_by_id(p.first);
536
537      list.append(Aleph::pair<Product, long>(product, p.second));
538    }
539}
540
541real get_product_used_capacity(const db_id_t & product_id,
542                               const std::string & year)
543{
544  AutoConnection conn;
545  DBQuery query(conn);
546  StrQuery str_query; 
547
548  str_query.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::ID));
549  str_query.addSelect(concatas(DB::PLANT_TABLE_NAME, DB::USED_CAPACITY));
550
551  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
552  str_query.addFrom(DB::PLANT_TABLE_NAME);
553
554  str_query.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID),
555                               id_to_str(product_id)));
556  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID),
557                        concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID)));
558
559  if (not query.exec(str_query))
560    {
561      std::string msg = "Cannot execute query: " + std::string(str_query);
562      throw std::logic_error(msg);
563    }
564
565  if (not query.next())
566    return 0;
567
568  db_id_t plant_id = std::atol(query.getValue(0));
569  real plant_capacity = std::atof(query.getValue(1));
570
571  query.clear();
572  str_query.clear();
573
574  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID));
575  str_query.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
576                               DB::PRODUCED_QUANTITY));
577  str_query.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
578                               DB::PRICE));
579
580  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
581  str_query.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME);
582
583  str_query.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID),
584                        id_to_str(plant_id)));
585  str_query.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID),
586                        concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
587                               DB::PRODUCT_ID)));
588  str_query.addWhere(in_year(DB::YEAR, year));
589
590  if (not query.exec(str_query))
591    {
592      std::string msg = "Cannot execute query: " + std::string(str_query);
593      throw std::logic_error(msg);
594    }
595
596  real total_amount = 0.0;
597
598  real amount = 0.0;
599
600  while (query.next())
601    {
602      db_id_t p_id  = std::atol(query.getValue(0));
603      real quantity = std::atof(query.getValue(1));
604      real price    = std::atof(query.getValue(2));
605
606      total_amount += quantity * price;
607
608      if (p_id == product_id)
609        amount = quantity * price;
610    }
611
612  real proportion = total_amount == 0.0 ? 0.0 : amount / total_amount;
613
614  return plant_capacity * proportion;
615}
616
617std::tuple<real, real, real>
618get_product_staff_numbers(const std::string & rif,
619                          const std::string & year,
620                          const db_id_t & product_id)
621{
622  AutoConnection conn;
623  DBQuery query(conn);
624  StrQuery str_query;
625
626  /* Se le suma una unidad al año porque el registro de empleos se hace al
627     año siguiente.
628  */
629  int y = std::atoi(year.c_str()) + 1;
630
631  std::stringstream sstr_year;
632
633  sstr_year << y;
634
635  std::string max_date = sstr_year.str() + "-12-31";
636
637  str_query.addSelect(DB::ADMINISTRATIVE);
638  str_query.addSelect(DB::OPERATING);
639  str_query.addSelect(DB::WORKDAY);
640
641  str_query.addFrom(DB::COMPANY_OCCUPANCY_TABLE_NAME);
642
643  str_query.addWhere(op(DB::COMPANY_RIF, single_quote(rif)));
644  str_query.addWhere(op(DB::ACTIVE, "TRUE"));
645  str_query.addWhere(op(DB::INSERTION_DATE, single_quote(max_date), "<="));
646
647  str_query.addOrderBy(DB::INSERTION_DATE);
648
649  str_query.setOrderByOption("DESC");
650
651  if (not query.exec(str_query))
652    {
653      std::string msg = "Cannot execute query: " + std::string(str_query);
654      throw std::logic_error(msg);
655    }
656
657  if (not query.next())
658    return std::tuple<real, real, real>(0.0, 0.0, 0.0);
659
660  real administrative = std::atof(query.getValue(0));
661  real operating = std::atof(query.getValue(1));
662  real workday = std::atof(query.getValue(2));
663
664  query.clear();
665  str_query.clear();
666
667  str_query.addSelect(concatas(DB::PRODUCT_TABLE_NAME, DB::ID));
668  str_query.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
669                               DB::PRODUCED_QUANTITY));
670  str_query.addSelect(concatas(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
671                               DB::PRICE));
672
673  str_query.addFrom(DB::PRODUCT_TABLE_NAME);
674  str_query.addFrom(DB::PLANT_TABLE_NAME);
675  str_query.addFrom(DB::PRODUCTION_QUANTITIES_TABLE_NAME);
676
677  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::UE_RIF),
678                        single_quote(rif)));
679  str_query.addWhere(op(concat(DB::PLANT_TABLE_NAME, DB::ID),
680                        concat(DB::PRODUCT_TABLE_NAME, DB::PLANT_ID)));
681  str_query.addWhere(op(concat(DB::PRODUCT_TABLE_NAME, DB::ID),
682                        concat(DB::PRODUCTION_QUANTITIES_TABLE_NAME,
683                               DB::PRODUCT_ID)));
684  str_query.addWhere(in_year(DB::YEAR, year));
685
686  if (not query.exec(str_query))
687    {
688      std::string msg = "Cannot execute query: " + std::string(str_query);
689      throw std::logic_error(msg);
690    }
691
692  real total_amount = 0.0;
693
694  real amount = 0.0;
695
696  while (query.next())
697    {
698      db_id_t p_id  = std::atol(query.getValue(0));
699      real quantity = std::atof(query.getValue(1));
700      real price    = std::atof(query.getValue(2));
701
702      total_amount += quantity * price;
703
704      if (p_id == product_id)
705        amount = quantity * price;
706    }
707
708  real proportion = total_amount == 0.0 ? 0.0 : amount / total_amount;
709
710  administrative *= proportion;
711  operating *= proportion;
712
713  return std::tuple<real, real, real>(workday, administrative, operating);
714}
715
716real get_external_sales(const std::string & tariff_code,
717                        const std::string & seller_rif,
718                        const std::string & year,
719                        const std::string & buyers_to_exclude)
720{
721  AutoConnection conn;
722  DBQuery query(conn);
723  StrQuery str_query;
724
725  str_query.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME,
726                               DB::BOUGHT_QUANTITY));
727  str_query.addSelect(concatas(DB::ACQ_PRICE_TABLE_NAME, DB::NAC_ACQ_PRICE));
728
729  str_query.addFrom(DB::INPUT_TABLE_NAME);
730  str_query.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME);
731  str_query.addFrom(DB::SUPPLIER_TABLE_NAME);
732  str_query.addFrom(DB::ACQ_PRICE_TABLE_NAME);
733  str_query.addFrom(DB::PLANT_TABLE_NAME);
734
735  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::TARIFF_CODE),
736                        single_quote(tariff_code)));
737  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::ID),
738                        concat(DB::INPUT_SUPPLIER_TABLE_NAME,
739                               DB::INPUT_ID)));
740  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME,
741                               DB::YEAR_R), year));
742  str_query.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME,
743                               DB::SUPPLIER_ID),
744                        concat(DB::SUPPLIER_TABLE_NAME, DB::ID)));
745  str_query.addWhere(op(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF),
746                        single_quote(seller_rif)));
747  str_query.addWhere(op(concat(DB::ACQ_PRICE_TABLE_NAME, DB::INPUT_ID),
748                        concat(DB::INPUT_TABLE_NAME, DB::ID)));
749  str_query.addWhere(in_year(DB::YEAR, year));
750
751  str_query.addWhere(op(concat(DB::INPUT_TABLE_NAME, DB::PLANT_ID),
752                        concat(DB::PLANT_TABLE_NAME, DB::ID)));
753
754  if (buyers_to_exclude.size() > 0)
755    {
756
757      std::string exclude = concat(DB::PLANT_TABLE_NAME, DB::UE_RIF) +
758                            " NOT IN (" + buyers_to_exclude + ")";
759
760      str_query.addWhere(exclude);
761    }
762
763  if (not query.exec(str_query))
764    {
765      std::string msg = "Cannot execute query: " + std::string(str_query);
766      throw std::logic_error(msg);
767    }
768
769  real external_sales = 0.0;
770
771  while (query.next())
772    {
773      external_sales += std::atof(query.getValue(0)) *
774                        std::atof(query.getValue(1));
775    }
776
777  return external_sales;
778}
779
Note: See TracBrowser for help on using the repository browser.