2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2021-12-30 09:49:54 +00:00
|
|
|
import { stateCodeMap } from '../../accounting/gst';
|
2021-12-31 06:53:05 +00:00
|
|
|
import { convertPesaValuesToFloat } from '../../src/utils';
|
2019-07-30 12:02:49 +00:00
|
|
|
|
|
|
|
class BaseGSTR {
|
|
|
|
async getCompleteReport(gstrType, filters) {
|
|
|
|
if (['GSTR-1', 'GSTR-2'].includes(gstrType)) {
|
2021-12-30 09:49:54 +00:00
|
|
|
const place = filters.place;
|
|
|
|
delete filters.place;
|
2019-07-30 12:02:49 +00:00
|
|
|
let entries = await frappe.db.getAll({
|
2019-08-01 11:52:58 +00:00
|
|
|
doctype: gstrType === 'GSTR-1' ? 'SalesInvoice' : 'PurchaseInvoice',
|
2021-12-30 09:49:54 +00:00
|
|
|
filters,
|
2019-07-30 12:02:49 +00:00
|
|
|
});
|
2021-12-30 09:49:54 +00:00
|
|
|
filters.place = place;
|
2019-07-30 12:02:49 +00:00
|
|
|
|
|
|
|
let tableData = [];
|
|
|
|
for (let entry of entries) {
|
2021-12-30 09:49:54 +00:00
|
|
|
entry.doctype =
|
|
|
|
gstrType === 'GSTR-1' ? 'SalesInvoice' : 'PurchaseInvoice';
|
2019-07-30 12:02:49 +00:00
|
|
|
const row = await this.getRow(entry);
|
|
|
|
tableData.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(filters).length != 0) {
|
2021-12-30 09:49:54 +00:00
|
|
|
tableData = tableData.filter((row) => {
|
2019-07-30 12:02:49 +00:00
|
|
|
if (filters.account) return row.account === filters.account;
|
|
|
|
if (filters.transferType)
|
|
|
|
return row.transferType === filters.transferType;
|
|
|
|
if (filters.place) return row.place === filters.place;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2021-12-31 06:53:05 +00:00
|
|
|
tableData.forEach(convertPesaValuesToFloat);
|
2019-07-30 12:02:49 +00:00
|
|
|
return tableData;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRow(ledgerEntry) {
|
|
|
|
ledgerEntry = await frappe.getDoc(ledgerEntry.doctype, ledgerEntry.name);
|
2021-12-30 09:49:54 +00:00
|
|
|
|
|
|
|
const row = {};
|
2021-12-23 16:35:34 +00:00
|
|
|
const { gstin } = frappe.AccountingSettings;
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2019-07-30 12:02:49 +00:00
|
|
|
let party = await frappe.getDoc(
|
|
|
|
'Party',
|
|
|
|
ledgerEntry.customer || ledgerEntry.supplier
|
|
|
|
);
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2019-07-30 12:02:49 +00:00
|
|
|
if (party.address) {
|
|
|
|
let addressDetails = await frappe.getDoc('Address', party.address);
|
2021-12-29 04:24:57 +00:00
|
|
|
row.place = addressDetails.pos || '';
|
2019-07-30 12:02:49 +00:00
|
|
|
}
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2019-07-30 12:02:49 +00:00
|
|
|
row.gstin = party.gstin;
|
|
|
|
row.partyName = ledgerEntry.customer || ledgerEntry.supplier;
|
|
|
|
row.invNo = ledgerEntry.name;
|
|
|
|
row.invDate = ledgerEntry.date;
|
|
|
|
row.rate = 0;
|
2021-12-30 09:49:54 +00:00
|
|
|
row.inState =
|
|
|
|
gstin && gstin.substring(0, 2) === stateCodeMap[row.place?.toUpperCase()];
|
2019-07-30 12:02:49 +00:00
|
|
|
row.reverseCharge = !party.gstin ? 'Y' : 'N';
|
2021-12-30 09:49:54 +00:00
|
|
|
|
|
|
|
ledgerEntry.taxes?.forEach((tax) => {
|
2019-07-30 12:02:49 +00:00
|
|
|
row.rate += tax.rate;
|
2021-12-31 06:53:05 +00:00
|
|
|
const taxAmt = ledgerEntry.netTotal.percent(tax.rate);
|
2021-12-30 09:49:54 +00:00
|
|
|
|
|
|
|
switch (tax.account) {
|
|
|
|
case 'IGST': {
|
|
|
|
row.igstAmt = taxAmt;
|
|
|
|
row.inState = false;
|
|
|
|
}
|
|
|
|
case 'CGST':
|
|
|
|
row.cgstAmt = taxAmt;
|
|
|
|
case 'SGST':
|
|
|
|
row.sgstAmt = taxAmt;
|
|
|
|
case 'Nil Rated':
|
|
|
|
row.nilRated = true;
|
|
|
|
case 'Exempt':
|
|
|
|
row.exempt = true;
|
|
|
|
case 'Non GST':
|
|
|
|
row.nonGST = true;
|
|
|
|
}
|
2019-07-30 12:02:49 +00:00
|
|
|
});
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2019-07-30 12:02:49 +00:00
|
|
|
row.invAmt = ledgerEntry.grandTotal;
|
|
|
|
row.taxVal = ledgerEntry.netTotal;
|
2021-12-30 09:49:54 +00:00
|
|
|
|
2019-07-30 12:02:49 +00:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default BaseGSTR;
|