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
	 ('BCIISUE','count_invoices_edit_issue','SCNIVED','','','count/invoices','count','Permission to count invoices issue button','BUT','fa-solid fa-file-invoice-dollar',1,false,false,NULL),
	 ('BCIVIEW','count_invoices_edit_view','SCNIVED','','','count/invoices','count','Permission to count invoices view invoice button','BUT','fa-solid fa-eye',1,false,false,NULL);

INSERT INTO public.core_user_role_has_permission (role_id,permission_code) VALUES
    (25,'BCIISUE'),
    (25,'BCIVIEW');     

UPDATE public.core_assets
	SET asset_parameters='{"form": [[{"id": "client_id", "cols": 3, "list": "api/v1/registry/registrylist/CLI ", "name": "client_id", "type": "dropdown", "label": "table_count_invoices_client_id", "mandatory": false}, {"id": "supplier_id", "cols": 3, "list": "api/v1/registry/registrylist/SUP", "name": "supplier_id", "type": "dropdown", "label": "table_count_invoices_supplier_id", "mandatory": false}, {"id": "invoice_date", "cols": 3, "name": "invoice_date", "type": "date", "label": "table_count_invoices_invoice_date", "mandatory": true}, {"id": "invoice_total", "cols": 3, "name": "invoice_total", "type": "int", "label": "table_count_invoices_invoice_total", "disabled": true}], [{"id": "invoice_vat", "cols": 3, "name": "invoice_vat", "type": "int", "label": "table_count_invoices_invoice_vat", "disabled": true}, {"id": "currency", "cols": 3, "list": "api/v1/misc/codelists/currencies", "name": "currency", "type": "dropdown", "label": "table_count_invoices_currency", "mandatory": true}, {"id": "reference", "cols": 3, "name": "reference", "type": "text", "label": "table_count_invoices_reference", "mandatory": false}, {"id": "date_due", "cols": 3, "name": "date_due", "type": "date", "label": "table_count_invoices_date_due", "mandatory": true}], [{"id": "", "cols": 3, "name": "", "type": "toggle-list", "label": "Select options", "elements": [{"id": "paid", "name": "paid", "label": "table_count_invoices_paid"}, {"id": "credit_note", "name": "credit_note", "label": "table_count_invoices_credit_note"}, {"id": "proforma", "name": "proforma", "label": "table_count_invoices_proforma"}], "mandatory": false}]]}'
	WHERE asset_name ='count_invoices_form';

UPDATE public.core_assets
	SET asset_parameters='{"form": [[{"id": "email", "cols": 6, "name": "email", "type": "text", "label": "self_profile_email", "mandatory": true, "relatedHidden": "id"}, {"id": "password", "cols": 6, "name": "password", "type": "text", "label": "self_profile_password", "mandatory": true}], [{"id": "language", "cols": 6, "list": "/api/v1/misc/codelists/jc_languages", "name": "language", "type": "dropdown", "label": "self_profile_language", "mandatory": true}, {"id": "confirm_password", "cols": 6, "name": "confirm_password", "type": "text", "label": "self_profile_confirm_password", "mandatory": true}], [{"id": "meta.profile.logo", "cols": 4, "name": "meta[profile][logo]", "type": "file", "label": "Logo", "folder": "settings", "filename": "company-logo.png", "onchange": "jivaCoreFunc.onChange.fileUpload(this);", "mandatory": false}, {"id": "meta.profile.pic", "cols": 4, "name": "meta[profile][pic]", "type": "file", "label": "Picture", "onchange": "jivaCoreFunc.onChange.fileUpload(this);", "mandatory": false}, {"id": "", "cols": 4, "name": "", "type": "toggle-list", "label": "Select", "elements": [{"id": "userStatusSwitch", "name": "userStatusSwitch", "label": "Status"}], "mandatory": false}], [{"id": "meta.profile.business_name", "cols": 3, "name": "meta[profile][business_name]", "type": "text", "label": "business_name", "mandatory": false}, {"id": "meta.profile.tax_number", "cols": 3, "name": "meta[profile][tax_number]", "type": "text", "label": "tax_number", "mandatory": false}, {"id": "meta.profile.vat_id", "cols": 3, "name": "meta[profile][vat_id]", "type": "text", "label": "vat_id", "mandatory": false}, {"id": "meta.profile.registry_authority", "cols": 3, "name": "meta[profile][registry_authority]", "type": "text", "label": "registry_authority", "mandatory": false}], [{"id": "meta.profile.street_address", "cols": 3, "name": "meta[profile][street_address]", "type": "text", "label": "street_address", "mandatory": false}, {"id": "meta.profile.city", "cols": 3, "name": "meta[profile][city]", "type": "text", "label": "city", "mandatory": false}, {"id": "meta.profile.country", "cols": 3, "name": "meta[profile][country]", "type": "text", "label": "country", "mandatory": false}, {"id": "meta.profile.reg_num", "cols": 3, "name": "meta[profile][reg_num]", "type": "text", "label": "reg_num", "mandatory": false}], [{"id": "meta.profile.bank_name", "cols": 3, "name": "meta[profile][bank_name]", "type": "text", "label": "bank_name", "mandatory": false}, {"id": "meta.profile.iban", "cols": 3, "name": "meta[profile][iban]", "type": "text", "label": "iban", "mandatory": false}, {"id": "meta.profile.swift_code", "cols": 3, "name": "meta[profile][swift_code]", "type": "text", "label": "swift_code", "mandatory": false}, {"id": "meta.profile.nominal_capital", "cols": 3, "name": "meta[profile][nominal_capital]", "type": "text", "label": "nominal_capital", "mandatory": false}], [{"id": "meta.profile.siteUrl", "cols": 3, "name": "meta[profile][siteUrl]", "type": "text", "label": "siteUrl", "mandatory": false}, {"id": "meta.profile.mail", "cols": 3, "name": "meta[profile][mail]", "type": "text", "label": "mail", "mandatory": false}, {"id": "meta.profile.phone_num", "cols": 3, "name": "meta[profile][phone_num]", "type": "text", "label": "phone_num", "mandatory": false}, {"id": "meta.profile.secondary_reg_num", "cols": 3, "name": "meta[profile][secondary_reg_num]", "type": "text", "label": "secondary_reg_num", "mandatory": false}]]}'
	WHERE asset_name = 'self_profile_view_form';

