| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
- class CommissionHandler
- {
- public function __construct()
- {
- global $db, $user;
- $this->db = $db;
- $this->user = $user;
- }
- function getCommissionFromProduct($id)
- {
- $remiseSUM = 0;
- $commission = 0;
- $commission_type = '';
- $sqlRemise = "SELECT fdet.remise_percent FROM llx_facture as f
- INNER JOIN llx_facturedet as fdet ON fdet.fk_facture = f.rowid
- WHERE f.rowid = {$id}";
- $resultRemise = $this->db->query($sqlRemise);
- while ($remise = pg_fetch_assoc($resultRemise)) {
- $remiseSUM += $remise['remise_percent'];
- }
- $sql = "SELECT fk_product FROM public.llx_facturedet WHERE fk_facture = {$id}";
- $result = $this->db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $product = new Product($this->db);
- $res = $product->fetch($row['fk_product']);
- if ($res > 0) {
- if($remiseSUM > 0 && $product->array_options['options_reduced_commission'] > 0){
- $commission_type = $this->getCommissionType($product->array_options['options_reduced_commission_type']);
- if($commission_type != '%'){
- $commission += $product->array_options['options_reduced_commission'];
- }else{
- $commission = $product->array_options['options_reduced_commission'];
- }
- }else{
- $commission_type = $this->getCommissionType($product->array_options['options_commission_type']);
- if($commission_type != '%'){
- $commission += $product->array_options['options_commission_value'];
- }else{
- $commission = $product->array_options['options_commission_value'];
- }
-
- }
- }
- }
- return number_format($commission, 8, '.', '') . '_' . $commission_type;
- }
- private function getCommissionType($type)
- {
- switch ($type) {
- case '1':
- return '%';
- break;
- case '2':
- return 'Ft';
- break;
- case '3':
- return '€';
- break;
- }
- }
- function updateCommission($object)
- {
- global $user, $db;
- $commission = $this->getCommissionFromProduct($object->id);
- $facture = new Facture($db);
- $result = $facture->fetch($object->id);
- if ($result > 0) {
- $facture->array_options['options_commission'] = $commission;
- $facture->update($user);
- }
- //$objectFacture->commission = $commission;
- //$objectFacture->update($user);
- }
- function getAllCommissionByUserId($user, $date, $currency)
- {
- $result = 0;
- $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE
- fk_user_closing = {$user['user_id']} AND
- multicurrency_code = '{$currency}' AND
- date_closing < '{$date} 00:00:00'";
- $result = $this->db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $this->getAmountOfCommission($row);
- }
- return round($commissions, 3);
- }
- function getAllCommission($allUsersString, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
- {
- $commissions = 0;
- $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE f.fk_user_closing IN ({$allUsersString})
- AND EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND ";
- if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
- $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
- }
- $sql .= " multicurrency_code = '{$currency}' AND
- date_closing BETWEEN '{$from}' AND '{$to}'
- --AND fe.marked_for_storno IS NULL
- AND fe.commission_deduction is NULL
- AND (SELECT a.rowid FROM llx_facture as a WHERE a.fk_facture_source = f.rowid AND fk_user_closing IN ({$allUsersString})) Is NULL";
- //print $sql.'<br>';
- $result = $this->db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $this->getAmountOfCommission($row);
- }
- return $commissions;
- }
- function getCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice)
- {
- global $db;
- $commissions = 0;
- $sql = "SELECT f.rowid, f.total_ttc, fe.commission
- FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
- if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
- $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
- }
- $sql .= "
- fk_user_closing = {$user_id}
- AND multicurrency_code = '{$currency}'
- --AND fe.marked_for_storno IS NULL
- AND fe.commission_deduction is NULL
- AND date_closing BETWEEN '{$from}' AND '{$to}'";
- //print $sql;
- $result = $db->query($sql);
- if ($db->num_rows($result) != 0) {
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $this->getAmountOfCommission($row);
- }
- }
- return $commissions;
- }
- function getCashCommisonByUserId($user_id, $currency, $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $entity, $code)
- {
- $commissions = 0;
- //$from = date('Y-m-d H:i:s', strtotime("-1 day", strtotime($from)));
- //print $from;exit;
- $to = date('Y-m-d H:i:s', dol_now());
- $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
- FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
- if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
- $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
- }
- $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
- fk_user_closing = {$user_id}
- AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
- AND multicurrency_code = '{$currency}'
- --AND fe.marked_for_storno is NuLL
- AND fe.commission_deduction is NULL
- AND date_closing BETWEEN '{$from}' AND '{$to}'";
- //print $sql . '<br>';
- $result = $this->db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $row['multicurrency_total_ttc'];
- }
- return $commissions;
- }
- function getCashCommisonByUserIdHistory($user_id, $currency, $allComissionInvoicesOfUserInTheGroupFromUserInvoice, $id, $entity, $code)
- {
- global $db;
- $from = $this->calculateFromDate($id);
- $to = $this->calculateToDate($user_id, $from);
- $commissions = 0;
- $sql = "SELECT f.rowid, f.multicurrency_total_ttc, fe.commission
- FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE EXISTS (SELECT * FROM llx_bbus_bbticketinvoiceprinting as bbip WHERE bbip.fk_facture = f.rowid ORDER BY bbip.rowid DESC LIMIT 1) AND";
- if (!empty($allComissionInvoicesOfUserInTheGroupFromUserInvoice)) {
- $sql .= " f.rowid NOT IN({$allComissionInvoicesOfUserInTheGroupFromUserInvoice}) AND";
- }
- $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
- fk_user_closing = {$user_id}
- AND fk_mode_reglement = (SELECT id FROM public.llx_c_paiement WHERE entity = {$entity} AND code = '{$code}')
- AND multicurrency_code = '{$currency}'
- AND date_closing BETWEEN '{$from}' AND '{$to}'";
- //print $sql . '<br>';
- $result = $db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $row['multicurrency_total_ttc'];
- }
- return $commissions;
- }
- public function calculateFromDate($id)
- {
- global $db;
- $packageHistoryObj = new PackageHistory($db);
- $packageHistoryObj->fetch($id);
- return date('Y-m-d H:i:s', $packageHistoryObj->date_creation);
- }
- public function calculateToDate($user_id, $from)
- {
- global $db;
- $sql1 = "SELECT date_creation FROM public.llx_rollerstorage_packagehistory
- WHERE user_id = {$user_id}
- AND date_creation > '{$from}'
- ORDER BY rowid DESC";
- $result1 = $db->query($sql1);
- while ($row1 = pg_fetch_assoc($result1)) {
- $toDate = $row1['date_creation'];
- }
- $now = dol_now();
- return is_null($toDate) ? date('Y-m-d H:i:s', $now) : $toDate;
- }
- public function getAmountOfCommission($row)
- {
- $total_ttc = $row['total_ttc'];
- $commission = $row['commission'];
- if (isset($commission) && $commission != '_' && !is_null($commission)) {
- $array = explode('_', $commission);
- $number = $array[0];
- $type = $array[1];
- if ($type !== '%') {
- return $number;
- } else {
- //print number_format($number / 100, 4, '.', '');
- return $total_ttc * number_format($number / 100, 4, '.', '');
- }
- } else {
- return 0;
- }
- }
- function getOccassionOfThisPeriodByDateAndUserId_HUF($user_id, $from, $to)
- {
- $commissions = 0;
- $sql = "SELECT f.rowid, f.total_ttc, fe.commission FROM llx_facture as f
- INNER JOIN llx_facture_extrafields as fe ON fe.fk_object = f.rowid
- WHERE
- fk_user_closing = {$user_id} AND
- multicurrency_code = 'HUF' AND
- date_closing BETWEEN '{$from}' AND '{$to}' ";
- $result = $this->db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- $commissions += $this->getAmountOfCommission($row);
- }
- return $commissions;
- }
- function getCommisonByUserIdForAMonth($user_id, $year = null, $month = null)
- {
- global $db;
- $commissions = 0;
- $sql = "SELECT sum(amount) FROM llx_financialreport_userinvoice
- WHERE user_id = {$user_id}
- AND payment_type = 2";
- if (!is_null($year) && !is_null($month)) {
- $sql .= " AND DATE_PART('year', date_creation) = {$year} AND DATE_PART('month', date_creation) = {$month}";
- }
- $result = $db->query($sql);
- while ($row = pg_fetch_assoc($result)) {
- return $row['sum'];
- }
- }
- function getUserMinimumCommission($user_id)
- {
- global $db;
- $userObj = new User($db);
- $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}";
- $result = $db->query($sql);
- while($row = $db->fetch_object($result))
- {
- return $row->minimum_monthly_commission;
- }
- }
- function getTotalCommission($user_id, $from, $to)
- {
- global $db;
- $helper = new HelperUserInvoice($db);
- $allComissionInvoicesOfUserInTheGroupFromUserInvoice = $helper->getAllComissionInvoicesOfUserInTheGroupFromUserInvoice($user_id, $from, date("Y-m-d H:i:s", dol_now()));
- $commissionHUF = $this->getCommisonByUserId($user_id, 'HUF', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
- $commissionEUR = $this->getCommisonByUserId($user_id, 'EUR', $from, $to, $allComissionInvoicesOfUserInTheGroupFromUserInvoice);
- $fullCommission = $commissionHUF + $commissionEUR;
- return $fullCommission;
- }
- function getTotalCommissionFromThisPeriod($user_id, $from, $to)
- {
- global $db;
- $from = date("Y-m-d H:i:s", strtotime('-1 hour', strtotime($from)));
- $array = [];
- $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}'";
- $data = $db->query($sql);
- if(pg_num_rows($data) > 0){
- while ($row = pg_fetch_assoc($data)) {
- $array[] = $row;
- }
- }
- return $array;
- }
- function getDailyMinimumCommission()
- {
- global $db;
- $date = date('Y-m-d H:i:s', dol_now());
- $sql = "SELECT option.amount FROM public.llx_financialreport_commissioninterval AS interval
- INNER JOIN llx_financialreport_commissionoptions as option ON option.rowid = interval.option_id
- WHERE '{$date}' BETWEEN interval.interval_start AND interval.interval_end";
- //WHERE '2023-01-01 10:10:10' BETWEEN interval.interval_start AND interval.interval_end";
- $result = $db->query($sql);
- if(pg_num_rows($result) > 0){
- while ($row = pg_fetch_assoc($result)) {
- return $row['amount'];
- }
- }
- return 0;
- }
- }
|