* Copyright (C) 2004-2013 Laurent Destailleur * Copyright (C) 2004 Eric Seigne * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2012 Juanjo Menent * Copyright (C) 2012 Christophe Battarel * Copyright (C) 2014 Ion Agorria * Copyright (C) 2015 Alexandre Spangaro * * 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/product/fournisseurs.php * \ingroup product * \brief Page of tab suppliers for products */ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php'; $langs->load("products"); $langs->load("suppliers"); $langs->load("bills"); if (! empty($conf->margin->enabled)) $langs->load("margins"); $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alpha'); $rowid=GETPOST('rowid','int'); $action=GETPOST('action', 'alpha'); $cancel=GETPOST('cancel', 'alpha'); $socid=GETPOST('socid', 'int'); $backtopage=GETPOST('backtopage','alpha'); $error=0; // If socid provided by ajax company selector if (! empty($_REQUEST['search_fourn_id'])) { $_GET['id_fourn'] = $_GET['search_fourn_id']; $_POST['id_fourn'] = $_POST['search_fourn_id']; $_REQUEST['id_fourn'] = $_REQUEST['search_fourn_id']; } // Security check $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : '')); $fieldtype = (! empty($ref) ? 'ref' : 'rowid'); if ($user->societe_id) $socid=$user->societe_id; $result=restrictedArea($user,'produit|service&fournisseur',$fieldvalue,'product&product','','',$fieldtype); // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array $hookmanager->initHooks(array('pricesuppliercard','globalcard')); $product = new ProductFournisseur($db); $product->fetch($id,$ref); $sortfield = GETPOST("sortfield",'alpha'); $sortorder = GETPOST("sortorder",'alpha'); if (! $sortfield) $sortfield="s.nom"; if (! $sortorder) $sortorder="ASC"; /* * Actions */ if ($cancel) $action=''; $parameters=array('socid'=>$socid, 'id_prod'=>$id); $reshook=$hookmanager->executeHooks('doActions',$parameters,$product,$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 ($action == 'remove_pf') { $product = new ProductFournisseur($db); if ($product->fetch($id) > 0) { if ($rowid) { $result=$product->remove_product_fournisseur_price($rowid); $action = ''; setEventMessage($langs->trans("PriceRemoved")); } } } if ($action == 'updateprice') { $id_fourn=GETPOST("id_fourn"); if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn"); $ref_fourn=GETPOST("ref_fourn"); if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn"); $quantity=GETPOST("qty"); $remise_percent=price2num(GETPOST('remise_percent','alpha')); $npr = preg_match('/\*/', $_POST['tva_tx']) ? 1 : 0 ; $tva_tx = str_replace('*','', GETPOST('tva_tx','alpha')); $tva_tx = price2num($tva_tx); $price_expression = GETPOST('eid', 'int') ? GETPOST('eid', 'int') : ''; // Discard expression if not in expression mode $delivery_time_days = GETPOST('delivery_time_days', 'int') ? GETPOST('delivery_time_days', 'int') : ''; if ($tva_tx == '') { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("VATRateForSupplierProduct")), 'errors'); } if (! is_numeric($tva_tx)) { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldMustBeANumeric",'eeee'), 'errors'); } if (empty($quantity)) { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")), 'errors'); } if (empty($ref_fourn)) { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")), 'errors'); } if ($id_fourn <= 0) { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")), 'errors'); } if ($_POST["price"] < 0 || $_POST["price"] == '') { if ($price_expression === '') // Return error of missing price only if price_expression not set { $error++; $langs->load("errors"); setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")), 'errors'); } else { $_POST["price"] = 0; } } $product = new ProductFournisseur($db); $result=$product->fetch($id); if ($result <= 0) { $error++; setEventMessages($product->error, $product->errors, 'errors'); } if (! $error) { $db->begin(); if (! $error) { $ret=$product->add_fournisseur($user, $id_fourn, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice if ($ret == -3) { $error++; $product->fetch($product->product_id_already_linked); $productLink = $product->getNomUrl(1,'supplier'); setEventMessage($langs->trans("ReferenceSupplierIsAlreadyAssociatedWithAProduct",$productLink), 'errors'); } else if ($ret < 0) { $error++; setEventMessage($product->error, 'errors'); } } if (! $error) { $supplier=new Fournisseur($db); $result=$supplier->fetch($id_fourn); if (isset($_POST['ref_fourn_price_id'])) $product->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']); $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"], $remise_percent, 0, $npr, $delivery_time_days); if ($ret < 0) { $error++; setEventMessage($product->error, $product->errors, 'errors'); } else { if ($price_expression !== '') { //Check the expression validity by parsing it $priceparser = new PriceParser($db); $price_result = $priceparser->parseProductSupplier($id, $price_expression, $quantity, $tva_tx); if ($price_result < 0) { //Expression is not valid $error++; setEventMessage($priceparser->translatedError(), 'errors'); } } if (! $error && ! empty($conf->dynamicprices->enabled)) { $ret=$product->setPriceExpression($price_expression); if ($ret < 0) { $error++; setEventMessage($product->error, 'errors'); } } } } if (! $error) { $db->commit(); $action=''; } else { $db->rollback(); } } else { $action = 'add_price'; } } } /* * view */ $form = new Form($db); if ($id || $ref) { if ($action <> 're-edit') { $product = new ProductFournisseur($db); $result = $product->fetch($id,$ref); //$result = $product->fetch_fourn_data($_REQUEST["id_fourn"]); llxHeader("","",$langs->trans("CardProduct".$product->type)); } if ($result) { if ($action <> 'edit' && $action <> 're-edit') { $head=product_prepare_head($product); $titre=$langs->trans("CardProduct".$product->type); $picto=($product->type== Product::TYPE_SERVICE?'service':'product'); dol_fiche_head($head, 'suppliers', $titre, 0, $picto); print ''; // Reference print ''; print ''; print ''; // Label print ''; // Minimum Price print ''; print ''; // Status (to buy) print ''; print '
'.$langs->trans("Ref").''; print $form->showrefnav($product,'ref','',1,'ref'); print '
'.$langs->trans("Label").''.$product->label.'
'.$langs->trans("BuyingPriceMin").''; $product_fourn = new ProductFournisseur($db); if ($product_fourn->find_min_price_product_fournisseur($product->id) > 0) { if ($product_fourn->product_fourn_price_id > 0) print $product_fourn->display_price_product_fournisseur(); else print $langs->trans("NotDefined"); } print '
'.$langs->trans("Status").' ('.$langs->trans("Buy").')'; print $product->getLibStatut(2,1); print '
'; print "\n"; // Form to add or update a price if (($action == 'add_price' || $action == 'updateprice' ) && ($user->rights->produit->creer || $user->rights->service->creer)) { $langs->load("suppliers"); if ($rowid) { $product->fetch_product_fournisseur_price($rowid, 1); //Ignore the math expression when getting the price print_fiche_titre($langs->trans("ChangeSupplierPrice")); } else { print_fiche_titre($langs->trans("AddSupplierPrice")); } print '
'; print ''; print ''; dol_fiche_head(); print ''; print ''; // Ref supplier print ''; print ''; // Availability if (! empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) { $langs->load("propal"); print ''."\n"; } // Qty min print ''; print ''; print ''; // Vat rate $default_vat=''; // We don't have supplier, so we try to guess. // For this we build a fictive supplier with same properties than user but using vat) $mysoc2=dol_clone($mysoc); $mysoc2->name='Fictive seller with same country'; $mysoc2->tva_assuj=1; $default_vat=get_default_tva($mysoc2, $mysoc, $product->id, 0); print ''; print ''; if (! empty($conf->dynamicprices->enabled)) //Only show price mode and expression selector if module is enabled { // Price mode selector print ''; // This code hides the numeric price input if is not selected, loads the editor page if editor button is pressed print ''; } // Price qty min print ''; print ''; // Discount qty min print ''; print ''; print ''; // Delai livraison jours print ''; print ''; print ''; print ''; // Charges ???? if ($conf->global->PRODUCT_CHARGES) { if (! empty($conf->margin->enabled)) { print ''; print ''; print ''; print ''; } } if (is_object($hookmanager)) { $parameters=array('id_fourn'=>$id_fourn,'prod_id'=>$product->id); $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); } print '
'.$langs->trans("Supplier").''; if ($rowid) { $supplier=new Fournisseur($db); $supplier->fetch($socid); print $supplier->getNomUrl(1); print ''; print ''; print ''; print ''; print ''; } else { $events=array(); $events[]=array('method' => 'getVatRates', 'url' => dol_buildpath('/core/ajax/vatrates.php',1), 'htmlname' => 'tva_tx', 'params' => array()); print $form->select_company(GETPOST("id_fourn"),'id_fourn','fournisseur=1',1,0,0,$events); $parameters=array('filtre'=>"fournisseur=1",'html_name'=>'id_fourn','selected'=>GETPOST("id_fourn"),'showempty'=>1,'prod_id'=>$product->id); $reshook=$hookmanager->executeHooks('formCreateThirdpartyOptions',$parameters,$object,$action); if (empty($reshook)) { if (empty($form->result)) { print ' - '.$langs->trans("CreateDolibarrThirdPartySupplier").''; } } } print '
'.$langs->trans("SupplierRef").''; if ($rowid) { print $product->fourn_ref; } else { print ''; } print '
'.$langs->trans("Availability").''; $form->selectAvailabilityDelay($product->fk_availability,"oselDispo",1); print '
'.$langs->trans("QtyMin").''; $quantity = GETPOST('qty') ? GETPOST('qty') : "1"; if ($rowid) { print ''; print $product->fourn_qty; } else { print ''; } print '
'.$langs->trans("VATRateForSupplierProduct").''; //print $form->load_tva('tva_tx',$product->tva_tx,$supplier,$mysoc); // Do not use list here as it may be any vat rates for any country if (! empty($socid)) // When update { $default_vat=get_default_tva($supplier, $mysoc, $product->id); if (empty($default_vat)) $default_vat=$product->tva_tx; } print ''; print '
'.$langs->trans("PriceMode").''; $price_expression = new PriceExpression($db); $price_expression_list = array(0 => $langs->trans("PriceNumeric")); //Put the numeric mode as first option foreach ($price_expression->list_price_expression() as $entry) { $price_expression_list[$entry->id] = $entry->title; } $price_expression_preselection = GETPOST('eid') ? GETPOST('eid') : ($product->fk_supplier_price_expression ? $product->fk_supplier_price_expression : '0'); print $form->selectarray('eid', $price_expression_list, $price_expression_preselection); print ' 
'.$langs->trans("PriceExpressionEditor").'
'; print '
'.$langs->trans("PriceQtyMin").''; print ' '; print $form->selectPriceBaseType((GETPOST('price_base_type')?GETPOST('price_base_type'):$product->price_base_type), "price_base_type"); print '
'.$langs->trans("DiscountQtyMin").' %'; print '
'.$langs->trans('NbDaysToDelivery').' '.$langs->trans('days').'
'.$langs->trans("Charges").''; print '
'; dol_fiche_end(); print '
'; print ''; print '     '; print ''; print '
'; print '
'; } // Actions buttons print "\n
\n"; if ($action != 'add_price' && $action != 'updateprice') { $parameters=array(); $reshook=$hookmanager->executeHooks('addMoreActionsButtons',$parameters,$object,$action); // Note that $action and $object may have been modified by hook if (empty($reshook)) { if ($user->rights->produit->creer || $user->rights->service->creer) { print ''; print $langs->trans("AddSupplierPrice").''; } } } print "\n
\n"; print '
'; if ($user->rights->fournisseur->lire) { // Suppliers list title print ''; if ($product->isproduct()) $nblignefour=4; else $nblignefour=4; $param="&id=".$product->id; print ''; print_liste_field_titre($langs->trans("Suppliers"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("SupplierRef")); if (!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) print_liste_field_titre($langs->trans("Availability"),$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("VATRate")); print_liste_field_titre($langs->trans("PriceQtyMinHT")); print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("DiscountQtyMin")); print_liste_field_titre($langs->trans("NbDaysToDelivery"),$_SERVER["PHP_SELF"],"pfp.delivery_time_days","",$param,'align="right"',$sortfield,$sortorder); // Charges ???? if ($conf->global->PRODUCT_CHARGES) { if (! empty($conf->margin->enabled)) print_liste_field_titre($langs->trans("UnitCharges")); } print_liste_field_titre(''); print "\n"; $product_fourn = new ProductFournisseur($db); $product_fourn_list = $product_fourn->list_product_fournisseur_price($product->id, $sortfield, $sortorder); if (count($product_fourn_list)>0) { $var=true; foreach($product_fourn_list as $productfourn) { $var=!$var; print ""; print ''; // Supplier print ''; //Availability if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) { $form->load_cache_availability(); $availability= $form->cache_availability[$productfourn->fk_availability]['label']; print ''; } // Quantity print ''; // VAT rate print ''; // Price for the quantity print ''; // Charges ???? if ($conf->global->PRODUCT_CHARGES) { if (! empty($conf->margin->enabled)) { print ''; } } // Unit price print ''; // Discount print ''; // Delivery delay print ''; // Charges ???? if ($conf->global->PRODUCT_CHARGES) { if (! empty($conf->margin->enabled)) { print ''; } } if (is_object($hookmanager)) { $parameters=array('id_pfp'=>$productfourn->product_fourn_price_id,'id_fourn'=>$id_fourn,'prod_id'=>$product->id); $reshook=$hookmanager->executeHooks('printObjectLine',$parameters,$object,$action); } // Modify-Remove print ''; print ''; } } print '
'.$productfourn->getSocNomUrl(1,'supplier').''.$productfourn->fourn_ref.''.$availability.''; print $productfourn->fourn_qty; print ''; print vatrate($productfourn->fourn_tva_tx,true); print ''; print $productfourn->fourn_price?price($productfourn->fourn_price):""; print ''; print $productfourn->fourn_charges?price($productfourn->fourn_charges):""; print ''; print price($productfourn->fourn_unitprice); //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):" "); print ''; print price2num($productfourn->fourn_remise_percent).'%'; print ''; print $productfourn->delivery_time_days; print ''; print $productfourn->fourn_unitcharges?price($productfourn->fourn_unitcharges) : ($productfourn->fourn_qty?price($productfourn->fourn_charges/$productfourn->fourn_qty):" "); print ''; if ($user->rights->produit->creer || $user->rights->service->creer) { print ''.img_edit().""; print ''.img_picto($langs->trans("Remove"),'disable.png').''; } print '
'; } } } } else { print $langs->trans("ErrorUnknown"); } // End of page llxFooter(); $db->close();