PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/ajax/addinplace.php

https://bitbucket.org/speedealing/speedealing
PHP | 142 lines | 94 code | 22 blank | 26 comment | 19 complexity | a7902a8f089df67ffcf247f943e9c584 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2011-2013 Regis Houssin <regis.houssin@capnetworks.com>
  3. * Copyright (C) 2011-2013 Herve Prot <herve.prot@symeos.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. if (!defined('NOTOKENRENEWAL'))
  19. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  20. if (!defined('NOREQUIREMENU'))
  21. define('NOREQUIREMENU', '1');
  22. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  23. if (!defined('NOREQUIREAJAX'))
  24. define('NOREQUIREAJAX', '1');
  25. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  26. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  27. require '../../main.inc.php';
  28. $json = GETPOST('json', 'alpha');
  29. $class = GETPOST('class', 'alpha');
  30. /*
  31. * View
  32. */
  33. top_httphead('json');
  34. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  35. //print_r($_POST);
  36. error_log(print_r($_GET, true));
  37. error_log(print_r($_POST, true));
  38. if (!empty($json) && !empty($class)) {
  39. dol_include_once("/" . strtolower($class) . "/class/" . strtolower($class) . ".class.php");
  40. $object = new $class();
  41. $obj = new stdClass();
  42. if ($json == "add") {
  43. foreach ($object->fk_extrafields->fields as $key => $row) {
  44. if ($row->enable) {
  45. if (isset($row->class)) {
  46. if (!empty($_POST[$key])) {
  47. $class_tmp = $row->class;
  48. dol_include_once("/" . strtolower($class_tmp) . "/class/" . strtolower($class_tmp) . ".class.php");
  49. $object_tmp = new $class_tmp();
  50. $object->$key = new stdClass();
  51. $obj->$key = new stdClass();
  52. $object_tmp->fetch($_POST[$key]);
  53. $object->$key->id = $object_tmp->id;
  54. $object->$key->name = $object_tmp->name;
  55. $obj->$key->id = $object_tmp->id;
  56. $obj->$key->name = $object_tmp->name;
  57. }
  58. } else {
  59. if (!empty($_POST[$key])) {
  60. $object->$key = $_POST[$key];
  61. $obj->$key = $_POST[$key];
  62. } else {
  63. $object->$key = $row->default;
  64. $obj->$key = $row->default;
  65. }
  66. }
  67. }
  68. }
  69. if (method_exists($object, 'addInPlace'))
  70. $object->addInPlace($obj);
  71. try {
  72. $res = $object->record();
  73. $obj->_id = $res->id;
  74. } catch (Exception $exc) {
  75. error_log($exc->getMessage());
  76. exit;
  77. }
  78. } else if ($json == 'addline') {
  79. $line = new Line();
  80. $id = GETPOST('fk_invoice', 'alpha');
  81. $object->load($id);
  82. $idProduct = GETPOST('product');
  83. $productData = null;
  84. if (!empty($idProduct)) {
  85. dol_include_once("/product/class/product.class.php");
  86. $product = new Product($db);
  87. $product->load($idProduct);
  88. $line->description = $product->description;
  89. $line->pu_ht = $product->price->price;
  90. $line->tva_tx = $product->price->tva_tx;
  91. $line->product_type = $product->type;
  92. $productData = new stdClass();
  93. $productData->id = $idProduct;
  94. $productData->label = $product->label;
  95. //$productData->description = $product->description;
  96. } else {
  97. $line->pu_ht = GETPOST('pu_ht', "float");
  98. $line->tva_tx = GETPOST('tva_tx', "float");
  99. $line->product_type = GETPOST('product_type');
  100. }
  101. $line->description = GETPOST('description');
  102. $line->qty = GETPOST('qty');
  103. $line->remise = GETPOST('remise');
  104. $line->product = $productData;
  105. $line->verify();
  106. $object->updateline(count($object->lines), $line);
  107. $object->record();
  108. $obj->_id = $id . '#' . (intval($idline - 1));
  109. $obj->description = $line->description;
  110. $obj->pu_ht = $line->pu_ht;
  111. $obj->qty = $line->qty;
  112. $obj->remise = $line->remise;
  113. $obj->tva_tx = $line->tva_tx;
  114. $obj->total_ht = $line->total_ht;
  115. $obj->product = $productData;
  116. }
  117. }
  118. //error_log(json_encode($res));
  119. echo json_encode($obj);
  120. ?>