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