12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706 |
- <?php
- /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@capnetworks.com>
- * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
- * Copyright (C) 2007-2011 Jean Heimburger <jean@tiaris.info>
- * Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
- * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
- * Copyright (C) 2013-2014 Cedric GROSS <c.gross@kreiz-it.fr>
- * Copyright (C) 2013-2016 Marcos García <marcosgdf@gmail.com>
- * Copyright (C) 2011-2014 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
- * Copyright (C) 2014 Henry Florian <florian.henry@open-concept.pro>
- * Copyright (C) 2014-2016 Philippe Grand <philippe.grand@atoo-net.com>
- * Copyright (C) 2014 Ion agorria <ion@agorria.com>
- * Copyright (C) 2016-2017 Ferran Marcet <fmarcet@2byte.es>
- * Copyright (C) 2017 Gustavo Novaro
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
- /**
- * \file htdocs/product/class/product.class.php
- * \ingroup produit
- * \brief File of class to manage predefined products or services
- */
- require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
- require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
- require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
- /**
- * Class to manage products or services
- */
- class Product extends CommonObject
- {
- public $element='product';
- public $table_element='product';
- public $fk_element='fk_product';
- protected $childtables=array('supplier_proposaldet', 'propaldet','commandedet','facturedet','contratdet','facture_fourn_det','commande_fournisseurdet'); // To test if we can delete object
- protected $isnolinkedbythird = 1; // No field fk_soc
- protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
- /**
- * {@inheritdoc}
- */
- protected $table_ref_field = 'ref';
- public $regeximgext='\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm'; // See also into images.lib.php
- /*
- * @deprecated
- * @see label
- */
- public $libelle;
- /**
- * Product label
- * @var string
- */
- public $label;
- /**
- * Product descripion
- * @var string
- */
- public $description;
- /**
- * Check TYPE constants
- * @var int
- */
- public $type = self::TYPE_PRODUCT;
- /**
- * Selling price
- * @var float
- */
- public $price; // Price net
- /**
- * Price with tax
- * @var float
- */
- public $price_ttc;
- /**
- * Minimum price net
- * @var float
- */
- public $price_min;
- /**
- * Minimum price with tax
- * @var float
- */
- public $price_min_ttc;
- /*
- * Base price ('TTC' for price including tax or 'HT' for net price)
- * @var float
- */
- public $price_base_type;
- //! Arrays for multiprices
- public $multiprices=array();
- public $multiprices_ttc=array();
- public $multiprices_base_type=array();
- public $multiprices_min=array();
- public $multiprices_min_ttc=array();
- public $multiprices_tva_tx=array();
- public $multiprices_recuperableonly=array();
- //! Price by quantity arrays
- public $price_by_qty;
- public $prices_by_qty=array();
- public $prices_by_qty_id=array();
- public $prices_by_qty_list=array();
- //! Default VAT code for product (link to code into llx_c_tva but without foreign keys)
- public $default_vat_code;
- //! Default VAT rate of product
- public $tva_tx;
- //! French VAT NPR (0 or 1)
- public $tva_npr=0;
- //! Other local taxes
- public $localtax1_tx;
- public $localtax2_tx;
- public $localtax1_type;
- public $localtax2_type;
- /**
- * Stock real
- * @var int
- */
- public $stock_reel = 0;
- /**
- * Stock virtual
- * @var int
- */
- public $stock_theorique;
- /**
- * Cost price
- * @var float
- */
- public $cost_price;
- //! Average price value for product entry into stock (PMP)
- public $pmp;
- /**
- * Stock alert
- * @var int
- */
- public $seuil_stock_alerte;
- /**
- * Ask for replenishment when $desiredstock < $stock_reel
- */
- public $desiredstock;
- /*
- * Service expiration
- */
- public $duration_value;
- /**
- * Exoiration unit
- */
- public $duration_unit;
- /**
- * Status indicates whether the product is on sale '1' or not '0'
- * @var int
- */
- public $status;
- /**
- * Status indicate whether the product is available for purchase '1' or not '0'
- * @var int
- */
- public $status_buy;
- /**
- * Status indicates whether the product is a finished product '1' or a raw material '0'
- * @var int
- */
- public $finished;
- /**
- * We must manage lot/batch number, sell-by date and so on : '1':yes '0':no
- * @var int
- */
- public $status_batch;
- /**
- * Customs code
- * @var
- */
- public $customcode;
- /**
- * Product URL
- * @var string
- */
- public $url;
- //! Unites de mesure
- public $weight;
- public $weight_units;
- public $length;
- public $length_units;
- public $surface;
- public $surface_units;
- public $volume;
- public $volume_units;
- public $accountancy_code_buy;
- public $accountancy_code_sell;
- /**
- * Main barcode
- * barcode value
- * @var
- */
- public $barcode;
- /**
- * Additional barcodes (Some products have different barcodes according to the country of origin of manufacture)
- * @var array
- */
- public $barcodes_extra=array();
- public $stats_propale=array();
- public $stats_commande=array();
- public $stats_contrat=array();
- public $stats_facture=array();
- public $stats_commande_fournisseur=array();
- public $multilangs=array();
- //! Taille de l'image
- public $imgWidth;
- public $imgHeight;
- public $date_creation;
- public $date_modification;
- //! Id du fournisseur
- public $product_fourn_id;
- //! Product ID already linked to a reference supplier
- public $product_id_already_linked;
- public $nbphoto;
- //! Contains detail of stock of product into each warehouse
- public $stock_warehouse=array();
- public $oldcopy;
- public $fk_price_expression;
- /**
- * @deprecated
- * @see fourn_pu
- */
- public $buyprice;
- public $fourn_pu;
- public $fourn_price_base_type;
- /**
- * @deprecated
- * @see ref_supplier
- */
- public $ref_fourn;
- public $ref_supplier;
- /**
- * Unit code ('km', 'm', 'l', 'p', ...)
- * @var string
- */
- public $fk_unit;
- /**
- * Price is generated using multiprice rules
- * @var int
- */
- public $price_autogen = 0;
- /**
- * Regular product
- */
- const TYPE_PRODUCT = 0;
- /**
- * Service
- */
- const TYPE_SERVICE = 1;
- /**
- * Advanced feature: assembly kit
- */
- const TYPE_ASSEMBLYKIT = 2;
- /**
- * Advanced feature: stock kit
- */
- const TYPE_STOCKKIT = 3;
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- function __construct($db)
- {
- global $langs;
- $this->db = $db;
- $this->status = 0;
- $this->status_buy = 0;
- $this->nbphoto = 0;
- $this->stock_reel = 0;
- $this->seuil_stock_alerte = 0;
- $this->desiredstock = 0;
- $this->canvas = '';
- $this->status_batch=0;
- }
- /**
- * Check that ref and label are ok
- *
- * @return int >1 if OK, <=0 if KO
- */
- function check()
- {
- $this->ref = dol_sanitizeFileName(stripslashes($this->ref));
- $err = 0;
- if (dol_strlen(trim($this->ref)) == 0)
- $err++;
- if (dol_strlen(trim($this->label)) == 0)
- $err++;
- if ($err > 0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- /**
- * Insert product into database
- *
- * @param User $user User making insert
- * @param int $notrigger Disable triggers
- * @return int Id of product/service if OK, < 0 if KO
- */
- function create($user,$notrigger=0)
- {
- global $conf, $langs;
- $error=0;
- // Clean parameters
- $this->ref = dol_string_nospecial(trim($this->ref));
- $this->label = trim($this->label);
- $this->price_ttc=price2num($this->price_ttc);
- $this->price=price2num($this->price);
- $this->price_min_ttc=price2num($this->price_min_ttc);
- $this->price_min=price2num($this->price_min);
- if (empty($this->tva_tx)) $this->tva_tx = 0;
- if (empty($this->tva_npr)) $this->tva_npr = 0;
- //Local taxes
- if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
- if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
- if (empty($this->localtax1_type)) $this->localtax1_type = '0';
- if (empty($this->localtax2_type)) $this->localtax2_type = '0';
- if (empty($this->price)) $this->price = 0;
- if (empty($this->price_min)) $this->price_min = 0;
- // Price by quantity
- if (empty($this->price_by_qty)) $this->price_by_qty = 0;
- if (empty($this->status)) $this->status = 0;
- if (empty($this->status_buy)) $this->status_buy = 0;
- $price_ht=0;
- $price_ttc=0;
- $price_min_ht=0;
- $price_min_ttc=0;
- //
- if ($this->price_base_type == 'TTC' && $this->price_ttc > 0)
- {
- $price_ttc = price2num($this->price_ttc,'MU');
- $price_ht = price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),'MU');
- }
- //
- if ($this->price_base_type != 'TTC' && $this->price > 0)
- {
- $price_ht = price2num($this->price,'MU');
- $price_ttc = price2num($this->price * (1 + ($this->tva_tx / 100)),'MU');
- }
- //
- if (($this->price_min_ttc > 0) && ($this->price_base_type == 'TTC'))
- {
- $price_min_ttc = price2num($this->price_min_ttc,'MU');
- $price_min_ht = price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),'MU');
- }
- //
- if (($this->price_min > 0) && ($this->price_base_type != 'TTC'))
- {
- $price_min_ht = price2num($this->price_min,'MU');
- $price_min_ttc = price2num($this->price_min * (1 + ($this->tva_tx / 100)),'MU');
- }
- $this->accountancy_code_buy = trim($this->accountancy_code_buy);
- $this->accountancy_code_sell= trim($this->accountancy_code_sell);
- // Barcode value
- $this->barcode=trim($this->barcode);
- // Check parameters
- if (empty($this->label))
- {
- $this->error='ErrorMandatoryParametersNotProvided';
- return -1;
- }
- if (empty($this->ref))
- {
- // Load object modCodeProduct
- $module=(! empty($conf->global->PRODUCT_CODEPRODUCT_ADDON)?$conf->global->PRODUCT_CODEPRODUCT_ADDON:'mod_codeproduct_leopard');
- if ($module != 'mod_codeproduct_leopard') // Do not load module file for leopard
- {
- if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php')
- {
- $module = substr($module, 0, dol_strlen($module)-4);
- }
- dol_include_once('/core/modules/product/'.$module.'.php');
- $modCodeProduct = new $module;
- if (! empty($modCodeProduct->code_auto))
- {
- $this->ref = $modCodeProduct->getNextValue($this,$this->type);
- }
- unset($modCodeProduct);
- }
- if (empty($this->ref))
- {
- $this->error='ProductModuleNotSetupForAutoRef';
- return -2;
- }
- }
- dol_syslog(get_class($this)."::create ref=".$this->ref." price=".$this->price." price_ttc=".$this->price_ttc." tva_tx=".$this->tva_tx." price_base_type=".$this->price_base_type, LOG_DEBUG);
- $now=dol_now();
- $this->db->begin();
- // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
- if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code);
- // Check more parameters
- // If error, this->errors[] is filled
- $result = $this->verify();
- if ($result >= 0)
- {
- $sql = "SELECT count(*) as nb";
- $sql.= " FROM ".MAIN_DB_PREFIX."product";
- $sql.= " WHERE entity IN (".getEntity('product').")";
- $sql.= " AND ref = '" .$this->db->escape($this->ref)."'";
- $result = $this->db->query($sql);
- if ($result)
- {
- $obj = $this->db->fetch_object($result);
- if ($obj->nb == 0)
- {
- // Produit non deja existant
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."product (";
- $sql.= "datec";
- $sql.= ", entity";
- $sql.= ", ref";
- $sql.= ", ref_ext";
- $sql.= ", price_min";
- $sql.= ", price_min_ttc";
- $sql.= ", label";
- $sql.= ", fk_user_author";
- $sql.= ", fk_product_type";
- $sql.= ", price";
- $sql.= ", price_ttc";
- $sql.= ", price_base_type";
- $sql.= ", tobuy";
- $sql.= ", tosell";
- $sql.= ", accountancy_code_buy";
- $sql.= ", accountancy_code_sell";
- $sql.= ", canvas";
- $sql.= ", finished";
- $sql.= ", tobatch";
- $sql.= ", fk_unit";
- $sql.= ") VALUES (";
- $sql.= "'".$this->db->idate($now)."'";
- $sql.= ", ".$conf->entity;
- $sql.= ", '".$this->db->escape($this->ref)."'";
- $sql.= ", ".(! empty($this->ref_ext)?"'".$this->db->escape($this->ref_ext)."'":"null");
- $sql.= ", ".price2num($price_min_ht);
- $sql.= ", ".price2num($price_min_ttc);
- $sql.= ", ".(! empty($this->label)?"'".$this->db->escape($this->label)."'":"null");
- $sql.= ", ".$user->id;
- $sql.= ", ".$this->type;
- $sql.= ", ".price2num($price_ht);
- $sql.= ", ".price2num($price_ttc);
- $sql.= ", '".$this->price_base_type."'";
- $sql.= ", ".$this->status;
- $sql.= ", ".$this->status_buy;
- $sql.= ", '".$this->accountancy_code_buy."'";
- $sql.= ", '".$this->accountancy_code_sell."'";
- $sql.= ", '".$this->canvas."'";
- $sql.= ", ".((! isset($this->finished) || $this->finished < 0 || $this->finished == '') ? 'null' : (int) $this->finished);
- $sql.= ", ".((empty($this->status_batch) || $this->status_batch < 0)? '0':$this->status_batch);
- $sql.= ", ".(!$this->fk_unit ? 'NULL' : $this->fk_unit);
- $sql.= ")";
- dol_syslog(get_class($this)."::Create", LOG_DEBUG);
- $result = $this->db->query($sql);
- if ( $result )
- {
- $id = $this->db->last_insert_id(MAIN_DB_PREFIX."product");
- if ($id > 0)
- {
- $this->id = $id;
- $this->price = $price_ht;
- $this->price_ttc = $price_ttc;
- $this->price_min = $price_min_ht;
- $this->price_min_ttc = $price_min_ttc;
- $result = $this->_log_price($user);
- if ($result > 0)
- {
- if ($this->update($id, $user, true, 'add') <= 0)
- {
- $error++;
- }
- }
- else
- {
- $error++;
- $this->error=$this->db->lasterror();
- }
- }
- else
- {
- $error++;
- $this->error='ErrorFailedToGetInsertedId';
- }
- }
- else
- {
- $error++;
- $this->error=$this->db->lasterror();
- }
- }
- else
- {
- // Product already exists with this ref
- $langs->load("products");
- $error++;
- $this->error = "ErrorProductAlreadyExists";
- }
- }
- else
- {
- $error++;
- $this->error=$this->db->lasterror();
- }
- if (! $error && ! $notrigger)
- {
- // Call trigger
- $result=$this->call_trigger('PRODUCT_CREATE',$user);
- if ($result < 0) { $error++; }
- // End call triggers
- }
- if (! $error)
- {
- $this->db->commit();
- return $this->id;
- }
- else
- {
- $this->db->rollback();
- return -$error;
- }
- }
- else
- {
- $this->db->rollback();
- dol_syslog(get_class($this)."::Create fails verify ".join(',',$this->errors), LOG_WARNING);
- return -3;
- }
- }
- /**
- * Check properties of product are ok (like name, barcode, ...).
- * All properties must be already loaded on object (this->barcode, this->barcode_type_code, ...).
- *
- * @return int 0 if OK, <0 if KO
- */
- function verify()
- {
- $this->errors=array();
- $result = 0;
- $this->ref = trim($this->ref);
- if (! $this->ref)
- {
- $this->errors[] = 'ErrorBadRef';
- $result = -2;
- }
- $rescode = $this->check_barcode($this->barcode,$this->barcode_type_code);
- if ($rescode <> 0)
- {
- if ($rescode == -1)
- {
- $this->errors[] = 'ErrorBadBarCodeSyntax';
- }
- if ($rescode == -2)
- {
- $this->errors[] = 'ErrorBarCodeRequired';
- }
- if ($rescode == -3)
- {
- $this->errors[] = 'ErrorBarCodeAlreadyUsed';
- }
- $result = -3;
- }
- return $result;
- }
- /**
- * Check barcode
- *
- * @param string $valuetotest Value to test
- * @param string $typefortest Type of barcode (ISBN, EAN, ...)
- * @return int 0 if OK
- * -1 ErrorBadBarCodeSyntax
- * -2 ErrorBarCodeRequired
- * -3 ErrorBarCodeAlreadyUsed
- */
- function check_barcode($valuetotest,$typefortest)
- {
- global $conf;
- if (! empty($conf->barcode->enabled) && ! empty($conf->global->BARCODE_PRODUCT_ADDON_NUM))
- {
- $module=strtolower($conf->global->BARCODE_PRODUCT_ADDON_NUM);
- $dirsociete=array_merge(array('/core/modules/barcode/'),$conf->modules_parts['barcode']);
- foreach ($dirsociete as $dirroot)
- {
- $res=dol_include_once($dirroot.$module.'.php');
- if ($res) break;
- }
- $mod = new $module();
- dol_syslog(get_class($this)."::check_barcode value=".$valuetotest." type=".$typefortest." module=".$module);
- $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest);
- return $result;
- }
- else
- {
- return 0;
- }
- }
- /**
- * Update a record into database.
- * If batch flag is set to on, we create records into llx_product_batch
- *
- * @param int $id Id of product
- * @param User $user Object user making update
- * @param int $notrigger Disable triggers
- * @param string $action Current action for hookmanager ('add' or 'update')
- * @return int 1 if OK, -1 if ref already exists, -2 if other error
- */
- function update($id, $user, $notrigger=false, $action='update')
- {
- global $langs, $conf, $hookmanager;
- $error=0;
- // Check parameters
- if (! $this->label) $this->label = 'MISSING LABEL';
- // Clean parameters
- $this->ref = dol_string_nospecial(trim($this->ref));
- $this->label = trim($this->label);
- $this->description = trim($this->description);
- $this->note = (isset($this->note) ? trim($this->note) : null);
- $this->weight = price2num($this->weight);
- $this->weight_units = trim($this->weight_units);
- $this->length = price2num($this->length);
- $this->length_units = trim($this->length_units);
- $this->width = price2num($this->width);
- $this->width_units = trim($this->width_units);
- $this->height = price2num($this->height);
- $this->height_units = trim($this->height_units);
- // set unit not defined
- if ($this->length_units) $this->width_units = $this->length_units; // Not used yet
- if ($this->length_units) $this->height_units = $this->length_units; // Not used yet
- // Automated compute surface and volume if not filled
- if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units)
- {
- $this->surface = $this->length * $this->width;
- $this->surface_units = $this->length_units + $this->width_units;
- }
- if (empty($this->volume) && !empty($this->surface_units) && !empty($this->height) && $this->length_units == $this->height_units)
- {
- $this->volume = $this->surface * $this->height;
- $this->volume_units = $this->surface_units + $this->height_units;
- }
- $this->surface = price2num($this->surface);
- $this->surface_units = trim($this->surface_units);
- $this->volume = price2num($this->volume);
- $this->volume_units = trim($this->volume_units);
- if (empty($this->tva_tx)) $this->tva_tx = 0;
- if (empty($this->tva_npr)) $this->tva_npr = 0;
- if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
- if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
- if (empty($this->localtax1_type)) $this->localtax1_type = '0';
- if (empty($this->localtax2_type)) $this->localtax2_type = '0';
- if (empty($this->status)) $this->status = 0;
- if (empty($this->status_buy)) $this->status_buy = 0;
- if (empty($this->country_id)) $this->country_id = 0;
- // Barcode value
- $this->barcode=trim($this->barcode);
- $this->accountancy_code_buy = trim($this->accountancy_code_buy);
- $this->accountancy_code_sell= trim($this->accountancy_code_sell);
- $this->db->begin();
- // Check name is required and codes are ok or unique.
- // If error, this->errors[] is filled
- if ($action != 'add')
- {
- $result = $this->verify(); // We don't check when update called during a create because verify was already done
- }
- if ($result >= 0)
- {
- if (empty($this->oldcopy))
- {
- $org=new self($this->db);
- $org->fetch($this->id);
- $this->oldcopy=$org;
- }
- // Test if batch management is activated on existing product
- // If yes, we create missing entries into product_batch
- if ($this->hasbatch() && !$this->oldcopy->hasbatch())
- {
- //$valueforundefinedlot = 'Undefined'; // In previous version, 39 and lower
- $valueforundefinedlot = '000000';
- dol_syslog("Flag batch of product id=".$this->id." is set to ON, so we will create missing records into product_batch");
- $this->load_stock();
- foreach ($this->stock_warehouse as $idW => $ObjW) // For each warehouse where we have stocks defined for this product (for each lines in product_stock)
- {
- $qty_batch = 0;
- foreach ($ObjW->detail_batch as $detail) // Each lines of detail in product_batch of the current $ObjW = product_stock
- {
- if ($detail->batch == $valueforundefinedlot || $detail->batch == 'Undefined')
- {
- // We discard this line, we will create it later
- $sqlclean="DELETE FROM ".MAIN_DB_PREFIX."product_batch WHERE batch in('Undefined', '".$valueforundefinedlot."') AND fk_product_stock = ".$ObjW->id;
- $result = $this->db->query($sqlclean);
- if (! $result)
- {
- dol_print_error($this->db);
- exit;
- }
- continue;
- }
- $qty_batch += $detail->qty;
- }
- // Quantities in batch details are not same as stock quantity,
- // so we add a default batch record to complete and get same qty in parent and child table
- if ($ObjW->real <> $qty_batch)
- {
- $ObjBatch = new Productbatch($this->db);
- $ObjBatch->batch = $valueforundefinedlot;
- $ObjBatch->qty = ($ObjW->real - $qty_batch);
- $ObjBatch->fk_product_stock = $ObjW->id;
- if ($ObjBatch->create($user,1) < 0)
- {
- $error++;
- $this->errors=$ObjBatch->errors;
- }
- }
- }
- }
- // For automatic creation
- if ($this->barcode == -1) $this->barcode = $this->get_barcode($this,$this->barcode_type_code);
- $sql = "UPDATE ".MAIN_DB_PREFIX."product";
- $sql.= " SET label = '" . $this->db->escape($this->label) ."'";
- $sql.= ", ref = '" . $this->db->escape($this->ref) ."'";
- $sql.= ", ref_ext = ".(! empty($this->ref_ext)?"'".$this->db->escape($this->ref_ext)."'":"null");
- $sql.= ", default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null");
- $sql.= ", tva_tx = " . $this->tva_tx;
- $sql.= ", recuperableonly = " . $this->tva_npr;
- $sql.= ", localtax1_tx = " . $this->localtax1_tx;
- $sql.= ", localtax2_tx = " . $this->localtax2_tx;
- $sql.= ", localtax1_type = " . ($this->localtax1_type!=''?"'".$this->localtax1_type."'":"'0'");
- $sql.= ", localtax2_type = " . ($this->localtax2_type!=''?"'".$this->localtax2_type."'":"'0'");
- $sql.= ", barcode = ". (empty($this->barcode)?"null":"'".$this->db->escape($this->barcode)."'");
- $sql.= ", fk_barcode_type = ". (empty($this->barcode_type)?"null":$this->db->escape($this->barcode_type));
- $sql.= ", tosell = " . $this->status;
- $sql.= ", tobuy = " . $this->status_buy;
- $sql.= ", tobatch = " . ((empty($this->status_batch) || $this->status_batch < 0) ? '0' : $this->status_batch);
- $sql.= ", finished = " . ((! isset($this->finished) || $this->finished < 0) ? "null" : (int) $this->finished);
- $sql.= ", weight = " . ($this->weight!='' ? "'".$this->weight."'" : 'null');
- $sql.= ", weight_units = " . ($this->weight_units!='' ? "'".$this->weight_units."'": 'null');
- $sql.= ", length = " . ($this->length!='' ? "'".$this->length."'" : 'null');
- $sql.= ", length_units = " . ($this->length_units!='' ? "'".$this->length_units."'" : 'null');
- $sql.= ", width= " . ($this->width!='' ? "'".$this->width."'" : 'null');
- $sql.= ", width_units = " . ($this->width_units!='' ? "'".$this->width_units."'" : 'null');
- $sql.= ", height = " . ($this->height!='' ? "'".$this->height."'" : 'null');
- $sql.= ", height_units = " . ($this->height_units!='' ? "'".$this->height_units."'" : 'null');
- $sql.= ", surface = " . ($this->surface!='' ? "'".$this->surface."'" : 'null');
- $sql.= ", surface_units = " . ($this->surface_units!='' ? "'".$this->surface_units."'" : 'null');
- $sql.= ", volume = " . ($this->volume!='' ? "'".$this->volume."'" : 'null');
- $sql.= ", volume_units = " . ($this->volume_units!='' ? "'".$this->volume_units."'" : 'null');
- $sql.= ", seuil_stock_alerte = " . ((isset($this->seuil_stock_alerte) && $this->seuil_stock_alerte != '') ? "'".$this->seuil_stock_alerte."'" : "null");
- $sql.= ", description = '" . $this->db->escape($this->description) ."'";
- $sql.= ", url = " . ($this->url?"'".$this->db->escape($this->url)."'":'null');
- $sql.= ", customcode = '" . $this->db->escape($this->customcode) ."'";
- $sql.= ", fk_country = " . ($this->country_id > 0 ? $this->country_id : 'null');
- $sql.= ", note = ".(isset($this->note) ? "'" .$this->db->escape($this->note)."'" : 'null');
- $sql.= ", duration = '" . $this->db->escape($this->duration_value . $this->duration_unit) ."'";
- $sql.= ", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy)."'";
- $sql.= ", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell)."'";
- $sql.= ", desiredstock = " . ((isset($this->desiredstock) && $this->desiredstock != '') ? $this->desiredstock : "null");
- $sql.= ", cost_price = " . ($this->cost_price != '' ? $this->db->escape($this->cost_price) : 'null');
- $sql.= ", fk_unit= " . (!$this->fk_unit ? 'NULL' : $this->fk_unit);
- $sql.= ", price_autogen = " . (!$this->price_autogen ? 0 : 1);
- $sql.= ", fk_price_expression = ".($this->fk_price_expression != 0 ? $this->fk_price_expression : 'NULL');
- $sql.= ", fk_user_modif = ".($user->id > 0 ? $user->id : 'NULL');
- // stock field is not here because it is a denormalized value from product_stock.
- $sql.= " WHERE rowid = " . $id;
- dol_syslog(get_class($this)."::update", LOG_DEBUG);
- $resql=$this->db->query($sql);
- if ($resql)
- {
- $this->id = $id;
- // Multilangs
- if (! empty($conf->global->MAIN_MULTILANGS))
- {
- if ( $this->setMultiLangs($user) < 0)
- {
- $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql;
- return -2;
- }
- }
- $action='update';
- // Actions on extra fields (by external module or standard code)
- $hookmanager->initHooks(array('productdao'));
- $parameters=array('id'=>$this->id);
- $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
- if (empty($reshook))
- {
- if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
- {
- $result=$this->insertExtraFields();
- if ($result < 0)
- {
- $error++;
- }
- }
- }
- else if ($reshook < 0) $error++;
- if (! $error && ! $notrigger)
- {
- // Call trigger
- $result=$this->call_trigger('PRODUCT_MODIFY',$user);
- if ($result < 0) { $error++; }
- // End call triggers
- }
- if (! $error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref))
- {
- // We remove directory
- if ($conf->product->dir_output)
- {
- $olddir = $conf->product->dir_output . "/" . dol_sanitizeFileName($this->oldcopy->ref);
- $newdir = $conf->product->dir_output . "/" . dol_sanitizeFileName($this->ref);
- if (file_exists($olddir))
- {
- //include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
- //$res = dol_move($olddir, $newdir);
- // do not use dol_move with directory
- $res = @rename($olddir, $newdir);
- if (! $res)
- {
- $langs->load("errors");
- $this->error=$langs->trans('ErrorFailToRenameDir',$olddir,$newdir);
- $error++;
- }
- }
- }
- }
- if (! $error)
- {
- if ($conf->variants->enabled) {
- require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
- $comb = new ProductCombination($this->db);
- foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
- $currcomb->updateProperties($this);
- }
- }
- $this->db->commit();
- return 1;
- }
- else
- {
- $this->db->rollback();
- return -$error;
- }
- }
- else
- {
- if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
- {
- if (empty($conf->barcode->enabled)) $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref);
- else $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductBarCodeAlreadyExists",$this->barcode);
- $this->errors[]=$this->error;
- $this->db->rollback();
- return -1;
- }
- else
- {
- $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql;
- $this->errors[]=$this->error;
- $this->db->rollback();
- return -2;
- }
- }
- }
- else
- {
- $this->db->rollback();
- dol_syslog(get_class($this)."::Update fails verify ".join(',',$this->errors), LOG_WARNING);
- return -3;
- }
- }
- /**
- * Delete a product from database (if not used)
- *
- * @param User $user Product id (usage of this is deprecated, delete should be called without parameters on a fetched object)
- * @param int $notrigger Do not execute trigger
- * @return int < 0 if KO, 0 = Not possible, > 0 if OK
- */
- function delete(User $user, $notrigger=0)
- {
- // Deprecation warning
- if ($id > 0) {
- dol_syslog(__METHOD__ . " with parameter is deprecated", LOG_WARNING);
- }
- global $conf, $langs;
- require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
- $error=0;
- // Clean parameters
- if (empty($id)) $id=$this->id;
- else $this->fetch($id);
- // Check parameters
- if (empty($id))
- {
- $this->error = "Object must be fetched before calling delete";
- return -1;
- }
- if (($this->type == Product::TYPE_PRODUCT && empty($user->rights->produit->supprimer)) || ($this->type == Product::TYPE_SERVICE && empty($user->rights->service->supprimer)))
- {
- $this->error = "ErrorForbidden";
- return 0;
- }
- $objectisused = $this->isObjectUsed($id);
- if (empty($objectisused))
- {
- $this->db->begin();
- if (! $error && empty($notrigger))
- {
- // Call trigger
- $result=$this->call_trigger('PRODUCT_DELETE',$user);
- if ($result < 0) { $error++; }
- // End call triggers
- }
- // Delete from product_batch on product delete
- if (! $error)
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX.'product_batch';
- $sql.= " WHERE fk_product_stock IN (";
- $sql.= "SELECT rowid FROM ".MAIN_DB_PREFIX.'product_stock';
- $sql.= " WHERE fk_product = ".$id.")";
- dol_syslog(get_class($this).'::delete', LOG_DEBUG);
- $result = $this->db->query($sql);
- if (! $result)
- {
- $error++;
- $this->errors[] = $this->db->lasterror();
- }
- }
- // Delete all child tables
- if (! $error)
- {
- $elements = array('product_fournisseur_price','product_price','product_lang','categorie_product','product_stock','product_customer_price','product_lot'); // product_batch is done before
- foreach($elements as $table)
- {
- if (! $error)
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
- $sql.= " WHERE fk_product = ".$id;
- dol_syslog(get_class($this).'::delete', LOG_DEBUG);
- $result = $this->db->query($sql);
- if (! $result)
- {
- $error++;
- $this->errors[] = $this->db->lasterror();
- }
- }
- }
- }
- if (!$error) {
- require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
- require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
- //If it is a parent product, then we remove the association with child products
- $prodcomb = new ProductCombination($this->db);
- if ($prodcomb->deleteByFkProductParent($id) < 0) {
- $error++;
- $this->errors[] = 'Error deleting combinations';
- }
- //We also check if it is a child product
- if (!$error && ($prodcomb->fetchByFkProductChild($id) > 0) && ($prodcomb->delete() < 0)) {
- $error++;
- $this->errors[] = 'Error deleting child combination';
- }
- }
- // Delete product
- if (! $error)
- {
- $sqlz = "DELETE FROM ".MAIN_DB_PREFIX."product";
- $sqlz.= " WHERE rowid = ".$id;
- dol_syslog(get_class($this).'::delete', LOG_DEBUG);
- $resultz = $this->db->query($sqlz);
- if ( ! $resultz )
- {
- $error++;
- $this->errors[] = $this->db->lasterror();
- }
- }
- if (! $error)
- {
- // We remove directory
- $ref = dol_sanitizeFileName($this->ref);
- if ($conf->product->dir_output)
- {
- $dir = $conf->product->dir_output . "/" . $ref;
- if (file_exists($dir))
- {
- $res=@dol_delete_dir_recursive($dir);
- if (! $res)
- {
- $this->errors[] = 'ErrorFailToDeleteDir';
- $error++;
- }
- }
- }
- }
- // Remove extrafields
- if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
- {
- $result=$this->deleteExtraFields();
- if ($result < 0)
- {
- $error++;
- dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
- }
- }
- if (! $error)
- {
- $this->db->commit();
- return 1;
- }
- else
- {
- foreach($this->errors as $errmsg)
- {
- dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
- $this->error.=($this->error?', '.$errmsg:$errmsg);
- }
- $this->db->rollback();
- return -$error;
- }
- }
- else
- {
- $this->error = "ErrorRecordIsUsedCantDelete";
- return 0;
- }
- }
- /**
- * Update or add a translation for a product
- *
- * @param User $user Object user making update
- * @return int <0 if KO, >0 if OK
- */
- function setMultiLangs($user)
- {
- global $conf, $langs;
- $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
- $current_lang = $langs->getDefaultLang();
- foreach ($langs_available as $key => $value)
- {
- if ($key == $current_lang)
- {
- $sql = "SELECT rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_lang";
- $sql.= " WHERE fk_product=".$this->id;
- $sql.= " AND lang='".$key."'";
- $result = $this->db->query($sql);
- if ($this->db->num_rows($result)) // if there is already a description line for this language
- {
- $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_lang";
- $sql2.= " SET ";
- $sql2.= " label='".$this->db->escape($this->label)."',";
- $sql2.= " description='".$this->db->escape($this->description)."'";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.= ", note='".$this->db->escape($this->note)."'";
- $sql2.= " WHERE fk_product=".$this->id." AND lang='".$this->db->escape($key)."'";
- }
- else
- {
- $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_lang (fk_product, lang, label, description";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.=", note";
- $sql2.= ")";
- $sql2.= " VALUES(".$this->id.",'".$this->db->escape($key)."','". $this->db->escape($this->label)."',";
- $sql2.= " '".$this->db->escape($this->description)."'";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.= ", '".$this->db->escape($this->note)."'";
- $sql2.= ")";
- }
- dol_syslog(get_class($this).'::setMultiLangs key = current_lang = '.$key);
- if (! $this->db->query($sql2))
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- else if (isset($this->multilangs[$key]))
- {
- $sql = "SELECT rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_lang";
- $sql.= " WHERE fk_product=".$this->id;
- $sql.= " AND lang='".$key."'";
- $result = $this->db->query($sql);
- if ($this->db->num_rows($result)) // if there is already a description line for this language
- {
- $sql2 = "UPDATE ".MAIN_DB_PREFIX."product_lang";
- $sql2.= " SET ";
- $sql2.= " label='".$this->db->escape($this->multilangs["$key"]["label"])."',";
- $sql2.= " description='".$this->db->escape($this->multilangs["$key"]["description"])."'";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.= ", note='".$this->db->escape($this->multilangs["$key"]["note"])."'";
- $sql2.= " WHERE fk_product=".$this->id." AND lang='".$this->db->escape($key)."'";
- }
- else
- {
- $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."product_lang (fk_product, lang, label, description";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.=", note";
- $sql2.= ")";
- $sql2.= " VALUES(".$this->id.",'".$this->db->escape($key)."','". $this->db->escape($this->multilangs["$key"]["label"])."',";
- $sql2.= " '".$this->db->escape($this->multilangs["$key"]["description"])."'";
- if (! empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) $sql2.= ", '".$this->db->escape($this->note)."'";
- $sql2.= ")";
- }
- // We do not save if main fields are empty
- if ($this->multilangs["$key"]["label"] || $this->multilangs["$key"]["description"])
- {
- if (! $this->db->query($sql2))
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- }
- else
- {
- // language is not current language and we didn't provide a multilang description for this language
- }
- }
- // Call trigger
- $result = $this->call_trigger('PRODUCT_SET_MULTILANGS',$user);
- if ($result < 0) {
- $this->error = $this->db->lasterror();
- return -1;
- }
- // End call triggers
- return 1;
- }
- /**
- * Delete a language for this product
- *
- * @param string $langtodelete Language code to delete
- * @param User $user Object user making delete
- *
- * @return int <0 if KO, >0 if OK
- */
- function delMultiLangs($langtodelete, $user)
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_lang";
- $sql.= " WHERE fk_product=".$this->id." AND lang='".$this->db->escape($langtodelete)."'";
- dol_syslog(get_class($this).'::delMultiLangs', LOG_DEBUG);
- $result = $this->db->query($sql);
- if ($result)
- {
- // Call trigger
- $result = $this->call_trigger('PRODUCT_DEL_MULTILANGS',$user);
- if ($result < 0) {
- $this->error = $this->db->lasterror();
- dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
- return -1;
- }
- // End call triggers
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
- return -1;
- }
- }
- /*
- * Sets an accountancy code for a product.
- * Also calls PRODUCT_MODIFY trigger when modified
- *
- * @param string $type It can be 'buy' or 'sell'
- * @param string $value Accountancy code
- * @return int <0 KO >0 OK
- */
- public function setAccountancyCode($type, $value)
- {
- global $user, $langs, $conf;
- $this->db->begin();
- if ($type == 'buy') {
- $field = 'accountancy_code_buy';
- } elseif ($type == 'sell') {
- $field = 'accountancy_code_sell';
- } else {
- return -1;
- }
- $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ";
- $sql.= "$field = '".$this->db->escape($value)."'";
- $sql.= " WHERE rowid = ".$this->id;
- dol_syslog(get_class($this)."::".__FUNCTION__." sql=".$sql, LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- // Call triggers
- include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
- $interface=new Interfaces($this->db);
- $result=$interface->run_triggers('PRODUCT_MODIFY',$this,$user,$langs,$conf);
- if ($result < 0)
- {
- $this->errors=$interface->errors;
- $this->db->rollback();
- return -1;
- }
- // End call triggers
- $this->$field = $value;
- $this->db->commit();
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Load array this->multilangs
- *
- * @return int <0 if KO, >0 if OK
- */
- function getMultiLangs()
- {
- global $langs;
- $current_lang = $langs->getDefaultLang();
- $sql = "SELECT lang, label, description, note as other";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_lang";
- $sql.= " WHERE fk_product=".$this->id;
- $result = $this->db->query($sql);
- if ($result)
- {
- while ($obj = $this->db->fetch_object($result))
- {
- //print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
- if ($obj->lang == $current_lang) // si on a les traduct. dans la langue courante on les charge en infos principales.
- {
- $this->label = $obj->label;
- $this->description = $obj->description;
- $this->other = $obj->other;
- }
- $this->multilangs["$obj->lang"]["label"] = $obj->label;
- $this->multilangs["$obj->lang"]["description"] = $obj->description;
- $this->multilangs["$obj->lang"]["other"] = $obj->other;
- }
- return 1;
- }
- else
- {
- $this->error="Error: ".$this->db->lasterror()." - ".$sql;
- return -1;
- }
- }
- /**
- * Insert a track that we changed a customer price
- *
- * @param User $user User making change
- * @param int $level price level to change
- * @return int <0 if KO, >0 if OK
- */
- function _log_price($user,$level=0)
- {
- global $conf;
- $now=dol_now();
- // Clean parameters
- if (empty($this->price_by_qty)) $this->price_by_qty=0;
- // Add new price
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,tosell, tva_tx, default_vat_code, recuperableonly,";
- $sql.= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
- $sql.= " VALUES(".($level?$level:1).", '".$this->db->idate($now)."',".$this->id.",".$user->id.",".$this->price.",".$this->price_ttc.",'".$this->price_base_type."',".$this->status.",".$this->tva_tx.", ".($this->default_vat_code?("'".$this->default_vat_code."'"):"null").",".$this->tva_npr.",";
- $sql.= " ".$this->localtax1_tx.", ".$this->localtax2_tx.", '".$this->localtax1_type."', '".$this->localtax2_type."', ".$this->price_min.",".$this->price_min_ttc.",".$this->price_by_qty.",".$conf->entity.",".($this->fk_price_expression > 0?$this->fk_price_expression:'null');
- $sql.= ")";
- dol_syslog(get_class($this)."::_log_price", LOG_DEBUG);
- $resql=$this->db->query($sql);
- if(! $resql)
- {
- $this->error=$this->db->lasterror();
- dol_print_error($this->db);
- return -1;
- }
- else
- {
- return 1;
- }
- }
- /**
- * Delete a price line
- *
- * @param User $user Object user
- * @param int $rowid Line id to delete
- * @return int <0 if KO, >0 if OK
- */
- function log_price_delete($user,$rowid)
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price";
- $sql.= " WHERE rowid=".$rowid;
- dol_syslog(get_class($this)."::log_price_delete", LOG_DEBUG);
- $resql=$this->db->query($sql);
- if ($resql)
- {
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Read price used by a provider.
- * We enter as input couple prodfournprice/qty or triplet qty/product_id/fourn_ref.
- * This also set some properties on product like ->buyprice, ->fourn_pu, ...
- *
- * @param int $prodfournprice Id du tarif = rowid table product_fournisseur_price
- * @param double $qty Quantity asked or -1 to get first entry found
- * @param int $product_id Filter on a particular product id
- * @param string $fourn_ref Filter on a supplier price ref. 'none' to exclude ref in search.
- * @param int $fk_soc If of supplier
- * @return int <-1 if KO, -1 if qty not enough, 0 if OK but nothing found, id_product if OK and found. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...)
- */
- function get_buyprice($prodfournprice, $qty, $product_id=0, $fourn_ref='', $fk_soc=0)
- {
- global $conf;
- $result = 0;
- // We do a first seach with a select by searching with couple prodfournprice and qty only (later we will search on triplet qty/product_id/fourn_ref)
- $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent,";
- $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx, pfp.fk_supplier_price_expression";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
- $sql.= " WHERE pfp.rowid = ".$prodfournprice;
- if ($qty > 0) $sql.= " AND pfp.quantity <= ".$qty;
- $sql.= " ORDER BY pfp.quantity DESC";
- dol_syslog(get_class($this)."::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $obj = $this->db->fetch_object($resql);
- if ($obj && $obj->quantity > 0) // If we found a supplier prices from the id of supplier price
- {
- if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression))
- {
- require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
- $prod_supplier = new ProductFournisseur($this->db);
- $prod_supplier->product_fourn_price_id = $obj->rowid;
- $prod_supplier->id = $obj->fk_product;
- $prod_supplier->fourn_qty = $obj->quantity;
- $prod_supplier->fourn_tva_tx = $obj->tva_tx;
- $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
- $priceparser = new PriceParser($this->db);
- $price_result = $priceparser->parseProductSupplier($prod_supplier);
- if ($price_result >= 0) {
- $obj->price = $price_result;
- }
- }
- $this->buyprice = $obj->price; // deprecated
- $this->fourn_pu = $obj->price / $obj->quantity; // Unit price of product of supplier
- $this->fourn_price_base_type = 'HT'; // Price base type
- $this->ref_fourn = $obj->ref_fourn; // deprecated
- $this->ref_supplier = $obj->ref_fourn; // Ref supplier
- $this->remise_percent = $obj->remise_percent; // remise percent if present and not typed
- $this->vatrate_supplier = $obj->tva_tx; // Vat ref supplier
- $result=$obj->fk_product;
- return $result;
- }
- else // If not found
- {
- // We do a second search by doing a select again but searching with less reliable criteria: couple qty/id product, and if set fourn_ref or fk_soc.
- $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,";
- $sql.= " pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.tva_tx, pfp.fk_supplier_price_expression";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
- $sql.= " WHERE pfp.fk_product = ".$product_id;
- if ($fourn_ref != 'none') $sql.= " AND pfp.ref_fourn = '".$fourn_ref."'";
- if ($fk_soc > 0) $sql.= " AND pfp.fk_soc = ".$fk_soc;
- if ($qty > 0) $sql.= " AND pfp.quantity <= ".$qty;
- $sql.= " ORDER BY pfp.quantity DESC";
- $sql.= " LIMIT 1";
- dol_syslog(get_class($this)."::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $obj = $this->db->fetch_object($resql);
- if ($obj && $obj->quantity > 0) // If found
- {
- if (!empty($conf->dynamicprices->enabled) && !empty($obj->fk_supplier_price_expression))
- {
- require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
- $prod_supplier = new ProductFournisseur($this->db);
- $prod_supplier->product_fourn_price_id = $obj->rowid;
- $prod_supplier->id = $obj->fk_product;
- $prod_supplier->fourn_qty = $obj->quantity;
- $prod_supplier->fourn_tva_tx = $obj->tva_tx;
- $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
- $priceparser = new PriceParser($this->db);
- $price_result = $priceparser->parseProductSupplier($prod_supplier);
- if ($result >= 0) {
- $obj->price = $price_result;
- }
- }
- $this->buyprice = $obj->price; // deprecated
- $this->fourn_qty = $obj->quantity; // min quantity for price for a virtual supplier
- $this->fourn_pu = $obj->price / $obj->quantity; // Unit price of product for a virtual supplier
- $this->fourn_price_base_type = 'HT'; // Price base type for a virtual supplier
- $this->ref_fourn = $obj->ref_supplier; // deprecated
- $this->ref_supplier = $obj->ref_supplier; // Ref supplier
- $this->remise_percent = $obj->remise_percent; // remise percent if present and not typed
- $this->vatrate_supplier = $obj->tva_tx; // Vat ref supplier
- $result=$obj->fk_product;
- return $result;
- }
- else
- {
- return -1; // Ce produit n'existe pas avec cet id tarif fournisseur ou existe mais qte insuffisante, ni pour le couple produit/ref fournisseur dans la quantité.
- }
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -3;
- }
- }
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -2;
- }
- }
- /**
- * Modify customer price of a product/Service
- *
- * @param double $newprice New price
- * @param string $newpricebase HT or TTC
- * @param User $user Object user that make change
- * @param double $newvat New VAT Rate (For example 8.5. Should not be a string)
- * @param double $newminprice New price min
- * @param int $level 0=standard, >0 = level if multilevel prices
- * @param int $newnpr 0=Standard vat rate, 1=Special vat rate for French NPR VAT
- * @param int $newpsq 1 if it has price by quantity
- * @param int $ignore_autogen Used to avoid infinite loops
- * @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
- * @param string $newdefaultvatcode Default vat code
- * @return int <0 if KO, >0 if OK
- */
- function updatePrice($newprice, $newpricebase, $user, $newvat='',$newminprice='', $level=0, $newnpr=0, $newpsq=0, $ignore_autogen=0, $localtaxes_array=array(), $newdefaultvatcode='')
- {
- global $conf,$langs;
- $id=$this->id;
- dol_syslog(get_class($this)."::update_price id=".$id." newprice=".$newprice." newpricebase=".$newpricebase." newminprice=".$newminprice." level=".$level." npr=".$newnpr." newdefaultvatcode=".$newdefaultvatcode);
- // Clean parameters
- if (empty($this->tva_tx)) $this->tva_tx=0;
- if (empty($newnpr)) $newnpr=0;
- // Check parameters
- if ($newvat == '') $newvat=$this->tva_tx;
- // If multiprices are enabled, then we check if the current product is subject to price autogeneration
- // Price will be modified ONLY when the first one is the one that is being modified
- if (!empty($conf->global->PRODUIT_MULTIPRICES) && !$ignore_autogen && $this->price_autogen && ($level == 1))
- {
- return $this->generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpsq);
- }
- if (! empty($newminprice) && ($newminprice > $newprice))
- {
- $this->error='ErrorPriceCantBeLowerThanMinPrice';
- return -1;
- }
- if ($newprice!='' || $newprice==0)
- {
- if ($newpricebase == 'TTC')
- {
- $price_ttc = price2num($newprice,'MU');
- $price = price2num($newprice) / (1 + ($newvat / 100));
- $price = price2num($price,'MU');
- if ($newminprice!='' || $newminprice==0)
- {
- $price_min_ttc = price2num($newminprice,'MU');
- $price_min = price2num($newminprice) / (1 + ($newvat / 100));
- $price_min = price2num($price_min,'MU');
- }
- else
- {
- $price_min=0;
- $price_min_ttc=0;
- }
- }
- else
- {
- $price = price2num($newprice,'MU');
- $price_ttc = ( $newnpr != 1 ) ? price2num($newprice) * (1 + ($newvat / 100)) : $price;
- $price_ttc = price2num($price_ttc,'MU');
- if ($newminprice!='' || $newminprice==0)
- {
- $price_min = price2num($newminprice,'MU');
- $price_min_ttc = price2num($newminprice) * (1 + ($newvat / 100));
- $price_min_ttc = price2num($price_min_ttc,'MU');
- //print 'X'.$newminprice.'-'.$price_min;
- }
- else
- {
- $price_min=0;
- $price_min_ttc=0;
- }
- }
- //print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc;
- if (count($localtaxes_array) > 0)
- {
- $localtaxtype1=$localtaxes_array['0'];
- $localtax1=$localtaxes_array['1'];
- $localtaxtype2=$localtaxes_array['2'];
- $localtax2=$localtaxes_array['3'];
- }
- else // old method. deprecated because ot can't retreive type
- {
- $localtaxtype1='0';
- $localtax1=get_localtax($newvat,1);
- $localtaxtype2='0';
- $localtax2=get_localtax($newvat,2);
- }
- if (empty($localtax1)) $localtax1=0; // If = '' then = 0
- if (empty($localtax2)) $localtax2=0; // If = '' then = 0
- $this->db->begin();
- // Ne pas mettre de quote sur les numeriques decimaux.
- // Ceci provoque des stockages avec arrondis en base au lieu des valeurs exactes.
- $sql = "UPDATE ".MAIN_DB_PREFIX."product SET";
- $sql.= " price_base_type='".$newpricebase."',";
- $sql.= " price=".$price.",";
- $sql.= " price_ttc=".$price_ttc.",";
- $sql.= " price_min=".$price_min.",";
- $sql.= " price_min_ttc=".$price_min_ttc.",";
- $sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
- $sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
- $sql.= " localtax1_type=".($localtaxtype1!=''?"'".$localtaxtype1."'":"'0'").",";
- $sql.= " localtax2_type=".($localtaxtype2!=''?"'".$localtaxtype2."'":"'0'").",";
- $sql.= " default_vat_code=".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
- $sql.= " tva_tx='".price2num($newvat)."',";
- $sql.= " recuperableonly='".$newnpr."'";
- $sql.= " WHERE rowid = ".$id;
- dol_syslog(get_class($this)."::update_price", LOG_DEBUG);
- $resql=$this->db->query($sql);
- if ($resql)
- {
- $this->multiprices[$level] = $price;
- $this->multiprices_ttc[$level] = $price_ttc;
- $this->multiprices_min[$level]= $price_min;
- $this->multiprices_min_ttc[$level]= $price_min_ttc;
- $this->multiprices_base_type[$level]= $newpricebase;
- $this->multiprices_default_vat_code[$level]= $newdefaultvatcode;
- $this->multiprices_tva_tx[$level]= $newvat;
- $this->multiprices_recuperableonly[$level]= $newnpr;
- $this->price = $price;
- $this->price_ttc = $price_ttc;
- $this->price_min = $price_min;
- $this->price_min_ttc = $price_min_ttc;
- $this->price_base_type = $newpricebase;
- $this->default_vat_code = $newdefaultvatcode;
- $this->tva_tx = $newvat;
- $this->tva_npr = $newnpr;
- //Local taxes
- $this->localtax1_tx = $localtax1;
- $this->localtax2_tx = $localtax2;
- $this->localtax1_type = $localtaxtype1;
- $this->localtax2_type = $localtaxtype2;
- // Price by quantity
- $this->price_by_qty = $newpsq;
- $this->_log_price($user,$level); // Save price for level into table product_price
- $this->level = $level; // Store level of price edited for trigger
- // Call trigger
- $result=$this->call_trigger('PRODUCT_PRICE_MODIFY',$user);
- if ($result < 0)
- {
- $this->db->rollback();
- return -1;
- }
- // End call triggers
- $this->db->commit();
- }
- else
- {
- $this->db->rollback();
- dol_print_error($this->db);
- }
- }
- return 1;
- }
- /**
- * Sets the supplier price expression
- *
- * @param int $expression_id Expression
- * @return int <0 if KO, >0 if OK
- * @deprecated Use Product::update instead
- */
- function setPriceExpression($expression_id)
- {
- global $user;
- $this->fk_price_expression = $expression_id;
- return $this->update($this->id, $user);
- }
- /**
- * Load a product in memory from database
- *
- * @param int $id Id of product/service to load
- * @param string $ref Ref of product/service to load
- * @param string $ref_ext Ref ext of product/service to load
- * @param int $ignore_expression Ignores the math expression for calculating price and uses the db value instead
- * @return int <0 if KO, 0 if not found, >0 if OK
- */
- function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
- {
- include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
- global $langs, $conf;
- dol_syslog(get_class($this)."::fetch id=".$id." ref=".$ref." ref_ext=".$ref_ext);
- // Check parameters
- if (! $id && ! $ref && ! $ref_ext)
- {
- $this->error='ErrorWrongParameters';
- dol_print_error(get_class($this)."::fetch ".$this->error);
- return -1;
- }
- $sql = "SELECT rowid, ref, ref_ext, label, description, url, note as note_private, customcode, fk_country, price, price_ttc,";
- $sql.= " price_min, price_min_ttc, price_base_type, cost_price, default_vat_code, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, tosell,";
- $sql.= " tobuy, fk_product_type, duration, seuil_stock_alerte, canvas, weight, weight_units,";
- $sql.= " length, length_units, width, width_units, height, height_units,";
- $sql.= " surface, surface_units, volume, volume_units, barcode, fk_barcode_type, finished,";
- $sql.= " accountancy_code_buy, accountancy_code_sell, stock, pmp,";
- $sql.= " datec, tms, import_key, entity, desiredstock, tobatch, fk_unit,";
- $sql.= " fk_price_expression, price_autogen";
- $sql.= " FROM ".MAIN_DB_PREFIX."product";
- if ($id) $sql.= " WHERE rowid = ".$this->db->escape($id);
- else
- {
- $sql.= " WHERE entity IN (".getEntity($this->element, 1).")";
- if ($ref) $sql.= " AND ref = '".$this->db->escape($ref)."'";
- else if ($ref_ext) $sql.= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
- }
- $resql = $this->db->query($sql);
- if ( $resql )
- {
- if ($this->db->num_rows($resql) > 0)
- {
- $obj = $this->db->fetch_object($resql);
- $this->id = $obj->rowid;
- $this->ref = $obj->ref;
- $this->ref_ext = $obj->ref_ext;
- $this->label = $obj->label;
- $this->description = $obj->description;
- $this->url = $obj->url;
- $this->note_private = $obj->note_private;
- $this->note = $obj->note_private; // deprecated
- $this->type = $obj->fk_product_type;
- $this->status = $obj->tosell;
- $this->status_buy = $obj->tobuy;
- $this->status_batch = $obj->tobatch;
- $this->customcode = $obj->customcode;
- $this->country_id = $obj->fk_country;
- $this->country_code = getCountry($this->country_id,2,$this->db);
- $this->price = $obj->price;
- $this->price_ttc = $obj->price_ttc;
- $this->price_min = $obj->price_min;
- $this->price_min_ttc = $obj->price_min_ttc;
- $this->price_base_type = $obj->price_base_type;
- $this->cost_price = $obj->cost_price;
- $this->default_vat_code = $obj->default_vat_code;
- $this->tva_tx = $obj->tva_tx;
- //! French VAT NPR
- $this->tva_npr = $obj->tva_npr;
- //! Local taxes
- $this->localtax1_tx = $obj->localtax1_tx;
- $this->localtax2_tx = $obj->localtax2_tx;
- $this->localtax1_type = $obj->localtax1_type;
- $this->localtax2_type = $obj->localtax2_type;
- $this->finished = $obj->finished;
- $this->duration = $obj->duration;
- $this->duration_value = substr($obj->duration,0,dol_strlen($obj->duration)-1);
- $this->duration_unit = substr($obj->duration,-1);
- $this->canvas = $obj->canvas;
- $this->weight = $obj->weight;
- $this->weight_units = $obj->weight_units;
- $this->length = $obj->length;
- $this->length_units = $obj->length_units;
- $this->width = $obj->width;
- $this->width_units = $obj->width_units;
- $this->height = $obj->height;
- $this->height_units = $obj->height_units;
- $this->surface = $obj->surface;
- $this->surface_units = $obj->surface_units;
- $this->volume = $obj->volume;
- $this->volume_units = $obj->volume_units;
- $this->barcode = $obj->barcode;
- $this->barcode_type = $obj->fk_barcode_type;
- $this->accountancy_code_buy = $obj->accountancy_code_buy;
- $this->accountancy_code_sell = $obj->accountancy_code_sell;
- $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
- $this->desiredstock = $obj->desiredstock;
- $this->stock_reel = $obj->stock;
- $this->pmp = $obj->pmp;
- $this->date_creation = $obj->datec;
- $this->date_modification = $obj->tms;
- $this->import_key = $obj->import_key;
- $this->entity = $obj->entity;
- $this->ref_ext = $obj->ref_ext;
- $this->fk_price_expression = $obj->fk_price_expression;
- $this->fk_unit = $obj->fk_unit;
- $this->price_autogen = $obj->price_autogen;
- $this->db->free($resql);
- // Retreive all extrafield for current object
- // fetch optionals attributes and labels
- require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
- $extrafields=new ExtraFields($this->db);
- $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
- $this->fetch_optionals($this->id,$extralabels);
- // multilangs
- if (! empty($conf->global->MAIN_MULTILANGS)) $this->getMultiLangs();
- // Load multiprices array
- if (! empty($conf->global->PRODUIT_MULTIPRICES))
- {
- for ($i=1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
- {
- $sql = "SELECT price, price_ttc, price_min, price_min_ttc,";
- $sql.= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_price";
- $sql.= " WHERE entity IN (".getEntity('productprice').")";
- $sql.= " AND price_level=".$i;
- $sql.= " AND fk_product = ".$this->id;
- $sql.= " ORDER BY date_price DESC, rowid DESC";
- $sql.= " LIMIT 1";
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $result = $this->db->fetch_array($resql);
- $this->multiprices[$i]=$result["price"];
- $this->multiprices_ttc[$i]=$result["price_ttc"];
- $this->multiprices_min[$i]=$result["price_min"];
- $this->multiprices_min_ttc[$i]=$result["price_min_ttc"];
- $this->multiprices_base_type[$i]=$result["price_base_type"];
- // Next two fields are used only if PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL is on
- $this->multiprices_tva_tx[$i]=$result["tva_tx"]; // TODO Add ' ('.$result['default_vat_code'].')'
- $this->multiprices_recuperableonly[$i]=$result["recuperableonly"];
- // Price by quantity
- $this->prices_by_qty[$i]=$result["price_by_qty"];
- $this->prices_by_qty_id[$i]=$result["rowid"];
- // Récuperation de la liste des prix selon qty si flag positionné
- if ($this->prices_by_qty[$i] == 1)
- {
- $sql = "SELECT rowid, price, unitprice, quantity, remise_percent, remise";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
- $sql.= " WHERE fk_product_price = ".$this->prices_by_qty_id[$i];
- $sql.= " ORDER BY quantity ASC";
- $resultat=array();
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $ii=0;
- while ($result= $this->db->fetch_array($resql)) {
- $resultat[$ii]=array();
- $resultat[$ii]["rowid"]=$result["rowid"];
- $resultat[$ii]["price"]= $result["price"];
- $resultat[$ii]["unitprice"]= $result["unitprice"];
- $resultat[$ii]["quantity"]= $result["quantity"];
- $resultat[$ii]["remise_percent"]= $result["remise_percent"];
- $resultat[$ii]["remise"]= $result["remise"];
- $ii++;
- }
- $this->prices_by_qty_list[$i]=$resultat;
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- } else if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY))
- {
- $sql = "SELECT price, price_ttc, price_min, price_min_ttc,";
- $sql.= " price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_price";
- $sql.= " WHERE fk_product = ".$this->id;
- $sql.= " ORDER BY date_price DESC, rowid DESC";
- $sql.= " LIMIT 1";
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $result = $this->db->fetch_array($resql);
- // Price by quantity
- $this->prices_by_qty[0]=$result["price_by_qty"];
- $this->prices_by_qty_id[0]=$result["rowid"];
- // Récuperation de la liste des prix selon qty si flag positionné
- if ($this->prices_by_qty[0] == 1)
- {
- $sql = "SELECT rowid,price, unitprice, quantity, remise_percent, remise";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty";
- $sql.= " WHERE fk_product_price = ".$this->prices_by_qty_id[0];
- $sql.= " ORDER BY quantity ASC";
- $resultat=array();
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $ii=0;
- while ($result= $this->db->fetch_array($resql)) {
- $resultat[$ii]=array();
- $resultat[$ii]["rowid"]=$result["rowid"];
- $resultat[$ii]["price"]= $result["price"];
- $resultat[$ii]["unitprice"]= $result["unitprice"];
- $resultat[$ii]["quantity"]= $result["quantity"];
- $resultat[$ii]["remise_percent"]= $result["remise_percent"];
- $resultat[$ii]["remise"]= $result["remise"];
- $ii++;
- }
- $this->prices_by_qty_list[0]=$resultat;
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- if (!empty($conf->dynamicprices->enabled) && !empty($this->fk_price_expression) && empty($ignore_expression))
- {
- require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
- $priceparser = new PriceParser($this->db);
- $price_result = $priceparser->parseProduct($this);
- if ($price_result >= 0)
- {
- $this->price = $price_result;
- // Calculate the VAT
- $this->price_ttc = price2num($this->price) * (1 + ($this->tva_tx / 100));
- $this->price_ttc = price2num($this->price_ttc,'MU');
- }
- }
- // We should not load stock during the fetch. If someone need stock of product, he must call load_stock after fetching product.
- //$res=$this->load_stock();
- // instead we just init the stock_warehouse array
- $this->stock_warehouse = array();
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- /**
- * Charge tableau des stats propale pour le produit/service
- *
- * @param int $socid Id societe
- * @return array Tableau des stats
- */
- function load_stats_propale($socid=0)
- {
- global $conf;
- global $user;
- $sql = "SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb,";
- $sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as pd";
- $sql.= ", ".MAIN_DB_PREFIX."propal as p";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE p.rowid = pd.fk_propal";
- $sql.= " AND p.fk_soc = s.rowid";
- $sql.= " AND p.entity IN (".getEntity('propal').")";
- $sql.= " AND pd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- //$sql.= " AND pr.fk_statut != 0";
- if ($socid > 0) $sql.= " AND p.fk_soc = ".$socid;
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_propale['customers']=$obj->nb_customers;
- $this->stats_propale['nb']=$obj->nb;
- $this->stats_propale['rows']=$obj->nb_rows;
- $this->stats_propale['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Charge tableau des stats commande client pour le produit/service
- *
- * @param int $socid Id societe pour filtrer sur une societe
- * @param int $filtrestatut Id statut pour filtrer sur un statut
- * @return array Array of stats (nb=nb of order, qty=qty ordered)
- */
- function load_stats_commande($socid=0,$filtrestatut='')
- {
- global $conf,$user;
- $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
- $sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd";
- $sql.= ", ".MAIN_DB_PREFIX."commande as c";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE c.rowid = cd.fk_commande";
- $sql.= " AND c.fk_soc = s.rowid";
- $sql.= " AND c.entity IN (".getEntity('commande').")";
- $sql.= " AND cd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid;
- if ($filtrestatut <> '') $sql.= " AND c.fk_statut in (".$filtrestatut.")";
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_commande['customers']=$obj->nb_customers;
- $this->stats_commande['nb']=$obj->nb;
- $this->stats_commande['rows']=$obj->nb_rows;
- $this->stats_commande['qty']=$obj->qty?$obj->qty:0;
- // if it's a virtual product, maybe it is in order by extension
- if (! empty($conf->global->ORDER_ADD_ORDERS_WITH_PARENT_PROD_IF_INCDEC))
- {
- $TFather = $this->getFather();
- if (is_array($TFather) && !empty($TFather)) {
- foreach($TFather as &$fatherData) {
- $pFather = new Product($this->db);
- $pFather->id = $fatherData['id'];
- $qtyCoef = $fatherData['qty'];
- if ($fatherData['incdec']) {
- $pFather->load_stats_commande($socid, $filtrestatut);
- $this->stats_commande['customers']+=$pFather->stats_commande['customers'];
- $this->stats_commande['nb']+=$pFather->stats_commande['nb'];
- $this->stats_commande['rows']+=$pFather->stats_commande['rows'];
- $this->stats_commande['qty']+=$pFather->stats_commande['qty'] * $qtyCoef;
- }
- }
- }
- }
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Charge tableau des stats commande fournisseur pour le produit/service
- *
- * @param int $socid Id societe pour filtrer sur une societe
- * @param string $filtrestatut Id des statuts pour filtrer sur des statuts
- * @return array Tableau des stats
- */
- function load_stats_commande_fournisseur($socid=0,$filtrestatut='')
- {
- global $conf,$user;
- $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb,";
- $sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd";
- $sql.= ", ".MAIN_DB_PREFIX."commande_fournisseur as c";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE c.rowid = cd.fk_commande";
- $sql.= " AND c.fk_soc = s.rowid";
- $sql.= " AND c.entity IN (".getEntity('supplier_order').")";
- $sql.= " AND cd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid;
- if ($filtrestatut != '') $sql.= " AND c.fk_statut in (".$filtrestatut.")"; // Peut valoir 0
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_commande_fournisseur['suppliers']=$obj->nb_suppliers;
- $this->stats_commande_fournisseur['nb']=$obj->nb;
- $this->stats_commande_fournisseur['rows']=$obj->nb_rows;
- $this->stats_commande_fournisseur['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error().' sql='.$sql;
- return -1;
- }
- }
- /**
- * Charge tableau des stats expedition client pour le produit/service
- *
- * @param int $socid Id societe pour filtrer sur une societe
- * @param int $filtrestatut Id statut pour filtrer sur un statut
- * @return array Tableau des stats
- */
- function load_stats_sending($socid=0,$filtrestatut='')
- {
- global $conf,$user;
- $sql = "SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb,";
- $sql.= " COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."expeditiondet as ed";
- $sql.= ", ".MAIN_DB_PREFIX."commandedet as cd";
- $sql.= ", ".MAIN_DB_PREFIX."commande as c";
- $sql.= ", ".MAIN_DB_PREFIX."expedition as e";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE e.rowid = ed.fk_expedition";
- $sql.= " AND c.rowid = cd.fk_commande";
- $sql.= " AND e.fk_soc = s.rowid";
- $sql.= " AND e.entity IN (".getEntity('expedition').")";
- $sql.= " AND ed.fk_origin_line = cd.rowid";
- $sql.= " AND cd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND e.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND e.fk_soc = ".$socid;
- if ($filtrestatut <> '') $sql.= " AND c.fk_statut in (".$filtrestatut.")";
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_expedition['customers']=$obj->nb_customers;
- $this->stats_expedition['nb']=$obj->nb;
- $this->stats_expedition['rows']=$obj->nb_rows;
- $this->stats_expedition['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Charge tableau des stats réception fournisseur pour le produit/service
- *
- * @param int $socid Id societe pour filtrer sur une societe
- * @param int $filtrestatut Id statut pour filtrer sur un statut
- * @return array Tableau des stats
- */
- function load_stats_reception($socid=0,$filtrestatut='')
- {
- global $conf,$user;
- $sql = "SELECT COUNT(DISTINCT cf.fk_soc) as nb_customers, COUNT(DISTINCT cf.rowid) as nb,";
- $sql.= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur_dispatch as fd";
- $sql.= ", ".MAIN_DB_PREFIX."commande_fournisseur as cf";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE cf.rowid = fd.fk_commande";
- $sql.= " AND cf.fk_soc = s.rowid";
- $sql.= " AND cf.entity IN (".getEntity('supplier_order').")";
- $sql.= " AND fd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND cf.fk_soc = ".$socid;
- if ($filtrestatut <> '') $sql.= " AND cf.fk_statut in (".$filtrestatut.")";
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_reception['suppliers']=$obj->nb_customers;
- $this->stats_reception['nb']=$obj->nb;
- $this->stats_reception['rows']=$obj->nb_rows;
- $this->stats_reception['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Charge tableau des stats contrat pour le produit/service
- *
- * @param int $socid Id societe
- * @return array Tableau des stats
- */
- function load_stats_contrat($socid=0)
- {
- global $conf;
- global $user;
- $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
- $sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
- $sql.= ", ".MAIN_DB_PREFIX."contrat as c";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE c.rowid = cd.fk_contrat";
- $sql.= " AND c.fk_soc = s.rowid";
- $sql.= " AND c.entity IN (".getEntity('contract').")";
- $sql.= " AND cd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- //$sql.= " AND c.statut != 0";
- if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid;
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_contrat['customers']=$obj->nb_customers;
- $this->stats_contrat['nb']=$obj->nb;
- $this->stats_contrat['rows']=$obj->nb_rows;
- $this->stats_contrat['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error().' sql='.$sql;
- return -1;
- }
- }
- /**
- * Charge tableau des stats facture pour le produit/service
- *
- * @param int $socid Id societe
- * @return array Tableau des stats
- */
- function load_stats_facture($socid=0)
- {
- global $conf;
- global $user;
- $sql = "SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
- $sql.= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
- $sql.= ", ".MAIN_DB_PREFIX."facture as f";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE f.rowid = fd.fk_facture";
- $sql.= " AND f.fk_soc = s.rowid";
- $sql.= " AND f.entity IN (".getEntity('facture').")";
- $sql.= " AND fd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- //$sql.= " AND f.fk_statut != 0";
- if ($socid > 0) $sql .= " AND f.fk_soc = ".$socid;
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_facture['customers']=$obj->nb_customers;
- $this->stats_facture['nb']=$obj->nb;
- $this->stats_facture['rows']=$obj->nb_rows;
- $this->stats_facture['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Charge tableau des stats facture pour le produit/service
- *
- * @param int $socid Id societe
- * @return array Tableau des stats
- */
- function load_stats_facture_fournisseur($socid=0)
- {
- global $conf;
- global $user;
- $sql = "SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,";
- $sql.= " COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
- $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
- $sql.= ", ".MAIN_DB_PREFIX."facture_fourn as f";
- $sql.= ", ".MAIN_DB_PREFIX."societe as s";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE f.rowid = fd.fk_facture_fourn";
- $sql.= " AND f.fk_soc = s.rowid";
- $sql.= " AND f.entity IN (".getEntity('facture_fourn').")";
- $sql.= " AND fd.fk_product = ".$this->id;
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- //$sql.= " AND f.fk_statut != 0";
- if ($socid > 0) $sql .= " AND f.fk_soc = ".$socid;
- $result = $this->db->query($sql);
- if ( $result )
- {
- $obj=$this->db->fetch_object($result);
- $this->stats_facture_fournisseur['suppliers']=$obj->nb_suppliers;
- $this->stats_facture_fournisseur['nb']=$obj->nb;
- $this->stats_facture_fournisseur['rows']=$obj->nb_rows;
- $this->stats_facture_fournisseur['qty']=$obj->qty?$obj->qty:0;
- return 1;
- }
- else
- {
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Return an array formated for showing graphs
- *
- * @param string $sql Request to execute
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $year Year (0=current year)
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function _get_stats($sql, $mode, $year=0)
- {
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num)
- {
- $arr = $this->db->fetch_array($resql);
- if ($mode == 'byunit') $tab[$arr[1]] = $arr[0]; // 1st field
- if ($mode == 'bynumber') $tab[$arr[1]] = $arr[2]; // 3rd field
- $i++;
- }
- }
- else
- {
- $this->error=$this->db->error().' sql='.$sql;
- return -1;
- }
- if (empty($year))
- {
- $year = strftime('%Y',time());
- $month = strftime('%m',time());
- }
- else
- {
- $month=12; // We imagine we are at end of year, so we get last 12 month before, so all correct year.
- }
- $result = array();
- for ($j = 0 ; $j < 12 ; $j++)
- {
- $idx=ucfirst(dol_trunc(dol_print_date(dol_mktime(12,0,0,$month,1,$year),"%b"),3,'right','UTF-8',1));
- $monthnum=sprintf("%02s",$month);
- $result[$j] = array($idx,isset($tab[$year.$month])?$tab[$year.$month]:0);
- // $result[$j] = array($monthnum,isset($tab[$year.$month])?$tab[$year.$month]:0);
- $month = "0".($month - 1);
- if (dol_strlen($month) == 3)
- {
- $month = substr($month,1);
- }
- if ($month == 0)
- {
- $month = 12;
- $year = $year - 1;
- }
- }
- return array_reverse($result);
- }
- /**
- * Return nb of units or customers invoices in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_vente($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf;
- global $user;
- $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT f.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as d, ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE f.rowid = d.fk_facture";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND p.rowid = d.fk_product AND p.fk_product_type =".$filteronproducttype;
- $sql.= " AND f.fk_soc = s.rowid";
- $sql.= " AND f.entity IN (".getEntity('facture').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND f.fk_soc = $socid";
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(f.datef,'%Y%m')";
- $sql.= " ORDER BY date_format(f.datef,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Return nb of units or supplier invoices in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_achat($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf;
- global $user;
- $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT f.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as d, ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE f.rowid = d.fk_facture_fourn";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND p.rowid = d.fk_product AND p.fk_product_type =".$filteronproducttype;
- $sql.= " AND f.fk_soc = s.rowid";
- $sql.= " AND f.entity IN (".getEntity('facture_fourn').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND f.fk_soc = $socid";
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(f.datef,'%Y%m')";
- $sql.= " ORDER BY date_format(f.datef,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Return nb of units or proposals in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_propal($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf;
- global $user;
- $sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT p.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
- if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE p.rowid = d.fk_propal";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND prod.rowid = d.fk_product AND prod.fk_product_type =".$filteronproducttype;
- $sql.= " AND p.fk_soc = s.rowid";
- $sql.= " AND p.entity IN (".getEntity('propal').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND p.fk_soc = ".$socid;
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(p.datep,'%Y%m')";
- $sql.= " ORDER BY date_format(p.datep,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Return nb of units or proposals in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_propalsupplier($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf;
- global $user;
- $sql = "SELECT sum(d.qty), date_format(p.date_valid, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT p.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."supplier_proposaldet as d, ".MAIN_DB_PREFIX."supplier_proposal as p, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
- if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE p.rowid = d.fk_supplier_proposal";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND prod.rowid = d.fk_product AND prod.fk_product_type =".$filteronproducttype;
- $sql.= " AND p.fk_soc = s.rowid";
- $sql.= " AND p.entity IN (".getEntity('propal').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND p.fk_soc = ".$socid;
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(p.date_valid,'%Y%m')";
- $sql.= " ORDER BY date_format(p.date_valid,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Return nb of units or orders in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_order($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf, $user;
- $sql = "SELECT sum(d.qty), date_format(c.date_commande, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT c.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as d, ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
- if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE c.rowid = d.fk_commande";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND p.rowid = d.fk_product AND p.fk_product_type =".$filteronproducttype;
- $sql.= " AND c.fk_soc = s.rowid";
- $sql.= " AND c.entity IN (".getEntity('commande').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid;
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(c.date_commande,'%Y%m')";
- $sql.= " ORDER BY date_format(c.date_commande,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Return nb of units or orders in which product is included
- *
- * @param int $socid Limit count on a particular third party id
- * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities
- * @param int $filteronproducttype 0=To filter on product only, 1=To filter on services only
- * @param int $year Year (0=last 12 month)
- * @param string $morefilter More sql filters
- * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11
- */
- function get_nb_ordersupplier($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
- {
- global $conf, $user;
- $sql = "SELECT sum(d.qty), date_format(c.date_commande, '%Y%m')";
- if ($mode == 'bynumber') $sql.= ", count(DISTINCT c.rowid)";
- $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as d, ".MAIN_DB_PREFIX."commande_fournisseur as c, ".MAIN_DB_PREFIX."societe as s";
- if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
- if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
- $sql.= " WHERE c.rowid = d.fk_commande";
- if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
- else $sql.=" AND d.fk_product > 0";
- if ($filteronproducttype >= 0) $sql.= " AND p.rowid = d.fk_product AND p.fk_product_type =".$filteronproducttype;
- $sql.= " AND c.fk_soc = s.rowid";
- $sql.= " AND c.entity IN (".getEntity('supplier_order').")";
- if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
- if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid;
- $sql.=$morefilter;
- $sql.= " GROUP BY date_format(c.date_commande,'%Y%m')";
- $sql.= " ORDER BY date_format(c.date_commande,'%Y%m') DESC";
- return $this->_get_stats($sql,$mode, $year);
- }
- /**
- * Link a product/service to a parent product/service
- *
- * @param int $id_pere Id of parent product/service
- * @param int $id_fils Id of child product/service
- * @param int $qty Quantity
- * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease
- * @return int < 0 if KO, > 0 if OK
- */
- function add_sousproduit($id_pere, $id_fils, $qty, $incdec=1)
- {
- // Clean parameters
- if (! is_numeric($id_pere)) $id_pere=0;
- if (! is_numeric($id_fils)) $id_fils=0;
- if (! is_numeric($incdec)) $incdec=0;
- $result=$this->del_sousproduit($id_pere, $id_fils);
- if ($result < 0) return $result;
- // Check not already father of id_pere (to avoid father -> child -> father links)
- $sql = 'SELECT fk_product_pere from '.MAIN_DB_PREFIX.'product_association';
- $sql .= ' WHERE fk_product_pere = '.$id_fils.' AND fk_product_fils = '.$id_pere;
- if (! $this->db->query($sql))
- {
- dol_print_error($this->db);
- return -1;
- }
- else
- {
- $result = $this->db->query($sql);
- if ($result)
- {
- $num = $this->db->num_rows($result);
- if($num > 0)
- {
- $this->error="isFatherOfThis";
- return -1;
- }
- else
- {
- $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_association(fk_product_pere,fk_product_fils,qty,incdec)';
- $sql .= ' VALUES ('.$id_pere.', '.$id_fils.', '.$qty.', '.$incdec.')';
- if (! $this->db->query($sql))
- {
- dol_print_error($this->db);
- return -1;
- }
- else
- {
- return 1;
- }
- }
- }
- }
- }
- /**
- * Modify composed product
- *
- * @param int $id_pere Id of parent product/service
- * @param int $id_fils Id of child product/service
- * @param int $qty Quantity
- * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease
- * @return int < 0 if KO, > 0 if OK
- */
- function update_sousproduit($id_pere, $id_fils, $qty, $incdec=1)
- {
- // Clean parameters
- if (! is_numeric($id_pere)) $id_pere=0;
- if (! is_numeric($id_fils)) $id_fils=0;
- if (! is_numeric($incdec)) $incdec=1;
- if (! is_numeric($qty)) $qty=1;
- $sql = 'UPDATE '.MAIN_DB_PREFIX.'product_association SET ';
- $sql.= 'qty='.$qty;
- $sql.= ',incdec='.$incdec;
- $sql .= ' WHERE fk_product_pere='.$id_pere.' AND fk_product_fils='.$id_fils;
- if (!$this->db->query($sql))
- {
- dol_print_error($this->db);
- return -1;
- }
- else
- {
- return 1;
- }
- }
- /**
- * Retire le lien entre un sousproduit et un produit/service
- *
- * @param int $fk_parent Id du produit auquel ne sera plus lie le produit lie
- * @param int $fk_child Id du produit a ne plus lie
- * @return int < 0 if KO, > 0 if OK
- */
- function del_sousproduit($fk_parent, $fk_child)
- {
- if (! is_numeric($fk_parent)) $fk_parent=0;
- if (! is_numeric($fk_child)) $fk_child=0;
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_association";
- $sql.= " WHERE fk_product_pere = ".$fk_parent;
- $sql.= " AND fk_product_fils = ".$fk_child;
- dol_syslog(get_class($this).'::del_sousproduit', LOG_DEBUG);
- if (! $this->db->query($sql))
- {
- dol_print_error($this->db);
- return -1;
- }
- return 1;
- }
- /**
- * Verifie si c'est un sous-produit
- *
- * @param int $fk_parent Id du produit auquel le produit est lie
- * @param int $fk_child Id du produit lie
- * @return int < 0 si erreur, > 0 si ok
- */
- function is_sousproduit($fk_parent, $fk_child)
- {
- $sql = "SELECT fk_product_pere, qty, incdec";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_association";
- $sql.= " WHERE fk_product_pere = '".$fk_parent."'";
- $sql.= " AND fk_product_fils = '".$fk_child."'";
- $result = $this->db->query($sql);
- if ($result)
- {
- $num = $this->db->num_rows($result);
- if($num > 0)
- {
- $obj = $this->db->fetch_object($result);
- $this->is_sousproduit_qty = $obj->qty;
- $this->is_sousproduit_incdec = $obj->incdec;
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- /**
- * Add a supplier price for the product.
- * Note: Duplicate ref is accepted for different quantity only, or for different companies.
- *
- * @param User $user User that make link
- * @param int $id_fourn Supplier id
- * @param string $ref_fourn Supplier ref
- * @param float $quantity Quantity minimum for price
- * @return int < 0 if KO, 0 if link already exists for this product, > 0 if OK
- */
- function add_fournisseur($user, $id_fourn, $ref_fourn, $quantity)
- {
- global $conf;
- $now=dol_now();
- dol_syslog(get_class($this)."::add_fournisseur id_fourn = ".$id_fourn." ref_fourn=".$ref_fourn." quantity=".$quantity, LOG_DEBUG);
- if ($ref_fourn)
- {
- $sql = "SELECT rowid, fk_product";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
- $sql.= " WHERE fk_soc = ".$id_fourn;
- $sql.= " AND ref_fourn = '".$this->db->escape($ref_fourn)."'";
- $sql.= " AND fk_product != ".$this->id;
- $sql.= " AND entity IN (".getEntity('productprice').")";
- $resql=$this->db->query($sql);
- if ($resql)
- {
- $obj = $this->db->fetch_object($resql);
- if ($obj)
- {
- // If the supplier ref already exists but for another product (duplicate ref is accepted for different quantity only or different companies)
- $this->product_id_already_linked = $obj->fk_product;
- return -3;
- }
- $this->db->free($resql);
- }
- }
- $sql = "SELECT rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
- $sql.= " WHERE fk_soc = ".$id_fourn;
- if ($ref_fourn) $sql.= " AND ref_fourn = '".$this->db->escape($ref_fourn)."'";
- else $sql.= " AND (ref_fourn = '' OR ref_fourn IS NULL)";
- $sql.= " AND quantity = '".$quantity."'";
- $sql.= " AND fk_product = ".$this->id;
- $sql.= " AND entity IN (".getEntity('productprice').")";
- $resql=$this->db->query($sql);
- if ($resql)
- {
- $obj = $this->db->fetch_object($resql);
- // The reference supplier does not exist, we create it for this product.
- if (! $obj)
- {
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
- $sql.= "datec";
- $sql.= ", entity";
- $sql.= ", fk_product";
- $sql.= ", fk_soc";
- $sql.= ", ref_fourn";
- $sql.= ", quantity";
- $sql.= ", fk_user";
- $sql.= ", tva_tx";
- $sql.= ") VALUES (";
- $sql.= "'".$this->db->idate($now)."'";
- $sql.= ", ".$conf->entity;
- $sql.= ", ".$this->id;
- $sql.= ", ".$id_fourn;
- $sql.= ", '".$this->db->escape($ref_fourn)."'";
- $sql.= ", ".$quantity;
- $sql.= ", ".$user->id;
- $sql.= ", 0";
- $sql.= ")";
- if ($this->db->query($sql))
- {
- $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- // If the supplier price already exists for this product and quantity
- else
- {
- $this->product_fourn_price_id = $obj->rowid;
- return 0;
- }
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -2;
- }
- }
- /**
- * Renvoie la liste des fournisseurs du produit/service
- *
- * @return array Tableau des id de fournisseur
- */
- function list_suppliers()
- {
- global $conf;
- $list = array();
- $sql = "SELECT DISTINCT p.fk_soc";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as p";
- $sql.= " WHERE p.fk_product = ".$this->id;
- $sql.= " AND p.entity = ".$conf->entity;
- $result = $this->db->query($sql);
- if ($result)
- {
- $num = $this->db->num_rows($result);
- $i=0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($result);
- $list[$i] = $obj->fk_soc;
- $i++;
- }
- }
- return $list;
- }
- /**
- * Recopie les prix d'un produit/service sur un autre
- *
- * @param int $fromId Id product source
- * @param int $toId Id product target
- * @return nt < 0 if KO, > 0 if OK
- */
- function clone_price($fromId, $toId)
- {
- $this->db->begin();
- // les prix
- $sql = "INSERT ".MAIN_DB_PREFIX."product_price (";
- $sql.= " fk_product, date_price, price, tva_tx, localtax1_tx, localtax2_tx, fk_user_author, tosell)";
- $sql.= " SELECT ".$toId . ", date_price, price, tva_tx, localtax1_tx, localtax2_tx, fk_user_author, tosell";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_price ";
- $sql.= " WHERE fk_product = ". $fromId;
- dol_syslog(get_class($this).'::clone_price', LOG_DEBUG);
- if (! $this->db->query($sql))
- {
- $this->db->rollback();
- return -1;
- }
- $this->db->commit();
- return 1;
- }
- /**
- * Clone links between products
- *
- * @param int $fromId Product id
- * @param int $toId Product id
- * @return int <0 if KO, >0 if OK
- */
- function clone_associations($fromId, $toId)
- {
- $this->db->begin();
- $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_association (fk_product_pere, fk_product_fils, qty)';
- $sql.= " SELECT ".$toId.", fk_product_fils, qty FROM ".MAIN_DB_PREFIX."product_association";
- $sql.= " WHERE fk_product_pere = ".$fromId;
- dol_syslog(get_class($this).'::clone_association', LOG_DEBUG);
- if (! $this->db->query($sql))
- {
- $this->db->rollback();
- return -1;
- }
- $this->db->commit();
- return 1;
- }
- /**
- * Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre
- *
- * @param int $fromId Id produit source
- * @param int $toId Id produit cible
- * @return int < 0 si erreur, > 0 si ok
- */
- function clone_fournisseurs($fromId, $toId)
- {
- $this->db->begin();
- $now=dol_now();
- // les fournisseurs
- /*$sql = "INSERT ".MAIN_DB_PREFIX."product_fournisseur ("
- . " datec, fk_product, fk_soc, ref_fourn, fk_user_author )"
- . " SELECT '".$this->db->idate($now)."', ".$toId.", fk_soc, ref_fourn, fk_user_author"
- . " FROM ".MAIN_DB_PREFIX."product_fournisseur"
- . " WHERE fk_product = ".$fromId;
- if ( ! $this->db->query($sql ) )
- {
- $this->db->rollback();
- return -1;
- }*/
- // les prix de fournisseurs.
- $sql = "INSERT ".MAIN_DB_PREFIX."product_fournisseur_price (";
- $sql.= " datec, fk_product, fk_soc, price, quantity, fk_user)";
- $sql.= " SELECT '".$this->db->idate($now)."', ".$toId. ", fk_soc, price, quantity, fk_user";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
- $sql.= " WHERE fk_product = ".$fromId;
- dol_syslog(get_class($this).'::clone_fournisseurs', LOG_DEBUG);
- $resql=$this->db->query($sql);
- if (! $resql)
- {
- $this->db->rollback();
- return -1;
- }
- else
- {
- $this->db->commit();
- return 1;
- }
- }
- /**
- * Fonction recursive uniquement utilisee par get_arbo_each_prod, recompose l'arborescence des sousproduits
- * Define value of this->res
- *
- * @param array $prod Products array
- * @param string $compl_path Directory path of parents to add before
- * @param int $multiply Because each sublevel must be multiplicated by parent nb
- * @param int $level Init level
- * @param int $id_parent Id parent
- * @return void
- */
- function fetch_prod_arbo($prod, $compl_path="", $multiply=1, $level=1, $id_parent=0)
- {
- global $conf,$langs;
- $product = new Product($this->db);
- //var_dump($prod);
- foreach($prod as $id_product => $desc_pere) // $id_product is 0 (first call starting with root top) or an id of a sub_product
- {
- if (is_array($desc_pere)) // If desc_pere is an array, this means it's a child
- {
- $id=(! empty($desc_pere[0]) ? $desc_pere[0] :'');
- $nb=(! empty($desc_pere[1]) ? $desc_pere[1] :'');
- $type=(! empty($desc_pere[2]) ? $desc_pere[2] :'');
- $label=(! empty($desc_pere[3]) ? $desc_pere[3] :'');
- $incdec=!empty($desc_pere[4]) ? $desc_pere[4] : 0;
- if ($multiply < 1) $multiply=1;
- //print "XXX We add id=".$id." - label=".$label." - nb=".$nb." - multiply=".$multiply." fullpath=".$compl_path.$label."\n";
- $this->fetch($id); // Load product
- $this->load_stock('nobatch,novirtual'); // Load stock to get true this->stock_reel
- $this->res[]= array(
- 'id'=>$id, // Id product
- 'id_parent'=>$id_parent,
- 'ref'=>$this->ref, // Ref product
- 'nb'=>$nb, // Nb of units that compose parent product
- 'nb_total'=>$nb*$multiply, // Nb of units for all nb of product
- 'stock'=>$this->stock_reel, // Stock
- 'stock_alert'=>$this->seuil_stock_alerte, // Stock alert
- 'label'=>$label,
- 'fullpath'=>$compl_path.$label, // Label
- 'type'=>$type, // Nb of units that compose parent product
- 'desiredstock'=>$this->desiredstock,
- 'level'=>$level,
- 'incdec'=>$incdec,
- 'entity'=>$this->entity
- );
- // Recursive call if there is childs to child
- if (is_array($desc_pere['childs']))
- {
- //print 'YYY We go down for '.$desc_pere[3]." -> \n";
- $this->fetch_prod_arbo($desc_pere['childs'], $compl_path.$desc_pere[3]." -> ", $desc_pere[1]*$multiply, $level+1, $id);
- }
- }
- }
- }
- /**
- * fonction recursive uniquement utilisee par get_each_prod, ajoute chaque sousproduits dans le tableau res
- *
- * @param array $prod Products array
- * @return void
- */
- function fetch_prods($prod)
- {
- $this->res;
- foreach($prod as $nom_pere => $desc_pere)
- {
- // on est dans une sous-categorie
- if(is_array($desc_pere))
- $this->res[]= array($desc_pere[1],$desc_pere[0]);
- if(count($desc_pere) >1)
- {
- $this->fetch_prods($desc_pere);
- }
- }
- }
- /**
- * reconstruit l'arborescence des produits sous la forme d'un tableau
- *
- * @param int $multiply Because each sublevel must be multiplicated by parent nb
- * @return array $this->res
- */
- function get_arbo_each_prod($multiply=1)
- {
- $this->res = array();
- if (isset($this->sousprods) && is_array($this->sousprods))
- {
- foreach($this->sousprods as $prod_name => $desc_product)
- {
- if (is_array($desc_product)) $this->fetch_prod_arbo($desc_product,"",$multiply,1,$this->id);
- }
- }
- //var_dump($this->res);
- return $this->res;
- }
- /**
- * Renvoie tous les sousproduits dans le tableau res, chaque ligne de res contient : id -> qty
- *
- * @return array $this->res
- */
- function get_each_prod()
- {
- $this->res = array();
- if (is_array($this->sousprods))
- {
- foreach($this->sousprods as $nom_pere => $desc_pere)
- {
- if (count($desc_pere) >1) $this->fetch_prods($desc_pere);
- }
- sort($this->res);
- }
- return $this->res;
- }
- /**
- * Return all parent products for current product (first level only)
- *
- * @return array Array of product
- */
- function getFather()
- {
- $sql = "SELECT p.rowid, p.label as label, p.ref as ref, pa.fk_product_pere as id, p.fk_product_type, pa.qty, pa.incdec, p.entity";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_association as pa,";
- $sql.= " ".MAIN_DB_PREFIX."product as p";
- $sql.= " WHERE p.rowid = pa.fk_product_pere";
- $sql.= " AND pa.fk_product_fils = ".$this->id;
- $res = $this->db->query($sql);
- if ($res)
- {
- $prods = array ();
- while ($record = $this->db->fetch_array($res))
- {
- // $record['id'] = $record['rowid'] = id of father
- $prods[$record['id']]['id'] = $record['rowid'];
- $prods[$record['id']]['ref'] = $record['ref'];
- $prods[$record['id']]['label'] = $record['label'];
- $prods[$record['id']]['qty'] = $record['qty'];
- $prods[$record['id']]['incdec'] = $record['incdec'];
- $prods[$record['id']]['fk_product_type'] = $record['fk_product_type'];
- $prods[$record['id']]['entity'] = $record['entity'];
- }
- return $prods;
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- /**
- * Return childs of product $id
- *
- * @param int $id Id of product to search childs of
- * @param int $firstlevelonly Return only direct child
- * @param int $level Level of recursing call (start to 1)
- * @return array Prod
- */
- function getChildsArbo($id, $firstlevelonly=0, $level=1)
- {
- global $alreadyfound;
- $sql = "SELECT p.rowid, p.label as label, pa.qty as qty, pa.fk_product_fils as id, p.fk_product_type, pa.incdec";
- $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
- $sql.= ", ".MAIN_DB_PREFIX."product_association as pa";
- $sql.= " WHERE p.rowid = pa.fk_product_fils";
- $sql.= " AND pa.fk_product_pere = ".$id;
- $sql.= " AND pa.fk_product_fils != ".$id; // This should not happens, it is to avoid infinite loop if it happens
- dol_syslog(get_class($this).'::getChildsArbo id='.$id.' level='.$level, LOG_DEBUG);
- if ($level == 1) $alreadyfound=array($id=>1); // We init array of found object to start of tree, so if we found it later (should not happened), we stop immediatly
- // Protection against infinite loop
- if ($level > 30) return array();
- $res = $this->db->query($sql);
- if ($res)
- {
- $prods = array();
- while ($rec = $this->db->fetch_array($res))
- {
- if (! empty($alreadyfound[$rec['rowid']]))
- {
- dol_syslog(get_class($this).'::getChildsArbo the product id='.$rec['rowid'].' was already found at a higher level in tree. We discard to avoid infinite loop', LOG_WARNING);
- continue;
- }
- $alreadyfound[$rec['rowid']]=1;
- $prods[$rec['rowid']]= array(
- 0=>$rec['rowid'],
- 1=>$rec['qty'],
- 2=>$rec['fk_product_type'],
- 3=>$this->db->escape($rec['label']),
- 4=>$rec['incdec']
- );
- //$prods[$this->db->escape($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty'],2=>$rec['fk_product_type']);
- //$prods[$this->db->escape($rec['label'])]= array(0=>$rec['id'],1=>$rec['qty']);
- if (empty($firstlevelonly))
- {
- $listofchilds=$this->getChildsArbo($rec['rowid'], 0, $level + 1);
- foreach($listofchilds as $keyChild => $valueChild)
- {
- $prods[$rec['rowid']]['childs'][$keyChild] = $valueChild;
- }
- }
- }
- return $prods;
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
- /**
- * Return tree of all subproducts for product. Tree contains id, name and quantity.
- * Set this->sousprods
- *
- * @return void
- */
- function get_sousproduits_arbo()
- {
- $parent=array();
- foreach($this->getChildsArbo($this->id) as $keyChild => $valueChild) // Warning. getChildsArbo can call getChildsArbo recursively. Starting point is $value[0]=id of product
- {
- $parent[$this->label][$keyChild] = $valueChild;
- }
- foreach($parent as $key => $value) // key=label, value is array of childs
- {
- $this->sousprods[$key] = $value;
- }
- }
- /**
- * Return clicable link of object (with eventually picto)
- *
- * @param int $withpicto Add picto into link
- * @param string $option Where point the link ('stock', 'composition', 'category', 'supplier', '')
- * @param int $maxlength Maxlength of ref
- * @return string String with URL
- */
- function getNomUrl($withpicto=0,$option='',$maxlength=0)
- {
- global $conf, $langs, $hookmanager;
- include_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
- $result='';
- $newref=$this->ref;
- if ($maxlength) $newref=dol_trunc($newref,$maxlength,'middle');
- if ($this->type == Product::TYPE_PRODUCT) $label = '<u>' . $langs->trans("ShowProduct") . '</u>';
- if ($this->type == Product::TYPE_SERVICE) $label = '<u>' . $langs->trans("ShowService") . '</u>';
- if (! empty($this->ref))
- $label .= '<br><b>' . $langs->trans('ProductRef') . ':</b> ' . $this->ref;
- if (! empty($this->label))
- $label .= '<br><b>' . $langs->trans('ProductLabel') . ':</b> ' . $this->label;
- if ($this->type == Product::TYPE_PRODUCT)
- {
- if ($this->weight) $label.="<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuring_units_string($this->weight_units,"weight");
- if ($this->length) $label.="<br><b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuring_units_string($this->length_units,'length');
- if ($this->surface) $label.="<br><b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuring_units_string($this->surface_units,'surface');
- if ($this->volume) $label.="<br><b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuring_units_string($this->volume_units,'volume');
- if (! empty($conf->productbatch->enabled))
- {
- $label.="<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0,2);
- }
- }
- if ($this->type == Product::TYPE_SERVICE)
- {
- //
- }
- if (! empty($this->entity))
- {
- $tmpphoto = $this->show_photos($conf->product->multidir_output[$this->entity],1,1,0,0,0,80);
- if ($this->nbphoto > 0) $label .= '<br>' . $tmpphoto;
- }
- $linkclose='';
- if (empty($notooltip))
- {
- if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
- {
- $label=$langs->trans("ShowOrder");
- $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
- }
- $linkclose.= ' title="'.dol_escape_htmltag($label, 1, 1).'"';
- $linkclose.=' class="classfortooltip"';
- if (! is_object($hookmanager))
- {
- include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
- $hookmanager=new HookManager($this->db);
- }
- $hookmanager->initHooks(array('productdao'));
- $parameters=array('id'=>$this->id);
- $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
- if ($reshook > 0) $linkclose = $hookmanager->resPrint;
- }
- if ($option == 'supplier' || $option == 'category') {
- $url = DOL_URL_ROOT.'/product/fournisseurs.php?id='.$this->id;
- } else if ($option == 'stock') {
- $url = DOL_URL_ROOT.'/product/stock/product.php?id='.$this->id;
- } else if ($option == 'composition') {
- $url = DOL_URL_ROOT.'/product/composition/card.php?id='.$this->id;
- } else {
- $url = DOL_URL_ROOT.'/product/card.php?id='.$this->id;
- }
- $linkstart = '<a href="'.$url.'"';
- $linkstart.=$linkclose.'>';
- $linkend='</a>';
- if ($withpicto) {
- if ($this->type == Product::TYPE_PRODUCT) $result.=($linkstart.img_object(($notooltip?'':$label), 'product', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
- if ($this->type == Product::TYPE_SERVICE) $result.=($linkstart.img_object(($notooltip?'':$label), 'service', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
- }
- $result.=$linkstart.$newref.$linkend;
- return $result;
- }
- /**
- * Create a document onto disk according to template module.
- *
- * @param string $modele Force model to use ('' to not force)
- * @param Translate $outputlangs Object langs to use for output
- * @param int $hidedetails Hide details of lines
- * @param int $hidedesc Hide description
- * @param int $hideref Hide ref
- * @return int 0 if KO, 1 if OK
- */
- public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
- {
- global $conf,$user,$langs;
- $langs->load("products");
- // Positionne le modele sur le nom du modele a utiliser
- if (! dol_strlen($modele))
- {
- if (! empty($conf->global->PRODUCT_ADDON_PDF))
- {
- $modele = $conf->global->PRODUCT_ADDON_PDF;
- }
- else
- {
- $modele = 'strato';
- }
- }
- $modelpath = "core/modules/product/doc/";
- return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- /**
- * Return label of status of object
- *
- * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
- * @param int $type 0=Sell, 1=Buy, 2=Batch Number management
- * @return string Label of status
- */
- function getLibStatut($mode=0, $type=0)
- {
- switch ($type)
- {
- case 0:
- return $this->LibStatut($this->status,$mode,$type);
- case 1:
- return $this->LibStatut($this->status_buy,$mode,$type);
- case 2:
- return $this->LibStatut($this->status_batch,$mode,$type);
- default:
- //Simulate previous behavior but should return an error string
- return $this->LibStatut($this->status_buy,$mode,$type);
- }
- }
- /**
- * Return label of a given status
- *
- * @param int $status Statut
- * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
- * @param int $type 0=Status "to sell", 1=Status "to buy", 2=Status "to Batch"
- * @return string Label of status
- */
- function LibStatut($status,$mode=0,$type=0)
- {
- global $conf, $langs;
- $langs->load('products');
- if (! empty($conf->productbatch->enabled)) $langs->load("productbatch");
- if ($type == 2)
- {
- switch ($mode)
- {
- case 0:
- return ($status == 0 ? $langs->trans('ProductStatusNotOnBatch') : $langs->trans('ProductStatusOnBatch'));
- case 1:
- return ($status == 0 ? $langs->trans('ProductStatusNotOnBatchShort') : $langs->trans('ProductStatusOnBatchShort'));
- case 2:
- return $this->LibStatut($status,3,2).' '.$this->LibStatut($status,1,2);
- case 3:
- if ($status == 0 )
- {
- return img_picto($langs->trans('ProductStatusNotOnBatch'),'statut5');
- }
- else
- {
- return img_picto($langs->trans('ProductStatusOnBatch'),'statut4');
- }
- case 4:
- return $this->LibStatut($status,3,2).' '.$this->LibStatut($status,0,2);
- case 5:
- return $this->LibStatut($status,1,2).' '.$this->LibStatut($status,3,2);
- default:
- return $langs->trans('Unknown');
- }
- }
- if ($mode == 0)
- {
- if ($status == 0) return ($type==0 ? $langs->trans('ProductStatusNotOnSellShort'):$langs->trans('ProductStatusNotOnBuyShort'));
- if ($status == 1) return ($type==0 ? $langs->trans('ProductStatusOnSellShort'):$langs->trans('ProductStatusOnBuyShort'));
- }
- if ($mode == 1)
- {
- if ($status == 0) return ($type==0 ? $langs->trans('ProductStatusNotOnSell'):$langs->trans('ProductStatusNotOnBuy'));
- if ($status == 1) return ($type==0 ? $langs->trans('ProductStatusOnSell'):$langs->trans('ProductStatusOnBuy'));
- }
- if ($mode == 2)
- {
- if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5', 'class="pictostatus"').' '.($type==0 ? $langs->trans('ProductStatusNotOnSellShort'):$langs->trans('ProductStatusNotOnBuyShort'));
- if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4', 'class="pictostatus"').' '.($type==0 ? $langs->trans('ProductStatusOnSellShort'):$langs->trans('ProductStatusOnBuyShort'));
- }
- if ($mode == 3)
- {
- if ($status == 0) return img_picto(($type==0 ? $langs->trans('ProductStatusNotOnSell') : $langs->trans('ProductStatusNotOnBuy')),'statut5', 'class="pictostatus"');
- if ($status == 1) return img_picto(($type==0 ? $langs->trans('ProductStatusOnSell') : $langs->trans('ProductStatusOnBuy')),'statut4', 'class="pictostatus"');
- }
- if ($mode == 4)
- {
- if ($status == 0) return img_picto($langs->trans('ProductStatusNotOnSell'),'statut5', 'class="pictostatus"').' '.($type==0 ? $langs->trans('ProductStatusNotOnSell'):$langs->trans('ProductStatusNotOnBuy'));
- if ($status == 1) return img_picto($langs->trans('ProductStatusOnSell'),'statut4', 'class="pictostatus"').' '.($type==0 ? $langs->trans('ProductStatusOnSell'):$langs->trans('ProductStatusOnBuy'));
- }
- if ($mode == 5)
- {
- if ($status == 0) return ($type==0 ? $langs->trans('ProductStatusNotOnSellShort'):$langs->trans('ProductStatusNotOnBuyShort')).' '.img_picto(($type==0 ? $langs->trans('ProductStatusNotOnSell'):$langs->trans('ProductStatusNotOnBuy')), 'statut5', 'class="pictostatus"');
- if ($status == 1) return ($type==0 ? $langs->trans('ProductStatusOnSellShort'):$langs->trans('ProductStatusOnBuyShort')).' '.img_picto(($type==0 ? $langs->trans('ProductStatusOnSell'):$langs->trans('ProductStatusOnBuy')),'statut4', 'class="pictostatus"');
- }
- if ($mode == 6)
- {
- if ($status == 0) return ($type==0 ? $langs->trans('ProductStatusNotOnSellShort'):$langs->trans('ProductStatusNotOnBuyShort')).' '.img_picto(($type==0 ? $langs->trans('ProductStatusNotOnSell'):$langs->trans('ProductStatusNotOnBuy')), 'statut5', 'class="pictostatus"');
- if ($status == 1) return ($type==0 ? $langs->trans('ProductStatusOnSellShort'):$langs->trans('ProductStatusOnBuyShort')).' '.img_picto(($type==0 ? $langs->trans('ProductStatusOnSell'):$langs->trans('ProductStatusOnBuy')),'statut4', 'class="pictostatus"');
- }
- return $langs->trans('Unknown');
- }
- /**
- * Retourne le libelle du finished du produit
- *
- * @return string Libelle
- */
- function getLibFinished()
- {
- global $langs;
- $langs->load('products');
- if ($this->finished == '0') return $langs->trans("RowMaterial");
- if ($this->finished == '1') return $langs->trans("Finished");
- return '';
- }
- /**
- * Adjust stock in a warehouse for product
- *
- * @param User $user user asking change
- * @param int $id_entrepot id of warehouse
- * @param double $nbpiece nb of units
- * @param int $movement 0 = add, 1 = remove
- * @param string $label Label of stock movement
- * @param double $price Unit price HT of product, used to calculate average weighted price (PMP in french). If 0, average weighted price is not changed.
- * @param string $inventorycode Inventory code
- * @param string $origin_element Origin element type
- * @param int $origin_id Origin id of element
- * @return int <0 if KO, >0 if OK
- */
- function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $inventorycode='', $origin_element='', $origin_id=null)
- {
- if ($id_entrepot)
- {
- $this->db->begin();
- require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php';
- $op[0] = "+".trim($nbpiece);
- $op[1] = "-".trim($nbpiece);
- $movementstock=new MouvementStock($this->db);
- $movementstock->setOrigin($origin_element, $origin_id);
- $result=$movementstock->_create($user,$this->id,$id_entrepot,$op[$movement],$movement,$price,$label,$inventorycode);
- if ($result >= 0)
- {
- $this->db->commit();
- return 1;
- }
- else
- {
- $this->error=$movementstock->error;
- $this->errors=$movementstock->errors;
- $this->db->rollback();
- return -1;
- }
- }
- }
- /**
- * Adjust stock in a warehouse for product with batch number
- *
- * @param User $user user asking change
- * @param int $id_entrepot id of warehouse
- * @param double $nbpiece nb of units
- * @param int $movement 0 = add, 1 = remove
- * @param string $label Label of stock movement
- * @param double $price Price to use for stock eval
- * @param date $dlc eat-by date
- * @param date $dluo sell-by date
- * @param string $lot Lot number
- * @param string $inventorycode Inventory code
- * @param string $origin_element Origin element type
- * @param int $origin_id Origin id of element
- * @return int <0 if KO, >0 if OK
- */
- function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $dlc='', $dluo='',$lot='', $inventorycode='', $origin_element='', $origin_id=null)
- {
- if ($id_entrepot)
- {
- $this->db->begin();
- require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php';
- $op[0] = "+".trim($nbpiece);
- $op[1] = "-".trim($nbpiece);
- $movementstock=new MouvementStock($this->db);
- $movementstock->setOrigin($origin_element, $origin_id);
- $result=$movementstock->_create($user,$this->id,$id_entrepot,$op[$movement],$movement,$price,$label,$inventorycode,'',$dlc,$dluo,$lot);
- if ($result >= 0)
- {
- $this->db->commit();
- return 1;
- }
- else
- {
- $this->error=$movementstock->error;
- $this->errors=$movementstock->errors;
- $this->db->rollback();
- return -1;
- }
- }
- }
- /**
- * Load information about stock of a product into ->stock_reel, ->stock_warehouse[] (including stock_warehouse[idwarehouse]->detail_batch for batch products)
- * This function need a lot of load. If you use it on list, use a cache to execute it once for each product id.
- * If ENTREPOT_EXTRA_STATUS set, filtering on warehouse status possible.
- *
- * @param string $option '' = Load all stock info, also from closed and internal warehouses,
- * 'nobatch' = Do not load batch information,
- * 'novirtual' = Do not load virtual stock,
- * 'warehouseopen' = Load stock from open warehouses only,
- * 'warehouseclosed' = Load stock from closed warehouses only,
- * 'warehouseinternal' = Load stock from warehouses for internal correction/transfer only
- * @return int < 0 if KO, > 0 if OK
- * @see load_virtual_stock, getBatchInfo
- */
- function load_stock($option='')
- {
- global $conf;
- $this->stock_reel = 0;
- $this->stock_warehouse = array();
- $this->stock_theorique = 0;
- $warehouseStatus = array();
- if (preg_match('/warehouseclosed/', $option))
- {
- $warehouseStatus[] = Entrepot::STATUS_CLOSED;
- }
- if (preg_match('/warehouseopen/', $option))
- {
- $warehouseStatus[] = Entrepot::STATUS_OPEN_ALL;
- }
- if (preg_match('/warehouseinternal/', $option))
- {
- $warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL;
- }
- $sql = "SELECT ps.rowid, ps.reel, ps.fk_entrepot";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps";
- $sql.= ", ".MAIN_DB_PREFIX."entrepot as w";
- $sql.= " WHERE w.entity IN (".getEntity('stock').")";
- $sql.= " AND w.rowid = ps.fk_entrepot";
- $sql.= " AND ps.fk_product = ".$this->id;
- if ($conf->global->ENTREPOT_EXTRA_STATUS && count($warehouseStatus)) $sql.= " AND w.statut IN (".implode(',',$warehouseStatus).")";
- dol_syslog(get_class($this)."::load_stock", LOG_DEBUG);
- $result = $this->db->query($sql);
- if ($result)
- {
- $num = $this->db->num_rows($result);
- $i=0;
- if ($num > 0)
- {
- while ($i < $num)
- {
- $row = $this->db->fetch_object($result);
- $this->stock_warehouse[$row->fk_entrepot] = new stdClass();
- $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
- $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
- if ((! preg_match('/nobatch/', $option)) && $this->hasbatch()) $this->stock_warehouse[$row->fk_entrepot]->detail_batch=Productbatch::findAll($this->db,$row->rowid,1);
- $this->stock_reel+=$row->reel;
- $i++;
- }
- }
- $this->db->free($result);
- if (! preg_match('/novirtual/', $option))
- {
- $this->load_virtual_stock(); // This also load stats_commande_fournisseur, ...
- }
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Load value ->stock_theorique of a product. Property this->id must be defined.
- * This function need a lot of load. If you use it on list, use a cache to execute it one for each product id.
- *
- * @return int < 0 if KO, > 0 if OK
- * @see load_stock, getBatchInfo
- */
- function load_virtual_stock()
- {
- global $conf;
- $stock_commande_client=0;
- $stock_commande_fournisseur=0;
- $stock_sending_client=0;
- $stock_reception_fournisseur=0;
- if (! empty($conf->commande->enabled))
- {
- $result=$this->load_stats_commande(0,'1,2');
- if ($result < 0) dol_print_error($this->db,$this->error);
- $stock_commande_client=$this->stats_commande['qty'];
- }
- if (! empty($conf->expedition->enabled))
- {
- $result=$this->load_stats_sending(0,'1,2');
- if ($result < 0) dol_print_error($this->db,$this->error);
- $stock_sending_client=$this->stats_expedition['qty'];
- }
- if (! empty($conf->fournisseur->enabled))
- {
- $result=$this->load_stats_commande_fournisseur(0,'1,2,3,4');
- if ($result < 0) dol_print_error($this->db,$this->error);
- $stock_commande_fournisseur=$this->stats_commande_fournisseur['qty'];
- $result=$this->load_stats_reception(0,'4');
- if ($result < 0) dol_print_error($this->db,$this->error);
- $stock_reception_fournisseur=$this->stats_reception['qty'];
- }
- // Stock decrease mode
- if (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || ! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
- $this->stock_theorique=$this->stock_reel-$stock_commande_client+$stock_sending_client;
- }
- if (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER)) {
- $this->stock_theorique=$this->stock_reel;
- }
- if (! empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
- $this->stock_theorique=$this->stock_reel-$stock_commande_client;
- }
- // Stock Increase mode
- if (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
- $this->stock_theorique+=$stock_commande_fournisseur-$stock_reception_fournisseur;
- }
- if (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
- $this->stock_theorique-=$stock_reception_fournisseur;
- }
- if (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
- $this->stock_theorique+=$stock_commande_fournisseur-$stock_reception_fournisseur;
- }
- }
- /**
- * Load existing information about a serial
- *
- * @param string $batch Lot/serial number
- * @return array Array with record into product_batch
- * @see load_stock, load_virtual_stock
- */
- function loadBatchInfo($batch)
- {
- $result=array();
- $sql = "SELECT pb.batch, pb.eatby, pb.sellby, SUM(pb.qty) FROM ".MAIN_DB_PREFIX."product_batch as pb, ".MAIN_DB_PREFIX."product_stock as ps";
- $sql.= " WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = ".$this->id." AND pb.batch = '".$this->db->escape($batch)."'";
- $sql.= " GROUP BY pb.batch, pb.eatby, pb.sellby";
- dol_syslog(get_class($this)."::loadBatchInfo load first entry found for lot/serial = ".$batch, LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- $i=0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $result[]=array('batch'=>$batch, 'eatby'=>$this->db->jdate($obj->eatby), 'sellby'=>$this->db->jdate($obj->sellby), 'qty'=>$obj->qty);
- $i++;
- }
- return $result;
- }
- else
- {
- dol_print_error($this->db);
- $this->db->rollback();
- return array();
- }
- }
- /**
- * Move an uploaded file described into $file array into target directory $sdir.
- *
- * @param string $sdir Target directory
- * @param string $file Array of file info of file to upload: array('name'=>..., 'tmp_name'=>...)
- * @return int <0 if KO, >0 if OK
- */
- function add_photo($sdir, $file)
- {
- global $conf;
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- $result = 0;
- $dir = $sdir;
- if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) $dir .= '/'. get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos";
- else $dir .= '/'.get_exdir(0,0,0,0,$this,'product').dol_sanitizeFileName($this->ref);
- dol_mkdir($dir);
- $dir_osencoded=$dir;
- if (is_dir($dir_osencoded))
- {
- $originImage = $dir . '/' . $file['name'];
- // Cree fichier en taille origine
- $result=dol_move_uploaded_file($file['tmp_name'], $originImage, 1);
- if (file_exists(dol_osencode($originImage)))
- {
- // Create thumbs
- $this->addThumbs($originImage);
- }
- }
- if (is_numeric($result) && $result > 0) return 1;
- else return -1;
- }
- /**
- * Return if at least one photo is available
- *
- * @param string $sdir Directory to scan
- * @return boolean True if at least one photo is available, False if not
- */
- function is_photo_available($sdir)
- {
- include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
- include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php';
- global $conf;
- $dir = $sdir;
- if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) $dir .= '/'. get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
- else $dir .= '/'.get_exdir(0,0,0,0,$this,'product').dol_sanitizeFileName($this->ref).'/';
- $nbphoto=0;
- $dir_osencoded=dol_osencode($dir);
- if (file_exists($dir_osencoded))
- {
- $handle=opendir($dir_osencoded);
- if (is_resource($handle))
- {
- while (($file = readdir($handle)) !== false)
- {
- if (! utf8_check($file)) $file=utf8_encode($file); // To be sure data is stored in UTF8 in memory
- if (dol_is_file($dir.$file) && image_format_supported($file) > 0) return true;
- }
- }
- }
- return false;
- }
- /**
- * Show photos of a product (nbmax maximum), into several columns
- * TODO Move this into html.formproduct.class.php
- *
- * @param string $sdir Directory to scan (full absolute path)
- * @param int $size 0=original size, 1='small' use thumbnail if possible
- * @param int $nbmax Nombre maximum de photos (0=pas de max)
- * @param int $nbbyrow Number of image per line or -1 to use div. Used only if size=1.
- * @param int $showfilename 1=Show filename
- * @param int $showaction 1=Show icon with action links (resize, delete)
- * @param int $maxHeight Max height of original image when size='small' (so we can use original even if small requested). If 0, always use 'small' thumb image.
- * @param int $maxWidth Max width of original image when size='small'
- * @param int $nolink Do not add a href link to view enlarged imaged into a new tab
- * @return string Html code to show photo. Number of photos shown is saved in this->nbphoto
- */
- function show_photos($sdir,$size=0,$nbmax=0,$nbbyrow=5,$showfilename=0,$showaction=0,$maxHeight=120,$maxWidth=160,$nolink=0)
- {
- global $conf,$user,$langs;
- include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
- include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php';
- $sortfield='position_name';
- $sortorder='asc';
- $dir = $sdir . '/';
- $pdir = '/';
- $dir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
- $pdir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
- if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
- {
- $dirold .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
- $pdirold .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
- }
- // Defined relative dir to DOL_DATA_ROOT
- $relativedir = '';
- if ($dir)
- {
- $relativedir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $dir);
- $relativedir = preg_replace('/^[\\/]/','',$relativedir);
- $relativedir = preg_replace('/[\\/]$/','',$relativedir);
- }
- $dirthumb = $dir.'thumbs/';
- $pdirthumb = $pdir.'thumbs/';
- $return ='<!-- Photo -->'."\n";
- $nbphoto=0;
- $filearray=dol_dir_list($dir,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
- if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs
- {
- $filearrayold=dol_dir_list($dirold,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
- $filearray=array_merge($filearray, $filearrayold);
- }
- $filearrayindatabase = dol_dir_list_in_database($relativedir, '', null, 'name', SORT_ASC);
- //var_dump($filearray);
- //var_dump($filearrayindatabase);
- // Complete filearray with properties found into $filearrayindatabase
- foreach($filearray as $key => $val)
- {
- $found=0;
- // Search if it exists into $filearrayindatabase
- foreach($filearrayindatabase as $key2 => $val2)
- {
- if ($filearrayindatabase[$key2]['name'] == $filearray[$key]['name'])
- {
- $filearray[$key]['position_name']=($filearrayindatabase[$key2]['position']?$filearrayindatabase[$key2]['position']:'0').'_'.$filearrayindatabase[$key2]['name'];
- $filearray[$key]['position']=$filearrayindatabase[$key2]['position'];
- $filearray[$key]['cover']=$filearrayindatabase[$key2]['cover'];
- $filearray[$key]['acl']=$filearrayindatabase[$key2]['acl'];
- $filearray[$key]['rowid']=$filearrayindatabase[$key2]['rowid'];
- $filearray[$key]['label']=$filearrayindatabase[$key2]['label'];
- $found=1;
- break;
- }
- }
- }
- if (count($filearray))
- {
- if ($sortfield && $sortorder)
- {
- $filearray=dol_sort_array($filearray, $sortfield, $sortorder);
- }
- foreach($filearray as $key => $val)
- {
- $photo='';
- $file = $val['name'];
- //if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory
- //if (dol_is_file($dir.$file) && image_format_supported($file) >= 0)
- if (image_format_supported($file) >= 0)
- {
- $nbphoto++;
- $photo = $file;
- $viewfilename = $file;
- if ($size == 1 || $size == 'small') { // Format vignette
- // Find name of thumb file
- $photo_vignette=basename(getImageFileNameForSize($dir.$file, '_small'));
- if (! dol_is_file($dirthumb.$photo_vignette)) $photo_vignette='';
- // Get filesize of original file
- $imgarray=dol_getImageSize($dir.$photo);
- if ($nbbyrow > 0)
- {
- if ($nbphoto == 1) $return.= '<table width="100%" valign="top" align="center" border="0" cellpadding="2" cellspacing="2">';
- if ($nbphoto % $nbbyrow == 1) $return.= '<tr align=center valign=middle border=1>';
- $return.= '<td width="'.ceil(100/$nbbyrow).'%" class="photo">';
- }
- else if ($nbbyrow < 0) $return .= '<div class="inline-block">';
- $return.= "\n";
- $relativefile=preg_replace('/^\//', '', $pdir.$photo);
- if (empty($nolink))
- {
- $urladvanced=getAdvancedPreviewUrl('product', $relativefile, 0, 'entity='.$this->entity);
- if ($urladvanced) $return.='<a href="'.$urladvanced.'">';
- else $return.= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" class="aphoto" target="_blank">';
- }
- // Show image (width height=$maxHeight)
- // Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine
- $alt=$langs->transnoentitiesnoconv('File').': '.$relativefile;
- $alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height'];
- if (empty($maxHeight) || $photo_vignette && $imgarray['height'] > $maxHeight)
- {
- $return.= '<!-- Show thumb -->';
- $return.= '<img class="photo photowithmargin" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdirthumb.$photo_vignette).'" title="'.dol_escape_htmltag($alt).'">';
- }
- else {
- $return.= '<!-- Show original file -->';
- $return.= '<img class="photo photowithmargin" border="0" height="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" title="'.dol_escape_htmltag($alt).'">';
- }
- if (empty($nolink)) $return.= '</a>';
- $return.="\n";
- if ($showfilename) $return.= '<br>'.$viewfilename;
- if ($showaction)
- {
- $return.= '<br>';
- // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
- if ($photo_vignette && (image_format_supported($photo) > 0) && ($this->imgWidth > $maxWidth || $this->imgHeight > $maxHeight))
- {
- $return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&action=addthumb&file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').' </a>';
- }
- if ($user->rights->produit->creer || $user->rights->service->creer)
- {
- // Link to resize
- $return.= '<a href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode('produit|service').'&id='.$this->id.'&file='.urlencode($pdir.$viewfilename).'" title="'.dol_escape_htmltag($langs->trans("Resize")).'">'.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'</a> ';
- // Link to delete
- $return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&action=delete&file='.urlencode($pdir.$viewfilename).'">';
- $return.= img_delete().'</a>';
- }
- }
- $return.= "\n";
- if ($nbbyrow > 0)
- {
- $return.= '</td>';
- if (($nbphoto % $nbbyrow) == 0) $return.= '</tr>';
- }
- else if ($nbbyrow < 0) $return.='</div>';
- }
- if (empty($size)) { // Format origine
- $return.= '<img class="photo photowithmargin" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'">';
- if ($showfilename) $return.= '<br>'.$viewfilename;
- if ($showaction)
- {
- if ($user->rights->produit->creer || $user->rights->service->creer)
- {
- // Link to resize
- $return.= '<a href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode('produit|service').'&id='.$this->id.'&file='.urlencode($pdir.$viewfilename).'" title="'.dol_escape_htmltag($langs->trans("Resize")).'">'.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'</a> ';
- // Link to delete
- $return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&action=delete&file='.urlencode($pdir.$viewfilename).'">';
- $return.= img_delete().'</a>';
- }
- }
- }
- // On continue ou on arrete de boucler ?
- if ($nbmax && $nbphoto >= $nbmax) break;
- }
- }
- if ($size==1 || $size=='small')
- {
- if ($nbbyrow > 0)
- {
- // Ferme tableau
- while ($nbphoto % $nbbyrow)
- {
- $return.= '<td width="'.ceil(100/$nbbyrow).'%"> </td>';
- $nbphoto++;
- }
- if ($nbphoto) $return.= '</table>';
- }
- }
- }
- $this->nbphoto = $nbphoto;
- return $return;
- }
- /**
- * Retourne tableau de toutes les photos du produit
- *
- * @param string $dir Repertoire a scanner
- * @param int $nbmax Nombre maximum de photos (0=pas de max)
- * @return array Tableau de photos
- */
- function liste_photos($dir,$nbmax=0)
- {
- include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
- $nbphoto=0;
- $tabobj=array();
- $dir_osencoded=dol_osencode($dir);
- $handle=@opendir($dir_osencoded);
- if (is_resource($handle))
- {
- while (($file = readdir($handle)) !== false)
- {
- if (! utf8_check($file)) $file=utf8_encode($file); // readdir returns ISO
- if (dol_is_file($dir.$file) && image_format_supported($file) >= 0)
- {
- $nbphoto++;
- // On determine nom du fichier vignette
- $photo=$file;
- $photo_vignette='';
- if (preg_match('/('.$this->regeximgext.')$/i', $photo, $regs))
- {
- $photo_vignette=preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0];
- }
- $dirthumb = $dir.'thumbs/';
- // Objet
- $obj=array();
- $obj['photo']=$photo;
- if ($photo_vignette && dol_is_file($dirthumb.$photo_vignette)) $obj['photo_vignette']='thumbs/' . $photo_vignette;
- else $obj['photo_vignette']="";
- $tabobj[$nbphoto-1]=$obj;
- // On continue ou on arrete de boucler ?
- if ($nbmax && $nbphoto >= $nbmax) break;
- }
- }
- closedir($handle);
- }
- return $tabobj;
- }
- /**
- * Efface la photo du produit et sa vignette
- *
- * @param string $file Chemin de l'image
- * @return void
- */
- function delete_photo($file)
- {
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
- $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
- $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
- $filename = preg_replace('/'.preg_quote($dir,'/').'/i','',$file); // Nom du fichier
- // On efface l'image d'origine
- dol_delete_file($file, 0, 0, 0, $this); // For triggers
- // Si elle existe, on efface la vignette
- if (preg_match('/('.$this->regeximgext.')$/i',$filename,$regs))
- {
- $photo_vignette=preg_replace('/'.$regs[0].'/i','',$filename).'_small'.$regs[0];
- if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
- {
- dol_delete_file($dirthumb.$photo_vignette);
- }
- $photo_vignette=preg_replace('/'.$regs[0].'/i','',$filename).'_mini'.$regs[0];
- if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
- {
- dol_delete_file($dirthumb.$photo_vignette);
- }
- }
- }
- /**
- * Load size of image file
- *
- * @param string $file Path to file
- * @return void
- */
- function get_image_size($file)
- {
- $file_osencoded=dol_osencode($file);
- $infoImg = getimagesize($file_osencoded); // Get information on image
- $this->imgWidth = $infoImg[0]; // Largeur de l'image
- $this->imgHeight = $infoImg[1]; // Hauteur de l'image
- }
- /**
- * Load indicators this->nb for the dashboard
- *
- * @return int <0 if KO, >0 if OK
- */
- function load_state_board()
- {
- global $conf, $user, $hookmanager;
- $this->nb=array();
- $sql = "SELECT count(p.rowid) as nb, fk_product_type";
- $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
- $sql.= ' WHERE p.entity IN ('.getEntity($this->element, 1).')';
- // Add where from hooks
- if (is_object($hookmanager))
- {
- $parameters=array();
- $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
- $sql.=$hookmanager->resPrint;
- }
- $sql.= ' GROUP BY fk_product_type';
- $resql=$this->db->query($sql);
- if ($resql)
- {
- while ($obj=$this->db->fetch_object($resql))
- {
- if ($obj->fk_product_type == 1) $this->nb["services"]=$obj->nb;
- else $this->nb["products"]=$obj->nb;
- }
- $this->db->free($resql);
- return 1;
- }
- else
- {
- dol_print_error($this->db);
- $this->error=$this->db->error();
- return -1;
- }
- }
- /**
- * Return if object is a product
- *
- * @return boolean True if it's a product
- */
- function isProduct()
- {
- return ($this->type == Product::TYPE_PRODUCT ? true : false);
- }
- /**
- * Return if object is a product
- *
- * @return boolean True if it's a service
- */
- function isService()
- {
- return ($this->type == Product::TYPE_SERVICE ? true : false);
- }
- /**
- * Get a barcode from the module to generate barcode values.
- * Return value is stored into this->barcode
- *
- * @param Product $object Object product or service
- * @param string $type Barcode type (ean, isbn, ...)
- * @return void
- */
- function get_barcode($object,$type='')
- {
- global $conf;
- $result='';
- if (! empty($conf->global->BARCODE_PRODUCT_ADDON_NUM))
- {
- $dirsociete=array_merge(array('/core/modules/barcode/'),$conf->modules_parts['barcode']);
- foreach ($dirsociete as $dirroot)
- {
- $res=dol_include_once($dirroot.$conf->global->BARCODE_PRODUCT_ADDON_NUM.'.php');
- if ($res) break;
- }
- $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
- $mod = new $var;
- $result=$mod->getNextValue($object,$type);
- dol_syslog(get_class($this)."::get_barcode barcode=".$result." module=".$var);
- }
- return $result;
- }
- /**
- * Initialise an instance with random values.
- * Used to build previews or test instances.
- * id must be 0 if object instance is a specimen.
- *
- * @return void
- */
- function initAsSpecimen()
- {
- global $user,$langs,$conf,$mysoc;
- $now=dol_now();
- // Initialize parameters
- $this->specimen=1;
- $this->id=0;
- $this->ref = 'PRODUCT_SPEC';
- $this->label = 'PRODUCT SPECIMEN';
- $this->description = 'This is description of this product specimen that was created the '.dol_print_date($now,'dayhourlog').'.';
- $this->specimen=1;
- $this->country_id=1;
- $this->tosell=1;
- $this->tobuy=1;
- $this->tobatch=0;
- $this->note='This is a comment (private)';
- $this->date_creation = $now;
- $this->date_modification = $now;
- $this->weight = 4;
- $this->weight_unit = 1;
- $this->length = 5;
- $this->length_unit = 1;
- $this->width = 6;
- $this->width_unit = 0;
- $this->height = null;
- $this->height_unit = null;
- $this->surface = 30;
- $this->surface_unit = 0;
- $this->volume = 300;
- $this->volume_unit = 0;
- $this->barcode=-1; // Create barcode automatically
- }
- /**
- * Returns the text label from units dictionary
- *
- * @param string $type Label type (long or short)
- * @return string|int <0 if ko, label if ok
- */
- function getLabelOfUnit($type='long')
- {
- global $langs;
- if (!$this->fk_unit) {
- return '';
- }
- $langs->load('products');
- $this->db->begin();
- $label_type = 'label';
- if ($type == 'short')
- {
- $label_type = 'short_label';
- }
- $sql = 'select '.$label_type.' from '.MAIN_DB_PREFIX.'c_units where rowid='.$this->fk_unit;
- $resql = $this->db->query($sql);
- if($resql && $this->db->num_rows($resql) > 0)
- {
- $res = $this->db->fetch_array($resql);
- $label = $res[$label_type];
- $this->db->free($resql);
- return $label;
- }
- else
- {
- $this->error=$this->db->error().' sql='.$sql;
- dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
- return -1;
- }
- }
- /**
- * Return if object has a sell-by date or eat-by date
- *
- * @return boolean True if it's has
- */
- function hasbatch()
- {
- return ($this->status_batch == 1 ? true : false);
- }
- /**
- * Return minimum product recommended price
- *
- * @return int Minimum recommanded price that is higher price among all suppliers * PRODUCT_MINIMUM_RECOMMENDED_PRICE
- */
- function min_recommended_price()
- {
- global $conf;
- $maxpricesupplier=0;
- if (! empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
- {
- require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
- $product_fourn = new ProductFournisseur($this->db);
- $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->id, '', '');
- if (is_array($product_fourn_list) && count($product_fourn_list)>0)
- {
- foreach($product_fourn_list as $productfourn)
- {
- if ($productfourn->fourn_unitprice > $maxpricesupplier)
- {
- $maxpricesupplier = $productfourn->fourn_unitprice;
- }
- }
- $maxpricesupplier *= $conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE;
- }
- }
- return $maxpricesupplier;
- }
- /**
- * Sets object to supplied categories.
- *
- * Deletes object from existing categories not supplied.
- * Adds it to non existing supplied categories.
- * Existing categories are left untouch.
- *
- * @param int[]|int $categories Category or categories IDs
- */
- public function setCategories($categories) {
- // Handle single category
- if (! is_array($categories)) {
- $categories = array($categories);
- }
- // Get current categories
- require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
- $c = new Categorie($this->db);
- $existing = $c->containing($this->id, Categorie::TYPE_PRODUCT, 'id');
- // Diff
- if (is_array($existing)) {
- $to_del = array_diff($existing, $categories);
- $to_add = array_diff($categories, $existing);
- } else {
- $to_del = array(); // Nothing to delete
- $to_add = $categories;
- }
- // Process
- foreach($to_del as $del) {
- if ($c->fetch($del) > 0) {
- $c->del_type($this, 'product');
- }
- }
- foreach ($to_add as $add) {
- if ($c->fetch($add) > 0) {
- $c->add_type($this, 'product');
- }
- }
- return;
- }
- /**
- * Function used to replace a thirdparty id with another one.
- *
- * @param DoliDB $db Database handler
- * @param int $origin_id Old thirdparty id
- * @param int $dest_id New thirdparty id
- * @return bool
- */
- public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
- {
- $tables = array(
- 'product_customer_price',
- 'product_customer_price_log'
- );
- return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
- }
- /**
- * Generates prices for a product based on product multiprice generation rules
- *
- * @param User $user User that updates the prices
- * @param float $baseprice Base price
- * @param string $price_type Base price type
- * @param float $price_vat VAT % tax
- * @param int $npr NPR
- * @param string $psq ¿?
- * @return int -1 KO, 1 OK
- */
- public function generateMultiprices(User $user, $baseprice, $price_type, $price_vat, $npr, $psq)
- {
- global $conf, $db;
- $sql = "SELECT rowid, level, fk_level, var_percent, var_min_percent FROM ".MAIN_DB_PREFIX."product_pricerules";
- $query = $db->query($sql);
- $rules = array();
- while ($result = $db->fetch_object($query)) {
- $rules[$result->level] = $result;
- }
- //Because prices can be based on other level's prices, we temporarily store them
- $prices = array(
- 1 => $baseprice
- );
- for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
- $price = $baseprice;
- $price_min = $baseprice;
- //We have to make sure it does exist and it is > 0
- //First price level only allows changing min_price
- if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
- $price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent/100));
- }
- $prices[$i] = $price;
- //We have to make sure it does exist and it is > 0
- if (isset($rules[$i]->var_min_percent) && $rules[$i]->var_min_percent) {
- $price_min = $price * (1 - ($rules[$i]->var_min_percent/100));
- }
- //Little check to make sure the price is modified before triggering generation
- $check_amount = (($price == $this->multiprices[$i]) && ($price_min == $this->multiprices_min[$i]));
- $check_type = ($baseprice == $this->multiprices_base_type[$i]);
- if ($check_amount && $check_type) {
- continue;
- }
- if ($this->updatePrice($price, $price_type, $user, $price_vat, $price_min, $i, $npr, $psq, true) < 0) {
- return -1;
- }
- }
- return 1;
- }
- /**
- * Returns the rights used for this class
- * @return stdClass
- */
- public function getRights()
- {
- global $user;
- if ($this->isProduct()) {
- return $user->rights->produit;
- } else {
- return $user->rights->service;
- }
- }
- /**
- * Load information for tab info
- *
- * @param int $id Id of thirdparty to load
- * @return void
- */
- function info($id)
- {
- $sql = "SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification,";
- $sql.= " p.fk_user_author, p.fk_user_modif";
- $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as p";
- $sql.= " WHERE p.rowid = ".$id;
- $result=$this->db->query($sql);
- if ($result)
- {
- if ($this->db->num_rows($result))
- {
- $obj = $this->db->fetch_object($result);
- $this->id = $obj->rowid;
- if ($obj->fk_user_author) {
- $cuser = new User($this->db);
- $cuser->fetch($obj->fk_user_author);
- $this->user_creation = $cuser;
- }
- if ($obj->fk_user_modif) {
- $muser = new User($this->db);
- $muser->fetch($obj->fk_user_modif);
- $this->user_modification = $muser;
- }
- $this->ref = $obj->ref;
- $this->date_creation = $this->db->jdate($obj->date_creation);
- $this->date_modification = $this->db->jdate($obj->date_modification);
- }
- $this->db->free($result);
- }
- else
- {
- dol_print_error($this->db);
- }
- }
- }
|