refs #5914 WIP created transferInvoiceOut
This commit is contained in:
parent
22f76ec6a9
commit
3ef74cab5c
|
@ -6,10 +6,35 @@ module.exports = Self => {
|
|||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'data',
|
||||
type: 'Object',
|
||||
arg: 'id',
|
||||
type: 'number',
|
||||
required: true
|
||||
}
|
||||
},
|
||||
{
|
||||
arg: 'ref',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
arg: 'newClientFk',
|
||||
type: 'number',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
arg: 'cplusRectificationId',
|
||||
type: 'number',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
arg: 'cplusInvoiceType477Id',
|
||||
type: 'number',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
arg: 'invoiceCorrectionTypeId',
|
||||
type: 'number',
|
||||
required: true
|
||||
},
|
||||
],
|
||||
returns: {
|
||||
type: 'boolean',
|
||||
|
@ -21,7 +46,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.transferInvoiceOut = async(ctx, params, options) => {
|
||||
Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => {
|
||||
const models = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
@ -34,16 +59,12 @@ module.exports = Self => {
|
|||
myOptions.transaction = tx;
|
||||
}
|
||||
try {
|
||||
const {id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477FkId, invoiceCorrectionTypeId} = params;
|
||||
if (!id || !ref || !newClientFk || !cplusRectificationId || !cplusInvoiceType477FkId || !invoiceCorrectionTypeId)
|
||||
throw new UserError(`There are missing fields.`);
|
||||
|
||||
// Refund tickets and group
|
||||
const filter = {where: {refFk: ref}};
|
||||
const tickets = await models.Ticket.find(filter, myOptions);
|
||||
const ticketsIds = tickets.map(ticket => ticket.id);
|
||||
await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
|
||||
|
||||
console.log('Ticket refunded');
|
||||
// Clone tickets
|
||||
const refundAgencyMode = await models.AgencyMode.findOne({
|
||||
include: {
|
||||
|
@ -58,25 +79,20 @@ module.exports = Self => {
|
|||
const refoundZoneId = refundAgencyMode.zones()[0].id;
|
||||
const services = await models.TicketService.find(filter, myOptions);
|
||||
const servicesIds = services.map(service => service.id);
|
||||
const salesFilter = {
|
||||
where: {id: {inq: salesIds}},
|
||||
include: {
|
||||
relation: 'components',
|
||||
scope: {
|
||||
fields: ['saleFk', 'componentFk', 'value']
|
||||
}
|
||||
}
|
||||
};
|
||||
const salesFilter = {where: {ticketFk: {inq: ticketsIds}}};
|
||||
const sales = await models.Sale.find(salesFilter, myOptions);
|
||||
const isRefund = false;
|
||||
const clonedTicketIds = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, isRefund, myOptions);
|
||||
|
||||
const clonedTickets = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, false, false, myOptions);
|
||||
console.log('cloned tickets');
|
||||
// Update client
|
||||
for (const clonedTicketId of clonedTicketIds) {
|
||||
const ticket = await models.Ticket.findById(clonedTicketId, myOptions);
|
||||
await ticket.updateAttributes({clientFk: newClientFk});
|
||||
for (const clonedTicket of clonedTickets) {
|
||||
// const ticket = await models.Ticket.findById(clonedTicketId, myOptions);
|
||||
console.log(clonedTicket);
|
||||
await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions);
|
||||
console.log(clonedTicket);
|
||||
}
|
||||
// Quick invoice
|
||||
const clonedTicketIds = clonedTickets.map(clonedTicket => clonedTicket.id);
|
||||
console.log(clonedTicketIds);
|
||||
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
|
||||
|
||||
// Insert InvoiceCorrection
|
||||
|
@ -85,7 +101,7 @@ module.exports = Self => {
|
|||
correctingFk: invoiceId,
|
||||
correctedFk: id,
|
||||
cplusRectificationTypeFk: cplusRectificationId,
|
||||
cplusInvoiceType477Fk: cplusInvoiceType477FkId,
|
||||
cplusInvoiceType477Fk: cplusInvoiceType477Id,
|
||||
invoiceCorrectionType: invoiceCorrectionTypeId
|
||||
});
|
||||
}
|
||||
|
|
|
@ -222,12 +222,13 @@
|
|||
<vn-horizontal>
|
||||
<vn-autocomplete
|
||||
vn-one
|
||||
vn-id="cplusInvoiceType477"
|
||||
vn-id="cplusInvoiceType"
|
||||
data="cplusInvoiceType477"
|
||||
show-field="description"
|
||||
value-field="id"
|
||||
required="true"
|
||||
ng-model="$ctrl.cplusInvoiceType477"
|
||||
search-function="{or: [{id: $search}, {description: {like: '%'+ $search +'%'}}]}"
|
||||
label="Class">
|
||||
</vn-autocomplete>
|
||||
<vn-autocomplete
|
||||
|
|
|
@ -128,14 +128,12 @@ class Controller extends Section {
|
|||
|
||||
transferInvoice() {
|
||||
const params = {
|
||||
data: {
|
||||
id: this.invoiceOut.id,
|
||||
ref: this.invoiceOut.ref,
|
||||
newClientFk: this.invoiceOut.client.id,
|
||||
cplusRectificationId: this.cplusRectificationType,
|
||||
cplusInvoiceType477Id: this.cplusInvoiceType477,
|
||||
invoiceCorrectionTypeId: this.invoiceCorrectionType
|
||||
}
|
||||
id: this.invoiceOut.id,
|
||||
ref: this.invoiceOut.ref,
|
||||
newClientFk: this.invoiceOut.client.id,
|
||||
cplusRectificationId: this.cplusRectificationType,
|
||||
cplusInvoiceType477Id: this.cplusInvoiceType477,
|
||||
invoiceCorrectionTypeId: this.invoiceCorrectionType
|
||||
};
|
||||
this.$http.post(`InvoiceOuts/transferInvoice`, params).then(res => console.log(res.data));
|
||||
}
|
||||
|
|
|
@ -1,67 +1,104 @@
|
|||
module.exports = Self => {
|
||||
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, isRefund, myOptions) => {
|
||||
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, negative, myOptions) => {
|
||||
const models = Self.app.models;
|
||||
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
||||
let tx;
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
||||
const [firstTicketId] = ticketsIds;
|
||||
const now = Date.vnNew();
|
||||
let updatedTickets = [];
|
||||
let newTicket;
|
||||
const filter = {
|
||||
include: [
|
||||
{relation: 'address'},
|
||||
{
|
||||
relation: 'sale',
|
||||
inq: sales,
|
||||
},
|
||||
]
|
||||
};
|
||||
try {
|
||||
for (const [index, ticketId] of ticketsIds.entries()) {
|
||||
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
|
||||
if (!group || !index) {
|
||||
newTicket = await models.Ticket.create({
|
||||
clientFk: ticket.clientFk,
|
||||
shipped: now,
|
||||
addressFk: ticket.address().id,
|
||||
agencyModeFk: refundAgencyMode.id,
|
||||
nickname: ticket.address().nickname,
|
||||
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
|
||||
companyFk: ticket.companyFk,
|
||||
landed: now,
|
||||
zoneFk: refoundZoneId
|
||||
}, myOptions);
|
||||
updatedTickets.push(newTicket);
|
||||
}
|
||||
const sales = ticket.sale();
|
||||
const saleIds = sales.map(sale => sale.id);
|
||||
const saleComponentsFilter = {
|
||||
where: {saleFk: {inq: saleIds}},
|
||||
scope: {
|
||||
fields: ['saleFk', 'componentFk', 'value']
|
||||
}
|
||||
};
|
||||
for (const sale of sales) {
|
||||
const createdSale = await models.Sale.create({
|
||||
ticketFk: newTicket.id,
|
||||
itemFk: sale.itemFk,
|
||||
quantity: (negative) ? - sale.quantity : sale.quantity,
|
||||
concept: sale.concept,
|
||||
price: sale.price,
|
||||
discount: sale.discount,
|
||||
}, myOptions);
|
||||
const components = await models.SaleComponent.find(saleComponentsFilter, myOptions);
|
||||
// const components = sale.components();
|
||||
for (const component of components)
|
||||
component.saleFk = createdSale.id;
|
||||
|
||||
for (const ticketId of ticketsIds) {
|
||||
const filter = {include: {relation: 'address'}};
|
||||
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
|
||||
|
||||
const ticketUpdated = await models.Ticket.create({
|
||||
clientFk: ticket.clientFk,
|
||||
shipped: now,
|
||||
addressFk: ticket.address().id,
|
||||
agencyModeFk: refundAgencyMode.id,
|
||||
nickname: ticket.address().nickname,
|
||||
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
|
||||
companyFk: ticket.companyFk,
|
||||
landed: now,
|
||||
zoneFk: refoundZoneId
|
||||
}, myOptions);
|
||||
updatedTickets.push(ticketUpdated);
|
||||
}
|
||||
|
||||
for (const sale of sales) {
|
||||
const createdSale = await models.Sale.create({
|
||||
ticketFk: sale.ticketFk,
|
||||
itemFk: sale.itemFk,
|
||||
quantity: (isRefund) ? - sale.quantity : sale.quantity,
|
||||
concept: sale.concept,
|
||||
price: sale.price,
|
||||
discount: sale.discount,
|
||||
}, myOptions);
|
||||
|
||||
const components = sale.components();
|
||||
for (const component of components)
|
||||
component.saleFk = createdSale.id;
|
||||
|
||||
await models.SaleComponent.create(components, myOptions);
|
||||
}
|
||||
|
||||
if (servicesIds && servicesIds.length > 0) {
|
||||
const servicesFilter = {
|
||||
where: {id: {inq: servicesIds}}
|
||||
};
|
||||
const services = await models.TicketService.find(servicesFilter, myOptions);
|
||||
for (const service of services) {
|
||||
await models.TicketService.create({
|
||||
description: service.description,
|
||||
quantity: (isRefund) ? - service.quantity : service.quantity,
|
||||
price: service.price,
|
||||
taxClassFk: service.taxClassFk,
|
||||
ticketFk: service.ticketFk,
|
||||
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
||||
}, myOptions);
|
||||
await models.SaleComponent.create(components, myOptions);
|
||||
}
|
||||
}
|
||||
if (servicesIds && servicesIds.length > 0) {
|
||||
const servicesFilter = {
|
||||
where: {id: {inq: servicesIds}}
|
||||
};
|
||||
const services = await models.TicketService.find(servicesFilter, myOptions);
|
||||
for (const service of services) {
|
||||
await models.TicketService.create({
|
||||
description: service.description,
|
||||
quantity: (negative) ? - service.quantity : service.quantity,
|
||||
price: service.price,
|
||||
taxClassFk: service.taxClassFk,
|
||||
ticketFk: service.ticketFk,
|
||||
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
||||
}, myOptions);
|
||||
}
|
||||
}
|
||||
|
||||
const query = `CALL vn.ticket_recalc(?, NULL)`;
|
||||
|
||||
if (group) {
|
||||
await Self.rawSql(query, [newTicket.id], myOptions);
|
||||
if (tx) await tx.commit();
|
||||
return {
|
||||
refundTicket: newTicket,
|
||||
originalTicketFk: firstTicketId
|
||||
};
|
||||
} else {
|
||||
for (const updatedTicket of updatedTickets)
|
||||
await Self.rawSql(query, [updatedTicket.id], myOptions);
|
||||
if (tx) await tx.commit();
|
||||
return updatedTickets/* updatedTickets.map(updatedTicket => updatedTicket.id) */;
|
||||
}
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
|
||||
const query = `CALL vn.ticket_recalc(?, NULL)`;
|
||||
|
||||
for (const updatedTicket of updatedTickets)
|
||||
await Self.rawSql(query, [updatedTicket.id], myOptions);
|
||||
return updatedTickets.map(updatedTicket => updatedTicket.id);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -155,19 +155,23 @@ module.exports = Self => {
|
|||
}
|
||||
};
|
||||
const sales = await models.Sale.find(salesFilter, myOptions);
|
||||
const group = true;
|
||||
const isRefund = true;
|
||||
|
||||
const refundTicket = await cloneAndGroup(
|
||||
const clonedTicket = await models.Sale.clone(
|
||||
sales,
|
||||
refundAgencyMode,
|
||||
refoundZoneId,
|
||||
servicesIds,
|
||||
withWarehouse,
|
||||
group,
|
||||
isRefund,
|
||||
true,
|
||||
true,
|
||||
myOptions
|
||||
);
|
||||
|
||||
const refundTicket = clonedTicket.refundTicket;
|
||||
|
||||
await models.TicketRefund.create({
|
||||
refundTicketFk: refundTicket.id,
|
||||
originalTicketFk: clonedTicket.originalTicketFk,
|
||||
}, myOptions);
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return refundTicket;
|
||||
|
@ -177,74 +181,7 @@ module.exports = Self => {
|
|||
}
|
||||
};
|
||||
|
||||
async function cloneAndGroup(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) {
|
||||
if (!group) {
|
||||
const tickets = await models.Sale.clone(sales,
|
||||
refundAgencyMode,
|
||||
refoundZoneId, servicesIds,
|
||||
withWarehouse,
|
||||
isRefund,
|
||||
myOptions);
|
||||
return tickets;
|
||||
}
|
||||
|
||||
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
||||
const [firstTicketId] = ticketsIds;
|
||||
const filter = {include: {relation: 'address'}};
|
||||
const ticket = await models.Ticket.findById(firstTicketId, filter, myOptions);
|
||||
|
||||
const ticketUpdated = await models.Ticket.create({
|
||||
clientFk: ticket.clientFk,
|
||||
shipped: now,
|
||||
addressFk: ticket.address().id,
|
||||
agencyModeFk: refundAgencyMode.id,
|
||||
nickname: ticket.address().nickname,
|
||||
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
|
||||
companyFk: ticket.companyFk,
|
||||
landed: now,
|
||||
zoneFk: refoundZoneId
|
||||
}, myOptions);
|
||||
|
||||
for (const sale of sales) {
|
||||
const createdSale = await models.Sale.create({
|
||||
ticketFk: ticketUpdated.id,
|
||||
itemFk: sale.itemFk,
|
||||
quantity: (isRefund) ? - sale.quantity : sale.quantity,
|
||||
concept: sale.concept,
|
||||
price: sale.price,
|
||||
discount: sale.discount,
|
||||
}, myOptions);
|
||||
|
||||
const components = sale.components();
|
||||
for (const component of components)
|
||||
component.saleFk = createdSale.id;
|
||||
|
||||
await models.SaleComponent.create(components, myOptions);
|
||||
}
|
||||
|
||||
if (servicesIds && servicesIds.length > 0) {
|
||||
const servicesFilter = {
|
||||
where: {id: {inq: servicesIds}}
|
||||
};
|
||||
const services = await models.TicketService.find(servicesFilter, myOptions);
|
||||
for (const service of services) {
|
||||
await models.TicketService.create({
|
||||
description: service.description,
|
||||
quantity: (isRefund) ? - service.quantity : service.quantity,
|
||||
price: service.price,
|
||||
taxClassFk: service.taxClassFk,
|
||||
ticketFk: ticketUpdated.id,
|
||||
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
||||
}, myOptions);
|
||||
}
|
||||
}
|
||||
|
||||
const query = `CALL vn.ticket_recalc(?, NULL)`;
|
||||
await Self.rawSql(query, [refundTicket.id], myOptions);
|
||||
return ticketUpdated;
|
||||
}
|
||||
|
||||
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {
|
||||
/* async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {
|
||||
const models = Self.app.models;
|
||||
|
||||
const filter = {include: {relation: 'address'}};
|
||||
|
@ -268,5 +205,5 @@ module.exports = Self => {
|
|||
}, myOptions);
|
||||
|
||||
return refundTicket;
|
||||
}
|
||||
} */
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
describe('Sale refund()', () => {
|
||||
fdescribe('Sale refund()', () => {
|
||||
const userId = 5;
|
||||
const ctx = {req: {accessToken: userId}};
|
||||
const activeCtx = {
|
||||
|
|
|
@ -39,7 +39,6 @@ module.exports = Self => {
|
|||
|
||||
try {
|
||||
const filter = {where: {ticketFk: {inq: ticketsIds}}};
|
||||
|
||||
const sales = await models.Sale.find(filter, myOptions);
|
||||
const salesIds = sales.map(sale => sale.id);
|
||||
|
||||
|
|
|
@ -136,6 +136,11 @@
|
|||
"type": "belongsTo",
|
||||
"model": "Zone",
|
||||
"foreignKey": "zoneFk"
|
||||
},
|
||||
"sale": {
|
||||
"type": "hasMany",
|
||||
"model": "Sale",
|
||||
"foreignKey": "ticketFk"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue