diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2026-01-15 11:17:37 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2026-01-15 11:17:37 +0700 |
| commit | bd6c4da5e84a1b0aa98a9b41918cd70f836d2b59 (patch) | |
| tree | 11522eaf7efa2e1378379b57b7817229c19c5195 | |
| parent | 69d1aba2d4da72667bd2a43fa35e591a2caadba5 (diff) | |
benerin upload bills jadiin queuing
| -rwxr-xr-x | fixco_custom/models/stock_picking.py | 2 | ||||
| -rw-r--r-- | fixco_custom/models/upload_bills.py | 216 | ||||
| -rw-r--r-- | fixco_custom/models/upload_ginee.py | 2 | ||||
| -rw-r--r-- | fixco_custom/views/upload_bills.xml | 3 |
4 files changed, 173 insertions, 50 deletions
diff --git a/fixco_custom/models/stock_picking.py b/fixco_custom/models/stock_picking.py index 526a0eb..c72f3a4 100755 --- a/fixco_custom/models/stock_picking.py +++ b/fixco_custom/models/stock_picking.py @@ -223,7 +223,7 @@ class StockPicking(models.Model): def action_create_invoice_from_mr(self): """Create the invoice associated to the PO. """ - if not self.env.user.id == 13: + if self.env.user.id not in (13, 15, 8): raise UserError('Hanya Accounting yang bisa membuat Bill') precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') diff --git a/fixco_custom/models/upload_bills.py b/fixco_custom/models/upload_bills.py index 0c037f3..3b460de 100644 --- a/fixco_custom/models/upload_bills.py +++ b/fixco_custom/models/upload_bills.py @@ -126,70 +126,192 @@ class UploadBills(models.Model): } } + def queue_job(self): + self.date_upload = datetime.utcnow() + for line in self.bills_lines: + queue_job = self.env['queue.job'].search([('res_id', '=', line.id), ('method_name', '=', 'action_create_bills'), ('state', '!=', 'error')], limit=1) + if queue_job: + continue + self.env['queue.job'].create({ + 'name': f'Upload Bills {line.no_bu}', + 'model_name': 'upload.bills.line', + 'method_name': 'action_create_bills', + 'res_id': line.id, + }) + + # def action_create_bills(self): + # all_invoice_ids = [] + + # failed_bus = [] + + # for line in self.bills_lines: + # bu_in = self.env['stock.picking'].search([ + # ('name', '=', line.no_bu), + # ('picking_type_code', '=', 'incoming') + # ], limit=1) + + # if not bu_in: + # failed_bus.append(f"{line.no_bu} (BU tidak ditemukan)") + # continue + + # try: + # created_invoices = bu_in.action_create_invoice_from_mr() + # except Exception as e: + # failed_bus.append(f"{bu_in.name} ({str(e)})") + # continue + + # # Sinkron faktur dan tanggal ke account.move + # if isinstance(created_invoices, dict) and 'res_id' in created_invoices: + # try: + # invoice = self.env['account.move'].browse(created_invoices['res_id']) + # invoice.write({ + # 'faktur_pajak': line.faktur_pajak, + # 'invoice_date': line.date + # }) + # invoice.action_post() + # all_invoice_ids.append(invoice.id) + # except Exception as e: + # failed_bus.append(f"{bu_in.name} ({str(e)})") + # continue + + # elif isinstance(created_invoices, dict) and 'domain' in created_invoices: + # try: + # # Banyak invoice + # invoice_ids = created_invoices.get('domain', [])[0][2] or [] + # invoices = self.env['account.move'].browse(invoice_ids) + # invoices.write({ + # 'faktur_pajak': line.faktur_pajak, + # 'invoice_date': line.date + # }) + # invoices.action_post() + # all_invoice_ids.extend(invoices.ids) + # except Exception as e: + # failed_bus.append(f"{bu_in.name} ({str(e)})") + # continue + + # else: + # raise UserError("Gagal menemukan invoice yang baru dibuat.") + + # if failed_bus: + # raise UserError( + # "Beberapa BU IN gagal diproses:\n- " + "\n- ".join(failed_bus) + # ) + + # if not all_invoice_ids: + # return {'type': 'ir.actions.act_window_close'} + + # action = self.env.ref('account.action_move_in_invoice_type').read()[0] + # if len(all_invoice_ids) == 1: + # action.update({ + # 'view_mode': 'tree,form', + # 'res_id': all_invoice_ids, + # }) + # else: + # action.update({ + # 'domain': [('id', 'in', all_invoice_ids)], + # 'view_mode': 'tree,form', + # }) + # return action + + +class UploadBillsLine(models.Model): + _name = "upload.bills.line" + _description = "Upload Bills Line" + _inherit = ['mail.thread'] + + upload_bills_id = fields.Many2one('upload.bills', string='Upload') + no_bu = fields.Char('No BU IN') + date = fields.Date('Date') + faktur_pajak = fields.Char('Faktur Pajak') + state = fields.Selection([ + ('draft', 'Draft'), + ('waiting', 'Waiting Another Operation'), + ('confirmed', 'Waiting'), + ('assigned', 'Ready'), + ('done', 'Done'), + ('cancel', 'Cancelled'), + ], string='Status', compute='_compute_state', + copy=False, readonly=True, tracking=True) + + + def _compute_state(self): + for line in self: + picking = self.env['stock.picking'].search([ + ('name', '=', line.no_bu), + ('picking_type_code', '=', 'incoming') + ], limit=1) + line.state = picking.state + def action_create_bills(self): - all_invoice_ids = [] + # all_invoice_ids = [] - for line in self.bills_lines: + failed_bus = [] + + for line in self: bu_in = self.env['stock.picking'].search([ ('name', '=', line.no_bu), ('picking_type_code', '=', 'incoming') ], limit=1) if not bu_in: - raise UserError(f"BU IN '{line.no_bu}' tidak ditemukan.") + failed_bus.append(f"{line.no_bu} (BU tidak ditemukan)") + continue - # Panggil pembuatan invoice - created_invoices = bu_in.action_create_invoice_from_mr() + try: + created_invoices = bu_in.action_create_invoice_from_mr() + except Exception as e: + failed_bus.append(f"{bu_in.name} ({str(e)})") + continue # Sinkron faktur dan tanggal ke account.move if isinstance(created_invoices, dict) and 'res_id' in created_invoices: - # Satu invoice - invoice = self.env['account.move'].browse(created_invoices['res_id']) - invoice.write({ - 'faktur_pajak': line.faktur_pajak, - 'invoice_date': line.date - }) - invoice.action_post() - all_invoice_ids.append(invoice.id) + try: + invoice = self.env['account.move'].browse(created_invoices['res_id']) + invoice.write({ + 'faktur_pajak': line.faktur_pajak, + 'invoice_date': line.date + }) + invoice.action_post() + # all_invoice_ids.append(invoice.id) + except Exception as e: + failed_bus.append(f"{bu_in.name} ({str(e)})") + continue elif isinstance(created_invoices, dict) and 'domain' in created_invoices: - # Banyak invoice - invoice_ids = created_invoices.get('domain', [])[0][2] or [] - invoices = self.env['account.move'].browse(invoice_ids) - invoices.write({ - 'faktur_pajak': line.faktur_pajak, - 'invoice_date': line.date - }) - invoices.action_post() - all_invoice_ids.extend(invoices.ids) + try: + # Banyak invoice + invoice_ids = created_invoices.get('domain', [])[0][2] or [] + invoices = self.env['account.move'].browse(invoice_ids) + invoices.write({ + 'faktur_pajak': line.faktur_pajak, + 'invoice_date': line.date + }) + invoices.action_post() + # all_invoice_ids.extend(invoices.ids) + except Exception as e: + failed_bus.append(f"{bu_in.name} ({str(e)})") + continue else: raise UserError("Gagal menemukan invoice yang baru dibuat.") - # Tampilkan invoice(s) ke user - if not all_invoice_ids: - return {'type': 'ir.actions.act_window_close'} + if failed_bus: + raise UserError( + "Beberapa BU IN gagal diproses:\n- " + "\n- ".join(failed_bus) + ) - action = self.env.ref('account.action_move_in_invoice_type').read()[0] - if len(all_invoice_ids) == 1: - action.update({ - 'view_mode': 'tree,form', - 'res_id': all_invoice_ids, - }) - else: - action.update({ - 'domain': [('id', 'in', all_invoice_ids)], - 'view_mode': 'tree,form', - }) - return action - - -class UploadBillsLine(models.Model): - _name = "upload.bills.line" - _description = "Upload Bills Line" - _inherit = ['mail.thread'] + # if not all_invoice_ids: + # return {'type': 'ir.actions.act_window_close'} - upload_bills_id = fields.Many2one('upload.bills', string='Upload') - no_bu = fields.Char('No BU IN') - date = fields.Date('Date') - faktur_pajak = fields.Char('Faktur Pajak') + # action = self.env.ref('account.action_move_in_invoice_type').read()[0] + # if len(all_invoice_ids) == 1: + # action.update({ + # 'view_mode': 'tree,form', + # 'res_id': all_invoice_ids, + # }) + # else: + # action.update({ + # 'domain': [('id', 'in', all_invoice_ids)], + # 'view_mode': 'tree,form', + # }) + # return action diff --git a/fixco_custom/models/upload_ginee.py b/fixco_custom/models/upload_ginee.py index 9830383..b211709 100644 --- a/fixco_custom/models/upload_ginee.py +++ b/fixco_custom/models/upload_ginee.py @@ -174,7 +174,7 @@ class UploadGinee(models.Model): if queue_job: continue self.env['queue.job'].create({ - 'name': f'Get Order Ginee {line.invoice_marketplace}', + 'name': f'Get Order Ginee {line.invoice_marketplace}', 'model_name': 'upload.ginee.line', 'method_name': 'get_order_id_and_create_detail_order', 'res_id': line.id, diff --git a/fixco_custom/views/upload_bills.xml b/fixco_custom/views/upload_bills.xml index 472fefc..5e09306 100644 --- a/fixco_custom/views/upload_bills.xml +++ b/fixco_custom/views/upload_bills.xml @@ -20,7 +20,7 @@ <form string="Upload Bills"> <header> <button name="action_import_excel" string="Import Excel" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> - <button name="action_create_bills" string="Create Bills" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> + <button name="queue_job" string="Create Bills" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> <field name="number" widget="field_no_edit" options="{'no_open': True}"/> <field name="date_upload"/> <field name="user_id" widget="field_no_edit" options="{'no_open': True}"/> @@ -33,6 +33,7 @@ <field name="bills_lines"> <tree editable="bottom"> <field name="no_bu"/> + <field name="state"/> <field name="date"/> <field name="faktur_pajak"/> </tree> |
