* Copyright (C) 2004-2015 Laurent Destailleur * Copyright (C) 2005 Eric Seigne * Copyright (C) 2005-2013 Regis Houssin * Copyright (C) 2006 Andre Cianfarani * Copyright (C) 2015 Marcos GarcĂ­a * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/societe/price.php * \ingroup product * \brief Page to show product prices by customer */ // Load Dolibarr environment require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php'; $prodcustprice = new Productcustomerprice($db); } // Load translation files required by the page $langs->loadLangs(array("products", "companies", "bills")); // Get parameters $action = GETPOST('action', 'aZ09'); $search_prod = GETPOST('search_prod', 'alpha'); $cancel = GETPOST('cancel', 'alpha'); $search_label = GETPOST('search_label', 'alpha'); $search_price = GETPOST('search_price'); $search_price_ttc = GETPOST('search_price_ttc'); // Security check $socid = GETPOST('socid', 'int') ?GETPOST('socid', 'int') : GETPOST('id', 'int'); if ($user->socid) { $socid = $user->socid; } $result = restrictedArea($user, 'societe', $socid, '&societe'); // Initialize objects $object = new Societe($db); // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager->initHooks(array('thirdpartycustomerprice', 'globalcard')); $error = 0; /* * Actions */ $parameters = array('id'=>$socid); $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks if ($reshook < 0) { setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); } if (empty($reshook)) { if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers $search_prod = $search_label = $search_price = $search_price_ttc = ''; } if ($action == 'add_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) { if (!(GETPOST('prodid', 'int') > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("Product")), null, 'errors'); $action = 'add_customer_price'; } if (!$error) { $update_child_soc = GETPOST('updatechildprice'); // add price by customer $prodcustprice->fk_soc = $socid; $prodcustprice->ref_customer = GETPOST('ref_customer', 'alpha'); $prodcustprice->fk_product = GETPOST('prodid', 'int'); $prodcustprice->price = price2num(GETPOST("price"), 'MU'); $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)' // We must define tva_tx, npr and local taxes $vatratecode = ''; $tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0'; // If value contains the unique code of vat line (new recommended method), we use it to find npr and local taxes if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg)) { // We look into database using code (we can't use get_localtax() because it depends on buyer that is not known). Same in update price. $vatratecode = $reg[1]; // Get record from code $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type"; $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'"; $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1"; $sql .= " AND t.code ='".$db->escape($vatratecode)."'"; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); $npr = $obj->recuperableonly; $localtax1 = $obj->localtax1; $localtax2 = $obj->localtax2; $localtax1_type = $obj->localtax1_type; $localtax2_type = $obj->localtax2_type; } } $prodcustprice->default_vat_code = $vatratecode; $prodcustprice->tva_tx = $tva_tx; $prodcustprice->recuperableonly = $npr; $prodcustprice->localtax1_tx = $localtax1; $prodcustprice->localtax2_tx = $localtax2; $prodcustprice->localtax1_type = $localtax1_type; $prodcustprice->localtax2_type = $localtax2_type; $result = $prodcustprice->create($user, 0, $update_child_soc); if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); } else { setEventMessages($langs->trans("Save"), null, 'mesgs'); } $action = ''; } } if ($action == 'delete_customer_price' && ($user->rights->produit->creer || $user->rights->service->creer)) { // Delete price by customer $prodcustprice->id = GETPOST('lineid', 'int'); $result = $prodcustprice->delete($user); if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'mesgs'); } else { setEventMessages($langs->trans('RecordDeleted'), null, 'errors'); } $action = ''; } if ($action == 'update_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) { $prodcustprice->fetch(GETPOST('lineid', 'int')); $update_child_soc = GETPOST('updatechildprice'); // update price by customer $prodcustprice->ref_customer = GETPOST('ref_customer', 'alpha'); $prodcustprice->price = price2num(GETPOST("price"), 'MU'); $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $prodcustprice->tva_tx = str_replace('*', '', GETPOST("tva_tx")); $prodcustprice->recuperableonly = (preg_match('/\*/', GETPOST("tva_tx")) ? 1 : 0); $result = $prodcustprice->update($user, 0, $update_child_soc); if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); } else { setEventMessages($langs->trans("Save"), null, 'mesgs'); } $action = ''; } } /* * View */ $form = new Form($db); $object = new Societe($db); $result = $object->fetch($socid); llxHeader("", $langs->trans("ThirdParty").'-'.$langs->trans('PriceByCustomer')); if (isModEnabled('notification')) { $langs->load("mails"); } $head = societe_prepare_head($object); print dol_get_fiche_head($head, 'price', $langs->trans("ThirdParty"), -1, 'company'); $linkback = ''.$langs->trans("BackToList").''; dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom'); print '
'; print '
'; print ''; // Type Prospect/Customer/Supplier print ''; if (!empty($conf->global->SOCIETE_USEPREFIX)) { // Old not used prefix field print ''; } if ($object->client) { print ''; } if ($object->fournisseur) { print ''; } print '
'.$langs->trans('NatureOfThirdParty').''; print $object->getTypeUrl(1); print '
'.$langs->trans('Prefix').''.$object->prefix_comm.'
'; print $langs->trans('CustomerCode').''; print $object->code_client; $tmpcheck = $object->check_codeclient(); if ($tmpcheck != 0 && $tmpcheck != -5) { print ' ('.$langs->trans("WrongCustomerCode").')'; } print '
'; print $langs->trans('SupplierCode').''; print $object->code_fournisseur; $tmpcheck = $object->check_codefournisseur(); if ($tmpcheck != 0 && $tmpcheck != -5) { print ' ('.$langs->trans("WrongSupplierCode").')'; } print '
'; print '
'; print dol_get_fiche_end(); if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { $prodcustprice = new Productcustomerprice($db); $sortfield = GETPOST('sortfield', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma'); $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit; $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1 $offset = $limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) { $sortorder = "ASC"; } if (!$sortfield) { $sortfield = "soc.nom"; } // Build filter to display only concerned lines $filter = array( 't.fk_soc' => $object->id ); if (!empty($search_prod)) { $filter ['prod.ref'] = $search_prod; } if (!empty($search_label)) { $filter ['prod.label'] = $search_label; } if (!empty($search_price)) { $filter ['t.price'] = $search_price; } if (!empty($search_price_ttc)) { $filter ['t.price_ttc'] = $search_price_ttc; } if ($action == 'add_customer_price') { // Create mode print '
'; print ''."\n"; print load_fiche_titre($langs->trans('PriceByCustomer')); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; // Ref. Customer print ''; print ''; // VAT print ''; // Price base print ''; print ''; print ''; // Price print ''; // Price minimum print ''; // Update all child soc print ''; print ''; print ''; print '
'.$langs->trans('Product').''; $form->select_produits('', 'prodid', '', 0); print '
'.$langs->trans('RefCustomer').'
'.$langs->trans("VATRate").''; print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, '', false, 1); print '
'; print $langs->trans('PriceBase'); print ''; print $form->selectPriceBaseType($object->price_base_type, "price_base_type"); print '
'; $text = $langs->trans('SellingPrice'); print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); print ''; print ''; print '
'; $text = $langs->trans('MinPrice'); print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); print ''; print '
'; print $langs->trans('ForceUpdateChildPriceSoc'); print ''; print ''; print '
'; print $form->buttonsSaveCancel(); print '
'; } elseif ($action == 'edit_customer_price') { // Edit mode print load_fiche_titre($langs->trans('PriceByCustomer')); $result = $prodcustprice->fetch(GETPOST('lineid', 'int')); if ($result <= 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); } else { print '
'; print ''; print ''; print ''; print ''; print ''; print ''; $staticprod = new Product($db); $staticprod->fetch($prodcustprice->fk_product); print ""; print ''; // Ref. Customer print ''; print ''; // VAT print ''; // Price base print ''; print ''; print ''; // Price print ''; // Price minimum print ''; // Update all child soc print ''; print ''; print ''; print '
'.$langs->trans('Product').'".$staticprod->getNomUrl(1)."
'.$langs->trans('RefCustomer').'
'.$langs->trans("VATRate").''; print $form->load_tva("tva_tx", $prodcustprice->tva_tx, $mysoc, '', $staticprod->id, $prodcustprice->recuperableonly); print '
'; print $langs->trans('PriceBase'); print ''; print $form->selectPriceBaseType($prodcustprice->price_base_type, "price_base_type"); print '
'; $text = $langs->trans('SellingPrice'); print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); print ''; if ($prodcustprice->price_base_type == 'TTC') { print ''; } else { print ''; } print '
'; $text = $langs->trans('MinPrice'); print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); print ''; if ($prodcustprice->price_base_type == 'TTC') { print ''; } else { print ''; } print '
'; print $langs->trans('ForceUpdateChildPriceSoc'); print ''; print ''; print '
'; print $form->buttonsSaveCancel(); print '
'; } } elseif ($action == 'showlog_customer_price') { print '
'; print ''."\n"; $filter = array( 't.fk_product' => GETPOST('prodid', 'int'), 't.fk_soc' => $socid ); // Count total nb of records $nbtotalofrecords = ''; $result = $prodcustprice->fetch_all_log($sortorder, $sortfield, $conf->liste_limit, $offset, $filter); if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); } else { if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { $nbtotalofrecords = $result; } } $option = '&socid='.GETPOST('socid', 'int').'&prodid='.GETPOST('prodid', 'int'); print_barre_liste($langs->trans('PriceByCustomerLog'), $page, $_SERVER ['PHP_SELF'], $option, $sortfield, $sortorder, '', count($prodcustprice->lines), $nbtotalofrecords); if (count($prodcustprice->lines) > 0) { print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; foreach ($prodcustprice->lines as $line) { $staticprod = new Product($db); $staticprod->fetch($line->fk_product); $userstatic = new User($db); $userstatic->fetch($line->fk_user); print ''; print ""; print ''; print ""; print '"; print '"; print '"; print '"; print ''; print ''; // User print ''; print ''; } print "
'.$langs->trans("Product").''.$langs->trans('RefCustomer').''.$langs->trans("AppliedPricesFrom").''.$langs->trans("PriceBase").''.$langs->trans("VAT").''.$langs->trans("HT").''.$langs->trans("TTC").''.$langs->trans("MinPrice").' '.$langs->trans("HT").''.$langs->trans("MinPrice").' '.$langs->trans("TTC").''.$langs->trans("ChangedBy").'
".$staticprod->getNomUrl(1)."'.$line->ref_customer.'".dol_print_date($line->datec, "dayhour")."'.$langs->trans($line->price_base_type)."'.vatrate($line->tva_tx, true, $line->recuperableonly)."'.price($line->price)."'.price($line->price_ttc)."'.price($line->price_min).''.price($line->price_min_ttc).''; print $userstatic->getNomUrl(-1); print '
"; } else { print $langs->trans('None'); } print "\n".'
'."\n"; print ''; print "\n

