
CREATE TABLE public.code_list_count_document_types (
	code varchar(3) NOT NULL,
	description varchar(60) NOT NULL,
	ord int8 NULL,
	icon varchar(255) NULL,
	misc varchar(255) NULL,
	properties jsonb NULL,
	CONSTRAINT idx_primary PRIMARY KEY (code)
);
CREATE UNIQUE INDEX code_list_count_document_types_idx ON public.code_list_count_document_types USING btree (code);

INSERT INTO public.code_list_count_document_types (code,description)
	VALUES ('INV','Invoice');
INSERT INTO public.code_list_count_document_types (code,description)
	VALUES ('CDN','Credit note');
INSERT INTO public.code_list_count_document_types (code,description)
	VALUES ('QOT','Quotation');
INSERT INTO public.code_list_count_document_types (code,description)
	VALUES ('AVN','Advanced');


ALTER TABLE public.count_invoices ADD document_type varchar(3) NULL;

update count_invoices set document_type = 'INV' where credit_note = false and proforma = false;
update count_invoices set document_type = 'CDN' where credit_note = true and proforma = false;
update count_invoices set document_type = 'QOT' where credit_note = false and proforma = true;

INSERT INTO core_permissions (code,permission_name,parent,permission_function,default_child,permission_namespace,"module",permission_description,permission_type,icon,ord,hidden,guest,meta) VALUES
	 ('BCINADA','count_invoices_view_add_advanced','SCNTIVW','','','count/invoices','count','Add advanced button','BUT','fa-solid fa-file-invoice-dollar',1,false,false,NULL);

INSERT INTO core_permissions (code,permission_name,parent,permission_function,default_child,permission_namespace,"module",permission_description,permission_type,icon,ord,hidden,guest,meta) VALUES
	 ('BCINSRC','count_invoices_button_search','SCNTIVW','','','count/invoices','count','Filter button search','BUT','fa-solid fa-magnifying-glass',1,false,false,NULL),
	 ('BCINFLT','count_invoices_button_filters','SCNTIVW','','','count/invoices','count','Filter button filters','BUT','fa-solid fa-filter',1,false,false,NULL),
	 ('BCINRMV','count_invoices_button_filters_remove','SCNTIVW','','','count/invoices','count','Filter button remove','BUT','fa-solid fa-xmark',1,false,false,NULL);


INSERT INTO core_assets (asset_type,asset_name,description,custom,asset_innerhtml,asset_parameters,asset_code,before_trigger,after_trigger,failure_rollback,exec_order,meta,role_id) VALUES
	 ('form','count_invoices_filters_form','Form for count invoices list filters',false,NULL,'{"form": [[{"id": "re.entity_name", "cols": 3, "name": "re.entity_name", "type": "text", "label": "form_count_invoices_list_filters_client_name", "mandatory": false, "onkeydown": "jivaCoreFunc.setFilter(event);", "filterType": "like"}]]}',NULL,NULL,NULL,NULL,NULL,NULL,NULL);

DROP INDEX public.count_invoices_reference_idx;

CREATE UNIQUE INDEX count_invoices_reference_idx ON public.count_invoices (reference,document_type);
