Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
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
106 changes: 97 additions & 9 deletions packages/plugin-cards-api/src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default {
types: [
{
label: 'Cards',
type: 'cards'
type: 'cards',
subTypes: ['deal', 'task', 'ticket', 'purchase', 'stageDeal']
}
],

Expand All @@ -74,14 +75,16 @@ export default {
return [...commonFields, ...uniqueFields];
},

replaceContent: async ({ subdomain, data: { stageId, itemId, content } }) => {
replaceContent: async ({
subdomain,
data: { stageId, itemId, content, contentype, itemIds }
}) => {
const models = await generateModels(subdomain);
const stage = await models.Stages.findOne({ _id: stageId });

if (!stage) {
return '';
}

let collection;

if (stage.type == 'deal') {
Expand All @@ -101,15 +104,27 @@ export default {
if (stage.type == 'growthHack') {
collection = models.GrowthHacks;
}

if (!collection) {
return '';
}

const item = await collection.findOne({ _id: itemId });

if (!item) {
return '';
let item;
if (contentype == 'cards:stage') {
const items = await collection.find({
stageId: stageId,
_id: { $in: itemIds.split(',') }
});
if (!items) {
return '';
}
item = await cardsStage(items);
if (!item) {
return '';
}
} else {
item = await collection.findOne({ _id: itemId });
if (!item) {
return '';
}
}

const simpleFields = ['name', 'description'];
Expand Down Expand Up @@ -406,3 +421,76 @@ export default {
return [replacedContent];
}
};

function cardsStage(items: any[]) {
try {
const itemsArray = items;
const aggregatedData: Record<string, any> = {
amount: {
AED: 0
},
productsData: []
};
itemsArray.forEach(item => {
const combinedNames = itemsArray.map(item => item.name).join(',');
aggregatedData.isComplete = item.isComplete;
aggregatedData.assignedUserIds = item.assignedUserIds;
aggregatedData.watchedUserIds = item.watchedUserIds;
aggregatedData.labelIds = item.labelIds;
aggregatedData.tagIds = item.tagIds;
aggregatedData.branchIds = item.branchIds;
aggregatedData.departmentIds = item.departmentIds;
aggregatedData.modifiedAt = item.modifiedAt;
aggregatedData.createdAt = item.createdAt;
aggregatedData.stageChangedDate = item.stageChangedDate;
aggregatedData.sourceConversationIds = item.sourceConversationIds;
aggregatedData.status = item.status;
aggregatedData.name = combinedNames;
aggregatedData.stageId = item.stageId;
aggregatedData.customFieldsData = item.customFieldsData;
aggregatedData.initialStageId = item.initialStageId;
aggregatedData.modifiedBy = item.modifiedBy;
aggregatedData.userId = item.userId;
aggregatedData.searchText = combinedNames;

if (item.productsData) {
item.productsData.forEach(product => {
const existingProduct = aggregatedData.productsData.find(
p =>
p.productId === product.productId &&
p.branchId === product.branchId &&
p.departmentId === product.departmentId
);

if (existingProduct) {
existingProduct.quantity += product.quantity;
existingProduct.amount += product.amount;
} else {
aggregatedData.productsData.push({
tax: product.tax,
taxPercent: product.taxPercent,
discount: product.discount,
vatPercent: product.vatPercent,
discountPercent: product.discountPercent,
amount: product.amount,
currency: product.currency,
tickUsed: product.tickUsed,
maxQuantity: product.maxQuantity,
quantity: product.quantity,
productId: product.productId,
unitPrice: product.unitPrice,
globalUnitPrice: product.globalUnitPrice,
unitPricePercent: product.unitPricePercent
});
}

// Update the total amount for this stage
aggregatedData.amount.AED += product.amount * product.quantity;
});
}
});
return aggregatedData;
} catch (error) {
return { error: error.message };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const documentQueries = {
}

if (subType) {
selector.subType = subType;
selector.$or = [
{ subType },
{ subType: { $exists: false } },
{ subType: { $in: ['', null, undefined] } }
];
}

if (limit) {
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-documents-ui/src/graphql/queries.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const documents = `
query documents($page: Int, $perPage: Int, $contentType: String) {
documents(page: $page, perPage: $perPage, contentType: $contentType) {
query documents($page: Int, $perPage: Int, $contentType: String, $subType: String) {
documents(page: $page, perPage: $perPage, contentType: $contentType, subType: $subType) {
_id
contentType
subType
name
createdAt
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ export default class PrintActionButton extends React.Component<Props, State> {
}

loadDocuments = () => {
const { item } = this.props;
this.setState({ loading: true });

client
.mutate({
mutation: gql(queries.documents),
variables: { contentType: 'cards' }
variables: { contentType: 'cards', subType: item.stage?.type }
})
.then(({ data }) => {
this.setState({ documents: data.documents });
Expand Down
57 changes: 48 additions & 9 deletions packages/ui-cards/src/boards/components/stage/Stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import { renderAmount } from '../../utils';
import ItemList from '../stage/ItemList';
import { OverlayTrigger, Popover, Dropdown } from 'react-bootstrap';
import { Row } from '@erxes/ui-settings/src/styles';
import { isEnabled } from '@erxes/ui/src/utils/core';
import StageModal from './StageModal';

type Props = {
loadingItems: () => boolean;
removeStage: (stageId: string) => void;
index: number;
stage: IStage;
length: number;
items: IItem[];
items: any[];
onAddItem: (stageId: string, item: IItem) => void;
onRemoveItem: (itemId: string, stageId: string) => void;
loadMore: () => void;
Expand All @@ -43,6 +45,8 @@ type Props = {

type State = {
showSortOptions: boolean;
renderModal: boolean;
items: any[];
};

export default class Stage extends React.Component<Props, State> {
Expand All @@ -51,14 +55,16 @@ export default class Stage extends React.Component<Props, State> {

constructor(props: Props) {
super(props);

this.bodyRef = React.createRef();

this.state = { showSortOptions: false };
this.state = {
showSortOptions: false,
renderModal: false,
items: []
};
}

componentDidMount() {
// Load items until scroll created
const handle = setInterval(() => {
if (this.props.loadingItems()) {
return;
Expand Down Expand Up @@ -88,10 +94,11 @@ export default class Stage extends React.Component<Props, State> {

shouldComponentUpdate(nextProps: Props, nextState: State) {
const { stage, index, length, items, loadingItems } = this.props;
const { showSortOptions } = this.state;
const { showSortOptions, renderModal } = this.state;

if (
showSortOptions !== nextState.showSortOptions ||
renderModal !== nextState.renderModal ||
index !== nextProps.index ||
loadingItems() !== nextProps.loadingItems() ||
length !== nextProps.length ||
Expand All @@ -114,10 +121,27 @@ export default class Stage extends React.Component<Props, State> {
this.setState({ showSortOptions: !showSortOptions });
};

toggleModal = () => {
this.setState(prevState => ({
renderModal: !prevState.renderModal
}));
this.onClosePopover();
};

onChangeCheckbox = (id: string, isChecked: boolean) => {
const { items } = this.props;
const changeItems = [...items];
changeItems.map(item => {
if (item._id === id) {
item.checked = isChecked;
}
});
this.setState({ items: changeItems });
};

renderPopover() {
const { stage } = this.props;
const { stage, options } = this.props;
const { showSortOptions } = this.state;

const archiveList = () => {
this.props.archiveList();
this.onClosePopover();
Expand Down Expand Up @@ -149,10 +173,13 @@ export default class Stage extends React.Component<Props, State> {
<li onClick={removeStage} key="remove-stage">
{__('Remove stage')}
</li>

<Dropdown.Divider />

<li onClick={this.toggleSortOptions}>{__('Sort By')}</li>
{isEnabled('documents') && options.type === 'deal' && (
<li>
<a onClick={this.toggleModal}>{__('Print document')}</a>
</li>
)}
</>
)}
</ActionList>
Expand Down Expand Up @@ -333,6 +360,16 @@ export default class Stage extends React.Component<Props, State> {
);
}

renderTriggerModal() {
return this.state.renderModal ? (
<StageModal
item={this.props.items}
toggleModal={this.toggleModal}
stage={this.props.stage}
/>
) : null;
}

render() {
const { index, stage } = this.props;

Expand All @@ -353,6 +390,7 @@ export default class Stage extends React.Component<Props, State> {
</div>
{this.renderCtrl()}
</StageTitle>

<Row>
{renderAmount(stage.amount)}
{renderAmount(stage.unUsedAmount, false)}
Expand All @@ -361,6 +399,7 @@ export default class Stage extends React.Component<Props, State> {
</Header>
<Body innerRef={this.bodyRef} onScroll={this.onScroll}>
{this.renderItemList()}
{this.renderTriggerModal()},
</Body>
{this.renderAddItemTrigger()}
</StageRoot>
Expand Down
Loading