提交 af5dfd18 编写于 作者: R root

生成订单的同时扣除产品库存

上级 64c3635b
......@@ -37,27 +37,29 @@ class Placeorder{
if($this->checkOrderInfoAndInit($post)){
# 如果游客用户勾选了注册账号,则注册,登录,并把地址写入到用户的address中
$gus_status = $this->guestCreateAndLoginAccount($post);
$save_address_status = $this->saveNewAddress($post);
$save_address_status = $this->updateAddress($post);
if($gus_status && $save_address_status){
# 更新Cart信息
$this->updateCart();
# 更新Cart信息
//$this->updateCart();
# 设置checkout type
$serviceOrder = Yii::$service->order;
$checkout_type = $serviceOrder::CHECKOUT_TYPE_STANDARD;
$serviceOrder->setCheckoutType($checkout_type);
# 将购物车数据,生成订单。
Yii::$service->order->generateOrderByCart($this->_billing,$this->_shipping_method,$this->_payment_method);
$startUrl = Yii::$service->payment->getStandardStartUrl();
Yii::$service->url->redirect($startUrl);
exit;
//return true;
$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing,$this->_shipping_method,$this->_payment_method);
if($genarateStatus){
$startUrl = Yii::$service->payment->getStandardStartUrl();
Yii::$service->url->redirect($startUrl);
exit;
//return true;
}
}
}else{
}
}
//echo 333;exit;
Yii::$service->page->message->addByHelperErrors();
return false;
}
/**
......@@ -109,12 +111,12 @@ class Placeorder{
* 登录用户,保存货运地址到customer address ,然后把生成的
* address_id 写入到cart中。
* shipping method写入到cart中
* payment method 写入到cart中
* payment method 写入到cart中 updateCart
*/
public function saveNewAddress($post){
public function updateAddress($post){
if(!Yii::$app->user->isGuest){
$billing = $post['billing'];
$address_id = $post['address_id'];
$address_id = $post['address_id'];
if(!$address_id){
$identity = Yii::$app->user->identity;
$customer_id = $identity['id'];
......@@ -141,8 +143,11 @@ class Placeorder{
return false;
}
//echo "$address_id,$this->_shipping_method,$this->_payment_method";
return Yii::$service->cart->updateLoginCart($address_id,$this->_shipping_method,$this->_payment_method);
}
return Yii::$service->cart->updateLoginCart($this->_address_id,$this->_shipping_method,$this->_payment_method);
}else{
return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
}
return true;
}
......@@ -150,6 +155,7 @@ class Placeorder{
/**
* 如果是游客,那么保存货运地址到购物车表。
*/
/*
public function updateCart(){
if(Yii::$app->user->isGuest){
return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
......@@ -157,6 +163,8 @@ class Placeorder{
return Yii::$service->cart->updateLoginCart($this->_address_id,$this->_shipping_method,$this->_payment_method);
}
}
*/
/**
* @property $post | Array
* @return boolean
......
......@@ -18,47 +18,49 @@ use yii\base\InvalidValueException;
class Start {
public function getLastData(){
$loaderImg = Yii::$service->image->getImgUrl('images/loader.gif','appfront');
$paypalLogoImg = Yii::$service->image->getImgUrl('custom/logo.png','appfront');
$increment_id = Yii::$service->order->getSessionIncrementId();
$order = Yii::$service->order->getInfoByIncrementId($increment_id);
//var_dump($order);
//exit;
Yii::$service->payment->setPaymentMethod($order['payment_method']);
$success_redirect_url = Yii::$service->payment->getStandardSuccessRedirectUrl();
$cancel_url = Yii::$service->payment->getStandardCancelUrl();
$ipn_url = Yii::$service->payment->getStandardIpnUrl();
$store_name = Yii::$service->store->currentStore;
$payment_action = "sale";
$cmd = '_cart';
$upload = 1;
$tax = 0.00;
$tax_cart = 0.00;
$payment_url = Yii::$service->payment->getStandardPaymentUrl();
$account = Yii::$service->payment->getStandardAccount();
return [
'loader_img' => $loaderImg,
'paypal_logo_img' => $paypalLogoImg,
'order' => $order,
'success_redirect_url' => $success_redirect_url,
'cancel_url' => $cancel_url,
'ipn_url' => $ipn_url,
'store_name' => $store_name,
'payment_action' => $payment_action,
'cmd' => $cmd,
'upload' => $upload,
'tax' => $tax,
'tax_cart' => $tax_cart,
'payment_url' => $payment_url,
'account' => $account,
'product_items_and_shipping' => $this->getProductItemsAndShipping($order),
'address_html' => $this->getAddressHtml($order),
];
if($increment_id){
$order = Yii::$service->order->getInfoByIncrementId($increment_id);
if($order){
//var_dump($order);exit;
Yii::$service->payment->setPaymentMethod($order['payment_method']);
$success_redirect_url = Yii::$service->payment->getStandardSuccessRedirectUrl();
$cancel_url = Yii::$service->payment->getStandardCancelUrl();
$ipn_url = Yii::$service->payment->getStandardIpnUrl();
$store_name = Yii::$service->store->currentStore;
$payment_action = "sale";
$cmd = '_cart';
$upload = 1;
$tax = 0.00;
$tax_cart = 0.00;
$payment_url = Yii::$service->payment->getStandardPaymentUrl();
$account = Yii::$service->payment->getStandardAccount();
return [
'loader_img' => $loaderImg,
'paypal_logo_img' => $paypalLogoImg,
'order' => $order,
'success_redirect_url' => $success_redirect_url,
'cancel_url' => $cancel_url,
'ipn_url' => $ipn_url,
'store_name' => $store_name,
'payment_action' => $payment_action,
'cmd' => $cmd,
'upload' => $upload,
'tax' => $tax,
'tax_cart' => $tax_cart,
'payment_url' => $payment_url,
'account' => $account,
'product_items_and_shipping' => $this->getProductItemsAndShipping($order),
'address_html' => $this->getAddressHtml($order),
];
}
}
}
public function getAddressHtml($order){
......
......@@ -23,7 +23,11 @@ class StandardController extends AppfrontController
public function actionStart(){
Yii::$service->page->theme->layoutFile = 'blank.php';
$data = $this->getBlock()->getLastData();
return $this->render($this->action->id,$data);
if(is_array($data) && !empty($data)){
return $this->render($this->action->id,$data);
}else{
Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
}
......
<div class="main container one-column">
<div class="col-main">
<?= Yii::$service->page->widget->render('flashmessage'); ?>
<form action="<?= Yii::$service->url->getUrl('checkout/onepage'); ?>" method="post" id="onestepcheckout-form">
<?= \fec\helpers\CRequest::getCsrfInputHtml(); ?>
<fieldset style="margin: 0;" class="group-select">
......
......@@ -91,6 +91,10 @@ return [
'info' => [
'class' => 'fecshop\services\product\Info',
],
'stock' => [
'class' => 'fecshop\services\product\Stock',
],
/* #暂时没用
......
......@@ -269,6 +269,12 @@ class Order extends Service
$state = $address['state'];
//echo "$shipping_method,$country,$state";exit;
$cartInfo = Yii::$service->cart->getCartInfo($shipping_method,$country,$state);
# 检查产品是否有库存,如果没有库存则返回false
$deductStatus = Yii::$service->product->stock->checkItemsStock($cartInfo['products']);
if(!$deductStatus){
return false;
}
$myOrder = new MyOrder;
$myOrder['order_status'] = $this->payment_status_pending;
$myOrder['store'] = $cartInfo['store'];
......@@ -324,19 +330,20 @@ class Order extends Service
$orderModel->save();
Yii::$service->order->item->saveOrderItems($cartInfo['products'],$order_id,$cartInfo['store']);
$this->setSessionIncrementId($increment_id);
# 扣除库存。(订单生成后,库存产品库存。)
# (备注)需要另起一个脚本,用来处理半个小时后,还没有支付的订单,将订单取消,然后将订单里面的产品库存返还。
# 如果是无限库存(没有库存就去采购的方式),那么不需要跑这个脚本,将库存设置的非常大即可。
Yii::$service->product->stock->deduct($cartInfo['products']);
# 如果是登录用户,那么,在生成订单后,需要清空购物车。
if(!Yii::$app->user->isGuest){
Yii::$service->cart->clearCartProduct();
}
# 扣除库存。
# (备注)需要另起一个脚本,用来处理半个小时后,还没有支付的订单,将订单取消,然后将订单里面的产品库存返还。
# 如果是无限库存(没有库存就去采购的方式),那么不需要跑这个脚本,将库存设置的非常大即可。
return true;
}else{
Yii::$service->helper->errors->add('generate order fail');
return false;
}
return false;
}
/**
* @property $increment_id | String ,order订单号
......
......@@ -140,8 +140,8 @@ class Product extends Service
/**
* 通过sku查询产品
*/
protected function actionGetBySku($sku){
return $this->_product->getBySku($sku);
protected function actionGetBySku($sku,$returnArr=true){
return $this->_product->getBySku($sku,$returnArr);
}
/**
......
......@@ -82,10 +82,12 @@ class Coupon extends Service
if(!$coupon_code){
$coupon_code = $this->_coupon_code;
}
$one = MyCoupon::findOne(['coupon_code' => $coupon_code]);
if($one['coupon_code']){
$this->_coupon_model = $one;
if($coupon_code){
$one = MyCoupon::findOne(['coupon_code' => $coupon_code]);
if($one['coupon_code']){
$this->_coupon_model = $one;
}
}
}
if($this->_coupon_model){
......@@ -276,7 +278,7 @@ class Coupon extends Service
}
}else{
Yii::$service->helper->errors->add("coupon is not exist");
//Yii::$service->helper->errors->add("coupon is not exist");
}
}
......
......@@ -64,14 +64,15 @@ class Message extends Service
*/
protected function actionAddByHelperErrors(){
$errors = Yii::$service->helper->errors->get(true);
var_dump($errors);
if($errors){
if(is_array($errors) && !empty($errors)){
foreach($errors as $error){
if(is_array($error) && !empty($error)){
foreach($error as $er){
Yii::$service->page->message->addError($er);
}
}
//if(is_array($error) && !empty($error)){
// foreach($error as $er){
Yii::$service->page->message->addError($error);
//}
//}
}
}
}
......
......@@ -40,13 +40,17 @@ class ProductMongodb implements ProductInterface
public function getBySku($sku,$returnArr=true){
if($sku){
if($returnArr){
return Product::find()->asArray()
$product = Product::find()->asArray()
->where(['sku' => $sku])
->one()
;
}else{
return Product::findOne(['sku' => $sku]);
$product = Product::findOne(['sku' => $sku]);
}
$primaryKey = $this->getPrimaryKey() ;
if(isset($product[$primaryKey]) && !empty($product[$primaryKey])){
return $product;
}
}
}
......
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services\product;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidValueException;
use fecshop\services\Service;
use fec\helpers\CDate;
use fec\helpers\CUser;
use fecshop\models\mongodb\Product;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Stock extends Service
{
# product model arr
protected $_product_arr;
# product items(譬如购物车信息)
protected $_product_items;
# 是否已经检查产品是否有库存。
protected $_checkItemsStockStatus;
//protected $CheckItemsStock
/**
* @property $items | Array , example:
* [
[
'sku' => 'xxxxx',
'qty' => 2,
'custom_option_sku' => 'cos_1', # 存在该项,则应该到产品的
],
[
'sku' => 'yyyyy',
'qty' => 1,
],
]
* @return boolean
* 扣除产品库存。如果扣除成功,则返回true,如果返回失败,则返回false
*/
protected function actionDeduct($items){
if(!$this->_checkItemsStockStatus){
if(!$this->checkItemsStock($items)){
return false;
}
}
$product_arr = $this->_product_arr;
$product_items = $this->_product_items;
# 开始扣除库存。
if(is_array($product_items) && !empty($product_items)){
foreach($product_items as $k=>$item){
$sku = $item['sku'];
$sale_qty = $item['qty'];
$custom_option_sku = $item['custom_option_sku'];
if($sku && $sale_qty){
$product = $product_arr[$k];
if($product){
if(!$custom_option_sku){
$is_in_stock = $product['is_in_stock'];
$product_qty = $product['qty'];
$product->qty = $product_qty - $sale_qty;
$product->save();
}else{
$custom_option = $product['custom_option'];
if(isset($custom_option[$custom_option_sku]['qty']) && !empty($custom_option[$custom_option_sku]['qty'])){
$custom_option[$custom_option_sku]['qty'] -= $sale_qty;
$product->custom_option = $custom_option;
$product->save();
}
}
}
}
}
return true;
}
}
/**
* @property $items | Array , example:
* [
[
'sku' => 'xxxxx',
'qty' => 2,
'custom_option_sku' => 'cos_1', # 存在该项,则应该到产品的
],
[
'sku' => 'yyyyy',
'qty' => 1,
],
]
* @return boolean
* 检查产品的库存是否允许扣除。如果扣除成功,则返回true,如果返回失败,则返回false
*/
protected function actionCheckItemsStock($items){
$product_arr = [];
$i = 0;
$product_items = [];
# 首先检查,库存是否满足,不满足则返回false
if(is_array($items) && !empty($items)){
foreach($items as $item){
//var_dump($item);
$sku = $item['sku'];
$sale_qty = $item['qty'];
$custom_option_sku = $item['custom_option_sku'];
if($sku && $sale_qty){
$product = Yii::$service->product->getBySku($sku,false);
if($product){
$is_in_stock = $product['is_in_stock'];
$product_qty = $product['qty'];
if($this->productIsInStock($product,$sale_qty,$custom_option_sku)){
$product_items[$i] = $item;
$product_arr[$i] = $product;
$i++;
}else{
Yii::$service->helper->errors->add('product sku('.$sku.') is stock out');
//echo 3;
return false;
}
}else{
Yii::$service->helper->errors->add('product sku('.$sku.') is not exist');
//echo 4;
return false;
}
}else{
Yii::$service->helper->errors->add('cart sku qty is empty');
//echo 5;
return false;
}
}
}
$this->_checkItemsStockStatus = true;
$this->_product_arr = $product_arr;
$this->_product_items = $product_items;
return true;
}
/**
* @property $product_items | Array
* @return boolean
* 返还产品库存。
*/
protected function actionReturn($product_items){
# 开始扣除库存。
if(is_array($product_items) && !empty($product_items)){
foreach($product_items as $k=>$item){
$sku = $item['sku'];
$sale_qty = $item['qty'];
$custom_option_sku = $item['custom_option_sku'];
if($sku && $sale_qty){
$product = Yii::$service->product->getBySku($sku);
if($product){
if(!$custom_option_sku){
$is_in_stock = $product['is_in_stock'];
$product_qty = $product['qty'];
$product->qty = $product_qty + $sale_qty;
$product->save();
}else{
$custom_option = $product['custom_option'];
if(isset($custom_option[$custom_option_sku]['qty']) && !empty($custom_option[$custom_option_sku]['qty'])){
$custom_option[$custom_option_sku]['qty'] += $sale_qty;
$product->custom_option = $custom_option;
$product->save();
}
}
}
}
}
}
return true;
}
/**
* @property $product | Object, Product Model
* @property sale_qty | Int 需要出售的个数
* @property custom_option_sku | String 产品custom option sku
* @return boolean
* 返还产品库存。
*/
protected function actionProductIsInStock($product,$sale_qty,$custom_option_sku){
$is_in_stock = $product['is_in_stock'];
$product_qty = $product['qty'];
if($is_in_stock == 1){
if($custom_option_sku){
$custom_option = $product['custom_option'];
if(isset($custom_option[$custom_option_sku]['qty']) && !empty($custom_option[$custom_option_sku]['qty'])){
$custom_option_qty = $custom_option[$custom_option_sku]['qty'];
if(($custom_option_qty > 0) && ($custom_option_qty > $sale_qty) ){
return true;
}
}
}else if(($product_qty > 0) && ($product_qty > $sale_qty) ){
return true;
}
}
return false;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册