| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487 |
- <?php
- namespace bizGhs\stat\classes;
- use biz\stat\classes\StatStockInClass;
- use biz\stat\classes\StatStockOutClass;
- use bizGhs\cg\classes\CgRefundClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\custom\classes\DistClass;
- use bizGhs\expend\classes\ExpendClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizGhs\order\classes\PurchaseOrderClass;
- use bizGhs\order\classes\PurchaseOrderItemClass;
- use bizGhs\order\classes\RefundOrderClass;
- use bizGhs\order\classes\StockWastageOrderClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\staff\classes\SalaryClass;
- use bizHd\goods\classes\PlantCgClass;
- use bizHd\order\classes\OrderClass as HdOrderClass;
- use bizGhs\order\classes\OrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\refund\classes\HdRefundClass;
- use common\components\arrayUtil;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- class StatSaleClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\stat\models\StatSale';
- public static function wastage($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
- $totalPrice = 0;
- $totalCost = 0;
- if (!empty($list)) {
- foreach ($list as $order) {
- $price = $order['price'] ?? 0;
- $totalPrice = bcadd($totalPrice, $price, 2);
- $cost = $order['cost'] ?? 0;
- $totalCost = bcadd($totalCost, $cost, 2);
- }
- }
- $totalPrice = floatval($totalPrice);
- return ['totalPrice' => $totalPrice, 'totalCost' => $totalCost];
- }
- //结账跟踪 ssh 20230503
- public static function OrderClear($mainId)
- {
- $get = Yii::$app->request->get();
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $timeStyle = $get['timeStyle'] ?? 0;
- if (isset($get['searchTime']) && !empty($get['searchTime'])) {
- $searchTime = $get['searchTime'];
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- if ($timeStyle == 0) {
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- } else {
- $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
- }
- } else {
- $payTime = 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- }
- $customId = $get['customId'] ?? 0;
- if (!empty($customId)) {
- $where['customId'] = $customId;
- }
- $unClear = $get['unClear'] ?? 0;
- if ($unClear == 1) {
- $where['clearId'] = 0;
- $where['remainDebtPrice>'] = 0;
- }
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- return ['list' => $ghsOrderList];
- }
- //客户下单榜 ssh 20230502
- public static function statCustomCg($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
- $arr = [];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $totalAmount = 0;
- $totalNum = 0;
- $totalProfit = 0;
- $totalCost = 0;
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $cost = $ghsOrder['remainCost'] ?? 0;
- $profit = bcsub($actPrice, $cost, 2);
- $orderPrice = $ghsOrder['orderPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $customId = $ghsOrder['customId'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- $book = $ghsOrder['book'] ?? 0;
- //扎数
- $count = $ghsOrder['itemNum'] ?? 0;
- if ($status == 1 || $status == 5) {
- continue;
- }
- if ($book == 1 && $status == 2) {
- continue;
- }
- $customName = $customList[$customId]['name'] ?? '';
- $py = $customList[$customId]['py'] ?? '';
- $totalNum++;
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (isset($arr[$customId])) {
- $arr[$customId]['num']++;
- $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
- $arr[$customId]['count'] = bcadd($arr[$customId]['count'], $count, 2);
- $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
- $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
- $arr[$customId]['cost'] = bcadd($arr[$customId]['cost'], $cost, 2);
- $arr[$customId]['profit'] = bcadd($arr[$customId]['profit'], $profit, 2);
- } else {
- $arr[$customId] = [
- 'customId' => $customId,
- 'customName' => $customName,
- 'py' => $py,
- 'num' => 1,
- 'count' => $count,
- 'amount' => $actPrice,
- 'levelName' => '',
- 'orderPrice' => $orderPrice,
- 'tkPrice' => $tkPrice,
- 'cost' => $cost,
- 'profit' => $profit,
- ];
- }
- }
- $arr = arrayUtil::arraySort($arr, 'profit');
- }
- $totalAmount = floatval($totalAmount);
- return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
- }
- public static function profile($mainId)
- {
- //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
- //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- //出库
- $stockOut = StatStockOutClass::getSum($where);
- //采购入库
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- $cg = 0;
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $actPrice = $cgItem['actPrice'] ?? 0;
- $paidPrice = $cgItem['paidPrice'] ?? 0;
- if ($actPrice <= 0) {
- // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
- $cg = bcadd($cg, 0, 2);
- } else {
- $total = bcadd($actPrice, $paidPrice, 2);
- //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
- //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
- //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
- if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $a1 = bcadd($shortCharge, $longCharge, 2);
- $a2 = bcadd($pickCharge, $localCharge, 2);
- $a = bcadd($a1, $a2, 2);
- //注意下面是减号 bcsub
- $total = bcsub($total, $a, 2);
- }
- $cg = bcadd($cg, $total, 2);
- }
- }
- }
- $cg = floatval($cg);
- $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
- $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
- $lsCgAmount = 0;
- if (!empty($lsCgList)) {
- foreach ($lsCgList as $lsCgItem) {
- $actPrice = $lsCgItem['actPrice'] ?? 0;
- $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
- }
- }
- $lsCgAmount = floatval($lsCgAmount);
- $lzWhere = ['mainId' => $mainId, 'status' => 3];
- $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
- $lzCgAmount = 0;
- if (!empty($lzCgList)) {
- foreach ($lzCgList as $lzInfo) {
- $actPrice = $lzInfo['actPrice'] ?? 0;
- $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
- }
- }
- $lzCgAmount = floatval($lzCgAmount);
- //调拨入库
- $stockIn = StatStockInClass::getSum($where);
- $totalOrderNum = 0;
- //批发开单
- $pfIncome = 0;
- $pfSendCost = 0;
- $pfPack = 0;
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- $sendCost = $ghsOrder['sendCost'] ?? 0;
- $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
- $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
- $packCost = $ghsOrder['packCost'] ?? 0;
- $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
- $remainPackCost = bcsub($packCost, $refundPackCost, 2);
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
- $pfPack = bcadd($pfPack, $remainPackCost, 2);
- $totalOrderNum++;
- }
- }
- //零售开单
- $payCodeIncome = 0;
- $lsAfterSale = 0;
- $lsGiveBalance = 0;
- $lsIncome = 0;
- $lsSendCost = 0;
- $lsServiceFee = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $onlinePay = $hdOrder['onlinePay'] ?? 1;
- $repeat = $hdOrder['repeat'] ?? 0;
- $forward = $hdOrder['forward'] ?? 0;
- if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
- $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
- }
- if ($forward == 1) {
- //零售售后付款
- $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
- } else {
- $cash = $hdOrder['cash'] ?? 0;
- $serviceFee = $hdOrder['serviceFee'] ?? 0;
- $sendCost = $hdOrder['sendCost'] ?? 0;
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- //如果$actPrice<=0,则退款取消先不计算成本
- if ($actPrice > 0) {
- $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
- $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
- }
- $totalOrderNum++;
- if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
- $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
- $payCodeIncome = floatval($payCodeIncome);
- }
- $giveBalance = $hdOrder['giveBalance'] ?? 0;
- $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
- $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
- $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
- }
- }
- $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
- $salary = 0;
- $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
- if (!empty($salaryList)) {
- foreach ($salaryList as $item) {
- $salary = bcadd($salary, $item->amount, 2);
- }
- }
- $condition = [
- 'mainId' => $mainId,
- 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
- 'delStatus' => 0,
- ];
- $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
- $expendTypeMap = dict::getDict('expendTypeMap');
- $expendAmount = [];
- if (!empty($expendList)) {
- foreach ($expendList as $item) {
- $type = $item->type ?? 0;
- $amount = $item->amount ?? 0;
- $name = $expendTypeMap[$type] ?? '暂无';
- if (!isset($expendAmount[$type]['amount'])) {
- $expendAmount[$type]['amount'] = 0;
- }
- $expendAmount[$type]['name'] = $name;
- $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
- }
- }
- $income = [
- ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
- ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
- ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
- ];
- $expend = [
- ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
- ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
- ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
- ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
- ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
- ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
- ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
- ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
- ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
- ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
- ['name' => '收款码收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
- ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
- ];
- if (!empty($expendAmount)) {
- foreach ($expendAmount as $it) {
- $expend[] = $it;
- }
- }
- $balance = 0;
- foreach ($income as $it) {
- $amount = $it['amount'] ?? 0;
- $balance = bcadd($amount, $balance, 2);
- }
- foreach ($expend as $it) {
- $amount = $it['amount'] ?? 0;
- $balance = bcsub($balance, $amount, 2);
- }
- return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
- }
- //按花材统计收入 ssh 20231111
- public static function incomeOutItem($mainId)
- {
- $get = Yii::$app->request->get();
- $export = $get['export'] ?? 0;
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号666');
- }
- $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
- if (empty($productList)) {
- util::fail('没有找到花材');
- }
- $arr = [];
- //打包费和运费
- $packFreight = 0;
- $onlyRefundCg = 0;
- $onlyRefund = 0;
- $totalDiscountAmount = 0;
- $totalLabourCost = 0;
- //采购入库
- $tbCgIncome = [];
- $tbCgIncomeStaff = [];
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $packingCharge = $cgItem['packingCharge'] ?? 0;
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $packFreight = bcadd($packFreight, $packingCharge, 2);
- $packFreight = bcadd($packFreight, $shortCharge, 2);
- $packFreight = bcadd($packFreight, $longCharge, 2);
- $packFreight = bcadd($packFreight, $pickCharge, 2);
- $packFreight = bcadd($packFreight, $localCharge, 2);
- $tkPrice = $cgItem['tkPrice'] ?? 0;
- $orderSn = $cgItem['orderSn'] ?? '';
- $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgItemInfoList)) {
- foreach ($cgItemInfoList as $cgData) {
- $itemNum = $cgData->itemNum ?? 0;
- $productId = $cgData->productId ?? 0;
- $refundNum = $cgData->refundNum ?? 0;
- $remainNum = bcsub($itemNum, $refundNum);
- $bigPrice = $cgData->bigPrice ?? 0;
- $amount = bcmul($remainNum, $bigPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['expend'])) {
- $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
- $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = $amount;
- $arr[$productId]['outNum'] = $remainNum;
- $arr[$productId]['inNum'] = 0;
- $arr[$productId]['income'] = 0;
- }
- }
- if ($tkPrice > 0) {
- $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgRefundOrderList)) {
- foreach ($cgRefundOrderList as $cgRefundOrder) {
- if ($cgRefundOrder->refundType == 2) {
- $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
- }
- }
- }
- }
- }
- }
- }
- //批发开单
- $pfIncome = 0;
- $tbSaleIncome = [];
- $tbSaleIncomeStaff = [];
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
- $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $labourCost = $ghsOrder['labourCost'] ?? 0;
- $discountAmount = $ghsOrder['discountAmount'] ?? 0;
- $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
- $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $refundNum = $orderItem->refundNum ?? 0;
- $name = $orderItem->name ?? '';
- $productId = $orderItem->productId ?? 0;
- $num = $orderItem->xhNum ?? 0;
- $remainNum = bcsub($num, $refundNum);
- $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
- $amount = bcmul($remainNum, $xhUnitPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['income'])) {
- $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
- $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = 0;
- $arr[$productId]['outNum'] = 0;
- $arr[$productId]['inNum'] = $remainNum;
- $arr[$productId]['income'] = $amount;
- }
- //单独统计
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
- $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
- $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
- } else {
- $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 1) {
- //退货退款前面已经考虑,这里不再处理 OK
- } else {
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- }
- //零售开单
- $lsIncome = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $cash = $hdOrder['cash'] ?? 0;
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- $orderSn = $hdOrder['orderSn'] ?? '';
- $tkPrice = $hdOrder['tkPrice'] ?? 0;
- $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $unitPrice = $orderItem->unitPrice ?? 0;
- $refundNum = $orderItem->refundNum ?? 0;
- $num = $orderItem->num ?? 0;
- $productId = $orderItem->itemId ?? 0;
- $remainNum = bcsub($num, $refundNum);
- $amount = bcmul($remainNum, $unitPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['income'])) {
- $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
- $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = 0;
- $arr[$productId]['outNum'] = 0;
- $arr[$productId]['inNum'] = $remainNum;
- $arr[$productId]['income'] = $amount;
- }
- }
- if ($tkPrice > 0) {
- $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 2) {
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- $totalIncome = 0;
- $totalExpend = 0;
- if (!empty($arr)) {
- foreach ($arr as $key => $val) {
- $income = $val['income'] ?? 0;
- $income = floatval($income);
- $totalIncome = bcadd($totalIncome, $income, 2);
- $expend = $val['expend'] ?? 0;
- $expend = floatval($expend);
- $totalExpend = bcadd($totalExpend, $expend, 2);
- $profit = bcsub($income, $expend, 2);
- $arr[$key]['profit'] = floatval($profit);
- $arr[$key]['income'] = floatval($income);
- $arr[$key]['expend'] = floatval($expend);
- }
- }
- //总成本算上物流打包等费用
- $totalExpend = bcadd($totalExpend, $packFreight, 2);
- //总收入算上采购仅退款
- $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
- //总收入加上人工费
- $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
- //总收入减掉优惠金额
- $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
- //总成本算上销售仅退款
- $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
- $totalProfit = bcsub($totalIncome, $totalExpend, 2);
- $totalIncome = floatval($totalIncome);
- $totalExpend = floatval($totalExpend);
- $totalProfit = floatval($totalProfit);
- $arr = array_values($arr);
- $arr = arrayUtil::arraySort($arr, 'profit');
- if ($export == 1) {
- $statData = [
- ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
- ];
- $list = array_merge($arr, $statData);
- self::exportIncomeOutItem($list, $mainId);
- }
- util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
- 'totalIncome' => $totalIncome,
- 'totalExpend' => $totalExpend,
- 'totalProfit' => $totalProfit,
- 'tbSaleIncome' => $tbSaleIncome,
- 'tbCgIncome' => $tbCgIncome,
- 'tbCgIncomeStaff' => $tbCgIncomeStaff,
- 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
- 'totalLabourCost' => $totalLabourCost,
- 'totalDiscountAmount' => $totalDiscountAmount,
- ]);
- }
- //按分类统计收入 ssh 20231111
- public static function classProfile($mainId)
- {
- $get = Yii::$app->request->get();
- $export = $get['export'] ?? 0;
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号780');
- }
- $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
- if (empty($classList)) {
- util::fail('没有找到花材');
- }
- $arr = [];
- //打包费和运费
- $packFreight = 0;
- $onlyRefundCg = 0;
- $onlyRefund = 0;
- $totalDiscountAmount = 0;
- $totalLabourCost = 0;
- //采购入库
- $tbCgIncome = [];
- $tbCgIncomeStaff = [];
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $packingCharge = $cgItem['packingCharge'] ?? 0;
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $cgStaffId = $cgItem['cgStaffId'] ?? 0;
- $cgStaffName = $cgItem['cgStaffName'] ?? '';
- $actPrice = $cgItem['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $packFreight = bcadd($packFreight, $packingCharge, 2);
- $packFreight = bcadd($packFreight, $shortCharge, 2);
- $packFreight = bcadd($packFreight, $longCharge, 2);
- $packFreight = bcadd($packFreight, $pickCharge, 2);
- $packFreight = bcadd($packFreight, $localCharge, 2);
- $tkPrice = $cgItem['tkPrice'] ?? 0;
- $orderSn = $cgItem['orderSn'] ?? '';
- $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgItemInfoList)) {
- foreach ($cgItemInfoList as $cgData) {
- $itemNum = $cgData->itemNum ?? 0;
- $productId = $cgData->productId ?? 0;
- $name = $cgData->name ?? '';
- $refundNum = $cgData->refundNum ?? 0;
- $remainNum = bcsub($itemNum, $refundNum);
- $bigPrice = $cgData->bigPrice ?? 0;
- $amount = bcmul($remainNum, $bigPrice, 2);
- $classId = $cgData->classId;
- $className = $classList[$classId]['name'] ?? '已删除';
- if ($className == '运费') {
- //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
- }
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['expend'])) {
- $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
- $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = $amount;
- $arr[$classId]['income'] = 0;
- $arr[$classId]['cgNum'] = $remainNum;
- $arr[$classId]['saleNum'] = 0;
- }
- //单独统计
- $belongCost = $cgData->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbCgIncome[$productId])) {
- $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
- $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
- } else {
- $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
- }
- if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
- $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
- $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
- } else {
- $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
- }
- }
- }
- if ($tkPrice > 0) {
- $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgRefundOrderList)) {
- foreach ($cgRefundOrderList as $cgRefundOrder) {
- if ($cgRefundOrder->refundType == 2) {
- $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
- }
- }
- }
- }
- }
- }
- }
- //批发开单
- $pfIncome = 0;
- $tbSaleIncome = [];
- $tbSaleIncomeStaff = [];
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
- $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $labourCost = $ghsOrder['labourCost'] ?? 0;
- $discountAmount = $ghsOrder['discountAmount'] ?? 0;
- $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
- $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $refundNum = $orderItem->refundNum ?? 0;
- $name = $orderItem->name ?? '';
- $productId = $orderItem->productId ?? 0;
- $num = $orderItem->xhNum ?? 0;
- $remainNum = bcsub($num, $refundNum);
- $classId = $orderItem->classId ?? 0;
- $className = $classList[$classId]['name'] ?? '已删除';
- $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
- $amount = bcmul($remainNum, $xhUnitPrice, 2);
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['income'])) {
- $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
- $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = 0;
- $arr[$classId]['income'] = $amount;
- $arr[$classId]['saleNum'] = $remainNum;
- $arr[$classId]['cgNum'] = 0;
- }
- //单独统计
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
- $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
- $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
- } else {
- $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 1) {
- //退货退款前面已经考虑,这里不再处理 OK
- } else {
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- }
- //零售开单
- $lsIncome = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $cash = $hdOrder['cash'] ?? 0;
- if ($actPrice <= 0 && $cash <= 0) {
- continue;
- }
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- $orderSn = $hdOrder['orderSn'] ?? '';
- $tkPrice = $hdOrder['tkPrice'] ?? 0;
- $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $classId = $orderItem->classId ?? 0;
- $unitPrice = $orderItem->unitPrice ?? 0;
- $refundNum = $orderItem->refundNum ?? 0;
- $num = $orderItem->num ?? 0;
- $productId = $orderItem->itemId ?? 0;
- $name = $orderItem->name ?? '';
- $remainNum = bcsub($num, $refundNum);
- $amount = bcmul($remainNum, $unitPrice, 2);
- $className = $classList[$classId]['name'] ?? '已删除';
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['income'])) {
- $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
- $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = 0;
- $arr[$classId]['income'] = $amount;
- $arr[$classId]['saleNum'] = $remainNum;
- $arr[$classId]['cgNum'] = 0;
- }
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 2) {
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- $totalIncome = 0;
- $totalExpend = 0;
- if (!empty($arr)) {
- foreach ($arr as $key => $val) {
- $income = $val['income'] ?? 0;
- $income = floatval($income);
- $totalIncome = bcadd($totalIncome, $income, 2);
- $expend = $val['expend'] ?? 0;
- $expend = floatval($expend);
- $totalExpend = bcadd($totalExpend, $expend, 2);
- $profit = bcsub($income, $expend, 2);
- $arr[$key]['profit'] = floatval($profit);
- $arr[$key]['income'] = floatval($income);
- $arr[$key]['expend'] = floatval($expend);
- }
- }
- //总成本算上物流打包等费用
- $totalExpend = bcadd($totalExpend, $packFreight, 2);
- //总收入算上采购仅退款
- $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
- //总收入加上人工费
- $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
- //总收入减掉优惠金额
- $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
- //总成本算上销售仅退款
- $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
- $totalProfit = bcsub($totalIncome, $totalExpend, 2);
- $totalIncome = floatval($totalIncome);
- $totalExpend = floatval($totalExpend);
- $totalProfit = floatval($totalProfit);
- $arr = array_values($arr);
- $arr = arrayUtil::arraySort($arr, 'profit');
- if ($export == 1) {
- $statData = [
- ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
- ];
- $list = array_merge($arr, $statData);
- self::exportIncomeClass($list, $mainId);
- }
- util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
- 'totalIncome' => $totalIncome,
- 'totalExpend' => $totalExpend,
- 'totalProfit' => $totalProfit,
- 'tbSaleIncome' => $tbSaleIncome,
- 'tbCgIncome' => $tbCgIncome,
- 'tbCgIncomeStaff' => $tbCgIncomeStaff,
- 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
- 'totalLabourCost' => $totalLabourCost,
- 'totalDiscountAmount' => $totalDiscountAmount,
- ]);
- }
- public static function exportIncomeOutItem($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
- $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '收支花材-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function exportIncomeClass($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
- $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- }
- $fileName = '收支分类-' . time();
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function exportItemSale($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '花材销量-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function send($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号699');
- }
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $arr = [];
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
- $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
- if (isset($arr[$sendStaffId])) {
- $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
- $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
- } else {
- $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
- }
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $refundNum = $orderItem->refundNum ?? 0;
- $num = $orderItem->xhNum ?? 0;
- $remainNum = bcsub($num, $refundNum);
- $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
- }
- }
- }
- }
- if (!empty($arr)) {
- $arr = array_values($arr);
- }
- return $arr;
- }
- public static function customDistStat($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,distId', 'id');
- $distMap = DistClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
- $arr = [];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $totalAmount = 0;
- $totalNum = 0;
- $totalProfit = 0;
- $totalCost = 0;
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $cost = $ghsOrder['remainCost'] ?? 0;
- $profit = bcsub($actPrice, $cost, 2);
- $orderPrice = $ghsOrder['orderPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $customId = $ghsOrder['customId'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- $book = $ghsOrder['book'] ?? 0;
- //扎数
- $count = $ghsOrder['itemNum'] ?? 0;
- if ($status == 1 || $status == 5) {
- continue;
- }
- if ($book == 1 && $status == 2) {
- continue;
- }
- $distId = $customList[$customId]['distId'] ?? 0;
- $distName = isset($distMap[$distId]) ? $distMap[$distId]['name'] : '未划片区';
- $totalNum++;
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (isset($arr[$distId])) {
- $arr[$distId]['num']++;
- $arr[$distId]['amount'] = bcadd($arr[$distId]['amount'], $actPrice, 2);
- $arr[$distId]['orderPrice'] = bcadd($arr[$distId]['orderPrice'], $orderPrice, 2);
- $arr[$distId]['count'] = bcadd($arr[$distId]['count'], $count, 2);
- $arr[$distId]['tkPrice'] = bcadd($arr[$distId]['tkPrice'], $tkPrice, 2);
- $arr[$distId]['cost'] = bcadd($arr[$distId]['cost'], $cost, 2);
- $arr[$distId]['profit'] = bcadd($arr[$distId]['profit'], $profit, 2);
- } else {
- $arr[$distId] = [
- 'distId' => $distId,
- 'distName' => $distName,
- 'num' => 1,
- 'count' => $count,
- 'amount' => $actPrice,
- 'levelName' => '',
- 'orderPrice' => $orderPrice,
- 'tkPrice' => $tkPrice,
- 'cost' => $cost,
- 'profit' => $profit,
- ];
- }
- }
- $arr = arrayUtil::arraySort($arr, 'profit');
- }
- $totalAmount = floatval($totalAmount);
- return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
- }
- }
|