clearCache(); } function clearCache() { $this->commissions_configuration = array(); $this->commissions_configuration_cached = false; } function findCommissionAmount($orders_id, &$description, $commercials_id=NULL, $customers_id=NULL) { if ( is_null($customers_id) or $customers_id == "" ) $customers_id = $this->findCustomerId($orders_id); if ( is_null($commercials_id) or $commercials_id == "" ) $commercials_id = $this->findCommercialId($orders_id); $query = tep_db_query("select value from " . TABLE_COMMISSIONS_CONFIGURATION . " where commercials_id=" . $commercials_id . " AND customers_id IS NULL or customers_id=" . $customers_id . " order by customers_id DESC"); $commission_configuration = tep_db_fetch_array($query); $amount = trim($commission_configuration['value']); if (substr($amount, -1) == '%') { $amount_txt = $amount; $amount = substr($amount, 0, -1); if ( $orders_id != "" ) { $query = tep_db_query("select value from " . TABLE_ORDERS_TOTAL . " where orders_id= " . $orders_id . " AND class = 'ot_total'"); $amount_ht = tep_db_fetch_array($query); $amount_ht = $amount_ht['value']; $amount = $amount / 100.0 * $amount_ht; $description = $amount_txt . " de " . $amount_ht . "HT (=" . $amount . ") pour " . $this->findCustomerName($customers_id); } else { $amount = 0; } } else { $description = "montant fixe de $amount pour " . $this->findCustomerName($customers_id); ; } return (double)$amount; } function findCustomerName($customers_id) { if ( $customers_id == "" ) return ""; $query = tep_db_query("select concat(customers_firstname, ' ', customers_lastname) as name from " . TABLE_CUSTOMERS . " where customers_id=" . $customers_id); $name = tep_db_fetch_array($query); return $name['name']; } function findCommissionId($orders_id) { if ( $orders_id == "" ) return ""; $query = tep_db_query("select id from " . TABLE_COMMISSIONS . " where orders_id=" . $orders_id); $id = tep_db_fetch_array($query); return $id['id']; } function findCustomerId($orders_id) { $query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id=" . $orders_id); $customers_id = tep_db_fetch_array($query); return $customers_id['customers_id']; } function findCommercialId($orders_id) { $customers_id = $this->findCustomerId($orders_id); if ( $customers_id == "" ) return ""; $query = tep_db_query("select commercials_id from " . TABLE_CUSTOMERS . " where customers_id=" . $customers_id); $commercials_id = tep_db_fetch_array($query); return $commercials_id['commercials_id']; } function createCommissions($orders_id, $commercials_id=NULL, $customers_id=NULL, $amount=NULL, $status=NULL, $comments=NULL) { // customers_id n'est utilisé que si amount n'est pas fourni if ( is_null($commercials_id) or $commercials_id == "" ) $commercials_id = $this->findCommercialId($orders_id); if ( $commercials_id == "" ) return -1; // aucun commercial pour la commande donnée, création de la comission impossible if ( is_null($amount) or $amount == "" ) { $amount = $this->findCommissionAmount($orders_id, $description, $commercials_id, $customers_id); } if ( is_null($status) or status == "" ) $status = 'C'; if ( (int)$amount > 0 ) { return tep_db_perform(TABLE_COMMISSIONS, array( 'commercials_id' => $commercials_id, 'orders_id' => $orders_id, 'amount' => $amount, 'date_created' => 'now()', 'date_modified' => 'now()', 'status' => $status, 'date_status' => 'now()', 'comments' => $comments, ) ); } return -1; } function countCommissions($commercials_id) { $check_query = tep_db_query("select count(*) as total from " . TABLE_COMMISSIONS . " where commercials_id = '" . (int)$commercials_id . "'"); $check = tep_db_fetch_array($check_query); return $check['total']; } function retrieveCommercialCommissions($commercials_id) { $check_query = tep_db_query("select * from " . TABLE_COMMISSIONS . " where commercials_id = '" . (int)$commercials_id . "'"); $check = tep_db_fetch_array($check_query); return $check['total']; } function retrieveCommission($id) { if ( is_null($id) or $id == "" ) return array(); // pas de numéro fourni ? renvoi un tableau vide $query = tep_db_query("select * from " . TABLE_COMMISSIONS. " where id=" . $id); $ret = tep_db_fetch_array($query); return $ret; } function deleteCommission($id) { return tep_db_query("DELETE from " . TABLE_COMMISSIONS. " where id=" . $id); } function updateCommission($id, $commercials_id=NULL, $amount=NULL, $status=NULL, $comments=NULL) { $data = array(); if ( ! is_null($commercials_id) ) { $data['commercials_id'] = tep_db_input($commercials_id); } if ( ! is_null($amount) ) { $data['amount'] = tep_db_input($amount); } if ( ! is_null($status) ) { $data['status'] = tep_db_input($status); $data['date_status'] = 'now()'; } if ( ! is_null($comments) ) { $data['comments'] = str_replace('
','', nl2br($comments)); } if ( sizeof($data) > 0 ) { $data['date_modified'] = 'now()'; return tep_db_perform(TABLE_COMMISSIONS, $data, "update", "id=" . $id); } } function retrieveCommissionsStatusesAsDropDown() { $ret = array(); foreach ( $this->getAllCommissionStatusDescription() as $id => $desc ) { $ret[] = array("id" => $id, "text" => $desc); } return $ret; } function getAllCommissionStatusDescription() { $ret = array( "C" => "créée", "A" => "annulée", "R" => "réglée", ); return $ret; } function getCommissionStatusDescription($id) { $ret = $this->getAllCommissionStatusDescription(); return $ret[$id]; } function retrieveCommissionsConfiguration($commercials_id) { $this->clearCache(); $query = tep_db_query("select * from " . TABLE_COMMISSIONS_CONFIGURATION . " where commercials_id = " . $commercials_id . " order by customers_id DESC"); while ($commission = tep_db_fetch_array($query)) { $id = $commission['commercials_id']; if ( is_null($this->commissions_configuration[$id]) ) $this->commissions_configuration[$id] = array(); $this->commissions_configuration[$id][] = $commission; } $this->commissions_configuration_cached = true; return $this->commissions_configuration; } function deleteCommissionsConfiguration($id) { return tep_db_query("DELETE from " . TABLE_COMMISSIONS_CONFIGURATION . " where id = " . $id); } function insertCommissionsConfiguration($commercials_id, $customers_id, $value) { if ( trim($customers_id) == "" ) $customers_id = "null"; $data = array( "commercials_id" => $commercials_id, "customers_id" => $customers_id, "date_modified" => 'now()', "value" => $value, ); return tep_db_perform(TABLE_COMMISSIONS_CONFIGURATION, $data); } function updateCommissionsConfiguration($id, $commercials_id, $customers_id, $value) { if ( trim($customers_id) == "" ) $customers_id = "null"; $data = array( "commercials_id" => $commercials_id, "customers_id" => $customers_id, "date_modified" => 'now()', "value" => $value, ); return tep_db_perform(TABLE_COMMISSIONS_CONFIGURATION, $data, "update", "id=" . $id); } function retrieveAllCommercialsAsDropDown() { $query = tep_db_query("select customers_id, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where is_commercial = 1"); $ret = array(); $ret[] = array("id" => "", "text" => "Aucun"); while ($data = tep_db_fetch_array($query)) { $ret[] = array("id" => $data['customers_id'], "text" => $data['customers_firstname'] . " " . $data['customers_lastname']); } return $ret; } function retrieveAllCommercialCustomersAsDropDown($cid, $extra_id=NULL) { $where=""; if ( ! is_null($extra_id) ) $where = " or customers_id = $extra_id"; $query = tep_db_query("select customers_id, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where commercials_id = $cid" . $where); $ret = array(); $ret[] = array("id" => "", "text" => "Autre"); while ($data = tep_db_fetch_array($query)) { $ret[] = array("id" => $data['customers_id'], "text" => $data['customers_firstname'] . " " . $data['customers_lastname']); } return $ret; } } ?>