commissionhandler.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  3. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_curl.class.php';
  4. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus_log.class.php';
  5. class CommissionHandler
  6. {
  7. use CurlApi;
  8. public function __construct()
  9. {
  10. global $db, $user;
  11. $this->db = $db;
  12. $this->user = $user;
  13. }
  14. function getCommissionFromProduct($id)
  15. {
  16. $remiseSUM = 0;
  17. $commission = 0;
  18. $commission_type = '';
  19. $sqlRemise = "SELECT fdet.remise_percent FROM llx_facture as f
  20. INNER JOIN llx_facturedet as fdet ON fdet.fk_facture = f.rowid
  21. WHERE f.rowid = {$id}";
  22. $resultRemise = $this->db->query($sqlRemise);
  23. while ($remise = pg_fetch_assoc($resultRemise)) {
  24. $remiseSUM += $remise['remise_percent'];
  25. }
  26. $sql = "SELECT fk_product FROM public.llx_facturedet WHERE fk_facture = {$id}";
  27. $result = $this->db->query($sql);
  28. while ($row = pg_fetch_assoc($result)) {
  29. $product = new Product($this->db);
  30. $res = $product->fetch($row['fk_product']);
  31. if ($res > 0) {
  32. if($remiseSUM > 0 && $product->array_options['options_reduced_commission'] > 0){
  33. $commission_type = $this->getCommissionType($product->array_options['options_reduced_commission_type']);
  34. if($commission_type != '%'){
  35. $commission += $product->array_options['options_reduced_commission'];
  36. }else{
  37. $commission = $product->array_options['options_reduced_commission'];
  38. }
  39. }else{
  40. $commission_type = $this->getCommissionType($product->array_options['options_commission_type']);
  41. if($commission_type != '%'){
  42. $commission += $product->array_options['options_commission_value'];
  43. }else{
  44. $commission = $product->array_options['options_commission_value'];
  45. }
  46. }
  47. }
  48. }
  49. return number_format($commission, 8, '.', '') . '_' . $commission_type;
  50. }
  51. private function getCommissionType($type)
  52. {
  53. switch ($type) {
  54. case '1':
  55. return '%';
  56. break;
  57. case '2':
  58. return 'Ft';
  59. break;
  60. case '3':
  61. return '€';
  62. break;
  63. }
  64. }
  65. function updateCommission($object)
  66. {
  67. global $user, $db;
  68. $commission = $this->getCommissionFromProduct($object->id);
  69. $facture = new Facture($db);
  70. $result = $facture->fetch($object->id);
  71. if ($result > 0) {
  72. $facture->array_options['options_commission'] = $commission;
  73. $facture->update($user);
  74. }
  75. //$objectFacture->commission = $commission;
  76. //$objectFacture->update($user);
  77. }
  78. function getAllCommissionByUserId($user, $date, $currency)
  79. {
  80. $result = 0;
  81. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  82. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  83. WHERE
  84. fk_user_closing = {$user['user_id']} AND
  85. multicurrency_code = '{$currency}' AND
  86. date_closing < '{$date} 00:00:00'";
  87. $result = $this->db->query($sql);
  88. while ($row = pg_fetch_assoc($result)) {
  89. $commissions += $this->getAmountOfCommission($row);
  90. }
  91. return round($commissions, 3);
  92. }
  93. function getAllCommission($allUsersString, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
  94. {
  95. //print $allComissionInvoicesOfUserInTheGroupFromUserInvoice;
  96. if($to == null){
  97. $to = date('Y-m-d H:i:s', dol_now());
  98. }
  99. $crossShoppingFacturesString = '';
  100. $sqlForCrossShoppingFactures = "SELECT f.ref
  101. FROM llx_facture as f
  102. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  103. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid WHERE";
  104. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  105. $sqlForCrossShoppingFactures .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  106. }
  107. $sqlForCrossShoppingFactures .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing IN ({$allUsersString})) Is NULL AND
  108. fk_user_closing IN ({$allUsersString})
  109. AND multicurrency_code = '{$currency}'
  110. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  111. $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
  112. if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
  113. while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
  114. if ($crossShoppingFacturesString == "") {
  115. $crossShoppingFacturesString .= "'" . $factureCSRow['ref'] . "'";
  116. } else {
  117. $crossShoppingFacturesString .= ",'" . $factureCSRow['ref'] . "'";
  118. }
  119. }
  120. }
  121. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  122. $postFields = json_encode($params);
  123. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  124. global $db;
  125. $commissions = 0;
  126. $sql = "SELECT f.rowid, f.total_ttc, fe.commission, f.ref, f.date_closing
  127. FROM llx_facture as f
  128. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  129. INNER JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE ";
  130. if ($crossShoppingFactures != '') {
  131. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  132. }
  133. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  134. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  135. }
  136. $sql .= "
  137. fk_user_closing IN ({$allUsersString})
  138. AND multicurrency_code = '{$currency}'
  139. AND fe.commission_deduction is NULL
  140. AND date_closing BETWEEN '{$from}' AND '{$to}'
  141. GROUP BY f.rowid, f.total_ttc, fe.commission, f.ref";
  142. //print $sql;
  143. $result = $db->query($sql);
  144. if ($db->num_rows($result) != 0) {
  145. while ($row = pg_fetch_assoc($result)) {
  146. $commissions += $this->getAmountOfCommission($row);
  147. }
  148. }
  149. if ($crossShoppingFactures != '') {
  150. $sqlCS = "SELECT f.rowid, f.total_ttc, fe.commission, f.ref
  151. FROM llx_facture as f
  152. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid WHERE f.ref in ({$crossShoppingFactures}) AND";
  153. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  154. $sqlCS .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  155. }
  156. $sqlCS .= "
  157. fk_user_closing IN ({$allUsersString})
  158. AND multicurrency_code = '{$currency}'
  159. AND fe.commission_deduction is NULL
  160. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  161. //print $sqlCS;
  162. $resultCS = $db->query($sqlCS);
  163. if ($db->num_rows($resultCS) != 0) {
  164. while ($rowCS = pg_fetch_assoc($resultCS)) {
  165. $commissions += $this->getAmountOfCommission($rowCS);
  166. }
  167. }
  168. }
  169. return $commissions;
  170. /* $commissions = 0;
  171. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  172. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  173. WHERE f.fk_user_closing IN ({$allUsersString})
  174. AND EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND ";
  175. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  176. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  177. }
  178. $sql .= " multicurrency_code = '{$currency}' AND
  179. date_closing BETWEEN '{$from}' AND '{$to}'
  180. --AND fe.marked_for_storno IS NULL
  181. AND fe.commission_deduction is NULL
  182. AND (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing IN ({$allUsersString})) Is NULL";
  183. //print $sql.'<br>';
  184. $result = $this->db->query($sql);
  185. while ($row = pg_fetch_assoc($result)) {
  186. $commissions += $this->getAmountOfCommission($row);
  187. }
  188. return $commissions; */
  189. }
  190. function getCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
  191. {
  192. //print $allComissionInvoicesOfUserInTheGroupFromUserInvoice;
  193. if($to == null){
  194. $to = date('Y-m-d H:i:s', dol_now());
  195. }
  196. $crossShoppingFacturesString = '';
  197. $sqlForCrossShoppingFactures = "SELECT f.ref
  198. FROM llx_facture as f
  199. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  200. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid WHERE";
  201. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  202. $sqlForCrossShoppingFactures .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  203. }
  204. $sqlForCrossShoppingFactures .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
  205. fk_user_closing = {$user_id}
  206. AND multicurrency_code = '{$currency}'
  207. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  208. //print $sqlForCrossShoppingFactures . '<br>';
  209. $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
  210. if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
  211. while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
  212. if ($crossShoppingFacturesString == "") {
  213. $crossShoppingFacturesString .= "'" . $factureCSRow['ref'] . "'";
  214. } else {
  215. $crossShoppingFacturesString .= ",'" . $factureCSRow['ref'] . "'";
  216. }
  217. }
  218. }
  219. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  220. $postFields = json_encode($params);
  221. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  222. global $db;
  223. $commissions = 0;
  224. $sql = "SELECT f.rowid, f.total_ttc, fe.commission, f.ref, f.date_closing
  225. FROM llx_facture as f
  226. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  227. INNER JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE ";
  228. if ($crossShoppingFactures != '') {
  229. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  230. }
  231. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  232. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  233. }
  234. $sql .= "
  235. fk_user_closing = {$user_id}
  236. AND multicurrency_code = '{$currency}'
  237. AND fe.commission_deduction is NULL
  238. AND date_closing BETWEEN '{$from}' AND '{$to}'
  239. GROUP BY f.rowid, f.total_ttc, fe.commission, f.ref";
  240. //print $sql;
  241. $result = $db->query($sql);
  242. if ($db->num_rows($result) != 0) {
  243. while ($row = pg_fetch_assoc($result)) {
  244. $commissions += $this->getAmountOfCommission($row);
  245. }
  246. }
  247. if ($crossShoppingFactures != '') {
  248. $sqlCS = "SELECT f.rowid, f.total_ttc, fe.commission, f.ref
  249. FROM llx_facture as f
  250. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid WHERE f.ref in ({$crossShoppingFactures}) AND";
  251. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  252. $sqlCS .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  253. }
  254. $sqlCS .= "
  255. fk_user_closing = {$user_id}
  256. AND multicurrency_code = '{$currency}'
  257. AND fe.commission_deduction is NULL
  258. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  259. //print $sqlCS;
  260. $resultCS = $db->query($sqlCS);
  261. if ($db->num_rows($resultCS) != 0) {
  262. while ($rowCS = pg_fetch_assoc($resultCS)) {
  263. $commissions += $this->getAmountOfCommission($rowCS);
  264. }
  265. }
  266. }
  267. return $commissions;
  268. }
  269. function getCashCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $entity, $code)
  270. {
  271. $to = date('Y-m-d H:i:s', dol_now());
  272. $crossShoppingFacturesString = "";
  273. $sqlForCrossShoppingFactures = "SELECT f.ref
  274. FROM llx_facture as f
  275. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  276. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  277. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE";
  278. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  279. $sqlForCrossShoppingFactures .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  280. }
  281. $sqlForCrossShoppingFactures .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
  282. fk_user_closing = {$user_id}
  283. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  284. AND multicurrency_code = '{$currency}'
  285. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  286. //print $sqlForCrossShoppingFactures . '<br>';
  287. $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
  288. if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
  289. while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
  290. if ($crossShoppingFacturesString == "") {
  291. $crossShoppingFacturesString .= "'" . $factureCSRow['ref'] . "'";
  292. } else {
  293. $crossShoppingFacturesString .= ",'" . $factureCSRow['ref'] . "'";
  294. }
  295. }
  296. }
  297. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  298. $postFields = json_encode($params);
  299. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  300. //print $crossShoppingFactures;
  301. $sql = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures);
  302. $commissions = $this->getMulticurrencyTotalTTC($sql);
  303. $commissionsCroSho = 0;
  304. if($crossShoppingFactures !== ""){
  305. $sqlCroSho = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, true);
  306. $commissionsCroSho = $this->getMulticurrencyTotalTTC($sqlCroSho);
  307. }
  308. return $commissions + $commissionsCroSho;
  309. }
  310. function getMulticurrencyTotalTTC($sql)
  311. {
  312. $commissions = 0;
  313. $result = $this->db->query($sql);
  314. while ($factureRow = pg_fetch_assoc($result)) {
  315. $commissions += $factureRow['multicurrency_total_ttc'];
  316. }
  317. return $commissions;
  318. }
  319. function createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, $crossShopping = false)
  320. {
  321. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission, f.ref
  322. FROM llx_facture as f
  323. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid WHERE ";
  324. if ($crossShopping && $crossShoppingFactures != "") {
  325. $sql .= " f.ref in ({$crossShoppingFactures}) AND";
  326. } else {
  327. $sql .= " EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  328. if ($crossShoppingFactures != "") {
  329. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  330. }
  331. }
  332. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  333. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  334. }
  335. $sql .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
  336. fk_user_closing = {$user_id}
  337. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  338. AND multicurrency_code = '{$currency}'
  339. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  340. return $sql;
  341. }
  342. function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
  343. {
  344. global $db;
  345. $from = $this->calculateFromDate($id);
  346. $to = $this->calculateToDate($user_id, $from);
  347. $commissions = 0;
  348. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  349. FROM llx_facture as f
  350. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  351. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  352. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  353. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  354. }
  355. $sql .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
  356. fk_user_closing = {$user_id}
  357. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  358. AND multicurrency_code = '{$currency}'
  359. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  360. //print $sql . '<br>';
  361. $result = $db->query($sql);
  362. while ($row = pg_fetch_assoc($result)) {
  363. $commissions += $row['multicurrency_total_ttc'];
  364. }
  365. return $commissions;
  366. }
  367. public function calculateFromDate($id)
  368. {
  369. global $db;
  370. $packageHistoryObj = new PackageHistory($db);
  371. $packageHistoryObj->fetch($id);
  372. return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
  373. }
  374. public function calculateToDate($user_id, $from)
  375. {
  376. global $db;
  377. $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
  378. WHERE user_id = {$user_id}
  379. AND date_creation > '{$from}'
  380. ORDER BY rowid DESC";
  381. $result1 = $db->query($sql1);
  382. while ($row1 = pg_fetch_assoc($result1)) {
  383. $toDate = $row1['date_creation'];
  384. }
  385. $now = dol_now();
  386. return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
  387. }
  388. public function getAmountOfCommission($row)
  389. {
  390. $total_ttc = $row['total_ttc'];
  391. $commission = $row['commission'];
  392. if (isset($commission) && $commission != '_' && !is_null($commission)) {
  393. $array = explode('_', $commission);
  394. $number = $array[0];
  395. $type = $array[1];
  396. if ($type !== '%') {
  397. return $number;
  398. } else {
  399. //print number_format($number / 100, 4, '.', '');
  400. return $total_ttc * number_format($number / 100, 4, '.', '');
  401. }
  402. } else {
  403. return 0;
  404. }
  405. }
  406. function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
  407. {
  408. $commissions = 0;
  409. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  410. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  411. WHERE
  412. fk_user_closing = {$user_id} AND
  413. multicurrency_code = 'HUF' AND
  414. date_closing BETWEEN '{$from}' AND '{$to}' ";
  415. $result = $this->db->query($sql);
  416. while ($row = pg_fetch_assoc($result)) {
  417. $commissions += $this->getAmountOfCommission($row);
  418. }
  419. return $commissions;
  420. }
  421. function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
  422. {
  423. global $db;
  424. $commissions = 0;
  425. $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
  426. WHERE user_id = {$user_id}
  427. AND payment_type = 2";
  428. if (!is_null($year) && !is_null($month)) {
  429. $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
  430. }
  431. $result = $db->query($sql);
  432. while ($row = pg_fetch_assoc($result)) {
  433. return $row['sum'];
  434. }
  435. }
  436. function getUserMinimumCommission($user_id)
  437. {
  438. global $db;
  439. $userObj = new User($db);
  440. $sql = "SELECT ue.minimum_monthly_commission FROM llx_user as u INNER JOIN llx_user_extrafields as ue ON ue.fk_object = u.rowid WHERE u.rowid = {$user_id}";
  441. $result = $db->query($sql);
  442. while($row = $db->fetch_object($result))
  443. {
  444. return $row->minimum_monthly_commission;
  445. }
  446. }
  447. function getTotalCommission($user_id, $from, $to)
  448. {
  449. global $db;
  450. $helper = new HelperUserInvoice($db);
  451. $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
  452. $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  453. $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  454. $fullCommission = $commissionHUF + $commissionEUR;
  455. return $fullCommission;
  456. }
  457. function getTotalCommissionFromThisPeriod($user_id, $from, $to)
  458. {
  459. global $db;
  460. $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
  461. $array = [];
  462. $sql = "SELECT label, amount, date_creation FROM " . MAIN_DB_PREFIX . "financialreport_userinvoice WHERE user_id = {$user_id} AND payment_type = 2 AND date_creation BETWEEN '{$from}' AND '{$to}'";
  463. $data = $db->query($sql);
  464. if(pg_num_rows($data) > 0){
  465. while ($row = pg_fetch_assoc($data)) {
  466. $array[] = $row;
  467. }
  468. }
  469. return $array;
  470. }
  471. function getDailyMinimumCommission()
  472. {
  473. global $db;
  474. $date = date('Y-m-d H:i:s', dol_now());
  475. $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
  476. INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
  477. WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
  478. //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
  479. $result = $db->query($sql);
  480. if(pg_num_rows($result) > 0){
  481. while ($row = pg_fetch_assoc($result)) {
  482. return $row['amount'];
  483. }
  484. }
  485. return 0;
  486. }
  487. }