INSERT INTO public.core_permissions (code,permission_name,parent,permission_function,default_child,permission_namespace,"module",permission_description,permission_type,icon,ord,hidden,guest,meta) VALUES
	 ('XCIRGFD','count_invoices_rows_get_from_description','SCNIVED','getRowFromDesc','','count/invoices','count','Permission to execute getRowFromDesc function','EXE','',1,false,false,NULL),
	 ('XCIISUE','count_invoices_issue','SCNIVED','updInvoiceStatus','','count/invoices','count','Permission to execute invoices change status','EXE','',1,false,false,NULL);

INSERT INTO public.core_user_role_has_permission (role_id,permission_code) VALUES
    (25,'XCIRGFD'),
    (25,'XCIISUE');  

UPDATE public.core_settings cs
	SET value='{
  "content": [
    {
      "font": {
        "face": "montserrat",
        "size": 11,
        "type": ""
      },
      "rows": [
        [
          {
            "font": {
              "type": "B"
            },
            "text": "Receiver",
            "align": "L",
            "width": 77,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          },
          {
            "font": {
              "type": "B"
            },
            "text": "Invoice",
            "align": "L",
            "width": 37,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          },
          {
            "font": {
              "type": "B"
            },
            "text": "Date",
            "align": "L",
            "width": 37,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          },
          {
            "font": {
              "type": "B"
            },
            "text": "Total",
            "align": "L",
            "width": 37,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          }
        ],
        [
          {
            "text": "{{0:registry_entity}}\n{{0:registry_address}}\n{{0:registry_loc}}\n{{0:registry_vat}}",
            "align": "L",
            "width": 77,
            "border": "B"
          },
          {
            "text": "{{0:invoice_number}}\n\n\n\n",
            "align": "L",
            "width": 37,
            "border": "B"
          },
          {
            "text": "{{0:invoice_date}}\n\n\n\n",
            "align": "L",
            "width": 37,
            "border": "B"
          },
          {
            "font": {
              "type": "B"
            },
            "text": "{{0:invoice_total}}\n\n\n\n",
            "align": "R",
            "width": 37,
            "border": "B"
          }
        ]
      ],
      "type": "table",
      "rowHeight": 7
    },
    {
      "type": "spacer",
      "height": 15
    },
    {
      "font": {
        "face": "montserrat",
        "size": 11,
        "type": ""
      },
      "rows": [
        [
          {
            "font": {
              "type": "B"
            },
            "text": "Description",
            "align": "L",
            "width": 151,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          },
          {
            "font": {
              "type": "B"
            },
            "text": "Amount",
            "align": "L",
            "width": 37,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          }
        ]
      ],
      "type": "table",
      "rowHeight": 7
    },
    {
      "font": {
        "face": "montserrat",
        "size": 11,
        "type": ""
      },
      "loop": [
        {
          "text": "{{1:description}}",
          "align": "L",
          "width": 151,
          "border": 0
        },
        {
          "text": "{{1:unit_price}}",
          "align": "R",
          "width": 37,
          "border": 0
        }
      ],
      "type": "table",
      "dataset": 1,
      "rowHeight": 7
    },
    {
      "type": "setY",
      "value": 190
    },
    {
      "font": {
        "face": "montserrat",
        "size": 11,
        "type": ""
      },
      "rows": [
        [
          {
            "font": {
              "type": "B"
            },
            "text": "Total",
            "align": "L",
            "width": 151,
            "border": "B"
          },
          {
            "font": {
              "type": "B"
            },
            "text": "{{0:invoice_total}}",
            "align": "R",
            "width": 37,
            "border": "B"
          }
        ]
      ],
      "type": "table",
      "rowHeight": 7
    },
    {
      "type": "spacer",
      "height": 15
    },
    {
      "font": {
        "face": "montserrat",
        "size": 11,
        "type": ""
      },
      "rows": [
        [
          {
            "font": {
              "type": "B"
            },
            "text": "Notes",
            "align": "L",
            "width": 188,
            "border": 0,
            "textColor": [
              255,
              255,
              255
            ],
            "backgroundColor": [
              0,
              0,
              185
            ]
          }
        ],
        [
          {
            "text": "Versamento da effettuare sul conto:\nIBAN: SI56 1010 0005 9559 838\nSWIFT: BAKOSI2X",
            "align": "L",
            "width": 188,
            "border": "B"
          }
        ]
      ],
      "type": "table",
      "rowHeight": 7
    }
  ],
  "queries": [
    "select \n    re.entity_name as registry_entity, re.entity_address as registry_address, re.meta->>''vat'' as registry_vat,\n    CONCAT(re.entity_pcode, '' - '', re.entity_locality, '' - '', cc.description) as registry_loc,\n    ci.invoice_number, ci.invoice_date, ci.invoice_total\nfrom count_invoices ci \nleft join registry_entities re on ci.client_id = re.id\nleft join code_list_countries cc on cc.code = re.entity_country\nwhere ci.id = :id;",
    "SELECT * FROM count_invoices_rows WHERE invoice_id=:id",
    "select cu.meta->''profile''->>''business_name'' as business_name\nfrom count_invoices ci \njoin core_users cu on ci.created_by = cu.id \nwhere ci.id=:id "
  ],
  "firstPage": {
    "footer": {
      "font": {
        "face": "montserrat",
        "size": 7,
        "type": ""
      },
      "text": "",
      "width": 190,
      "border": 1,
      "height": 25,
      "content": [
        {
          "rows": [
            "{{2:business_name}}",
            "Davčna št: 73478776",
            "VAT ID: SI 73478776",
            "Registrski organ: Okrožno sodišče v Kopru"
          ],
          "width": 60
        },
        {
          "rows": [
            "Šmarska Cesta 7c",
            "6000 Koper",
            "Slovenija",
            "Reg. Št: 2021/41784"
          ],
          "width": 43
        },
        {
          "rows": [
            "Banka Intesa Sanpaolo d.d.",
            "IBAN SI56 1010 005 9559 838",
            "SWIFT BAKOSI2X",
            "Osnovni kapital: 7.500 EUR"
          ],
          "width": 50
        },
        {
          "rows": [
            "www.jivalabs.com",
            "info@jivalabs.com",
            "+386 70 179 333",
            "M. Št: 8974632000"
          ],
          "width": 43
        }
      ],
      "position": {
        "x": 10,
        "y": 265
      }
    },
    "header": {
      "font": {
        "face": "montserrat",
        "size": 12,
        "type": "I"
      },
      "logo": {
        "url": "../storage/settings/company-logo.png",
        "Xpos": 87.5,
        "Ypos": 10,
        "width": 35
      },
      "text": "",
      "width": 190,
      "border": 0,
      "height": 10
    }
  },
  "otherPages": {
    "footer": {
      "font": {
        "face": "montserrat",
        "size": 7,
        "type": ""
      },
      "text": "",
      "width": 190,
      "border": 1,
      "height": 25,
      "content": [
        {
          "rows": [
            "JivaLabs, Razvoj Računalniških Rešitev, d.o.o.",
            "Davčna št: 73478776",
            "VAT ID: SI 73478776",
            "Registrski organ: Okrožno sodišče v Kopru"
          ],
          "width": 60
        },
        {
          "rows": [
            "Šmarska Cesta 7c",
            "6000 Koper",
            "Slovenija",
            "Reg. Št: 2021/41784"
          ],
          "width": 43
        },
        {
          "rows": [
            "Banka Intesa Sanpaolo d.d.",
            "IBAN SI56 1010 005 9559 838",
            "SWIFT BAKOSI2X",
            "Osnovni kapital: 7.500 EUR"
          ],
          "width": 50
        },
        {
          "rows": [
            "www.jivalabs.com",
            "info@jivalabs.com",
            "+386 70 179 333",
            "M. Št: 8974632000"
          ],
          "width": 43
        }
      ],
      "position": {
        "x": 10,
        "y": 265
      }
    },
    "header": {
      "font": {
        "face": "montserrat",
        "size": 12,
        "type": "I"
      },
      "logo": {
        "url": "../storage/settings/company-logo.png",
        "Xpos": 10,
        "Ypos": 10,
        "width": 30
      },
      "text": "",
      "width": 190,
      "border": 0,
      "height": 10
    }
  },
  "additionalFonts": [
    {
      "file": "Montserrat-Regular.php",
      "name": "montserrat",
      "type": ""
    },
    {
      "file": "Montserrat-Bold.php",
      "name": "montserrat",
      "type": "B"
    },
    {
      "file": "Montserrat-Italic.php",
      "name": "montserrat",
      "type": "I"
    }
  ]
}'::jsonb,properties='{
  "section_desc": "Count Module Settings",
  "section_icon": "fa-solid fa-calculator",
  "setting_desc": "Invoice template",
  "setting_icon": "fa-solid fa-file-invoice",
  "section_button": "Change template",
  "setting_button": "Edit"
}'::jsonb
	WHERE cs.setting_type = 'count_settings' and cs.setting_name = 'invoice_template';

