commissionhandler.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
  287. if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
  288. while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
  289. if ($crossShoppingFacturesString == "") {
  290. $crossShoppingFacturesString .= "'" . $factureCSRow['ref'] . "'";
  291. } else {
  292. $crossShoppingFacturesString .= ",'" . $factureCSRow['ref'] . "'";
  293. }
  294. }
  295. }
  296. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  297. $postFields = json_encode($params);
  298. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  299. $sql = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures);
  300. $commissions = $this->getMulticurrencyTotalTTC($sql);
  301. $commissionsCroSho = 0;
  302. if($crossShoppingFactures !== ""){
  303. $sqlCroSho = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, true);
  304. $commissionsCroSho = $this->getMulticurrencyTotalTTC($sqlCroSho);
  305. }
  306. return $commissions + $commissionsCroSho;
  307. }
  308. function getMulticurrencyTotalTTC($sql)
  309. {
  310. $commissions = 0;
  311. $result = $this->db->query($sql);
  312. while ($factureRow = pg_fetch_assoc($result)) {
  313. $commissions += $factureRow['multicurrency_total_ttc'];
  314. }
  315. return $commissions;
  316. }
  317. function createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, $crossShopping = false)
  318. {
  319. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission, f.ref
  320. FROM llx_facture as f
  321. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid WHERE ";
  322. if ($crossShopping && $crossShoppingFactures != "") {
  323. $sql .= " f.ref in ({$crossShoppingFactures}) AND";
  324. } else {
  325. $sql .= " EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  326. if ($crossShoppingFactures != "") {
  327. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  328. }
  329. }
  330. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  331. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  332. }
  333. $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
  334. fk_user_closing = {$user_id}
  335. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  336. AND multicurrency_code = '{$currency}'
  337. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  338. return $sql;
  339. }
  340. function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
  341. {
  342. global $db;
  343. $from = $this->calculateFromDate($id);
  344. $to = $this->calculateToDate($user_id, $from);
  345. $commissions = 0;
  346. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  347. FROM llx_facture as f
  348. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  349. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  350. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  351. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  352. }
  353. $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
  354. fk_user_closing = {$user_id}
  355. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  356. AND multicurrency_code = '{$currency}'
  357. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  358. //print $sql . '<br>';
  359. $result = $db->query($sql);
  360. while ($row = pg_fetch_assoc($result)) {
  361. $commissions += $row['multicurrency_total_ttc'];
  362. }
  363. return $commissions;
  364. }
  365. public function calculateFromDate($id)
  366. {
  367. global $db;
  368. $packageHistoryObj = new PackageHistory($db);
  369. $packageHistoryObj->fetch($id);
  370. return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
  371. }
  372. public function calculateToDate($user_id, $from)
  373. {
  374. global $db;
  375. $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
  376. WHERE user_id = {$user_id}
  377. AND date_creation > '{$from}'
  378. ORDER BY rowid DESC";
  379. $result1 = $db->query($sql1);
  380. while ($row1 = pg_fetch_assoc($result1)) {
  381. $toDate = $row1['date_creation'];
  382. }
  383. $now = dol_now();
  384. return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
  385. }
  386. public function getAmountOfCommission($row)
  387. {
  388. $total_ttc = $row['total_ttc'];
  389. $commission = $row['commission'];
  390. if (isset($commission) && $commission != '_' && !is_null($commission)) {
  391. $array = explode('_', $commission);
  392. $number = $array[0];
  393. $type = $array[1];
  394. if ($type !== '%') {
  395. return $number;
  396. } else {
  397. //print number_format($number / 100, 4, '.', '');
  398. return $total_ttc * number_format($number / 100, 4, '.', '');
  399. }
  400. } else {
  401. return 0;
  402. }
  403. }
  404. function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
  405. {
  406. $commissions = 0;
  407. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  408. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  409. WHERE
  410. fk_user_closing = {$user_id} AND
  411. multicurrency_code = 'HUF' AND
  412. date_closing BETWEEN '{$from}' AND '{$to}' ";
  413. $result = $this->db->query($sql);
  414. while ($row = pg_fetch_assoc($result)) {
  415. $commissions += $this->getAmountOfCommission($row);
  416. }
  417. return $commissions;
  418. }
  419. function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
  420. {
  421. global $db;
  422. $commissions = 0;
  423. $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
  424. WHERE user_id = {$user_id}
  425. AND payment_type = 2";
  426. if (!is_null($year) && !is_null($month)) {
  427. $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
  428. }
  429. $result = $db->query($sql);
  430. while ($row = pg_fetch_assoc($result)) {
  431. return $row['sum'];
  432. }
  433. }
  434. function getUserMinimumCommission($user_id)
  435. {
  436. global $db;
  437. $userObj = new User($db);
  438. $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}";
  439. $result = $db->query($sql);
  440. while($row = $db->fetch_object($result))
  441. {
  442. return $row->minimum_monthly_commission;
  443. }
  444. }
  445. function getTotalCommission($user_id, $from, $to)
  446. {
  447. global $db;
  448. $helper = new HelperUserInvoice($db);
  449. $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
  450. $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  451. $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  452. $fullCommission = $commissionHUF + $commissionEUR;
  453. return $fullCommission;
  454. }
  455. function getTotalCommissionFromThisPeriod($user_id, $from, $to)
  456. {
  457. global $db;
  458. $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
  459. $array = [];
  460. $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}'";
  461. $data = $db->query($sql);
  462. if(pg_num_rows($data) > 0){
  463. while ($row = pg_fetch_assoc($data)) {
  464. $array[] = $row;
  465. }
  466. }
  467. return $array;
  468. }
  469. function getDailyMinimumCommission()
  470. {
  471. global $db;
  472. $date = date('Y-m-d H:i:s', dol_now());
  473. $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
  474. INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
  475. WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
  476. //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
  477. $result = $db->query($sql);
  478. if(pg_num_rows($result) > 0){
  479. while ($row = pg_fetch_assoc($result)) {
  480. return $row['amount'];
  481. }
  482. }
  483. return 0;
  484. }
  485. }