我在CakePHP中编写了一个应用程序。我正面临一个问题。有时,当订单下达时,它没有反映在数据库中,但我收到了一封电子邮件,它反映在我的PayU帐户信息中心,有人订购了x金额的订单。< / p>
以下是我的代码。有什么问题?
if(!empty($this->data)){
$data = $this->data;
$userInfo = $this->User->find('first',array('conditions'=>array('User.id'=>$userID)));
$sales_rep_email = $userInfo["User"]["sales_rep_email"];
$user_email = $userInfo["User"]["primary_contact_email"];
$virtual_account = $userInfo["User"]["virtual_account"];
if($data['payment_method']=="RTGS Payment"){
$orderData['Order']['invoice_no'] = 0;
$orderData['Order']['invoice_prefix'] = "INV-2015-00";
$orderData['Order']['user_id'] = $userInfo['User']['id'];
$orderData['Order']['name'] = $userInfo['User']['name'];
$orderData['Order']['email'] = $userInfo['User']['primary_contact_email'];
$orderData['Order']['phone'] = $userInfo['User']['primary_contact_number'];
$orderData['Order']['billing_address'] = $userInfo['User']['address'].", ".$userInfo['User']['city'].", ".$userInfo['User']['district'].", ".$userInfo['User']['state']."-".$userInfo['User']['pin_code'];
$orderData['Order']['order_item_count'] = $shop['Order']['quantity'];
$orderData['Order']['shipping'] = $data['shipping_method'];
$orderData['Order']['total'] = $shop['Order']['total'];
$orderData['Order']['payment_method'] = $data['payment_method'];
if($data['shipping_method']=="Express Delivery"){
$orderData['Order']['delivery_charges'] = 400;
}
if($data['payment_method']=="RTGS Payment"){
$orderData['Order']['status'] = 'open';
$orderData['Order']['temp_status'] = 'rtgs';
}else if($data['payment_method']=="COD"){
$orderData['Order']['status'] = 'open';
$orderData['Order']['temp_status'] = 'cod';
$orderData['Order']['customer_status'] = 'unconfirmed';
}else{
$orderData['Order']['status'] = 'open';
$orderData['Order']['temp_status'] = 'open';
}
$this->Order->save($orderData);
$id = $this->Order->getLastInsertId();
$orderupdateData['Order']['orderid'] = date("Y").date("m").'-'.$id;
$orderupdateData['Order']['temporary_order'] = "TO".'-'.$id;
$orderupdateData['Order']['id'] = $id;
$this->Order->save($orderupdateData);
$orderItemData = array();
foreach($shop['OrderItem'] as $key=>$item){
$quantityNewCheckOut = 0;
if(!empty($data['payment_method'])){
$this->manageInventory($item['Product']['smb_code'],$item['quantity']);
}
$orderItemData['OrderItem']['order_id'] = $id;
$orderItemData['OrderItem']['sku_id'] = $key;
$inventoryData = $this->Inventory->find("first",array("conditions"=>array("Inventory.smb_item_code"=>$item['Product']['smb_code'])));
$quantityNewCheckOut = (int)$inventoryData["Inventory"]["quantity"];
if($quantityNewCheckOut<=0){
$orderItemData["OrderItem"]["mod_name"] = "back";
}else if($quantityNewCheckOut>0){
$orderItemData["OrderItem"]["mod_name"] = "available";
}
$orderItemData['OrderItem']['name'] = $item['Product']['smb_code'];
$orderItemData['OrderItem']['quantity'] = $item['quantity'];
$orderItemData['OrderItem']['price'] = $item['price'];
if(!empty($item['discount_amount'])){
$orderItemData['OrderItem']['discount_amount'] = $item['discount_amount'];
}
$orderItemData['OrderItem']['subtotal'] = $item['subtotal'];
$orderItemData['OrderItem']['status'] = "open";
$this->OrderItem->save($orderItemData);
$this->OrderItem->create();
unset($orderItemData);
}
$orderDetails = $this->OrderItem->find("all",array("conditions"=>array("OrderItem.order_id"=>$id),'recursive'=>2));
$content = "<b>Order Date:</b>".date("d F Y H:i:s")."<br/>";
$content .= "<b>Temporary Order Number:</b>"."TO".'-'.$id."<br/><br/><br/>";
$content .= "<table border='1'><thead><th>Company Name</th><th>Brand</th><th>SMB Item Code</th><th>Mfg Code</th><th>Product Description</th><th>UOM</th><th>MRP</th><th>Unit Price</th><th>Qty</th><th>Subtotal</th><th>Tax%</th><th>Total</th></thead><tbody>";
foreach($orderDetails as $orderItem){
$content .= "<tr>";
$content .= "<td>".$orderItem["Sku"]["Company"]["name"]."</td>";
$content .= "<td>".$orderItem["Sku"]["Brand"]["name"]."</td>";
$content .= "<td>".$orderItem["Sku"]["smb_code"]."</td>";
$content .= "<td>".$orderItem["Sku"]["title"]."</td>";
$content .= "<td>".$orderItem["Sku"]["description"]."</td>";
$content .= "<td>".$orderItem["Sku"]["uom"]."</td>";
$content .= "<td>"."Rs.".$orderItem["Sku"]["mrp"]."</td>";
$content .= "<td>"."Rs.".$orderItem["OrderItem"]["price"]."</td>";
$content .= "<td>".$orderItem["OrderItem"]["quantity"]."</td>";
$content .= "<td>"."Rs.".sprintf('%01.2f', $orderItem["OrderItem"]["price"] * $orderItem["OrderItem"]["quantity"])."</td>";
$content .= "<td>".$orderItem["Sku"]["Product"]["tax"]."</td>";
$content .= "<td>"."Rs.".$orderItem["OrderItem"]["subtotal"]."</td>";
$content.= "</tr>";
}
if($data['shipping_method']=="Express Delivery"){
$totalAmount = ($orderItem["Order"]["total"])+400;
$content .= "<tr><td style='text-align:right' colspan='10'>Delivery Charges:</td><td style='text-align:right'colspan='2'>"."Rs. 400</td></tr>";
$content .= "<tr><td style='text-align:right' colspan='10'>Grand Total:</td><td style='text-align:right'colspan='2'>"."Rs. ".$totalAmount."</td></tr>";
}else{
$content .= "<tr><td style='text-align:right' colspan='10'>Grand Total:</td><td style='text-align:right'colspan='2'>"."Rs. ".$orderItem["Order"]["total"]."</td></tr>";
}
$content .= "</tbody></table>";
$emailTemplate = $this->EmailTemplate->find('first',array('conditions'=>array('EmailTemplate.id'=>'23')));
$emailContent = $emailTemplate['EmailTemplate']['html_content'];
$subject = $emailTemplate['EmailTemplate']['subject']."TO".'-'.$id;
$template_info = str_replace(array('$temp_order','$virtual_id','$content','$salesrep'),array("TO".'-'.$id,$virtual_account,$content,$sales_rep_email),$emailContent);
$email = new CakeEmail();
$email->template('email_template');
$email->emailFormat('both');
$email->viewVars(array('emailContent' => $template_info));
$to = array($user_email);//$to = '';
$from = '';
$email->to($to);
$email->bcc("");
$email->cc($sales_rep_email);
$email->from(array($from=>''));
$email->subject($subject);
$email->smtpOptions = array(
'port'=>'25',
'timeout'=>'30',
'host' => '',
'username'=>'',
'password'=>'',
);
//Set delivery method
$email->delivery = 'smtp';
//pry($email);
$email->send();
$this->redirect(array('action'=>"successOrderRTGS"));
}else{
$paymentMethod = $data['payment_method'];
$shippingMethod = $data['shipping_method'];
if(!empty($paymentMethod)){
$this->Session->write("User.payment_method",$paymentMethod);
$this->Session->write("User.shipping_method",$shippingMethod);
}
$orderData['Order']['total'] = $shop['Order']['total'];
if($data['shipping_method']=="Express Delivery"){
$orderData['Order']['delivery_charges'] = 400;
$amount =($shop['Order']['total'])+400;
}else{
$amount =($shop['Order']['total']);
}
$this->pay_page( array ( 'key' => '', 'txnid' => uniqid( 'test' ), 'amount' => $amount,
'firstname' => $userInfo['User']['name'], 'email' => $userInfo['User']['primary_contact_email'], 'phone' => $userInfo['User']['primary_contact_number'],
'productinfo' => 'Product Info', 'surl' => 'payment_success', 'furl' => 'payment_failure'), '' );
}
}
答案 0 :(得分:2)
尽管没有在数据库中保存Order
数据,但您的代码发送电子邮件的原因是因为您没有检查save()
是否成功。由于验证错误,它可能会失败,但您的代码不会对此做任何事情。
处理此问题的正确方法是将save()
方法包含在if
条件中:
if ($this->Order->save($orderData)) {
//send email
//redirect
} else {
Debugger::log($this->Order->validationErrors);
$this->Flash->set('Order could not be saved');
}
您还应该重构您的操作以减小其大小。使用MVC框架的主要目的是通过分离业务逻辑,控制逻辑和视图来使生活更轻松。如果您发现自己在控制器中编写HTML,那么您可能做错了什么。
有趣的阅读:Fat models, skinny controllers and the MVC design pattern