| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446 |
- <?php
- namespace bizHd\purchase\classes;
- use biz\ghs\classes\GhsClass;
- use biz\product\classes\XjClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use biz\stock\classes\StockClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\admin\classes\AdminClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\CostChangeClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\OnStockRecordClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\cg\classes\CgTreeClass;
- use bizHd\cg\classes\CgXjClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\stat\classes\StatIncomeClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\IntraCityExpress;
- use common\components\mapUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use bizHd\base\classes\BaseClass;
- use common\components\lakala\Lakala;
- use Yii;
- use yii\helpers\ArrayHelper;
- class PurchaseClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizHd\purchase\models\Purchase';
- //订单状态 1待付款,待确认,2待配送,3配送中,4已完成,5取消,失败
- const STATUS_UN_PAY = 1;
- const STATUS_UN_SEND = 2;
- const STATUS_SENDING = 3;
- const STATUS_COMPLETE = 4;
- const STATUS_CANCEL = 5;
- //未知
- const DEBT_UNKNOWN = 0;
- //欠款
- const DEBT_YES = 1;
- //没有欠款
- const DEBT_NO = 2;
- //配送,这里的数值是错的,请参考dict.php的数值
- const GET_TYPE_SEND = 1;
- //自取,这里的数值是错的,请参考dict.php的数值
- const GET_TYPE_SELF_GET = 2;
- const REFUND_NO = 1;
- const REFUND_YES = 2;
- //获取打包费
- public static function getKmPackCost($mainId)
- {
- $cost = dict::getDict('kmPackCost');
- if (getenv('YII_ENV') == 'production') {
- //云漫梦金鹏 徐记花卉 康多鲜 旭海 打包费0元
- if (in_array($mainId, [10728, 20528, 10866, 38657, 41121])) {
- $cost = dict::getDict('kmPackCost2');
- }
- //花瀚鲜花供应链
- if (in_array($mainId, [50, 14499])) {
- $cost = dict::getDict('kmPackCost3');
- }
- } else {
- }
- return $cost;
- }
- //订单发货主方法 ssh 20231119
- public static function orderSend($cg, $params, $allowRepeat)
- {
- $respond = self::orderFh($cg, $params, $allowRepeat);
- $orderId = $cg->saleId ?? 0;
- $orderInfo = OrderClass::getById($orderId, true);
- if (empty($orderInfo)) {
- util::fail('没有找到销售单信息');
- }
- OrderClass::orderFh($orderInfo, $params, $allowRepeat);
- return $respond;
- }
- public static function cancelFh($cg, $params)
- {
- $cg->status = 2;
- $cg->save();
- $orderSn = $cg->orderSn ?? '';
- $itemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $mainId = $cg->mainId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- foreach ($itemList as $key => $val) {
- $productId = $val->productId ?? 0;
- $itemId = $val->itemId ?? 0;
- $num = $val->xhNum ?? 0;
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材呢');
- }
- //减少路上库存
- $onRecord = [];
- $onRecord['sjId'] = $sjId;
- $onRecord['shopId'] = $shopId;
- $onRecord['mainId'] = $mainId;
- $onRecord['orderSn'] = $orderSn;
- $onRecord['productId'] = $productId;
- $onRecord['itemId'] = $itemId;
- $onRecord['relateName'] = $ghsName;
- $onRecord['itemNum'] = $num;
- $oldOnStock = $product->onStock ?? 0;
- $onRecord['oldStock'] = $oldOnStock;
- $newOnStock = bcsub($oldOnStock, $num);
- $onRecord['newStock'] = $newOnStock;
- $onRecord['ptStyle'] = 1;
- OnStockRecordClass::cancelCg($onRecord);
- ProductClass::decreaseOnStockByItemNum($productId, $num);
- }
- }
- }
- //订单发货子方法 ssh 20231119
- public static function orderFh($cg, $params, $allowRepeat = false, $notice = false)
- {
- if ($notice) {
- $noticeText = json_encode(['id' => $cg->id]);
- $noticeKey = "ghsKdNoticeHd";
- Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
- }
- if ($cg->status == 3) {
- if (!$allowRepeat) {
- util::fail('订单已发货');
- } else {
- return true;
- }
- }
- $fhType = $params['fhType'] ?? 0;
- if (!is_numeric($fhType)) {
- util::fail('请选择发货类型');
- }
- if ($fhType == 1) {
- $fhWl = $params['fhWl'] ?? '';
- $fhWlNo = $params['fhWlNo'] ?? '';
- if (empty($fhWl)) {
- util::fail('请填写物流名称');
- }
- if (empty($fhWlNo)) {
- util::fail('请填写物流编号');
- }
- $cg->fhWl = $fhWl;
- $cg->fhWlNo = $fhWlNo;
- }
- $cg->fhType = $fhType;
- $cg->status = 3;
- $cg->fhTime = date("Y-m-d H:i:s");
- $cg->save();
- $orderSn = $cg->orderSn ?? '';
- $itemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $mainId = $cg->mainId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- foreach ($itemList as $key => $val) {
- $productId = $val->productId ?? 0;
- $itemId = $val->itemId ?? 0;
- $num = $val->xhNum ?? 0;
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材呢');
- }
- //增加路上库存
- $onRecord = [];
- $onRecord['sjId'] = $sjId;
- $onRecord['shopId'] = $shopId;
- $onRecord['mainId'] = $mainId;
- $onRecord['orderSn'] = $orderSn;
- $onRecord['productId'] = $productId;
- $onRecord['itemId'] = $itemId;
- $onRecord['relateName'] = $ghsName;
- $onRecord['itemNum'] = $num;
- $oldOnStock = $product->onStock ?? 0;
- $onRecord['oldStock'] = $oldOnStock;
- $newOnStock = bcadd($oldOnStock, $num);
- $onRecord['newStock'] = $newOnStock;
- $onRecord['ptStyle'] = 1;
- OnStockRecordClass::addPurchaseOrderRecord($onRecord);
- ProductClass::addOnStockByItemNum($productId, $num);
- }
- }
- return $cg;
- }
- //确认收货 ssh 20231119
- public static function confirmTake($cg)
- {
- //订单如果没有发货,则先发货
- if ($cg->status == self::STATUS_UN_SEND) {
- $cg = self::orderSend($cg, [], true);
- }
- // 构建“冲销”函数的参数 -- 变动前的花材存库数据
- $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
- $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
- $oldStocks = [];
- foreach ($productIds as $productId) {
- $product = ProductClass::getById($productId, true);
- $oldStocks[$productId] = $product;
- }
- self::takeToPutIn($cg);
- $shop = ShopClass::getLockById($cg->shopId);
- if (empty($shop)) {
- util::fail('没有找到门店14');
- }
- $adminId = $shop->adminId;
- $mainId = $shop->mainId;
- PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $mainId, '系统');
- }
- //确认收货并入库 ssh 20231119
- public static function takeToPutIn($cg)
- {
- if ($cg->status != 3) {
- util::fail('订单不是待收货状态哦,编号:cg' . $cg->id);
- }
- $saleId = $cg->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->status != 3) {
- util::fail('订单不是待收货状态,订单状态:' . $order->status . ',订单ID:' . $saleId);
- }
- $order->status = 4;
- $order->save();
- //加库存 lqh 2021-05-10
- $orderSn = $cg->orderSn;
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $mainId = $cg->mainId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- foreach ($purchaseItem as $v) {
- //小单位库存不考虑入库 ssh 202310
- $itemNum = $v['itemNum'];
- if ($itemNum > 0) {
- $productId = $v['productId'];
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $itemNum);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $cg->sjId;
- $recordData['shopId'] = $cg->shopId;
- $recordData['mainId'] = $cg->mainId;
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $itemNum;
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = $cg->ghsName ?? '';
- StockRecordClass::addPurchaseOrderRecord($recordData);
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材哈');
- }
- //减少路上库存
- $onRecord = [];
- $onRecord['sjId'] = $sjId;
- $onRecord['shopId'] = $shopId;
- $onRecord['mainId'] = $mainId;
- $onRecord['orderSn'] = $orderSn;
- $onRecord['productId'] = $productId;
- $onRecord['itemId'] = $v['itemId'] ?? 0;
- $onRecord['relateName'] = $ghsName;
- $onRecord['itemNum'] = $itemNum;
- $oldOnStock = $product->onStock ?? 0;
- $onRecord['oldStock'] = $oldOnStock;
- $onRecord['newStock'] = bcsub($oldOnStock, $itemNum);
- $onRecord['ptStyle'] = 1;
- OnStockRecordClass::addInStockOrderRecord($onRecord);
- ProductClass::decreaseOnStockByItemNum($productId, $itemNum);
- $unitPrice = $v['xhUnitPrice'] ?? 0;
- $product->cost = $unitPrice;
- $product->save();
- $ptItemId = $product->itemId ?? 0;
- $mainId = $product->mainId ?? 0;
- $staffId = $cg->shopAdminId ?? 0;
- $staffName = $cg->shopAdminName ?? '';
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => 0,
- 'mainId' => $mainId,
- 'itemId' => $productId,
- 'ptItemId' => $ptItemId,
- 'cost' => $unitPrice,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'name' => $product->name ?? '',
- 'cover' => $product->cover ?? '',
- 'targetId' => $cg->id ?? 0,
- 'event' => '采购',
- ];
- CostChangeClass::addData($changeData);
- }
- }
- $cg->status = 4;
- $cg->stockChange = 1;
- $cg->save();
- }
- // 库存回滚为入库前的状态
- public static function rollbackProductStock($oldStocks, $shop, $adminId, $mainId, $staffName)
- {
- $itemInfo = [];
- foreach ($oldStocks as $productId => $product) {
- $ratio = $product->ratio;
- $formNum = ProductClass::formatStock($product->stock, $ratio); //花材的库存转换
- $itemInfo[] = [
- 'productId' => $productId,
- 'itemNum' => $product->stock, // 回滚为入库前的库存
- 'itemId' => $product->itemId,
- 'bigNum' => $formNum['bigNum'],
- 'smallNum' => $formNum['smallNum'],
- 'price' => $product->price,
- ];
- }
- $data = [
- 'sjId' => $shop->sjId,
- 'shopId' => $shop->id,
- 'adminId' => $adminId,
- 'mainId' => $mainId,
- 'staffName' => $staffName,
- 'itemInfo' => $itemInfo,
- 'pdType' => 'rollbackStock',
- 'remark' => '',
- ];
- \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
- }
- // 指定花材新增库存
- public static function increaseProductStock($itemDatas, $shop, $adminId, $mainId, $staffName)
- {
- $itemInfo = [];
- foreach ($itemDatas as $productId => $arr) {
- $addNum = $arr['addNum']; //新增的数值
- $product = $arr['product'];
- $ratio = $product->ratio;
- $newStock = bcadd($product->stock, $addNum, 2);
- $formNum = ProductClass::formatStock($newStock, $ratio); //花材的库存转换
- $itemInfo[] = [
- 'productId' => $productId,
- 'itemNum' => $newStock, // 新库存数
- 'itemId' => $product->itemId,
- 'bigNum' => $formNum['bigNum'],
- 'smallNum' => $formNum['smallNum'],
- 'price' => $product->price,
- ];
- }
- $data = [
- 'sjId' => $shop->sjId,
- 'shopId' => $shop->id,
- 'adminId' => $adminId,
- 'mainId' => $mainId,
- 'staffName' => $staffName,
- 'itemInfo' => $itemInfo,
- 'pdType' => 'rollbackStock',
- 'remark' => '',
- ];
- \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
- }
- public static function notSameCity($shop, $ghsShop)
- {
- return true;
- $shopLat = $shop->lat ?? '';
- $shopLong = $shop->long ?? '';
- if (empty($shopLat) || empty($shopLong)) {
- util::fail('请完善门店地址');
- }
- $ghsShopLat = $ghsShop->lat ?? '';
- $ghsShopLong = $ghsShop->long ?? '';
- $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
- $formatDistance = ceil($distance / 1000);
- if ($formatDistance > 150) {
- return false;
- }
- return true;
- }
- //计算距离和运费【另外还有个地方要同步修改,关键词calc_freight_distance】 ssh 20221003
- public static function getDistanceFee($shop, $ghsShop, $weight, $custom)
- {
- $ability = ShopClass::hasIntraCity($ghsShop);
- $openIntraCity = $ability['openIntraCity'] ?? 0;
- if ($openIntraCity == 0) {
- //没有开启同城配送能力,不计算运费和距离
- return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
- }
- $shopLat = $shop->lat ?? '';
- $shopLong = $shop->long ?? '';
- if (empty($shopLat) || empty($shopLong)) {
- util::fail('请完善你的收花地址');
- }
- $address = $shop->address ?? '';
- if (empty($address)) {
- util::fail('请完善收花地址');
- }
- if ($weight <= 0) {
- util::fail('花材没有重量');
- }
- $wxStoreId = $ability['wxStoreId'] ?? '';
- $packageNum = 1;
- $price = 300;
- $cargo = [
- 'cargo_name' => '鲜花花材', // 商品名称 -- $order 中没有,要在 goodsInfo 中取
- 'cargo_weight' => intval($weight * 1000), // 单位:克 -- 把千克转换为克
- 'cargo_price' => intval($price * 100), // 单位:分 -- 把元转换为分
- 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- 'cargo_num' => $packageNum
- ];
- $customName = $custom->name ?? '';
- if (empty($customName)) {
- util::fail('客户名称是空的');
- }
- $customMobile = $custom->mobile ?? '';
- if (empty($customMobile)) {
- util::fail('客户手机缺失');
- }
- //根据环境变量判断是否使用沙盒环境
- // $useSandBox = getenv('YII_ENV') == 'production' ? 0 : 1;
- $orderData = [
- 'wx_store_id' => $wxStoreId,
- 'user_name' => $customName,
- 'user_phone' => $customMobile,
- 'user_lng' => $shopLong,
- 'user_lat' => $shopLat,
- 'user_address' => $address,
- 'cargo' => $cargo,
- // 'use_sandbox' => $useSandBox,//正式环境不能传这个参数,会报错
- ];
- //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
- $result = IntraCityExpress::preAddOrder($orderData);
- if ($result['errcode'] !== 0) {
- $errMsg = $result['errmsg'] ?? '';
- noticeUtil::push('运费查询失败原因:' . $errMsg . '参数:' . $result['errcode'], '15280215347');
- util::fail('运费查询失败,编号1160');
- }
- $distance = $result['distance'] ?? 0;
- $showDistance = sprintf("%.1f", ($distance / 1000));
- $fee = $result['est_fee'] ?? 0;
- $totalFee = sprintf("%.1f", ($fee / 100));
- //noticeUtil::push("批发订单,获取运费:{$totalFee} 重量:{$weight} 距离:{$showDistance} 客户名:{$customMobile} 手机号 {$customMobile} 经纬 {$shopLong} {$shopLat} 金额:{$price} 数量:{$packageNum}", '15280215347');
- return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
- }
- //到货之后 ssh 20220321
- public static function arrival($cg, $data)
- {
- if ($cg->stockChange == 1) {
- util::fail('您已操作过了');
- }
- $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
- $packCost = isset($data['packCost']) && is_numeric($data['packCost']) ? $data['packCost'] : 0;
- $cg->sendCost = $sendCost;
- $cg->packCost = $packCost;
- $cg->save();
- $sjId = $data['sjId'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- //product [{"productId":"3674","num":0,"price":1}]
- $product = $data['product'] ?? '';
- if (empty($product)) {
- util::fail('请填写成本价和到货数量');
- }
- $ghsName = $cg->ghsName ?? '';
- $orderSn = $cg->orderSn ?? '';
- $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($list)) {
- util::fail('没有找到花材哦');
- }
- $weight = 0;
- $itemPrice = 0;
- foreach ($list as $key => $val) {
- $productId = $val->productId ?? 0;
- if (isset($product[$productId]) == false) {
- util::fail('没有找到对应采购花材');
- }
- $num = $product[$productId]['num'] ?? 0;
- $price = $product[$productId]['price'] ?? 0;
- if ($price <= 0) {
- util::fail('花材金额需要大于0');
- }
- $totalPrice = bcmul($num, $price, 2);
- $itemPrice = bcadd($itemPrice, $totalPrice, 2);
- $val->price = $price;
- $val->itemNum = $num;
- $val->preTotalPrice = $totalPrice;
- $val->totalPrice = $totalPrice;
- $val->xhNum = $num;
- $val->xhUnitPrice = $price;
- $val->xhPrice = $totalPrice;
- $val->save();
- $unitWeight = $val->unitWeight ?? 0;
- $currentWeight = bcmul($num, $unitWeight, 2);
- $weight = bcadd($weight, $currentWeight, 2);
- $unitType = $val->xhUnitType ?? 0;
- $bigNum = 0;
- $smallNum = 0;
- if ($unitType == dict::getDict('unitType', 'big')) {
- $bigNum = $num;
- } else {
- $smallNum = $num;
- }
- //增加库存
- $option = ['relateName' => $ghsName];
- StockClass::purchaseItem($orderSn, $sjId, $shopId, $productId, $bigNum, $smallNum, $option);
- }
- $cg->itemPrice = $itemPrice;
- $sendCost = $cg->sendCost ?? 0;
- $currentPrice = bcadd($itemPrice, $sendCost, 2);
- $packCost = $cg->packCost ?? 0;
- $currentPrice = bcadd($currentPrice, $packCost, 2);
- $kiloFee = $data['kiloFee'] ?? 0;
- $miniKilo = $data['miniKilo'] ?? 0;
- $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
- $cg->prePrice = $currentPrice;
- $cg->orderPrice = $currentPrice;
- $cg->actPrice = $currentPrice;
- $cg->realPrice = $currentPrice;
- $cg->stockChange = 1;
- $cg->save();
- $mainId = $cg->mainId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $cgId = $cg->id ?? 0;
- $saleRefundId = $data['saleRefundId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到零售店的门店');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息12');
- }
- $main->cgUnSend -= 1;
- $main->cgFinish += 1;
- $main->save();
- $cgRefund = null;
- $bookPrice = $cg->bookPrice ?? 0;
- if ($bookPrice == $currentPrice) {
- return ['refund' => $cgRefund];
- } elseif ($bookPrice > $currentPrice) {
- //退款
- $refundPrice = bcsub($bookPrice, $currentPrice, 2);
- $book = 1;
- $refundOrderSn = orderSn::getCgRefundSn();
- $refundType = CgRefundClass::REFUND_TYPE_MONEY;
- $refundData = [
- 'book' => $book,
- 'mainId' => $mainId,
- 'orderSn' => $refundOrderSn,
- 'cgId' => $cgId,
- 'status' => CgRefundClass::STATUS_COMPLETE,
- 'shopAdminId' => 0,
- 'shopAdminName' => '',
- 'refundType' => $refundType,
- 'refundPrice' => $refundPrice,
- 'saleRefundId' => $saleRefundId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- ];
- $cgRefund = CgRefundClass::add($refundData, true);
- //门店收入增加
- $currentTotalIncome = bcadd($main->totalIncome, $refundPrice, 2);
- $main->totalIncome = $currentTotalIncome;
- $main->save();
- //供货商退款,向供货商采购总金额减少
- StatCgClass::replace($main, $shop, $refundPrice, 0, false);
- //供货商退款,按供货商分别采购的金额减少
- StatCgGhsClass::hdCgGhsReplace(false, $cg, $refundPrice, 0);
- $currentType = dict::getDict('capitalType', 'hdBookRefund', 'id');
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $event = "预订单{$orderSn}多退少补({$ghsName})";
- $capitalData = [
- 'capitalType' => $currentType,
- 'io' => 1,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //收入统计
- StatIncomeClass::updateOrInsert($main, $shop, $refundPrice);
- $ghsId = $cg->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有找到供货商的门店');
- }
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- $aliParams = [
- 'refundSn' => $refundOrderSn,
- 'orderSn' => $orderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- ];
- $response = $laResource->refund($aliParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('退款失败了哦!请注意:' . $errMsg . ' 金额:' . $refundFee, '15280215347');
- util::fail('退款失败');
- }
- $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
- $cgRefund->thirdRefundNo = $refundTradeNo;
- $cgRefund->save();
- $cg->refundPrice = $refundPrice;
- $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
- $cg->refund = self::REFUND_YES;
- $cg->refundLog = 1;
- $cg->save();
- } else {
- //欠款
- $currentDebt = bcsub($currentPrice, $bookPrice, 2);
- $cg->debtPrice = $currentDebt;
- $cg->remainDebtPrice = $currentDebt;
- $cg->debt = PurchaseClass::DEBT_YES;
- $cg->save();
- $payWay = dict::getDict('payWay', 'debtPay');
- $ghsId = $cg->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $cg, true);
- //支出统计
- StatOutClass::updateOrInsert($main, $shop, $currentDebt);
- //支出流水
- $capitalType = dict::getDict('capitalType', 'hdBookFinalPay', 'id');
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $currentExpend = bcadd($main->totalExpend, $currentDebt, 2);
- $main->totalExpend = $currentExpend;
- $main->save();
- $event = "预订单{$orderSn}补尾款欠款";
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentExpend,
- 'payWay' => $payWay,
- 'amount' => $currentDebt,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //补款给供货商,采购总金额增加
- StatCgClass::replace($main, $shop, $currentDebt, 0, true);
- //补款给供货商,按供货商分别统计采购量增加
- StatCgGhsClass::hdCgGhsReplace(true, $cg, $currentDebt, 0);
- }
- return ['refund' => $cgRefund];
- }
- //添加采购 ssh 2021.1.18
- public static function addPurchase($data, $ghs)
- {
- $shopId = $data['shopId'] ?? 0;
- $sjId = $data['sjId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $ghsId = $data['ghsId'] ?? 0;
- $book = $data['book'] ?? 0;
- $shopAdminId = $data['shopAdminId'] ?? 0;
- $customId = $data['customId'] ?? 0;
- $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'ghsId' => $ghsId, 'customId' => $customId];
- $orderSn = orderSn::getPurchaseSn($snData);
- $data['orderSn'] = $orderSn;
- $prePrice = 0;
- $live = $ghs['live'] ?? 1;
- $sendTimeWant = !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- if ($sendTimeWant < date("Y-m-d")) {
- util::fail('您选了过去时间');
- }
- $data['sendTimeWant'] = $sendTimeWant;
- if (empty($data['product'])) {
- util::fail('请选择花材17');
- }
- $productList = $data['product'];
- $productList = self::mergeItemInfo($productList);
- $data['kindNum'] = count($productList); //多少种花材
- //传过来的是供货商的productId,需要转换为零售端的productId
- //数据格式 [{"productId":"113","bigNum":1,"smallNum":0,"classId":"-2","hdProductId":1}]
- if ($live == 1) {
- //花店真实向批发店采购花材,因为拿的供货商的ID,所以需要转化花店的id
- $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId, $ghs);
- $ghsProductData = self::getProductMapData($productList, "ghsProductId");
- } else {
- //虚拟采购用的自己的ID所以不需要转化
- $ghsProductData = [];
- }
- $hdProductData = self::getProductMapData($productList, "productId");
- $giveLevel = $ghs['giveLevel'] ?? 0;
- $priceMap = CustomClass::$levelPriceKeyMap;
- //这个参数应该是要作废的,但下面加的参数是有用的,多处要同步修改,关键词 abandon_add_price_map ,ssh 20260116
- $addPriceMap = CustomClass::$levelAddPriceKeyMap;
- //但这边加的参数是有用的,给指定客户涨价的
- $addPriceMap['risePercent'] = $ghs['risePercent'] ?? 0;
- //累计满减
- $totalReachDiscount = 0;
- //计算价格= 各个productId的price*num(bigNum,smallNum) + sendCost(配送费)
- $bigNum = 0; //总共多少扎
- $smallNum = 0; //总共多少支
- $totalWeight = 0;
- foreach ($productList as $key => $product) {
- $currentBigNum = $product['bigNum'] ?? 0;
- $currentSmallNum = $product['smallNum'] ?? 0;
- $bigNum += $product['bigNum'] ?? 0;
- $smallNum += $product['smallNum'] ?? 0;
- $productList[$key]['orderSn'] = $orderSn;
- if ($live == 1) {
- $ghsProductId = $product['ghsProductId'];
- $hdProductId = $product['productId'];
- } else {
- $hdProductId = $product['productId'] ?? 0;
- $ghsProductId = 0;
- }
- $unitWeight = $hdProductData[$hdProductId]['weight'] ?? 0;
- $variety = $hdProductData[$hdProductId]['variety'] ?? 0;
- $ratio = $hdProductData[$hdProductId]['ratio'] ?? 0;
- $ratioType = $hdProductData[$hdProductId]['ratioType'] ?? 0;
- $itemId = $hdProductData[$hdProductId]['itemId'] ?? 0;
- $rank = $hdProductData[$hdProductId]['rank'] ?? 'B';
- $stock = $hdProductData[$hdProductId]['stock'] ?? 0;
- $kind = $hdProductData[$hdProductId]['kind'] ?? 0;
- $stockFormat = ProductClass::formatStock($stock, $ratio);
- $limitKey = ProductClass::LIMIT_BUY_KEY . $ghsProductId; // 注意:要使用批发的花材id
- $hasNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $customId]);
- $hasBuyNum = $hasNum ?: 0;
- $itemNum = ProductClass::mergeItemNum($product['bigNum'], $product['smallNum'], $ratio);
- $hasBuyNum = bcadd($itemNum, $hasBuyNum, 2); // 已购买总数量
- $weight = $hdProductData[$hdProductId]['weight'] ?? 0;
- $currentWeight = bcmul($weight, $itemNum, 2);
- $totalWeight = bcadd($currentWeight, $totalWeight, 2);
- //1、供货商开单改价 2、花店向虚拟供货商采购录入价格
- $userPrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : 0;
- $currentGhsProduct = $ghsProductData[$ghsProductId] ?? [];
- if (!empty($currentGhsProduct)) {
- //用批发店的花材名称
- $itemName = $currentGhsProduct['name'] ?? '';
- $cover = $currentGhsProduct['cover'] ?? '';
- //如果零售店自己下预订单,预付0.1元,有两个地方要同步修改,搜索关键词 pre0.1
- if ($book == 1 && $shopAdminId > 0) {
- $price = 0.1;
- } else {
- //用批发店花材的价格
- $price = ProductClass::getFinalPrice($currentGhsProduct, $giveLevel, $priceMap, $addPriceMap, $userPrice, $itemNum, $hasBuyNum);
- }
- $thisPrice = $price;
- $reachNum = $currentGhsProduct['reachNum'] ?? 0;
- if ($reachNum > 0) {
- if ($itemNum >= $reachNum) {
- $reachNumDiscount = $currentGhsProduct['reachNumDiscount'] ?? 0;
- $currentReachDiscount = bcmul($reachNumDiscount, $itemNum, 2);
- $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
- }
- }
- } else {
- $currentHdProduct = $hdProductData[$hdProductId] ?? [];
- if (empty($currentHdProduct)) {
- util::fail('花材有问题哦');
- }
- $itemName = $currentHdProduct['name'] ?? '';
- $cover = $currentHdProduct['cover'] ?? '';
- $price = $userPrice;
- $thisPrice = $userPrice;
- }
- if ($currentSmallNum > 0) {
- $productTotalPrice = bcmul($currentSmallNum, $price, 2);
- } else {
- $productTotalPrice = bcmul($currentBigNum, $price, 2);
- }
- $itemInfo = [
- 'itemName' => $itemName,
- 'itemCover' => $cover,
- 'bigNum' => $product['bigNum'] ?? 0,
- 'smallNum' => $product['smallNum'] ?? 0,
- 'bigNumStock' => $stockFormat['bigNum'],
- 'smallNumStock' => $stockFormat['smallNum'],
- 'itemUnit' => $ratio ? 2 : 1,
- 'ratio' => $ratio,
- 'rank' => $rank,
- 'bigUnit' => $hdProductData[$hdProductId]['bigUnit'],
- 'smallUnit' => $hdProductData[$hdProductId]['smallUnit'],
- ];
- if ($itemNum <= 0) {
- util::fail($itemName . ' 数量要大于零');
- }
- $productList[$key]['itemInfo'] = json_encode($itemInfo);
- $productList[$key]['variety'] = $variety;
- $productList[$key]['itemId'] = $itemId;
- $productList[$key]['preItemNum'] = $itemNum;
- $productList[$key]['itemNum'] = $itemNum;
- $productList[$key]['bigNum'] = $product['bigNum'] ?? 0;
- $productList[$key]['smallNum'] = $product['smallNum'] ?? 0;
- $productList[$key]['price'] = $thisPrice;
- $productList[$key]['ratio'] = $ratio;
- $productList[$key]['ratioType'] = $ratioType;
- $productList[$key]['unitWeight'] = $unitWeight;
- $productList[$key]['totalPrice'] = $productTotalPrice;
- $productList[$key]['preTotalPrice'] = $productTotalPrice;
- $productList[$key]['productId'] = $hdProductId;
- $productList[$key]['ghsProductId'] = $ghsProductId;
- $productList[$key]['smallPrice'] = $price;
- $productList[$key]['mainId'] = $mainId;
- $productList[$key]['ghsId'] = $ghsId;
- $productList[$key]['name'] = $itemName;
- $productList[$key]['cover'] = $cover;
- $productList[$key]['kind'] = $kind;
- $currentBigUnit = $hdProductData[$hdProductId]['bigUnit'] ?? '';
- $currentSmallUnit = $hdProductData[$hdProductId]['smallUnit'] ?? '';
- if ($currentBigNum > 0) {
- $xhNum = $currentBigNum;
- $xhUnitName = $currentBigUnit;
- $xhUnitType = dict::getDict('unitType', 'big');
- } else {
- $xhNum = $currentSmallNum;
- $xhUnitName = $currentSmallUnit;
- $xhUnitType = dict::getDict('unitType', 'small');
- }
- $itemPrice = $price;
- $price = bcmul($itemPrice, $xhNum, 2);
- //客户下单时会出现同时选了扎和支,很奇怪,前端没有找到原因,后端限制
- if ($currentBigNum > 0 && $currentSmallNum > 0) {
- util::fail($itemName . " {$currentBigUnit}和{$currentSmallUnit}只能选一种");
- }
- $productList[$key]['xhNum'] = $xhNum;
- $productList[$key]['xhUnitName'] = $xhUnitName;
- $productList[$key]['xhUnitType'] = $xhUnitType;
- $productList[$key]['xhUnitPrice'] = $itemPrice;
- $productList[$key]['xhPrice'] = $price;
- $productList[$key]['xhPreNum'] = $xhNum;
- $productList[$key]['xhPreUnitName'] = $xhUnitName;
- $productList[$key]['xhPreUnitType'] = $xhUnitType;
- $productList[$key]['xhPreUnitPrice'] = $itemPrice;
- $productList[$key]['xhPrePrice'] = $price;
- $prePrice = bcadd($prePrice, $productTotalPrice, 2);
- }
- $data['itemPrice'] = $prePrice;
- $sendCost = $data['sendCost'] ?? 0.00;
- $prePrice = bcadd($prePrice, $sendCost, 2);
- $packCost = $data['packCost'] ?? 0;
- $prePrice = bcadd($prePrice, $packCost, 2);
- $data['weight'] = $totalWeight;
- //预订单需要增加服务费
- if ($book == 1) {
- $kiloFee = $ghs['kiloFee'] ?? 0;
- $miniKilo = $ghs['miniKilo'] ?? 0;
- $calcWeight = max($totalWeight, $miniKilo);
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $prePrice = bcadd($prePrice, $allKiloFee, 2);
- $data['allKiloFee'] = $allKiloFee;
- $data['kiloFee'] = $kiloFee;
- $data['miniKilo'] = $miniKilo;
- $data['stockChange'] = 0;
- }
- $actPrice = $prePrice;
- if (isset($data['modifyPrice'])) {
- $modifyPrice = $data['modifyPrice'];
- if ($modifyPrice < $prePrice) {
- //优惠金额
- $data['discountAmount'] = bcsub($prePrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- }
- if ($modifyPrice > $prePrice) {
- //人工和资材费用
- $data['labourCost'] = bcsub($modifyPrice, $prePrice, 2);
- }
- $actPrice = $modifyPrice;
- if ($book == 1) {
- $actPrice = 0;
- }
- } else {
- //客户自己下单才有整单满减,门店帮忙开单没有,,多个地方要同步修改,关键词 meet_amount_num_reduce
- if ($book == 0) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (!empty($ghsShop)) {
- $meetNum = $ghsShop->meetNum ?? 0;
- $meetAmount = $ghsShop->meetAmount ?? 0;
- $cutStyle = $ghsShop->cutStyle ?? 0;
- $cutOption = $ghsShop->cutAmount ?? 0;
- if ($cutStyle == 0) {
- $cutAmount = $cutOption;
- } else {
- $miniOption = bcsub(1, $cutOption, 2);
- $cutAmount = bcmul($actPrice, $miniOption, 2);
- }
- if ($cutAmount > 0 && $meetNum > 0 && $meetAmount > 0) {
- if ($bigNum >= $meetNum && $data['itemPrice'] >= $meetAmount) {
- if ($cutAmount < $actPrice) {
- $actPrice = bcsub($actPrice, $cutAmount, 2);
- $data['orderReachDiscountPrice'] = $cutAmount;
- } else {
- noticeUtil::push('订单金额不足满减金额,ghsShopId:' . $ghsShopId, '15280215347');
- }
- }
- }
- }
- }
- }
- //如果有设置指定客户涨价百分之几,然后又设置了涨价的幅度。【这个功能主要用于客户不收他运费,门店不赚钱,收他运费客户不乐意,所以把运费加到花材里】,多处同步修改,关键词 rise_percent_rang
- if (isset($data['itemPrice'])) {
- if (isset($data['orderReachDiscountPrice'])) {
- $originActPrice = bcadd($actPrice, $data['orderReachDiscountPrice'], 2);
- } else {
- $originActPrice = $actPrice;
- }
- if ($originActPrice >= $data['itemPrice']) {
- if (isset($ghs['risePercent']) && $ghs['risePercent'] > 0) {
- if (isset($ghs['riseRange']) && $ghs['riseRange'] > 0) {
- $risePercent = $ghs['risePercent'];
- $riseRange = $ghs['riseRange'];
- // 计算涨价金额:涨价后金额 * (涨价百分比 / (100 + 涨价百分比))
- $riseAmount = bcmul($data['itemPrice'], bcdiv($risePercent, bcadd(100, $risePercent, 0), 4), 2);
- if ($riseAmount > $riseRange) {
- //应该退回的金额
- $backAmount = bcsub($riseAmount, $riseRange, 2);
- $actPrice = bcsub($actPrice, $backAmount, 2);
- if (isset($data['orderReachDiscountPrice'])) {
- if ($data['orderReachDiscountPrice'] < $backAmount) {
- //前面整单满减的,就不减了,所以这里面加回来
- $actPrice = bcadd($actPrice, $data['orderReachDiscountPrice'], 2);
- //改成减多加钱里面的
- $data['orderReachDiscountPrice'] = $backAmount;
- }
- } else {
- $data['orderReachDiscountPrice'] = $backAmount;
- }
- }
- }
- }
- }
- }
- //这个字段记录每个花材满多少扎减掉的金额,并汇总金额,区别于整单满减的 $cutAmount。保存起来应该是在哪里有用,$totalReachDiscount在这边不影响actPrice,估计因为取花材的价格里::getFinalPrice()已经使用了优惠后的价格,有多处,关键词 reachDiscountPrice_explain ssh 20260116
- $data['reachDiscountPrice'] = $totalReachDiscount;
- $data['prePrice'] = $prePrice;
- $data['bookPrice'] = $actPrice;
- $data['orderPrice'] = $actPrice;
- $data['actPrice'] = $actPrice;
- $data['realPrice'] = $actPrice;
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- $data['discount'] = 1;
- $data['status'] = self::STATUS_UN_PAY;
- //采购单有效期
- $currentTime = time();
- $validTime = dict::getDict('order_pay_has_time');
- $current = $currentTime + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- if ($book == 1) {
- $fiveTime = bcmul(86400, 5);
- $bookDeadline = bcadd($currentTime, $fiveTime);
- $data['deadline'] = date("Y-m-d H:i:s", $bookDeadline);
- }
- //采购单自动记为欠款单时间
- $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
- $currentSet = $currentTime + $localConfirmTime;
- $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
- //保存订单
- $respond = self::add($data, true);
- foreach ($productList as $productVal) {
- //增加采购单的花材
- $returnItem = PurchaseItemClass::add($productVal, true);
- $productId = $productVal['productId'];
- $unitPrice = $returnItem->price ?? 0;
- $currentId = $returnItem->id ?? 0;
- $currentPtItemId = $returnItem->itemId ?? 0;
- if (!empty($data['xj'][$currentPtItemId])) {
- $xjArr = $data['xj'][$currentPtItemId];
- if (is_array($xjArr)) {
- $ids = array_column($xjArr, 'id');
- $sjInfo = XjClass::getByIds($ids, null, 'id');
- $xjData = [];
- foreach ($xjArr as $xjItem) {
- $id = $xjItem['id'] ?? 0;
- $num = $xjItem['num'] ?? 0;
- $name = $sjInfo[$id]['name'] ?? '';
- $cover = $sjInfo[$id]['cover'] ?? '';
- $xjData[] = ['xjId' => $id, 'name' => $name, 'ptItemId' => $currentPtItemId,
- 'num' => $num, 'price' => $unitPrice, 'itemId' => $currentId, 'cover' => $cover];
- }
- CgXjClass::batchAdd($xjData);
- $returnItem->variety = 1;
- $returnItem->save();
- }
- }
- if (!empty($data['treeData'][$currentPtItemId])) {
- $myTreeData = $data['treeData'][$currentPtItemId];
- foreach($myTreeData as $treeItem) {
- $treeNum = $treeItem['num'] ?? 0;
- $treeParams = [
- 'orderSn' => $orderSn,
- 'num' => $treeNum,
- 'productId' => $productId,
- ];
- CgTreeClass::add($treeParams);
- $dishNum = count($myTreeData);
- $returnItem->dishNum = $dishNum;
- $returnItem->save();
- }
- }
- }
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店59 ' . $shopId);
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息13');
- }
- $main->totalPurchaseOrder += 1;
- $main->cgUnPay += 1;
- $main->save();
- return $respond;
- }
- //添加采购 ssh 2021.1.18
- public static function updatePurchase($purchase, $data)
- {
- util::fail('已删除');
- }
- //组合数据 ssh 2021.1.25
- public static function groupPurchase($list, $showButton = false, $showProduct = false)
- {
- $ghsIds = array_unique(array_filter(array_column($list, 'ghsId')));
- $ghsInfo = GhsClass::getGhsByIds($ghsIds);
- $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
- $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
- $time = time();
- foreach ($list as $key => $val) {
- //剩余支付时间
- $remainPayTime = 0;
- $deadline = $val['deadline'] ?? '';
- if (!empty($deadline)) {
- $deadTime = strtotime($deadline);
- if (($deadTime - $time - 30) > 0) {
- $remainPayTime = $deadTime - $time;
- $remainPayTime = $remainPayTime - 30;
- }
- }
- $list[$key]['remainPayTime'] = $remainPayTime;
- $adminId = $val['adminId'] ?? 0;
- $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
- $ghsId = $val['ghsId'] ?? 0;
- $list[$key]['supplierName'] = $ghsInfo[$ghsId]['name'] ?? '';
- $list[$key]['supplierAvatar'] = $ghsInfo[$ghsId]['smallLogoUrl'] ?? '';
- $list[$key]['supplierMobile'] = $ghsInfo[$ghsId]['mobile'] ?? '';
- $list[$key]['supplierAddress'] = $ghsInfo[$ghsId]['address'] ?? '';
- $list[$key]['debtAmount'] = $ghsInfo[$ghsId]['debtAmount'] ?? '';
- //剩余欠款
- $balance = $ghsInfo[$ghsId]['balance'] ?? 0;
- $debtAmount = $ghsInfo[$ghsId]['debtAmount'] ?? 0;
- $ghsSalt = $ghsInfo[$ghsId]['salt'] ?? '';
- $remainDebtAmount = bcsub($debtAmount, $balance, 2);
- $list[$key]['remainDebtAmount'] = floatval($remainDebtAmount);
- $list[$key]['inMyGhsBalance'] = floatval($balance);
- $list[$key]['ghsSalt'] = $ghsSalt;
- $list[$key]['lat'] = $ghsInfo[$ghsId]['lat'] ?? '';
- $list[$key]['long'] = $ghsInfo[$ghsId]['long'] ?? '';
- $list[$key]['supplierId'] = $ghsInfo[$ghsId]['id'] ?? '';
- if ($showButton) {
- $list[$key]['buttonList'] = [
- ['type' => 'pay', 'show' => 1, 'disable' => 1],
- ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
- ];
- }
- $list[$key]['sendProgress'] = [];
- if ($showProduct) {
- $orderSn = $val['orderSn'];
- $orderItem = PurchaseItemClass::getOrderItemDetail($orderSn);
- $itemData = [];
- $xjData = [];
- if ($orderItem) {
- foreach ($orderItem as $v) {
- $info = json_decode($v['itemInfo'], true);
- $tmp = $info;
- unset($v['itemInfo']);
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp['name'] = $tmp['itemName'] ?? '';
- $tmp = array_merge($tmp, $v);
- $shortCover = $info['itemCover'] ?? '';
- $tmp['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- if (isset($tmp['variety']) && $tmp['variety'] == 1) {
- $currentItemId = $tmp['id'] ?? 0;
- $xjList = CgXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
- if (!empty($xjList)) {
- foreach ($xjList as $xjKey => $xj) {
- $xjBase = $tmp;
- $num = $xj['num'] ?? 0;
- $name = $xj['name'] ?? '';
- $cover = $xj['cover'] ?? '';
- $price = $xj['price'] ?? '';
- $totalPrice = bcmul($num, $price, 2);
- if ($num > 0) {
- $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $xjBase['name'] = $name;
- $xjBase['itemNum'] = floatval($num);
- $xjBase['bigNum'] = floatval($num);
- $xjBase['smallNum'] = 0;
- $xjBase['price'] = $price;
- $xjBase['totalPrice'] = $totalPrice;
- $xjBase['xhNum'] = floatval($num);
- $xjAddList[] = $xjBase;
- }
- $xjData[] = $xjBase;
- }
- } else {
- $itemData[] = $tmp;
- }
- } else {
- $itemData[] = $tmp;
- }
- }
- if (!empty($xjData)) {
- $itemData = array_merge($itemData, $xjData);
- }
- }
- $list[$key]['product'] = $itemData;
- }
- }
- return $list;
- }
- //订单取消,变更状态,占用库存释放 ssh 2021.5.8
- public static function cancel($id)
- {
- $purchase = PurchaseClass::getLockById($id);
- $purchase->status = self::STATUS_CANCEL;
- $purchase->cancelTime = date("Y-m-d H:i:s");
- $purchase->save();
- $shopId = $purchase->shopId;
- $orderSn = $purchase->orderSn ?? '';
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店60');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息14');
- }
- $main->cgUnPay -= 1;
- $main->cgCancel += 1;
- $main->save();
- $ghsId = $purchase->ghsId;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有找到供货商门店');
- }
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = ['orderSn' => $orderSn];
- $response = $laResource->close($closeParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- //主扫(手机上微信下单拉取微信支付)调用这个方法有效,如果收银机上扫客户收款码的,调用这个方法无效,具体说明看 https://o.lakala.com/#/home/document/detail?id=115 所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
- //noticeUtil::push('花店采购 ' . $orderSn . ' 关单没有成功哦' . $msg . ' ' . $code, '15280215347');
- }
- //收银台关单
- $closeParams = ['orderSn' => $orderSn];
- $response = $laResource->cashClose($closeParams);
- if (!isset($response['code']) || $response['code'] != '000000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- //聚合收银台(APP发起的微信和支付支付)调用这个方法有效,说明 https://o.lakala.com/#/home/document/detail?id=722,其他情况调用无法,所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
- //noticeUtil::push('花店采购 ' . $orderSn . ' 收银台关单的提示:' . $msg . ' ' . $code, '15280215347');
- }
- return $purchase;
- }
- //采购统计2021-05-08
- //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
- public static function statOrderInfo($shopId, $statType)
- {
- $start = '';
- $end = '';
- switch ($statType) {
- case "today":
- $start = date('Y-m-d');
- $end = $start;
- break;
- case "yesterday" :
- $start = date('Y-m-d', strtotime("-1 day"));
- $end = $start;
- break;
- case "seven":
- $start = date('Y-m-d', strtotime("-6 day"));
- $end = date('Y-m-d');
- break;
- case "thirty":
- $start = date('Y-m-d', strtotime("-29 day"));
- $end = date('Y-m-d');
- break;
- default:
- }
- $where = [];
- $where['shopId'] = $shopId;
- $where['addTime'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "id,actPrice");
- $totalAmount = 0;
- $totalNum = count($res);
- foreach ($res as $v) {
- $totalAmount = bcadd($totalAmount, $v['actPrice'], 2);
- }
- return [
- 'totalAmount' => $totalAmount,
- 'totalNum' => $totalNum,
- ];
- }
- //采购单完成 ssh 2021.5.15
- public static function complete($purchase)
- {
- $purchase->status = self::STATUS_COMPLETE;
- $purchase->save();
- $shopId = $purchase->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店61');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息15');
- }
- $main->cgSending -= 1;
- $main->cgFinish += 1;
- $main->save();
- /********订单完成后加库存 lqh 2021-05-10*********/
- $orderSn = $purchase->orderSn;
- $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- foreach ($purchaseItem as $v) {
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $purchase->sjId;
- $recordData['shopId'] = $purchase->shopId;
- $recordData['mainId'] = $purchase->mainId;
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $v['itemNum'];
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $orderSn;
- StockRecordClass::addPurchaseOrderRecord($recordData);
- }
- }
- //订单信息变更为配送中 ssh 2021.5.15
- public static function sending($purchaseId)
- {
- $purchase = PurchaseClass::getById($purchaseId, true);
- if (empty($purchase)) {
- util::fail('没有采购单信息');
- }
- $purchase->status = PurchaseClass::STATUS_SENDING;
- $purchase->save();
- $shopId = $purchase->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店62');
- }
- $shop->cgUnSend -= 1;
- $shop->cgSending += 1;
- $shop->save();
- //自动完成订单时不需要通知
- $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
- if ($noNeedWxNotice == 0) {
- WxMessageClass::cgOrderHasSend($shop, $purchase);
- }
- }
- public static function valid($cg, $shopId)
- {
- if (isset($cg->shopId) == false || $cg->shopId != $shopId) {
- util::fail('无法访问,编号1255');
- }
- }
- }
|