Skip to content

Commit

Permalink
feat(stock value report) change currency
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremielodi committed Jan 14, 2019
1 parent bcab92b commit 3a1a450
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
Expand Up @@ -42,6 +42,7 @@ function StockValueConfigController($sce, Notify, SavedReports, AppCache, report
const params = {
depot_uuid : vm.depot.uuid,
dateTo : vm.dateTo,
currency_id : vm.currency_id,
};

// update cached configuration
Expand Down
Expand Up @@ -40,6 +40,11 @@ <h3 translate>REPORT.STOCK_VALUE.TITLE</h3>
on-change="ReportConfigCtrl.onDateChange(date)">
</bh-date-editor>

<!-- the currency to be used in the footer -->
<bh-currency-select
currency-id="ReportConfigCtrl.currency_id">
</bh-currency-select>

<!-- preview -->
<bh-loading-button loading-state="ConfigForm.$loading">
<span translate>REPORT.UTIL.PREVIEW</span>
Expand Down
8 changes: 6 additions & 2 deletions server/controllers/stock/reports/stock/value.js
@@ -1,3 +1,5 @@
const Exchange = require('../../../finance/exchange');

const {
_, db, ReportManager, pdfOptions, STOCK_VALUE_REPORT_TEMPLATE,
} = require('../common');
Expand All @@ -13,6 +15,7 @@ const {
*/
function stockValue(req, res, next) {
const data = {};
const enterpriseId = req.session.enterprise.id;
let options;
let report;

Expand All @@ -30,14 +33,15 @@ function stockValue(req, res, next) {
data.dateTo = options.dateTo;
return db.one('SELECT * FROM depot WHERE uuid=?', [db.bid(options.depot_uuid)]).then(depot => {
data.depot = depot;
return db.exec('CALL stockValue(?, ?);', [db.bid(options.depot_uuid), options.dateTo]);
return db.exec('CALL stockValue(?,?,?);', [db.bid(options.depot_uuid), options.dateTo, options.currency_id]);
})
.then((stockValues) => {
data.stockValues = stockValues[0] || [];

const stokTolal = stockValues[1][0] || {};
data.stocktotal = stokTolal.total;
data.emptyResult = data.stockValues.length === 0;
data.rate = Exchange.getExchangeRate(enterpriseId, options.currency_id, new Date());
data.currency_id = options.currency_id;
return report.render(data);
})
.then((result) => {
Expand Down
Expand Up @@ -40,7 +40,7 @@
<td style="width:50%">{{inventory_name}}</td>
<td class="text-right">{{stockQtt}}</td>
<td class="text-right">{{stockUnitCost}}</td>
<td class="text-right">{{currency stockValue ../../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{currency stockValue ../currency_id}}</td>
</tr>
{{/each}}
<!-- no data -->
Expand All @@ -49,7 +49,7 @@
{{else}}
<tr>
<th colspan="3" class="text-right">{{ translate "FORM.LABELS.TOTAL"}}</th>
<th class="text-right">{{ currency stocktotal ../metadata.enterprise.currency_id }}</th>
<th class="text-right">{{ currency stocktotal ./currency_id }}</th>
</tr>
{{/if}}

Expand Down
18 changes: 12 additions & 6 deletions server/models/procedures/stock.sql
Expand Up @@ -430,20 +430,23 @@ SELECT * FROM stage_movement;

END$$


/* retrieve the stock status( current qtt, unit_cost, value) for each inventory in a depot */
DROP PROCEDURE IF EXISTS `stockValue`$$

CREATE PROCEDURE `stockValue`(IN _depot_uuid BINARY(16), IN _dateTo DATE)
CREATE PROCEDURE `stockValue`(
IN _depot_uuid BINARY(16),
IN _dateTo DATE,
IN _currency_id INT
)
BEGIN
DECLARE done BOOLEAN;
DECLARE mvtIsExit, mvtQtt, mvtUnitCost, mvtValue DECIMAL(19, 4);
DECLARE newQuantity, newValue, newCost DECIMAL(19, 4);
DECLARE newQuantity, newValue, newCost, exchangeRate DECIMAL(19, 4);
DECLARE stockQtt, stockUnitCost, stockValue DECIMAL(19, 4);
DECLARE _documentReference VARCHAR(100);
DECLARE _date DATETIME;
DECLARE _inventory_uuid BINARY(16);
DECLARE _iteration, _newStock INT;
DECLARE _iteration, _newStock, _enterprise_id INT;


DECLARE curs1 CURSOR FOR
Expand Down Expand Up @@ -473,7 +476,9 @@ BEGIN
stockValue DECIMAL(19, 4),
iteration INT
);


SET _enterprise_id = (SELECT enterprise_id FROM depot WHERE uuid= _depot_uuid);
SET exchangeRate = IFNULL(GetExchangeRate(_enterprise_id,_currency_id ,_dateTo), 1);

OPEN curs1;
read_loop: LOOP
Expand All @@ -499,7 +504,8 @@ BEGIN
SET stockValue = 0;
SET _iteration = 0;
END IF;


SET mvtUnitCost = mvtUnitCost*(exchangeRate);
-- stock exit movement, the stock quantity decreases
IF mvtIsExit = 1 THEN
SET stockQtt = stockQtt - mvtQtt;
Expand Down

0 comments on commit 3a1a450

Please sign in to comment.