Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 4002-item.itemType
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-05-23 12:41:01 +02:00
commit 9bbdeb2840
15 changed files with 213 additions and 181 deletions

View File

@ -2,49 +2,64 @@ DROP PROCEDURE IF EXISTS vn.ticket_doRefund;
DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doRefund`(IN vOriginTicket INT, OUT vNewTicket INT)
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doRefund`(OUT vNewTicket INT)
BEGIN
/**
* Crea un ticket de abono a partir de tmp.sale y/o tmp.ticketService
*
* @return vNewTicket
*/
DECLARE vDone BIT DEFAULT 0;
DECLARE vCustomer MEDIUMINT;
DECLARE vClientFk MEDIUMINT;
DECLARE vWarehouse TINYINT;
DECLARE vCompany MEDIUMINT;
DECLARE vAddress MEDIUMINT;
DECLARE vRefundAgencyMode INT;
DECLARE vItemFk INT;
DECLARE vQuantity DECIMAL (10,2);
DECLARE vConcept VARCHAR(50);
DECLARE vPrice DECIMAL (10,2);
DECLARE vDiscount TINYINT;
DECLARE vRefundAgencyMode INT;
DECLARE vItemFk INT;
DECLARE vQuantity DECIMAL (10,2);
DECLARE vConcept VARCHAR(50);
DECLARE vPrice DECIMAL (10,2);
DECLARE vDiscount TINYINT;
DECLARE vSaleNew INT;
DECLARE vSaleMain INT;
DECLARE vZoneFk INT;
DECLARE vDescription VARCHAR(50);
DECLARE vTaxClassFk INT;
DECLARE vTicketServiceTypeFk INT;
DECLARE cSales CURSOR FOR
SELECT *
FROM tmp.sale;
DECLARE vSaleMain INT;
DECLARE vZoneFk INT;
DECLARE vDescription VARCHAR(50);
DECLARE vTaxClassFk INT;
DECLARE vTicketServiceTypeFk INT;
DECLARE vOriginTicket INT;
DECLARE cSales CURSOR FOR
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
FROM tmp.sale s;
DECLARE cTicketServices CURSOR FOR
SELECT *
FROM tmp.ticketService;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = 1;
SELECT id INTO vRefundAgencyMode
SELECT ts.description, - ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk
FROM tmp.ticketService ts;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SELECT sub.ticketFk INTO vOriginTicket
FROM (
SELECT s.ticketFk
FROM tmp.sale s
UNION ALL
SELECT ts.ticketFk
FROM tmp.ticketService ts
) sub
LIMIT 1;
SELECT id INTO vRefundAgencyMode
FROM agencyMode WHERE `name` = 'ABONO';
SELECT clientFk, warehouseFk, companyFk, addressFk
INTO vCustomer, vWarehouse, vCompany, vAddress
FROM ticket
WHERE id = vOriginTicket;
SELECT id INTO vZoneFk
INTO vClientFk, vWarehouse, vCompany, vAddress
FROM ticket
WHERE id = vOriginTicket;
SELECT id INTO vZoneFk
FROM zone WHERE agencyModeFk = vRefundAgencyMode
LIMIT 1;
LIMIT 1;
INSERT INTO vn.ticket (
clientFk,
shipped,
@ -54,10 +69,10 @@ BEGIN
warehouseFk,
companyFk,
landed,
zoneFk
zoneFk
)
SELECT
vCustomer,
vClientFk,
CURDATE(),
vAddress,
vRefundAgencyMode,
@ -65,49 +80,48 @@ BEGIN
vWarehouse,
vCompany,
CURDATE(),
vZoneFk
vZoneFk
FROM address a
WHERE a.id = vAddress;
SET vNewTicket = LAST_INSERT_ID();
SET vDone := 0;
SET vDone := FALSE;
OPEN cSales;
FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;
FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;
WHILE NOT vDone DO
INSERT INTO vn.sale(ticketFk, itemFk, quantity, concept, price, discount)
VALUES( vNewTicket, vItemFk, vQuantity, vConcept, vPrice, vDiscount );
SET vSaleNew = LAST_INSERT_ID();
INSERT INTO vn.saleComponent(saleFk,componentFk,`value`)
SELECT vSaleNew,componentFk,`value`
FROM vn.saleComponent
WHERE saleFk = vSaleMain;
SET vSaleNew = LAST_INSERT_ID();
INSERT INTO vn.saleComponent(saleFk,componentFk,`value`)
SELECT vSaleNew,componentFk,`value`
FROM vn.saleComponent
WHERE saleFk = vSaleMain;
FETCH cSales INTO vSaleMain, vItemFk, vQuantity, vConcept, vPrice, vDiscount;
END WHILE;
CLOSE cSales;
SET vDone := 0;
SET vDone := FALSE;
OPEN cTicketServices;
FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;
FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;
WHILE NOT vDone DO
INSERT INTO vn.ticketService(description, quantity, price, taxClassFk, ticketFk, ticketServiceTypeFk)
VALUES(vDescription, vQuantity, vPrice, vTaxClassFk, vNewTicket, vTicketServiceTypeFk);
FETCH cTicketServices INTO vDescription, vQuantity, vPrice, vTaxClassFk, vTicketServiceTypeFk;
END WHILE;
CLOSE cTicketServices;
INSERT INTO vn.ticketRefund(refundTicketFk, originalTicketFk)
VALUES(vNewTicket, vOriginTicket);
END$$
DELIMITER ;

