| 1 |
import { reactive } from "@odoo/owl"; |
| 2 |
import { useService } from "@web/core/utils/hooks"; |
| 3 |
import { serializeDate, serializeDateTime } from "@web/core/l10n/dates"; |
| 4 |
const { DateTime } = luxon; |
| 5 |
|
| 6 |
|
| 7 |
export class StockValuationReportController { |
| 8 |
constructor(action) { |
| 9 |
this.action = action; |
| 10 |
this.actionService = useService("action"); |
| 11 |
this.dialog = useService("dialog"); |
| 12 |
this.orm = useService("orm"); |
| 13 |
this.state = reactive({ |
| 14 |
date: DateTime.now(), |
| 15 |
}); |
| 16 |
} |
| 17 |
|
| 18 |
async load() { |
| 19 |
await this.loadReportData(); |
| 20 |
this.currencyId = this.data.currency_id; |
| 21 |
this.companyId = this.data.company_id; |
| 22 |
} |
| 23 |
|
| 24 |
async loadReportData() { |
| 25 |
const kwargs = { |
| 26 |
date: this.state.date.toISODate() || false, |
| 27 |
}; |
| 28 |
const res = await this.orm.call( |
| 29 |
"stock_account.stock.valuation.report", |
| 30 |
"get_report_values", |
| 31 |
[], |
| 32 |
kwargs |
| 33 |
); |
| 34 |
this.data = res.data; |
| 35 |
|
| 36 |
if (this.data.inventory_loss) { |
| 37 |
for (const line of this.data.inventory_loss.lines) { |
| 38 |
line.account = this.data.accounts_by_id[line.account_id]; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
for (const line of this.data.stock_variation.lines) { |
| 43 |
line.account = this.data.accounts_by_id[line.account_id]; |
| 44 |
} |
| 45 |
|
| 46 |
this.data.initial_balance.lines = []; |
| 47 |
this.data.initial_balance.accounts = []; |
| 48 |
for (let [accountId, data] of Object.entries(this.data.initial_balance.lines_by_account_id)) { |
| 49 |
const account = this.data.accounts_by_id[accountId]; |
| 50 |
this.data.initial_balance.lines.push({ |
| 51 |
label: account.display_name, |
| 52 |
value: data.value, |
| 53 |
account_id: accountId, |
| 54 |
}); |
| 55 |
this.data.initial_balance.accounts.push(...data.accounts); |
| 56 |
} |
| 57 |
|
| 58 |
this.data.ending_stock.lines = []; |
| 59 |
this.data.ending_stock.accounts = []; |
| 60 |
for (let [accountId, data] of Object.entries(this.data.ending_stock.lines_by_account_id)) { |
| 61 |
const account = this.data.accounts_by_id[accountId]; |
| 62 |
this.data.ending_stock.lines.push({ |
| 63 |
label: account?.display_name, |
| 64 |
value: data.value, |
| 65 |
account_id: accountId, |
| 66 |
}); |
| 67 |
this.data.ending_stock.accounts.push(...data.accounts); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
async setDate(date) { |
| 72 |
this.state.date = date; |
| 73 |
this.dateAsString = serializeDateTime(date); |
| 74 |
await this.loadReportData(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
async actionGenerateEntry() { |
| 79 |
const args = [[this.companyId]]; |
| 80 |
const date = serializeDate(this.state.date); |
| 81 |
if (date != serializeDate(DateTime.now())) { |
| 82 |
args.push(date); |
| 83 |
} |
| 84 |
const action = await this.orm.call("res.company", "action_close_stock_valuation", args); |
| 85 |
if (action) { |
| 86 |
this.actionService.doAction(action); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
actionPrintReport(format="pdf") { |
| 91 |
if (format === "pdf") { |
| 92 |
return this.orm.call("stock_account.stock.valuation.report", "action_print_as_pdf"); |
| 93 |
} else if (format === "xlsx") { |
| 94 |
return this.orm.call("stock_account.stock.valuation.report", "action_print_as_xlsx"); |
| 95 |
} |
| 96 |
} |
| 97 |
} |