Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function send_invoice_simple($invoice, $customer_jurnal_id)
- {
- try {
- // Ambil detail invoice
- $details = $this->db->get_where('sid_invoice_detail', ['invoice_id' => $invoice->invoice_id])->result();
- if (empty($details)) {
- return ['success' => false, 'message' => 'Detail invoice kosong'];
- }
- // Siapkan transaction lines
- $transactionLines = [];
- foreach ($details as $detail) {
- $product_name = in_array($detail->invoice_detail_desc, ['Biaya Instalasi', 'Sewa Alat']) ? $detail->invoice_detail_desc : 'Tagihan Bulanan';
- $transactionLines[] = [
- 'product_name' => $product_name,
- 'quantity' => (int)($detail->invoice_detail_qty ?? 1),
- 'rate' => (int)($detail->invoice_detail_tarif ?? 0),
- 'discount' => (int)($detail->invoice_detail_disc ?? 0)
- ];
- }
- // Siapkan payload
- $payload = [
- 'sales_invoice' => [
- 'transaction_date' => date('Y-m-d', strtotime($invoice->invoice_tanggal)),
- 'due_date' => date('Y-m-d', strtotime($invoice->invoice_tanggal_tempo)),
- 'transaction_no' => $invoice->invoice_nomor,
- 'person_id' => $customer_jurnal_id, // Gunakan person_id bukan person_name
- 'address' => $invoice->pelanggan_alamat ?? '',
- 'term_name' => $invoice->jenis_bayar_nama ?? 'Custom',
- 'transaction_lines_attributes' => $transactionLines,
- 'is_shipped' => false,
- 'shipping_price' => 0,
- // 'discount_unit' => (int)($invoice->invoice_diskon ?? 0),
- 'discount_unit' => 0,
- 'discount_type_name' => 'value',
- 'deposit_to_name' => 'KAS KECIL KANTOR',
- 'deposit' => (int) $invoice->invoice_dibayar ?? 0,
- 'source' => 'ISKNet.id',
- 'memo' => 'Invoice dari ISKNet.id'
- ]
- ];
- // Kirim ke Mekari
- $response = $this->mekari_jurnal->request(
- 'POST',
- '/public/jurnal/api/v1/sales_invoices',
- $payload
- );
- $httpCode = $response['http_code'] ?? 0;
- if ($httpCode === 201 && isset($response['response']['sales_invoice']['id'])) {
- $jurnal_id = $response['response']['sales_invoice']['id'];
- $jurnal_no = $response['response']['sales_invoice']['transaction_no'];
- // Update database
- $this->db->where('invoice_id', $invoice->invoice_id)->update('sid_invoice', [
- 'invoice_jurnal_id' => $jurnal_id,
- 'invoice_jurnal_no' => $jurnal_no,
- 'invoice_jurnal_status' => $response['response']['sales_invoice']['status'] ?? 'open',
- 'invoice_jurnal_response' => json_encode($response['response']['sales_invoice']),
- 'invoice_jurnal_sync_at' => date('Y-m-d H:i:s'),
- 'invoice_update' => date('Y-m-d H:i:s')
- ]);
- // PAID INVOICE
- $paid = $this->create_receive_payment_paid($invoice, $jurnal_id, $customer_jurnal_id);
- if (!$paid['success']) {
- return [
- 'success' => false,
- 'message' => 'Invoice terkirim, Gagal set Paid : ' . $paid['message'],
- 'jurnal_id' => $jurnal_id
- ];
- }
- return [
- 'success' => true,
- 'message' => 'Invoice Berhasil dikirim & dibayar',
- 'jurnal_id' => $jurnal_id
- ];
- } else {
- $error = $this->extract_error_simple($response);
- // Update dengan status failed
- $this->db->where('invoice_id', $invoice->invoice_id)->update('sid_invoice', [
- 'invoice_jurnal_status' => 'failed',
- 'invoice_jurnal_error' => $error,
- 'invoice_jurnal_response' => json_encode($response['response'] ?? []),
- 'invoice_jurnal_sync_at' => date('Y-m-d H:i:s'),
- 'invoice_update' => date('Y-m-d H:i:s')
- ]);
- return ['success' => false, 'message' => 'Gagal: ' . $error];
- }
- } catch (Exception $e) {
- return ['success' => false, 'message' => 'API Error: ' . $e->getMessage()];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment