示例#1
0
 public function actionSaleMobile()
 {
     $this->checkLogin();
     $user_id = Yii::app()->request->cookies['user_id']->value;
     if (!empty($_POST)) {
         $barcode = Util::input($_POST['barcode']);
         $info = Product::getInfoByBarcode($barcode);
         $user = User::model()->findByPk((int) $user_id);
         $saleTemp = new SaleTemp();
         $saleTemp->barcode = $barcode;
         $saleTemp->serial = Util::input($_POST['serial']);
         $saleTemp->qty = 1;
         $saleTemp->qty_per_pack = $info['qty_per_pack'];
         $saleTemp->price = $info['price'];
         $saleTemp->user_id = $user_id;
         $saleTemp->branch_id = $user->branch_id;
         $saleTemp->pk_temp = rand(1000, 10000);
         $saleTemp->created_at = new CDbExpression('NOW()');
         $saleTemp->old_price = $info['old_price'];
         $saleTemp->sale_type = 'mobile';
         $saleTemp->name = $info['name'];
         if ($saleTemp->save()) {
             $this->redirect(array('SaleMobile'));
         }
     }
     $saleTemps = SaleTemp::model()->findAll(array('condition' => 'user_id = :user_id AND sale_type = :sale_type', 'params' => array('user_id' => $user_id, 'sale_type' => 'mobile'), 'order' => 'created_at DESC'));
     $this->renderPartial('//Basic/SaleMobile', array('saleTemps' => $saleTemps, 'sum' => 0));
 }
示例#2
0
 public function actionSale()
 {
     if (!empty($_POST)) {
         $product_name;
         $qty_sub_stock;
         $old_price;
         // find product
         $product = Product::model()->findByAttributes(array('product_code' => Util::input($_POST['product_code'])));
         // not found find from barcode_price
         if (empty($product)) {
             $barcodePrice = BarcodePrice::model()->findByAttributes(array('barcode' => Util::input($_POST['product_code'])));
             if (!empty($barcodePrice)) {
                 $product = $barcodePrice->getProduct();
                 $product_name = $product->product_name . " ( {$barcodePrice->name} )";
                 $qty_sub_stock = $barcodePrice->qty_sub_stock;
                 $old_price = $barcodePrice->price_before;
             }
         } else {
             $product_name = $product->product_name;
             $qty_sub_stock = $product->product_total_per_pack;
         }
         // found product
         if (!empty($product)) {
             // find price
             $price = $product->product_price;
             $old_price = $product->product_price_buy;
             // find default price and send price
             $sale_condition = Util::input($_POST['sale_condition']);
             if ($sale_condition == 'one') {
                 // ขายปลีก
                 $price = $product->product_price;
             } else {
                 // ขายส่ง
                 $price = $product->product_price_send;
             }
             // find by barcode price
             $barcodePrice = BarcodePrice::model()->findByAttributes(array('barcode' => Util::input($_POST['product_code'])));
             if (!empty($barcodePrice)) {
                 $price = $barcodePrice->price;
                 $old_price = $barcodePrice->price_before;
                 $product_name = $product->product_name . " ( {$barcodePrice->name} )";
                 $qty_sub_stock = $barcodePrice->qty_sub_stock;
             }
             // find between price
             $productPrice = ProductPrice::model()->find(array('condition' => '
         product_barcode = :product_barcode
         AND 
         (qty <= :qty AND :qty <= qty_end)
       ', 'params' => array('qty' => (int) Util::input($_POST['qty']), 'product_barcode' => Util::input($_POST['product_code']))));
             if (!empty($productPrice)) {
                 if ($sale_condition == 'one') {
                     $price = $productPrice->price;
                 } else {
                     $price = $productPrice->price_send;
                 }
             }
             // find qty_sub_stock
             $qty_sub_stock = $product->product_total_per_pack;
             if (!empty($barcodePrice)) {
                 $qty_sub_stock = $barcodePrice->qty_sub_stock;
             }
             // default qty_sub_stock
             if ($qty_sub_stock == 0) {
                 $qty_sub_stock = 1;
             }
             // check condition
             $configSoftware = ConfigSoftware::model()->find();
             $is_sale = false;
             if ($configSoftware->sale_out_of_stock == 'yes') {
                 $is_sale = true;
             } else {
                 if ($product->product_quantity > 0) {
                     $is_sale = true;
                 }
             }
             if ($is_sale) {
                 // save to temp
                 $saleTemp = new SaleTemp();
                 $saleTemp->barcode = Util::input($_POST['product_code']);
                 $saleTemp->name = $product_name;
                 $saleTemp->serial = Util::input($_POST['serial']);
                 $saleTemp->price = str_replace(',', '', $price);
                 $saleTemp->qty = str_replace(',', '', $_POST['qty']);
                 $saleTemp->qty_per_pack = $qty_sub_stock;
                 $saleTemp->user_id = Yii::app()->request->cookies['user_id']->value;
                 $saleTemp->branch_id = (int) Util::input($_POST['branch_id']);
                 $saleTemp->pk_temp = rand(1, 99999);
                 $saleTemp->created_at = new CDbExpression('NOW()');
                 $saleTemp->old_price = $old_price;
                 // save and update stock
                 if ($saleTemp->save()) {
                     Yii::app()->session['branch_id'] = (int) Util::input($_POST['branch_id']);
                     echo 'success';
                 }
             } else {
                 echo Yii::t("lang", "product_out_of_stock");
             }
         }
     }
 }