commissionhandler.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  3. class CommissionHandler
  4. {
  5. public function __construct()
  6. {
  7. global $db, $user;
  8. $this->db = $db;
  9. $this->user = $user;
  10. }
  11. function getCommissionFromProduct($id)
  12. {
  13. $remiseSUM = 0;
  14. $commission = 0;
  15. $commission_type = '';
  16. $sqlRemise = "SELECT fdet.remise_percent FROM llx_facture as f
  17. INNER JOIN llx_facturedet as fdet ON fdet.fk_facture = f.rowid
  18. WHERE f.rowid = {$id}";
  19. $resultRemise = $this->db->query($sqlRemise);
  20. while ($remise = pg_fetch_assoc($resultRemise)) {
  21. $remiseSUM += $remise['remise_percent'];
  22. }
  23. $sql = "SELECT fk_product FROM public.llx_facturedet WHERE fk_facture = {$id}";
  24. $result = $this->db->query($sql);
  25. while ($row = pg_fetch_assoc($result)) {
  26. $product = new Product($this->db);
  27. $res = $product->fetch($row['fk_product']);
  28. if ($res > 0) {
  29. if($remiseSUM > 0 && $product->array_options['options_reduced_commission'] > 0){
  30. $commission_type = $this->getCommissionType($product->array_options['options_reduced_commission_type']);
  31. if($commission_type != '%'){
  32. $commission += $product->array_options['options_reduced_commission'];
  33. }else{
  34. $commission = $product->array_options['options_reduced_commission'];
  35. }
  36. }else{
  37. $commission_type = $this->getCommissionType($product->array_options['options_commission_type']);
  38. if($commission_type != '%'){
  39. $commission += $product->array_options['options_commission_value'];
  40. }else{
  41. $commission = $product->array_options['options_commission_value'];
  42. }
  43. }
  44. }
  45. }
  46. return number_format($commission, 8, '.', '') . '_' . $commission_type;
  47. }
  48. private function getCommissionType($type)
  49. {
  50. switch ($type) {
  51. case '1':
  52. return '%';
  53. break;
  54. case '2':
  55. return 'Ft';
  56. break;
  57. case '3':
  58. return '€';
  59. break;
  60. }
  61. }
  62. function updateCommission($object)
  63. {
  64. global $user, $db;
  65. $commission = $this->getCommissionFromProduct($object->id);
  66. $facture = new Facture($db);
  67. $result = $facture->fetch($object->id);
  68. if ($result > 0) {
  69. $facture->array_options['options_commission'] = $commission;
  70. $facture->update($user);
  71. }
  72. //$objectFacture->commission = $commission;
  73. //$objectFacture->update($user);
  74. }
  75. function getAllCommissionByUserId($user, $date, $currency)
  76. {
  77. $result = 0;
  78. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  79. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  80. WHERE
  81. fk_user_closing = {$user['user_id']} AND
  82. multicurrency_code = '{$currency}' AND
  83. date_closing < '{$date} 00:00:00'";
  84. $result = $this->db->query($sql);
  85. while ($row = pg_fetch_assoc($result)) {
  86. $commissions += $this->getAmountOfCommission($row);
  87. }
  88. return round($commissions, 3);
  89. }
  90. function getAllCommission($allUsersString, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
  91. {
  92. $commissions = 0;
  93. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  94. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  95. WHERE f.fk_user_closing IN ({$allUsersString})
  96. AND EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND ";
  97. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  98. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  99. }
  100. $sql .= " multicurrency_code = '{$currency}' AND
  101. date_closing BETWEEN '{$from}' AND '{$to}'
  102. --AND fe.marked_for_storno IS NULL
  103. AND fe.commission_deduction is NULL
  104. AND (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing IN ({$allUsersString})) Is NULL";
  105. //print $sql.'<br>';
  106. $result = $this->db->query($sql);
  107. while ($row = pg_fetch_assoc($result)) {
  108. $commissions += $this->getAmountOfCommission($row);
  109. }
  110. return $commissions;
  111. }
  112. function getCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
  113. {
  114. global $db;
  115. $commissions = 0;
  116. $sql = "SELECT f.rowid, f.total_ttc, fe.commission
  117. FROM llx_facture as f
  118. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  119. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  120. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  121. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  122. }
  123. $sql .= "
  124. fk_user_closing = {$user_id}
  125. AND multicurrency_code = '{$currency}'
  126. --AND fe.marked_for_storno IS NULL
  127. AND fe.commission_deduction is NULL
  128. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  129. //print $sql;
  130. $result = $db->query($sql);
  131. if ($db->num_rows($result) != 0) {
  132. while ($row = pg_fetch_assoc($result)) {
  133. $commissions += $this->getAmountOfCommission($row);
  134. }
  135. }
  136. return $commissions;
  137. }
  138. function getCashCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $entity, $code)
  139. {
  140. $commissions = 0;
  141. //$from = date('Y-m-d H:i:s', strtotime("-1 day", strtotime($from)));
  142. //print $from;exit;
  143. $to = date('Y-m-d H:i:s', dol_now());
  144. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  145. FROM llx_facture as f
  146. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  147. WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
  148. if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
  149. $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
  150. }
  151. $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
  152. fk_user_closing = {$user_id}
  153. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  154. AND multicurrency_code = '{$currency}'
  155. --AND fe.marked_for_storno is NuLL
  156. AND fe.commission_deduction is NULL
  157. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  158. //print $sql . '<br>';
  159. $result = $this->db->query($sql);
  160. while ($row = pg_fetch_assoc($result)) {
  161. $commissions += $row['multicurrency_total_ttc'];
  162. }
  163. return $commissions;
  164. }
  165. function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
  166. {
  167. global $db;
  168. $from = $this->calculateFromDate($id);
  169. $to = $this->calculateToDate($user_id, $from);
  170. $commissions = 0;
  171. $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
  172. FROM llx_facture as f
  173. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  174. WHERE 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 .= " (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing = {$user_id}) Is NULL AND
  179. fk_user_closing = {$user_id}
  180. AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
  181. AND multicurrency_code = '{$currency}'
  182. AND date_closing BETWEEN '{$from}' AND '{$to}'";
  183. //print $sql . '<br>';
  184. $result = $db->query($sql);
  185. while ($row = pg_fetch_assoc($result)) {
  186. $commissions += $row['multicurrency_total_ttc'];
  187. }
  188. return $commissions;
  189. }
  190. public function calculateFromDate($id)
  191. {
  192. global $db;
  193. $packageHistoryObj = new PackageHistory($db);
  194. $packageHistoryObj->fetch($id);
  195. return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
  196. }
  197. public function calculateToDate($user_id, $from)
  198. {
  199. global $db;
  200. $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
  201. WHERE user_id = {$user_id}
  202. AND date_creation > '{$from}'
  203. ORDER BY rowid DESC";
  204. $result1 = $db->query($sql1);
  205. while ($row1 = pg_fetch_assoc($result1)) {
  206. $toDate = $row1['date_creation'];
  207. }
  208. $now = dol_now();
  209. return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
  210. }
  211. public function getAmountOfCommission($row)
  212. {
  213. $total_ttc = $row['total_ttc'];
  214. $commission = $row['commission'];
  215. if (isset($commission) && $commission != '_' && !is_null($commission)) {
  216. $array = explode('_', $commission);
  217. $number = $array[0];
  218. $type = $array[1];
  219. if ($type !== '%') {
  220. return $number;
  221. } else {
  222. //print number_format($number / 100, 4, '.', '');
  223. return $total_ttc * number_format($number / 100, 4, '.', '');
  224. }
  225. } else {
  226. return 0;
  227. }
  228. }
  229. function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
  230. {
  231. $commissions = 0;
  232. $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
  233. INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
  234. WHERE
  235. fk_user_closing = {$user_id} AND
  236. multicurrency_code = 'HUF' AND
  237. date_closing BETWEEN '{$from}' AND '{$to}' ";
  238. $result = $this->db->query($sql);
  239. while ($row = pg_fetch_assoc($result)) {
  240. $commissions += $this->getAmountOfCommission($row);
  241. }
  242. return $commissions;
  243. }
  244. function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
  245. {
  246. global $db;
  247. $commissions = 0;
  248. $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
  249. WHERE user_id = {$user_id}
  250. AND payment_type = 2";
  251. if (!is_null($year) && !is_null($month)) {
  252. $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
  253. }
  254. $result = $db->query($sql);
  255. while ($row = pg_fetch_assoc($result)) {
  256. return $row['sum'];
  257. }
  258. }
  259. function getUserMinimumCommission($user_id)
  260. {
  261. global $db;
  262. $userObj = new User($db);
  263. $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}";
  264. $result = $db->query($sql);
  265. while($row = $db->fetch_object($result))
  266. {
  267. return $row->minimum_monthly_commission;
  268. }
  269. }
  270. function getTotalCommission($user_id, $from, $to)
  271. {
  272. global $db;
  273. $helper = new HelperUserInvoice($db);
  274. $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
  275. $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  276. $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
  277. $fullCommission = $commissionHUF + $commissionEUR;
  278. return $fullCommission;
  279. }
  280. function getTotalCommissionFromThisPeriod($user_id, $from, $to)
  281. {
  282. global $db;
  283. $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
  284. $array = [];
  285. $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}'";
  286. $data = $db->query($sql);
  287. if(pg_num_rows($data) > 0){
  288. while ($row = pg_fetch_assoc($data)) {
  289. $array[] = $row;
  290. }
  291. }
  292. return $array;
  293. }
  294. function getDailyMinimumCommission()
  295. {
  296. global $db;
  297. $date = date('Y-m-d H:i:s', dol_now());
  298. $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
  299. INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
  300. WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
  301. //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
  302. $result = $db->query($sql);
  303. if(pg_num_rows($result) > 0){
  304. while ($row = pg_fetch_assoc($result)) {
  305. return $row['amount'];
  306. }
  307. }
  308. return 0;
  309. }
  310. }