Command içeriği burada Payment'ı buluyor foreach içine alıyor ve bir service'e gönderiyorum.
https://prnt.sc/iIQKPCjFsNDJ lines
https://prnt.sc/V4yvVauo4oLM payment->data
public function handle()
{
$paymentId = $this->argument('payment_id');
if(is_null($paymentId)) {
$this->error('This payment id is invalid');
return;
}
$payment = Payment::find($paymentId);
if(is_null($payment)) {
$this->error('Payment not found');
return;
}
foreach ($payment->data as $key => $line) {
if ($line->product->auto_invoice) {
$this->createInvoice($payment, $line);
}
}
}
public function createInvoice($payment, $line)
{
$turkcellservice = new TurkcellService();
$invoice = $turkcellservice->login()
->createOutboxInvoice($payment, $line);
}
Servis kısmında yakalıyorum ve bir alt metoda gönderiyorum burada.
public function createOutboxInvoice(Payment $payment, $lines, $forceArsiv = false, $forceEInvoice = false)
{
$url = $this->baseUrl . '/outboxinvoice/create';
$eInvoiceUser = optional($payment->invoiceAddress)->is_company && optional($payment->invoiceAddress)->e_invoice ? true : false;
if ($forceArsiv) {
$eInvoiceUser = false;
}
if($forceEInvoice) {
$eInvoiceUser = true;
}
$invoice = $this->buildInvoice($payment, $lines, $eInvoiceUser);
$invoice->setRecordType($eInvoiceUser ? 1 : 0);
}
Bir alt metodum da $lines değişkenini foreach'e alarak tekrar dönmem gerekiyor fakat foreach'e girdiğim de sadece true yazıyor fakat $payment->data olarak kullanınca $lines'ın içeriğinin aynısını veriyor burada bir hata mı var acaba lines ve payment->data içeriklerini de ekliyorum yukarıda
private function buildInvoice(Payment $payment, $lines, $eInvoiceUser)
{
$address = $payment->invoiceAddress;
$user = $payment->user;
$addressBook = new AddressBookModel();
$addressBook->setIdentificationNumber((string)$address->tax_number);
$addressBook->setName($address->is_company ? $address->company_name : $address->first_name);
$addressBook->setReceiverPersonSurName($address->last_name);
$addressBook->setReceiverStreet($address->address);
$addressBook->setReceiverDistrict(optional($address->district)->name);
$addressBook->setReceiverCity(optional($address->city)->name);
$addressBook->setReceiverCountry('tr');
$addressBook->setReceiverPhoneNumber($address->phone_number);
$addressBook->setReceiverEmail($user->email);
$addressBook->setReceiverTaxOffice($address->tax_office);
$aliasInvoices = PaymentTurkcellIntegration::whereIn('payment_product_id', PaymentProduct::where('payment_id', $payment->id)->pluck('id'))
->whereNotNull('alias')
->first();
if($aliasInvoices) {
$addressBook->setAlias($aliasInvoices->alias);
}
$generalInfo = new GeneralInfoModel();
$generalInfo->setInvoiceProfileType($eInvoiceUser ? InvoiceProfileType::COMMERCIAL : InvoiceProfileType::ARCHIVE);
$generalInfo->setIssueDate(now());
$generalInfo->setCurrencyCode('TRY');
// $generalInfo->setPrefix('TC6');
$generalInfo->setExchangeRate(1);
$generalInfo->setTotalAmount($payment->price);
$generalInfo->setType(1);
$eArchiveInfo = new EArchiveInfoModel();
$eArchiveInfo->setSendEmail(true);
$eArchiveInfo->setAllowOldEArsivCustomer(false);
$invoiceLines = [];
$invoiceCount=null;
foreach ($payment->data as $lineKey => $line) {
Log::info($line);
/** @var PaymentProduct $paymentProduct */
$invoiceCount=$line->qty ? $line->qty : 1;
$invoiceLine = new InvoiceLineModel();
$invoiceLine->setDisableVatExemption(true);
$invoiceLine->setInventoryCard($line->product->name);
$invoiceLine->setAmount($line->qty ? $line->qty : 1);
$invoiceLine->setUnitPrice($line->item_data['main_price']);
$invoiceLine->setUnitCode('C62');
$invoiceLine->setVatRate($line->tax);
$invoiceLine->setLineAmount($line->grand_total);
$amount = $line->grand_total;
$totalDiscountAmount = 0;
if ($line->discount) {
$totalDiscountAmount = $line->discount;
$invoiceLine->setDiscountAmount($line->discount);
$invoiceLine->setDiscountRate($line->discount * 100 / $amount);
}
if($line->discountCode) {
$price = $line->item_data['main_price'];
if( $line->item_data['discount_type'] == '%' ) {
$discountTotal = (($price * $line->discountCode->amount) / 100)*$invoiceCount;
$totalDiscountAmount = $discountTotal;
$invoiceLine->setDiscountAmount($discountTotal);
$invoiceLine->setDiscountRate($line->discountCode->amount);
}else if( $line->item_data['discount_type'] == 'TL' ) {
$totalDiscountAmount = ($line->discountCode->amount)*$invoiceCount;
$invoiceLine->setDiscountAmount($totalDiscountAmount);
$discountRate = (100 * $line->discountCode->amount) / ($price + $line->discountCode->amount);
$invoiceLine->setDiscountRate($discountRate);
}
}
if($line->wallet_status) {
$price = $line->item_data['main_price'];
$totalDiscountAmount = $totalDiscountAmount + $line->wallet_status;
$discountRate = (100 * $totalDiscountAmount) / ($price + $totalDiscountAmount);
$invoiceLine->setDiscountAmount($totalDiscountAmount);
$invoiceLine->setDiscountRate($discountRate);
}
// $subtotal = $amount - $line->grand_total;
$invoiceLines[] = $invoiceLine->getJsonObject();
}