<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — script based price conversion]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=2496&amp;type=atom" />
	<updated>2011-11-13T18:07:45Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=2496</id>
		<entry>
			<title type="html"><![CDATA[Re: script based price conversion]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=10703#p10703" />
			<content type="html"><![CDATA[<p>Tom,<br />yes there are.</p><p>unfortunately, that was the only way of being able to keep prices in EUR (or USD), but invoice them in base currency - in my case RON.</p><p>if you have a better ideea, I am willing to implement</p><p>more, I would like to make quotes/SO in EUR (or USD), but make the invoice in RON.<br />I couldn&#039;t figure out how.</p><p>Regards,<br />S</p>]]></content>
			<author>
				<name><![CDATA[serbanc]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=10111</uri>
			</author>
			<updated>2011-11-13T18:07:45Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=10703#p10703</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: script based price conversion]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=10700#p10700" />
			<content type="html"><![CDATA[<p>I may be missing something, but aren&#039;t there hooks already for maintaining the current Exchange Rate from your base currency (EUR) and others (like USD)</p><p>tom</p>]]></content>
			<author>
				<name><![CDATA[tom]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=331</uri>
			</author>
			<updated>2011-11-13T13:50:28Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=10700#p10700</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: script based price conversion]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=10668#p10668" />
			<content type="html"><![CDATA[<p>If this module turns out to be popular, we can put it in the repository. Thanks.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2011-11-08T07:47:42Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=10668#p10668</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: script based price conversion]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=10667#p10667" />
			<content type="html"><![CDATA[<p>- in /modules/cronjobs add:<br /><strong> rec_price.php</strong><br />&lt;?php</p><p>$page_security = &#039;SA_SALESPRICE&#039;;<br />$path_to_root = &quot;../..&quot;;</p><br /><p>define(&quot;_CJ&quot;, 1); // needed in hooks.inc in order to skip authentication<br />define(&quot;_CJ_USER&quot;, &quot;cron&quot;);<br />define(&quot;_CJ_COY&quot;, &#039;1&#039;);</p><br /><p>$_POST[&quot;company_login_name&quot;] = _CJ_COY;</p><p>include_once($path_to_root . &quot;/includes/session.inc&quot;);</p><br /><br /><br /><br /><br /><p>include_once($path_to_root . &quot;/sales/includes/sales_db.inc&quot;);<br />include_once($path_to_root . &quot;/sales/includes/db/sales_types_db.inc&quot;);<br />include_once($path_to_root . &quot;/inventory/includes/inventory_db.inc&quot;);</p><p>//required for working w/ currencies<br />include_once($path_to_root . &quot;/gl/includes/db/gl_db_currencies.inc&quot;);<br />include_once($path_to_root . &quot;/gl/includes/db/gl_db_rates.inc&quot;);</p><br /><p>function get_last_exchange_rate($curr_abrev) {<br />&nbsp; &nbsp; $sql = &quot;SELECT rate_buy FROM &quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; .TB_PREF.&quot;exchange_rates &quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; .&quot;WHERE curr_code=&quot;.db_escape($curr_abrev).&quot; ORDER BY date_ DESC limit 1&quot;;</p><p>&nbsp; &nbsp; $result = db_query($sql, &quot;could not get last exchange rate for $curr_abrev &quot;);</p><p>&nbsp; &nbsp; if(db_num_rows($result) == 0) <br />&nbsp; &nbsp; return 0;</p><p>&nbsp; &nbsp; $row = db_fetch($result);<br />&nbsp; &nbsp; return $row[0];<br />}</p><p>// get a list of all currencies and their latest exchange rates<br />$curr_from = array();<br />$curr_base = &#039;&#039;;</p><p>$cs = get_currencies();<br />while($c = db_fetch($cs)) {<br />&nbsp; &nbsp;if (is_company_currency($c[&#039;curr_abrev&#039;])) {<br />&nbsp; &nbsp; //is base currency<br />&nbsp; &nbsp; $curr_base = $c[&#039;curr_abrev&#039;];<br />//&nbsp; &nbsp; echo &quot;base = $curr_base\n&quot;;<br />&nbsp; &nbsp;} else {<br />&nbsp; &nbsp; // get the latest exchange rate for this currency<br />&nbsp; &nbsp; $r = get_last_exchange_rate($c[&#039;curr_abrev&#039;]);<br />&nbsp; &nbsp; if ($r == 0) {<br />&nbsp; &nbsp; &nbsp; &nbsp; echo &#039;There is no exchange rate for currency: &#039; . $c[&#039;curr_abrev&#039;];<br />&nbsp; &nbsp; &nbsp; &nbsp; exit();<br />&nbsp; &nbsp; } else {<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$curr_from[$c[&#039;curr_abrev&#039;]] = $r;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;echo &quot;added &quot; . $c[&#039;curr_abrev&#039;] . &quot;\n&quot;;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp;}<br />}</p><br /><br /><p>// get all the items<br />$items = get_items();</p><p>while ($item = db_fetch($items)) {<br />&nbsp; &nbsp; echo &quot;------------------------------------------------------------------------------------\n&quot;;<br />&nbsp; &nbsp; echo &quot;processing art code=&quot; . $item[&#039;stock_id&#039;] . &quot; name=&quot; . $item[&#039;description&#039;] . &quot;\n&quot;;<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; // get prices for channels<br />&nbsp; &nbsp; $prices = array();<br />&nbsp; &nbsp; $prices_list = get_prices($item[&#039;stock_id&#039;]);<br />&nbsp; &nbsp; while ($myrow = db_fetch($prices_list)) {<br />&nbsp; &nbsp; echo &quot;adding price for channel id=&quot; . $myrow[&#039;sales_type_id&#039;] . &quot; currency=&quot; . $myrow[&#039;curr_abrev&#039;] . &quot;\n&quot;;<br />&nbsp; &nbsp; $prices[$myrow[&#039;sales_type_id&#039;]][$myrow[&#039;curr_abrev&#039;]] = array(&#039;price_id&#039;=&gt;$myrow[&#039;id&#039;], &#039;price&#039;=&gt;$myrow[&#039;price&#039;]);<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; //foreach sales type (aka channel) perform price count check<br />&nbsp; &nbsp; foreach($prices as $sales_type_id=&gt;$price_per_sale_type) {<br />&nbsp; &nbsp; &nbsp; &nbsp; echo &quot;checking pricing for channel id=$sales_type_id\n&quot;;<br />&nbsp; &nbsp; //check currency prices usage (count)<br />&nbsp; &nbsp; $count = 0;<br />&nbsp; &nbsp; foreach($curr_from as $c=&gt;$r) {<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if (isset($price_per_sale_type[$c]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count++;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; echo &quot;currency usage count =$count\n&quot;;</p><p>&nbsp; &nbsp; if ($count == 0) {<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;echo &quot;this article has no defined price for any of the foreign currencies in this channel. no processing\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;continue;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; if ($count &gt; 1) {<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;echo &quot;this article has prices defined for more than one foreign currency in this channel. no processing\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;continue;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; echo &quot;creating or updating price for base currency $curr_base\n&quot;;<br />&nbsp; &nbsp; //determine currency for this price<br />&nbsp; &nbsp; $price_c = &#039;&#039;;<br />&nbsp; &nbsp; foreach($curr_from as $c=&gt;$r) {<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if (isset($price_per_sale_type[$c]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price_c = $c;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; echo &quot;price is expressed in $price_c\n&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; $price_in_base_curr = $price_per_sale_type[$price_c][&#039;price&#039;] * $curr_from[$price_c];<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; </p><p>//&nbsp; &nbsp; print_r($price_per_sale_type);<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; if (isset($price_per_sale_type[$curr_base])) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;price id is&quot; . $price_per_sale_type[$curr_base][&#039;price_id&#039;] . &quot;\n&quot;;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;echo &quot;This article has a price in $curr_base. update it. price=$price_in_base_curr\n&quot;;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;update_item_price($price_per_sale_type[$curr_base][&#039;price_id&#039;], $sales_type_id, $curr_base, $price_in_base_curr);<br />&nbsp; &nbsp; &nbsp; &nbsp; } else {<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;echo &quot;This article does not have a price in $curr_base. create it. price=$price_in_base_curr\n&quot;;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;add_item_price($item[&#039;stock_id&#039;], $sales_type_id, $curr_base, $price_in_base_curr);<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; echo &quot;------------------------------------------------------------------------------------\n&quot;;</p><p>}<br />?&gt;</p><p><strong> setup </strong><br />- create a user (eg cron) and assign the proper rights for operation<br />- change the user in define(&#039;_CJ_USER&#039;,.......<br />- run the script via php-cli (php rec_price.php)</p><p><strong>Notes</strong><br />- this is very very alpha code - use it with care<br />- most probably you do need such price updates, so use these files as skeletons for developing your own command line scripts <img src="https://frontaccounting.com/punbb/img/smilies/smile.png" width="15" height="15" alt="smile" /></p>]]></content>
			<author>
				<name><![CDATA[serbanc]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=10111</uri>
			</author>
			<updated>2011-11-07T21:00:18Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=10667#p10667</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[script based price conversion]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=10666#p10666" />
			<content type="html"><![CDATA[<p>Since I need to keep prices in EUR/USD and to make invoices in other currency, I created a php script (that can be added to a crontab) that calculates the prices in base currency, based on the prices in other currencies.</p><p>how to install (by hand)<br />- create a folder in /modules/cronjobs<br />- in /installed_extensions.php add:<br />XX =&gt; array(<br />&nbsp; &#039;name&#039; =&gt; &#039;Cronjobs&#039;,<br />&nbsp; &#039;active&#039; =&gt; true,<br />&nbsp; &#039;package&#039; =&gt; &#039;cronjobs&#039;,<br />&nbsp; &#039;type&#039; =&gt; &#039;extension&#039;,<br />&nbsp; &#039;version&#039; =&gt; &#039;0.1&#039;,<br />&nbsp; &#039;path&#039; =&gt; &#039;modules/cronjobs&#039;,<br />),</p><p>where XX is the next extension id - do not forget to increment $next_extension_id</p><p>- in /modules/cronjobs add:<br /><strong>hooks.php</strong><br />&lt;?php<br />class hooks_cronjobs extends hooks {<br />&nbsp; &nbsp; var $module_name = &#039;cronjobs&#039;;<br />&nbsp; &nbsp; public function __construct() {<br />&nbsp; &nbsp; &nbsp; &nbsp; global $_SESSION;<br />&nbsp; &nbsp; &nbsp; &nbsp; // if _CJ is defined, login user non interactively<br />&nbsp; &nbsp; &nbsp; &nbsp; if (_CJ==1) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[&#039;wa_current_user&#039;]-&gt;username = _CJ_USER;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[&#039;wa_current_user&#039;]-&gt;loginname = _CJ_USER;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[&#039;wa_current_user&#039;]-&gt;name = _CJ_USER;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[&#039;wa_current_user&#039;]-&gt;set_company(_CJ_COY);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[&#039;wa_current_user&#039;]-&gt;logged = true;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />}<br />?&gt;<br />--- continued on next post</p>]]></content>
			<author>
				<name><![CDATA[serbanc]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=10111</uri>
			</author>
			<updated>2011-11-07T20:51:51Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=10666#p10666</id>
		</entry>
</feed>