\n"; } else { // View mode /* * Action bar */ print "\n".'
'."\n"; if ($user->rights->produit->creer || $user->rights->service->creer) { print ''; } print "\n
\n"; // Count total nb of records $nbtotalofrecords = ''; if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { $nbtotalofrecords = $prodcustprice->fetchAll('', '', 0, 0, $filter); } $result = $prodcustprice->fetchAll($sortorder, $sortfield, $conf->liste_limit, $offset, $filter); if ($result < 0) { setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors'); } $option = '&search_prod='.$search_prod.'&id='.$object->id.'&label='.$search_label.'&price='.$search_price.'&price_ttc='.$search_price_ttc; print ''."\n"; print_barre_liste($langs->trans('PriceForEachProduct'), $page, $_SERVER['PHP_SELF'], $option, $sortfield, $sortorder, '', count($prodcustprice->lines), $nbtotalofrecords, ''); print ''; print ''; print ''; print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if (count($prodcustprice->lines) > 0 || $search_prod) { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; // Print the search button print ''; print ''; } if (count($prodcustprice->lines) > 0) { foreach ($prodcustprice->lines as $line) { $staticprod = new Product($db); $staticprod->fetch($line->fk_product); $userstatic = new User($db); $userstatic->fetch($line->fk_user); print ''; print ""; print ""; print ''; print ""; print '"; print '"; print '"; print '"; print ''; print ''; // User print ''; // Action if ($user->rights->produit->creer || $user->rights->service->creer) { print ''; } print "\n"; } } else { $colspan = 10; if ($user->rights->produit->supprimer || $user->rights->service->supprimer) { $colspan += 1; } print ''; } print "
'.$langs->trans("Ref").''.$langs->trans("Product").''.$langs->trans('RefCustomer').''.$langs->trans("AppliedPricesFrom").''.$langs->trans("PriceBase").''.$langs->trans("VAT").''.$langs->trans("HT").''.$langs->trans("TTC").''.$langs->trans("MinPrice").' '.$langs->trans("HT").''.$langs->trans("MinPrice").' '.$langs->trans("TTC").''.$langs->trans("ChangedBy").'
'; $searchpicto = $form->showFilterAndCheckAddButtons(0); print $searchpicto; print '
".$staticprod->getNomUrl(1)."".$staticprod->label."'.$line->ref_customer.'".dol_print_date($line->datec, "dayhour")."'.$langs->trans($line->price_base_type)."'.vatrate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), true, $line->recuperableonly)."'.price($line->price)."'.price($line->price_ttc)."'.price($line->price_min).''.price($line->price_min_ttc).''; print $userstatic->getNomUrl(-1); print ''; print 'id.'&prodid='.$line->fk_product.'">'; print img_info(); print ''; print ' '; print 'id.'&lineid='.$line->id.'">'; print img_edit('default', 0, 'style="vertical-align: middle;"'); print ''; print ' '; print 'id.'&lineid='.$line->id.'">'; print img_delete('default', 'style="vertical-align: middle;"'); print ''; print '
'.$langs->trans('None').'
"; print '
'; print "
"; } } // End of page llxFooter(); $db->close();