mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
feat: rewrite repositories with base entity repository class.
feat: sales and purchases status. feat: sales and purchases auto-increment number. fix: settings find query with extra columns.
This commit is contained in:
@@ -1,75 +1,16 @@
|
||||
import TenantRepository from "./TenantRepository";
|
||||
|
||||
import { Customer } from 'models'
|
||||
export default class CustomerRepository extends TenantRepository {
|
||||
all() {
|
||||
const { Contact } = this.models;
|
||||
|
||||
return this.cache.get('customers', () => {
|
||||
return Contact.query().modify('customer');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customer details of the given id.
|
||||
* @param {number} customerId - Customer id.
|
||||
* Constructor method.
|
||||
*/
|
||||
getById(customerId: number) {
|
||||
const { Contact } = this.models;
|
||||
|
||||
return this.cache.get(`customers.id.${customerId}`, () => {
|
||||
return Contact.query().modifier('customer').findById(customerId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the given customer exists.
|
||||
* @param {number} customerId
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isExists(customerId: number) {
|
||||
return !!this.getById(customerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the sales invoices that assocaited to the given customer.
|
||||
* @param {number} customerId
|
||||
*/
|
||||
getSalesInvoices(customerId: number) {
|
||||
const { SaleInvoice } = this.models;
|
||||
|
||||
return this.cache.get(`customers.invoices.${customerId}`, () => {
|
||||
return SaleInvoice.query().where('customer_id', customerId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customers details of the given ids.
|
||||
* @param {number[]} customersIds - Customers ids.
|
||||
* @return {IContact[]}
|
||||
*/
|
||||
customers(customersIds: number[]) {
|
||||
const { Contact } = this.models;
|
||||
return Contact.query().modifier('customer').whereIn('id', customersIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customers of the given ids with associated sales invoices.
|
||||
* @param {number[]} customersIds - Customers ids.
|
||||
*/
|
||||
customersWithSalesInvoices(customersIds: number[]) {
|
||||
const { Contact } = this.models;
|
||||
return Contact.query().modify('customer')
|
||||
.whereIn('id', customersIds)
|
||||
.withGraphFetched('salesInvoices');
|
||||
constructor(knex, cache) {
|
||||
super(knex, cache);
|
||||
this.model = Customer;
|
||||
}
|
||||
|
||||
changeBalance(vendorId: number, amount: number) {
|
||||
const { Contact } = this.models;
|
||||
const changeMethod = (amount > 0) ? 'increment' : 'decrement';
|
||||
|
||||
return Contact.query()
|
||||
.where('id', vendorId)
|
||||
[changeMethod]('balance', Math.abs(amount));
|
||||
return super.changeNumber({ id: vendorId }, 'balance', amount);
|
||||
}
|
||||
|
||||
async changeDiffBalance(
|
||||
|
||||
Reference in New Issue
Block a user