Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ux: allow to create custom sidebar entries #1027

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions fyo/model/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,30 @@ export class Doc extends Observable<DocValue | Doc[]> {
return true;
}

get canUndoSubmit() {
if (!this.canCancel) {
return false;
}

if (this.fyo.singles.SystemSettings?.provisionalModeSince == null) {
return false;
}

const submittedAt: Date = (this.submittedAt || this.created) as Date;
if (!submittedAt) {
return false;
}

if (
submittedAt <
(this.fyo.singles.SystemSettings.provisionalModeSince as Date)
) {
return false;
}

return true;
}

get canCancel() {
if (!this.schema.isSubmittable) {
return false;
Expand Down Expand Up @@ -344,14 +368,17 @@ export class Doc extends Observable<DocValue | Doc[]> {
value?: DocValue | Doc[] | DocValueMap[]
): boolean {
if (fieldname === 'numberSeries' && !this.notInserted) {
// console.log("cannot set %s, numberSeries inserted", fieldname)
return false;
}

if (value === undefined) {
// console.log("cannot set %s, undefined value", fieldname)
return false;
}

if (this.fieldMap[fieldname] === undefined) {
// console.log("cannot set %s, no fieldMap", fieldname, this.fieldMap)
return false;
}

Expand Down Expand Up @@ -940,12 +967,27 @@ export class Doc extends Observable<DocValue | Doc[]> {

await this.trigger('beforeSubmit');
await this.setAndSync('submitted', true);
await this.setAndSync('submittedAt', new Date());
await this.trigger('afterSubmit');

this.fyo.telemetry.log(Verb.Submitted, this.schemaName);
this.fyo.doc.observer.trigger(`submit:${this.schemaName}`, this.name);
}

async submitUndo() {
if (!this.schema.isSubmittable || !this.submitted || this.cancelled) {
return;
}

await this.trigger('beforeSubmitUndo');
await this.setAndSync('submitted', false);
await this.setAndSync('submittedAt', null);
await this.trigger('afterSubmitUndo');

this.fyo.telemetry.log(Verb.SubmitUndone, this.schemaName);
this.fyo.doc.observer.trigger(`submitUndo:${this.schemaName}`, this.name);
}

async cancel() {
if (!this.schema.isSubmittable || !this.submitted || this.cancelled) {
return;
Expand Down Expand Up @@ -1058,6 +1100,8 @@ export class Doc extends Observable<DocValue | Doc[]> {
async afterSync() {}
async beforeSubmit() {}
async afterSubmit() {}
async beforeSubmitUndo() {}
async afterSubmitUndo() {}
async beforeRename() {}
async afterRename() {}
async beforeCancel() {}
Expand Down
1 change: 1 addition & 0 deletions fyo/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum Verb {
Created = 'created',
Deleted = 'deleted',
Submitted = 'submitted',
SubmitUndone = 'submitUndone',
Cancelled = 'cancelled',
Imported = 'imported',
Exported = 'exported',
Expand Down
13 changes: 13 additions & 0 deletions models/Transactional/Transactional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export abstract class Transactional extends Doc {
await posting.post();
}

async afterSubmitUndo(): Promise<void> {
await super.afterSubmitUndo();
if (!this.isTransactional) {
return;
}

await this._deletePostings();
}

async afterCancel(): Promise<void> {
await super.afterCancel();
if (!this.isTransactional) {
Expand All @@ -76,6 +85,10 @@ export abstract class Transactional extends Doc {
return;
}

await this._deletePostings();
}

async _deletePostings(): Promise<void> {
const ledgerEntryIds = (await this.fyo.db.getAll(
ModelNameEnum.AccountingLedgerEntry,
{
Expand Down
30 changes: 27 additions & 3 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,29 @@ export abstract class Invoice extends Transactional {
}
}

async afterSubmitUndo() {
await super.afterSubmitUndo();
await this._cancelPayments({ undo: true });
await this._updatePartyOutStanding();
await this._updateIsItemsReturned();
await this._removeLoyaltyPointEntry();
this.reduceUsedCountOfCoupons();
}

async _undoPayments() {
const paymentIds = await this.getPaymentIds();
for (const paymentId of paymentIds) {
const paymentDoc = (await this.fyo.doc.getDoc(
'Payment',
paymentId
)) as Payment;
await paymentDoc.cancel();
}
}

async afterCancel() {
await super.afterCancel();
await this._cancelPayments();
await this._cancelPayments({ undo: false });
await this._updatePartyOutStanding();
await this._updateIsItemsReturned();
await this._removeLoyaltyPointEntry();
Expand All @@ -258,14 +278,18 @@ export abstract class Invoice extends Transactional {
await removeLoyaltyPoint(this);
}

async _cancelPayments() {
async _cancelPayments({ undo }: { undo: boolean }) {
const paymentIds = await this.getPaymentIds();
for (const paymentId of paymentIds) {
const paymentDoc = (await this.fyo.doc.getDoc(
'Payment',
paymentId
)) as Payment;
await paymentDoc.cancel();
if (undo) {
await paymentDoc.submitUndo();
} else {
await paymentDoc.cancel();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions models/baseModels/Payment/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ export class Payment extends Transactional {
}
}

async afterSubmitUndo() {
await super.afterSubmitUndo();
await this.revertOutstandingAmount();
}

async afterCancel() {
await super.afterCancel();
await this.revertOutstandingAmount();
Expand Down
22 changes: 22 additions & 0 deletions models/baseModels/SidebarEntry/SidebarEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Doc } from 'fyo/model/doc';
import type { QueryFilter } from 'utils/db/types';

export class SidebarEntry extends Doc {
section?: string;
route?: string;
model?: string;
filters?: string;

fullRoute(): string {
switch (this.route) {
case '/list':
return `/list/${this.model!}/${this.name!}`;
default:
return this.route!;
}
}

parsedFilters(): QueryFilter {
return JSON.parse(this.filters!) as QueryFilter;
}
}
2 changes: 2 additions & 0 deletions models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { SalesInvoiceItem } from './baseModels/SalesInvoiceItem/SalesInvoiceItem
import { SalesQuote } from './baseModels/SalesQuote/SalesQuote';
import { SalesQuoteItem } from './baseModels/SalesQuoteItem/SalesQuoteItem';
import { SetupWizard } from './baseModels/SetupWizard/SetupWizard';
import { SidebarEntry } from './baseModels/SidebarEntry/SidebarEntry';
import { Tax } from './baseModels/Tax/Tax';
import { TaxSummary } from './baseModels/TaxSummary/TaxSummary';
import { Batch } from './inventory/Batch';
Expand Down Expand Up @@ -83,6 +84,7 @@ export const models = {
SalesQuoteItem,
SerialNumber,
SetupWizard,
SidebarEntry,
PrintTemplate,
Tax,
TaxSummary,
Expand Down
4 changes: 4 additions & 0 deletions models/inventory/StockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class StockManager {
await this.#sync();
}

async undoTransfers() {
await this.cancelTransfers();
}

async cancelTransfers() {
const { referenceName, referenceType } = this.details;
await this.fyo.db.deleteAll(ModelNameEnum.StockLedgerEntry, {
Expand Down
5 changes: 5 additions & 0 deletions models/inventory/StockMovement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export class StockMovement extends Transfer {
await updateSerialNumbers(this, false);
}

async afterSubmitUndo(): Promise<void> {
await super.afterSubmitUndo();
await updateSerialNumbers(this, true);
}

async afterCancel(): Promise<void> {
await super.afterCancel();
await updateSerialNumbers(this, true);
Expand Down
7 changes: 7 additions & 0 deletions models/inventory/StockTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ export abstract class StockTransfer extends Transfer {
await this._updateItemsReturned();
}

async afterSubmitUndo() {
await super.afterSubmitUndo();
await updateSerialNumbers(this, false, this.isReturn);
await this._updateBackReference();
await this._updateItemsReturned();
}

async afterCancel(): Promise<void> {
await super.afterCancel();
await updateSerialNumbers(this, true, this.isReturn);
Expand Down
13 changes: 13 additions & 0 deletions models/inventory/Transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ export abstract class Transfer extends Transactional {
await this._getStockManager().createTransfers(transferDetails);
}

async beforeSubmitUndo(): Promise<void> {
await super.beforeSubmitUndo();
const transferDetails = this._getTransferDetails();
const stockManager = this._getStockManager();
stockManager.isCancelled = true;
await stockManager.validateCancel(transferDetails);
}

async afterSubmitUndo(): Promise<void> {
await super.afterSubmitUndo();
await this._getStockManager().undoTransfers();
}

async beforeCancel(): Promise<void> {
await super.beforeCancel();
const transferDetails = this._getTransferDetails();
Expand Down
1 change: 1 addition & 0 deletions models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum ModelNameEnum {
LoyaltyPointEntry = 'LoyaltyPointEntry',
CollectionRulesItems = 'CollectionRulesItems',
CouponCode = 'CouponCode',
SidebarEntry = 'SidebarEntry',

AppliedCouponCodes = 'AppliedCouponCodes',
Payment = 'Payment',
Expand Down
46 changes: 46 additions & 0 deletions schemas/app/SidebarEntry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "SidebarEntry",
"label": "Sidebar Entry",
"naming": "manual",
"fields": [
{
"fieldname": "name",
"section": "Default",
"label": "Name",
"fieldtype": "Data",
"required": true,
"placeholder": "Entry Name"
},
{
"fieldname": "section",
"section": "Default",
"label": "Section",
"fieldtype": "Data",
"required": false
},
{
"fieldname": "route",
"section": "Default",
"label": "Route",
"fieldtype": "Data",
"required": true,
"default": "/list"
},
{
"fieldname": "model",
"section": "Default",
"label": "Document Type",
"fieldtype": "Data",
"required": true,
"default": "JournalEntry"
},
{
"fieldname": "filters",
"section": "Default",
"label": "Filters",
"fieldtype": "Data",
"required": true,
"default": "{}"
}
]
}
7 changes: 7 additions & 0 deletions schemas/core/SystemSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
"default": false,
"description": "Sets the theme of the app.",
"section": "Theme"
},
{
"fieldname": "provisionalModeSince",
"label": "Provisional Mode Since",
"fieldtype": "Datetime",
"description": "Date since the provisional mode is set, or NULL for definitive mode",
"hidden": true
}
],
"quickEditFields": [
Expand Down
8 changes: 8 additions & 0 deletions schemas/meta/submittable.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"meta": true,
"section": "System"
},
{
"fieldname": "submittedAt",
"label": "Submition Date",
"fieldtype": "Datetime",
"required": false,
"meta": true,
"section": "System"
},
{
"fieldname": "cancelled",
"label": "Cancelled",
Expand Down
2 changes: 2 additions & 0 deletions schemas/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import PurchaseReceiptItem from './app/inventory/PurchaseReceiptItem.json';
import SerialNumber from './app/inventory/SerialNumber.json';
import Shipment from './app/inventory/Shipment.json';
import ShipmentItem from './app/inventory/ShipmentItem.json';
import SidebarEntry from './app/SidebarEntry.json';
import StockLedgerEntry from './app/inventory/StockLedgerEntry.json';
import StockMovement from './app/inventory/StockMovement.json';
import StockMovementItem from './app/inventory/StockMovementItem.json';
Expand Down Expand Up @@ -92,6 +93,7 @@ export const appSchemas: Schema[] | SchemaStub[] = [
SetupWizard as Schema,
GetStarted as Schema,
PrintTemplate as Schema,
SidebarEntry as Schema,

Color as Schema,
Currency as Schema,
Expand Down
Loading
Loading