commissionhandler.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. $commissions = 0;
  96. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  97. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  98. WHERE f.fk_user_closing IN ({$allUsersString})
  99. AND EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND ";
  100. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  101. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  102. }
  103. $sql .= " multicurrency_code = '{$currency}' AND
  104. date_closing BETWEEN '{$from}' AND '{$to}'
  105. --AND fe.marked_for_storno IS NULL
  106. AND fe.commission_deduction is NULL
  107. AND (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing IN ({$allUsersString})) Is NULL";
  108. //print $sql.'<br>';
  109. $result = $this->db->query($sql);
  110. while ($row = pg_fetch_assoc($result)) {
  111. $commissions += $this->getAmountOfCommission($row);
  112. }
  113. return $commissions;
  114. }
  115. function getCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
  116. {
  117. $to = date('Y-m-d H:i:s', dol_now());
  118. $crossShoppingFacturesString = "'MrNSTR2409-001003'";
  119. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  120. $postFields = json_encode($params);
  121. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  122. global $db;
  123. $commissions = 0;
  124. $sql = "SELECT f.rowid, f.total_ttc, fe.commission, f.ref, bbt.rowid as bbt_rowid, bbtip.rowid as bbtip_rowid
  125. FROM llx_facture as f
  126. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  127. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  128. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE ";
  129. if ($crossShoppingFactures != '') {
  130. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  131. }
  132. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  133. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  134. }
  135. $sql .= "
  136. fk_user_closing = {$user_id}
  137. AND multicurrency_code = '{$currency}'
  138. AND fe.commission_deduction is NULL
  139. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  140. //print $sql;
  141. $result = $db->query($sql);
  142. if ($db->num_rows($result) != 0) {
  143. while ($row = pg_fetch_assoc($result)) {
  144. if (is_null($row['bbt_rowid']) || !is_null($row['bbtip_rowid'])) {
  145. $commissions += $this->getAmountOfCommission($row);
  146. }
  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 = {$user_id}
  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. }
  171. function getCashCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $entity, $code)
  172. {
  173. $to = date('Y-m-d H:i:s', dol_now());
  174. $crossShoppingFacturesString = "";
  175. $sqlForCrossShoppingFactures = "SELECT f.ref
  176. FROM llx_facture as f
  177. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  178. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  179. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE";
  180. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  181. $sqlForCrossShoppingFactures .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  182. }
  183. $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
  184. fk_user_closing = {$user_id}
  185. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  186. AND multicurrency_code = '{$currency}'
  187. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  188. $resultForCrossShoppingFactures = $this->db->query($sqlForCrossShoppingFactures);
  189. if ($this->db->num_rows($resultForCrossShoppingFactures) > 0) {
  190. while ($factureCSRow = pg_fetch_assoc($resultForCrossShoppingFactures)) {
  191. if ($crossShoppingFacturesString == "") {
  192. $crossShoppingFacturesString .= "'" . $factureCSRow['ref'] . "'";
  193. } else {
  194. $crossShoppingFacturesString .= ",'" . $factureCSRow['ref'] . "'";
  195. }
  196. }
  197. }
  198. $params = ["from" => $from, "to" => $to, "factures" => $crossShoppingFacturesString];
  199. $postFields = json_encode($params);
  200. $crossShoppingFactures = $this->curlRunner('bookingapi/getPrintedFacturesRefs', $postFields, 'POST', true);
  201. $sql = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures);
  202. $commissions = $this->getMulticurrencyTotalTTC($sql);
  203. $commissionsCroSho = 0;
  204. if($crossShoppingFactures !== ""){
  205. $sqlCroSho = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, true);
  206. $commissionsCroSho = $this->getMulticurrencyTotalTTC($sqlCroSho);
  207. }
  208. return $commissions + $commissionsCroSho;
  209. }
  210. function getMulticurrencyTotalTTC($sql)
  211. {
  212. $commissions = 0;
  213. $result = $this->db->query($sql);
  214. while ($factureRow = pg_fetch_assoc($result)) {
  215. //if (is_null($factureRow['bbt_rowid']) || !is_null($factureRow['bbtip_rowid'])) {
  216. $commissions += $factureRow['multicurrency_total_ttc'];
  217. //}
  218. }
  219. return $commissions;
  220. }
  221. function createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, $crossShopping = false)
  222. {
  223. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission, f.ref, bbt.rowid as bbt_rowid, bbtip.rowid as bbtip_rowid
  224. FROM llx_facture as f
  225. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  226. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  227. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE";
  228. if ($crossShopping && $crossShoppingFactures != "") {
  229. $sql .= " f.ref in ({$crossShoppingFactures}) AND";
  230. } else {
  231. if ($crossShoppingFactures != "") {
  232. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  233. }
  234. }
  235. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  236. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  237. }
  238. $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
  239. fk_user_closing = {$user_id}
  240. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  241. AND multicurrency_code = '{$currency}'
  242. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  243. return $sql;
  244. }
  245. function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
  246. {
  247. global $db;
  248. $from = $this->calculateFromDate($id);
  249. $to = $this->calculateToDate($user_id, $from);
  250. $commissions = 0;
  251. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  252. FROM llx_facture as f
  253. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  254. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  255. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  256. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  257. }
  258. $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
  259. fk_user_closing = {$user_id}
  260. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  261. AND multicurrency_code = '{$currency}'
  262. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  263. //print $sql . '<br>';
  264. $result = $db->query($sql);
  265. while ($row = pg_fetch_assoc($result)) {
  266. $commissions += $row['multicurrency_total_ttc'];
  267. }
  268. return $commissions;
  269. }
  270. public function calculateFromDate($id)
  271. {
  272. global $db;
  273. $packageHistoryObj = new PackageHistory($db);
  274. $packageHistoryObj->fetch($id);
  275. return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
  276. }
  277. public function calculateToDate($user_id, $from)
  278. {
  279. global $db;
  280. $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
  281. WHERE user_id = {$user_id}
  282. AND date_creation > '{$from}'
  283. ORDER BY rowid DESC";
  284. $result1 = $db->query($sql1);
  285. while ($row1 = pg_fetch_assoc($result1)) {
  286. $toDate = $row1['date_creation'];
  287. }
  288. $now = dol_now();
  289. return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
  290. }
  291. public function getAmountOfCommission($row)
  292. {
  293. $total_ttc = $row['total_ttc'];
  294. $commission = $row['commission'];
  295. if (isset($commission) && $commission != '_' && !is_null($commission)) {
  296. $array = explode('_', $commission);
  297. $number = $array[0];
  298. $type = $array[1];
  299. if ($type !== '%') {
  300. return $number;
  301. } else {
  302. //print number_format($number / 100, 4, '.', '');
  303. return $total_ttc * number_format($number / 100, 4, '.', '');
  304. }
  305. } else {
  306. return 0;
  307. }
  308. }
  309. function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
  310. {
  311. $commissions = 0;
  312. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  313. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  314. WHERE
  315. fk_user_closing = {$user_id} AND
  316. multicurrency_code = 'HUF' AND
  317. date_closing BETWEEN '{$from}' AND '{$to}' ";
  318. $result = $this->db->query($sql);
  319. while ($row = pg_fetch_assoc($result)) {
  320. $commissions += $this->getAmountOfCommission($row);
  321. }
  322. return $commissions;
  323. }
  324. function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
  325. {
  326. global $db;
  327. $commissions = 0;
  328. $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
  329. WHERE user_id = {$user_id}
  330. AND payment_type = 2";
  331. if (!is_null($year) && !is_null($month)) {
  332. $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
  333. }
  334. $result = $db->query($sql);
  335. while ($row = pg_fetch_assoc($result)) {
  336. return $row['sum'];
  337. }
  338. }
  339. function getUserMinimumCommission($user_id)
  340. {
  341. global $db;
  342. $userObj = new User($db);
  343. $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}";
  344. $result = $db->query($sql);
  345. while($row = $db->fetch_object($result))
  346. {
  347. return $row->minimum_monthly_commission;
  348. }
  349. }
  350. function getTotalCommission($user_id, $from, $to)
  351. {
  352. global $db;
  353. $helper = new HelperUserInvoice($db);
  354. $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
  355. $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  356. $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  357. $fullCommission = $commissionHUF + $commissionEUR;
  358. return $fullCommission;
  359. }
  360. function getTotalCommissionFromThisPeriod($user_id, $from, $to)
  361. {
  362. global $db;
  363. $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
  364. $array = [];
  365. $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}'";
  366. $data = $db->query($sql);
  367. if(pg_num_rows($data) > 0){
  368. while ($row = pg_fetch_assoc($data)) {
  369. $array[] = $row;
  370. }
  371. }
  372. return $array;
  373. }
  374. function getDailyMinimumCommission()
  375. {
  376. global $db;
  377. $date = date('Y-m-d H:i:s', dol_now());
  378. $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
  379. INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
  380. WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
  381. //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
  382. $result = $db->query($sql);
  383. if(pg_num_rows($result) > 0){
  384. while ($row = pg_fetch_assoc($result)) {
  385. return $row['amount'];
  386. }
  387. }
  388. return 0;
  389. }
  390. }