View File

@ -76,6 +76,13 @@
translate>
Show CITES letter
</vn-item>
<vn-item
ng-click="refundConfirmation.show()"
name="refundInvoice"
vn-tooltip="Create a single ticket with all the content of the current invoice"
translate>
Refund
</vn-item>
</vn-list>
</vn-menu>
<vn-confirm
@ -88,6 +95,11 @@
on-accept="$ctrl.bookInvoiceOut()"
question="Are you sure you want to book this invoice?">
</vn-confirm>
<vn-confirm
vn-id="refundConfirmation"
on-accept="$ctrl.refundInvoiceOut()"
question="Are you sure you want to refund this invoice?">
</vn-confirm>
<vn-client-descriptor-popover
vn-id="clientDescriptor">
</vn-client-descriptor-popover>

View File

@ -116,6 +116,35 @@ class Controller extends Section {
invoiceId: this.id
});
}
async refundInvoiceOut() {
let filter = {
where: {refFk: this.invoiceOut.ref}
};
const tickets = await this.$http.get('Tickets', {filter});
this.tickets = tickets.data;
this.ticketsIds = [];
for (let ticket of this.tickets)
this.ticketsIds.push(ticket.id);
filter = {
where: {ticketFk: {inq: this.ticketsIds}}
};
const sales = await this.$http.get('Sales', {filter});
this.sales = sales.data;
const ticketServices = await this.$http.get('TicketServices', {filter});
this.services = ticketServices.data;
const params = {
sales: this.sales,
services: this.services
};
const query = `Sales/refund`;
return this.$http.post(query, params).then(res => {
this.$state.go('ticket.card.sale', {id: res.data});
});
}
}
Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];

View File

@ -122,4 +122,34 @@ describe('vnInvoiceOutDescriptorMenu', () => {
expect(controller.vnApp.showMessage).toHaveBeenCalled();
});
});
// #4084 review with Juan
xdescribe('refundInvoiceOut()', () => {
it('should make a query and go to ticket.card.sale', () => {
controller.$state.go = jest.fn();
const invoiceOut = {
id: 1,
ref: 'T1111111'
};
controller.invoiceOut = invoiceOut;
const tickets = [{id: 1}];
const sales = [{id: 1}];
const services = [{id: 2}];
$httpBackend.expectGET(`Tickets`).respond(tickets);
$httpBackend.expectGET(`Sales`).respond(sales);
$httpBackend.expectGET(`TicketServices`).respond(services);
const expectedParams = {
sales: sales,
services: services
};
$httpBackend.expectPOST(`Sales/refund`, expectedParams).respond();
controller.refundInvoiceOut();
$httpBackend.flush();
expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.sale', {id: undefined});
});
});
});

View File

@ -12,6 +12,8 @@ Are you sure you want to delete this invoice?: Estas seguro de eliminar esta fac
Are you sure you want to clone this invoice?: Estas seguro de clonar esta factura?
InvoiceOut booked: Factura asentada
Are you sure you want to book this invoice?: Estas seguro de querer asentar esta factura?
Are you sure you want to refund this invoice?: Estas seguro de querer abonar esta factura?
Create a single ticket with all the content of the current invoice: Crear un ticket unico con todo el contenido de la factura actual
Regenerate PDF invoice: Regenerar PDF factura
The invoice PDF document has been regenerated: El documento PDF de la factura ha sido regenerado
The email can't be empty: El correo no puede estar vacío