INSERT INTO public.core_permissions (code,permission_name,parent,permission_function,default_child,permission_namespace,"module",permission_description,permission_type,icon,ord,hidden,guest,meta) VALUES
	 ('XCCFADD','count_cash_flow_add','SCCFLED','addCountCashFlow','','count/cash_flow','count','Permission to create cash flows','EXE','',1,false,false,NULL),
	 ('XCCFIMP','count_cash_flow_import_file','SCCFLED','importCashFlowFile','','count/cash_flow','count','Permission to import cash flow files','EXE','',1,false,false,NULL),
	 ('XCCFDEL','count_cash_flow_del','SCCFLED','delCountCashFlow','','count/cash_flow','count','Permission to delete count cash flows','EXE','',1,false,false,NULL),
	 ('SCCFLED','count_cash_flow_edit','PCNTCFL','','','count/cash_flow','count','Section to edit count cash flows','SEC','',1,false,false,NULL),
	 ('BCCFBCK','count_cash_flow_edit_back','SCCFLED','','','count/cash_flow','count','Count cash flow back button','BUT','fas fa-arrow-left',1,false,false,NULL),
	 ('BCCFSAV','count_cash_flow_edit_save','SCCFLED','','','count/cash_flow','count','Count cash flow save button','BUT','fas fa-save',1,false,false,NULL),
	 ('XCCFGET','count_cash_flow_get','SCCFLVW','getCountCashFlow','','count/cash_flow','count','Permission to get count cash flows','EXE','',1,false,false,NULL),
	 ('PCNTCFL','count_cash_flow_page','MCOUNT','getObjects','','count/cash_flow','count','Cash flow management page','PAG','fa-solid fa-money-bill-wave',1,false,false,NULL),
	 ('XCCFUPD','count_cash_flow_update','SCCFLED','updCountCashFlow','','count/cash_flow','count','Permission to update count cash flow','EXE','',1,false,false,NULL),
	 ('SCCFLVW','count_cash_flow_view','PCNTCFL','','','count/cash_flow','count','Section to view count cash flow','SEC','',1,false,false,NULL),
	 ('BCCFADD','count_cash_flow_view_add','SCCFLVW','','','count/cash_flow','count','Add cash flow button','BUT','fas fa-plus',1,false,false,NULL);
INSERT INTO public.core_permissions (code,permission_name,parent,permission_function,default_child,permission_namespace,"module",permission_description,permission_type,icon,ord,hidden,guest,meta) VALUES
	 ('BCCFDEL','count_cash_flow_view_del','SCCFLVW','','','count/cash_flow','count','Delete cash flow button','BUT','fas fa-trash',1,false,false,NULL),
	 ('BCCFEDT','count_cash_flow_view_edit','SCCFLVW','','','count/cash_flow','count','Edit cash flow button','BUT','fas fa-edit',1,false,false,NULL);



CREATE UNIQUE INDEX count_cash_flow_id_idx ON public.count_cash_flow USING btree (id);