commissionhandler.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. $sqlCroSho = $this->createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, true);
  204. $commissionsCroSho = $this->getMulticurrencyTotalTTC($sqlCroSho);
  205. return $commissions + $commissionsCroSho;
  206. }
  207. function getMulticurrencyTotalTTC($sql)
  208. {
  209. $commissions = 0;
  210. $result = $this->db->query($sql);
  211. while ($factureRow = pg_fetch_assoc($result)) {
  212. //if (is_null($factureRow['bbt_rowid']) || !is_null($factureRow['bbtip_rowid'])) {
  213. $commissions += $factureRow['multicurrency_total_ttc'];
  214. //}
  215. }
  216. return $commissions;
  217. }
  218. function createSQL($from, $to, $currency, $code, $entity, $user_id, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $crossShoppingFactures, $crossShopping = false)
  219. {
  220. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission, f.ref, bbt.rowid as bbt_rowid, bbtip.rowid as bbtip_rowid
  221. FROM llx_facture as f
  222. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  223. LEFT JOIN llx_bbus_bbticket as bbt ON bbt.fk_facture = f.rowid
  224. LEFT JOIN llx_bbus_bbticketinvoiceprinting as bbtip ON bbtip.invoice_number = f.ref WHERE";
  225. if ($crossShopping && $crossShoppingFactures != "") {
  226. $sql .= " f.ref in ({$crossShoppingFactures}) AND";
  227. } else {
  228. if ($crossShoppingFactures != "") {
  229. $sql .= " f.ref not in ({$crossShoppingFactures}) AND";
  230. }
  231. }
  232. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  233. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  234. }
  235. $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
  236. fk_user_closing = {$user_id}
  237. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  238. AND multicurrency_code = '{$currency}'
  239. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  240. return $sql;
  241. }
  242. function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
  243. {
  244. global $db;
  245. $from = $this->calculateFromDate($id);
  246. $to = $this->calculateToDate($user_id, $from);
  247. $commissions = 0;
  248. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  249. FROM llx_facture as f
  250. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  251. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  252. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  253. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  254. }
  255. $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
  256. fk_user_closing = {$user_id}
  257. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  258. AND multicurrency_code = '{$currency}'
  259. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  260. //print $sql . '<br>';
  261. $result = $db->query($sql);
  262. while ($row = pg_fetch_assoc($result)) {
  263. $commissions += $row['multicurrency_total_ttc'];
  264. }
  265. return $commissions;
  266. }
  267. public function calculateFromDate($id)
  268. {
  269. global $db;
  270. $packageHistoryObj = new PackageHistory($db);
  271. $packageHistoryObj->fetch($id);
  272. return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
  273. }
  274. public function calculateToDate($user_id, $from)
  275. {
  276. global $db;
  277. $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
  278. WHERE user_id = {$user_id}
  279. AND date_creation > '{$from}'
  280. ORDER BY rowid DESC";
  281. $result1 = $db->query($sql1);
  282. while ($row1 = pg_fetch_assoc($result1)) {
  283. $toDate = $row1['date_creation'];
  284. }
  285. $now = dol_now();
  286. return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
  287. }
  288. public function getAmountOfCommission($row)
  289. {
  290. $total_ttc = $row['total_ttc'];
  291. $commission = $row['commission'];
  292. if (isset($commission) && $commission != '_' && !is_null($commission)) {
  293. $array = explode('_', $commission);
  294. $number = $array[0];
  295. $type = $array[1];
  296. if ($type !== '%') {
  297. return $number;
  298. } else {
  299. //print number_format($number / 100, 4, '.', '');
  300. return $total_ttc * number_format($number / 100, 4, '.', '');
  301. }
  302. } else {
  303. return 0;
  304. }
  305. }
  306. function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
  307. {
  308. $commissions = 0;
  309. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  310. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  311. WHERE
  312. fk_user_closing = {$user_id} AND
  313. multicurrency_code = 'HUF' AND
  314. date_closing BETWEEN '{$from}' AND '{$to}' ";
  315. $result = $this->db->query($sql);
  316. while ($row = pg_fetch_assoc($result)) {
  317. $commissions += $this->getAmountOfCommission($row);
  318. }
  319. return $commissions;
  320. }
  321. function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
  322. {
  323. global $db;
  324. $commissions = 0;
  325. $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
  326. WHERE user_id = {$user_id}
  327. AND payment_type = 2";
  328. if (!is_null($year) && !is_null($month)) {
  329. $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
  330. }
  331. $result = $db->query($sql);
  332. while ($row = pg_fetch_assoc($result)) {
  333. return $row['sum'];
  334. }
  335. }
  336. function getUserMinimumCommission($user_id)
  337. {
  338. global $db;
  339. $userObj = new User($db);
  340. $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}";
  341. $result = $db->query($sql);
  342. while($row = $db->fetch_object($result))
  343. {
  344. return $row->minimum_monthly_commission;
  345. }
  346. }
  347. function getTotalCommission($user_id, $from, $to)
  348. {
  349. global $db;
  350. $helper = new HelperUserInvoice($db);
  351. $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
  352. $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  353. $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  354. $fullCommission = $commissionHUF + $commissionEUR;
  355. return $fullCommission;
  356. }
  357. function getTotalCommissionFromThisPeriod($user_id, $from, $to)
  358. {
  359. global $db;
  360. $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
  361. $array = [];
  362. $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}'";
  363. $data = $db->query($sql);
  364. if(pg_num_rows($data) > 0){
  365. while ($row = pg_fetch_assoc($data)) {
  366. $array[] = $row;
  367. }
  368. }
  369. return $array;
  370. }
  371. function getDailyMinimumCommission()
  372. {
  373. global $db;
  374. $date = date('Y-m-d H:i:s', dol_now());
  375. $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
  376. INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
  377. WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
  378. //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
  379. $result = $db->query($sql);
  380. if(pg_num_rows($result) > 0){
  381. while ($row = pg_fetch_assoc($result)) {
  382. return $row['amount'];
  383. }
  384. }
  385. return 0;
  386. }
  387. }