View File

@ -2,19 +2,19 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('refund', {
description: 'Create ticket with the selected lines changing the sign to the quantites',
description: 'Create ticket refund with lines and services changing the sign to the quantites',
accessType: 'WRITE',
accepts: [{
arg: 'sales',
description: 'The sales',
type: ['object'],
required: true
required: false
},
{
arg: 'ticketId',
type: 'number',
required: true,
description: 'The ticket id'
arg: 'services',
type: ['object'],
required: false,
description: 'The services'
}],
returns: {
type: 'number',
@ -26,7 +26,7 @@ module.exports = Self => {
}
});
Self.refund = async(ctx, sales, ticketId, options) => {
Self.refund = async(ctx, sales, services, options) => {
const myOptions = {};
let tx;
@ -39,7 +39,6 @@ module.exports = Self => {
}
try {
const salesIds = [];
const userId = ctx.req.accessToken.userId;
const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager');
@ -49,39 +48,47 @@ module.exports = Self => {
if (!hasValidRole)
throw new UserError(`You don't have privileges to create refund`);
for (let sale of sales)
salesIds.push(sale.id);
const salesIds = [];
if (sales) {
for (let sale of sales)
salesIds.push(sale.id);
} else
salesIds.push(null);
const servicesIds = [];
if (services) {
for (let service of services)
servicesIds.push(service.id);
} else
servicesIds.push(null);
const query = `
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketService;
CREATE TEMPORARY TABLE tmp.sale
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
SELECT s.id, s.itemFk, s.quantity, s.concept, s.price, s.discount, s.ticketFk
FROM sale s
WHERE s.id IN (?);
CREATE TEMPORARY TABLE tmp.ticketService(
description VARCHAR(50),
quantity DECIMAL (10,2),
price DECIMAL (10,2),
taxClassFk INT,
ticketServiceTypeFk INT
);
CALL vn.ticket_doRefund(?, @newTicket);
CREATE TEMPORARY TABLE tmp.ticketService
SELECT ts.description, ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk, ts.ticketFk
FROM ticketService ts
WHERE ts.id IN (?);
CALL vn.ticket_doRefund(@newTicket);
DROP TEMPORARY TABLE tmp.sale;
DROP TEMPORARY TABLE tmp.ticketService;`;
await Self.rawSql(query, [salesIds, ticketId], myOptions);
await Self.rawSql(query, [salesIds, servicesIds], myOptions);
const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions);
ticketId = newTicket.id;
const newTicketId = newTicket.id;
if (tx) await tx.commit();
return ticketId;
return newTicketId;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -1,78 +0,0 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('refundAll', {
description: 'Create ticket with all lines and services changing the sign to the quantites',
accessType: 'WRITE',
accepts: [{
arg: 'ticketId',
type: 'number',
required: true,
description: 'The ticket id'
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/refundAll`,
verb: 'post'
}
});
Self.refundAll = async(ctx, ticketId, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const userId = ctx.req.accessToken.userId;
const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager');
const isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant');
const hasValidRole = isClaimManager || isSalesAssistant;
if (!hasValidRole)
throw new UserError(`You don't have privileges to create refund`);
const query = `
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketService;
CREATE TEMPORARY TABLE tmp.sale
SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount
FROM sale s
JOIN ticket t ON t.id = s.ticketFk
WHERE t.id IN (?);
CREATE TEMPORARY TABLE tmp.ticketService
SELECT ts.description, - ts.quantity, ts.price, ts.taxClassFk, ts.ticketServiceTypeFk
FROM ticketService ts
WHERE ts.ticketFk IN (?);
CALL vn.ticket_doRefund(?, @newTicket);
DROP TEMPORARY TABLE tmp.sale;
DROP TEMPORARY TABLE tmp.ticketService;`;
await Self.rawSql(query, [ticketId, ticketId, ticketId], myOptions);
const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions);
ticketId = newTicket.id;
if (tx) await tx.commit();
return ticketId;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,20 +1,20 @@
const models = require('vn-loopback/server/server').models;
describe('sale refund()', () => {
const sales = [
{id: 7, ticketFk: 11},
{id: 8, ticketFk: 11}
];
const services = [{id: 1}];
it('should create ticket with the selected lines changing the sign to the quantites', async() => {
const tx = await models.Sale.beginTransaction({});
const ctx = {req: {accessToken: {userId: 9}}};
const ticketId = 11;
const sales = [
{id: 7, ticketFk: 11},
{id: 8, ticketFk: 11}
];
try {
const options = {transaction: tx};
const response = await models.Sale.refund(ctx, sales, ticketId, options);
const response = await models.Sale.refund(ctx, sales, services, options);
const [newTicketId] = await models.Sale.rawSql('SELECT MAX(t.id) id FROM vn.ticket t;', null, options);
expect(response).toEqual(newTicketId.id);
@ -30,17 +30,12 @@ describe('sale refund()', () => {
const tx = await models.Sale.beginTransaction({});
const ctx = {req: {accessToken: {userId: 1}}};
const ticketId = 11;
const sales = [
{id: 7, ticketFk: 11}
];
let error;
try {
const options = {transaction: tx};
await models.Sale.refund(ctx, sales, ticketId, options);
await models.Sale.refund(ctx, sales, services, options);
await tx.rollback();
} catch (e) {

View File

@ -7,7 +7,6 @@ module.exports = Self => {
require('../methods/sale/updateConcept')(Self);
require('../methods/sale/recalculatePrice')(Self);
require('../methods/sale/refund')(Self);
require('../methods/sale/refundAll')(Self);
require('../methods/sale/canEdit')(Self);
Self.validatesPresenceOf('concept', {

View File

@ -302,7 +302,7 @@
<!-- Refund all confirmation dialog -->
<vn-confirm
vn-id="refundAllConfirmation"
on-accept="$ctrl.refundAll()"
on-accept="$ctrl.refund()"
question="Are you sure you want to refund all?"
message="Refund all">
</vn-confirm>

View File

@ -273,9 +273,21 @@ class Controller extends Section {
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
refundAll() {
const params = {ticketId: this.id};
const query = `Sales/refundAll`;
async refund() {
const filter = {
where: {ticketFk: this.id}
};
const sales = await this.$http.get('Sales', {filter});
this.sales = sales.data;
const ticketServices = await this.$http.get('TicketServices', {filter});
this.services = ticketServices.data;
const params = {
sales: this.sales,
services: this.services
};
const query = `Sales/refund`;
return this.$http.post(query, params).then(res => {
this.$state.go('ticket.card.sale', {id: res.data});
});

View File

@ -262,16 +262,27 @@ describe('Ticket Component vnTicketDescriptorMenu', () => {
});
});
describe('refundAll()', () => {
// #4084 review with Juan
xdescribe('refund()', () => {
it('should make a query and go to ticket.card.sale', () => {
jest.spyOn(controller.$state, 'go').mockReturnValue();
const expectedParams = {ticketId: ticket.id};
controller.$state.go = jest.fn();
$httpBackend.expect('POST', `Sales/refundAll`, expectedParams).respond({ticketId: 16});
controller.refundAll();
controller._id = ticket.id;
const sales = [{id: 1}];
const services = [{id: 2}];
$httpBackend.expectGET(`Sales`).respond(sales);
$httpBackend.expectGET(`TicketServices`).respond(services);
const expectedParams = {
sales: sales,
services: services
};
$httpBackend.expectPOST(`Sales/refund`, expectedParams).respond();
controller.refund();
$httpBackend.flush();
expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.sale', {id: {ticketId: ticket.id}});
expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.sale', {id: undefined});
});
});

View File

@ -483,7 +483,7 @@ class Controller extends Section {
const sales = this.selectedValidSales();
if (!sales) return;
const params = {sales: sales, ticketId: this.ticket.id};
const params = {sales: sales};
const query = `Sales/refund`;
this.resetChanges();
this.$http.post(query, params).then(res => {

View File

@ -42,7 +42,7 @@
</append>
</vn-autocomplete>
<vn-input-number
vn-one min="0"
vn-one
step="1"
label="Quantity"
ng-model="service.quantity"

View File

@ -67,7 +67,6 @@
<vn-input-number
label="Bonus"
ng-model="$ctrl.zone.bonus"
min="0"
step="0.01"
vn-acl="deliveryBoss"
rule>